]> git.basschouten.com Git - openhab-addons.git/blob
391d36f63b000a2657a699de738a1fcd02a5ab5f
[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.discovery;
14
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.CONFIG_PANEL_ID;
16
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
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;
38
39 /**
40  * The {@link NanoleafPanelsDiscoveryService} is responsible for discovering the individual
41  * panels connected to the controller.
42  *
43  * @author Martin Raepple - Initial contribution
44  */
45 @NonNullByDefault
46 public class NanoleafPanelsDiscoveryService extends AbstractDiscoveryService implements NanoleafControllerListener {
47
48     private static final int SEARCH_TIMEOUT_SECONDS = 60;
49
50     private final Logger logger = LoggerFactory.getLogger(NanoleafPanelsDiscoveryService.class);
51     private final NanoleafControllerHandler bridgeHandler;
52
53     /**
54      * Constructs a new {@link NanoleafPanelsDiscoveryService} attached to the given bridge handler.
55      *
56      * @param nanoleafControllerHandler The bridge handler this discovery service is attached to
57      */
58     public NanoleafPanelsDiscoveryService(NanoleafControllerHandler nanoleafControllerHandler) {
59         super(NanoleafHandlerFactory.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIMEOUT_SECONDS, false);
60         this.bridgeHandler = nanoleafControllerHandler;
61     }
62
63     @Override
64     protected void startScan() {
65         logger.debug("Starting Nanoleaf panel discovery");
66         bridgeHandler.registerControllerListener(this);
67     }
68
69     @Override
70     protected synchronized void stopScan() {
71         logger.debug("Stopping Nanoleaf panel discovery");
72         super.stopScan();
73         bridgeHandler.unregisterControllerListener(this);
74     }
75
76     /**
77      * Called by the controller handler with bridge and panel data
78      *
79      * @param bridge The controller
80      * @param controllerInfo Panel data (and more)
81      */
82     @Override
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();
86         @Nullable
87         Layout layout = panelLayout.getLayout();
88
89         if (layout != null && layout.getNumPanels() > 0) {
90             @Nullable
91             final List<PositionDatum> positionData = layout.getPositionData();
92             if (positionData != null) {
93                 Iterator<PositionDatum> iterator = positionData.iterator();
94                 while (iterator.hasNext()) {
95                     @Nullable
96                     PositionDatum panel = iterator.next();
97                     ThingUID newPanelThingUID = new ThingUID(NanoleafBindingConstants.THING_TYPE_LIGHT_PANEL, bridge,
98                             Integer.toString(panel.getPanelId()));
99
100                     final Map<String, Object> properties = new HashMap<>(1);
101                     properties.put(CONFIG_PANEL_ID, panel.getPanelId());
102
103                     DiscoveryResult newPanel = DiscoveryResultBuilder.create(newPanelThingUID).withBridge(bridge)
104                             .withProperties(properties).withLabel("Light Panel " + panel.getPanelId())
105                             .withRepresentationProperty(CONFIG_PANEL_ID).build();
106
107                     logger.debug("Adding panel with id {} to inbox", panel.getPanelId());
108                     thingDiscovered(newPanel);
109                 }
110             } else {
111                 logger.debug("Couldn't add panels to inbox as layout position data was null");
112             }
113
114         } else {
115             logger.info("No panels found or connected to controller");
116         }
117     }
118 }