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