]> git.basschouten.com Git - openhab-addons.git/blob
824c650bbc840bd6e7e80c9cae277c813ae45be2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.salus.internal.discovery;
14
15 import static org.openhab.binding.salus.internal.SalusBindingConstants.SALUS_DEVICE_TYPE;
16 import static org.openhab.binding.salus.internal.SalusBindingConstants.SALUS_IT600_DEVICE_TYPE;
17 import static org.openhab.binding.salus.internal.SalusBindingConstants.SUPPORTED_THING_TYPES_UIDS;
18 import static org.openhab.binding.salus.internal.SalusBindingConstants.SalusDevice.DSN;
19 import static org.openhab.binding.salus.internal.SalusBindingConstants.SalusDevice.IT_600;
20 import static org.openhab.binding.salus.internal.SalusBindingConstants.SalusDevice.OEM_MODEL;
21
22 import java.util.Locale;
23 import java.util.Map;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.salus.internal.handler.CloudApi;
27 import org.openhab.binding.salus.internal.handler.CloudBridgeHandler;
28 import org.openhab.binding.salus.internal.rest.Device;
29 import org.openhab.binding.salus.internal.rest.SalusApiException;
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  * @author Martin GrzeĊ›lowski - Initial contribution
40  */
41 @NonNullByDefault
42 public class CloudDiscovery extends AbstractDiscoveryService {
43     private final Logger logger = LoggerFactory.getLogger(CloudDiscovery.class);
44     private final CloudApi cloudApi;
45     private final ThingUID bridgeUid;
46
47     public CloudDiscovery(CloudBridgeHandler bridgeHandler, CloudApi cloudApi, ThingUID bridgeUid)
48             throws IllegalArgumentException {
49         super(SUPPORTED_THING_TYPES_UIDS, 10, true);
50         this.cloudApi = cloudApi;
51         this.bridgeUid = bridgeUid;
52     }
53
54     @Override
55     protected void startScan() {
56         try {
57             var devices = cloudApi.findDevices();
58             logger.debug("Found {} devices while scanning", devices.size());
59             devices.stream().filter(Device::isConnected).forEach(this::addThing);
60         } catch (SalusApiException e) {
61             logger.warn("Error while scanning", e);
62             stopScan();
63         }
64     }
65
66     private void addThing(Device device) {
67         logger.debug("Adding device \"{}\" ({}) to found things", device.name(), device.dsn());
68         var thingUID = new ThingUID(findDeviceType(device), bridgeUid, device.dsn());
69         var discoveryResult = createDiscoveryResult(thingUID, buildThingLabel(device), buildThingProperties(device));
70         thingDiscovered(discoveryResult);
71     }
72
73     private static ThingTypeUID findDeviceType(Device device) {
74         var props = device.properties();
75         if (props.containsKey(OEM_MODEL)) {
76             var model = props.get(OEM_MODEL);
77             if (model != null) {
78                 if (model.toString().toLowerCase(Locale.ENGLISH).contains(IT_600)) {
79                     return SALUS_IT600_DEVICE_TYPE;
80                 }
81             }
82         }
83         return SALUS_DEVICE_TYPE;
84     }
85
86     private DiscoveryResult createDiscoveryResult(ThingUID thingUID, String label, Map<String, Object> properties) {
87         return DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUid).withProperties(properties).withLabel(label)
88                 .withRepresentationProperty(DSN).build();
89     }
90
91     private String buildThingLabel(Device device) {
92         var name = device.name();
93         return (!"".equals(name)) ? name : device.dsn();
94     }
95
96     private Map<String, Object> buildThingProperties(Device device) {
97         return Map.of(DSN, device.dsn());
98     }
99 }