2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal;
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.*;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.openhab.binding.nanoleaf.internal.discovery.NanoleafPanelsDiscoveryService;
29 import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler;
30 import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.framework.ServiceRegistration;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The {@link NanoleafHandlerFactory} is responsible for creating the controller (bridge)
49 * and panel (thing) handlers.
51 * @author Martin Raepple - Initial contribution
54 @Component(configurationPid = "binding.nanoleaf", service = ThingHandlerFactory.class)
55 public class NanoleafHandlerFactory extends BaseThingHandlerFactory {
57 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
58 .unmodifiableSet(Stream.of(THING_TYPE_LIGHT_PANEL, THING_TYPE_CONTROLLER).collect(Collectors.toSet()));
60 private final Logger logger = LoggerFactory.getLogger(NanoleafHandlerFactory.class);
61 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
62 private final HttpClient httpClient;
65 public NanoleafHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
66 this.httpClient = httpClientFactory.getCommonHttpClient();
70 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
75 protected @Nullable ThingHandler createHandler(Thing thing) {
76 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78 if (THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
79 NanoleafControllerHandler handler = new NanoleafControllerHandler((Bridge) thing, httpClient);
80 registerDiscoveryService(handler);
81 logger.debug("Nanoleaf controller handler created.");
83 } else if (THING_TYPE_LIGHT_PANEL.equals(thingTypeUID)) {
84 NanoleafPanelHandler handler = new NanoleafPanelHandler(thing, httpClient);
85 logger.debug("Nanoleaf panel handler created.");
92 protected void removeHandler(ThingHandler thingHandler) {
93 if (thingHandler instanceof NanoleafControllerHandler) {
94 unregisterDiscoveryService(thingHandler.getThing());
95 logger.debug("Nanoleaf controller handler removed.");
99 private synchronized void registerDiscoveryService(NanoleafControllerHandler bridgeHandler) {
100 NanoleafPanelsDiscoveryService discoveryService = new NanoleafPanelsDiscoveryService(bridgeHandler);
101 discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
102 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
103 logger.debug("Discovery service for panels registered.");
106 @SuppressWarnings("null")
107 private synchronized void unregisterDiscoveryService(Thing thing) {
109 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
110 // would require null check but "if (response!=null)" throws warning on comoile time :ยด-(
111 if (serviceReg != null) {
112 serviceReg.unregister();
114 logger.debug("Discovery service for panels removed.");