2 * Copyright (c) 2010-2023 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.chromecast.internal.factory;
15 import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.SUPPORTED_THING_TYPES_UIDS;
17 import java.util.Dictionary;
19 import java.util.concurrent.ConcurrentHashMap;
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;
43 * The {@link ChromecastHandlerFactory} is responsible for creating things and thing handlers.
45 * @author Kai Kreuzer - Initial contribution
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.chromecast")
49 public class ChromecastHandlerFactory extends BaseThingHandlerFactory {
50 private final Logger logger = LoggerFactory.getLogger(ChromecastHandlerFactory.class);
52 private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
53 private final AudioHTTPServer audioHTTPServer;
54 private final NetworkAddressService networkAddressService;
56 /** url (scheme+server+port) to use for playing notification sounds. */
57 private @Nullable String callbackUrl;
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;
68 protected void activate(ComponentContext componentContext) {
69 super.activate(componentContext);
70 Dictionary<String, Object> properties = componentContext.getProperties();
71 callbackUrl = (String) properties.get("callbackUrl");
75 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
80 protected @Nullable ThingHandler createHandler(Thing thing) {
81 ChromecastHandler handler = new ChromecastHandler(thing);
82 ChromecastAudioSink audioSink = new ChromecastAudioSink(handler, audioHTTPServer, createCallbackUrl());
84 @SuppressWarnings("unchecked")
85 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
86 .registerService(AudioSink.class.getName(), audioSink, null);
87 audioSinkRegistrations.put(thing.getUID().toString(), reg);
92 private @Nullable String createCallbackUrl() {
93 if (callbackUrl != null) {
96 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
97 if (ipAddress == null) {
98 logger.warn("No network interface could be found.");
102 // we do not use SSL as it can cause certificate validation issues.
103 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
105 logger.warn("Cannot find port of the http service.");
109 return "http://" + ipAddress + ":" + port;
114 public void unregisterHandler(Thing thing) {
115 super.unregisterHandler(thing);
116 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());