2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.smartthings.internal.discovery;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import java.util.regex.Pattern;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.smartthings.internal.SmartthingsBindingConstants;
28 import org.openhab.binding.smartthings.internal.SmartthingsHandlerFactory;
29 import org.openhab.binding.smartthings.internal.dto.SmartthingsDeviceData;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.osgi.service.event.Event;
39 import org.osgi.service.event.EventHandler;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.gson.Gson;
46 * Smartthings Discovery service
48 * @author Bob Raker - Initial contribution
51 @Component(service = { DiscoveryService.class,
52 EventHandler.class }, immediate = true, configurationPid = "discovery.smartthings", property = "event.topics=org/openhab/binding/smartthings/discovery")
53 public class SmartthingsDiscoveryService extends AbstractDiscoveryService implements EventHandler {
54 private static final int DISCOVERY_TIMEOUT_SEC = 30;
55 private static final int INITIAL_DELAY_SEC = 10; // Delay 10 sec to give time for bridge and things to be created
56 private static final int SCAN_INTERVAL_SEC = 600;
58 private final Pattern findIllegalChars = Pattern.compile("[^A-Za-z0-9_-]");
60 private final Logger logger = LoggerFactory.getLogger(SmartthingsDiscoveryService.class);
62 private final Gson gson;
64 private @Nullable SmartthingsHandlerFactory smartthingsHandlerFactory;
66 private @Nullable ScheduledFuture<?> scanningJob;
71 public SmartthingsDiscoveryService() {
72 super(SmartthingsBindingConstants.SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIMEOUT_SEC);
77 protected void setThingHandlerFactory(ThingHandlerFactory handlerFactory) {
78 if (handlerFactory instanceof SmartthingsHandlerFactory) {
79 smartthingsHandlerFactory = (SmartthingsHandlerFactory) handlerFactory;
83 protected void unsetThingHandlerFactory(ThingHandlerFactory handlerFactory) {
84 // Make sure it is this handleFactory that should be unset
85 if (handlerFactory == smartthingsHandlerFactory) {
86 this.smartthingsHandlerFactory = null;
91 * Called from the UI when starting a search.
94 public void startScan() {
95 sendSmartthingsDiscoveryRequest();
99 * Stops a running scan.
102 protected synchronized void stopScan() {
104 removeOlderResults(getTimestampOfLastScan());
108 * Starts background scanning for attached devices.
111 protected void startBackgroundDiscovery() {
112 if (scanningJob == null) {
113 this.scanningJob = scheduler.scheduleWithFixedDelay(this::sendSmartthingsDiscoveryRequest,
114 INITIAL_DELAY_SEC, SCAN_INTERVAL_SEC, TimeUnit.SECONDS);
115 logger.debug("Discovery background scanning job started");
120 * Stops background scanning for attached devices.
123 protected void stopBackgroundDiscovery() {
124 final ScheduledFuture<?> currentScanningJob = scanningJob;
125 if (currentScanningJob != null) {
126 currentScanningJob.cancel(false);
132 * Start the discovery process by sending a discovery request to the Smartthings Hub
134 private void sendSmartthingsDiscoveryRequest() {
135 final SmartthingsHandlerFactory currentSmartthingsHandlerFactory = smartthingsHandlerFactory;
136 if (currentSmartthingsHandlerFactory != null) {
138 String discoveryMsg = "{\"discovery\": \"yes\"}";
139 currentSmartthingsHandlerFactory.sendDeviceCommand("/discovery", 5, discoveryMsg);
140 // Smartthings will not return a response to this message but will send it's response message
141 // which will get picked up by the SmartthingBridgeHandler.receivedPushMessage handler
142 } catch (InterruptedException | TimeoutException | ExecutionException e) {
143 logger.warn("Attempt to send command to the Smartthings hub failed with: {}", e.getMessage());
149 * Handle discovery data returned from the Smartthings hub.
150 * The data is delivered into the SmartthingServlet. From there it is sent here via the Event service
153 public void handleEvent(@Nullable Event event) {
155 logger.info("SmartthingsDiscoveryService.handleEvent: event is uexpectedly null");
158 String topic = event.getTopic();
159 String data = (String) event.getProperty("data");
161 logger.debug("Event received on topic: {} but the data field is null", topic);
164 logger.trace("Event received on topic: {}", topic);
167 // The data returned from the Smartthings hub is a list of strings where each
168 // element is the data for one device. That device string is another json object
169 List<String> devices = new ArrayList<String>();
170 devices = gson.fromJson(data, devices.getClass());
171 for (String device : devices) {
172 SmartthingsDeviceData deviceData = gson.fromJson(device, SmartthingsDeviceData.class);
173 createDevice(deviceData);
178 * Create a device with the data from the Smartthings hub
180 * @param deviceData Device data from the hub
182 private void createDevice(SmartthingsDeviceData deviceData) {
183 logger.trace("Discovery: Creating device: ThingType {} with name {}", deviceData.capability, deviceData.name);
185 // Build the UID as a string smartthings:{ThingType}:{BridgeName}:{DeviceName}
186 String name = deviceData.name; // Note: this is necessary for null analysis to work
189 "Unexpectedly received data for a device with no name. Check the Smartthings hub devices and make sure every device has a name");
192 String deviceNameNoSpaces = name.replaceAll("\\s", "_");
193 String smartthingsDeviceName = findIllegalChars.matcher(deviceNameNoSpaces).replaceAll("");
194 final SmartthingsHandlerFactory currentSmartthingsHandlerFactory = smartthingsHandlerFactory;
195 if (currentSmartthingsHandlerFactory == null) {
197 "SmartthingsDiscoveryService: smartthingshandlerfactory is unexpectedly null, could not create device {}",
201 ThingUID bridgeUid = currentSmartthingsHandlerFactory.getBridgeHandler().getThing().getUID();
202 String bridgeId = bridgeUid.getId();
203 String uidStr = String.format("smartthings:%s:%s:%s", deviceData.capability, bridgeId, smartthingsDeviceName);
205 Map<String, Object> properties = new HashMap<>();
206 properties.put("smartthingsName", name);
207 properties.put("deviceId", deviceData.id);
209 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(new ThingUID(uidStr)).withProperties(properties)
210 .withRepresentationProperty("deviceId").withBridge(bridgeUid).withLabel(name).build();
212 thingDiscovered(discoveryResult);