Skip to content

Commit

Permalink
fixes and code improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K committed Oct 31, 2020
1 parent afb6138 commit 554708d
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.io.net.http.TlsTrustManagerProvider;
import org.openhab.core.io.net.http.TrustAllTrustMananger;
import org.openhab.core.io.net.http.TrustAllTrustManager;
import org.osgi.service.component.annotations.Component;

/**
Expand All @@ -35,6 +35,6 @@ public String getHostName() {

@Override
public X509ExtendedTrustManager getTrustManager() {
return TrustAllTrustMananger.getInstance();
return TrustAllTrustManager.getInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public SOAPConnector(HttpClient httpClient, String endpointBaseURL) {
* @throws IOException if a problem while writing the SOAP message to the Request occurs
* @throws SOAPException if a problem with creating the SOAP message occurs
*/
@SuppressWarnings("unchecked")
private Request prepareSOAPRequest(SCPDServiceType service, String soapAction, Map<String, String> arguments)
throws IOException, SOAPException {
MessageFactory messageFactory = MessageFactory.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public Optional<State> getStateFromSOAPValue(SOAPMessage soapMessage, String ele
} catch (NoSuchMethodException | IllegalAccessException e) {
logger.warn("Postprocessor {} not found, this most likely is a programming error", postProcessor, e);
} catch (InvocationTargetException e) {
logger.info("Postprocessor {} failed: {}", postProcessor, e.getCause().getMessage());
Throwable cause = e.getCause();
logger.info("Postprocessor {} failed: {}", postProcessor,
cause != null ? cause.getMessage() : e.getMessage());
}
return null;
}).or(Optional::empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.binding.tr064.internal;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -35,9 +34,5 @@ public class Tr064BindingConstants {
public static final ThingTypeUID THING_TYPE_SUBDEVICE = new ThingTypeUID(BINDING_ID, "subdevice");
public static final ThingTypeUID THING_TYPE_SUBDEVICE_LAN = new ThingTypeUID(BINDING_ID, "subdeviceLan");

public static final List<ChannelTypeDescription> CHANNEL_TYPES = new ArrayList<>();

static {
CHANNEL_TYPES.addAll(Util.readXMLChannelConfig());
}
public static final List<ChannelTypeDescription> CHANNEL_TYPES = Util.readXMLChannelConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.BridgeHandler;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.util.UIDUtils;
Expand Down Expand Up @@ -62,6 +63,11 @@ public void setThingHandler(ThingHandler thingHandler) {

@Override
public void deactivate() {
BridgeHandler bridgeHandler = this.bridgeHandler;
if (bridgeHandler == null) {
logger.warn("Bridgehandler not found, could not cleanup discovery results.");
return;
}
removeOlderResults(new Date().getTime(), bridgeHandler.getThing().getUID());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ public class Tr064HandlerFactory extends BaseThingHandlerFactory {
.collect(Collectors.toSet());

private final HttpClient httpClient;
private final Tr064ChannelTypeProvider channelTypeProvider;
private final PhonebookProfileFactory phonebookProfileFactory;

// the Tr064ChannelTypeProvider is needed for creating the channels and
// referenced here to make sure it is available before things are
// initialized
@SuppressWarnings("unused")
private final Tr064ChannelTypeProvider channelTypeProvider;

@Activate
public Tr064HandlerFactory(@Reference HttpClientFactory httpClientFactory,
@Reference Tr064ChannelTypeProvider channelTypeProvider,
Expand Down Expand Up @@ -78,7 +83,6 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
}

@Override
@SuppressWarnings("null")
protected void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof Tr064RootHandler) {
phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public class Tr064RootHandler extends BaseBridgeHandler implements PhonebookProv
}

@Override
@SuppressWarnings("null")
public void handleCommand(ChannelUID channelUID, Command command) {
Tr064ChannelConfig channelConfig = channels.get(channelUID);
if (channelConfig == null) {
Expand All @@ -101,6 +100,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}

if (command instanceof RefreshType) {
SOAPConnector soapConnector = this.soapConnector;
State state = stateCache.putIfAbsentAndGet(channelUID,
() -> soapConnector.getChannelStateFromDevice(channelConfig, channels, stateCache));
if (state != null) {
Expand Down Expand Up @@ -315,27 +315,33 @@ private void installPolling() {
}
}

@SuppressWarnings("unchecked")
private Collection<Phonebook> processPhonebookList(SOAPMessage soapMessagePhonebookList,
SCPDServiceType scpdService) {
SOAPValueConverter soapValueConverter = new SOAPValueConverter(httpClient);
return soapValueConverter.getStateFromSOAPValue(soapMessagePhonebookList, "NewPhonebookList", null)
return (Collection<Phonebook>) soapValueConverter
.getStateFromSOAPValue(soapMessagePhonebookList, "NewPhonebookList", null)
.map(phonebookList -> Arrays.stream(phonebookList.toString().split(","))).orElse(Stream.empty())
.map(index -> {
try {
SOAPMessage soapMessageURL = soapConnector.doSOAPRequest(scpdService, "GetPhonebook",
Map.of("NewPhonebookID", index));
return soapValueConverter.getStateFromSOAPValue(soapMessageURL, "NewPhonebookURL", null)
.map(url -> (Phonebook) new Tr064PhonebookImpl(httpClient, url.toString()))
.orElse(null);
.map(url -> (Phonebook) new Tr064PhonebookImpl(httpClient, url.toString()));
} catch (Tr064CommunicationException e) {
logger.warn("Failed to get phonebook with index {}:", index, e);
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
return Optional.empty();
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
}

private void retrievePhonebooks() {
String serviceId = "urn:X_AVM-DE_OnTel-com:serviceId:X_AVM-DE_OnTel1";
SCPDUtil scpdUtil = this.scpdUtil;
if (scpdUtil == null) {
logger.warn("Cannot find SCPDUtil. This is most likely a programming error.");
return;
}
Optional<SCPDServiceType> scpdService = scpdUtil.getDevice("").flatMap(deviceType -> deviceType.getServiceList()
.stream().filter(service -> service.getServiceId().equals(serviceId)).findFirst());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public void dispose() {
* poll remote device for channel values
*/
private void poll() {
SOAPConnector soapConnector = this.soapConnector;
channels.forEach((channelUID, channelConfig) -> {
if (isLinked(channelUID)) {
State state = stateCache.putIfAbsentAndGet(channelUID, () -> soapConnector == null ? UnDefType.UNDEF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Optional<SCPDDeviceType> getDevice(String udn) {
* @param serviceId the service id
* @return the service
*/
public Optional<@Nullable SCPDScpdType> getService(String serviceId) {
public Optional<SCPDScpdType> getService(String serviceId) {
return Optional.ofNullable(serviceMap.get(serviceId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Util {
/**
* read the channel config from the resource file (static initialization)
*
* @return
* @return a list of all available channel configurations
*/
public static List<ChannelTypeDescription> readXMLChannelConfig() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@
<parameter name="NewDeflectionId" thingParameter="callDeflectionIndices"/>
</setAction>
</channel>
<channel name="wanBlockByIP" label="WAN Block By IP"
description="Block/Unblock WAN access with the given IP.">
<channel name="wanBlockByIP" label="WAN Block By IP" description="Block/Unblock WAN access with the given IP.">
<item type="Switch"/>
<service deviceType="urn:dslforum-org:device:InternetGatewayDevice:1"
serviceId="urn:X_AVM-DE_HostFilter-com:serviceId:X_AVM-DE_HostFilter1"/>
serviceId="urn:X_AVM-DE_HostFilter-com:serviceId:X_AVM-DE_HostFilter1"/>
<getAction name="GetWANAccessByIP" argument="NewDisallow">
<parameter name="NewIPv4Address" thingParameter="wanBlockIPs"/>
</getAction>
Expand Down

0 comments on commit 554708d

Please sign in to comment.