]> git.basschouten.com Git - openhab-addons.git/blob
f835aa2957276dba3644ed9ccd6d511276472419
[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.freeboxos.internal;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.freeboxos.internal.api.ApiHandler;
25 import org.openhab.binding.freeboxos.internal.api.rest.FreeboxOsSession;
26 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.Category;
27 import org.openhab.binding.freeboxos.internal.handler.ActivePlayerHandler;
28 import org.openhab.binding.freeboxos.internal.handler.AlarmHandler;
29 import org.openhab.binding.freeboxos.internal.handler.BasicShutterHandler;
30 import org.openhab.binding.freeboxos.internal.handler.CallHandler;
31 import org.openhab.binding.freeboxos.internal.handler.CameraHandler;
32 import org.openhab.binding.freeboxos.internal.handler.DectHandler;
33 import org.openhab.binding.freeboxos.internal.handler.FreeboxOsHandler;
34 import org.openhab.binding.freeboxos.internal.handler.FreeplugHandler;
35 import org.openhab.binding.freeboxos.internal.handler.FxsHandler;
36 import org.openhab.binding.freeboxos.internal.handler.HostHandler;
37 import org.openhab.binding.freeboxos.internal.handler.KeyfobHandler;
38 import org.openhab.binding.freeboxos.internal.handler.PlayerHandler;
39 import org.openhab.binding.freeboxos.internal.handler.RepeaterHandler;
40 import org.openhab.binding.freeboxos.internal.handler.RevolutionHandler;
41 import org.openhab.binding.freeboxos.internal.handler.ServerHandler;
42 import org.openhab.binding.freeboxos.internal.handler.ShutterHandler;
43 import org.openhab.binding.freeboxos.internal.handler.VmHandler;
44 import org.openhab.binding.freeboxos.internal.handler.WifiStationHandler;
45 import org.openhab.core.audio.AudioHTTPServer;
46 import org.openhab.core.i18n.TimeZoneProvider;
47 import org.openhab.core.io.net.http.HttpClientFactory;
48 import org.openhab.core.net.HttpServiceUtil;
49 import org.openhab.core.net.NetworkAddressService;
50 import org.openhab.core.thing.Bridge;
51 import org.openhab.core.thing.Thing;
52 import org.openhab.core.thing.ThingTypeUID;
53 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
54 import org.openhab.core.thing.binding.ThingHandler;
55 import org.openhab.core.thing.binding.ThingHandlerFactory;
56 import org.osgi.service.component.ComponentContext;
57 import org.osgi.service.component.annotations.Activate;
58 import org.osgi.service.component.annotations.Component;
59 import org.osgi.service.component.annotations.Modified;
60 import org.osgi.service.component.annotations.Reference;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  * The {@link FreeboxOsHandlerFactory} is responsible for creating things and thing handlers.
66  *
67  * @author GaĆ«l L'hopital - Initial contribution
68  */
69 @NonNullByDefault
70 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.freeboxos")
71 public class FreeboxOsHandlerFactory extends BaseThingHandlerFactory {
72     private static final String TIMEOUT = "timeout";
73     private static final String CALLBACK_URL = "callBackUrl";
74
75     private final Logger logger = LoggerFactory.getLogger(FreeboxOsHandlerFactory.class);
76
77     private final NetworkAddressService networkAddressService;
78     private final AudioHTTPServer audioHTTPServer;
79     private final HttpClient httpClient;
80     private final ApiHandler apiHandler;
81     private String callbackURL = "";
82
83     @Activate
84     public FreeboxOsHandlerFactory(final @Reference AudioHTTPServer audioHTTPServer,
85             final @Reference NetworkAddressService networkAddressService,
86             final @Reference HttpClientFactory httpClientFactory, final @Reference TimeZoneProvider timeZoneProvider,
87             ComponentContext componentContext, Map<String, Object> config) {
88         super.activate(componentContext);
89
90         this.audioHTTPServer = audioHTTPServer;
91         this.httpClient = httpClientFactory.getCommonHttpClient();
92         this.networkAddressService = networkAddressService;
93         this.apiHandler = new ApiHandler(httpClient, timeZoneProvider);
94
95         configChanged(config);
96     }
97
98     @Modified
99     public void configChanged(Map<String, Object> config) {
100         String timeout = (String) config.getOrDefault(TIMEOUT, "8");
101         apiHandler.setTimeout(TimeUnit.SECONDS.toMillis(Long.parseLong(timeout)));
102
103         callbackURL = (String) config.getOrDefault(CALLBACK_URL, "");
104         int port = HttpServiceUtil.getHttpServicePort(bundleContext);
105         if (callbackURL.isEmpty() && port != -1) {
106             String openHabIp = Objects.requireNonNull(networkAddressService.getPrimaryIpv4HostAddress());
107             // we do not use SSL as it can cause certificate validation issues.
108             callbackURL = "http://%s:%d".formatted(openHabIp, port);
109         }
110         if (callbackURL.isEmpty()) {
111             logger.warn("Unable to build a correct call back URL to stream media contents");
112             return;
113         }
114     }
115
116     @Override
117     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
118         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
119     }
120
121     @Override
122     protected @Nullable ThingHandler createHandler(Thing thing) {
123         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
124
125         if (BRIDGE_TYPE_API.equals(thingTypeUID)) {
126             return new FreeboxOsHandler((Bridge) thing, new FreeboxOsSession(apiHandler), callbackURL, bundleContext,
127                     audioHTTPServer);
128         } else if (THING_TYPE_FREEPLUG.equals(thingTypeUID)) {
129             return new FreeplugHandler(thing);
130         } else if (THING_TYPE_FXS.equals(thingTypeUID)) {
131             return new FxsHandler(thing);
132         } else if (THING_TYPE_DECT.equals(thingTypeUID)) {
133             return new DectHandler(thing);
134         } else if (THING_TYPE_CALL.equals(thingTypeUID)) {
135             return new CallHandler(thing);
136         } else if (THING_TYPE_REVOLUTION.equals(thingTypeUID)) {
137             return new RevolutionHandler(thing);
138         } else if (THING_TYPE_DELTA.equals(thingTypeUID)) {
139             return new ServerHandler(thing);
140         } else if (THING_TYPE_HOST.equals(thingTypeUID)) {
141             return new HostHandler(thing);
142         } else if (THING_TYPE_WIFI_HOST.equals(thingTypeUID)) {
143             return new WifiStationHandler(thing);
144         } else if (THING_TYPE_REPEATER.equals(thingTypeUID)) {
145             return new RepeaterHandler(thing);
146         } else if (THING_TYPE_VM.equals(thingTypeUID)) {
147             return new VmHandler(thing);
148         } else if (THING_TYPE_ACTIVE_PLAYER.equals(thingTypeUID)) {
149             return new ActivePlayerHandler(thing);
150         } else if (THING_TYPE_PLAYER.equals(thingTypeUID)) {
151             return new PlayerHandler(thing);
152         } else if (Category.BASIC_SHUTTER.getThingTypeUID().equals(thingTypeUID)) {
153             return new BasicShutterHandler(thing);
154         } else if (Category.SHUTTER.getThingTypeUID().equals(thingTypeUID)) {
155             return new ShutterHandler(thing);
156         } else if (Category.ALARM.getThingTypeUID().equals(thingTypeUID)) {
157             return new AlarmHandler(thing);
158         } else if (Category.KFB.getThingTypeUID().equals(thingTypeUID)) {
159             return new KeyfobHandler(thing);
160         } else if (Category.CAMERA.getThingTypeUID().equals(thingTypeUID)) {
161             return new CameraHandler(thing);
162         }
163
164         return null;
165     }
166 }