2 * Copyright (c) 2010-2021 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.digitalstrom.internal.discovery;
15 import static org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants.BINDING_ID;
17 import java.util.Arrays;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
24 import org.apache.commons.lang.StringUtils;
25 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
26 import org.openhab.binding.digitalstrom.internal.handler.BridgeHandler;
27 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Circuit;
28 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Device;
29 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.GeneralDeviceInformation;
30 import org.openhab.binding.digitalstrom.internal.providers.DsDeviceThingTypeProvider;
31 import org.openhab.core.config.discovery.AbstractDiscoveryService;
32 import org.openhab.core.config.discovery.DiscoveryResult;
33 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link DeviceDiscoveryService} discovers all digitalSTROM-Devices, of one supported device-color-type. The
41 * device-color-type has to be given to the {@link #DeviceDiscoveryService(BridgeHandler, ThingTypeUID)} as
42 * {@link ThingTypeUID}. The supported {@link ThingTypeUID} can be found at
43 * {@link DeviceHandler#SUPPORTED_THING_TYPES}.
45 * @author Michael Ochel - Initial contribution
46 * @author Matthias Siegele - Initial contribution
48 public class DeviceDiscoveryService extends AbstractDiscoveryService {
50 private final Logger logger = LoggerFactory.getLogger(DeviceDiscoveryService.class);
52 private final BridgeHandler bridgeHandler;
53 private final String deviceType;
54 private final ThingUID bridgeUID;
56 public static final int TIMEOUT = 10;
59 * Creates a new {@link DeviceDiscoveryService} for the given supported {@link ThingTypeUID}.
61 * @param bridgeHandler (must not be null)
62 * @param supportedThingType (must not be null)
63 * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
65 public DeviceDiscoveryService(BridgeHandler bridgeHandler, ThingTypeUID supportedThingType)
66 throws IllegalArgumentException {
67 super(new HashSet<>(Arrays.asList(supportedThingType)), TIMEOUT, true);
68 this.deviceType = supportedThingType.getId();
69 this.bridgeHandler = bridgeHandler;
70 bridgeUID = bridgeHandler.getThing().getUID();
74 * Deactivates the {@link DeviceDiscoveryService} and removes the {@link DiscoveryResult}s.
77 public void deactivate() {
78 logger.debug("deactivate discovery service for device type {} thing types are: {}", deviceType,
79 super.getSupportedThingTypes().toString());
80 removeOlderResults(new Date().getTime());
84 protected void startScan() {
85 if (bridgeHandler != null) {
86 if (!DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString().equals(deviceType)) {
87 List<Device> devices = bridgeHandler.getDevices();
88 if (devices != null) {
89 for (Device device : devices) {
90 onDeviceAddedInternal(device);
94 List<Circuit> circuits = bridgeHandler.getCircuits();
95 if (circuits != null) {
96 for (Circuit circuit : circuits) {
97 onDeviceAddedInternal(circuit);
105 protected synchronized void stopScan() {
107 removeOlderResults(getTimestampOfLastScan());
110 private void onDeviceAddedInternal(GeneralDeviceInformation device) {
111 boolean isSupported = false;
112 if (device instanceof Device) {
113 Device tempDevice = (Device) device;
114 if ((tempDevice.isSensorDevice() && deviceType.equals(tempDevice.getHWinfo().replaceAll("-", "")))
115 || (deviceType.equals(tempDevice.getHWinfo().substring(0, 2))
116 && (tempDevice.isDeviceWithOutput() || tempDevice.isBinaryInputDevice())
117 && tempDevice.isPresent())) {
120 } else if (device instanceof Circuit
121 && DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString().equals(deviceType)) {
125 ThingUID thingUID = getThingUID(device);
126 if (thingUID != null) {
127 Map<String, Object> properties = new HashMap<>(1);
128 properties.put(DigitalSTROMBindingConstants.DEVICE_DSID, device.getDSID().getValue());
129 String deviceName = null;
130 if (StringUtils.isNotBlank(device.getName())) {
131 deviceName = device.getName();
133 // if no name is set, the dSID will be used as name
134 deviceName = device.getDSID().getValue();
136 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
137 .withBridge(bridgeUID).withLabel(deviceName).build();
139 thingDiscovered(discoveryResult);
141 if (device instanceof Device) {
142 logger.debug("Discovered unsupported device hardware type '{}' with uid {}",
143 ((Device) device).getHWinfo(), device.getDSUID());
147 if (device instanceof Device) {
149 "Discovered device with disabled or no output mode. Device was not added to inbox. "
150 + "Device information: hardware info: {}, dSUID: {}, device-name: {}, output value: {}",
151 ((Device) device).getHWinfo(), device.getDSUID(), device.getName(),
152 ((Device) device).getOutputMode());
157 private ThingUID getThingUID(GeneralDeviceInformation device) {
158 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
159 ThingTypeUID thingTypeUID = null;
160 if (device instanceof Device) {
161 Device tempDevice = (Device) device;
162 thingTypeUID = new ThingTypeUID(BINDING_ID, tempDevice.getHWinfo().substring(0, 2));
163 if (tempDevice.isSensorDevice() && deviceType.equals(tempDevice.getHWinfo().replaceAll("-", ""))) {
164 thingTypeUID = new ThingTypeUID(BINDING_ID, deviceType);
167 thingTypeUID = new ThingTypeUID(BINDING_ID,
168 DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString());
170 if (getSupportedThingTypes().contains(thingTypeUID)) {
171 String thingDeviceId = device.getDSID().toString();
172 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, thingDeviceId);
180 * Removes the {@link Thing} of the given {@link Device}.
182 * @param device (must not be null)
184 public void onDeviceRemoved(GeneralDeviceInformation device) {
185 ThingUID thingUID = getThingUID(device);
187 if (thingUID != null) {
188 thingRemoved(thingUID);
193 * Creates a {@link DiscoveryResult} for the given {@link Device}, if the {@link Device} is supported and the
194 * {@link Device#getOutputMode()} is unequal {@link OutputModeEnum#DISABLED}.
196 * @param device (must not be null)
198 public void onDeviceAdded(GeneralDeviceInformation device) {
199 if (super.isBackgroundDiscoveryEnabled()) {
200 onDeviceAddedInternal(device);
205 * Returns the ID of this {@link DeviceDiscoveryService}.
207 * @return id of the service
209 public String getID() {