[rfxcom) Use ThingHandlerService for discovery (#9082)
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
parent
1797b2e245
commit
e58a0469ad
@ -12,6 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.rfxcom.internal;
|
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.exceptions.RFXComException;
|
||||||
import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
|
import org.openhab.binding.rfxcom.internal.messages.RFXComDeviceMessage;
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
@ -22,6 +23,7 @@ import org.openhab.core.thing.ThingUID;
|
|||||||
*
|
*
|
||||||
* @author Pauli Anttila - Initial contribution
|
* @author Pauli Anttila - Initial contribution
|
||||||
*/
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
public interface DeviceMessageListener {
|
public interface DeviceMessageListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -12,28 +12,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.rfxcom.internal;
|
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.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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.RFXComBridgeHandler;
|
||||||
import org.openhab.binding.rfxcom.internal.handler.RFXComHandler;
|
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.io.transport.serial.SerialPortManager;
|
||||||
import org.openhab.core.thing.Bridge;
|
import org.openhab.core.thing.Bridge;
|
||||||
import org.openhab.core.thing.Thing;
|
import org.openhab.core.thing.Thing;
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
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.BaseThingHandlerFactory;
|
||||||
import org.openhab.core.thing.binding.ThingHandler;
|
import org.openhab.core.thing.binding.ThingHandler;
|
||||||
import org.openhab.core.thing.binding.ThingHandlerFactory;
|
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.Activate;
|
||||||
import org.osgi.service.component.annotations.Component;
|
import org.osgi.service.component.annotations.Component;
|
||||||
import org.osgi.service.component.annotations.Reference;
|
import org.osgi.service.component.annotations.Reference;
|
||||||
@ -53,11 +46,6 @@ public class RFXComHandlerFactory extends BaseThingHandlerFactory {
|
|||||||
RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.stream())
|
RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.stream())
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
/**
|
|
||||||
* Service registration map
|
|
||||||
*/
|
|
||||||
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
|
|
||||||
|
|
||||||
private final SerialPortManager serialPortManager;
|
private final SerialPortManager serialPortManager;
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
@ -75,39 +63,11 @@ public class RFXComHandlerFactory extends BaseThingHandlerFactory {
|
|||||||
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
||||||
|
|
||||||
if (RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
|
if (RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
|
||||||
RFXComBridgeHandler handler = new RFXComBridgeHandler((Bridge) thing, serialPortManager);
|
return new RFXComBridgeHandler((Bridge) thing, serialPortManager);
|
||||||
registerDeviceDiscoveryService(handler);
|
|
||||||
return handler;
|
|
||||||
} else if (supportsThingType(thingTypeUID)) {
|
} else if (supportsThingType(thingTypeUID)) {
|
||||||
return new RFXComHandler(thing);
|
return new RFXComHandler(thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,8 @@ import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.ID_
|
|||||||
|
|
||||||
import java.util.Set;
|
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.DeviceMessageListener;
|
||||||
import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
|
import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
|
||||||
import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
|
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.config.discovery.DiscoveryResultBuilder;
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
import org.openhab.core.thing.ThingTypeUID;
|
||||||
import org.openhab.core.thing.ThingUID;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -33,25 +37,48 @@ import org.slf4j.LoggerFactory;
|
|||||||
* devices that send messages to RFXCOM bridge.
|
* devices that send messages to RFXCOM bridge.
|
||||||
*
|
*
|
||||||
* @author Pauli Anttila - Initial contribution
|
* @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 Logger logger = LoggerFactory.getLogger(RFXComDeviceDiscoveryService.class);
|
||||||
private final int DISCOVERY_TTL = 3600;
|
private final int DISCOVERY_TTL = 3600;
|
||||||
|
|
||||||
private RFXComBridgeHandler bridgeHandler;
|
private @Nullable RFXComBridgeHandler bridgeHandler;
|
||||||
|
|
||||||
public RFXComDeviceDiscoveryService(RFXComBridgeHandler rfxcomBridgeHandler) {
|
public RFXComDeviceDiscoveryService() {
|
||||||
super(null, 1, false);
|
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() {
|
public void activate() {
|
||||||
bridgeHandler.registerDeviceStatusListener(this);
|
super.activate(null);
|
||||||
|
RFXComBridgeHandler handler = bridgeHandler;
|
||||||
|
if (handler != null) {
|
||||||
|
handler.registerDeviceStatusListener(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deactivate() {
|
public void deactivate() {
|
||||||
bridgeHandler.unregisterDeviceStatusListener(this);
|
RFXComBridgeHandler handler = bridgeHandler;
|
||||||
|
if (handler != null) {
|
||||||
|
handler.unregisterDeviceStatusListener(this);
|
||||||
|
}
|
||||||
|
super.deactivate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -76,7 +103,10 @@ public class RFXComDeviceDiscoveryService extends AbstractDiscoveryService imple
|
|||||||
}
|
}
|
||||||
ThingUID thingUID = new ThingUID(uid, bridge, id.replace(ID_DELIMITER, "_"));
|
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);
|
logger.trace("Adding new RFXCOM {} with id '{}' to smarthome inbox", thingUID, id);
|
||||||
DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(thingUID).withBridge(bridge)
|
DiscoveryResultBuilder discoveryResultBuilder = DiscoveryResultBuilder.create(thingUID).withBridge(bridge)
|
||||||
.withTTL(DISCOVERY_TTL);
|
.withTTL(DISCOVERY_TTL);
|
||||||
|
|||||||
@ -13,6 +13,8 @@
|
|||||||
package org.openhab.binding.rfxcom.internal.handler;
|
package org.openhab.binding.rfxcom.internal.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
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.RFXComJD2XXConnector;
|
||||||
import org.openhab.binding.rfxcom.internal.connector.RFXComSerialConnector;
|
import org.openhab.binding.rfxcom.internal.connector.RFXComSerialConnector;
|
||||||
import org.openhab.binding.rfxcom.internal.connector.RFXComTcpConnector;
|
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.RFXComException;
|
||||||
import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageNotImplementedException;
|
import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageNotImplementedException;
|
||||||
import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage;
|
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.ThingStatus;
|
||||||
import org.openhab.core.thing.ThingStatusDetail;
|
import org.openhab.core.thing.ThingStatusDetail;
|
||||||
import org.openhab.core.thing.binding.BaseBridgeHandler;
|
import org.openhab.core.thing.binding.BaseBridgeHandler;
|
||||||
|
import org.openhab.core.thing.binding.ThingHandlerService;
|
||||||
import org.openhab.core.types.Command;
|
import org.openhab.core.types.Command;
|
||||||
import org.openhab.core.util.HexUtils;
|
import org.openhab.core.util.HexUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -114,6 +118,11 @@ public class RFXComBridgeHandler extends BaseBridgeHandler {
|
|||||||
this.serialPortManager = serialPortManager;
|
this.serialPortManager = serialPortManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Class<? extends ThingHandlerService>> getServices() {
|
||||||
|
return Collections.singleton(RFXComDeviceDiscoveryService.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCommand(ChannelUID channelUID, Command command) {
|
public void handleCommand(ChannelUID channelUID, Command command) {
|
||||||
logger.debug("Bridge commands not supported.");
|
logger.debug("Bridge commands not supported.");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user