1 //Create a class for color control
3 import Timer.SimulatedTimer
5 public class ColorControls {
6 private int deviceNumbers
7 private List colorControls
10 //For one device(We cannot have obj.id)-> We should have obj[0].id
11 private String id = "colorControlID0"
12 private String label = "colorControl0"
13 private String displayName = "colorControl0"
14 private String color = "Red"
15 private String currentColor = "Red"
16 private String currentSwitch = "off"
17 private int level = 50
18 private int currentLevel = 50
20 private int currentHue = 50
21 private int saturation = 50
22 private int currentSaturation = 50
23 private int colorTemperature = 15000
24 private boolean colorChanged = false
27 ColorControls(Closure sendEvent, int deviceNumbers, boolean init) {
28 this.sendEvent = sendEvent
29 this.deviceNumbers = deviceNumbers
30 this.colorControls = []
34 this.currentLevel = 20
38 this.currentSaturation = 40
39 this.colorTemperature = 10000
41 this.currentSwitch = "off"
44 this.currentLevel = 50
48 this.currentSaturation = 50
49 this.colorTemperature = 15000
51 this.currentSwitch = "on"
53 colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
56 //Methods for closures
57 def count(Closure Input) {
58 colorControls.count(Input)
63 def each(Closure Input) {
64 colorControls.each(Input)
66 def find(Closure Input) {
67 colorControls.find(Input)
69 def sort(Closure Input) {
70 colorControls.sort(Input)
72 def collect(Closure Input) {
73 colorControls.collect(Input)
77 def setValue(LinkedHashMap eventDataMap) {
78 if (eventDataMap["name"] == "color") {
79 if (eventDataMap["value"] != colorControls[0].color) {
80 this.currentColor = eventDataMap["value"]
81 this.color = eventDataMap["value"]
82 colorControls[0].setValue(eventDataMap["value"], "color")
83 sendEvent(eventDataMap)
85 } else if (eventDataMap["name"] == "hue") {
86 if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
87 this.hue = eventDataMap["value"].toInteger()
88 this.currentHue = eventDataMap["value"].toInteger()
89 colorControls[0].setValue(eventDataMap["value"], "hue")
90 sendEvent(eventDataMap)
92 } else if (eventDataMap["name"] == "saturation") {
93 if (eventDataMap["value"].toInteger() != colorControls[0].saturation) {
94 this.saturation = eventDataMap["value"].toInteger()
95 this.currentSaturation = eventDataMap["value"].toInteger()
96 colorControls[0].setValue(eventDataMap["value"], "saturation")
97 sendEvent(eventDataMap)
99 } else if (eventDataMap["name"] == "switch") {
100 if (eventDataMap["value"] != colorControls[0].currentSwitch) {
101 this.currentSwitch = eventDataMap["value"]
102 colorControls[0].setValue(eventDataMap["value"], "switch")
103 sendEvent(eventDataMap)
105 } else if (eventDataMap["name"] == "colorTemperature") {
106 if (eventDataMap["value"].toInteger() != colorControls[0].colorTemperature) {
107 this.colorTemperature = eventDataMap["value"].toInteger()
108 colorControls[0].setValue(eventDataMap["value"], "colorTemperature")
109 sendEvent(eventDataMap)
111 } else if (eventDataMap["name"] == "level") {
112 if (eventDataMap["value"].toInteger() != colorControls[0].level) {
113 this.currentLevel = eventDataMap["value"].toInteger()
114 this.level = eventDataMap["value"].toInteger()
115 colorControls[0].setValue(eventDataMap["value"], "level")
116 sendEvent(eventDataMap)
123 def setColor(LinkedHashMap metaData) {
124 def hexColor = metaData.hex
152 if (newColor != this.color) {
153 this.currentColor = newColor
154 this.color = newColor
155 colorControls[0].setColor(newColor)
156 if (this.colorChanged) {
157 this.colorChanged = false
159 this.colorChanged = true
162 def newLevel = metaData.level
163 def newHue = metaData.hue
164 def newSaturation = metaData.saturation
165 if (newLevel != null) {
168 if (newHue != null) {
171 if (newSaturation != null) {
172 setSaturation(newSaturation)
176 def setColor(String color) {
177 if (color != this.color) {
178 this.currentColor = color
180 colorControls[0].setColor(color)
184 def setHue(int hue) {
185 if (hue != this.hue) {
187 this.currentHue = hue
188 colorControls[0].setHue(hue)
192 def setHue(double hue) {
193 if (hue != this.hue) {
195 this.currentHue = hue
196 colorControls[0].setHue((int) hue)
200 def setSaturation(int saturation) {
201 if (saturation != this.saturation) {
202 this.currentSaturation = saturation
203 this.saturation = saturation
204 colorControls[0].setSaturation(saturation)
208 def setSaturation(double saturation) {
209 if (saturation != this.saturation) {
210 this.currentSaturation = saturation
211 this.saturation = saturation
212 colorControls[0].setSaturation(saturation)
216 def setLevel(int level) {
217 if (level != this.level) {
218 this.currentLevel = level
220 colorControls[0].setLevel(level)
224 def setLevel(long level) {
225 if (level != this.level) {
226 this.currentLevel = level
228 colorControls[0].setLevel(level)
232 def setColorTemperature(int colorTemperature) {
233 if (colorTemperature != this.colorTemperature) {
234 this.colorTemperature = colorTemperature
235 colorControls[0].setColorTemperature(colorTemperature)
239 def setColorTemperature(long colorTemperature) {
240 if (colorTemperature != this.colorTemperature) {
241 this.colorTemperature = colorTemperature
242 colorControls[0].setColorTemperature(colorTemperature)
247 if (this.currentSwitch != "on") {
248 this.currentSwitch = "on"
249 colorControls[0].on()
254 if (this.currentSwitch != "off") {
255 this.currentSwitch = "off"
256 colorControls[0].off()
260 def currentValue(String deviceFeature) {
261 colorControls[0].currentValue(deviceFeature)
264 def latestValue(String deviceFeature) {
265 colorControls[0].latestValue(deviceFeature)