]> git.basschouten.com Git - openhab-addons.git/blob
d89fea388cd259ef958018c4d5d607682faf6c6e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.concurrent.TimeUnit;
19
20 import javax.ws.rs.client.ClientBuilder;
21
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;
37
38 /**
39  * The {@link TeslaHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Karel Goderis - Initial contribution
43  * @author Nicolai Grødum - Adding token based auth
44  * @author Kai Kreuzer - Introduced account handler
45  */
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
47 @NonNullByDefault
48 public class TeslaHandlerFactory extends BaseThingHandlerFactory {
49
50     private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3;
51     private static final int EVENT_STREAM_READ_TIMEOUT = 200;
52
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);
55
56     private final ClientBuilder clientBuilder;
57     private final HttpClientFactory httpClientFactory;
58     private final WebSocketFactory webSocketFactory;
59
60     @Activate
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;
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     protected @Nullable ThingHandler createHandler(Thing thing) {
77         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
79         if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
80             return new TeslaAccountHandler((Bridge) thing, clientBuilder.build(), httpClientFactory);
81         } else {
82             return new TeslaVehicleHandler(thing, webSocketFactory);
83         }
84     }
85 }