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