]> git.basschouten.com Git - openhab-addons.git/blob
8c7f2a32e5663049325c17ed2c55c217384f4348
[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.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
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;
46
47 /**
48  * The {@link NanoleafHandlerFactory} is responsible for creating the controller (bridge)
49  * and panel (thing) handlers.
50  *
51  * @author Martin Raepple - Initial contribution
52  */
53 @NonNullByDefault
54 @Component(configurationPid = "binding.nanoleaf", service = ThingHandlerFactory.class)
55 public class NanoleafHandlerFactory extends BaseThingHandlerFactory {
56
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()));
59
60     private final Logger logger = LoggerFactory.getLogger(NanoleafHandlerFactory.class);
61     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
62     private final HttpClient httpClient;
63
64     @Activate
65     public NanoleafHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
66         this.httpClient = httpClientFactory.getCommonHttpClient();
67     }
68
69     @Override
70     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
72     }
73
74     @Override
75     protected @Nullable ThingHandler createHandler(Thing thing) {
76         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
77
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.");
82             return handler;
83         } else if (THING_TYPE_LIGHT_PANEL.equals(thingTypeUID)) {
84             NanoleafPanelHandler handler = new NanoleafPanelHandler(thing, httpClient);
85             logger.debug("Nanoleaf panel handler created.");
86             return handler;
87         }
88         return null;
89     }
90
91     @Override
92     protected void removeHandler(ThingHandler thingHandler) {
93         if (thingHandler instanceof NanoleafControllerHandler) {
94             unregisterDiscoveryService(thingHandler.getThing());
95             logger.debug("Nanoleaf controller handler removed.");
96         }
97     }
98
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.");
104     }
105
106     @SuppressWarnings("null")
107     private synchronized void unregisterDiscoveryService(Thing thing) {
108         @Nullable
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();
113         }
114         logger.debug("Discovery service for panels removed.");
115     }
116 }