]> git.basschouten.com Git - openhab-addons.git/blob
3a6fa1e29e17aee9abca6dcd8f8608d943df6a19
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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;
14
15 import java.util.Collections;
16 import java.util.Dictionary;
17 import java.util.Enumeration;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
25 import org.openhab.binding.pulseaudio.internal.discovery.PulseaudioDeviceDiscoveryService;
26 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioBridgeHandler;
27 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioHandler;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.ComponentContext;
39 import org.osgi.service.component.annotations.Component;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The {@link PulseaudioHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Tobias Bräutigam - Initial contribution
48  */
49 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.pulseaudio")
50 public class PulseaudioHandlerFactory extends BaseThingHandlerFactory {
51     private final Logger logger = LoggerFactory.getLogger(PulseaudioHandlerFactory.class);
52
53     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
54             .unmodifiableSet(Stream.concat(PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
55                     PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.stream()).collect(Collectors.toSet()));
56
57     private final Map<ThingHandler, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
58
59     @Override
60     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62     }
63
64     @Override
65     public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
66             ThingUID bridgeUID) {
67         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
68             return super.createThing(thingTypeUID, configuration, thingUID, null);
69         }
70         if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
71             ThingUID deviceUID = getPulseaudioDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
72             return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
73         }
74         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
75     }
76
77     private void registerDeviceDiscoveryService(PulseaudioBridgeHandler paBridgeHandler) {
78         PulseaudioDeviceDiscoveryService discoveryService = new PulseaudioDeviceDiscoveryService(paBridgeHandler);
79         discoveryService.activate();
80         this.discoveryServiceReg.put(paBridgeHandler,
81                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
82     }
83
84     private ThingUID getPulseaudioDeviceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
85             ThingUID bridgeUID) {
86         if (thingUID == null) {
87             String name = (String) configuration.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME);
88             return new ThingUID(thingTypeUID, name, bridgeUID.getId());
89         }
90         return thingUID;
91     }
92
93     @Override
94     protected void removeHandler(ThingHandler thingHandler) {
95         if (this.discoveryServiceReg.containsKey(thingHandler)) {
96             PulseaudioDeviceDiscoveryService service = (PulseaudioDeviceDiscoveryService) bundleContext
97                     .getService(discoveryServiceReg.get(thingHandler).getReference());
98             service.deactivate();
99             discoveryServiceReg.get(thingHandler).unregister();
100             discoveryServiceReg.remove(thingHandler);
101         }
102         super.removeHandler(thingHandler);
103     }
104
105     @Override
106     protected ThingHandler createHandler(Thing thing) {
107         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
108         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
109             PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing);
110             registerDeviceDiscoveryService(handler);
111             return handler;
112         } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
113             return new PulseaudioHandler(thing);
114         }
115
116         return null;
117     }
118
119     @Override
120     protected synchronized void activate(ComponentContext componentContext) {
121         super.activate(componentContext);
122         modified(componentContext);
123     }
124
125     protected synchronized void modified(ComponentContext componentContext) {
126         Dictionary<String, ?> properties = componentContext.getProperties();
127         logger.info("pulseaudio configuration update received ({})", properties);
128         if (properties == null) {
129             return;
130         }
131         Enumeration<String> e = properties.keys();
132         while (e.hasMoreElements()) {
133             String k = e.nextElement();
134             if (PulseaudioBindingConstants.TYPE_FILTERS.containsKey(k)) {
135                 PulseaudioBindingConstants.TYPE_FILTERS.put(k, (boolean) properties.get(k));
136             }
137             logger.debug("update received {}: {}", k, properties.get(k));
138         }
139     }
140 }