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.mpd.internal.discovery;
15 import java.net.Inet4Address;
16 import java.util.HashMap;
20 import javax.jmdns.ServiceInfo;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.mpd.internal.MPDBindingConstants;
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;
35 * Implementation of {@link MDNSDiscoveryParticipant} that will discover Music Player Daemons.
37 * @author Stefan Röllin - Initial contribution
42 public class MPDDiscoveryParticipant implements MDNSDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(MPDDiscoveryParticipant.class);
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Set.of(MPDBindingConstants.THING_TYPE_MPD);
52 public String getServiceType() {
53 return "_mpd._tcp.local.";
58 public DiscoveryResult createResult(ServiceInfo service) {
59 ThingUID uid = getThingUID(service);
60 String host = getHostAddress(service);
61 int port = service.getPort();
63 logger.debug("Music Player Daemon found on host {} port {}", host, port);
65 if (uid == null || host == null || host.isEmpty()) {
69 String uniquePropVal = String.format("%s-%d", host, port);
71 final Map<String, Object> properties = new HashMap<>(3);
72 properties.put(MPDBindingConstants.PARAMETER_IPADDRESS, host);
73 properties.put(MPDBindingConstants.PARAMETER_PORT, port);
74 properties.put(MPDBindingConstants.UNIQUE_ID, uniquePropVal);
76 String name = service.getName();
78 return DiscoveryResultBuilder.create(uid).withLabel(name).withProperties(properties)
79 .withRepresentationProperty(MPDBindingConstants.UNIQUE_ID).build();
83 private String getHostAddress(ServiceInfo service) {
84 if (service.getInet4Addresses() != null) {
85 for (Inet4Address addr : service.getInet4Addresses()) {
87 return addr.getHostAddress();
96 public ThingUID getThingUID(ServiceInfo service) {
97 if (getServiceType().equals(service.getType())) {
98 String name = getUIDName(service.getName());
99 if (!name.isEmpty()) {
100 return new ThingUID(MPDBindingConstants.THING_TYPE_MPD, name);
106 private String getUIDName(@Nullable String serviceName) {
107 if (serviceName == null) {
110 return serviceName.replaceAll("[^A-Za-z0-9_]", "_").replaceAll("_+", "_");