2 * Copyright (c) 2010-2024 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.salus.internal.discovery;
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;
22 import java.util.Locale;
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;
39 * @author Martin GrzeĊlowski - Initial contribution
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;
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;
55 protected void startScan() {
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);
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);
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);
78 if (model.toString().toLowerCase(Locale.ENGLISH).contains(IT_600)) {
79 return SALUS_IT600_DEVICE_TYPE;
83 return SALUS_DEVICE_TYPE;
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();
91 private String buildThingLabel(Device device) {
92 var name = device.name();
93 return (!"".equals(name)) ? name : device.dsn();
96 private Map<String, Object> buildThingProperties(Device device) {
97 return Map.of(DSN, device.dsn());