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