2 * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.binding.neeo.internal.NeeoConstants;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.osgi.service.component.annotations.Component;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Implementation of {@link MDNSDiscoveryParticipant} that will discover NEEO brain(s).
43 * @author Tim Roberts - initial contribution
46 @Component(immediate = true)
47 public class NeeoBrainDiscovery implements MDNSDiscoveryParticipant {
50 private Logger logger = LoggerFactory.getLogger(NeeoBrainDiscovery.class);
53 public Set<@Nullable ThingTypeUID> getSupportedThingTypeUIDs() {
54 return Collections.singleton(BRIDGE_TYPE_BRAIN);
58 public String getServiceType() {
59 return NeeoConstants.NEEO_MDNS_TYPE;
64 public DiscoveryResult createResult(@Nullable ServiceInfo service) {
65 if (service == null) {
69 final ThingUID uid = getThingUID(service);
74 logger.debug("createResult is evaluating: {}", service);
76 final Map<String, Object> properties = new HashMap<>(2);
78 final InetAddress ip = getIpAddress(service);
80 logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
83 final String inetAddress = ip.getHostAddress();
85 final String id = uid.getId();
86 final String label = service.getName() + " (" + id + ")";
88 properties.put(NeeoConstants.CONFIG_IPADDRESS, inetAddress);
89 properties.put(NeeoConstants.CONFIG_ENABLEFORWARDACTIONS, true);
91 logger.debug("Adding NEEO Brain to inbox: {} at {}", id, inetAddress);
92 return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
97 public ThingUID getThingUID(@Nullable ServiceInfo service) {
98 if (service == null) {
102 logger.debug("getThingUID is evaluating: {}", service);
103 if (!StringUtils.equals("neeo", service.getApplication())) {
104 logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
108 if (getIpAddress(service) == null) {
109 logger.debug("No IP address found in MDNS serviceinfo: {}", service);
113 String model = service.getPropertyString("hon"); // model
115 final String server = service.getServer(); // NEEO-xxxxx.local.
116 if (server != null) {
117 final int idx = server.indexOf(".");
119 model = server.substring(0, idx);
123 if (model == null || model.length() <= 5 || !model.toLowerCase().startsWith("neeo")) {
124 logger.debug("No 'hon' found in MDNS serviceinfo: {}", service);
128 final String id = model.substring(5);
129 logger.debug("NEEO Brain Found: {}", id);
131 return new ThingUID(BRIDGE_TYPE_BRAIN, id);
135 * Gets the ip address found in the {@link ServiceInfo}
137 * @param service a non-null service
138 * @return the ip address of the service or null if none found.
141 private InetAddress getIpAddress(ServiceInfo service) {
142 Objects.requireNonNull(service, "service cannot be null");
144 for (String addr : service.getHostAddresses()) {
146 return InetAddress.getByName(addr);
147 } catch (UnknownHostException e) {
152 for (InetAddress addr : service.getInet4Addresses()) {
155 // Fallback for Inet6addresses
156 for (InetAddress addr : service.getInet6Addresses()) {