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.asuswrt.internal;
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.*;
16 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingSettings.*;
17 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtErrorConstants.*;
19 import java.util.HashSet;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.eclipse.jetty.util.ssl.SslContextFactory;
26 import org.openhab.binding.asuswrt.internal.things.AsuswrtClient;
27 import org.openhab.binding.asuswrt.internal.things.AsuswrtInterface;
28 import org.openhab.binding.asuswrt.internal.things.AsuswrtRouter;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
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.Component;
38 import org.osgi.service.component.annotations.Deactivate;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link AsuswrtHandlerFactory} is responsible for creating things and thing handlers.
45 * @author Christian Wild - Initial contribution
48 @Component(configurationPid = "binding.asuswrt", service = ThingHandlerFactory.class)
49 public class AsuswrtHandlerFactory extends BaseThingHandlerFactory {
50 private final Logger logger = LoggerFactory.getLogger(AsuswrtHandlerFactory.class);
51 private final Set<AsuswrtRouter> routerHandlers = new HashSet<>();
52 private final HttpClient httpClient;
54 public AsuswrtHandlerFactory() {
55 // Set SslContextfactory
56 SslContextFactory sslContextFactory = new SslContextFactory.Client();
57 if (HTTP_SSL_TRUST_ALL) {
58 sslContextFactory.setTrustAll(true);
59 sslContextFactory.setEndpointIdentificationAlgorithm(null);
61 // Create new httpClient
62 httpClient = new HttpClient(sslContextFactory);
63 httpClient.setFollowRedirects(false);
64 httpClient.setMaxConnectionsPerDestination(HTTP_MAX_CONNECTIONS);
65 httpClient.setMaxRequestsQueuedPerDestination(HTTP_MAX_QUEUED_REQUESTS);
68 } catch (Exception e) {
69 logger.error(ERR_HTTP_CLIENT_FAILED);
75 protected void deactivate(ComponentContext componentContext) {
76 super.deactivate(componentContext);
79 } catch (Exception e) {
80 logger.debug("Unable to stop httpClient");
85 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
86 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
90 protected @Nullable ThingHandler createHandler(Thing thing) {
91 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
93 if (THING_TYPE_ROUTER.equals(thingTypeUID)) {
94 AsuswrtRouter router = new AsuswrtRouter((Bridge) thing, this.httpClient);
95 routerHandlers.add(router);
97 } else if (THING_TYPE_CLIENT.equals(thingTypeUID)) {
98 AsuswrtRouter router = getRouter(thing);
100 return new AsuswrtClient(thing, router);
102 } else if (THING_TYPE_INTERFACE.equals(thingTypeUID)) {
103 AsuswrtRouter router = getRouter(thing);
104 if (router != null) {
105 return new AsuswrtInterface(thing, router);
112 * Gets the {@link AsuswrtRouter} handler (Bridge) from a Thing.
114 protected @Nullable AsuswrtRouter getRouter(Thing thing) {
115 ThingUID bridgeUID = thing.getBridgeUID();
116 if (bridgeUID != null) {
117 for (AsuswrtRouter router : routerHandlers) {
118 if (bridgeUID.equals(router.getUID())) {
123 logger.warn(ERR_BRIDGE_NOT_DECLARED);