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.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;
42 * The {@link ChromecastHandlerFactory} is responsible for creating things and thing handlers.
44 * @author Kai Kreuzer - Initial contribution
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.chromecast")
48 public class ChromecastHandlerFactory extends BaseThingHandlerFactory {
49 private final Logger logger = LoggerFactory.getLogger(ChromecastHandlerFactory.class);
51 private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
52 private final AudioHTTPServer audioHTTPServer;
53 private final NetworkAddressService networkAddressService;
55 /** url (scheme+server+port) to use for playing notification sounds. */
56 private @Nullable String callbackUrl;
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;
67 protected void activate(ComponentContext componentContext) {
68 super.activate(componentContext);
69 Dictionary<String, Object> properties = componentContext.getProperties();
70 callbackUrl = (String) properties.get("callbackUrl");
74 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
79 protected @Nullable ThingHandler createHandler(Thing thing) {
80 ChromecastHandler handler = new ChromecastHandler(thing, audioHTTPServer, createCallbackUrl());
82 @SuppressWarnings("unchecked")
83 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
84 .registerService(AudioSink.class.getName(), handler, null);
85 audioSinkRegistrations.put(thing.getUID().toString(), reg);
90 private @Nullable String createCallbackUrl() {
91 if (callbackUrl != null) {
94 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
95 if (ipAddress == null) {
96 logger.warn("No network interface could be found.");
100 // we do not use SSL as it can cause certificate validation issues.
101 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
103 logger.warn("Cannot find port of the http service.");
107 return "http://" + ipAddress + ":" + port;
112 public void unregisterHandler(Thing thing) {
113 super.unregisterHandler(thing);
114 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());