From 63d7d30722a508c1ae1ea6bbac288f269f91e2c1 Mon Sep 17 00:00:00 2001 From: Massimo Valla Date: Sat, 16 Nov 2019 12:05:26 +0100 Subject: [PATCH] using ThingHandlerService after code review Signed-off-by: Massimo Valla --- .../handler/OpenWebNetBridgeHandler.java | 7 ++++ .../ZigBeeGatewayDiscoveryService.java | 32 ++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetBridgeHandler.java b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetBridgeHandler.java index 0380da9fac073..13c9477f7f536 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetBridgeHandler.java +++ b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetBridgeHandler.java @@ -32,9 +32,11 @@ import org.eclipse.smarthome.core.thing.ThingStatusDetail; import org.eclipse.smarthome.core.thing.ThingTypeUID; import org.eclipse.smarthome.core.thing.binding.ConfigStatusBridgeHandler; +import org.eclipse.smarthome.core.thing.binding.ThingHandlerService; import org.eclipse.smarthome.core.types.Command; import org.openhab.binding.openwebnet.OpenWebNetBindingConstants; import org.openhab.binding.openwebnet.internal.discovery.OpenWebNetDeviceDiscoveryService; +import org.openhab.binding.openwebnet.internal.discovery.ZigBeeGatewayDiscoveryService; import org.openwebnet.OpenDeviceType; import org.openwebnet.OpenError; import org.openwebnet.OpenGateway; @@ -568,4 +570,9 @@ public String normalizeWhere(String where) { } } + @Override + public Collection> getServices() { + logger.debug("############ getServices()"); + return Collections.singleton(ZigBeeGatewayDiscoveryService.class); + } } diff --git a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/discovery/ZigBeeGatewayDiscoveryService.java b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/discovery/ZigBeeGatewayDiscoveryService.java index d227e8d65d7dd..c61f3cc0bce50 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/discovery/ZigBeeGatewayDiscoveryService.java +++ b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/internal/discovery/ZigBeeGatewayDiscoveryService.java @@ -16,13 +16,17 @@ import java.util.Map; import java.util.Set; +import org.eclipse.jdt.annotation.Nullable; 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.config.discovery.DiscoveryService; 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.openwebnet.OpenWebNetBindingConstants; +import org.openhab.binding.openwebnet.handler.OpenWebNetBridgeHandler; //import org.openhab.binding.openwebnet.handler.OpenWebNetBridgeHandler; import org.openwebnet.OpenError; import org.openwebnet.OpenGatewayZigBee; @@ -41,9 +45,9 @@ * @author Massimo Valla - Initial contribution */ -@Component(service = DiscoveryService.class, configurationPid = "discovery.openwebnet") -public class ZigBeeGatewayDiscoveryService extends AbstractDiscoveryService implements OpenListener { - +@Component(service = DiscoveryService.class, configurationPid = "discovery.openwebent") +public class ZigBeeGatewayDiscoveryService extends AbstractDiscoveryService + implements OpenListener, ThingHandlerService { private final Logger logger = LoggerFactory.getLogger(ZigBeeGatewayDiscoveryService.class); private final static int DISCOVERY_TIMEOUT = 30; // seconds @@ -51,12 +55,14 @@ public class ZigBeeGatewayDiscoveryService extends AbstractDiscoveryService impl // TODO support multiple gateways at the same time private OpenGatewayZigBee zbgateway; private int gatewayZigBeeId = 0; + private OpenWebNetBridgeHandler bridgeHandler; + private ThingUID gatewayUID = null; public ZigBeeGatewayDiscoveryService() { super(OpenWebNetBindingConstants.BRIDGE_SUPPORTED_THING_TYPES, DISCOVERY_TIMEOUT, false); logger.debug( - "\n#############################################################################################\nOWN:BridgeDiscovery== constructor()"); + "\n#############################################################################################\n==OWN:ZigBeeGatewayDiscoveryService== constructor()"); } public ZigBeeGatewayDiscoveryService(int timeout) throws IllegalArgumentException { @@ -177,4 +183,22 @@ public void onMessage(OpenMessage msg) { } + @Override + public void setThingHandler(@Nullable ThingHandler handler) { + if (handler instanceof OpenWebNetBridgeHandler) { + bridgeHandler = (OpenWebNetBridgeHandler) handler; + // gatewayUID = bridgeHandler.getUID(); + } + } + + @Override + public @Nullable ThingHandler getThingHandler() { + return bridgeHandler; + } + + @Override + public void deactivate() { + super.deactivate(); + } + }