2 * Copyright (c) 2010-2023 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.tapocontrol.internal;
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
18 import java.util.HashSet;
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;
46 * The {@link ThingHandler} is responsible for handling commands, which are
47 * sent to one of the channels.
49 * @author Christian Wild - Initial contribution
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tapocontrol")
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;
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);
67 } catch (Exception e) {
68 logger.error("cannot start httpClient");
74 protected void deactivate(ComponentContext componentContext) {
75 super.deactivate(componentContext);
78 } catch (Exception e) {
79 logger.debug("unable to stop httpClient");
84 * Provides the supported thing types
87 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
88 if (thingTypeUID.equals(UNIVERSAL_THING_TYPE)) {
91 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
95 * Create handler of things.
98 protected @Nullable ThingHandler createHandler(Thing thing) {
99 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);