]> git.basschouten.com Git - openhab-addons.git/blob
06c32ab26605d9ef35ea90df704ae38ae62b426f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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         ServiceRegistration<?> serviceRegistration = this.discoveryServiceReg.get(thingHandler);
96         if (serviceRegistration != null) {
97             PulseaudioDeviceDiscoveryService service = (PulseaudioDeviceDiscoveryService) bundleContext
98                     .getService(serviceRegistration.getReference());
99             service.deactivate();
100             serviceRegistration.unregister();
101         }
102         discoveryServiceReg.remove(thingHandler);
103         super.removeHandler(thingHandler);
104     }
105
106     @Override
107     protected ThingHandler createHandler(Thing thing) {
108
109         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
110
111         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
112             PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing);
113             registerDeviceDiscoveryService(handler);
114             return handler;
115         } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
116             return new PulseaudioHandler(thing, bundleContext);
117         }
118
119         return null;
120     }
121
122     @Override
123     protected synchronized void activate(ComponentContext componentContext) {
124         super.activate(componentContext);
125         modified(componentContext);
126     }
127
128     protected synchronized void modified(ComponentContext componentContext) {
129         Dictionary<String, ?> properties = componentContext.getProperties();
130         logger.info("pulseaudio configuration update received ({})", properties);
131         if (properties == null) {
132             return;
133         }
134         Enumeration<String> e = properties.keys();
135         while (e.hasMoreElements()) {
136             String k = e.nextElement();
137             if (PulseaudioBindingConstants.TYPE_FILTERS.containsKey(k)) {
138                 PulseaudioBindingConstants.TYPE_FILTERS.put(k, (boolean) properties.get(k));
139             }
140             logger.debug("update received {}: {}", k, properties.get(k));
141         }
142     }
143 }