2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hyperion.internal;
15 import java.math.BigDecimal;
16 import java.util.Collections;
17 import java.util.HashMap;
21 import javax.jmdns.ServiceInfo;
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;
31 * The {@link HyperionDiscoveryParticipant} class is responsible for listening
32 * to MDNS responses and discovering Hyperion.ng servers.
34 * @author Daniel Walters - Initial contribution
38 public class HyperionDiscoveryParticipant implements MDNSDiscoveryParticipant {
41 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
42 return Collections.singleton(HyperionBindingConstants.THING_TYPE_SERVER_NG);
46 public String getServiceType() {
47 return "_hyperiond-json._tcp.local.";
51 public DiscoveryResult createResult(ServiceInfo service) {
52 DiscoveryResult result = null;
54 // return null if the service info is invalid / not fully formed
55 if (service.getHostAddresses().length == 0) {
59 final Map<String, Object> properties = new HashMap<>(2);
60 String host = service.getHostAddresses()[0];
61 BigDecimal port = new BigDecimal(service.getPort());
63 properties.put(HyperionBindingConstants.HOST, host);
64 properties.put(HyperionBindingConstants.PORT, port);
66 String longName = service.getName();
67 int pos = longName.indexOf("@");
68 if (pos < 0 || pos >= longName.length()) {
72 String friendlyName = longName.substring(0, pos);
73 ThingUID uid = getThingUID(service);
75 result = DiscoveryResultBuilder.create(uid).withThingType(HyperionBindingConstants.THING_TYPE_SERVER_NG)
76 .withProperties(properties).withLabel(friendlyName).build();
82 public ThingUID getThingUID(ServiceInfo service) {
83 String uid = service.getPropertyString("id");
85 String longName = service.getName();
86 int hashCode = longName.hashCode();
87 uid = Integer.toUnsignedString(hashCode);
89 return new ThingUID(HyperionBindingConstants.THING_TYPE_SERVER_NG, uid);