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.Collections;
17 import java.util.HashMap;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.mpd.internal.MPDBindingConstants;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Implementation of {@link MDNSDiscoveryParticipant} that will discover Music Player Daemons.
38 * @author Stefan Röllin - Initial contribution
43 public class MPDDiscoveryParticipant implements MDNSDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(MPDDiscoveryParticipant.class);
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return Collections.singleton(MPDBindingConstants.THING_TYPE_MPD);
53 public String getServiceType() {
54 return "_mpd._tcp.local.";
59 public DiscoveryResult createResult(ServiceInfo service) {
60 ThingUID uid = getThingUID(service);
61 String host = getHostAddress(service);
62 int port = service.getPort();
64 logger.debug("Music Player Daemon found on host {} port {}", host, port);
66 if (uid == null || host == null || host.isEmpty()) {
70 String uniquePropVal = String.format("%s-%d", host, port);
72 final Map<String, Object> properties = new HashMap<>(3);
73 properties.put(MPDBindingConstants.PARAMETER_IPADDRESS, host);
74 properties.put(MPDBindingConstants.PARAMETER_PORT, port);
75 properties.put(MPDBindingConstants.UNIQUE_ID, uniquePropVal);
77 String name = service.getName();
79 final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel(name).withProperties(properties)
80 .withRepresentationProperty(MPDBindingConstants.UNIQUE_ID).build();
85 private String getHostAddress(ServiceInfo service) {
86 if (service.getInet4Addresses() != null) {
87 for (Inet4Address addr : service.getInet4Addresses()) {
89 return addr.getHostAddress();
98 public ThingUID getThingUID(ServiceInfo service) {
99 if (getServiceType().equals(service.getType())) {
100 String name = getUIDName(service.getName());
101 if (!name.isEmpty()) {
102 return new ThingUID(MPDBindingConstants.THING_TYPE_MPD, name);
108 private String getUIDName(@Nullable String serviceName) {
109 if (serviceName == null) {
112 return serviceName.replaceAll("[^A-Za-z0-9_]", "_").replaceAll("_+", "_");