]> git.basschouten.com Git - openhab-addons.git/blob
5cda9603a5e7c2fd2ebe59049d62f14a61beab0b
[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.deconz.internal;
14
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.deconz.internal.handler.DeconzBridgeHandler;
22 import org.openhab.binding.deconz.internal.handler.LightThingHandler;
23 import org.openhab.binding.deconz.internal.handler.SensorThermostatThingHandler;
24 import org.openhab.binding.deconz.internal.handler.SensorThingHandler;
25 import org.openhab.binding.deconz.internal.netutils.AsyncHttpClient;
26 import org.openhab.binding.deconz.internal.types.LightType;
27 import org.openhab.binding.deconz.internal.types.LightTypeDeserializer;
28 import org.openhab.binding.deconz.internal.types.ThermostatMode;
29 import org.openhab.binding.deconz.internal.types.ThermostatModeGsonTypeAdapter;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.openhab.core.io.net.http.WebSocketFactory;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41
42 import com.google.gson.Gson;
43 import com.google.gson.GsonBuilder;
44
45 /**
46  * The {@link DeconzHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author David Graeff - Initial contribution
50  */
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.deconz")
52 @NonNullByDefault
53 public class DeconzHandlerFactory extends BaseThingHandlerFactory {
54     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
55             .of(DeconzBridgeHandler.SUPPORTED_THING_TYPES, LightThingHandler.SUPPORTED_THING_TYPE_UIDS,
56                     SensorThingHandler.SUPPORTED_THING_TYPES, SensorThermostatThingHandler.SUPPORTED_THING_TYPES)
57             .flatMap(Set::stream).collect(Collectors.toSet());
58
59     private final Gson gson;
60     private final WebSocketFactory webSocketFactory;
61     private final HttpClientFactory httpClientFactory;
62     private final StateDescriptionProvider stateDescriptionProvider;
63
64     @Activate
65     public DeconzHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
66             final @Reference HttpClientFactory httpClientFactory,
67             final @Reference StateDescriptionProvider stateDescriptionProvider) {
68         this.webSocketFactory = webSocketFactory;
69         this.httpClientFactory = httpClientFactory;
70         this.stateDescriptionProvider = stateDescriptionProvider;
71
72         GsonBuilder gsonBuilder = new GsonBuilder();
73         gsonBuilder.registerTypeAdapter(LightType.class, new LightTypeDeserializer());
74         gsonBuilder.registerTypeAdapter(ThermostatMode.class, new ThermostatModeGsonTypeAdapter());
75         gson = gsonBuilder.create();
76     }
77
78     @Override
79     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
80         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
81     }
82
83     @Override
84     protected @Nullable ThingHandler createHandler(Thing thing) {
85         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86
87         if (DeconzBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
88             return new DeconzBridgeHandler((Bridge) thing, webSocketFactory,
89                     new AsyncHttpClient(httpClientFactory.getCommonHttpClient()), gson);
90         } else if (LightThingHandler.SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID)) {
91             return new LightThingHandler(thing, gson, stateDescriptionProvider);
92         } else if (SensorThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
93             return new SensorThingHandler(thing, gson);
94         } else if (SensorThermostatThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
95             return new SensorThermostatThingHandler(thing, gson);
96         }
97
98         return null;
99     }
100 }