]> git.basschouten.com Git - openhab-addons.git/blob
5d0b2086e422289095dc372469de8e53d60ca1c1
[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.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.device.TapoBridgeHandler;
26 import org.openhab.binding.tapocontrol.internal.device.TapoLightStrip;
27 import org.openhab.binding.tapocontrol.internal.device.TapoSmartBulb;
28 import org.openhab.binding.tapocontrol.internal.device.TapoSmartPlug;
29 import org.openhab.binding.tapocontrol.internal.device.TapoUniversalDevice;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.service.component.ComponentContext;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Deactivate;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link TapoControlHandler} is responsible for handling commands, which are
47  * sent to one of the channels.
48  *
49  * @author Christian Wild - Initial contribution
50  */
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tapocontrol")
52 @NonNullByDefault
53 public class TapoControlHandlerFactory extends BaseThingHandlerFactory {
54     private final Logger logger = LoggerFactory.getLogger(TapoControlHandlerFactory.class);
55     private final Set<TapoBridgeHandler> accountHandlers = new HashSet<>();
56     private final HttpClient httpClient;
57
58     @Activate
59     public TapoControlHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
60         // create new httpClient
61         httpClient = httpClientFactory.createHttpClient(BINDING_ID, new SslContextFactory.Client());
62         httpClient.setFollowRedirects(false);
63         httpClient.setMaxConnectionsPerDestination(HTTP_MAX_CONNECTIONS);
64         httpClient.setMaxRequestsQueuedPerDestination(HTTP_MAX_QUEUED_REQUESTS);
65         try {
66             httpClient.start();
67         } catch (Exception e) {
68             logger.error("cannot start httpClient");
69         }
70     }
71
72     @Deactivate
73     @Override
74     protected void deactivate(ComponentContext componentContext) {
75         super.deactivate(componentContext);
76         try {
77             httpClient.stop();
78         } catch (Exception e) {
79             logger.debug("unable to stop httpClient");
80         }
81     }
82
83     /**
84      * Provides the supported thing types
85      */
86     @Override
87     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
88         if (thingTypeUID.equals(UNIVERSAL_THING_TYPE)) {
89             return true;
90         }
91         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
92     }
93
94     /**
95      * Create handler of things.
96      */
97     @Override
98     protected @Nullable ThingHandler createHandler(Thing thing) {
99         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
100
101         if (SUPPORTED_BRIDGE_UIDS.contains(thingTypeUID)) {
102             TapoBridgeHandler bridgeHandler = new TapoBridgeHandler((Bridge) thing, httpClient);
103             accountHandlers.add(bridgeHandler);
104             return bridgeHandler;
105         } else if (SUPPORTED_SMART_PLUG_UIDS.contains(thingTypeUID)) {
106             return new TapoSmartPlug(thing);
107         } else if (SUPPORTED_WHITE_BULB_UIDS.contains(thingTypeUID)) {
108             return new TapoSmartBulb(thing);
109         } else if (SUPPORTED_COLOR_BULB_UIDS.contains(thingTypeUID)) {
110             return new TapoSmartBulb(thing);
111         } else if (SUPPORTED_LIGHT_STRIP_UIDS.contains(thingTypeUID)) {
112             return new TapoLightStrip(thing);
113         } else if (thingTypeUID.equals(UNIVERSAL_THING_TYPE)) {
114             return new TapoUniversalDevice(thing);
115         }
116         return null;
117     }
118 }