]> git.basschouten.com Git - openhab-addons.git/blob
b36e53472461d4d9a01fc86f0be6e5d421da4234
[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.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.pulseaudio.internal.PulseaudioBindingConstants;
22 import org.openhab.binding.pulseaudio.internal.handler.DeviceStatusListener;
23 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioBridgeHandler;
24 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioHandler;
25 import org.openhab.binding.pulseaudio.internal.items.AbstractAudioDeviceConfig;
26 import org.openhab.binding.pulseaudio.internal.items.Sink;
27 import org.openhab.binding.pulseaudio.internal.items.SinkInput;
28 import org.openhab.binding.pulseaudio.internal.items.Source;
29 import org.openhab.binding.pulseaudio.internal.items.SourceOutput;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link PulseaudioDeviceDiscoveryService} class is used to discover Pulseaudio
41  * devices on a Pulseaudio server.
42  *
43  * @author Tobias Bräutigam - Initial contribution
44  */
45 @NonNullByDefault
46 public class PulseaudioDeviceDiscoveryService extends AbstractDiscoveryService implements DeviceStatusListener {
47
48     private final Logger logger = LoggerFactory.getLogger(PulseaudioDeviceDiscoveryService.class);
49
50     private PulseaudioBridgeHandler pulseaudioBridgeHandler;
51
52     public PulseaudioDeviceDiscoveryService(PulseaudioBridgeHandler pulseaudioBridgeHandler) {
53         super(PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS, 10, true);
54         this.pulseaudioBridgeHandler = pulseaudioBridgeHandler;
55     }
56
57     public void activate() {
58         pulseaudioBridgeHandler.registerDeviceStatusListener(this);
59     }
60
61     @Override
62     public void deactivate() {
63         pulseaudioBridgeHandler.unregisterDeviceStatusListener(this);
64     }
65
66     @Override
67     public Set<ThingTypeUID> getSupportedThingTypes() {
68         return PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS;
69     }
70
71     @Override
72     public void onDeviceAdded(Thing bridge, AbstractAudioDeviceConfig device) {
73         if (getAlreadyConfiguredThings().contains(device.getPaName())) {
74             return;
75         }
76
77         String uidName = device.getPaName();
78         logger.debug("device {} found", device);
79         ThingTypeUID thingType = null;
80         Map<String, Object> properties = new HashMap<>();
81         // All devices need this parameter
82         properties.put(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME, uidName);
83         if (device instanceof Sink) {
84             if (((Sink) device).isCombinedSink()) {
85                 thingType = PulseaudioBindingConstants.COMBINED_SINK_THING_TYPE;
86             } else {
87                 thingType = PulseaudioBindingConstants.SINK_THING_TYPE;
88             }
89         } else if (device instanceof SinkInput) {
90             thingType = PulseaudioBindingConstants.SINK_INPUT_THING_TYPE;
91         } else if (device instanceof Source) {
92             thingType = PulseaudioBindingConstants.SOURCE_THING_TYPE;
93         } else if (device instanceof SourceOutput) {
94             thingType = PulseaudioBindingConstants.SOURCE_OUTPUT_THING_TYPE;
95         }
96
97         if (thingType != null) {
98             logger.trace("Adding new pulseaudio {} with name '{}' to inbox", device.getClass().getSimpleName(),
99                     uidName);
100             ThingUID thingUID = new ThingUID(thingType, bridge.getUID(), device.getUIDName());
101             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
102                     .withBridge(bridge.getUID()).withLabel(device.getUIDName()).build();
103             thingDiscovered(discoveryResult);
104         }
105     }
106
107     public Set<String> getAlreadyConfiguredThings() {
108         return pulseaudioBridgeHandler.getThing().getThings().stream().map(Thing::getConfiguration)
109                 .map(conf -> (String) conf.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME))
110                 .collect(Collectors.toSet());
111     }
112
113     @Override
114     protected void startScan() {
115         pulseaudioBridgeHandler.resetKnownActiveDevices();
116     }
117 }