]> git.basschouten.com Git - openhab-addons.git/blob
8a8cdee117017e49f5096c7bb98741ef799624d8
[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.hyperion.internal;
14
15 import java.math.BigDecimal;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29
30 /**
31  * The {@link HyperionDiscoveryParticipant} class is responsible for listening
32  * to MDNS responses and discovering Hyperion.ng servers.
33  *
34  * @author Daniel Walters - Initial contribution
35  */
36
37 @Component
38 public class HyperionDiscoveryParticipant implements MDNSDiscoveryParticipant {
39
40     @Override
41     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
42         return Collections.singleton(HyperionBindingConstants.THING_TYPE_SERVER_NG);
43     }
44
45     @Override
46     public String getServiceType() {
47         return "_hyperiond-json._tcp.local.";
48     }
49
50     @Override
51     public DiscoveryResult createResult(ServiceInfo service) {
52         DiscoveryResult result = null;
53
54         // return null if the service info is invalid / not fully formed
55         if (service.getHostAddresses().length == 0) {
56             return null;
57         }
58
59         final Map<String, Object> properties = new HashMap<>(2);
60         String host = service.getHostAddresses()[0];
61         BigDecimal port = new BigDecimal(service.getPort());
62
63         properties.put(HyperionBindingConstants.HOST, host);
64         properties.put(HyperionBindingConstants.PORT, port);
65
66         String longName = service.getName();
67         int pos = longName.indexOf("@");
68         if (pos < 0 || pos >= longName.length()) {
69             return null;
70         }
71
72         String friendlyName = longName.substring(0, pos);
73         ThingUID uid = getThingUID(service);
74         if (uid != null) {
75             result = DiscoveryResultBuilder.create(uid).withThingType(HyperionBindingConstants.THING_TYPE_SERVER_NG)
76                     .withProperties(properties).withLabel(friendlyName).build();
77         }
78         return result;
79     }
80
81     @Override
82     public ThingUID getThingUID(ServiceInfo service) {
83         String uid = service.getPropertyString("id");
84         if (uid == null) {
85             String longName = service.getName();
86             int hashCode = longName.hashCode();
87             uid = Integer.toUnsignedString(hashCode);
88         }
89         return new ThingUID(HyperionBindingConstants.THING_TYPE_SERVER_NG, uid);
90     }
91 }