]> git.basschouten.com Git - openhab-addons.git/blob
20260d6473ade4aa865615dcf9ce60f0cd4f719c
[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.upnpcontrol.internal;
14
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.*;
16
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.jupnp.UpnpService;
25 import org.jupnp.model.meta.LocalDevice;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.jupnp.registry.Registry;
28 import org.jupnp.registry.RegistryListener;
29 import org.openhab.binding.upnpcontrol.internal.audiosink.UpnpAudioSink;
30 import org.openhab.binding.upnpcontrol.internal.audiosink.UpnpAudioSinkReg;
31 import org.openhab.binding.upnpcontrol.internal.audiosink.UpnpNotificationAudioSink;
32 import org.openhab.binding.upnpcontrol.internal.config.UpnpControlBindingConfiguration;
33 import org.openhab.binding.upnpcontrol.internal.handler.UpnpHandler;
34 import org.openhab.binding.upnpcontrol.internal.handler.UpnpRendererHandler;
35 import org.openhab.binding.upnpcontrol.internal.handler.UpnpServerHandler;
36 import org.openhab.core.audio.AudioHTTPServer;
37 import org.openhab.core.audio.AudioSink;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.io.transport.upnp.UpnpIOService;
40 import org.openhab.core.net.HttpServiceUtil;
41 import org.openhab.core.net.NetworkAddressService;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
45 import org.openhab.core.thing.binding.ThingHandler;
46 import org.openhab.core.thing.binding.ThingHandlerFactory;
47 import org.osgi.framework.ServiceRegistration;
48 import org.osgi.service.component.annotations.Activate;
49 import org.osgi.service.component.annotations.Component;
50 import org.osgi.service.component.annotations.Deactivate;
51 import org.osgi.service.component.annotations.Modified;
52 import org.osgi.service.component.annotations.Reference;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 /**
57  * The {@link UpnpControlHandlerFactory} is responsible for creating things and thing
58  * handlers.
59  *
60  * @author Mark Herwege - Initial contribution
61  */
62 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.upnpcontrol")
63 @NonNullByDefault
64 public class UpnpControlHandlerFactory extends BaseThingHandlerFactory implements UpnpAudioSinkReg, RegistryListener {
65     final UpnpControlBindingConfiguration configuration = new UpnpControlBindingConfiguration();
66
67     private final Logger logger = LoggerFactory.getLogger(UpnpControlHandlerFactory.class);
68
69     private ConcurrentMap<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
70     private ConcurrentMap<String, UpnpRendererHandler> upnpRenderers = new ConcurrentHashMap<>();
71     private ConcurrentMap<String, UpnpServerHandler> upnpServers = new ConcurrentHashMap<>();
72     private ConcurrentMap<String, UpnpHandler> handlers = new ConcurrentHashMap<>();
73     private ConcurrentMap<String, RemoteDevice> devices = new ConcurrentHashMap<>();
74
75     private final UpnpIOService upnpIOService;
76     private final UpnpService upnpService;
77     private final AudioHTTPServer audioHTTPServer;
78     private final NetworkAddressService networkAddressService;
79     private final UpnpDynamicStateDescriptionProvider upnpStateDescriptionProvider;
80     private final UpnpDynamicCommandDescriptionProvider upnpCommandDescriptionProvider;
81
82     private String callbackUrl = "";
83
84     @Activate
85     public UpnpControlHandlerFactory(final @Reference UpnpIOService upnpIOService, @Reference UpnpService upnpService,
86             final @Reference AudioHTTPServer audioHTTPServer,
87             final @Reference NetworkAddressService networkAddressService,
88             final @Reference UpnpDynamicStateDescriptionProvider dynamicStateDescriptionProvider,
89             final @Reference UpnpDynamicCommandDescriptionProvider dynamicCommandDescriptionProvider,
90             Map<String, Object> config) {
91         this.upnpIOService = upnpIOService;
92         this.upnpService = upnpService;
93         this.audioHTTPServer = audioHTTPServer;
94         this.networkAddressService = networkAddressService;
95         this.upnpStateDescriptionProvider = dynamicStateDescriptionProvider;
96         this.upnpCommandDescriptionProvider = dynamicCommandDescriptionProvider;
97
98         upnpService.getRegistry().addListener(this);
99
100         modified(config);
101     }
102
103     @Modified
104     protected void modified(Map<String, Object> config) {
105         // We update instead of replace the configuration object, so that if the user updates the
106         // configuration, the values are automatically available in all handlers. Because they all
107         // share the same instance.
108         configuration.update(new Configuration(config).as(UpnpControlBindingConfiguration.class));
109         logger.debug("Updated binding configuration to {}", configuration);
110     }
111
112     @Deactivate
113     protected void deActivate() {
114         upnpService.getRegistry().removeListener(this);
115     }
116
117     @Override
118     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
119         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
120     }
121
122     @Override
123     protected @Nullable ThingHandler createHandler(Thing thing) {
124         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
125
126         if (thingTypeUID.equals(THING_TYPE_RENDERER)) {
127             return addRenderer(thing);
128         } else if (thingTypeUID.equals(THING_TYPE_SERVER)) {
129             return addServer(thing);
130         }
131         return null;
132     }
133
134     @Override
135     public void unregisterHandler(Thing thing) {
136         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
137         String key = thing.getUID().toString();
138
139         if (thingTypeUID.equals(THING_TYPE_RENDERER)) {
140             removeRenderer(key);
141         } else if (thingTypeUID.equals(THING_TYPE_SERVER)) {
142             removeServer(key);
143         }
144         super.unregisterHandler(thing);
145     }
146
147     private UpnpServerHandler addServer(Thing thing) {
148         UpnpServerHandler handler = new UpnpServerHandler(thing, upnpIOService, upnpRenderers,
149                 upnpStateDescriptionProvider, upnpCommandDescriptionProvider, configuration);
150         String key = thing.getUID().toString();
151         upnpServers.put(key, handler);
152         logger.debug("Media server handler created for {} with UID {}", thing.getLabel(), thing.getUID());
153
154         String udn = handler.getUDN();
155         if (udn != null) {
156             handlers.put(udn, handler);
157             remoteDeviceUpdated(null, devices.get(udn));
158         }
159
160         return handler;
161     }
162
163     private UpnpRendererHandler addRenderer(Thing thing) {
164         callbackUrl = createCallbackUrl();
165         UpnpRendererHandler handler = new UpnpRendererHandler(thing, upnpIOService, this, upnpStateDescriptionProvider,
166                 upnpCommandDescriptionProvider, configuration);
167         String key = thing.getUID().toString();
168         upnpRenderers.put(key, handler);
169         upnpServers.forEach((thingId, value) -> value.addRendererOption(key));
170         logger.debug("Media renderer handler created for {} with UID {}", thing.getLabel(), thing.getUID());
171
172         String udn = handler.getUDN();
173         if (udn != null) {
174             handlers.put(udn, handler);
175             remoteDeviceUpdated(null, devices.get(udn));
176         }
177
178         return handler;
179     }
180
181     private void removeServer(String key) {
182         UpnpHandler handler = upnpServers.get(key);
183         if (handler == null) {
184             return;
185         }
186         logger.debug("Removing media server handler for {} with UID {}", handler.getThing().getLabel(),
187                 handler.getThing().getUID());
188         handlers.remove(handler.getUDN());
189         upnpServers.remove(key);
190     }
191
192     private void removeRenderer(String key) {
193         UpnpHandler handler = upnpServers.get(key);
194         if (handler == null) {
195             return;
196         }
197         logger.debug("Removing media renderer handler for {} with UID {}", handler.getThing().getLabel(),
198                 handler.getThing().getUID());
199
200         if (audioSinkRegistrations.containsKey(key)) {
201             logger.debug("Removing audio sink registration for {}", handler.getThing().getLabel());
202             ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(key);
203             if (reg != null) {
204                 reg.unregister();
205             }
206             audioSinkRegistrations.remove(key);
207         }
208
209         String notificationKey = key + NOTIFICATION_AUDIOSINK_EXTENSION;
210         if (audioSinkRegistrations.containsKey(notificationKey)) {
211             logger.debug("Removing notification audio sink registration for {}", handler.getThing().getLabel());
212             ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(notificationKey);
213             if (reg != null) {
214                 reg.unregister();
215             }
216             audioSinkRegistrations.remove(notificationKey);
217         }
218
219         upnpServers.forEach((thingId, value) -> value.removeRendererOption(key));
220         handlers.remove(handler.getUDN());
221         upnpRenderers.remove(key);
222     }
223
224     @Override
225     public void registerAudioSink(UpnpRendererHandler handler) {
226         if (!(callbackUrl.isEmpty())) {
227             UpnpAudioSink audioSink = new UpnpAudioSink(handler, audioHTTPServer, callbackUrl);
228             @SuppressWarnings("unchecked")
229             ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
230                     .registerService(AudioSink.class.getName(), audioSink, new Hashtable<String, Object>());
231             Thing thing = handler.getThing();
232             audioSinkRegistrations.put(thing.getUID().toString(), reg);
233             logger.debug("Audio sink added for media renderer {}", thing.getLabel());
234
235             UpnpNotificationAudioSink notificationAudioSink = new UpnpNotificationAudioSink(handler, audioHTTPServer,
236                     callbackUrl);
237             @SuppressWarnings("unchecked")
238             ServiceRegistration<AudioSink> notificationReg = (ServiceRegistration<AudioSink>) bundleContext
239                     .registerService(AudioSink.class.getName(), notificationAudioSink, new Hashtable<String, Object>());
240             audioSinkRegistrations.put(thing.getUID().toString() + NOTIFICATION_AUDIOSINK_EXTENSION, notificationReg);
241             logger.debug("Notification audio sink added for media renderer {}", thing.getLabel());
242         }
243     }
244
245     private String createCallbackUrl() {
246         if (!callbackUrl.isEmpty()) {
247             return callbackUrl;
248         }
249         NetworkAddressService nwaService = networkAddressService;
250         String ipAddress = nwaService.getPrimaryIpv4HostAddress();
251         if (ipAddress == null) {
252             logger.warn("No network interface could be found.");
253             return "";
254         }
255         int port = HttpServiceUtil.getHttpServicePort(bundleContext);
256         if (port == -1) {
257             logger.warn("Cannot find port of the http service.");
258             return "";
259         }
260         return "http://" + ipAddress + ":" + port;
261     }
262
263     @Override
264     public void remoteDeviceDiscoveryStarted(@Nullable Registry registry, @Nullable RemoteDevice device) {
265     }
266
267     @Override
268     public void remoteDeviceDiscoveryFailed(@Nullable Registry registry, @Nullable RemoteDevice device,
269             @Nullable Exception ex) {
270     }
271
272     @Override
273     public void remoteDeviceAdded(@Nullable Registry registry, @Nullable RemoteDevice device) {
274         if (device == null) {
275             return;
276         }
277
278         String udn = device.getIdentity().getUdn().getIdentifierString();
279         if ("MediaServer".equals(device.getType().getType()) || "MediaRenderer".equals(device.getType().getType())) {
280             devices.put(udn, device);
281         }
282
283         if (handlers.containsKey(udn)) {
284             remoteDeviceUpdated(registry, device);
285         }
286     }
287
288     @Override
289     public void remoteDeviceUpdated(@Nullable Registry registry, @Nullable RemoteDevice device) {
290         if (device == null) {
291             return;
292         }
293
294         String udn = device.getIdentity().getUdn().getIdentifierString();
295         UpnpHandler handler = handlers.get(udn);
296         if (handler != null) {
297             handler.updateDeviceConfig(device);
298         }
299     }
300
301     @Override
302     public void remoteDeviceRemoved(@Nullable Registry registry, @Nullable RemoteDevice device) {
303         if (device == null) {
304             return;
305         }
306         devices.remove(device.getIdentity().getUdn().getIdentifierString());
307     }
308
309     @Override
310     public void localDeviceAdded(@Nullable Registry registry, @Nullable LocalDevice device) {
311     }
312
313     @Override
314     public void localDeviceRemoved(@Nullable Registry registry, @Nullable LocalDevice device) {
315     }
316
317     @Override
318     public void beforeShutdown(@Nullable Registry registry) {
319         devices = new ConcurrentHashMap<>();
320     }
321
322     @Override
323     public void afterShutdown() {
324     }
325 }