]> git.basschouten.com Git - openhab-addons.git/blob
77b2d2da0d3ae4e2a1b952b5951dec10f1228d9a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Hashtable;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
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;
47
48 /**
49  * The {@link FreeboxHandlerFactory} is responsible for creating things and thing
50  * handlers.
51  *
52  * @author GaĆ«l L'hopital - Initial contribution
53  * @author Laurent Garnier - several thing types and handlers + discovery service
54  */
55 @NonNullByDefault
56 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.freebox")
57 public class FreeboxHandlerFactory extends BaseThingHandlerFactory {
58
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());
63
64     private final Logger logger = LoggerFactory.getLogger(FreeboxHandlerFactory.class);
65
66     private final Map<ThingUID, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
67
68     private final AudioHTTPServer audioHTTPServer;
69     private final NetworkAddressService networkAddressService;
70     private final TimeZoneProvider timeZoneProvider;
71
72     // url (scheme+server+port) to use for playing notification sounds
73     private @Nullable String callbackUrl;
74
75     @Activate
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;
82     }
83
84     @Override
85     protected void activate(ComponentContext componentContext) {
86         super.activate(componentContext);
87         Dictionary<String, Object> properties = componentContext.getProperties();
88         callbackUrl = (String) properties.get("callbackUrl");
89     }
90
91     @Override
92     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
93         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
94     }
95
96     @Override
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());
105             } else {
106                 newThingUID = thingUID;
107             }
108             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
109         }
110         throw new IllegalArgumentException(
111                 "The thing type " + thingTypeUID + " is not supported by the Freebox binding.");
112     }
113
114     @Override
115     protected @Nullable ThingHandler createHandler(Thing thing) {
116         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
117
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);
124             }
125             return handler;
126         }
127
128         return null;
129     }
130
131     @Override
132     protected void removeHandler(ThingHandler thingHandler) {
133         if (thingHandler instanceof FreeboxThingHandler) {
134             unregisterAudioSink(thingHandler.getThing());
135         }
136     }
137
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);
145     }
146
147     private synchronized void unregisterAudioSink(Thing thing) {
148         ServiceRegistration<AudioSink> reg = audioSinkRegistrations.remove(thing.getUID());
149         if (reg != null) {
150             reg.unregister();
151         }
152     }
153
154     private @Nullable String createCallbackUrl() {
155         if (callbackUrl != null) {
156             return callbackUrl;
157         } else {
158             String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
159             if (ipAddress == null) {
160                 logger.warn("No network interface could be found.");
161                 return null;
162             }
163
164             // we do not use SSL as it can cause certificate validation issues.
165             int port = HttpServiceUtil.getHttpServicePort(bundleContext);
166             if (port == -1) {
167                 logger.warn("Cannot find port of the http service.");
168                 return null;
169             }
170
171             return "http://" + ipAddress + ":" + port;
172         }
173     }
174 }