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.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.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link NanoleafPanelsDiscoveryService} is responsible for discovering the individual
41 * panels connected to the controller.
43 * @author Martin Raepple - Initial contribution
46 public class NanoleafPanelsDiscoveryService extends AbstractDiscoveryService implements NanoleafControllerListener {
48 private static final int SEARCH_TIMEOUT_SECONDS = 60;
50 private final Logger logger = LoggerFactory.getLogger(NanoleafPanelsDiscoveryService.class);
51 private final NanoleafControllerHandler bridgeHandler;
54 * Constructs a new {@link NanoleafPanelsDiscoveryService} attached to the given bridge handler.
56 * @param nanoleafControllerHandler The bridge handler this discovery service is attached to
58 public NanoleafPanelsDiscoveryService(NanoleafControllerHandler nanoleafControllerHandler) {
59 super(NanoleafHandlerFactory.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIMEOUT_SECONDS, false);
60 this.bridgeHandler = nanoleafControllerHandler;
64 protected void startScan() {
65 logger.debug("Starting Nanoleaf panel discovery");
66 bridgeHandler.registerControllerListener(this);
70 protected synchronized void stopScan() {
71 logger.debug("Stopping Nanoleaf panel discovery");
73 bridgeHandler.unregisterControllerListener(this);
77 * Called by the controller handler with bridge and panel data
79 * @param bridge The controller
80 * @param controllerInfo Panel data (and more)
83 public void onControllerInfoFetched(ThingUID bridge, ControllerInfo controllerInfo) {
84 logger.debug("Discover panels connected to controller with id {}", bridge.getAsString());
85 final PanelLayout panelLayout = controllerInfo.getPanelLayout();
87 Layout layout = panelLayout.getLayout();
89 if (layout != null && layout.getNumPanels() > 0) {
91 final List<PositionDatum> positionData = layout.getPositionData();
92 if (positionData != null) {
93 Iterator<PositionDatum> iterator = positionData.iterator();
94 while (iterator.hasNext()) {
96 PositionDatum panel = iterator.next();
97 ThingUID newPanelThingUID = new ThingUID(NanoleafBindingConstants.THING_TYPE_LIGHT_PANEL, bridge,
98 Integer.toString(panel.getPanelId()));
100 final Map<String, Object> properties = new HashMap<>(1);
101 properties.put(CONFIG_PANEL_ID, panel.getPanelId());
103 DiscoveryResult newPanel = DiscoveryResultBuilder.create(newPanelThingUID).withBridge(bridge)
104 .withProperties(properties).withLabel("Light Panel " + panel.getPanelId())
105 .withRepresentationProperty(CONFIG_PANEL_ID).build();
107 logger.debug("Adding panel with id {} to inbox", panel.getPanelId());
108 thingDiscovered(newPanel);
111 logger.debug("Couldn't add panels to inbox as layout position data was null");
115 logger.info("No panels found or connected to controller");