Skip to content

Commit

Permalink
[somfytahoma] improved discovery service registration (openhab#8439)
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Pecta <[email protected]>
  • Loading branch information
octa22 authored and CSchlipp committed Sep 12, 2020
1 parent 2ca09f9 commit 9959efc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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<>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, @Nullable Object> 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");
Expand Down Expand Up @@ -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;
Expand All @@ -124,7 +132,7 @@ private synchronized void runDiscovery() {
gatewayDiscovered(gw);
}

List<SomfyTahomaActionGroup> actions = bridge.listActionGroups();
List<SomfyTahomaActionGroup> actions = bridgeHandler.listActionGroups();

for (SomfyTahomaActionGroup group : actions) {
String oid = group.getOid();
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Integer> typeTable = new HashMap<>();
protected HashMap<String, String> stateNames = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -307,8 +308,8 @@ public void handleRemoval() {
}

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

@Override
Expand Down

0 comments on commit 9959efc

Please sign in to comment.