2 * Copyright (c) 2010-2022 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.concurrent.TimeUnit;
20 import javax.ws.rs.client.ClientBuilder;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
25 import org.openhab.binding.tesla.internal.handler.TeslaVehicleHandler;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.openhab.core.io.net.http.WebSocketFactory;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link TeslaHandlerFactory} is responsible for creating things and thing
42 * @author Karel Goderis - Initial contribution
43 * @author Nicolai Grødum - Adding token based auth
44 * @author Kai Kreuzer - Introduced account handler
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
48 public class TeslaHandlerFactory extends BaseThingHandlerFactory {
50 private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3;
51 private static final int EVENT_STREAM_READ_TIMEOUT = 200;
53 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS,
54 THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY);
56 private final ClientBuilder clientBuilder;
57 private final HttpClientFactory httpClientFactory;
58 private final WebSocketFactory webSocketFactory;
61 public TeslaHandlerFactory(@Reference ClientBuilder clientBuilder, @Reference HttpClientFactory httpClientFactory,
62 final @Reference WebSocketFactory webSocketFactory) {
63 this.clientBuilder = clientBuilder //
64 .connectTimeout(EVENT_STREAM_CONNECT_TIMEOUT, TimeUnit.SECONDS)
65 .readTimeout(EVENT_STREAM_READ_TIMEOUT, TimeUnit.SECONDS);
66 this.httpClientFactory = httpClientFactory;
67 this.webSocketFactory = webSocketFactory;
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
80 return new TeslaAccountHandler((Bridge) thing, clientBuilder.build(), httpClientFactory);
82 return new TeslaVehicleHandler(thing, webSocketFactory);