]> git.basschouten.com Git - openhab-addons.git/blob
2d3d9ab4122171d692fec0068d7e557835e5cba2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
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.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Modified;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link PulseaudioHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author Tobias Bräutigam - Initial contribution
50  */
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.pulseaudio")
52 @NonNullByDefault
53 public class PulseaudioHandlerFactory extends BaseThingHandlerFactory {
54     private final Logger logger = LoggerFactory.getLogger(PulseaudioHandlerFactory.class);
55
56     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
57             .unmodifiableSet(Stream.concat(PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
58                     PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.stream()).collect(Collectors.toSet()));
59
60     private final Map<ThingHandler, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
61
62     private PulseAudioBindingConfiguration configuration = new PulseAudioBindingConfiguration();
63
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     @Override
70     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
71             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
72         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
73             return super.createThing(thingTypeUID, configuration, thingUID, null);
74         }
75         if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
76             ThingUID deviceUID = getPulseaudioDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
77             return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
78         }
79         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
80     }
81
82     private void registerDeviceDiscoveryService(PulseaudioBridgeHandler paBridgeHandler) {
83         PulseaudioDeviceDiscoveryService discoveryService = new PulseaudioDeviceDiscoveryService(paBridgeHandler);
84         discoveryService.activate();
85         this.discoveryServiceReg.put(paBridgeHandler,
86                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
87     }
88
89     private ThingUID getPulseaudioDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
90             Configuration configuration, @Nullable ThingUID bridgeUID) {
91         if (thingUID == null) {
92             String name = (String) configuration.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME_OR_DESCRIPTION);
93             return new ThingUID(thingTypeUID, name, bridgeUID == null ? null : bridgeUID.getId());
94         }
95         return thingUID;
96     }
97
98     @Override
99     protected void removeHandler(ThingHandler thingHandler) {
100         ServiceRegistration<?> serviceRegistration = this.discoveryServiceReg.get(thingHandler);
101         if (serviceRegistration != null) {
102             PulseaudioDeviceDiscoveryService service = (PulseaudioDeviceDiscoveryService) bundleContext
103                     .getService(serviceRegistration.getReference());
104             if (service != null) {
105                 service.deactivate();
106             }
107             serviceRegistration.unregister();
108         }
109         discoveryServiceReg.remove(thingHandler);
110         super.removeHandler(thingHandler);
111     }
112
113     @Override
114     protected @Nullable ThingHandler createHandler(Thing thing) {
115         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
116
117         if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
118             PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing, configuration);
119             registerDeviceDiscoveryService(handler);
120             return handler;
121         } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
122             return new PulseaudioHandler(thing, bundleContext);
123         }
124
125         return null;
126     }
127
128     // The activate component call is used to access the bindings configuration
129     @Activate
130     protected synchronized void activate(ComponentContext componentContext, Map<String, Object> config) {
131         super.activate(componentContext);
132         modified(config);
133     }
134
135     @Modified
136     protected void modified(Map<String, Object> config) {
137         configuration.update(new Configuration(config).as(PulseAudioBindingConfiguration.class));
138         logger.debug("pulseaudio configuration update received ({})", config);
139     }
140 }