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