]> git.basschouten.com Git - openhab-addons.git/blob
fac074eb92ac9c2ef4df7b69245c50a779c997e8
[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.elroconnects.internal;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.eclipse.jetty.client.WWWAuthenticationProtocolHandler;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsAccountHandler;
22 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
23 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsCOAlarmHandler;
24 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
25 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsEntrySensorHandler;
26 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsHeatAlarmHandler;
27 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsMotionSensorHandler;
28 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsPowerSocketHandler;
29 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsSmokeAlarmHandler;
30 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsWaterAlarmHandler;
31 import org.openhab.core.io.net.http.HttpClientFactory;
32 import org.openhab.core.io.net.http.HttpClientInitializationException;
33 import org.openhab.core.net.NetworkAddressService;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Deactivate;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * The {@link ElroConnectsHandlerFactory} is responsible for creating things and thing
49  * handlers.
50  *
51  * @author Mark Herwege - Initial contribution
52  */
53 @NonNullByDefault
54 @Component(configurationPid = "binding.elroconnects", service = ThingHandlerFactory.class)
55 public class ElroConnectsHandlerFactory extends BaseThingHandlerFactory {
56     private final Logger logger = LoggerFactory.getLogger(ElroConnectsHandlerFactory.class);
57
58     private final HttpClientFactory httpClientFactory;
59     private final NetworkAddressService networkAddressService;
60     private final ElroConnectsDynamicStateDescriptionProvider dynamicStateDescriptionProvider;
61
62     private @Nullable HttpClient httpClient;
63
64     @Activate
65     public ElroConnectsHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
66             final @Reference NetworkAddressService networkAddressService,
67             final @Reference ElroConnectsDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
68         this.httpClientFactory = httpClientFactory;
69         this.networkAddressService = networkAddressService;
70         this.dynamicStateDescriptionProvider = dynamicStateDescriptionProvider;
71     }
72
73     @Deactivate
74     public void deactivate() {
75         HttpClient client = httpClient;
76
77         if (client != null) {
78             try {
79                 client.stop();
80             } catch (Exception e) {
81                 // catching exception is necessary due to the signature of HttpClient.stop()
82                 logger.debug("Failed to stop http client: {}", e.getMessage());
83             }
84             httpClient = null;
85         }
86     }
87
88     @Override
89     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
90         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
91     }
92
93     @Override
94     protected @Nullable ThingHandler createHandler(Thing thing) {
95         switch (thing.getThingTypeUID().getId()) {
96             case TYPE_ACCOUNT:
97                 return createElroConnectsAccountHandler(thing);
98             case TYPE_CONNECTOR:
99                 return new ElroConnectsBridgeHandler((Bridge) thing, networkAddressService,
100                         dynamicStateDescriptionProvider);
101             case TYPE_SMOKEALARM:
102                 return new ElroConnectsSmokeAlarmHandler(thing);
103             case TYPE_WATERALARM:
104                 return new ElroConnectsWaterAlarmHandler(thing);
105             case TYPE_COALARM:
106                 return new ElroConnectsCOAlarmHandler(thing);
107             case TYPE_HEATALARM:
108                 return new ElroConnectsHeatAlarmHandler(thing);
109             case TYPE_ENTRYSENSOR:
110                 return new ElroConnectsEntrySensorHandler(thing);
111             case TYPE_MOTIONSENSOR:
112                 return new ElroConnectsMotionSensorHandler(thing);
113             case TYPE_POWERSOCKET:
114                 return new ElroConnectsPowerSocketHandler(thing);
115             case TYPE_THSENSOR:
116                 return new ElroConnectsDeviceHandler(thing);
117             default:
118                 return null;
119         }
120     }
121
122     private ThingHandler createElroConnectsAccountHandler(Thing thing) {
123         // Create and start the httpClient for the first ElroConnectsAccountHandler that gets created. We cannot use the
124         // common http client because we need to disable the authentication protocol handler.
125         HttpClient client = httpClient;
126         if (client == null) {
127             client = httpClientFactory.createHttpClient(BINDING_ID);
128             httpClient = client;
129
130             try {
131                 client.start();
132
133                 // The getAccessToken call in the ElroConnectsAccountHandler returns an invalid 401 response on
134                 // authentication error, missing the www-authentication header. This header should be there according to
135                 // RFC7235. This workaround removes the protocol handler and the check.
136                 client.getProtocolHandlers().remove(WWWAuthenticationProtocolHandler.NAME);
137             } catch (Exception e) {
138                 // catching exception is necessary due to the signature of HttpClient.start()
139                 logger.debug("Failed to start http client: {}", e.getMessage());
140                 throw new HttpClientInitializationException("Could not initialize HttpClient", e);
141             }
142         }
143         return new ElroConnectsAccountHandler((Bridge) thing, client);
144     }
145 }