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