diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsHandlerFactory.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsHandlerFactory.java index 6e27b88f71364..78635fbcbdda8 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsHandlerFactory.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsHandlerFactory.java @@ -81,11 +81,6 @@ public SmartthingsHandlerFactory(final @Reference HttpService httpService, this.typeRegistry = typeRegistery; } - public void setSmartthingsDiscoveryService(SmartthingsDiscoveryService disco) { - logger.info("disco"); - this.discoService = disco; - } - @Override protected @Nullable ThingHandler createHandler(Thing thing) { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); @@ -101,7 +96,7 @@ public void setSmartthingsDiscoveryService(SmartthingsDiscoveryService disco) { } bridgeHandler = new SmartthingsCloudBridgeHandler((Bridge) thing, this, authService, bundleContext, - httpService, oAuthFactory, httpClientFactory, typeRegistry, discoService); + httpService, oAuthFactory, httpClientFactory, typeRegistry); SmartthingsAccountHandler accountHandler = bridgeHandler; authService.setSmartthingsAccountHandler(accountHandler); @@ -127,16 +122,4 @@ public void setSmartthingsDiscoveryService(SmartthingsDiscoveryService disco) { return null; } - @Nullable - public SmartthingsBridgeHandler getBridgeHandler() { - return bridgeHandler; - } - - @Nullable - public ThingUID getBridgeUID() { - if (bridgeHandler != null) { - return bridgeHandler.getThing().getUID(); - } - return null; - } } diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/discovery/SmartthingsDiscoveryService.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/discovery/SmartthingsDiscoveryService.java index 17c94e2400502..0a33f22f2b9ec 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/discovery/SmartthingsDiscoveryService.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/discovery/SmartthingsDiscoveryService.java @@ -20,7 +20,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.smartthings.internal.SmartthingsBindingConstants; -import org.openhab.binding.smartthings.internal.SmartthingsHandlerFactory; import org.openhab.binding.smartthings.internal.api.SmartthingsApi; import org.openhab.binding.smartthings.internal.dto.SmartthingsCategory; import org.openhab.binding.smartthings.internal.dto.SmartthingsComponent; @@ -33,10 +32,8 @@ import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.config.discovery.DiscoveryService; import org.openhab.core.thing.ThingUID; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Reference; -import org.osgi.service.event.Event; -import org.osgi.service.event.EventHandler; +import org.openhab.core.thing.binding.ThingHandler; +import org.openhab.core.thing.binding.ThingHandlerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,16 +43,16 @@ * @author Bob Raker - Initial contribution */ @NonNullByDefault -@Component(service = { DiscoveryService.class, - EventHandler.class }, configurationPid = "discovery.smartthings", property = "event.topics=org/openhab/binding/smartthings/discovery") -public class SmartthingsDiscoveryService extends AbstractDiscoveryService implements EventHandler { +public class SmartthingsDiscoveryService extends AbstractDiscoveryService + implements DiscoveryService, ThingHandlerService { + private static final int DISCOVERY_TIMEOUT_SEC = 30; private final Pattern findIllegalChars = Pattern.compile("[^A-Za-z0-9_-]"); private final Logger logger = LoggerFactory.getLogger(SmartthingsDiscoveryService.class); - private @Nullable SmartthingsHandlerFactory smartthingsHandlerFactory; + private @Nullable SmartthingsBridgeHandler smartthingsBridgeHandler; private @Nullable SmartthingsTypeRegistry typeRegistry; /* @@ -65,31 +62,10 @@ public SmartthingsDiscoveryService() { super(SmartthingsBindingConstants.SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_SEC); } - @Reference - protected void setSmartthingsTypeRegistry(SmartthingsTypeRegistry typeRegistry) { + public void setSmartthingsTypeRegistry(SmartthingsTypeRegistry typeRegistry) { this.typeRegistry = typeRegistry; } - protected void unsetSmartthingsTypeRegistry(SmartthingsTypeRegistry typeRegistry) { - // Make sure it is this handleFactory that should be unset - if (Objects.equals(this.typeRegistry, typeRegistry)) { - this.typeRegistry = null; - } - } - - @Reference - protected void setSmartthingsHubCommand(SmartthingsHandlerFactory handlerFactory) { - smartthingsHandlerFactory = handlerFactory; - smartthingsHandlerFactory.setSmartthingsDiscoveryService(this); - } - - protected void unsetSmartthingsHubCommand(SmartthingsHandlerFactory handlerFactory) { - // Make sure it is this handleFactory that should be unset - if (Objects.equals(handlerFactory, smartthingsHandlerFactory)) { - this.smartthingsHandlerFactory = null; - } - } - /** * Called from the UI when starting a search. */ @@ -99,10 +75,7 @@ public void startScan() { } public void doScan(Boolean addDevice) { - SmartthingsBridgeHandler bridge = null; - if (smartthingsHandlerFactory != null) { - bridge = smartthingsHandlerFactory.getBridgeHandler(); - } + SmartthingsBridgeHandler bridge = smartthingsBridgeHandler; if (bridge == null) { return; } @@ -147,8 +120,6 @@ public void doScan(Boolean addDevice) { if ("main".equals(compId)) { deviceType = catId; } - - logger.info(""); } } } @@ -196,8 +167,8 @@ private void createDevice(String deviceType, SmartthingsDevice device) { } String deviceNameNoSpaces = name.replaceAll("\\s", "_"); String smartthingsDeviceName = findIllegalChars.matcher(deviceNameNoSpaces).replaceAll(""); - if (smartthingsHandlerFactory != null) { - ThingUID bridgeUid = smartthingsHandlerFactory.getBridgeUID(); + if (smartthingsBridgeHandler != null) { + ThingUID bridgeUid = smartthingsBridgeHandler.getThing().getUID(); String bridgeId = bridgeUid.getId(); String uidStr = String.format("smartthings:%s:%s:%s", deviceType, bridgeId, smartthingsDeviceName); @@ -238,34 +209,20 @@ protected void startBackgroundDiscovery() { protected void stopBackgroundDiscovery() { } - /** - * Handle discovery data returned from the Smartthings hub. - * The data is delivered into the SmartthingServlet. From there it is sent here via the Event service - */ @Override - public void handleEvent(@Nullable Event event) { - if (event == null) { - logger.info("SmartthingsDiscoveryService.handleEvent: event is uexpectedly null"); - return; - } - String topic = event.getTopic(); - String data = (String) event.getProperty("data"); - if (data == null) { - logger.debug("Event received on topic: {} but the data field is null", topic); - return; - } else { - logger.trace("Event received on topic: {}", topic); + public void deactivate() { + } + + @Override + public void setThingHandler(@Nullable ThingHandler handler) { + if (handler instanceof SmartthingsBridgeHandler smartthingsBridgeHandler) { + this.smartthingsBridgeHandler = smartthingsBridgeHandler; + this.smartthingsBridgeHandler.registerDiscoveryListener(this); } + } - // The data returned from the Smartthings hub is a list of strings where each - // element is the data for one device. That device string is another json object - /* - * List devices = new ArrayList<>(); - * devices = gson.fromJson(data, devices.getClass()); - * for (String device : devices) { - * SmartthingsDeviceData deviceData = gson.fromJson(device, SmartthingsDeviceData.class); - * createDevice(Objects.requireNonNull(deviceData)); - * } - */ + @Override + public @Nullable ThingHandler getThingHandler() { + return smartthingsBridgeHandler; } -} +} \ No newline at end of file diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsBridgeHandler.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsBridgeHandler.java index 0e6793e42d988..0c0dd86214f80 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsBridgeHandler.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsBridgeHandler.java @@ -14,7 +14,7 @@ import java.io.IOException; import java.util.Collection; -import java.util.LinkedList; +import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -35,13 +35,13 @@ import org.openhab.core.auth.client.oauth2.OAuthException; import org.openhab.core.auth.client.oauth2.OAuthFactory; import org.openhab.core.auth.client.oauth2.OAuthResponseException; -import org.openhab.core.config.core.status.ConfigStatusMessage; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; -import org.openhab.core.thing.binding.ConfigStatusBridgeHandler; +import org.openhab.core.thing.binding.BaseBridgeHandler; +import org.openhab.core.thing.binding.ThingHandlerService; import org.openhab.core.types.Command; import org.osgi.framework.BundleContext; import org.osgi.service.component.annotations.Reference; @@ -56,7 +56,7 @@ * @author Bob Raker - Initial contribution */ @NonNullByDefault -public abstract class SmartthingsBridgeHandler extends ConfigStatusBridgeHandler +public abstract class SmartthingsBridgeHandler extends BaseBridgeHandler implements SmartthingsAccountHandler, AccessTokenRefreshListener { private final Logger logger = LoggerFactory.getLogger(SmartthingsBridgeHandler.class); @@ -79,8 +79,7 @@ public abstract class SmartthingsBridgeHandler extends ConfigStatusBridgeHandler public SmartthingsBridgeHandler(Bridge bridge, SmartthingsHandlerFactory smartthingsHandlerFactory, SmartthingsAuthService authService, BundleContext bundleContext, HttpService httpService, - OAuthFactory oAuthFactory, HttpClientFactory httpClientFactory, SmartthingsTypeRegistry typeRegistry, - SmartthingsDiscoveryService disco) { + OAuthFactory oAuthFactory, HttpClientFactory httpClientFactory, SmartthingsTypeRegistry typeRegistry) { super(bridge); this.smartthingsHandlerFactory = smartthingsHandlerFactory; @@ -90,7 +89,6 @@ public SmartthingsBridgeHandler(Bridge bridge, SmartthingsHandlerFactory smartth this.authService = authService; this.httpClientFactory = httpClientFactory; this.typeRegistry = typeRegistry; - this.discoService = disco; config = getThing().getConfiguration().as(SmartthingsBridgeConfig.class); } @@ -101,9 +99,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { } @Reference - protected void setSmartthingsDiscoveryService(SmartthingsDiscoveryService disco) { + public void registerDiscoveryListener(SmartthingsDiscoveryService disco) { this.discoService = disco; - logger.info("disco"); + this.discoService.setSmartthingsTypeRegistry(typeRegistry); } @Override @@ -164,13 +162,6 @@ public BundleContext getBundleContext() { return bundleContext; } - @Override - public Collection getConfigStatus() { - Collection configStatusMessages = new LinkedList<>(); - - return configStatusMessages; - } - @Override public boolean isAuthorized() { final AccessTokenResponse accessTokenResponse = getAccessTokenResponse(); @@ -252,4 +243,9 @@ public String getAppId() { public SmartthingsNetworkConnector getNetworkConnector() { return this.networkConnector; } + + @Override + public Collection> getServices() { + return Set.of(SmartthingsDiscoveryService.class); + } } diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsCloudBridgeHandler.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsCloudBridgeHandler.java index 53e838010ab5b..325f5cd1ec275 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsCloudBridgeHandler.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsCloudBridgeHandler.java @@ -12,19 +12,14 @@ */ package org.openhab.binding.smartthings.internal.handler; -import java.util.Collection; -import java.util.LinkedList; - import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.smartthings.internal.SmartthingsAuthService; import org.openhab.binding.smartthings.internal.SmartthingsHandlerFactory; import org.openhab.binding.smartthings.internal.api.SmartthingsApi; -import org.openhab.binding.smartthings.internal.discovery.SmartthingsDiscoveryService; import org.openhab.binding.smartthings.internal.dto.SmartthingsCapabilitie; import org.openhab.binding.smartthings.internal.type.SmartthingsException; import org.openhab.binding.smartthings.internal.type.SmartthingsTypeRegistry; import org.openhab.core.auth.client.oauth2.OAuthFactory; -import org.openhab.core.config.core.status.ConfigStatusMessage; import org.openhab.core.io.net.http.HttpClientFactory; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; @@ -48,10 +43,9 @@ public class SmartthingsCloudBridgeHandler extends SmartthingsBridgeHandler { public SmartthingsCloudBridgeHandler(Bridge bridge, SmartthingsHandlerFactory smartthingsHandlerFactory, SmartthingsAuthService authService, BundleContext bundleContext, HttpService httpService, - OAuthFactory oAuthFactory, HttpClientFactory httpClientFactory, SmartthingsTypeRegistry typeRegistry, - SmartthingsDiscoveryService disco) { + OAuthFactory oAuthFactory, HttpClientFactory httpClientFactory, SmartthingsTypeRegistry typeRegistry) { super(bridge, smartthingsHandlerFactory, authService, bundleContext, httpService, oAuthFactory, - httpClientFactory, typeRegistry, disco); + httpClientFactory, typeRegistry); } @Override @@ -132,10 +126,4 @@ public String getClientSecret() { return config.clientSecret; } - @Override - public Collection getConfigStatus() { - Collection configStatusMessages = new LinkedList(); - - return configStatusMessages; - } }