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