2 * Copyright (c) 2010-2022 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.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
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;
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<>();
59 private PulseAudioBindingConfiguration configuration = new PulseAudioBindingConfiguration();
62 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
69 if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
70 return super.createThing(thingTypeUID, configuration, thingUID, null);
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);
76 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
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<>()));
86 private ThingUID getPulseaudioDeviceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
88 if (thingUID == null) {
89 String name = (String) configuration.get(PulseaudioBindingConstants.DEVICE_PARAMETER_NAME);
90 return new ThingUID(thingTypeUID, name, bridgeUID.getId());
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();
104 discoveryServiceReg.remove(thingHandler);
105 super.removeHandler(thingHandler);
109 protected ThingHandler createHandler(Thing thing) {
111 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
113 if (PulseaudioBridgeHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
114 PulseaudioBridgeHandler handler = new PulseaudioBridgeHandler((Bridge) thing, configuration);
115 registerDeviceDiscoveryService(handler);
117 } else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
118 return new PulseaudioHandler(thing, bundleContext);
124 // The activate component call is used to access the bindings configuration
126 protected synchronized void activate(ComponentContext componentContext, Map<String, Object> config) {
127 super.activate(componentContext);
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);