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