]> git.basschouten.com Git - openhab-addons.git/blob
762da5f004a4b90058d344ec1556d67d60fae98e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.digitalstrom.internal.discovery;
14
15 import static org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants.BINDING_ID;
16
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;
22 import java.util.Map;
23
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;
37
38 /**
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}.
43  *
44  * @author Michael Ochel - Initial contribution
45  * @author Matthias Siegele - Initial contribution
46  */
47 public class DeviceDiscoveryService extends AbstractDiscoveryService {
48
49     private final Logger logger = LoggerFactory.getLogger(DeviceDiscoveryService.class);
50
51     private final BridgeHandler bridgeHandler;
52     private final String deviceType;
53     private final ThingUID bridgeUID;
54
55     public static final int TIMEOUT = 10;
56
57     /**
58      * Creates a new {@link DeviceDiscoveryService} for the given supported {@link ThingTypeUID}.
59      *
60      * @param bridgeHandler (must not be null)
61      * @param supportedThingType (must not be null)
62      * @throws IllegalArgumentException see {@link AbstractDiscoveryService#AbstractDiscoveryService(int)}
63      */
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();
70     }
71
72     /**
73      * Deactivates the {@link DeviceDiscoveryService} and removes the {@link DiscoveryResult}s.
74      */
75     @Override
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());
80     }
81
82     @Override
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);
90                     }
91                 }
92             } else {
93                 List<Circuit> circuits = bridgeHandler.getCircuits();
94                 if (circuits != null) {
95                     for (Circuit circuit : circuits) {
96                         onDeviceAddedInternal(circuit);
97                     }
98                 }
99             }
100         }
101     }
102
103     @Override
104     protected synchronized void stopScan() {
105         super.stopScan();
106         removeOlderResults(getTimestampOfLastScan());
107     }
108
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())) {
117                 isSupported = true;
118             }
119         } else if (device instanceof Circuit
120                 && DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString().equals(deviceType)) {
121             isSupported = true;
122         }
123         if (isSupported) {
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();
132                 }
133                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
134                         .withBridge(bridgeUID).withLabel(deviceName).build();
135
136                 thingDiscovered(discoveryResult);
137             } else {
138                 if (device instanceof Device) {
139                     logger.debug("Discovered unsupported device hardware type '{}' with uid {}",
140                             ((Device) device).getHWinfo(), device.getDSUID());
141                 }
142             }
143         } else {
144             if (device instanceof Device) {
145                 logger.debug(
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());
150             }
151         }
152     }
153
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);
162             }
163         } else {
164             thingTypeUID = new ThingTypeUID(BINDING_ID,
165                     DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString());
166         }
167         if (getSupportedThingTypes().contains(thingTypeUID)) {
168             String thingDeviceId = device.getDSID().toString();
169             ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, thingDeviceId);
170             return thingUID;
171         } else {
172             return null;
173         }
174     }
175
176     /**
177      * Removes the {@link Thing} of the given {@link Device}.
178      *
179      * @param device (must not be null)
180      */
181     public void onDeviceRemoved(GeneralDeviceInformation device) {
182         ThingUID thingUID = getThingUID(device);
183
184         if (thingUID != null) {
185             thingRemoved(thingUID);
186         }
187     }
188
189     /**
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}.
192      *
193      * @param device (must not be null)
194      */
195     public void onDeviceAdded(GeneralDeviceInformation device) {
196         if (super.isBackgroundDiscoveryEnabled()) {
197             onDeviceAddedInternal(device);
198         }
199     }
200
201     /**
202      * Returns the ID of this {@link DeviceDiscoveryService}.
203      *
204      * @return id of the service
205      */
206     public String getID() {
207         return deviceType;
208     }
209 }