Skip to content

Commit

Permalink
now using ThingHandlerService
Browse files Browse the repository at this point in the history
Signed-off-by: Massimo Valla <[email protected]>
  • Loading branch information
mvalla committed Aug 7, 2020
1 parent 6eaa140 commit 68b156c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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.handler.config.OpenWebNetBusBridgeConfig;
Expand Down Expand Up @@ -67,7 +68,7 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.BRIDGE_SUPPORTED_THING_TYPES;

// ConcurrentHashMap of devices registered to this BridgeHandler
// Association is: ownId (String) -> OpenWebNetThingHandler, with ownId = WHO.WHERE
// association is: ownId (String) -> OpenWebNetThingHandler, with ownId = WHO.WHERE
private Map<String, @Nullable OpenWebNetThingHandler> registeredDevices = new ConcurrentHashMap<>();

protected @Nullable OpenGateway gateway;
Expand All @@ -84,11 +85,6 @@ public OpenWebNetBridgeHandler(Bridge bridge) {
super(bridge);
}

@Nullable
public OpenGateway getGateway() {
return gateway;
}

public boolean isBusGateway() {
return isBusGateway;
}
Expand Down Expand Up @@ -215,6 +211,11 @@ private void disconnectGateway() {
reconnecting = false;
}

@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(OpenWebNetDeviceDiscoveryService.class);
}

/**
* Search for devices connected to this bridge handler's gateway
*
Expand Down Expand Up @@ -248,16 +249,15 @@ public synchronized void searchDevices() {

@Override
public void onNewDevice(@Nullable Where w, @Nullable OpenDeviceType deviceType, @Nullable BaseOpenMessage message) {
try {
OpenWebNetDeviceDiscoveryService service = deviceDiscoveryService;
if (w != null && deviceType != null && service != null) {
service.newDiscoveryResult(w, deviceType, message);
OpenWebNetDeviceDiscoveryService discService = deviceDiscoveryService;
if (discService != null) {
if (w != null && deviceType != null) {
discService.newDiscoveryResult(w, deviceType, message);
} else {
logger.warn("onNewDevice with null where/deviceType msg={}", message);
logger.warn("onNewDevice with null where/deviceType, msg={}", message);
}
} catch (RuntimeException e) {
logger.warn("Exception while discovering new device WHERE={}, deviceType={}: {}", w, deviceType,
e.getMessage());
} else {
logger.warn("onNewDevice but null deviceDiscoveryService");
}
}

Expand All @@ -276,16 +276,22 @@ public void scanStopped() {

private void discoverByActivation(BaseOpenMessage baseMsg) {
logger.debug("BridgeHandler.discoverByActivation() msg={}", baseMsg);
OpenWebNetDeviceDiscoveryService discService = deviceDiscoveryService;
if (discService == null) {
logger.warn("discoverByActivation: null OpenWebNetDeviceDiscoveryService, ignoring msg={}", baseMsg);
return;
}
if (baseMsg instanceof Lighting) {
OpenDeviceType type = null;
try {
type = baseMsg.detectDeviceType();
} catch (FrameException e) {
logger.warn("Exception while detecting device type: {}", e.getMessage());
}
OpenWebNetDeviceDiscoveryService service = deviceDiscoveryService;
if (type != null && service != null) {
service.newDiscoveryResult(baseMsg.getWhere(), type, baseMsg);
if (type != null) {
discService.newDiscoveryResult(baseMsg.getWhere(), type, baseMsg);
} else {
logger.debug("discoverByActivation: no device type detected from msg: {}", baseMsg);
}
}
}
Expand Down Expand Up @@ -321,7 +327,7 @@ protected void unregisterDevice(String oId) {
public void onEventMessage(@Nullable OpenMessage msg) {
logger.trace("RECEIVED <<<<< {}", msg);
if (msg == null) {
logger.debug("received msg is null");
logger.warn("received event msg is null");
return;
}
if (msg.isACK() || msg.isNACK()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,23 @@

import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.ALL_SUPPORTED_THING_TYPES;

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;
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.openhab.binding.openwebnet.handler.OpenWebNetBridgeHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetGenericHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetLightingHandler;
import org.openhab.binding.openwebnet.internal.discovery.OpenWebNetDeviceDiscoveryService;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link OpenWebNetHandlerFactory} is responsible for creating things and thing
* handlers.
* The {@link OpenWebNetHandlerFactory} is responsible for creating thing handlers.
*
* @author Massimo Valla - Initial contribution
*/
Expand All @@ -49,8 +40,6 @@ public class OpenWebNetHandlerFactory extends BaseThingHandlerFactory {

private final Logger logger = LoggerFactory.getLogger(OpenWebNetHandlerFactory.class);

private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return ALL_SUPPORTED_THING_TYPES.contains(thingTypeUID);
Expand All @@ -60,9 +49,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
protected @Nullable ThingHandler createHandler(Thing thing) {
if (OpenWebNetBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
logger.debug("creating NEW BRIDGE Handler");
OpenWebNetBridgeHandler handler = new OpenWebNetBridgeHandler((Bridge) thing);
registerDiscoveryService(handler);
return handler;
return new OpenWebNetBridgeHandler((Bridge) thing);
} else if (OpenWebNetGenericHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
logger.debug("creating NEW GENERIC Handler");
return new OpenWebNetGenericHandler(thing);
Expand All @@ -74,23 +61,4 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return null;
}

private void registerDiscoveryService(OpenWebNetBridgeHandler bridgeHandler) {
OpenWebNetDeviceDiscoveryService deviceDiscoveryService = new OpenWebNetDeviceDiscoveryService(bridgeHandler);
bridgeHandler.deviceDiscoveryService = deviceDiscoveryService;
this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), bundleContext.registerService(
DiscoveryService.class.getName(), deviceDiscoveryService, new Hashtable<String, Object>()));
}

@SuppressWarnings("null")
@Override
protected synchronized void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof OpenWebNetBridgeHandler) {
// remove discovery service, if bridge handler is removed
ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
if (serviceReg != null) {
serviceReg.unregister();
discoveryServiceRegs.remove(thingHandler.getThing().getUID());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
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.openwebnet4j.OpenDeviceType;
Expand All @@ -40,20 +43,19 @@
* @author Massimo Valla - Initial contribution
*/
@NonNullByDefault
public class OpenWebNetDeviceDiscoveryService extends AbstractDiscoveryService {
public class OpenWebNetDeviceDiscoveryService extends AbstractDiscoveryService
implements DiscoveryService, ThingHandlerService {

private final Logger logger = LoggerFactory.getLogger(OpenWebNetDeviceDiscoveryService.class);

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.DEVICE_SUPPORTED_THING_TYPES;
private static final int SEARCH_TIME_SEC = 60;

private final OpenWebNetBridgeHandler bridgeHandler;
private final ThingUID bridgeUID;
private @NonNullByDefault({}) OpenWebNetBridgeHandler bridgeHandler;
private @NonNullByDefault({}) ThingUID bridgeUID;

public OpenWebNetDeviceDiscoveryService(OpenWebNetBridgeHandler handler) {
super(SEARCH_TIME_SEC);
bridgeHandler = handler;
bridgeUID = handler.getThing().getUID();
public OpenWebNetDeviceDiscoveryService() {
super(SUPPORTED_THING_TYPES, SEARCH_TIME_SEC);
}

@Override
Expand Down Expand Up @@ -145,7 +147,6 @@ public void newDiscoveryResult(Where where, OpenDeviceType deviceType, @Nullable
properties.put(OpenWebNetBindingConstants.CONFIG_PROPERTY_WHERE, bridgeHandler.normalizeWhere(where));
properties.put(OpenWebNetBindingConstants.PROPERTY_OWNID, bridgeHandler.ownIdFromWhoWhere(where, deviceWho));
if (thingTypeUID == OpenWebNetBindingConstants.THING_TYPE_GENERIC_DEVICE) {
// generic thing
thingLabel = thingLabel + " (WHO=" + deviceWho + ", WHERE=" + whereLabel + ")";
} else {
thingLabel = thingLabel + " (WHERE=" + whereLabel + ")";
Expand All @@ -155,4 +156,24 @@ public void newDiscoveryResult(Where where, OpenDeviceType deviceType, @Nullable
.withLabel(thingLabel).build();
thingDiscovered(discoveryResult);
}

@Override
public void deactivate() {
super.deactivate();
}

@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof OpenWebNetBridgeHandler) {
logger.debug("attaching {} to handler {} ", this, handler);
bridgeHandler = (OpenWebNetBridgeHandler) handler;
bridgeHandler.deviceDiscoveryService = this;
bridgeUID = bridgeHandler.getThing().getUID();
}
}

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

0 comments on commit 68b156c

Please sign in to comment.