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