2 * Copyright (c) 2010-2022 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.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;
44 * The {@link TapoControlHandler} is responsible for handling commands, which are
45 * sent to one of the channels.
47 * @author Christian Wild - Initial contribution
49 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tapocontrol")
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;
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);
65 } catch (Exception e) {
66 logger.error("cannot start httpClient");
72 protected void deactivate(ComponentContext componentContext) {
73 super.deactivate(componentContext);
76 } catch (Exception e) {
77 logger.debug("unable to stop httpClient");
82 * Provides the supported thing types
85 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
86 if (thingTypeUID.equals(UNIVERSAL_THING_TYPE)) {
89 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
93 * Create handler of things.
96 protected @Nullable ThingHandler createHandler(Thing thing) {
97 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);