]> git.basschouten.com Git - openhab-addons.git/blob
f942a10f0d69827fc6cd2b46c0046280be8a54e5
[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.nanoleaf.internal;
14
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler;
26 import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
27 import org.openhab.core.io.net.http.HttpClientFactory;
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 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link NanoleafHandlerFactory} is responsible for creating the controller (bridge)
42  * and panel (thing) handlers.
43  *
44  * @author Martin Raepple - Initial contribution
45  * @author Kai Kreuzer - made discovery a handler service
46  */
47 @NonNullByDefault
48 @Component(configurationPid = "binding.nanoleaf", service = ThingHandlerFactory.class)
49 public class NanoleafHandlerFactory extends BaseThingHandlerFactory {
50
51     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
52             .unmodifiableSet(Stream.of(THING_TYPE_LIGHT_PANEL, THING_TYPE_CONTROLLER).collect(Collectors.toSet()));
53
54     private final Logger logger = LoggerFactory.getLogger(NanoleafHandlerFactory.class);
55     private final HttpClient httpClient;
56
57     @Activate
58     public NanoleafHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
59         this.httpClient = httpClientFactory.getCommonHttpClient();
60     }
61
62     @Override
63     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65     }
66
67     @Override
68     protected @Nullable ThingHandler createHandler(Thing thing) {
69         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70
71         if (THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
72             NanoleafControllerHandler handler = new NanoleafControllerHandler((Bridge) thing, httpClient);
73             logger.debug("Nanoleaf controller handler created.");
74             return handler;
75         } else if (THING_TYPE_LIGHT_PANEL.equals(thingTypeUID)) {
76             NanoleafPanelHandler handler = new NanoleafPanelHandler(thing, httpClient);
77             logger.debug("Nanoleaf panel handler created.");
78             return handler;
79         }
80         return null;
81     }
82 }