]> git.basschouten.com Git - openhab-addons.git/blob
982d69b690d01ed16431c9a2f714f32f9f39c16a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.neeo.internal.discovery;
14
15 import static org.openhab.binding.neeo.internal.NeeoConstants.BRIDGE_TYPE_BRAIN;
16
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Set;
24
25 import javax.jmdns.ServiceInfo;
26
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;
38
39 /**
40  * Implementation of {@link MDNSDiscoveryParticipant} that will discover NEEO brain(s).
41  *
42  * @author Tim Roberts - initial contribution
43  */
44 @NonNullByDefault
45 @Component
46 public class NeeoBrainDiscovery implements MDNSDiscoveryParticipant {
47
48     /** The logger */
49     private Logger logger = LoggerFactory.getLogger(NeeoBrainDiscovery.class);
50
51     @Override
52     public Set<@Nullable ThingTypeUID> getSupportedThingTypeUIDs() {
53         return Collections.singleton(BRIDGE_TYPE_BRAIN);
54     }
55
56     @Override
57     public String getServiceType() {
58         return NeeoConstants.NEEO_MDNS_TYPE;
59     }
60
61     @Nullable
62     @Override
63     public DiscoveryResult createResult(@Nullable ServiceInfo service) {
64         if (service == null) {
65             return null;
66         }
67
68         final ThingUID uid = getThingUID(service);
69         if (uid == null) {
70             return null;
71         }
72
73         logger.debug("createResult is evaluating: {}", service);
74
75         final Map<String, Object> properties = new HashMap<>(2);
76
77         final InetAddress ip = getIpAddress(service);
78         if (ip == null) {
79             logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
80             return null;
81         }
82         final String inetAddress = ip.getHostAddress();
83
84         final String id = uid.getId();
85         final String label = service.getName() + " (" + id + ")";
86
87         properties.put(NeeoConstants.CONFIG_IPADDRESS, inetAddress);
88         properties.put(NeeoConstants.CONFIG_ENABLEFORWARDACTIONS, true);
89
90         logger.debug("Adding NEEO Brain to inbox: {} at {}", id, inetAddress);
91         return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
92     }
93
94     @Nullable
95     @Override
96     public ThingUID getThingUID(@Nullable ServiceInfo service) {
97         if (service == null) {
98             return null;
99         }
100
101         logger.debug("getThingUID is evaluating: {}", service);
102         if (!"neeo".equals(service.getApplication())) {
103             logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
104             return null;
105         }
106
107         if (getIpAddress(service) == null) {
108             logger.debug("No IP address found in MDNS serviceinfo: {}", service);
109             return null;
110         }
111
112         String model = service.getPropertyString("hon"); // model
113         if (model == null) {
114             final String server = service.getServer(); // NEEO-xxxxx.local.
115             if (server != null) {
116                 final int idx = server.indexOf(".");
117                 if (idx >= 0) {
118                     model = server.substring(0, idx);
119                 }
120             }
121         }
122         if (model == null || model.length() <= 5 || !model.toLowerCase().startsWith("neeo")) {
123             logger.debug("No 'hon' found in MDNS serviceinfo: {}", service);
124             return null;
125         }
126
127         final String id = model.substring(5);
128         logger.debug("NEEO Brain Found: {}", id);
129
130         return new ThingUID(BRIDGE_TYPE_BRAIN, id);
131     }
132
133     /**
134      * Gets the ip address found in the {@link ServiceInfo}
135      *
136      * @param service a non-null service
137      * @return the ip address of the service or null if none found.
138      */
139     @Nullable
140     private InetAddress getIpAddress(ServiceInfo service) {
141         Objects.requireNonNull(service, "service cannot be null");
142
143         for (String addr : service.getHostAddresses()) {
144             try {
145                 return InetAddress.getByName(addr);
146             } catch (UnknownHostException e) {
147                 // ignore
148             }
149         }
150
151         for (InetAddress addr : service.getInet4Addresses()) {
152             return addr;
153         }
154         // Fallback for Inet6addresses
155         for (InetAddress addr : service.getInet6Addresses()) {
156             return addr;
157         }
158         return null;
159     }
160 }