]> git.basschouten.com Git - openhab-addons.git/blob
2c95c33423e40a77c402ab2a9bde8cd224ded7ba
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hdpowerview.internal.discovery;
14
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.regex.Pattern;
20
21 import javax.jmdns.ServiceInfo;
22
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;
34
35 /**
36  * Discovers HD PowerView hubs by means of mDNS
37  *
38  * @author Andrew Fiddian-Green - Initial contribution
39  */
40 @NonNullByDefault
41 @Component
42 public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class);
45
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");
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51         return Collections.singleton(THING_TYPE_HUB);
52     }
53
54     @Override
55     public String getServiceType() {
56         return "_powerview._tcp.local.";
57     }
58
59     @Override
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);
69                 return hub;
70             }
71         }
72         return null;
73     }
74
75     @Override
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('.', '_'));
80             }
81         }
82         return null;
83     }
84 }