]> git.basschouten.com Git - openhab-addons.git/blob
07b6ef6abd5c11a21e2e62a5c7ab9cedc5a58bfe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.colors;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
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;
25
26 /**
27  * Stores information about panels and their colors, while sending notifications to panels and controllers
28  * about updated states.
29  *
30  * @author Jørgen Austvik - Initial contribution
31  */
32 @NonNullByDefault
33 public class NanoleafPanelColors {
34
35     private final Logger logger = LoggerFactory.getLogger(NanoleafPanelColors.class);
36
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;
41
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
48                 updatePanel = true;
49             }
50         } else {
51             // First time we see this panels color - update the panel thing
52             updatePanel = true;
53         }
54
55         panelColors.put(panelId, color);
56
57         if (updatePanel) {
58             @Nullable
59             NanoleafPanelColorChangeListener panelHandler = panelChangeListeners.get(panelId);
60             if (panelHandler != null) {
61                 panelHandler.onPanelChangedColor(color);
62             }
63         }
64
65         return updatePanel;
66     }
67
68     private void updatePanelColor(Integer panelId, HSBType color) {
69         boolean updatePanel = updatePanelColorNoController(panelId, color);
70         if (updatePanel) {
71             notifyControllerListener();
72         }
73     }
74
75     private void notifyControllerListener() {
76         NanoleafControllerColorChangeListener privateControllerListener = controllerListener;
77         if (privateControllerListener != null) {
78             privateControllerListener.onPanelChangedColor();
79         }
80     }
81
82     /**
83      * Retrieves the color of the panel. Used by the panels to read their state.
84      *
85      * @param panelId The id of the panel
86      * @return The color of the panel
87      */
88     public @Nullable HSBType getPanelColor(Integer panelId) {
89         return panelColors.get(panelId);
90     }
91
92     /**
93      * Called from panels to update the state.
94      *
95      * @param panelId The panel that received the update
96      * @param color The new color of the panel
97      */
98     public void setPanelColor(Integer panelId, HSBType color) {
99         updatePanelColor(panelId, color);
100     }
101
102     public void registerChangeListener(Integer panelId, NanoleafPanelHandler panelListener) {
103         logger.trace("Adding color change listener for panel {}", panelId);
104         panelChangeListeners.put(panelId, panelListener);
105     }
106
107     public void unregisterChangeListener(Integer panelId) {
108         logger.trace("Removing color change listener for panel {}", panelId);
109         panelChangeListeners.remove(panelId);
110     }
111
112     public void registerChangeListener(NanoleafControllerColorChangeListener controllerListener) {
113         logger.trace("Setting color change listener for controller");
114         this.controllerListener = controllerListener;
115     }
116
117     /**
118      * Returns the color of a panel.
119      * 
120      * @param panelId The panel
121      * @param defaultColor Default color if panel is missing color information
122      * @return Color of the panel
123      */
124     public HSBType getColor(Integer panelId, HSBType defaultColor) {
125         return panelColors.getOrDefault(panelId, defaultColor);
126     }
127
128     /**
129      * Returns true if we have color information for the given panel.
130      *
131      * @param panelId The panel to check if has color
132      * @return true if we have color information about the panel
133      */
134     public boolean hasColor(Integer panelId) {
135         return panelColors.containsKey(panelId);
136     }
137
138     /**
139      * Sets all panels to the same color. This will make controller repaint only once.
140      * 
141      * @param panelIds Panels to update
142      * @param color The color for all panels
143      */
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);
149         }
150
151         if (updatePanel) {
152             notifyControllerListener();
153         }
154     }
155 }