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