2 * Copyright (c) 2010-2022 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.pulseaudio.internal.discovery;
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.util.HashMap;
21 import javax.jmdns.ServiceInfo;
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;
35 * The {@link PulseaudioDiscoveryParticipant} is responsible processing the
36 * results of searches for mDNS services of type _pulse-server._tcp.local.
38 * @author Tobias Bräutigam - Initial contribution
41 public class PulseaudioDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private final Logger logger = LoggerFactory.getLogger(PulseaudioDiscoveryParticipant.class);
46 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47 return PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS;
51 public DiscoveryResult createResult(ServiceInfo info) {
52 DiscoveryResult result = null;
53 ThingUID uid = getThingUID(info);
55 Map<String, Object> properties = new HashMap<>(3);
56 String label = "Pulseaudio server";
58 label = info.getName();
59 } catch (Exception e) {
60 // ignore and use default label
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);
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();
72 logger.trace("Created a DiscoveryResult for device '{}' on host '{}'", info.getName(), hostname);
75 } catch (IOException e) {
82 public ThingUID getThingUID(ServiceInfo info) {
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_"));
97 public String getServiceType() {
98 return "_pulse-server._tcp.local.";