43c71e99d058b1d8e94a4685403517c5e66f95d4
[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 = "on"
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)
35                 this.hue = initHue
36                 def initSat = Verify.getIntFromList(40, 50)
37                 this.saturation = initSat
38                 def initColor = Verify.getBoolean()
39                 if (initColor) {
40                         this.color = "red"
41                 } else {
42                         this.color = "blue"
43                 }
44                 def init = Verify.getBoolean()
45                 if (init) {
46                         this.currentSwitch = "off"
47                 } else {
48                         this.currentSwitch = "on"
49                 }*/
50
51                 colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
52         }
53
54         //Methods for closures
55         def count(Closure Input) {
56                 colorControls.count(Input)
57         }
58         def size() {
59                 colorControls.size()
60         }
61         def each(Closure Input) {
62                 colorControls.each(Input)
63         }
64         def find(Closure Input) {
65                 colorControls.find(Input)
66         }
67         def sort(Closure Input) {
68                 colorControls.sort(Input)
69         }
70         def collect(Closure Input) {
71                 colorControls.collect(Input)
72         }
73
74         //By model checker
75         def setValue(LinkedHashMap eventDataMap) {
76                 if (eventDataMap["name"] == "color") {
77                         if (eventDataMap["value"] != colorControls[0].color) {
78                                 this.currentColor = eventDataMap["value"]
79                                 this.color = eventDataMap["value"]
80                                 colorControls[0].setValue(eventDataMap["value"], "color")
81                                 sendEvent(eventDataMap)
82                         }       
83                 } else if (eventDataMap["name"] == "hue") {
84                         if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
85                                 this.hue = eventDataMap["value"].toInteger()
86                                 this.currentHue = eventDataMap["value"].toInteger()
87                                 colorControls[0].setValue(eventDataMap["value"], "hue")
88                                 sendEvent(eventDataMap)
89                         }
90                 } else if (eventDataMap["name"] == "saturation") {
91                         if (eventDataMap["value"].toInteger() != colorControls[0].saturation) {
92                                 this.saturation = eventDataMap["value"].toInteger()
93                                 this.currentSaturation = eventDataMap["value"].toInteger()
94                                 colorControls[0].setValue(eventDataMap["value"], "saturation")
95                                 sendEvent(eventDataMap)
96                         }
97                 } else if (eventDataMap["name"] == "switch") {
98                         if (eventDataMap["value"] != colorControls[0].currentSwitch) {
99                                 this.currentSwitch = eventDataMap["value"]
100                                 colorControls[0].setValue(eventDataMap["value"], "switch")
101                                 sendEvent(eventDataMap)
102                         }
103                 } else if (eventDataMap["name"] == "colorTemperature") {
104                         if (eventDataMap["value"].toInteger() != colorControls[0].colorTemperature) {
105                                 this.colorTemperature = eventDataMap["value"].toInteger()
106                                 colorControls[0].setValue(eventDataMap["value"], "colorTemperature")
107                                 sendEvent(eventDataMap)
108                         }
109                 } else if (eventDataMap["name"] == "level") {
110                         if (eventDataMap["value"].toInteger() != colorControls[0].level) {
111                                 this.currentLevel = eventDataMap["value"].toInteger() 
112                                 this.level = eventDataMap["value"].toInteger()
113                                 colorControls[0].setValue(eventDataMap["value"], "level")
114                                 sendEvent(eventDataMap)
115                         }
116                 }
117         }
118
119
120         //methods
121         def setColor(LinkedHashMap metaData) {
122                 def hexColor = metaData.hex
123                 switch (hexColor) {
124                         case "#0000FF":
125                                 color = "Blue"
126                                 break;
127                         case "#00FF00":
128                                 color = "Green"
129                                 break;
130                         case "#FFFF00":
131                                 color = "Yellow"
132                                 break;
133                         case "#FF6000":
134                                 color = "Orange"
135                                 break;
136                         case "#BF7FBF":
137                                 color = "Purple"
138                                 break;
139                         case "#FF5F5F":
140                                 color = "Pink"
141                                 break;
142                         case "#FF0000":
143                                 color = "Red"
144                                 break;
145                         default:
146                                 color = "Blue"
147                                 break;
148                 }
149                 if (color != this.color) {
150                         this.currentColor = color
151                         this.color = color
152                         colorControls[0].setColor(color)                        
153                 }
154         }
155
156         def setColor(String color) {
157                 if (color != this.color) {
158                         this.currentColor = color
159                         this.color = color
160                         colorControls[0].setColor(color)                        
161                 }
162         }
163
164         def setHue(int hue) {
165                 if (hue != this.hue) {
166                         this.hue = hue
167                         this.currentHue = hue
168                         colorControls[0].setHue(hue)
169                 }
170         }
171
172         def setSaturation(int saturation) {
173                 if (saturation != this.saturation) {
174                         this.currentSaturation = saturation
175                         this.saturation = saturation
176                         colorControls[0].setSaturation(saturation)                      
177                 }       
178         }
179
180         def setLevel(int level) {
181                 if (level != this.level) {
182                         this.currentLevel = level
183                         this.level = level
184                         colorControls[0].setLevel(level)
185                 }
186         }
187
188         def setColorTemperature(String colorTemperature) {
189                 if (colorTemperature != this.colorTemperature) {
190                         this.colorTemperature = colorTemperature
191                         colorControls[0].setColorTemperature(colorTemperature)                  
192                 }
193         }
194
195         def on(String currentSwitch) {
196                 if (currentSwitch != this.currentSwitch) {
197                         this.currentSwitch = currentSwitch
198                         colorControls[0].on(currentSwitch)                      
199                 }
200         }
201
202         def off(String currentSwitch) {
203                 if (currentSwitch != this.currentSwitch) {
204                         this.currentSwitch = currentSwitch
205                         colorControls[0].off(currentSwitch)                     
206                 }
207         }
208
209         def currentValue(String deviceFeature) {
210                 colorControls[0].currentValue(deviceFeature)
211         }
212
213         def latestValue(String deviceFeature) {
214                 colorControls[0].latestValue(deviceFeature)
215         }       
216
217         def getAt(int ix) {
218                 colorControls[ix]
219         }
220 }