]> git.basschouten.com Git - openhab-addons.git/blob
0dc244cf6e2ea6118a7dcf44000c3c5ee991bf31
[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.lifx.internal;
14
15 import static org.openhab.binding.lifx.internal.LifxBindingConstants.DEFAULT_COLOR;
16
17 import java.time.Duration;
18 import java.time.LocalDateTime;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.concurrent.CopyOnWriteArrayList;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.lifx.internal.dto.Effect;
26 import org.openhab.binding.lifx.internal.dto.HevCycleState;
27 import org.openhab.binding.lifx.internal.dto.PowerState;
28 import org.openhab.binding.lifx.internal.dto.SignalStrength;
29 import org.openhab.binding.lifx.internal.fields.HSBK;
30 import org.openhab.binding.lifx.internal.listener.LifxLightStateListener;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.PercentType;
34
35 /**
36  * The {@link LifxLightState} stores the properties that represent the state of a light.
37  *
38  * @author Wouter Born - Initial contribution
39  */
40 @NonNullByDefault
41 public class LifxLightState {
42
43     private HSBK[] colors = new HSBK[] { new HSBK(DEFAULT_COLOR) };
44     private @Nullable HevCycleState hevCycleState;
45     private @Nullable PercentType infrared;
46     private @Nullable PowerState powerState;
47     private @Nullable SignalStrength signalStrength;
48     private @Nullable Effect tileEffect;
49
50     private LocalDateTime lastChange = LocalDateTime.MIN;
51     private List<LifxLightStateListener> listeners = new CopyOnWriteArrayList<>();
52
53     public void copy(LifxLightState other) {
54         this.powerState = other.getPowerState();
55         this.colors = other.getColors();
56         this.hevCycleState = other.getHevCycleState();
57         this.infrared = other.getInfrared();
58         this.signalStrength = other.getSignalStrength();
59         this.tileEffect = other.getTileEffect();
60     }
61
62     public @Nullable PowerState getPowerState() {
63         return powerState;
64     }
65
66     public HSBK getColor() {
67         return colors.length > 0 ? new HSBK(colors[0]) : new HSBK(DEFAULT_COLOR);
68     }
69
70     public HSBK getColor(int zoneIndex) {
71         return zoneIndex < colors.length ? new HSBK(colors[zoneIndex]) : new HSBK(DEFAULT_COLOR);
72     }
73
74     public HSBK[] getColors() {
75         HSBK[] colorsCopy = new HSBK[colors.length];
76         for (int i = 0; i < colors.length; i++) {
77             colorsCopy[i] = colors[i] != null ? new HSBK(colors[i]) : null;
78         }
79         return colorsCopy;
80     }
81
82     public @Nullable HevCycleState getHevCycleState() {
83         return hevCycleState;
84     }
85
86     public @Nullable PercentType getInfrared() {
87         return infrared;
88     }
89
90     public @Nullable SignalStrength getSignalStrength() {
91         return signalStrength;
92     }
93
94     public @Nullable Effect getTileEffect() {
95         return tileEffect;
96     }
97
98     public void setColor(HSBType newHSB) {
99         HSBK newColor = getColor();
100         newColor.setHSB(newHSB);
101         setColor(newColor);
102     }
103
104     public void setColor(HSBType newHSB, int zoneIndex) {
105         HSBK newColor = getColor(zoneIndex);
106         newColor.setHSB(newHSB);
107         setColor(newColor, zoneIndex);
108     }
109
110     public void setBrightness(PercentType brightness) {
111         HSBK[] newColors = getColors();
112         for (HSBK newColor : newColors) {
113             newColor.setBrightness(brightness);
114         }
115         setColors(newColors);
116     }
117
118     public void setBrightness(PercentType brightness, int zoneIndex) {
119         HSBK newColor = getColor(zoneIndex);
120         newColor.setBrightness(brightness);
121         setColor(newColor, zoneIndex);
122     }
123
124     public void setColor(HSBK newColor) {
125         HSBK[] newColors = getColors();
126         Arrays.fill(newColors, newColor);
127         setColors(newColors);
128     }
129
130     public void setColor(HSBK newColor, int zoneIndex) {
131         HSBK[] newColors = getColors();
132         newColors[zoneIndex] = newColor;
133         setColors(newColors);
134     }
135
136     public void setColors(HSBK[] newColors) {
137         HSBK[] oldColors = this.colors;
138         this.colors = newColors;
139         updateLastChange();
140         listeners.forEach(listener -> listener.handleColorsChange(oldColors, newColors));
141     }
142
143     public void setPowerState(OnOffType newOnOff) {
144         setPowerState(PowerState.fromOnOffType(newOnOff));
145     }
146
147     public void setPowerState(PowerState newPowerState) {
148         PowerState oldPowerState = this.powerState;
149         this.powerState = newPowerState;
150         updateLastChange();
151         listeners.forEach(listener -> listener.handlePowerStateChange(oldPowerState, newPowerState));
152     }
153
154     public void setTemperature(int kelvin) {
155         HSBK[] newColors = getColors();
156         for (HSBK newColor : newColors) {
157             newColor.setKelvin(kelvin);
158         }
159         setColors(newColors);
160     }
161
162     public void setTemperature(int kelvin, int zoneIndex) {
163         HSBK newColor = getColor(zoneIndex);
164         newColor.setKelvin(kelvin);
165         setColor(newColor, zoneIndex);
166     }
167
168     public void setHevCycleState(HevCycleState newHevCycleState) {
169         HevCycleState oldHevCycleState = this.hevCycleState;
170         this.hevCycleState = newHevCycleState;
171         updateLastChange();
172         listeners.forEach(listener -> listener.handleHevCycleStateChange(oldHevCycleState, newHevCycleState));
173     }
174
175     public void setInfrared(PercentType newInfrared) {
176         PercentType oldInfrared = this.infrared;
177         this.infrared = newInfrared;
178         updateLastChange();
179         listeners.forEach(listener -> listener.handleInfraredChange(oldInfrared, newInfrared));
180     }
181
182     public void setSignalStrength(SignalStrength newSignalStrength) {
183         SignalStrength oldSignalStrength = this.signalStrength;
184         this.signalStrength = newSignalStrength;
185         updateLastChange();
186         listeners.forEach(listener -> listener.handleSignalStrengthChange(oldSignalStrength, newSignalStrength));
187     }
188
189     public void setTileEffect(Effect newEffect) {
190         // Caller has to take care that newEffect is another object
191         Effect oldEffect = tileEffect;
192         tileEffect = newEffect;
193         updateLastChange();
194         listeners.forEach(listener -> listener.handleTileEffectChange(oldEffect, newEffect));
195     }
196
197     private void updateLastChange() {
198         lastChange = LocalDateTime.now();
199     }
200
201     public Duration getDurationSinceLastChange() {
202         return Duration.between(lastChange, LocalDateTime.now());
203     }
204
205     public void addListener(LifxLightStateListener listener) {
206         listeners.add(listener);
207     }
208
209     public void removeListener(LifxLightStateListener listener) {
210         listeners.remove(listener);
211     }
212 }