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