]> git.basschouten.com Git - openhab-addons.git/blob
e983dbfc58804f51c395a989d2d38625bfab4494
[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.neohub.internal;
14
15 import java.net.Inet4Address;
16 import java.util.Collections;
17 import java.util.Set;
18
19 import javax.jmdns.ServiceInfo;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29
30 /**
31  * Discovers NeoHubs by means of mDNS-SD
32  *
33  * @author Andrew Fiddian-Green - Initial contribution
34  */
35 @NonNullByDefault
36 @Component
37 public class NeoHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
38
39     private static final String HEATMISER_NEO_HUB = "Heatmiser neoHub";
40
41     /**
42      * Check if the {@link ServiceInfo} refers to a valid NeoHub, and if so return its IPv4 address
43      *
44      * @param serviceInfo
45      * @return the ip address if it is a valid neohub, or null if not
46      */
47     private String getIpAddressIfValidNeoHub(ServiceInfo serviceInfo) {
48         if (serviceInfo.getName().contains(HEATMISER_NEO_HUB)) {
49             for (Inet4Address ipAddr : serviceInfo.getInet4Addresses()) {
50                 String ipStr = ipAddr.getHostAddress();
51                 return ipStr;
52             }
53         }
54         return "";
55     }
56
57     @Override
58     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
59         return Collections.singleton(NeoHubBindingConstants.THING_TYPE_NEOHUB);
60     }
61
62     @Override
63     public String getServiceType() {
64         return "_hap._tcp.local.";
65     }
66
67     @Override
68     public @Nullable DiscoveryResult createResult(ServiceInfo serviceInfo) {
69         String ipStr = getIpAddressIfValidNeoHub(serviceInfo);
70         if (!ipStr.isEmpty()) {
71             ThingUID thingUID = new ThingUID(NeoHubBindingConstants.THING_TYPE_NEOHUB, ipStr.replace('.', '_'));
72             DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
73                     .withProperty(NeoHubConfiguration.HOST_NAME, ipStr)
74                     .withRepresentationProperty(NeoHubConfiguration.HOST_NAME).withLabel("NeoHub (" + ipStr + ")")
75                     .build();
76             return hub;
77         }
78         return null;
79     }
80
81     @Override
82     public @Nullable ThingUID getThingUID(ServiceInfo serviceInfo) {
83         String ipStr = getIpAddressIfValidNeoHub(serviceInfo);
84         if (!ipStr.isEmpty()) {
85             return new ThingUID(NeoHubBindingConstants.THING_TYPE_NEOHUB, ipStr.replace('.', '_'));
86         }
87         return null;
88     }
89 }