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 = "on"
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
26 ColorControls(Closure sendEvent, int deviceNumbers, boolean init) {
27 this.sendEvent = sendEvent
28 this.deviceNumbers = deviceNumbers
29 this.colorControls = []
35 this.currentSwitch = "off"
40 this.currentSwitch = "on"
43 colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
46 //Methods for closures
47 def count(Closure Input) {
48 colorControls.count(Input)
53 def each(Closure Input) {
54 colorControls.each(Input)
56 def find(Closure Input) {
57 colorControls.find(Input)
59 def sort(Closure Input) {
60 colorControls.sort(Input)
62 def collect(Closure Input) {
63 colorControls.collect(Input)
67 def setValue(LinkedHashMap eventDataMap) {
68 if (eventDataMap["name"] == "color") {
69 if (eventDataMap["value"] != colorControls[0].color) {
70 this.currentColor = eventDataMap["value"]
71 this.color = eventDataMap["value"]
72 colorControls[0].setValue(eventDataMap["value"], "color")
73 sendEvent(eventDataMap)
75 } else if (eventDataMap["name"] == "hue") {
76 if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
77 this.hue = eventDataMap["value"].toInteger()
78 this.currentHue = eventDataMap["value"].toInteger()
79 colorControls[0].setValue(eventDataMap["value"], "hue")
80 sendEvent(eventDataMap)
82 } else if (eventDataMap["name"] == "saturation") {
83 if (eventDataMap["value"].toInteger() != colorControls[0].saturation) {
84 this.saturation = eventDataMap["value"].toInteger()
85 this.currentSaturation = eventDataMap["value"].toInteger()
86 colorControls[0].setValue(eventDataMap["value"], "saturation")
87 sendEvent(eventDataMap)
89 } else if (eventDataMap["name"] == "switch") {
90 if (eventDataMap["value"] != colorControls[0].currentSwitch) {
91 this.currentSwitch = eventDataMap["value"]
92 colorControls[0].setValue(eventDataMap["value"], "switch")
93 sendEvent(eventDataMap)
95 } else if (eventDataMap["name"] == "colorTemperature") {
96 if (eventDataMap["value"].toInteger() != colorControls[0].colorTemperature) {
97 this.colorTemperature = eventDataMap["value"].toInteger()
98 colorControls[0].setValue(eventDataMap["value"], "colorTemperature")
99 sendEvent(eventDataMap)
101 } else if (eventDataMap["name"] == "level") {
102 if (eventDataMap["value"].toInteger() != colorControls[0].level) {
103 this.currentLevel = eventDataMap["value"].toInteger()
104 this.level = eventDataMap["value"].toInteger()
105 colorControls[0].setValue(eventDataMap["value"], "level")
106 sendEvent(eventDataMap)
113 def setColor(LinkedHashMap metaData) {
114 def hexColor = metaData.hex
141 if (color != this.color) {
142 this.currentColor = color
144 colorControls[0].setColor(color)
148 def setColor(String color) {
149 if (color != this.color) {
150 this.currentColor = color
152 colorControls[0].setColor(color)
156 def setHue(int hue) {
157 if (hue != this.hue) {
159 this.currentHue = hue
160 colorControls[0].setHue(hue)
164 def setSaturation(int saturation) {
165 if (saturation != this.saturation) {
166 this.currentSaturation = saturation
167 this.saturation = saturation
168 colorControls[0].setSaturation(saturation)
172 def setLevel(int level) {
173 if (level != this.level) {
174 this.currentLevel = level
176 colorControls[0].setLevel(level)
180 def setColorTemperature(int colorTemperature) {
181 if (colorTemperature != this.colorTemperature) {
182 this.colorTemperature = colorTemperature
183 colorControls[0].setColorTemperature(colorTemperature)
188 if (this.currentSwitch != "on") {
189 this.currentSwitch = "on"
190 colorControls[0].on()
195 if (this.currentSwitch != "off") {
196 this.currentSwitch = "off"
197 colorControls[0].off()
201 def currentValue(String deviceFeature) {
202 colorControls[0].currentValue(deviceFeature)
205 def latestValue(String deviceFeature) {
206 colorControls[0].latestValue(deviceFeature)