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