]> git.basschouten.com Git - openhab-addons.git/blob
02c6a7a947d616ea744b02187a2e519b25b75606
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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
14 package org.openhab.binding.nanoleaf.internal.layout;
15
16 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.CONFIG_PANEL_ID;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
24 import org.openhab.core.library.types.HSBType;
25 import org.openhab.core.thing.Thing;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Stores the state of the panels.
31  *
32  * @author Jørgen Austvik - Initial contribution
33  */
34 @NonNullByDefault
35 public class PanelState {
36
37     private static final Logger logger = LoggerFactory.getLogger(PanelState.class);
38     private final Map<Integer, HSBType> panelStates = new HashMap<>();
39
40     public PanelState(List<Thing> panels) {
41         for (Thing panel : panels) {
42             Integer panelId = Integer.valueOf(panel.getConfiguration().get(CONFIG_PANEL_ID).toString());
43             NanoleafPanelHandler panelHandler = (NanoleafPanelHandler) panel.getHandler();
44             if (panelHandler != null) {
45                 HSBType c = panelHandler.getColor();
46
47                 if (c == null) {
48                     logger.trace("Panel {}: Failed to get color", panelId);
49                 }
50
51                 HSBType color = (c == null) ? HSBType.BLACK : c;
52                 panelStates.put(panelId, color);
53             } else {
54                 logger.trace("Panel {}: Couldn't find handler", panelId);
55             }
56         }
57     }
58
59     public HSBType getHSBForPanel(Integer panelId) {
60         if (logger.isTraceEnabled()) {
61             if (!panelStates.containsKey(panelId)) {
62                 logger.trace("Failed to get color for panel {}, falling back to black", panelId);
63             }
64         }
65
66         return panelStates.getOrDefault(panelId, HSBType.BLACK);
67     }
68 }