]> git.basschouten.com Git - openhab-addons.git/blob
0238678c349226a55e0fe4e5a8ed77160f054b41
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.tapocontrol.internal;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
17
18 import java.util.HashSet;
19 import java.util.Set;
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.eclipse.jetty.util.ssl.SslContextFactory;
25 import org.openhab.binding.tapocontrol.internal.devices.bridge.TapoBridgeHandler;
26 import org.openhab.binding.tapocontrol.internal.devices.rf.smartcontact.TapoSmartContactHandler;
27 import org.openhab.binding.tapocontrol.internal.devices.rf.wheatersensor.TapoWheaterSensorHandler;
28 import org.openhab.binding.tapocontrol.internal.devices.wifi.TapoUniversalDeviceHandler;
29 import org.openhab.binding.tapocontrol.internal.devices.wifi.bulb.TapoBulbHandler;
30 import org.openhab.binding.tapocontrol.internal.devices.wifi.hub.TapoHubHandler;
31 import org.openhab.binding.tapocontrol.internal.devices.wifi.lightstrip.TapoLightStripHandler;
32 import org.openhab.binding.tapocontrol.internal.devices.wifi.socket.TapoSocketHandler;
33 import org.openhab.binding.tapocontrol.internal.devices.wifi.socket.TapoSocketStripHandler;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
39 import org.openhab.core.thing.binding.ThingHandler;
40 import org.openhab.core.thing.binding.ThingHandlerFactory;
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.Deactivate;
45 import org.osgi.service.component.annotations.Reference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.google.gson.Gson;
50 import com.google.gson.GsonBuilder;
51
52 /**
53  * The {@link TapoControlHandlerFactory} is responsible for handling commands, which are
54  * sent to one of the channels.
55  *
56  * @author Christian Wild - Initial contribution
57  */
58 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tapocontrol")
59 @NonNullByDefault
60 public class TapoControlHandlerFactory extends BaseThingHandlerFactory {
61     public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().excludeFieldsWithoutExposeAnnotation()
62             .create();
63     private final Logger logger = LoggerFactory.getLogger(TapoControlHandlerFactory.class);
64     private final Set<TapoBridgeHandler> accountHandlers = new HashSet<>();
65     private final HttpClient httpClient;
66
67     @Activate
68     public TapoControlHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
69         // create new httpClient
70         httpClient = httpClientFactory.createHttpClient(BINDING_ID, new SslContextFactory.Client());
71         httpClient.setFollowRedirects(false);
72         httpClient.setMaxConnectionsPerDestination(HTTP_MAX_CONNECTIONS);
73         httpClient.setMaxRequestsQueuedPerDestination(HTTP_MAX_QUEUED_REQUESTS);
74         try {
75             httpClient.start();
76         } catch (Exception e) {
77             logger.error("cannot start httpClient");
78         }
79     }
80
81     @Deactivate
82     @Override
83     protected void deactivate(ComponentContext componentContext) {
84         super.deactivate(componentContext);
85         try {
86             httpClient.stop();
87         } catch (Exception e) {
88             logger.debug("unable to stop httpClient");
89         }
90     }
91
92     /**
93      * Provides the supported thing types
94      */
95     @Override
96     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
97         if (thingTypeUID.equals(UNIVERSAL_THING_TYPE)) {
98             return true;
99         }
100         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
101     }
102
103     /**
104      * Create handler of things.
105      */
106     @Override
107     protected @Nullable ThingHandler createHandler(Thing thing) {
108         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
109
110         if (SUPPORTED_BRIDGE_UIDS.contains(thingTypeUID)) {
111             TapoBridgeHandler bridgeHandler = new TapoBridgeHandler((Bridge) thing, httpClient);
112             accountHandlers.add(bridgeHandler);
113             return bridgeHandler;
114         } else if (SUPPORTED_HUB_UIDS.contains(thingTypeUID)) {
115             return new TapoHubHandler(thing);
116         } else if (SUPPORTED_SOCKET_UIDS.contains(thingTypeUID)) {
117             return new TapoSocketHandler(thing);
118         } else if (SUPPORTED_SOCKET_STRIP_UIDS.contains(thingTypeUID)) {
119             return new TapoSocketStripHandler(thing);
120         } else if (SUPPORTED_WHITE_BULB_UIDS.contains(thingTypeUID)) {
121             return new TapoBulbHandler(thing);
122         } else if (SUPPORTED_COLOR_BULB_UIDS.contains(thingTypeUID)) {
123             return new TapoBulbHandler(thing);
124         } else if (SUPPORTED_LIGHT_STRIP_UIDS.contains(thingTypeUID)) {
125             return new TapoLightStripHandler(thing);
126         } else if (SUPPORTED_SMART_CONTACTS.contains(thingTypeUID)) {
127             return new TapoSmartContactHandler(thing);
128         } else if (SUPPORTED_WHEATHER_SENSORS.contains(thingTypeUID)) {
129             return new TapoWheaterSensorHandler(thing);
130         } else if (thingTypeUID.equals(UNIVERSAL_THING_TYPE)) {
131             return new TapoUniversalDeviceHandler(thing);
132         }
133         return null;
134     }
135 }