]> git.basschouten.com Git - openhab-addons.git/blob
6693f61fd8e2f1bd06a8a07598390d432c301f6d
[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.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     private final CommandDescriptionProvider commandDescriptionProvider;
59
60     @Activate
61     public DeconzHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
62             final @Reference HttpClientFactory httpClientFactory,
63             final @Reference StateDescriptionProvider stateDescriptionProvider,
64             final @Reference CommandDescriptionProvider commandDescriptionProvider) {
65         this.webSocketFactory = webSocketFactory;
66         this.httpClientFactory = httpClientFactory;
67         this.stateDescriptionProvider = stateDescriptionProvider;
68         this.commandDescriptionProvider = commandDescriptionProvider;
69
70         GsonBuilder gsonBuilder = new GsonBuilder();
71         gsonBuilder.registerTypeAdapter(LightType.class, new LightTypeDeserializer());
72         gsonBuilder.registerTypeAdapter(GroupType.class, new GroupTypeDeserializer());
73         gsonBuilder.registerTypeAdapter(ResourceType.class, new ResourceTypeDeserializer());
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, commandDescriptionProvider);
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         } else if (GroupThingHandler.SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID)) {
97             return new GroupThingHandler(thing, gson, commandDescriptionProvider);
98         }
99
100         return null;
101     }
102 }