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;
18 import javax.jmdns.ServiceInfo;
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;
30 * Discovers NeoHubs by means of mDNS-SD
32 * @author Andrew Fiddian-Green - Initial contribution
36 public class NeoHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
38 private static final String HEATMISER_NEO_HUB = "Heatmiser neoHub";
41 * Check if the {@link ServiceInfo} refers to a valid NeoHub, and if so return its IPv4 address
44 * @return the ip address if it is a valid neohub, or null if not
46 private String getIpAddressIfValidNeoHub(ServiceInfo serviceInfo) {
47 if (serviceInfo.getName().contains(HEATMISER_NEO_HUB)) {
48 for (Inet4Address ipAddr : serviceInfo.getInet4Addresses()) {
49 return ipAddr.getHostAddress();
56 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
57 return Set.of(NeoHubBindingConstants.THING_TYPE_NEOHUB);
61 public String getServiceType() {
62 return "_hap._tcp.local.";
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 + ")")
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('.', '_'));