]> git.basschouten.com Git - openhab-addons.git/blob
9b961d14b98741c6079fd50c64fc952704e64a67
[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.orbitbhyve.internal;
14
15 import static org.openhab.binding.orbitbhyve.internal.OrbitBhyveBindingConstants.THING_TYPE_BRIDGE;
16 import static org.openhab.binding.orbitbhyve.internal.OrbitBhyveBindingConstants.THING_TYPE_SPRINKLER;
17
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.eclipse.jetty.websocket.client.WebSocketClient;
24 import org.openhab.binding.orbitbhyve.internal.handler.OrbitBhyveBridgeHandler;
25 import org.openhab.binding.orbitbhyve.internal.handler.OrbitBhyveSprinklerHandler;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.openhab.core.io.net.http.WebSocketFactory;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37
38 /**
39  * The {@link OrbitBhyveHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Ondrej Pecta - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.orbitbhyve", service = ThingHandlerFactory.class)
46 public class OrbitBhyveHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE, THING_TYPE_SPRINKLER);
49
50     /**
51      * the shared http client
52      */
53     private HttpClient httpClient;
54
55     /**
56      * the shared web socket client
57      */
58     private WebSocketClient webSocketClient;
59
60     @Activate
61     public OrbitBhyveHandlerFactory(@Reference HttpClientFactory httpClientFactory,
62             @Reference WebSocketFactory webSocketFactory) {
63         this.httpClient = httpClientFactory.getCommonHttpClient();
64         this.webSocketClient = webSocketFactory.getCommonWebSocketClient();
65     }
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     protected @Nullable ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
76         if (THING_TYPE_BRIDGE.equals(thingTypeUID)) {
77             return new OrbitBhyveBridgeHandler((Bridge) thing, httpClient, webSocketClient);
78         }
79         if (THING_TYPE_SPRINKLER.equals(thingTypeUID)) {
80             return new OrbitBhyveSprinklerHandler(thing);
81         }
82         return null;
83     }
84 }