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.HashMap;
20 import javax.jmdns.ServiceInfo;
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;
30 * The {@link HyperionDiscoveryParticipant} class is responsible for listening
31 * to MDNS responses and discovering Hyperion.ng servers.
33 * @author Daniel Walters - Initial contribution
37 public class HyperionDiscoveryParticipant implements MDNSDiscoveryParticipant {
40 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
41 return Set.of(HyperionBindingConstants.THING_TYPE_SERVER_NG);
45 public String getServiceType() {
46 return "_hyperiond-json._tcp.local.";
50 public DiscoveryResult createResult(ServiceInfo service) {
51 DiscoveryResult result = null;
53 // return null if the service info is invalid / not fully formed
54 if (service.getHostAddresses().length == 0) {
58 final Map<String, Object> properties = new HashMap<>(2);
59 String host = service.getHostAddresses()[0];
60 BigDecimal port = new BigDecimal(service.getPort());
62 properties.put(HyperionBindingConstants.HOST, host);
63 properties.put(HyperionBindingConstants.PORT, port);
65 String longName = service.getName();
66 int pos = longName.indexOf("@");
67 if (pos < 0 || pos >= longName.length()) {
71 String friendlyName = longName.substring(0, pos);
72 ThingUID uid = getThingUID(service);
74 result = DiscoveryResultBuilder.create(uid).withThingType(HyperionBindingConstants.THING_TYPE_SERVER_NG)
75 .withProperties(properties).withLabel(friendlyName).build();
81 public ThingUID getThingUID(ServiceInfo service) {
82 String uid = service.getPropertyString("id");
84 String longName = service.getName();
85 int hashCode = longName.hashCode();
86 uid = Integer.toUnsignedString(hashCode);
88 return new ThingUID(HyperionBindingConstants.THING_TYPE_SERVER_NG, uid);