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.neeo.internal.discovery;
15 import static org.openhab.binding.neeo.internal.NeeoConstants.BRIDGE_TYPE_BRAIN;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.Collections;
20 import java.util.HashMap;
22 import java.util.Objects;
25 import javax.jmdns.ServiceInfo;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.neeo.internal.NeeoConstants;
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 * Implementation of {@link MDNSDiscoveryParticipant} that will discover NEEO brain(s).
42 * @author Tim Roberts - initial contribution
46 public class NeeoBrainDiscovery implements MDNSDiscoveryParticipant {
49 private Logger logger = LoggerFactory.getLogger(NeeoBrainDiscovery.class);
52 public Set<@Nullable ThingTypeUID> getSupportedThingTypeUIDs() {
53 return Collections.singleton(BRIDGE_TYPE_BRAIN);
57 public String getServiceType() {
58 return NeeoConstants.NEEO_MDNS_TYPE;
63 public DiscoveryResult createResult(@Nullable ServiceInfo service) {
64 if (service == null) {
68 final ThingUID uid = getThingUID(service);
73 logger.debug("createResult is evaluating: {}", service);
75 final Map<String, Object> properties = new HashMap<>(2);
77 final InetAddress ip = getIpAddress(service);
79 logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
82 final String inetAddress = ip.getHostAddress();
84 final String id = uid.getId();
85 final String label = service.getName() + " (" + id + ")";
87 properties.put(NeeoConstants.CONFIG_IPADDRESS, inetAddress);
88 properties.put(NeeoConstants.CONFIG_ENABLEFORWARDACTIONS, true);
90 logger.debug("Adding NEEO Brain to inbox: {} at {}", id, inetAddress);
91 return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
96 public ThingUID getThingUID(@Nullable ServiceInfo service) {
97 if (service == null) {
101 logger.debug("getThingUID is evaluating: {}", service);
102 if (!"neeo".equals(service.getApplication())) {
103 logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
107 if (getIpAddress(service) == null) {
108 logger.debug("No IP address found in MDNS serviceinfo: {}", service);
112 String model = service.getPropertyString("hon"); // model
114 final String server = service.getServer(); // NEEO-xxxxx.local.
115 if (server != null) {
116 final int idx = server.indexOf(".");
118 model = server.substring(0, idx);
122 if (model == null || model.length() <= 5 || !model.toLowerCase().startsWith("neeo")) {
123 logger.debug("No 'hon' found in MDNS serviceinfo: {}", service);
127 final String id = model.substring(5);
128 logger.debug("NEEO Brain Found: {}", id);
130 return new ThingUID(BRIDGE_TYPE_BRAIN, id);
134 * Gets the ip address found in the {@link ServiceInfo}
136 * @param service a non-null service
137 * @return the ip address of the service or null if none found.
140 private InetAddress getIpAddress(ServiceInfo service) {
141 Objects.requireNonNull(service, "service cannot be null");
143 for (String addr : service.getHostAddresses()) {
145 return InetAddress.getByName(addr);
146 } catch (UnknownHostException e) {
151 for (InetAddress addr : service.getInet4Addresses()) {
154 // Fallback for Inet6addresses
155 for (InetAddress addr : service.getInet6Addresses()) {