2 * Copyright (c) 2010-2022 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
14 package org.openhab.binding.nanoleaf.internal.layout;
16 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.CONFIG_PANEL_ID;
18 import java.util.HashMap;
19 import java.util.List;
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;
30 * Stores the state of the panels.
32 * @author Jørgen Austvik - Initial contribution
35 public class PanelState {
37 private static final Logger logger = LoggerFactory.getLogger(PanelState.class);
38 private final Map<Integer, HSBType> panelStates = new HashMap<>();
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();
48 logger.trace("Panel {}: Failed to get color", panelId);
51 HSBType color = (c == null) ? HSBType.BLACK : c;
52 panelStates.put(panelId, color);
54 logger.trace("Panel {}: Couldn't find handler", panelId);
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);
66 return panelStates.getOrDefault(panelId, HSBType.BLACK);