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.Collections;
19 import java.util.HashMap;
22 import java.util.concurrent.ConcurrentHashMap;
24 import javax.jmdns.ServiceInfo;
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;
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
43 * so the bridge cat get the host name/ip address if that is unknown.
45 * @author Thomas Hentschel - Initial contribution
46 * @author Hilbrand Bouwkamp - Initial contribution
48 @Component(service = { EnvoyHostAddressCache.class, MDNSDiscoveryParticipant.class })
50 public class EnvoyDiscoveryParticipant implements MDNSDiscoveryParticipant, EnvoyHostAddressCache {
51 private static final String ENVOY_MDNS_ID = "envoy";
53 private final Logger logger = LoggerFactory.getLogger(EnvoyDiscoveryParticipant.class);
55 private final Map<String, @Nullable String> lastKnownHostAddresses = new ConcurrentHashMap<>();
58 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
59 return Collections.singleton(EnphaseBindingConstants.THING_TYPE_ENPHASE_ENVOY);
63 public String getServiceType() {
64 return "_enphase-envoy._tcp.local.";
68 public @Nullable DiscoveryResult createResult(final ServiceInfo info) {
69 final String id = info.getName();
71 logger.debug("id found: {} with type: {}", id, info.getType());
73 if (!id.contains(ENVOY_MDNS_ID)) {
77 if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
81 final ThingUID uid = getThingUID(info);
87 final Inet4Address hostname = info.getInet4Addresses()[0];
88 final String serialNumber = info.getPropertyString(DISCOVERY_SERIAL);
90 if (serialNumber == null) {
91 logger.debug("No serial number found in data for discovered Envoy {}: {}", id, info);
94 final String version = info.getPropertyString(DISCOVERY_VERSION);
95 final String hostAddress = hostname == null ? "" : hostname.getHostAddress();
97 lastKnownHostAddresses.put(serialNumber, hostAddress);
98 final Map<String, Object> properties = new HashMap<>(3);
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();
109 public String getLastKnownHostAddress(final String serialNumber) {
110 final String hostAddress = lastKnownHostAddresses.get(serialNumber);
112 return hostAddress == null ? "" : hostAddress;
116 public @Nullable ThingUID getThingUID(final ServiceInfo info) {
117 final String name = info.getName();
119 if (!name.contains(ENVOY_MDNS_ID)) {
120 logger.trace("Found other type of device that is not recognized as an Envoy: {}", name);
123 if (info.getInet4Addresses().length == 0 || info.getInet4Addresses()[0] == null) {
124 logger.debug("Found an Envoy, but no ip address is given: {}", info);
127 logger.debug("ServiceInfo addr: {}", info.getInet4Addresses()[0]);
128 if (getServiceType().equals(info.getType())) {
129 final String serial = info.getPropertyString(DISCOVERY_SERIAL);
131 logger.debug("Discovered an Envoy with serial number '{}'", serial);
132 return new ThingUID(THING_TYPE_ENPHASE_ENVOY, serial);