]> git.basschouten.com Git - openhab-addons.git/blob
d2d6e884c4a1c5c66e2b16a20d02056ac318fc1a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.freebox.internal;
14
15 import java.util.Dictionary;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
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;
50
51 /**
52  * The {@link FreeboxHandlerFactory} is responsible for creating things and thing
53  * handlers.
54  *
55  * @author GaĆ«l L'hopital - Initial contribution
56  * @author Laurent Garnier - several thing types and handlers + discovery service
57  */
58 @NonNullByDefault
59 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.freebox")
60 public class FreeboxHandlerFactory extends BaseThingHandlerFactory {
61
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());
66
67     private final Logger logger = LoggerFactory.getLogger(FreeboxHandlerFactory.class);
68
69     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
70     private final Map<ThingUID, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
71
72     private final AudioHTTPServer audioHTTPServer;
73     private final NetworkAddressService networkAddressService;
74     private final TimeZoneProvider timeZoneProvider;
75
76     // url (scheme+server+port) to use for playing notification sounds
77     private @Nullable String callbackUrl;
78
79     @Activate
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;
86     }
87
88     @Override
89     protected void activate(ComponentContext componentContext) {
90         super.activate(componentContext);
91         Dictionary<String, Object> properties = componentContext.getProperties();
92         callbackUrl = (String) properties.get("callbackUrl");
93     }
94
95     @Override
96     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
97         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
98     }
99
100     @Override
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());
109             } else {
110                 newThingUID = thingUID;
111             }
112             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
113         }
114         throw new IllegalArgumentException(
115                 "The thing type " + thingTypeUID + " is not supported by the Freebox binding.");
116     }
117
118     @Override
119     protected @Nullable ThingHandler createHandler(Thing thing) {
120         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
121
122         if (thingTypeUID.equals(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER)) {
123             FreeboxHandler handler = new FreeboxHandler((Bridge) thing);
124             registerDiscoveryService(handler);
125             return 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);
130             }
131             return handler;
132         }
133
134         return null;
135     }
136
137     @Override
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());
143         }
144     }
145
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<>()));
151     }
152
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();
162             }
163         }
164     }
165
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);
173     }
174
175     private synchronized void unregisterAudioSink(Thing thing) {
176         ServiceRegistration<AudioSink> reg = audioSinkRegistrations.remove(thing.getUID());
177         if (reg != null) {
178             reg.unregister();
179         }
180     }
181
182     private @Nullable String createCallbackUrl() {
183         if (callbackUrl != null) {
184             return callbackUrl;
185         } else {
186             String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
187             if (ipAddress == null) {
188                 logger.warn("No network interface could be found.");
189                 return null;
190             }
191
192             // we do not use SSL as it can cause certificate validation issues.
193             int port = HttpServiceUtil.getHttpServicePort(bundleContext);
194             if (port == -1) {
195                 logger.warn("Cannot find port of the http service.");
196                 return null;
197             }
198
199             return "http://" + ipAddress + ":" + port;
200         }
201     }
202 }