]> git.basschouten.com Git - openhab-addons.git/blob
ae6e246fc366b5cb63317e45d90cd0b095320bd5
[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.pulseaudio.internal.discovery;
14
15 import java.io.IOException;
16 import java.net.Socket;
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.pulseaudio.internal.PulseaudioBindingConstants;
26 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioBridgeHandler;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The {@link PulseaudioDiscoveryParticipant} is responsible processing the
38  * results of searches for mDNS services of type _pulse-server._tcp.local.
39  *
40  * @author Tobias Bräutigam - Initial contribution
41  */
42 @Component
43 @NonNullByDefault
44 public class PulseaudioDiscoveryParticipant implements MDNSDiscoveryParticipant {
45
46     private final Logger logger = LoggerFactory.getLogger(PulseaudioDiscoveryParticipant.class);
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS;
51     }
52
53     @Override
54     public @Nullable DiscoveryResult createResult(ServiceInfo info) {
55         DiscoveryResult result = null;
56         ThingUID uid = getThingUID(info);
57         if (uid != null) {
58             Map<String, Object> properties = new HashMap<>(3);
59             String label = "Pulseaudio server";
60             try {
61                 label = info.getName();
62             } catch (Exception e) {
63                 // ignore and use default label
64             }
65             // remove the domain from the name
66             String hostname = info.getServer().replace("." + info.getDomain() + ".", "");
67             try (Socket testSocket = new Socket(hostname, 4712)) {
68                 logger.debug("testing connection to pulseaudio server {}:4712", hostname);
69
70                 if (testSocket.isConnected()) {
71                     properties.put(PulseaudioBindingConstants.BRIDGE_PARAMETER_HOST, hostname);
72                     // we do not read the port here because the given port is 4713 and we need 4712 to query the server
73                     result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
74
75                     logger.trace("Created a DiscoveryResult for device '{}' on host '{}'", info.getName(), hostname);
76                 }
77                 return result;
78             } catch (IOException e) {
79             }
80         }
81         return result;
82     }
83
84     @Override
85     public @Nullable ThingUID getThingUID(ServiceInfo info) {
86         logger.debug("ServiceInfo: {}", info);
87         if (info.getType() != null) {
88             if (info.getType().equals(getServiceType())) {
89                 logger.trace("Discovered a pulseaudio server thing with name '{}'", info.getName());
90                 return new ThingUID(PulseaudioBindingConstants.BRIDGE_THING_TYPE, info.getName().replace("@", "_AT_"));
91             }
92         }
93         return null;
94     }
95
96     @Override
97     public String getServiceType() {
98         return "_pulse-server._tcp.local.";
99     }
100 }