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.*;
17 import java.net.Inet4Address;
18 import java.util.HashMap;
21 import java.util.concurrent.ConcurrentHashMap;
23 import javax.jmdns.ServiceInfo;
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;
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
42 * so the bridge cat get the host name/ip address if that is unknown.
44 * @author Thomas Hentschel - Initial contribution
45 * @author Hilbrand Bouwkamp - Initial contribution
47 @Component(service = { EnvoyHostAddressCache.class, MDNSDiscoveryParticipant.class })
49 public class EnvoyDiscoveryParticipant implements MDNSDiscoveryParticipant, EnvoyHostAddressCache {
50 private static final String ENVOY_MDNS_ID = "envoy";
52 private final Logger logger = LoggerFactory.getLogger(EnvoyDiscoveryParticipant.class);
54 private final Map<String, @Nullable String> lastKnownHostAddresses = new ConcurrentHashMap<>();
57 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
58 return Set.of(EnphaseBindingConstants.THING_TYPE_ENPHASE_ENVOY);
62 public String getServiceType() {
63 return "_enphase-envoy._tcp.local.";
67 public @Nullable DiscoveryResult createResult(final ServiceInfo info) {
68 final String id = info.getName();
70 logger.debug("id found: {} with type: {}", id, info.getType());
72 if (!id.contains(ENVOY_MDNS_ID)) {
76 if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
80 final ThingUID uid = getThingUID(info);
86 final Inet4Address hostname = info.getInet4Addresses()[0];
87 final String serialNumber = info.getPropertyString(DISCOVERY_SERIAL);
89 if (serialNumber == null) {
90 logger.debug("No serial number found in data for discovered Envoy {}: {}", id, info);
93 final String version = info.getPropertyString(DISCOVERY_VERSION);
94 final String hostAddress = hostname == null ? "" : hostname.getHostAddress();
96 lastKnownHostAddresses.put(serialNumber, hostAddress);
97 final Map<String, Object> properties = new HashMap<>(3);
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();
108 public String getLastKnownHostAddress(final String serialNumber) {
109 final String hostAddress = lastKnownHostAddresses.get(serialNumber);
111 return hostAddress == null ? "" : hostAddress;
115 public @Nullable ThingUID getThingUID(final ServiceInfo info) {
116 final String name = info.getName();
118 if (!name.contains(ENVOY_MDNS_ID)) {
119 logger.trace("Found other type of device that is not recognized as an Envoy: {}", name);
122 if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
123 logger.debug("Found an Envoy, but no ip address is given: {}", info);
126 logger.debug("ServiceInfo addr: {}", info.getInet4Addresses()[0]);
127 if (getServiceType().equals(info.getType())) {
128 final String serial = info.getPropertyString(DISCOVERY_SERIAL);
130 logger.debug("Discovered an Envoy with serial number '{}'", serial);
131 return new ThingUID(THING_TYPE_ENPHASE_ENVOY, serial);