]> git.basschouten.com Git - openhab-addons.git/commitdiff
[neohub] added automatic discovery of NeoHubs (#8805)
authorAndrew Fiddian-Green <software@whitebear.ch>
Wed, 21 Oct 2020 15:50:44 +0000 (16:50 +0100)
committerGitHub <noreply@github.com>
Wed, 21 Oct 2020 15:50:44 +0000 (08:50 -0700)
* [neohub] add mdns hub discovery
* [neohub] mdns discovery improved code style
* [neohub] changes in response to reviewer feedback

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubConfiguration.java
bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubDiscoveryParticipant.java [new file with mode: 0644]
bundles/org.openhab.binding.neohub/src/main/resources/OH-INF/thing/thing-types.xml

index 22d467af27b0869cd07f4b8c0dcfea5692ef2c22..0dde2182eb6ab88baf653c84477d6f5c7337f7a7 100644 (file)
@@ -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 (file)
index 0000000..07e7865
--- /dev/null
@@ -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<ThingTypeUID> 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;
+    }
+}
index 7ac5026909b421c6547b95d479f54cf65a262570..5d6676500479739027bf8d1a54189f94b97e35c9 100644 (file)
@@ -17,6 +17,7 @@
                        <property name="vendor">Heatmiser</property>
                        <property name="modelId">NeoHub</property>
                </properties>
+               <representation-property>hostName</representation-property>
 
                <config-description>
                        <parameter name="hostName" type="text" required="true">