From 44e3f9c90f74e7ac87abf0ff85d0eb6518ad6494 Mon Sep 17 00:00:00 2001 From: Andrew Fiddian-Green Date: Wed, 21 Oct 2020 16:50:44 +0100 Subject: [PATCH] [neohub] added automatic discovery of NeoHubs (#8805) * [neohub] add mdns hub discovery * [neohub] mdns discovery improved code style * [neohub] changes in response to reviewer feedback Signed-off-by: Andrew Fiddian-Green --- .../neohub/internal/NeoHubConfiguration.java | 3 + .../internal/NeoHubDiscoveryParticipant.java | 89 +++++++++++++++++++ .../resources/OH-INF/thing/thing-types.xml | 1 + 3 files changed, 93 insertions(+) create mode 100644 bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubDiscoveryParticipant.java diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubConfiguration.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubConfiguration.java index 22d467af27b..0dde2182eb6 100644 --- a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubConfiguration.java +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubConfiguration.java @@ -22,6 +22,9 @@ import org.eclipse.jdt.annotation.NonNullByDefault; */ @NonNullByDefault public class NeoHubConfiguration { + + public static final String HOST_NAME = "hostName"; + public String hostName = ""; public int portNumber; public int pollingInterval; diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubDiscoveryParticipant.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubDiscoveryParticipant.java new file mode 100644 index 00000000000..07e78650fc5 --- /dev/null +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubDiscoveryParticipant.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.neohub.internal; + +import java.net.Inet4Address; +import java.util.Collections; +import java.util.Set; + +import javax.jmdns.ServiceInfo; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.config.discovery.DiscoveryResult; +import org.openhab.core.config.discovery.DiscoveryResultBuilder; +import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant; +import org.openhab.core.thing.ThingTypeUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.service.component.annotations.Component; + +/** + * Discovers NeoHubs by means of mDNS-SD + * + * @author Andrew Fiddian-Green - Initial contribution + */ +@NonNullByDefault +@Component +public class NeoHubDiscoveryParticipant implements MDNSDiscoveryParticipant { + + private static final String HEATMISER_NEO_HUB = "Heatmiser neoHub"; + + /** + * Check if the {@link ServiceInfo} refers to a valid NeoHub, and if so return its IPv4 address + * + * @param serviceInfo + * @return the ip address if it is a valid neohub, or null if not + */ + private String getIpAddressIfValidNeoHub(ServiceInfo serviceInfo) { + if (serviceInfo.getName().contains(HEATMISER_NEO_HUB)) { + for (Inet4Address ipAddr : serviceInfo.getInet4Addresses()) { + String ipStr = ipAddr.getHostAddress(); + return ipStr; + } + } + return ""; + } + + @Override + public Set getSupportedThingTypeUIDs() { + return Collections.singleton(NeoHubBindingConstants.THING_TYPE_NEOHUB); + } + + @Override + public String getServiceType() { + return "_hap._tcp.local."; + } + + @Override + public @Nullable DiscoveryResult createResult(ServiceInfo serviceInfo) { + String ipStr = getIpAddressIfValidNeoHub(serviceInfo); + if (!ipStr.isEmpty()) { + ThingUID thingUID = new ThingUID(NeoHubBindingConstants.THING_TYPE_NEOHUB, ipStr.replace('.', '_')); + DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID) + .withProperty(NeoHubConfiguration.HOST_NAME, ipStr) + .withRepresentationProperty(NeoHubConfiguration.HOST_NAME).withLabel("NeoHub (" + ipStr + ")") + .build(); + return hub; + } + return null; + } + + @Override + public @Nullable ThingUID getThingUID(ServiceInfo serviceInfo) { + String ipStr = getIpAddressIfValidNeoHub(serviceInfo); + if (!ipStr.isEmpty()) { + return new ThingUID(NeoHubBindingConstants.THING_TYPE_NEOHUB, ipStr.replace('.', '_')); + } + return null; + } +} diff --git a/bundles/org.openhab.binding.neohub/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.neohub/src/main/resources/OH-INF/thing/thing-types.xml index 7ac5026909b..5d667650047 100644 --- a/bundles/org.openhab.binding.neohub/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.neohub/src/main/resources/OH-INF/thing/thing-types.xml @@ -17,6 +17,7 @@ Heatmiser NeoHub + hostName