Skip to content

Commit

Permalink
review / fix dependancy on discovery service
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 13, 2024
1 parent a42a337 commit bfb95af
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

/*
Expand All @@ -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.
*/
Expand All @@ -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;
}
Expand Down Expand Up @@ -147,8 +120,6 @@ public void doScan(Boolean addDevice) {
if ("main".equals(compId)) {
deviceType = catId;
}

logger.info("");
}
}
}
Expand Down Expand Up @@ -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);

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

Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -164,13 +162,6 @@ public BundleContext getBundleContext() {
return bundleContext;
}

@Override
public Collection<ConfigStatusMessage> getConfigStatus() {
Collection<ConfigStatusMessage> configStatusMessages = new LinkedList<>();

return configStatusMessages;
}

@Override
public boolean isAuthorized() {
final AccessTokenResponse accessTokenResponse = getAccessTokenResponse();
Expand Down Expand Up @@ -252,4 +243,9 @@ public String getAppId() {
public SmartthingsNetworkConnector getNetworkConnector() {
return this.networkConnector;
}

@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Set.of(SmartthingsDiscoveryService.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -132,10 +126,4 @@ public String getClientSecret() {
return config.clientSecret;
}

@Override
public Collection<ConfigStatusMessage> getConfigStatus() {
Collection<ConfigStatusMessage> configStatusMessages = new LinkedList<ConfigStatusMessage>();

return configStatusMessages;
}
}

0 comments on commit bfb95af

Please sign in to comment.