Skip to content

Commit

Permalink
migrate to OH3 and improve discovery registration
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed Sep 13, 2020
1 parent 8da8b44 commit 1aebd35
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 192 deletions.
33 changes: 0 additions & 33 deletions bundles/org.openhab.binding.tr064/.classpath

This file was deleted.

23 changes: 0 additions & 23 deletions bundles/org.openhab.binding.tr064/.project

This file was deleted.

12 changes: 1 addition & 11 deletions bundles/org.openhab.binding.tr064/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>2.5.7-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.tr064</artifactId>
Expand All @@ -16,16 +16,6 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<executions>
<execution>
<id>karaf-feature-verification</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.freebox-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<features name="org.openhab.binding.tr064-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-binding-tr064" description="TR-064 Binding" version="${project.version}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import javax.net.ssl.X509ExtendedTrustManager;

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.smarthome.core.cache.ExpiringCacheMap;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.tr064.internal.config.Tr064ChannelConfig;
import org.openhab.binding.tr064.internal.dto.config.ChannelTypeDescription;
import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType;
import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDActionType;
import org.openhab.core.cache.ExpiringCacheMap;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.State;
import org.openhab.binding.tr064.internal.config.Tr064ChannelConfig;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.openhab.binding.tr064.internal.dto.config.ChannelTypeDescription;
import org.openhab.binding.tr064.internal.util.Util;
import org.openhab.core.thing.ThingTypeUID;

/**
* The {@link Tr064BindingConstants} class defines common constants, which are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.type.ChannelType;
import org.eclipse.smarthome.core.thing.type.ChannelTypeBuilder;
import org.eclipse.smarthome.core.thing.type.ChannelTypeProvider;
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID;
import org.eclipse.smarthome.core.types.StateDescription;
import org.eclipse.smarthome.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeProvider;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
import java.util.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.util.UIDUtils;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDDeviceType;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
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.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.util.UIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -34,17 +37,27 @@
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class Tr064DiscoveryService extends AbstractDiscoveryService {
public class Tr064DiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
private static final int SEARCH_TIME = 5;
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SUBDEVICE);

private final Logger logger = LoggerFactory.getLogger(Tr064DiscoveryService.class);
private @Nullable Tr064RootHandler bridgeHandler;

private final Tr064RootHandler bridgeHandler;

public Tr064DiscoveryService(Tr064RootHandler bridgeHandler) {
public Tr064DiscoveryService() {
super(SEARCH_TIME);
this.bridgeHandler = bridgeHandler;
}

@Override
public void setThingHandler(ThingHandler thingHandler) {
if (thingHandler instanceof Tr064RootHandler) {
this.bridgeHandler = (Tr064RootHandler) thingHandler;
}
}

@Override
public @Nullable ThingHandler getThingHandler() {
return bridgeHandler;
}

@Override
Expand All @@ -59,19 +72,22 @@ public Set<ThingTypeUID> getSupportedThingTypes() {

@Override
public void startScan() {
Tr064RootHandler bridgeHandler = this.bridgeHandler;
if (bridgeHandler == null) {
logger.warn("Could not start discovery, bridge handler not set");
return;
}
List<SCPDDeviceType> devices = bridgeHandler.getAllSubDevices();
ThingUID bridgeUID = bridgeHandler.getThing().getUID();
devices.forEach(device -> {
logger.trace("Trying to add {} to discovery results on {}", device, bridgeUID);
String udn = device.getUDN();
if (udn != null) {
ThingTypeUID thingTypeUID;
switch (device.getDeviceType()) {
case "urn:dslforum-org:device:LANDevice:1":
thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
break;
default:
thingTypeUID = THING_TYPE_SUBDEVICE;
if ("urn:dslforum-org:device:LANDevice:1".equals(device.getDeviceType())) {
thingTypeUID = THING_TYPE_SUBDEVICE_LAN;
} else {
thingTypeUID = THING_TYPE_SUBDEVICE;
}
ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, UIDUtils.encode(udn));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider;
import org.eclipse.smarthome.core.types.StateDescription;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
import org.openhab.core.types.StateDescription;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.eclipse.smarthome.io.net.http.HttpClientFactory;
import org.openhab.binding.tr064.profile.phonebook.PhonebookProvider;
import org.openhab.binding.tr064.profile.phonebook.PhonebookProfileFactory;
import org.osgi.framework.ServiceRegistration;
import org.openhab.binding.tr064.profile.phonebook.PhonebookProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -58,7 +56,6 @@ public class Tr064HandlerFactory extends BaseThingHandlerFactory {
private @Nullable PhonebookProfileFactory phonebookProfileFactory = null;

private final Set<PhonebookProvider> phonebookProviders = ConcurrentHashMap.newKeySet();
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new ConcurrentHashMap<>();

@Activate
public Tr064HandlerFactory(@Reference HttpClientFactory httpClientFactory,
Expand All @@ -68,14 +65,12 @@ public Tr064HandlerFactory(@Reference HttpClientFactory httpClientFactory,
}

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
public void setPhonebookTransformationProfileFactory(
PhonebookProfileFactory phonebookProfileFactory) {
public void setPhonebookTransformationProfileFactory(PhonebookProfileFactory phonebookProfileFactory) {
this.phonebookProfileFactory = phonebookProfileFactory;
phonebookProviders.forEach(phonebookProfileFactory::registerPhonebookProvider);
}

public void unsetPhonebookTransformationProfileFactory(
PhonebookProfileFactory phonebookProfileFactory) {
public void unsetPhonebookTransformationProfileFactory(PhonebookProfileFactory phonebookProfileFactory) {
this.phonebookProfileFactory = null;
}

Expand All @@ -90,7 +85,6 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {

if (Tr064RootHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
Tr064RootHandler handler = new Tr064RootHandler((Bridge) thing, httpClient);
registerDeviceDiscoveryService(handler);
phonebookProviders.add(handler);
PhonebookProfileFactory phonebookProfileFactory = this.phonebookProfileFactory;
if (phonebookProfileFactory != null) {
Expand All @@ -109,26 +103,11 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
protected void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof Tr064RootHandler) {
final ThingUID thingUID = thingHandler.getThing().getUID();
final ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingUID);
if (serviceReg != null) {
serviceReg.unregister();
}
phonebookProviders.remove(thingHandler);
PhonebookProfileFactory phonebookProfileFactory = this.phonebookProfileFactory;
if (phonebookProfileFactory != null) {
phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);
}
}
}

/**
* create and register a new discovery service for the given bridge handler
*
* @param bridgeHandler the bridgehandler (root device)
*/
private void registerDeviceDiscoveryService(Tr064RootHandler bridgeHandler) {
Tr064DiscoveryService discoveryService = new Tr064DiscoveryService(bridgeHandler);
discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), bundleContext
.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<String, Object>()));
}
}
Loading

0 comments on commit 1aebd35

Please sign in to comment.