]> git.basschouten.com Git - openhab-addons.git/blob
fa660963b10c891869a4222bb7bfc884bc3e242e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.vesync.internal;
14
15 import static org.openhab.binding.vesync.internal.VeSyncConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.vesync.internal.api.IHttpClientProvider;
23 import org.openhab.binding.vesync.internal.handlers.VeSyncBridgeHandler;
24 import org.openhab.binding.vesync.internal.handlers.VeSyncDeviceAirHumidifierHandler;
25 import org.openhab.binding.vesync.internal.handlers.VeSyncDeviceAirPurifierHandler;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35
36 /**
37  * The {@link org.openhab.binding.vesync.internal.VeSyncHandlerFactory} is responsible for creating
38  * things and thing handlers.
39  *
40  * @author David Goodyear - Initial contribution
41  */
42 @NonNullByDefault
43 @Component(configurationPid = "binding.vesync", service = ThingHandlerFactory.class)
44 public class VeSyncHandlerFactory extends BaseThingHandlerFactory implements IHttpClientProvider {
45
46     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE,
47             THING_TYPE_AIR_PURIFIER, THING_TYPE_AIR_HUMIDIFIER);
48
49     private @Nullable HttpClient httpClientRef = null;
50
51     @Override
52     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
54     }
55
56     @Override
57     protected @Nullable ThingHandler createHandler(Thing thing) {
58         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
59
60         if (VeSyncDeviceAirPurifierHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
61             return new VeSyncDeviceAirPurifierHandler(thing);
62         } else if (VeSyncDeviceAirHumidifierHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
63             return new VeSyncDeviceAirHumidifierHandler(thing);
64         } else if (THING_TYPE_BRIDGE.equals(thingTypeUID)) {
65             return new VeSyncBridgeHandler((Bridge) thing, this);
66         }
67
68         return null;
69     }
70
71     @Reference
72     protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
73         httpClientRef = httpClientFactory.getCommonHttpClient();
74     }
75
76     @Override
77     public @Nullable HttpClient getHttpClient() {
78         return httpClientRef;
79     }
80 }