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 tempDevice) {
112 if ((tempDevice.isSensorDevice() && deviceType.equals(tempDevice.getHWinfo().replaceAll("-", "")))
113 || (deviceType.equals(tempDevice.getHWinfo().substring(0, 2))
114 && (tempDevice.isDeviceWithOutput() || tempDevice.isBinaryInputDevice())
115 && tempDevice.isPresent())) {
118 } else if (device instanceof Circuit
119 && DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString().equals(deviceType)) {
123 ThingUID thingUID = getThingUID(device);
124 if (thingUID != null) {
125 Map<String, Object> properties = new HashMap<>(1);
126 properties.put(DigitalSTROMBindingConstants.DEVICE_DSID, device.getDSID().getValue());
127 String deviceName = device.getName();
128 if (deviceName == null || deviceName.isBlank()) {
129 // if no name is set, the dSID will be used as name
130 deviceName = device.getDSID().getValue();
132 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
133 .withBridge(bridgeUID).withLabel(deviceName).build();
135 thingDiscovered(discoveryResult);
137 if (device instanceof Device device1) {
138 logger.debug("Discovered unsupported device hardware type '{}' with uid {}", device1.getHWinfo(),
143 if (device instanceof Device device1) {
145 Discovered device with disabled or no output mode. Device was not added to inbox. \
146 Device information: hardware info: {}, dSUID: {}, device-name: {}, output value: {}\
147 """, device1.getHWinfo(), device.getDSUID(), device.getName(), device1.getOutputMode());
152 private ThingUID getThingUID(GeneralDeviceInformation device) {
153 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
154 ThingTypeUID thingTypeUID = null;
155 if (device instanceof Device tempDevice) {
156 thingTypeUID = new ThingTypeUID(BINDING_ID, tempDevice.getHWinfo().substring(0, 2));
157 if (tempDevice.isSensorDevice() && deviceType.equals(tempDevice.getHWinfo().replaceAll("-", ""))) {
158 thingTypeUID = new ThingTypeUID(BINDING_ID, deviceType);
161 thingTypeUID = new ThingTypeUID(BINDING_ID,
162 DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString());
164 if (getSupportedThingTypes().contains(thingTypeUID)) {
165 String thingDeviceId = device.getDSID().toString();
166 return new ThingUID(thingTypeUID, bridgeUID, thingDeviceId);
173 * Removes the {@link Thing} of the given {@link Device}.
175 * @param device (must not be null)
177 public void onDeviceRemoved(GeneralDeviceInformation device) {
178 ThingUID thingUID = getThingUID(device);
180 if (thingUID != null) {
181 thingRemoved(thingUID);
186 * Creates a {@link DiscoveryResult} for the given {@link Device}, if the {@link Device} is supported and the
187 * {@link Device#getOutputMode()} is unequal {@link OutputModeEnum#DISABLED}.
189 * @param device (must not be null)
191 public void onDeviceAdded(GeneralDeviceInformation device) {
192 if (super.isBackgroundDiscoveryEnabled()) {
193 onDeviceAddedInternal(device);
198 * Returns the ID of this {@link DeviceDiscoveryService}.
200 * @return id of the service
202 public String getID() {