]> git.basschouten.com Git - openhab-addons.git/blob
fdb94b7d81b5e4ccf684da2be000a0b30aa0e552
[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.Set;
18
19 import javax.jmdns.ServiceInfo;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Discovers HD PowerView hubs by means of mDNS
35  *
36  * @author Andrew Fiddian-Green - Initial contribution
37  */
38 @NonNullByDefault
39 @Component
40 public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
41
42     private static final String LABEL_KEY = "discovery.hub.label";
43
44     private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Set.of(THING_TYPE_HUB);
49     }
50
51     @Override
52     public String getServiceType() {
53         return "_powerview._tcp.local.";
54     }
55
56     @Override
57     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
58         for (String host : service.getHostAddresses()) {
59             if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
60                 ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
61                 DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
62                         .withProperty(HDPowerViewHubConfiguration.HOST, host)
63                         .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
64                         .withLabel(String.format("@text/%s [\"%s\"]", LABEL_KEY, host)).build();
65                 logger.debug("mDNS discovered Gen 1/2 hub on host '{}'", host);
66                 return hub;
67             }
68         }
69         return null;
70     }
71
72     @Override
73     public @Nullable ThingUID getThingUID(ServiceInfo service) {
74         for (String host : service.getHostAddresses()) {
75             if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
76                 return new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
77             }
78         }
79         return null;
80     }
81 }