2 * Copyright (c) 2010-2023 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.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
25 import org.openhab.binding.digitalstrom.internal.handler.BridgeHandler;
26 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Circuit;
27 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Device;
28 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.GeneralDeviceInformation;
29 import org.openhab.binding.digitalstrom.internal.providers.DsDeviceThingTypeProvider;
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.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link DeviceDiscoveryService} discovers all digitalSTROM-Devices, of one supported device-color-type. The
40 * device-color-type has to be given to the {@link #DeviceDiscoveryService(BridgeHandler, ThingTypeUID)} as
41 * {@link ThingTypeUID}. The supported {@link ThingTypeUID} can be found at
42 * {@link DeviceHandler#SUPPORTED_THING_TYPES}.
44 * @author Michael Ochel - Initial contribution
45 * @author Matthias Siegele - Initial contribution
47 public class DeviceDiscoveryService extends AbstractDiscoveryService {
49 private final Logger logger = LoggerFactory.getLogger(DeviceDiscoveryService.class);
51 private final BridgeHandler bridgeHandler;
52 private final String deviceType;
53 private final ThingUID bridgeUID;
55 public static final int TIMEOUT = 10;
58 * Creates a new {@link DeviceDiscoveryService} for the given supported {@link ThingTypeUID}.
60 * @param bridgeHandler (must not be null)
61 * @param supportedThingType (must not be null)
62 * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
64 public DeviceDiscoveryService(BridgeHandler bridgeHandler, ThingTypeUID supportedThingType)
65 throws IllegalArgumentException {
66 super(new HashSet<>(Arrays.asList(supportedThingType)), TIMEOUT, true);
67 this.deviceType = supportedThingType.getId();
68 this.bridgeHandler = bridgeHandler;
69 bridgeUID = bridgeHandler.getThing().getUID();
73 * Deactivates the {@link DeviceDiscoveryService} and removes the {@link DiscoveryResult}s.
76 public void deactivate() {
77 logger.debug("deactivate discovery service for device type {} thing types are: {}", deviceType,
78 super.getSupportedThingTypes().toString());
79 removeOlderResults(new Date().getTime());
83 protected void startScan() {
84 if (bridgeHandler != null) {
85 if (!DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString().equals(deviceType)) {
86 List<Device> devices = bridgeHandler.getDevices();
87 if (devices != null) {
88 for (Device device : devices) {
89 onDeviceAddedInternal(device);
93 List<Circuit> circuits = bridgeHandler.getCircuits();
94 if (circuits != null) {
95 for (Circuit circuit : circuits) {
96 onDeviceAddedInternal(circuit);
104 protected synchronized void stopScan() {
106 removeOlderResults(getTimestampOfLastScan());
109 private void onDeviceAddedInternal(GeneralDeviceInformation device) {
110 boolean isSupported = false;
111 if (device instanceof Device) {
112 Device tempDevice = (Device) device;
113 if ((tempDevice.isSensorDevice() && deviceType.equals(tempDevice.getHWinfo().replaceAll("-", "")))
114 || (deviceType.equals(tempDevice.getHWinfo().substring(0, 2))
115 && (tempDevice.isDeviceWithOutput() || tempDevice.isBinaryInputDevice())
116 && tempDevice.isPresent())) {
119 } else if (device instanceof Circuit
120 && DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString().equals(deviceType)) {
124 ThingUID thingUID = getThingUID(device);
125 if (thingUID != null) {
126 Map<String, Object> properties = new HashMap<>(1);
127 properties.put(DigitalSTROMBindingConstants.DEVICE_DSID, device.getDSID().getValue());
128 String deviceName = device.getName();
129 if (deviceName == null || deviceName.isBlank()) {
130 // if no name is set, the dSID will be used as name
131 deviceName = device.getDSID().getValue();
133 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
134 .withBridge(bridgeUID).withLabel(deviceName).build();
136 thingDiscovered(discoveryResult);
138 if (device instanceof Device) {
139 logger.debug("Discovered unsupported device hardware type '{}' with uid {}",
140 ((Device) device).getHWinfo(), device.getDSUID());
144 if (device instanceof Device) {
146 "Discovered device with disabled or no output mode. Device was not added to inbox. "
147 + "Device information: hardware info: {}, dSUID: {}, device-name: {}, output value: {}",
148 ((Device) device).getHWinfo(), device.getDSUID(), device.getName(),
149 ((Device) device).getOutputMode());
154 private ThingUID getThingUID(GeneralDeviceInformation device) {
155 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
156 ThingTypeUID thingTypeUID = null;
157 if (device instanceof Device) {
158 Device tempDevice = (Device) device;
159 thingTypeUID = new ThingTypeUID(BINDING_ID, tempDevice.getHWinfo().substring(0, 2));
160 if (tempDevice.isSensorDevice() && deviceType.equals(tempDevice.getHWinfo().replaceAll("-", ""))) {
161 thingTypeUID = new ThingTypeUID(BINDING_ID, deviceType);
164 thingTypeUID = new ThingTypeUID(BINDING_ID,
165 DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString());
167 if (getSupportedThingTypes().contains(thingTypeUID)) {
168 String thingDeviceId = device.getDSID().toString();
169 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, thingDeviceId);
177 * Removes the {@link Thing} of the given {@link Device}.
179 * @param device (must not be null)
181 public void onDeviceRemoved(GeneralDeviceInformation device) {
182 ThingUID thingUID = getThingUID(device);
184 if (thingUID != null) {
185 thingRemoved(thingUID);
190 * Creates a {@link DiscoveryResult} for the given {@link Device}, if the {@link Device} is supported and the
191 * {@link Device#getOutputMode()} is unequal {@link OutputModeEnum#DISABLED}.
193 * @param device (must not be null)
195 public void onDeviceAdded(GeneralDeviceInformation device) {
196 if (super.isBackgroundDiscoveryEnabled()) {
197 onDeviceAddedInternal(device);
202 * Returns the ID of this {@link DeviceDiscoveryService}.
204 * @return id of the service
206 public String getID() {