]> git.basschouten.com Git - openhab-addons.git/blob
b52d3d0ecc1bce6e09f9ac737845b74830b6645c
[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.asuswrt.internal;
14
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.*;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
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;
41
42 /**
43  * The {@link AsuswrtHandlerFactory} is responsible for creating things and thing handlers.
44  *
45  * @author Christian Wild - Initial contribution
46  */
47 @NonNullByDefault
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;
53
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);
60         }
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);
66         try {
67             httpClient.start();
68         } catch (Exception e) {
69             logger.error(ERR_HTTP_CLIENT_FAILED);
70         }
71     }
72
73     @Deactivate
74     @Override
75     protected void deactivate(ComponentContext componentContext) {
76         super.deactivate(componentContext);
77         try {
78             httpClient.stop();
79         } catch (Exception e) {
80             logger.debug("Unable to stop httpClient");
81         }
82     }
83
84     @Override
85     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
86         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
87     }
88
89     @Override
90     protected @Nullable ThingHandler createHandler(Thing thing) {
91         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
92
93         if (THING_TYPE_ROUTER.equals(thingTypeUID)) {
94             AsuswrtRouter router = new AsuswrtRouter((Bridge) thing, this.httpClient);
95             routerHandlers.add(router);
96             return router;
97         } else if (THING_TYPE_CLIENT.equals(thingTypeUID)) {
98             AsuswrtRouter router = getRouter(thing);
99             if (router != null) {
100                 return new AsuswrtClient(thing, router);
101             }
102         } else if (THING_TYPE_INTERFACE.equals(thingTypeUID)) {
103             AsuswrtRouter router = getRouter(thing);
104             if (router != null) {
105                 return new AsuswrtInterface(thing, router);
106             }
107         }
108         return null;
109     }
110
111     /**
112      * Gets the {@link AsuswrtRouter} handler (Bridge) from a Thing.
113      */
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())) {
119                     return router;
120                 }
121             }
122         }
123         logger.warn(ERR_BRIDGE_NOT_DECLARED);
124         return null;
125     }
126 }