2 * Copyright (c) 2010-2020 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.tesla.internal;
15 import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 import javax.ws.rs.client.ClientBuilder;
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;
36 * The {@link TeslaHandlerFactory} is responsible for creating things and thing
39 * @author Karel Goderis - Initial contribution
40 * @author Nicolai Grødum - Adding token based auth
41 * @author Kai Kreuzer - Introduced account handler
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
44 public class TeslaHandlerFactory extends BaseThingHandlerFactory {
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";
51 public static final String READ_TIMEOUT = "http.receive.timeout";
52 public static final String CONNECT_TIMEOUT = "http.connection.timeout";
54 private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3000;
55 private static final int EVENT_STREAM_READ_TIMEOUT = 200000;
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());
61 @Reference(cardinality = ReferenceCardinality.OPTIONAL)
62 private ClientBuilder injectedClientBuilder;
64 private ClientBuilder clientBuilder;
67 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
72 protected ThingHandler createHandler(Thing thing) {
73 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75 if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
76 return new TeslaAccountHandler((Bridge) thing, getClientBuilder().build());
78 return new TeslaVehicleHandler(thing, getClientBuilder());
82 private synchronized ClientBuilder getClientBuilder() {
83 if (clientBuilder == null) {
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);
95 throw new IllegalStateException("No JAX RS Client Builder available.");