2 * Copyright (c) 2010-2023 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.allplay.internal;
15 import static org.openhab.binding.allplay.internal.AllPlayBindingConstants.SPEAKER_THING_TYPE;
17 import java.util.Dictionary;
18 import java.util.Hashtable;
21 import java.util.concurrent.ConcurrentHashMap;
23 import org.openhab.binding.allplay.internal.handler.AllPlayHandler;
24 import org.openhab.core.audio.AudioHTTPServer;
25 import org.openhab.core.audio.AudioSink;
26 import org.openhab.core.net.HttpServiceUtil;
27 import org.openhab.core.net.NetworkAddressService;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingRegistry;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.framework.ServiceRegistration;
35 import org.osgi.service.component.ComponentContext;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import de.kaizencode.tchaikovsky.AllPlay;
43 import de.kaizencode.tchaikovsky.exception.AllPlayException;
46 * The {@link AllPlayHandlerFactory} is responsible for creating things and thing
49 * @author Dominic Lerbs - Initial contribution
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.allplay")
52 public class AllPlayHandlerFactory extends BaseThingHandlerFactory {
54 private final Logger logger = LoggerFactory.getLogger(AllPlayHandlerFactory.class);
56 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(SPEAKER_THING_TYPE);
57 private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
59 private AllPlay allPlay;
60 private AllPlayBindingProperties bindingProperties;
62 // Bindings should not use the ThingRegistry! See https://github.com/openhab/openhab-addons/pull/6080 and
63 // https://github.com/eclipse/smarthome/issues/5182
64 private final ThingRegistry thingRegistry;
65 private final AudioHTTPServer audioHTTPServer;
66 private final NetworkAddressService networkAddressService;
67 private String callbackUrl;
70 public AllPlayHandlerFactory(final @Reference ThingRegistry thingRegistry,
71 final @Reference AudioHTTPServer audioHTTPServer,
72 final @Reference NetworkAddressService networkAddressService) {
73 this.thingRegistry = thingRegistry;
74 this.audioHTTPServer = audioHTTPServer;
75 this.networkAddressService = networkAddressService;
79 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
80 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
84 protected ThingHandler createHandler(Thing thing) {
85 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86 if (thingTypeUID.equals(SPEAKER_THING_TYPE)) {
87 logger.debug("Creating AllPlayHandler for thing {}", thing.getUID());
89 AllPlayHandler handler = new AllPlayHandler(thingRegistry, thing, allPlay, bindingProperties);
90 registerAudioSink(thing, handler);
97 private void registerAudioSink(Thing thing, AllPlayHandler handler) {
98 AllPlayAudioSink audioSink = new AllPlayAudioSink(handler, audioHTTPServer, callbackUrl);
99 @SuppressWarnings("unchecked")
100 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
101 .registerService(AudioSink.class.getName(), audioSink, new Hashtable<>());
102 audioSinkRegistrations.put(thing.getUID().toString(), reg);
106 public void unregisterHandler(Thing thing) {
107 super.unregisterHandler(thing);
108 unregisterAudioSink(thing);
111 private void unregisterAudioSink(Thing thing) {
112 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
119 protected void activate(ComponentContext componentContext) {
120 super.activate(componentContext);
121 logger.debug("Activating AllPlayHandlerFactory");
122 allPlay = new AllPlay("openHAB2");
124 logger.debug("Connecting to AllPlay");
126 } catch (AllPlayException e) {
127 logger.error("Cannot initialize AllPlay", e);
129 Dictionary<String, Object> properties = componentContext.getProperties();
130 bindingProperties = new AllPlayBindingProperties(properties);
131 callbackUrl = assembleCallbackUrl();
135 protected void deactivate(ComponentContext componentContext) {
136 logger.debug("Deactivating AllPlayHandlerFactory");
137 allPlay.disconnect();
139 super.deactivate(componentContext);
142 private String assembleCallbackUrl() {
143 String callbackUrl = bindingProperties.getCallbackUrl();
144 if (callbackUrl == null) {
145 String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
146 if (ipAddress == null) {
147 logger.warn("No network interface could be found.");
150 // we do not use SSL as it can cause certificate validation issues.
151 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
153 logger.warn("Cannot find port of the http service.");
156 callbackUrl = "http://" + ipAddress + ":" + port;