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.hdpowerview.internal.discovery;
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
17 import java.util.Collections;
19 import java.util.regex.Pattern;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Discovers HD PowerView hubs by means of mDNS
38 * @author Andrew Fiddian-Green - Initial contribution
42 public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class);
46 private static final Pattern VALID_IP_V4_ADDRESS = Pattern
47 .compile("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return Collections.singleton(THING_TYPE_HUB);
55 public String getServiceType() {
56 return "_powerview._tcp.local.";
60 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
61 for (String host : service.getHostAddresses()) {
62 if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
63 ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
64 DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
65 .withProperty(HDPowerViewHubConfiguration.HOST, host)
66 .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
67 .withLabel("PowerView Hub (" + host + ")").build();
68 logger.debug("mDNS discovered hub on host '{}'", host);
76 public @Nullable ThingUID getThingUID(ServiceInfo service) {
77 for (String host : service.getHostAddresses()) {
78 if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
79 return new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));