]> git.basschouten.com Git - openhab-addons.git/blob
8d3a43f55e075a14bb4220880cf023c947aa677e
[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.allplay.internal;
14
15 import static org.openhab.binding.allplay.internal.AllPlayBindingConstants.SPEAKER_THING_TYPE;
16
17 import java.util.Dictionary;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22
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;
41
42 import de.kaizencode.tchaikovsky.AllPlay;
43 import de.kaizencode.tchaikovsky.exception.AllPlayException;
44
45 /**
46  * The {@link AllPlayHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author Dominic Lerbs - Initial contribution
50  */
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.allplay")
52 public class AllPlayHandlerFactory extends BaseThingHandlerFactory {
53
54     private final Logger logger = LoggerFactory.getLogger(AllPlayHandlerFactory.class);
55
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<>();
58
59     private AllPlay allPlay;
60     private AllPlayBindingProperties bindingProperties;
61
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;
68
69     @Activate
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;
76     }
77
78     @Override
79     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
80         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
81     }
82
83     @Override
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());
88
89             AllPlayHandler handler = new AllPlayHandler(thingRegistry, thing, allPlay, bindingProperties);
90             registerAudioSink(thing, handler);
91
92             return handler;
93         }
94         return null;
95     }
96
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);
103     }
104
105     @Override
106     public void unregisterHandler(Thing thing) {
107         super.unregisterHandler(thing);
108         unregisterAudioSink(thing);
109     }
110
111     private void unregisterAudioSink(Thing thing) {
112         ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
113         if (reg != null) {
114             reg.unregister();
115         }
116     }
117
118     @Override
119     protected void activate(ComponentContext componentContext) {
120         super.activate(componentContext);
121         logger.debug("Activating AllPlayHandlerFactory");
122         allPlay = new AllPlay("openHAB2");
123         try {
124             logger.debug("Connecting to AllPlay");
125             allPlay.connect();
126         } catch (AllPlayException e) {
127             logger.error("Cannot initialize AllPlay", e);
128         }
129         Dictionary<String, Object> properties = componentContext.getProperties();
130         bindingProperties = new AllPlayBindingProperties(properties);
131         callbackUrl = assembleCallbackUrl();
132     }
133
134     @Override
135     protected void deactivate(ComponentContext componentContext) {
136         logger.debug("Deactivating AllPlayHandlerFactory");
137         allPlay.disconnect();
138         allPlay = null;
139         super.deactivate(componentContext);
140     }
141
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.");
148                 return null;
149             }
150             // we do not use SSL as it can cause certificate validation issues.
151             final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
152             if (port == -1) {
153                 logger.warn("Cannot find port of the http service.");
154                 return null;
155             }
156             callbackUrl = "http://" + ipAddress + ":" + port;
157         }
158         return callbackUrl;
159     }
160 }