2 * Copyright (c) 2010-2020 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.HashMap;
17 import java.util.Hashtable;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.freebox.internal.discovery.FreeboxDiscoveryService;
27 import org.openhab.binding.freebox.internal.handler.FreeboxHandler;
28 import org.openhab.binding.freebox.internal.handler.FreeboxThingHandler;
29 import org.openhab.core.audio.AudioHTTPServer;
30 import org.openhab.core.audio.AudioSink;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.config.discovery.DiscoveryService;
33 import org.openhab.core.i18n.TimeZoneProvider;
34 import org.openhab.core.net.HttpServiceUtil;
35 import org.openhab.core.net.NetworkAddressService;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.framework.ServiceRegistration;
44 import org.osgi.service.component.ComponentContext;
45 import org.osgi.service.component.annotations.Activate;
46 import org.osgi.service.component.annotations.Component;
47 import org.osgi.service.component.annotations.Reference;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * The {@link FreeboxHandlerFactory} is responsible for creating things and thing
55 * @author Gaƫl L'hopital - Initial contribution
56 * @author Laurent Garnier - several thing types and handlers + discovery service
59 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.freebox")
60 public class FreeboxHandlerFactory extends BaseThingHandlerFactory {
62 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
63 .concat(FreeboxBindingConstants.SUPPORTED_BRIDGE_TYPES_UIDS.stream(),
64 FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS.stream())
65 .collect(Collectors.toSet());
67 private final Logger logger = LoggerFactory.getLogger(FreeboxHandlerFactory.class);
69 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
70 private final Map<ThingUID, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
72 private final AudioHTTPServer audioHTTPServer;
73 private final NetworkAddressService networkAddressService;
74 private final TimeZoneProvider timeZoneProvider;
76 // url (scheme+server+port) to use for playing notification sounds
77 private @Nullable String callbackUrl;
80 public FreeboxHandlerFactory(final @Reference AudioHTTPServer audioHTTPServer,
81 final @Reference NetworkAddressService networkAddressService,
82 final @Reference TimeZoneProvider timeZoneProvider) {
83 this.audioHTTPServer = audioHTTPServer;
84 this.networkAddressService = networkAddressService;
85 this.timeZoneProvider = timeZoneProvider;
89 protected void activate(ComponentContext componentContext) {
90 super.activate(componentContext);
91 Dictionary<String, Object> properties = componentContext.getProperties();
92 callbackUrl = (String) properties.get("callbackUrl");
96 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
97 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
101 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
102 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
103 if (thingTypeUID.equals(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER)) {
104 return super.createThing(thingTypeUID, configuration, thingUID, null);
105 } else if (FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
106 ThingUID newThingUID;
107 if (bridgeUID != null && thingUID != null) {
108 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
110 newThingUID = thingUID;
112 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
114 throw new IllegalArgumentException(
115 "The thing type " + thingTypeUID + " is not supported by the Freebox binding.");
119 protected @Nullable ThingHandler createHandler(Thing thing) {
120 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
122 if (thingTypeUID.equals(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER)) {
123 FreeboxHandler handler = new FreeboxHandler((Bridge) thing);
124 registerDiscoveryService(handler);
126 } else if (FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
127 FreeboxThingHandler handler = new FreeboxThingHandler(thing, timeZoneProvider);
128 if (FreeboxBindingConstants.FREEBOX_THING_TYPE_AIRPLAY.equals(thingTypeUID)) {
129 registerAudioSink(handler);
138 protected void removeHandler(ThingHandler thingHandler) {
139 if (thingHandler instanceof FreeboxHandler) {
140 unregisterDiscoveryService(thingHandler.getThing());
141 } else if (thingHandler instanceof FreeboxThingHandler) {
142 unregisterAudioSink(thingHandler.getThing());
146 private synchronized void registerDiscoveryService(FreeboxHandler bridgeHandler) {
147 FreeboxDiscoveryService discoveryService = new FreeboxDiscoveryService(bridgeHandler);
148 discoveryService.activate(null);
149 discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
150 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
153 private synchronized void unregisterDiscoveryService(Thing thing) {
154 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
155 if (serviceReg != null) {
156 // remove discovery service, if bridge handler is removed
157 FreeboxDiscoveryService service = (FreeboxDiscoveryService) bundleContext
158 .getService(serviceReg.getReference());
159 serviceReg.unregister();
160 if (service != null) {
161 service.deactivate();
166 private synchronized void registerAudioSink(FreeboxThingHandler thingHandler) {
167 String callbackUrl = createCallbackUrl();
168 FreeboxAirPlayAudioSink audioSink = new FreeboxAirPlayAudioSink(thingHandler, audioHTTPServer, callbackUrl);
169 @SuppressWarnings("unchecked")
170 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
171 .registerService(AudioSink.class.getName(), audioSink, new Hashtable<>());
172 audioSinkRegistrations.put(thingHandler.getThing().getUID(), reg);
175 private synchronized void unregisterAudioSink(Thing thing) {
176 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.remove(thing.getUID());
182 private @Nullable String createCallbackUrl() {
183 if (callbackUrl != null) {
186 String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
187 if (ipAddress == null) {
188 logger.warn("No network interface could be found.");
192 // we do not use SSL as it can cause certificate validation issues.
193 int port = HttpServiceUtil.getHttpServicePort(bundleContext);
195 logger.warn("Cannot find port of the http service.");
199 return "http://" + ipAddress + ":" + port;