2 * Copyright (c) 2010-2024 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.upnpcontrol.internal;
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.*;
17 import java.util.Hashtable;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
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;
57 * The {@link UpnpControlHandlerFactory} is responsible for creating things and thing
60 * @author Mark Herwege - Initial contribution
62 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.upnpcontrol")
64 public class UpnpControlHandlerFactory extends BaseThingHandlerFactory implements UpnpAudioSinkReg, RegistryListener {
65 final UpnpControlBindingConfiguration configuration = new UpnpControlBindingConfiguration();
67 private final Logger logger = LoggerFactory.getLogger(UpnpControlHandlerFactory.class);
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<>();
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;
82 private String callbackUrl = "";
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;
98 upnpService.getRegistry().addListener(this);
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);
113 protected void deActivate() {
114 upnpService.getRegistry().removeListener(this);
118 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
119 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
123 protected @Nullable ThingHandler createHandler(Thing thing) {
124 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
126 if (thingTypeUID.equals(THING_TYPE_RENDERER)) {
127 return addRenderer(thing);
128 } else if (thingTypeUID.equals(THING_TYPE_SERVER)) {
129 return addServer(thing);
135 public void unregisterHandler(Thing thing) {
136 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
137 String key = thing.getUID().toString();
139 if (thingTypeUID.equals(THING_TYPE_RENDERER)) {
141 } else if (thingTypeUID.equals(THING_TYPE_SERVER)) {
144 super.unregisterHandler(thing);
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());
154 String udn = handler.getUDN();
156 handlers.put(udn, handler);
157 remoteDeviceUpdated(null, devices.get(udn));
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());
172 String udn = handler.getUDN();
174 handlers.put(udn, handler);
175 remoteDeviceUpdated(null, devices.get(udn));
181 private void removeServer(String key) {
182 UpnpHandler handler = upnpServers.get(key);
183 if (handler == null) {
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);
192 private void removeRenderer(String key) {
193 UpnpHandler handler = upnpServers.get(key);
194 if (handler == null) {
197 logger.debug("Removing media renderer handler for {} with UID {}", handler.getThing().getLabel(),
198 handler.getThing().getUID());
200 if (audioSinkRegistrations.containsKey(key)) {
201 logger.debug("Removing audio sink registration for {}", handler.getThing().getLabel());
202 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(key);
206 audioSinkRegistrations.remove(key);
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);
216 audioSinkRegistrations.remove(notificationKey);
219 upnpServers.forEach((thingId, value) -> value.removeRendererOption(key));
220 handlers.remove(handler.getUDN());
221 upnpRenderers.remove(key);
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());
235 UpnpNotificationAudioSink notificationAudioSink = new UpnpNotificationAudioSink(handler, audioHTTPServer,
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());
245 private String createCallbackUrl() {
246 if (!callbackUrl.isEmpty()) {
249 NetworkAddressService nwaService = networkAddressService;
250 String ipAddress = nwaService.getPrimaryIpv4HostAddress();
251 if (ipAddress == null) {
252 logger.warn("No network interface could be found.");
255 int port = HttpServiceUtil.getHttpServicePort(bundleContext);
257 logger.warn("Cannot find port of the http service.");
260 return "http://" + ipAddress + ":" + port;
264 public void remoteDeviceDiscoveryStarted(@Nullable Registry registry, @Nullable RemoteDevice device) {
268 public void remoteDeviceDiscoveryFailed(@Nullable Registry registry, @Nullable RemoteDevice device,
269 @Nullable Exception ex) {
273 public void remoteDeviceAdded(@Nullable Registry registry, @Nullable RemoteDevice device) {
274 if (device == null) {
278 String udn = device.getIdentity().getUdn().getIdentifierString();
279 if ("MediaServer".equals(device.getType().getType()) || "MediaRenderer".equals(device.getType().getType())) {
280 devices.put(udn, device);
283 if (handlers.containsKey(udn)) {
284 remoteDeviceUpdated(registry, device);
289 public void remoteDeviceUpdated(@Nullable Registry registry, @Nullable RemoteDevice device) {
290 if (device == null) {
294 String udn = device.getIdentity().getUdn().getIdentifierString();
295 UpnpHandler handler = handlers.get(udn);
296 if (handler != null) {
297 handler.updateDeviceConfig(device);
302 public void remoteDeviceRemoved(@Nullable Registry registry, @Nullable RemoteDevice device) {
303 if (device == null) {
306 devices.remove(device.getIdentity().getUdn().getIdentifierString());
310 public void localDeviceAdded(@Nullable Registry registry, @Nullable LocalDevice device) {
314 public void localDeviceRemoved(@Nullable Registry registry, @Nullable LocalDevice device) {
318 public void beforeShutdown(@Nullable Registry registry) {
319 devices = new ConcurrentHashMap<>();
323 public void afterShutdown() {