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.neohub.internal;
15 import java.net.Inet4Address;
16 import java.util.Collections;
19 import javax.jmdns.ServiceInfo;
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;
31 * Discovers NeoHubs by means of mDNS-SD
33 * @author Andrew Fiddian-Green - Initial contribution
37 public class NeoHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
39 private static final String HEATMISER_NEO_HUB = "Heatmiser neoHub";
42 * Check if the {@link ServiceInfo} refers to a valid NeoHub, and if so return its IPv4 address
45 * @return the ip address if it is a valid neohub, or null if not
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();
58 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
59 return Collections.singleton(NeoHubBindingConstants.THING_TYPE_NEOHUB);
63 public String getServiceType() {
64 return "_hap._tcp.local.";
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 + ")")
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('.', '_'));