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