From 9959efcd4ac92ae595e429a3243faaab023ddce9 Mon Sep 17 00:00:00 2001 From: Ondrej Pecta Date: Thu, 10 Sep 2020 08:22:51 +0200 Subject: [PATCH] [somfytahoma] improved discovery service registration (#8439) Signed-off-by: Ondrej Pecta --- .../internal/SomfyTahomaHandlerFactory.java | 29 +---------- .../SomfyTahomaItemDiscoveryService.java | 49 +++++++++++-------- .../handler/SomfyTahomaBaseThingHandler.java | 2 +- .../handler/SomfyTahomaBridgeHandler.java | 11 +++-- 4 files changed, 37 insertions(+), 54 deletions(-) diff --git a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/SomfyTahomaHandlerFactory.java b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/SomfyTahomaHandlerFactory.java index 2a8faebfefc03..e8675dd6ed627 100644 --- a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/SomfyTahomaHandlerFactory.java +++ b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/SomfyTahomaHandlerFactory.java @@ -15,12 +15,10 @@ import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*; import java.util.HashMap; -import java.util.Hashtable; import java.util.Map; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -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; @@ -29,7 +27,6 @@ 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.somfytahoma.internal.discovery.SomfyTahomaItemDiscoveryService; import org.openhab.binding.somfytahoma.internal.handler.*; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.annotations.Activate; @@ -72,9 +69,7 @@ protected ThingHandler createHandler(Thing thing) { logger.debug("Creating handler for {}", thing.getThingTypeUID().getId()); if (thingTypeUID.equals(THING_TYPE_BRIDGE)) { - SomfyTahomaBridgeHandler handler = new SomfyTahomaBridgeHandler((Bridge) thing, httpClientFactory); - registerItemDiscoveryService(handler); - return handler; + return new SomfyTahomaBridgeHandler((Bridge) thing, httpClientFactory); } else if (thingTypeUID.equals(THING_TYPE_GATEWAY)) { return new SomfyTahomaGatewayHandler(thing); } else if (thingTypeUID.equals(THING_TYPE_ROLLERSHUTTER)) { @@ -156,26 +151,4 @@ protected ThingHandler createHandler(Thing thing) { return null; } } - - @Override - protected synchronized void removeHandler(ThingHandler thingHandler) { - if (thingHandler instanceof SomfyTahomaBridgeHandler) { - ServiceRegistration serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID()); - // remove discovery service, if bridge handler is removed - SomfyTahomaItemDiscoveryService service = (SomfyTahomaItemDiscoveryService) bundleContext - .getService(serviceReg.getReference()); - if (service != null) { - service.deactivate(); - } - serviceReg.unregister(); - discoveryServiceRegs.remove(thingHandler.getThing().getUID()); - } - } - - private synchronized void registerItemDiscoveryService(SomfyTahomaBridgeHandler bridgeHandler) { - SomfyTahomaItemDiscoveryService discoveryService = new SomfyTahomaItemDiscoveryService(bridgeHandler); - discoveryService.activate(null); - this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), - bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>())); - } } diff --git a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/discovery/SomfyTahomaItemDiscoveryService.java b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/discovery/SomfyTahomaItemDiscoveryService.java index b0654a2e06c5b..b498fb24f27de 100644 --- a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/discovery/SomfyTahomaItemDiscoveryService.java +++ b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/discovery/SomfyTahomaItemDiscoveryService.java @@ -25,17 +25,18 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService; import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder; +import org.eclipse.smarthome.config.discovery.DiscoveryService; import org.eclipse.smarthome.core.thing.ThingStatus; import org.eclipse.smarthome.core.thing.ThingTypeUID; import org.eclipse.smarthome.core.thing.ThingUID; +import org.eclipse.smarthome.core.thing.binding.ThingHandler; +import org.eclipse.smarthome.core.thing.binding.ThingHandlerService; import org.openhab.binding.somfytahoma.internal.handler.SomfyTahomaBridgeHandler; import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaActionGroup; import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaDevice; import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaGateway; import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaSetup; import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaState; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Deactivate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,38 +47,45 @@ * @author Ondrej Pecta - Initial contribution */ @NonNullByDefault -public class SomfyTahomaItemDiscoveryService extends AbstractDiscoveryService { +public class SomfyTahomaItemDiscoveryService extends AbstractDiscoveryService + implements DiscoveryService, ThingHandlerService { private final Logger logger = LoggerFactory.getLogger(SomfyTahomaItemDiscoveryService.class); - private SomfyTahomaBridgeHandler bridge; + private @Nullable SomfyTahomaBridgeHandler bridgeHandler; private @Nullable ScheduledFuture discoveryJob; private static final int DISCOVERY_TIMEOUT_SEC = 10; private static final int DISCOVERY_REFRESH_SEC = 3600; - public SomfyTahomaItemDiscoveryService(SomfyTahomaBridgeHandler bridgeHandler) { + public SomfyTahomaItemDiscoveryService() { super(DISCOVERY_TIMEOUT_SEC); logger.debug("Creating discovery service"); - this.bridge = bridgeHandler; } - /** - * Called on component activation. - */ @Override - @Activate - public void activate(@Nullable Map configProperties) { - super.activate(configProperties); + public void activate() { + super.activate(null); } @Override - @Deactivate public void deactivate() { super.deactivate(); } + @Override + public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) { + if (handler instanceof SomfyTahomaBridgeHandler) { + bridgeHandler = (SomfyTahomaBridgeHandler) handler; + } + } + + @Override + public @Nullable ThingHandler getThingHandler() { + return bridgeHandler; + } + @Override protected void startBackgroundDiscovery() { logger.debug("Starting SomfyTahoma background discovery"); @@ -110,8 +118,8 @@ protected void startScan() { private synchronized void runDiscovery() { logger.debug("Starting scanning for things..."); - if (bridge.getThing().getStatus().equals(ThingStatus.ONLINE)) { - SomfyTahomaSetup setup = bridge.getSetup(); + if (bridgeHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) { + SomfyTahomaSetup setup = bridgeHandler.getSetup(); if (setup == null) { return; @@ -124,7 +132,7 @@ private synchronized void runDiscovery() { gatewayDiscovered(gw); } - List actions = bridge.listActionGroups(); + List actions = bridgeHandler.listActionGroups(); for (SomfyTahomaActionGroup group : actions) { String oid = group.getOid(); @@ -365,11 +373,12 @@ private void deviceDiscovered(String label, String deviceURL, String oid, ThingT properties.put(RSSI_LEVEL_STATE, "-1"); } - ThingUID thingUID = new ThingUID(thingTypeUID, bridge.getThing().getUID(), oid); + ThingUID thingUID = new ThingUID(thingTypeUID, bridgeHandler.getThing().getUID(), oid); logger.debug("Detected a/an {} - label: {} oid: {}", thingTypeUID.getId(), label, oid); thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withProperties(properties) - .withRepresentationProperty("url").withLabel(label).withBridge(bridge.getThing().getUID()).build()); + .withRepresentationProperty("url").withLabel(label).withBridge(bridgeHandler.getThing().getUID()) + .build()); } private void actionGroupDiscovered(String label, String deviceURL, String oid) { @@ -383,11 +392,11 @@ private void gatewayDiscovered(SomfyTahomaGateway gw) { properties.put("id", id); properties.put("type", type); - ThingUID thingUID = new ThingUID(THING_TYPE_GATEWAY, bridge.getThing().getUID(), id); + ThingUID thingUID = new ThingUID(THING_TYPE_GATEWAY, bridgeHandler.getThing().getUID(), id); logger.debug("Detected a gateway with id: {} and type: {}", id, type); thingDiscovered(DiscoveryResultBuilder.create(thingUID).withThingType(THING_TYPE_GATEWAY) .withProperties(properties).withRepresentationProperty("id").withLabel("Somfy Gateway (" + type + ")") - .withBridge(bridge.getThing().getUID()).build()); + .withBridge(bridgeHandler.getThing().getUID()).build()); } } diff --git a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBaseThingHandler.java b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBaseThingHandler.java index a64ae63d67717..abd752d2593f9 100644 --- a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBaseThingHandler.java +++ b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBaseThingHandler.java @@ -42,7 +42,7 @@ @NonNullByDefault public abstract class SomfyTahomaBaseThingHandler extends BaseThingHandler { - private final Logger logger = LoggerFactory.getLogger(SomfyTahomaBaseThingHandler.class); + private final Logger logger = LoggerFactory.getLogger(getClass()); private HashMap typeTable = new HashMap<>(); protected HashMap stateNames = new HashMap<>(); diff --git a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java index 97e3297258bae..4dd4ebb272799 100644 --- a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java +++ b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java @@ -32,12 +32,13 @@ import org.eclipse.jetty.client.util.StringContentProvider; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; -import org.eclipse.smarthome.config.core.status.ConfigStatusMessage; import org.eclipse.smarthome.core.thing.*; -import org.eclipse.smarthome.core.thing.binding.ConfigStatusBridgeHandler; +import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler; +import org.eclipse.smarthome.core.thing.binding.ThingHandlerService; import org.eclipse.smarthome.core.types.Command; import org.eclipse.smarthome.io.net.http.HttpClientFactory; import org.openhab.binding.somfytahoma.internal.config.SomfyTahomaConfig; +import org.openhab.binding.somfytahoma.internal.discovery.SomfyTahomaItemDiscoveryService; import org.openhab.binding.somfytahoma.internal.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +54,7 @@ * @author Ondrej Pecta - Initial contribution */ @NonNullByDefault -public class SomfyTahomaBridgeHandler extends ConfigStatusBridgeHandler { +public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { private final Logger logger = LoggerFactory.getLogger(SomfyTahomaBridgeHandler.class); @@ -307,8 +308,8 @@ public void handleRemoval() { } @Override - public Collection getConfigStatus() { - return Collections.emptyList(); + public Collection> getServices() { + return Collections.singleton(SomfyTahomaItemDiscoveryService.class); } @Override