2 * Copyright (c) 2010-2023 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.enphase.internal.discovery;
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;
22 import java.net.Inet4Address;
23 import java.util.HashMap;
26 import java.util.concurrent.ConcurrentHashMap;
28 import javax.jmdns.ServiceInfo;
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;
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.
48 * @author Thomas Hentschel - Initial contribution
49 * @author Hilbrand Bouwkamp - Initial contribution
51 @Component(service = { EnvoyHostAddressCache.class, MDNSDiscoveryParticipant.class })
53 public class EnvoyDiscoveryParticipant implements MDNSDiscoveryParticipant, EnvoyHostAddressCache {
54 private static final String ENVOY_MDNS_ID = "envoy";
56 private final Logger logger = LoggerFactory.getLogger(EnvoyDiscoveryParticipant.class);
58 private final Map<String, @Nullable String> lastKnownHostAddresses = new ConcurrentHashMap<>();
61 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
62 return Set.of(THING_TYPE_ENPHASE_ENVOY);
66 public String getServiceType() {
67 return "_enphase-envoy._tcp.local.";
71 public @Nullable DiscoveryResult createResult(final ServiceInfo info) {
72 final String id = info.getName();
74 logger.debug("id found: {} with type: {}", id, info.getType());
76 if (!id.contains(ENVOY_MDNS_ID)) {
80 if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
84 final ThingUID uid = getThingUID(info);
90 final Inet4Address hostname = info.getInet4Addresses()[0];
91 final String serialNumber = info.getPropertyString(DISCOVERY_SERIAL);
93 if (serialNumber == null) {
94 logger.debug("No serial number found in data for discovered Envoy {}: {}", id, info);
97 final String version = info.getPropertyString(DISCOVERY_VERSION);
98 final String hostAddress = hostname == null ? "" : hostname.getHostAddress();
100 lastKnownHostAddresses.put(serialNumber, hostAddress);
101 final Map<String, Object> properties = new HashMap<>(3);
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();
112 public String getLastKnownHostAddress(final String serialNumber) {
113 final String hostAddress = lastKnownHostAddresses.get(serialNumber);
115 return hostAddress == null ? "" : hostAddress;
119 public @Nullable ThingUID getThingUID(final ServiceInfo info) {
120 final String name = info.getName();
122 if (!name.contains(ENVOY_MDNS_ID)) {
123 logger.trace("Found other type of device that is not recognized as an Envoy: {}", name);
126 if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
127 logger.debug("Found an Envoy, but no ip address is given: {}", info);
130 logger.debug("ServiceInfo addr: {}", info.getInet4Addresses()[0]);
131 if (getServiceType().equals(info.getType())) {
132 final String serial = info.getPropertyString(DISCOVERY_SERIAL);
134 logger.debug("Discovered an Envoy with serial number '{}'", serial);
135 return new ThingUID(THING_TYPE_ENPHASE_ENVOY, serial);