Infrastructure works for lights' group now!
[smartthings-infrastructure.git] / ColorControl / ColorControls.groovy
1 //Create a class for color control
2 package ColorControl
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class ColorControls {
9         private int deviceNumbers
10         private List colorControls
11         def sendEvent
12
13         //For one device(We cannot have obj.id)-> We should have obj[0].id
14         private String id = "colorControlID0"
15         private String label = "colorControl0"
16         private String displayName = "colorControl0"
17         private String color = "red"
18         private String currentColor = "red"
19         private String currentSwitch = "off"
20         private int level = 50
21         private int currentLevel = 50
22         private int hue = 50
23         private int currentHue = 50
24         private int saturation = 50
25         private int currentSaturation = 50
26         private int colorTemperature = 15000
27         
28
29         ColorControls(Closure sendEvent, int deviceNumbers) {
30                 this.sendEvent = sendEvent
31                 this.deviceNumbers = deviceNumbers
32                 this.colorControls = []
33
34                 /*def initHue = Verify.getIntFromList(30, 50, 70)
35                 this.hue = initHue
36                 def initSat = Verify.getIntFromList(40, 50, 60)
37                 this.saturation = initSat
38                 def init = Verify.getInt(0,2)
39                 if (init == 0) {
40                         this.color = "red"
41                 } else if (init == 1) {
42                         this.color = "green"
43                 } else {
44                         this.color = "blue"
45                 }*/
46
47                 colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
48         }
49
50         //Methods for closures
51         def count(Closure Input) {
52                 colorControls.count(Input)
53         }
54         def size() {
55                 colorControls.size()
56         }
57         def each(Closure Input) {
58                 colorControls.each(Input)
59         }
60         def find(Closure Input) {
61                 colorControls.find(Input)
62         }
63         def sort(Closure Input) {
64                 colorControls.sort(Input)
65         }
66         def collect(Closure Input) {
67                 colorControls.collect(Input)
68         }
69
70         //By model checker
71         def setValue(LinkedHashMap eventDataMap) {
72                 if (eventDataMap["name"] == "color") {
73                         if (eventDataMap["value"] != colorControls[0].color) {
74                                 this.currentColor = eventDataMap["value"]
75                                 this.color = eventDataMap["value"]
76                                 colorControls[0].setValue(eventDataMap["value"], "color")
77                                 sendEvent(eventDataMap)
78                         }       
79                 } else if (eventDataMap["name"] == "hue") {
80                         if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
81                                 this.hue = eventDataMap["value"].toInteger()
82                                 this.currentHue = eventDataMap["value"].toInteger()
83                                 colorControls[0].setValue(eventDataMap["value"], "hue")
84                                 sendEvent(eventDataMap)
85                         }
86                 } else if (eventDataMap["name"] == "saturation") {
87                         if (eventDataMap["value"].toInteger() != colorControls[0].saturation) {
88                                 this.saturation = eventDataMap["value"].toInteger()
89                                 this.currentSaturation = eventDataMap["value"].toInteger()
90                                 colorControls[0].setValue(eventDataMap["value"], "saturation")
91                                 sendEvent(eventDataMap)
92                         }
93                 } else if (eventDataMap["name"] == "switch") {
94                         if (eventDataMap["value"] != colorControls[0].currentSwitch) {
95                                 this.currentSwitch = eventDataMap["value"]
96                                 colorControls[0].setValue(eventDataMap["value"], "switch")
97                                 sendEvent(eventDataMap)
98                         }
99                 } else if (eventDataMap["name"] == "colorTemperature") {
100                         if (eventDataMap["value"].toInteger() != colorControls[0].colorTemperature) {
101                                 this.colorTemperature = eventDataMap["value"].toInteger()
102                                 colorControls[0].setValue(eventDataMap["value"], "colorTemperature")
103                                 sendEvent(eventDataMap)
104                         }
105                 } else if (eventDataMap["name"] == "level") {
106                         if (eventDataMap["value"].toInteger() != colorControls[0].level) {
107                                 this.currentLevel = eventDataMap["value"].toInteger() 
108                                 this.level = eventDataMap["value"].toInteger()
109                                 colorControls[0].setValue(eventDataMap["value"], "level")
110                                 sendEvent(eventDataMap)
111                         }
112                 }
113         }
114
115
116         //methods
117         def setColor(LinkedHashMap metaData) {
118                 def hexColor = metaData.hex
119                 switch (hexColor) {
120                         case "#0000FF":
121                                 color = "Blue"
122                                 break;
123                         case "#00FF00":
124                                 color = "Green"
125                                 break;
126                         case "#FFFF00":
127                                 color = "Yellow"
128                                 break;
129                         case "#FF6000":
130                                 color = "Orange"
131                                 break;
132                         case "#BF7FBF":
133                                 color = "Purple"
134                                 break;
135                         case "#FF5F5F":
136                                 color = "Pink"
137                                 break;
138                         case "#FF0000":
139                                 color = "Red"
140                                 break;
141                         default:
142                                 color = "Blue"
143                                 break;
144                 }
145                 if (color != this.color) {
146                         this.currentColor = color
147                         this.color = color
148                         colorControls[0].setColor(color)                        
149                 }
150         }
151
152         def setColor(String color) {
153                 if (color != this.color) {
154                         this.currentColor = color
155                         this.color = color
156                         colorControls[0].setColor(color)                        
157                 }
158         }
159
160         def setHue(int hue) {
161                 if (hue != this.hue) {
162                         this.hue = hue
163                         this.currentHue = hue
164                         colorControls[0].setHue(hue)
165                 }
166         }
167
168         def setSaturation(int saturation) {
169                 if (saturation != this.saturation) {
170                         this.currentSaturation = saturation
171                         this.saturation = saturation
172                         colorControls[0].setSaturation(saturation)                      
173                 }       
174         }
175
176         def setLevel(int level) {
177                 if (level != this.level) {
178                         this.currentLevel = level
179                         this.level = level
180                         colorControls[0].setLevel(level)
181                 }
182         }
183
184         def setColorTemperature(String colorTemperature) {
185                 if (colorTemperature != this.colorTemperature) {
186                         this.colorTemperature = colorTemperature
187                         colorControls[0].setColorTemperature(colorTemperature)                  
188                 }
189         }
190
191         def on(String currentSwitch) {
192                 if (currentSwitch != this.currentSwitch) {
193                         this.currentSwitch = currentSwitch
194                         colorControls[0].on(currentSwitch)                      
195                 }
196         }
197
198         def off(String currentSwitch) {
199                 if (currentSwitch != this.currentSwitch) {
200                         this.currentSwitch = currentSwitch
201                         colorControls[0].off(currentSwitch)                     
202                 }
203         }
204
205         def currentValue(String deviceFeature) {
206                 colorControls[0].currentValue(deviceFeature)
207         }
208
209         def latestValue(String deviceFeature) {
210                 colorControls[0].latestValue(deviceFeature)
211         }       
212
213         def getAt(int ix) {
214                 colorControls[ix]
215         }
216 }