2 * Copyright (c) 2010-2022 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.freebox.internal;
15 import java.util.Dictionary;
16 import java.util.Hashtable;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.freebox.internal.handler.FreeboxHandler;
26 import org.openhab.binding.freebox.internal.handler.FreeboxThingHandler;
27 import org.openhab.core.audio.AudioHTTPServer;
28 import org.openhab.core.audio.AudioSink;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.i18n.TimeZoneProvider;
31 import org.openhab.core.net.HttpServiceUtil;
32 import org.openhab.core.net.NetworkAddressService;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.framework.ServiceRegistration;
41 import org.osgi.service.component.ComponentContext;
42 import org.osgi.service.component.annotations.Activate;
43 import org.osgi.service.component.annotations.Component;
44 import org.osgi.service.component.annotations.Reference;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link FreeboxHandlerFactory} is responsible for creating things and thing
52 * @author Gaƫl L'hopital - Initial contribution
53 * @author Laurent Garnier - several thing types and handlers + discovery service
56 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.freebox")
57 public class FreeboxHandlerFactory extends BaseThingHandlerFactory {
59 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
60 .concat(FreeboxBindingConstants.SUPPORTED_BRIDGE_TYPES_UIDS.stream(),
61 FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS.stream())
62 .collect(Collectors.toSet());
64 private final Logger logger = LoggerFactory.getLogger(FreeboxHandlerFactory.class);
66 private final Map<ThingUID, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
68 private final AudioHTTPServer audioHTTPServer;
69 private final NetworkAddressService networkAddressService;
70 private final TimeZoneProvider timeZoneProvider;
72 // url (scheme+server+port) to use for playing notification sounds
73 private @Nullable String callbackUrl;
76 public FreeboxHandlerFactory(final @Reference AudioHTTPServer audioHTTPServer,
77 final @Reference NetworkAddressService networkAddressService,
78 final @Reference TimeZoneProvider timeZoneProvider) {
79 this.audioHTTPServer = audioHTTPServer;
80 this.networkAddressService = networkAddressService;
81 this.timeZoneProvider = timeZoneProvider;
85 protected void activate(ComponentContext componentContext) {
86 super.activate(componentContext);
87 Dictionary<String, Object> properties = componentContext.getProperties();
88 callbackUrl = (String) properties.get("callbackUrl");
92 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
93 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
97 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
98 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
99 if (thingTypeUID.equals(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER)) {
100 return super.createThing(thingTypeUID, configuration, thingUID, null);
101 } else if (FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
102 ThingUID newThingUID;
103 if (bridgeUID != null && thingUID != null) {
104 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
106 newThingUID = thingUID;
108 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
110 throw new IllegalArgumentException(
111 "The thing type " + thingTypeUID + " is not supported by the Freebox binding.");
115 protected @Nullable ThingHandler createHandler(Thing thing) {
116 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
118 if (thingTypeUID.equals(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER)) {
119 return new FreeboxHandler((Bridge) thing);
120 } else if (FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
121 FreeboxThingHandler handler = new FreeboxThingHandler(thing, timeZoneProvider);
122 if (FreeboxBindingConstants.FREEBOX_THING_TYPE_AIRPLAY.equals(thingTypeUID)) {
123 registerAudioSink(handler);
132 protected void removeHandler(ThingHandler thingHandler) {
133 if (thingHandler instanceof FreeboxThingHandler) {
134 unregisterAudioSink(thingHandler.getThing());
138 private synchronized void registerAudioSink(FreeboxThingHandler thingHandler) {
139 String callbackUrl = createCallbackUrl();
140 FreeboxAirPlayAudioSink audioSink = new FreeboxAirPlayAudioSink(thingHandler, audioHTTPServer, callbackUrl);
141 @SuppressWarnings("unchecked")
142 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
143 .registerService(AudioSink.class.getName(), audioSink, new Hashtable<>());
144 audioSinkRegistrations.put(thingHandler.getThing().getUID(), reg);
147 private synchronized void unregisterAudioSink(Thing thing) {
148 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.remove(thing.getUID());
154 private @Nullable String createCallbackUrl() {
155 if (callbackUrl != null) {
158 String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
159 if (ipAddress == null) {
160 logger.warn("No network interface could be found.");
164 // we do not use SSL as it can cause certificate validation issues.
165 int port = HttpServiceUtil.getHttpServicePort(bundleContext);
167 logger.warn("Cannot find port of the http service.");
171 return "http://" + ipAddress + ":" + port;