]> git.basschouten.com Git - openhab-addons.git/blob
42988e404d89a614d8adc92b4938f2eaeeb76fb3
[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.mpd.internal.discovery;
14
15 import java.net.Inet4Address;
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.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;
33
34 /**
35  * Implementation of {@link MDNSDiscoveryParticipant} that will discover Music Player Daemons.
36  *
37  * @author Stefan Röllin - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 @Component
42 public class MPDDiscoveryParticipant implements MDNSDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(MPDDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Set.of(MPDBindingConstants.THING_TYPE_MPD);
49     }
50
51     @Override
52     public String getServiceType() {
53         return "_mpd._tcp.local.";
54     }
55
56     @Override
57     @Nullable
58     public DiscoveryResult createResult(ServiceInfo service) {
59         ThingUID uid = getThingUID(service);
60         String host = getHostAddress(service);
61         int port = service.getPort();
62
63         logger.debug("Music Player Daemon found on host {} port {}", host, port);
64
65         if (uid == null || host == null || host.isEmpty()) {
66             return null;
67         }
68
69         String uniquePropVal = String.format("%s-%d", host, port);
70
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);
75
76         String name = service.getName();
77
78         return DiscoveryResultBuilder.create(uid).withLabel(name).withProperties(properties)
79                 .withRepresentationProperty(MPDBindingConstants.UNIQUE_ID).build();
80     }
81
82     @Nullable
83     private String getHostAddress(ServiceInfo service) {
84         if (service.getInet4Addresses() != null) {
85             for (Inet4Address addr : service.getInet4Addresses()) {
86                 if (addr != null) {
87                     return addr.getHostAddress();
88                 }
89             }
90         }
91         return null;
92     }
93
94     @Override
95     @Nullable
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);
101             }
102         }
103         return null;
104     }
105
106     private String getUIDName(@Nullable String serviceName) {
107         if (serviceName == null) {
108             return "";
109         }
110         return serviceName.replaceAll("[^A-Za-z0-9_]", "_").replaceAll("_+", "_");
111     }
112 }