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