2 * Copyright (c) 2010-2024 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.freeboxos.internal;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
18 import java.util.Objects;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.freeboxos.internal.api.ApiHandler;
24 import org.openhab.binding.freeboxos.internal.api.rest.FreeboxOsSession;
25 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.Category;
26 import org.openhab.binding.freeboxos.internal.handler.ActivePlayerHandler;
27 import org.openhab.binding.freeboxos.internal.handler.AlarmHandler;
28 import org.openhab.binding.freeboxos.internal.handler.BasicShutterHandler;
29 import org.openhab.binding.freeboxos.internal.handler.CallHandler;
30 import org.openhab.binding.freeboxos.internal.handler.CameraHandler;
31 import org.openhab.binding.freeboxos.internal.handler.DectHandler;
32 import org.openhab.binding.freeboxos.internal.handler.FreeboxOsHandler;
33 import org.openhab.binding.freeboxos.internal.handler.FreeplugHandler;
34 import org.openhab.binding.freeboxos.internal.handler.FxsHandler;
35 import org.openhab.binding.freeboxos.internal.handler.HostHandler;
36 import org.openhab.binding.freeboxos.internal.handler.KeyfobHandler;
37 import org.openhab.binding.freeboxos.internal.handler.PirHandler;
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.Deactivate;
60 import org.osgi.service.component.annotations.Modified;
61 import org.osgi.service.component.annotations.Reference;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * The {@link FreeboxOsHandlerFactory} is responsible for creating things and thing handlers.
68 * @author Gaƫl L'hopital - Initial contribution
71 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.freeboxos")
72 public class FreeboxOsHandlerFactory extends BaseThingHandlerFactory {
73 private static final String TIMEOUT = "timeout";
74 private static final String CALLBACK_URL = "callBackUrl";
76 private final Logger logger = LoggerFactory.getLogger(FreeboxOsHandlerFactory.class);
78 private final NetworkAddressService networkAddressService;
79 private final AudioHTTPServer audioHTTPServer;
80 private final ApiHandler apiHandler;
81 private String callbackURL = "";
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);
90 this.audioHTTPServer = audioHTTPServer;
91 this.networkAddressService = networkAddressService;
92 this.apiHandler = new ApiHandler(httpClientFactory, timeZoneProvider);
94 configChanged(config);
99 public void deactivate(ComponentContext componentContext) {
100 super.deactivate(componentContext);
101 apiHandler.dispose();
105 public void configChanged(Map<String, Object> config) {
106 String timeout = (String) config.getOrDefault(TIMEOUT, "8");
107 apiHandler.setTimeout(TimeUnit.SECONDS.toMillis(Long.parseLong(timeout)));
109 callbackURL = (String) config.getOrDefault(CALLBACK_URL, "");
110 int port = HttpServiceUtil.getHttpServicePort(bundleContext);
111 if (callbackURL.isEmpty() && port != -1) {
112 String openHabIp = Objects.requireNonNull(networkAddressService.getPrimaryIpv4HostAddress());
113 // we do not use SSL as it can cause certificate validation issues.
114 callbackURL = "http://%s:%d".formatted(openHabIp, port);
116 if (callbackURL.isEmpty()) {
117 logger.warn("Unable to build a correct call back URL to stream media contents");
123 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
124 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
128 protected @Nullable ThingHandler createHandler(Thing thing) {
129 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
131 if (BRIDGE_TYPE_API.equals(thingTypeUID)) {
132 return new FreeboxOsHandler((Bridge) thing, new FreeboxOsSession(apiHandler), callbackURL, bundleContext,
134 } else if (THING_TYPE_FREEPLUG.equals(thingTypeUID)) {
135 return new FreeplugHandler(thing);
136 } else if (THING_TYPE_FXS.equals(thingTypeUID)) {
137 return new FxsHandler(thing);
138 } else if (THING_TYPE_DECT.equals(thingTypeUID)) {
139 return new DectHandler(thing);
140 } else if (THING_TYPE_CALL.equals(thingTypeUID)) {
141 return new CallHandler(thing);
142 } else if (THING_TYPE_REVOLUTION.equals(thingTypeUID)) {
143 return new RevolutionHandler(thing);
144 } else if (THING_TYPE_DELTA.equals(thingTypeUID)) {
145 return new ServerHandler(thing);
146 } else if (THING_TYPE_HOST.equals(thingTypeUID)) {
147 return new HostHandler(thing);
148 } else if (THING_TYPE_WIFI_HOST.equals(thingTypeUID)) {
149 return new WifiStationHandler(thing);
150 } else if (THING_TYPE_REPEATER.equals(thingTypeUID)) {
151 return new RepeaterHandler(thing);
152 } else if (THING_TYPE_VM.equals(thingTypeUID)) {
153 return new VmHandler(thing);
154 } else if (THING_TYPE_ACTIVE_PLAYER.equals(thingTypeUID)) {
155 return new ActivePlayerHandler(thing);
156 } else if (THING_TYPE_PLAYER.equals(thingTypeUID)) {
157 return new PlayerHandler(thing);
158 } else if (Category.BASIC_SHUTTER.thingTypeUID.equals(thingTypeUID)) {
159 return new BasicShutterHandler(thing);
160 } else if (Category.SHUTTER.thingTypeUID.equals(thingTypeUID)) {
161 return new ShutterHandler(thing);
162 } else if (Category.ALARM.thingTypeUID.equals(thingTypeUID)) {
163 return new AlarmHandler(thing);
164 } else if (Category.KFB.thingTypeUID.equals(thingTypeUID)) {
165 return new KeyfobHandler(thing);
166 } else if (Category.CAMERA.thingTypeUID.equals(thingTypeUID)) {
167 return new CameraHandler(thing);
168 } else if (Category.PIR.thingTypeUID.equals(thingTypeUID)) {
169 return new PirHandler(thing);