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