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.colors;
15 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.nanoleaf.internal.handler.NanoleafPanelHandler;
22 import org.openhab.core.library.types.HSBType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Stores information about panels and their colors, while sending notifications to panels and controllers
28 * about updated states.
30 * @author Jørgen Austvik - Initial contribution
33 public class NanoleafPanelColors {
35 private final Logger logger = LoggerFactory.getLogger(NanoleafPanelColors.class);
37 // holds current color data per panel
38 private final Map<Integer, HSBType> panelColors = new ConcurrentHashMap<>();
39 private final Map<Integer, NanoleafPanelColorChangeListener> panelChangeListeners = new ConcurrentHashMap<>();
40 private @Nullable NanoleafControllerColorChangeListener controllerListener;
42 private boolean updatePanelColorNoController(Integer panelId, HSBType color) {
43 boolean updatePanel = false;
44 if (panelColors.containsKey(panelId)) {
45 HSBType existingColor = panelColors.get(panelId);
46 if (existingColor != null && !existingColor.equals(color)) {
47 // Color change - update the panel thing
51 // First time we see this panels color - update the panel thing
55 panelColors.put(panelId, color);
59 NanoleafPanelColorChangeListener panelHandler = panelChangeListeners.get(panelId);
60 if (panelHandler != null) {
61 panelHandler.onPanelChangedColor(color);
68 private void updatePanelColor(Integer panelId, HSBType color) {
69 boolean updatePanel = updatePanelColorNoController(panelId, color);
71 notifyControllerListener();
75 private void notifyControllerListener() {
76 NanoleafControllerColorChangeListener privateControllerListener = controllerListener;
77 if (privateControllerListener != null) {
78 privateControllerListener.onPanelChangedColor();
83 * Retrieves the color of the panel. Used by the panels to read their state.
85 * @param panelId The id of the panel
86 * @return The color of the panel
88 public @Nullable HSBType getPanelColor(Integer panelId) {
89 return panelColors.get(panelId);
93 * Called from panels to update the state.
95 * @param panelId The panel that received the update
96 * @param color The new color of the panel
98 public void setPanelColor(Integer panelId, HSBType color) {
99 updatePanelColor(panelId, color);
102 public void registerChangeListener(Integer panelId, NanoleafPanelHandler panelListener) {
103 logger.trace("Adding color change listener for panel {}", panelId);
104 panelChangeListeners.put(panelId, panelListener);
107 public void unregisterChangeListener(Integer panelId) {
108 logger.trace("Removing color change listener for panel {}", panelId);
109 panelChangeListeners.remove(panelId);
112 public void registerChangeListener(NanoleafControllerColorChangeListener controllerListener) {
113 logger.trace("Setting color change listener for controller");
114 this.controllerListener = controllerListener;
118 * Returns the color of a panel.
120 * @param panelId The panel
121 * @param defaultColor Default color if panel is missing color information
122 * @return Color of the panel
124 public HSBType getColor(Integer panelId, HSBType defaultColor) {
125 return panelColors.getOrDefault(panelId, defaultColor);
129 * Returns true if we have color information for the given panel.
131 * @param panelId The panel to check if has color
132 * @return true if we have color information about the panel
134 public boolean hasColor(Integer panelId) {
135 return panelColors.containsKey(panelId);
139 * Sets all panels to the same color. This will make controller repaint only once.
141 * @param panelIds Panels to update
142 * @param color The color for all panels
144 public void setMultiple(List<Integer> panelIds, HSBType color) {
145 logger.debug("Setting all panels to color {}", color);
146 boolean updatePanel = false;
147 for (Integer panelId : panelIds) {
148 updatePanel |= updatePanelColorNoController(panelId, color);
152 notifyControllerListener();