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