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