2 * Copyright (c) 2010-2023 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.discovery;
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.CONFIG_PANEL_ID;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants;
25 import org.openhab.binding.nanoleaf.internal.NanoleafControllerListener;
26 import org.openhab.binding.nanoleaf.internal.NanoleafHandlerFactory;
27 import org.openhab.binding.nanoleaf.internal.handler.NanoleafControllerHandler;
28 import org.openhab.binding.nanoleaf.internal.model.ControllerInfo;
29 import org.openhab.binding.nanoleaf.internal.model.Layout;
30 import org.openhab.binding.nanoleaf.internal.model.PanelLayout;
31 import org.openhab.binding.nanoleaf.internal.model.PositionDatum;
32 import org.openhab.core.config.discovery.AbstractDiscoveryService;
33 import org.openhab.core.config.discovery.DiscoveryResult;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.BridgeHandler;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link NanoleafPanelsDiscoveryService} is responsible for discovering the individual
44 * panels connected to the controller.
46 * @author Martin Raepple - Initial contribution
47 * @author Kai Kreuzer - Made it a ThingHandlerService
50 public class NanoleafPanelsDiscoveryService extends AbstractDiscoveryService
51 implements NanoleafControllerListener, ThingHandlerService {
53 private static final int SEARCH_TIMEOUT_SECONDS = 60;
55 private final Logger logger = LoggerFactory.getLogger(NanoleafPanelsDiscoveryService.class);
56 private @Nullable NanoleafControllerHandler bridgeHandler;
57 private @Nullable ControllerInfo controllerInfo;
60 * Constructs a new {@link NanoleafPanelsDiscoveryService}.
62 public NanoleafPanelsDiscoveryService() {
63 super(NanoleafHandlerFactory.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIMEOUT_SECONDS, false);
67 public void deactivate() {
68 NanoleafControllerHandler localBridgeHandler = bridgeHandler;
69 if (localBridgeHandler != null) {
70 Boolean result = localBridgeHandler.unregisterControllerListener(this);
71 logger.debug("unregistration of controller was {}", result ? "successful" : "unsuccessful");
77 protected void startScan() {
78 logger.debug("Starting Nanoleaf panel discovery");
79 createResultsFromControllerInfo();
83 * Called by the controller handler with bridge and panel data
85 * @param bridge The controller
86 * @param controllerInfo Panel data (and more)
89 public void onControllerInfoFetched(ThingUID bridge, ControllerInfo controllerInfo) {
90 this.controllerInfo = controllerInfo;
93 private void createResultsFromControllerInfo() {
95 BridgeHandler localBridgeHandler = bridgeHandler;
96 if (localBridgeHandler != null) {
97 bridgeUID = localBridgeHandler.getThing().getUID();
102 ControllerInfo localControllerInfo = controllerInfo;
103 if (localControllerInfo != null) {
104 final PanelLayout panelLayout = localControllerInfo.getPanelLayout();
106 Layout layout = panelLayout.getLayout();
108 if (layout != null && layout.getNumPanels() > 0) {
110 final List<PositionDatum> positionData = layout.getPositionData();
111 if (positionData != null) {
112 Iterator<PositionDatum> iterator = positionData.iterator();
113 while (iterator.hasNext()) {
115 PositionDatum panel = iterator.next();
116 ThingUID newPanelThingUID = new ThingUID(NanoleafBindingConstants.THING_TYPE_LIGHT_PANEL,
117 bridgeUID, Integer.toString(panel.getPanelId()));
119 final Map<String, Object> properties = new HashMap<>(1);
120 properties.put(CONFIG_PANEL_ID, panel.getPanelId());
122 DiscoveryResult newPanel = DiscoveryResultBuilder.create(newPanelThingUID).withBridge(bridgeUID)
123 .withProperties(properties).withLabel("Light Panel " + panel.getPanelId())
124 .withRepresentationProperty(CONFIG_PANEL_ID).build();
126 logger.debug("Adding panel with id {} to inbox", panel.getPanelId());
127 thingDiscovered(newPanel);
130 logger.debug("Couldn't add panels to inbox as layout position data was null");
134 logger.debug("No panels found or connected to controller");
140 public void setThingHandler(ThingHandler handler) {
141 this.bridgeHandler = (NanoleafControllerHandler) handler;
142 NanoleafControllerHandler localBridgeHandler = (NanoleafControllerHandler) handler;
144 localBridgeHandler.registerControllerListener(this);
148 public @Nullable ThingHandler getThingHandler() {
149 return bridgeHandler;