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 if (this.discoveryServiceReg.containsKey(thingHandler)) {
96 PulseaudioDeviceDiscoveryService service = (PulseaudioDeviceDiscoveryService) bundleContext
97 .getService(discoveryServiceReg.get(thingHandler).getReference());
99 discoveryServiceReg.get(thingHandler).unregister();
100 discoveryServiceReg.remove(thingHandler);
102 super.removeHandler(thingHandler);
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);
112 } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
113 return new PulseaudioHandler(thing);
120 protected synchronized void activate(ComponentContext componentContext) {
121 super.activate(componentContext);
122 modified(componentContext);
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) {
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));
137 logger.debug("update received {}: {}", k, properties.get(k));