2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pulseaudio.internal;
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;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
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;
44 * The {@link PulseaudioHandlerFactory} is responsible for creating things and thing
47 * @author Tobias Bräutigam - Initial contribution
49 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.pulseaudio")
50 public class PulseaudioHandlerFactory extends BaseThingHandlerFactory {
51 private final Logger logger = LoggerFactory.getLogger(PulseaudioHandlerFactory.class);
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()));
57 private final Map<ThingHandler, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
67 if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
68 return super.createThing(thingTypeUID, configuration, thingUID, null);
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);
74 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
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<>()));
84 private ThingUID getPulseaudioDeviceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
86 if (thingUID == null) {
87 String name = (String) configuration.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME);
88 return new ThingUID(thingTypeUID, name, bridgeUID.getId());
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());
100 serviceRegistration.unregister();
102 discoveryServiceReg.remove(thingHandler);
103 super.removeHandler(thingHandler);
107 protected ThingHandler createHandler(Thing thing) {
109 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
111 if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
112 PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing);
113 registerDeviceDiscoveryService(handler);
115 } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
116 return new PulseaudioHandler(thing, bundleContext);
123 protected synchronized void activate(ComponentContext componentContext) {
124 super.activate(componentContext);
125 modified(componentContext);
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) {
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));
140 logger.debug("update received {}: {}", k, properties.get(k));