]> git.basschouten.com Git - openhab-addons.git/blob
02e5635880d2f02e28e9790491b114ab15ff3207
[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.nanoleaf.internal.discovery;
14
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.nanoleaf.internal.NanoleafHandlerFactory;
26 import org.openhab.binding.nanoleaf.internal.OpenAPIUtils;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link NanoleafMDNSDiscoveryParticipant} is responsible for discovering new Nanoleaf controllers (bridges).
39  *
40  * @author Martin Raepple - Initial contribution
41  * @author Stefan Höhn - further improvements for static defined things
42  * @see <a href="https://openhab.org/documentation/development/bindings/discovery-services.html">MSDN
43  *      Discovery</a>
44  */
45 @Component(configurationPid = "discovery.nanoleaf")
46 @NonNullByDefault
47 public class NanoleafMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
48
49     private final Logger logger = LoggerFactory.getLogger(NanoleafMDNSDiscoveryParticipant.class);
50
51     @Override
52     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return NanoleafHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
54     }
55
56     @Override
57     public String getServiceType() {
58         return SERVICE_TYPE;
59     }
60
61     @Override
62     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
63         final ThingUID uid = getThingUID(service);
64         if (uid == null) {
65             return null;
66         }
67         final Map<String, Object> properties = new HashMap<>(2);
68         String host = service.getHostAddresses()[0];
69         properties.put(CONFIG_ADDRESS, host);
70         int port = service.getPort();
71         properties.put(CONFIG_PORT, port);
72         String firmwareVersion = service.getPropertyString("srcvers");
73         properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareVersion);
74         String modelId = service.getPropertyString("md");
75         properties.put(Thing.PROPERTY_MODEL_ID, modelId);
76         properties.put(Thing.PROPERTY_VENDOR, "Nanoleaf");
77         String qualifiedName = service.getQualifiedName();
78         logger.debug("Device found: {}", qualifiedName);
79
80         logger.trace("Discovered nanoleaf host: {} port: {} firmWare: {} modelId: {} qualifiedName: {}", host, port,
81                 firmwareVersion, modelId, qualifiedName);
82         logger.debug("Adding Nanoleaf controller {} with FW version {} found at {}:{} to inbox", qualifiedName,
83                 firmwareVersion, host, port);
84         if (!OpenAPIUtils.checkRequiredFirmware(service.getPropertyString("md"), firmwareVersion)) {
85             logger.debug("Nanoleaf controller firmware is too old. Must be {} or higher",
86                     MODEL_ID_LIGHTPANELS.equals(modelId) ? API_MIN_FW_VER_LIGHTPANELS : API_MIN_FW_VER_CANVAS);
87         }
88
89         final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
90                 .withProperties(properties).withLabel(service.getName()).withRepresentationProperty(CONFIG_ADDRESS)
91                 .build();
92         return result;
93     }
94
95     @Override
96     public @Nullable ThingUID getThingUID(ServiceInfo service) {
97         ThingTypeUID thingTypeUID = getThingType(service);
98         if (thingTypeUID != null) {
99             String id = service.getPropertyString("id").replace(":", "");
100             return new ThingUID(thingTypeUID, id);
101         } else {
102             return null;
103         }
104     }
105
106     private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
107         String model = service.getPropertyString("md"); // model
108         logger.debug("Nanoleaf Type: {}", model);
109         if (model == null) {
110             return null;
111         }
112         return THING_TYPE_CONTROLLER;
113     }
114 }