]> git.basschouten.com Git - openhab-addons.git/blob
ed57eaefec97bf1a93f5a857140cbad0a3b6ae76
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.tesla.internal;
14
15 import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
16
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 import javax.ws.rs.client.ClientBuilder;
22
23 import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
24 import org.openhab.binding.tesla.internal.handler.TeslaVehicleHandler;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33 import org.osgi.service.component.annotations.ReferenceCardinality;
34
35 /**
36  * The {@link TeslaHandlerFactory} is responsible for creating things and thing
37  * handlers.
38  *
39  * @author Karel Goderis - Initial contribution
40  * @author Nicolai Grødum - Adding token based auth
41  * @author Kai Kreuzer - Introduced account handler
42  */
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
44 public class TeslaHandlerFactory extends BaseThingHandlerFactory {
45
46     // TODO: Those constants are Jersey specific - once we move away from Jersey,
47     // this can be removed and the client builder creation simplified.
48     public static final String READ_TIMEOUT_JERSEY = "jersey.config.client.readTimeout";
49     public static final String CONNECT_TIMEOUT_JERSEY = "jersey.config.client.connectTimeout";
50
51     public static final String READ_TIMEOUT = "http.receive.timeout";
52     public static final String CONNECT_TIMEOUT = "http.connection.timeout";
53
54     private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3000;
55     private static final int EVENT_STREAM_READ_TIMEOUT = 200000;
56
57     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
58             .of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS, THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY)
59             .collect(Collectors.toSet());
60
61     @Reference(cardinality = ReferenceCardinality.OPTIONAL)
62     private ClientBuilder injectedClientBuilder;
63
64     private ClientBuilder clientBuilder;
65
66     @Override
67     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69     }
70
71     @Override
72     protected ThingHandler createHandler(Thing thing) {
73         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74
75         if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
76             return new TeslaAccountHandler((Bridge) thing, getClientBuilder().build());
77         } else {
78             return new TeslaVehicleHandler(thing, getClientBuilder());
79         }
80     }
81
82     private synchronized ClientBuilder getClientBuilder() {
83         if (clientBuilder == null) {
84             try {
85                 clientBuilder = ClientBuilder.newBuilder();
86                 clientBuilder.property(CONNECT_TIMEOUT_JERSEY, EVENT_STREAM_CONNECT_TIMEOUT);
87                 clientBuilder.property(READ_TIMEOUT_JERSEY, EVENT_STREAM_READ_TIMEOUT);
88             } catch (Exception e) {
89                 // we seem to have no Jersey, so let's hope for an injected builder by CXF
90                 if (this.injectedClientBuilder != null) {
91                     clientBuilder = injectedClientBuilder;
92                     clientBuilder.property(CONNECT_TIMEOUT, EVENT_STREAM_CONNECT_TIMEOUT);
93                     clientBuilder.property(READ_TIMEOUT, EVENT_STREAM_READ_TIMEOUT);
94                 } else {
95                     throw new IllegalStateException("No JAX RS Client Builder available.");
96                 }
97             }
98         }
99         return clientBuilder;
100     }
101 }