]> git.basschouten.com Git - openhab-addons.git/blob
02fe022f20b2e9508f9941af041bfbdc9d3d1ba1
[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.enphase.internal.discovery;
14
15 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.*;
16
17 import java.net.Inet4Address;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22
23 import javax.jmdns.ServiceInfo;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.enphase.internal.EnphaseBindingConstants;
28 import org.openhab.binding.enphase.internal.EnvoyHostAddressCache;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Component;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * MDNS discovery participant for discovering Envoy gateways.
40  * This service also keeps track of any discovered Envoys host name to provide this information for existing Envoy
41  * bridges
42  * so the bridge cat get the host name/ip address if that is unknown.
43  *
44  * @author Thomas Hentschel - Initial contribution
45  * @author Hilbrand Bouwkamp - Initial contribution
46  */
47 @Component(service = { EnvoyHostAddressCache.class, MDNSDiscoveryParticipant.class })
48 @NonNullByDefault
49 public class EnvoyDiscoveryParticipant implements MDNSDiscoveryParticipant, EnvoyHostAddressCache {
50     private static final String ENVOY_MDNS_ID = "envoy";
51
52     private final Logger logger = LoggerFactory.getLogger(EnvoyDiscoveryParticipant.class);
53
54     private final Map<String, @Nullable String> lastKnownHostAddresses = new ConcurrentHashMap<>();
55
56     @Override
57     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
58         return Set.of(EnphaseBindingConstants.THING_TYPE_ENPHASE_ENVOY);
59     }
60
61     @Override
62     public String getServiceType() {
63         return "_enphase-envoy._tcp.local.";
64     }
65
66     @Override
67     public @Nullable DiscoveryResult createResult(final ServiceInfo info) {
68         final String id = info.getName();
69
70         logger.debug("id found: {} with type: {}", id, info.getType());
71
72         if (!id.contains(ENVOY_MDNS_ID)) {
73             return null;
74         }
75
76         if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
77             return null;
78         }
79
80         final ThingUID uid = getThingUID(info);
81
82         if (uid == null) {
83             return null;
84         }
85
86         final Inet4Address hostname = info.getInet4Addresses()[0];
87         final String serialNumber = info.getPropertyString(DISCOVERY_SERIAL);
88
89         if (serialNumber == null) {
90             logger.debug("No serial number found in data for discovered Envoy {}: {}", id, info);
91             return null;
92         }
93         final String version = info.getPropertyString(DISCOVERY_VERSION);
94         final String hostAddress = hostname == null ? "" : hostname.getHostAddress();
95
96         lastKnownHostAddresses.put(serialNumber, hostAddress);
97         final Map<String, Object> properties = new HashMap<>(3);
98
99         properties.put(CONFIG_SERIAL_NUMBER, serialNumber);
100         properties.put(CONFIG_HOSTNAME, hostAddress);
101         properties.put(PROPERTY_VERSION, version);
102         return DiscoveryResultBuilder.create(uid).withProperties(properties)
103                 .withRepresentationProperty(CONFIG_SERIAL_NUMBER)
104                 .withLabel("Enphase Envoy " + defaultPassword(serialNumber)).build();
105     }
106
107     @Override
108     public String getLastKnownHostAddress(final String serialNumber) {
109         final String hostAddress = lastKnownHostAddresses.get(serialNumber);
110
111         return hostAddress == null ? "" : hostAddress;
112     }
113
114     @Override
115     public @Nullable ThingUID getThingUID(final ServiceInfo info) {
116         final String name = info.getName();
117
118         if (!name.contains(ENVOY_MDNS_ID)) {
119             logger.trace("Found other type of device that is not recognized as an Envoy: {}", name);
120             return null;
121         }
122         if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
123             logger.debug("Found an Envoy, but no ip address is given: {}", info);
124             return null;
125         }
126         logger.debug("ServiceInfo addr: {}", info.getInet4Addresses()[0]);
127         if (getServiceType().equals(info.getType())) {
128             final String serial = info.getPropertyString(DISCOVERY_SERIAL);
129
130             logger.debug("Discovered an Envoy with serial number '{}'", serial);
131             return new ThingUID(THING_TYPE_ENPHASE_ENVOY, serial);
132         }
133         return null;
134     }
135 }