]> git.basschouten.com Git - openhab-addons.git/blob
59943dc790a1e5cbde215f0e1b07f417d1e797d3
[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;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.openhab.binding.pulseaudio.internal.discovery.PulseaudioDeviceDiscoveryService;
24 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioBridgeHandler;
25 import org.openhab.binding.pulseaudio.internal.handler.PulseaudioHandler;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.framework.ServiceRegistration;
36 import org.osgi.service.component.ComponentContext;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Modified;
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     private PulseAudioBindingConfiguration configuration = new PulseAudioBindingConfiguration();
60
61     @Override
62     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
64     }
65
66     @Override
67     public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
68             ThingUID bridgeUID) {
69         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
70             return super.createThing(thingTypeUID, configuration, thingUID, null);
71         }
72         if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
73             ThingUID deviceUID = getPulseaudioDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
74             return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
75         }
76         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
77     }
78
79     private void registerDeviceDiscoveryService(PulseaudioBridgeHandler paBridgeHandler) {
80         PulseaudioDeviceDiscoveryService discoveryService = new PulseaudioDeviceDiscoveryService(paBridgeHandler);
81         discoveryService.activate();
82         this.discoveryServiceReg.put(paBridgeHandler,
83                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
84     }
85
86     private ThingUID getPulseaudioDeviceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
87             ThingUID bridgeUID) {
88         if (thingUID == null) {
89             String name = (String) configuration.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME);
90             return new ThingUID(thingTypeUID, name, bridgeUID.getId());
91         }
92         return thingUID;
93     }
94
95     @Override
96     protected void removeHandler(ThingHandler thingHandler) {
97         ServiceRegistration<?> serviceRegistration = this.discoveryServiceReg.get(thingHandler);
98         if (serviceRegistration != null) {
99             PulseaudioDeviceDiscoveryService service = (PulseaudioDeviceDiscoveryService) bundleContext
100                     .getService(serviceRegistration.getReference());
101             service.deactivate();
102             serviceRegistration.unregister();
103         }
104         discoveryServiceReg.remove(thingHandler);
105         super.removeHandler(thingHandler);
106     }
107
108     @Override
109     protected ThingHandler createHandler(Thing thing) {
110
111         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
112
113         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
114             PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing, configuration);
115             registerDeviceDiscoveryService(handler);
116             return handler;
117         } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
118             return new PulseaudioHandler(thing, bundleContext);
119         }
120
121         return null;
122     }
123
124     // The activate component call is used to access the bindings configuration
125     @Activate
126     protected synchronized void activate(ComponentContext componentContext, Map<String, Object> config) {
127         super.activate(componentContext);
128         modified(config);
129     }
130
131     @Modified
132     protected void modified(Map<String, Object> config) {
133         configuration.update(new Configuration(config).as(PulseAudioBindingConfiguration.class));
134         logger.debug("pulseaudio configuration update received ({})", config);
135     }
136 }