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