]> git.basschouten.com Git - openhab-addons.git/commitdiff
[rfxcom) Use ThingHandlerService for discovery (#9082)
authorlolodomo <lg.hc@free.fr>
Sat, 21 Nov 2020 03:16:55 +0000 (04:16 +0100)
committerGitHub <noreply@github.com>
Sat, 21 Nov 2020 03:16:55 +0000 (19:16 -0800)
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/DeviceMessageListener.java
bundles/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/RFXComHandlerFactory.java
bundles/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/discovery/RFXComDeviceDiscoveryService.java
bundles/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/handler/RFXComBridgeHandler.java

index 81c90ddb59c09576a64e2ce78d156008212feda0..692e710cabfb374f18cbab2e3a79291ec54d27f4 100644 (file)
@@ -12,6 +12,7 @@
  */
 package org.openhab.binding.rfxcom.internal;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
 import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
 import org.openhab.core.thing.ThingUID;
@@ -22,6 +23,7 @@ import org.openhab.core.thing.ThingUID;
  *
  * @author Pauli Anttila - Initial contribution
  */
+@NonNullByDefault
 public interface DeviceMessageListener {
 
     /**
index ebd63177d47dd293d6432bede570c78653a55ca7..f47e8b8497bab97ae0b9a08174c9fa9ebe4736bd 100644 (file)
  */
 package org.openhab.binding.rfxcom.internal;
 
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.binding.rfxcom.internal.discovery.RFXComDeviceDiscoveryService;
 import org.openhab.binding.rfxcom.internal.handler.RFXComBridgeHandler;
 import org.openhab.binding.rfxcom.internal.handler.RFXComHandler;
-import org.openhab.core.config.discovery.DiscoveryService;
 import org.openhab.core.io.transport.serial.SerialPortManager;
 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.framework.ServiceRegistration;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
@@ -53,11 +46,6 @@ public class RFXComHandlerFactory extends BaseThingHandlerFactory {
                     RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.stream())
             .collect(Collectors.toSet());
 
-    /**
-     * Service registration map
-     */
-    private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
-
     private final SerialPortManager serialPortManager;
 
     @Activate
@@ -75,39 +63,11 @@ public class RFXComHandlerFactory extends BaseThingHandlerFactory {
         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
 
         if (RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
-            RFXComBridgeHandler handler = new RFXComBridgeHandler((Bridge) thing, serialPortManager);
-            registerDeviceDiscoveryService(handler);
-            return handler;
+            return new RFXComBridgeHandler((Bridge) thing, serialPortManager);
         } else if (supportsThingType(thingTypeUID)) {
             return new RFXComHandler(thing);
         }
 
         return null;
     }
-
-    @Override
-    protected void removeHandler(ThingHandler thingHandler) {
-        if (thingHandler instanceof RFXComBridgeHandler) {
-            unregisterDeviceDiscoveryService(thingHandler.getThing());
-        }
-    }
-
-    private synchronized void registerDeviceDiscoveryService(RFXComBridgeHandler handler) {
-        RFXComDeviceDiscoveryService discoveryService = new RFXComDeviceDiscoveryService(handler);
-        discoveryService.activate();
-        this.discoveryServiceRegs.put(handler.getThing().getUID(),
-                bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
-    }
-
-    private synchronized void unregisterDeviceDiscoveryService(Thing thing) {
-        ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
-        if (serviceReg != null) {
-            RFXComDeviceDiscoveryService service = (RFXComDeviceDiscoveryService) bundleContext
-                    .getService(serviceReg.getReference());
-            serviceReg.unregister();
-            if (service != null) {
-                service.deactivate();
-            }
-        }
-    }
 }
index 48f698218a01ba729ecceec4016a906d7b9a41bb..2055b76b4e8fbb99d4da0f15b34a9bb87fc7ae8d 100644 (file)
@@ -16,6 +16,8 @@ import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.ID_
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.rfxcom.internal.DeviceMessageListener;
 import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
@@ -25,6 +27,8 @@ import org.openhab.core.config.discovery.AbstractDiscoveryService;
 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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -33,25 +37,48 @@ import org.slf4j.LoggerFactory;
  * devices that send messages to RFXCOM bridge.
  *
  * @author Pauli Anttila - Initial contribution
+ * @author Laurent Garnier - use ThingHandlerService
  */
-public class RFXComDeviceDiscoveryService extends AbstractDiscoveryService implements DeviceMessageListener {
+@NonNullByDefault
+public class RFXComDeviceDiscoveryService extends AbstractDiscoveryService
+        implements DeviceMessageListener, ThingHandlerService {
     private final Logger logger = LoggerFactory.getLogger(RFXComDeviceDiscoveryService.class);
     private final int DISCOVERY_TTL = 3600;
 
-    private RFXComBridgeHandler bridgeHandler;
+    private @Nullable RFXComBridgeHandler bridgeHandler;
 
-    public RFXComDeviceDiscoveryService(RFXComBridgeHandler rfxcomBridgeHandler) {
+    public RFXComDeviceDiscoveryService() {
         super(null, 1, false);
-        this.bridgeHandler = rfxcomBridgeHandler;
     }
 
+    @Override
+    public void setThingHandler(ThingHandler handler) {
+        if (handler instanceof RFXComBridgeHandler) {
+            bridgeHandler = (RFXComBridgeHandler) handler;
+        }
+    }
+
+    @Override
+    public @Nullable ThingHandler getThingHandler() {
+        return bridgeHandler;
+    }
+
+    @Override
     public void activate() {
-        bridgeHandler.registerDeviceStatusListener(this);
+        super.activate(null);
+        RFXComBridgeHandler handler = bridgeHandler;
+        if (handler != null) {
+            handler.registerDeviceStatusListener(this);
+        }
     }
 
     @Override
     public void deactivate() {
-        bridgeHandler.unregisterDeviceStatusListener(this);
+        RFXComBridgeHandler handler = bridgeHandler;
+        if (handler != null) {
+            handler.unregisterDeviceStatusListener(this);
+        }
+        super.deactivate();
     }
 
     @Override
@@ -76,7 +103,10 @@ public class RFXComDeviceDiscoveryService extends AbstractDiscoveryService imple
         }
         ThingUID thingUID = new ThingUID(uid, bridge, id.replace(ID_DELIMITER, "_"));
 
-        if (!bridgeHandler.getConfiguration().disableDiscovery) {
+        RFXComBridgeHandler handler = bridgeHandler;
+        if (handler == null) {
+            logger.trace("Ignoring RFXCOM {} with id '{}' - bridge handler is null", thingUID, id);
+        } else if (!handler.getConfiguration().disableDiscovery) {
             logger.trace("Adding new RFXCOM {} with id '{}' to smarthome inbox", thingUID, id);
             DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(thingUID).withBridge(bridge)
                     .withTTL(DISCOVERY_TTL);
index 2adc3db362784617edf83d19e96ac8c7f79c1f08..81854128c81afc0080c4f0954534aec8659970e1 100644 (file)
@@ -13,6 +13,8 @@
 package org.openhab.binding.rfxcom.internal.handler;
 
 import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Queue;
 import java.util.concurrent.CopyOnWriteArrayList;
@@ -28,6 +30,7 @@ import org.openhab.binding.rfxcom.internal.connector.RFXComEventListener;
 import org.openhab.binding.rfxcom.internal.connector.RFXComJD2XXConnector;
 import org.openhab.binding.rfxcom.internal.connector.RFXComSerialConnector;
 import org.openhab.binding.rfxcom.internal.connector.RFXComTcpConnector;
+import org.openhab.binding.rfxcom.internal.discovery.RFXComDeviceDiscoveryService;
 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
 import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageNotImplementedException;
 import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage;
@@ -46,6 +49,7 @@ import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
 import org.openhab.core.thing.binding.BaseBridgeHandler;
+import org.openhab.core.thing.binding.ThingHandlerService;
 import org.openhab.core.types.Command;
 import org.openhab.core.util.HexUtils;
 import org.slf4j.Logger;
@@ -114,6 +118,11 @@ public class RFXComBridgeHandler extends BaseBridgeHandler {
         this.serialPortManager = serialPortManager;
     }
 
+    @Override
+    public Collection<Class<? extends ThingHandlerService>> getServices() {
+        return Collections.singleton(RFXComDeviceDiscoveryService.class);
+    }
+
     @Override
     public void handleCommand(ChannelUID channelUID, Command command) {
         logger.debug("Bridge commands not supported.");