]> git.basschouten.com Git - openhab-addons.git/blob
b91f06c04f09a6ce32f4d7d7a81002246ab9c2eb
[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.chromecast.internal.factory;
14
15 import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.SUPPORTED_THING_TYPES_UIDS;
16
17 import java.util.Dictionary;
18 import java.util.Map;
19 import java.util.concurrent.ConcurrentHashMap;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.chromecast.internal.ChromecastAudioSink;
24 import org.openhab.binding.chromecast.internal.handler.ChromecastHandler;
25 import org.openhab.core.audio.AudioHTTPServer;
26 import org.openhab.core.audio.AudioSink;
27 import org.openhab.core.net.HttpServiceUtil;
28 import org.openhab.core.net.NetworkAddressService;
29 import org.openhab.core.thing.Thing;
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 /**
43  * The {@link ChromecastHandlerFactory} is responsible for creating things and thing handlers.
44  *
45  * @author Kai Kreuzer - Initial contribution
46  */
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.chromecast")
48 @NonNullByDefault
49 public class ChromecastHandlerFactory extends BaseThingHandlerFactory {
50     private final Logger logger = LoggerFactory.getLogger(ChromecastHandlerFactory.class);
51
52     private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
53     private final AudioHTTPServer audioHTTPServer;
54     private final NetworkAddressService networkAddressService;
55
56     /** url (scheme+server+port) to use for playing notification sounds. */
57     private @Nullable String callbackUrl;
58
59     @Activate
60     public ChromecastHandlerFactory(final @Reference AudioHTTPServer audioHTTPServer,
61             final @Reference NetworkAddressService networkAddressService) {
62         logger.debug("Creating new instance of ChromecastHandlerFactory");
63         this.audioHTTPServer = audioHTTPServer;
64         this.networkAddressService = networkAddressService;
65     }
66
67     @Override
68     protected void activate(ComponentContext componentContext) {
69         super.activate(componentContext);
70         Dictionary<String, Object> properties = componentContext.getProperties();
71         callbackUrl = (String) properties.get("callbackUrl");
72     }
73
74     @Override
75     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
77     }
78
79     @Override
80     protected @Nullable ThingHandler createHandler(Thing thing) {
81         ChromecastHandler handler = new ChromecastHandler(thing);
82         ChromecastAudioSink audioSink = new ChromecastAudioSink(handler, audioHTTPServer, createCallbackUrl());
83
84         @SuppressWarnings("unchecked")
85         ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
86                 .registerService(AudioSink.class.getName(), audioSink, null);
87         audioSinkRegistrations.put(thing.getUID().toString(), reg);
88
89         return handler;
90     }
91
92     private @Nullable String createCallbackUrl() {
93         if (callbackUrl != null) {
94             return callbackUrl;
95         } else {
96             final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
97             if (ipAddress == null) {
98                 logger.warn("No network interface could be found.");
99                 return null;
100             }
101
102             // we do not use SSL as it can cause certificate validation issues.
103             final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
104             if (port == -1) {
105                 logger.warn("Cannot find port of the http service.");
106                 return null;
107             }
108
109             return "http://" + ipAddress + ":" + port;
110         }
111     }
112
113     @Override
114     public void unregisterHandler(Thing thing) {
115         super.unregisterHandler(thing);
116         ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
117         reg.unregister();
118     }
119 }