]> git.basschouten.com Git - openhab-addons.git/blob
3b262328d8102c511a2ef3a6ebf52a705cf6d167
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36
37 /**
38  * The {@link TeslaHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Karel Goderis - Initial contribution
42  * @author Nicolai Grødum - Adding token based auth
43  * @author Kai Kreuzer - Introduced account handler
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
46 @NonNullByDefault
47 public class TeslaHandlerFactory extends BaseThingHandlerFactory {
48
49     private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3;
50     private static final int EVENT_STREAM_READ_TIMEOUT = 200;
51
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS,
53             THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY);
54
55     private final ClientBuilder clientBuilder;
56     private final HttpClientFactory httpClientFactory;
57
58     @Activate
59     public TeslaHandlerFactory(@Reference ClientBuilder clientBuilder, @Reference HttpClientFactory httpClientFactory) {
60         this.clientBuilder = clientBuilder //
61                 .connectTimeout(EVENT_STREAM_CONNECT_TIMEOUT, TimeUnit.SECONDS)
62                 .readTimeout(EVENT_STREAM_READ_TIMEOUT, TimeUnit.SECONDS);
63         this.httpClientFactory = httpClientFactory;
64     }
65
66     @Override
67     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69     }
70
71     @Override
72     protected @Nullable ThingHandler createHandler(Thing thing) {
73         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74
75         if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
76             return new TeslaAccountHandler((Bridge) thing, clientBuilder.build(), httpClientFactory);
77         } else {
78             return new TeslaVehicleHandler(thing, clientBuilder);
79         }
80     }
81 }