Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / ColorControl / ColorControls.groovy
1 //Create a class for color control
2 package ColorControl
3 import Timer.SimulatedTimer
4
5 public class ColorControls {
6         private int deviceNumbers
7         private List colorControls
8         def sendEvent
9
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
19         private int hue = 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
25         
26
27         ColorControls(Closure sendEvent, int deviceNumbers, boolean init) {
28                 this.sendEvent = sendEvent
29                 this.deviceNumbers = deviceNumbers
30                 this.colorControls = []
31
32                 if (init) {
33                         this.level = 20
34                         this.currentLevel = 20
35                         this.hue = 30
36                         this.currentHue = 30
37                         this.saturation = 40
38                         this.currentSaturation = 40
39                         this.colorTemperature = 10000
40                         this.color = "Red"
41                         this.currentSwitch = "off"
42                 } else {
43                         this.level = 50
44                         this.currentLevel = 50
45                         this.hue = 50
46                         this.currentHue = 50
47                         this.saturation = 50
48                         this.currentSaturation = 50
49                         this.colorTemperature = 15000
50                         this.color = "Blue"
51                         this.currentSwitch = "on"
52                 }
53                 colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
54         }
55
56         //Methods for closures
57         def count(Closure Input) {
58                 colorControls.count(Input)
59         }
60         def size() {
61                 colorControls.size()
62         }
63         def each(Closure Input) {
64                 colorControls.each(Input)
65         }
66         def find(Closure Input) {
67                 colorControls.find(Input)
68         }
69         def sort(Closure Input) {
70                 colorControls.sort(Input)
71         }
72         def collect(Closure Input) {
73                 colorControls.collect(Input)
74         }
75
76         //By model checker
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)
84                         }       
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)
91                         }
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)
98                         }
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)
104                         }
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)
110                         }
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)
117                         }
118                 }
119         }
120
121
122         //methods
123         def setColor(LinkedHashMap metaData) {
124                 def hexColor = metaData.hex
125                 def newColor
126                 switch (hexColor) {
127                         case "#0000FF":
128                                 newColor = "Blue"
129                                 break;
130                         case "#00FF00":
131                                 newColor = "Green"
132                                 break;
133                         case "#FFFF00":
134                                 newColor = "Yellow"
135                                 break;
136                         case "#FF6000":
137                                 newColor = "Orange"
138                                 break;
139                         case "#BF7FBF":
140                                 newColor = "Purple"
141                                 break;
142                         case "#FF5F5F":
143                                 newColor = "Pink"
144                                 break;
145                         case "#FF0000":
146                                 newColor = "Red"
147                                 break;
148                         default:
149                                 newColor = "Blue"
150                                 break;
151                 }
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
158                         } else {
159                                 this.colorChanged = true
160                         }
161                 }
162                 def newLevel = metaData.level
163                 def newHue = metaData.hue
164                 def newSaturation = metaData.saturation
165                 if (newLevel != null) {
166                         setLevel(newLevel)
167                 }
168                 if (newHue != null) {
169                         setHue(newHue)
170                 }
171                 if (newSaturation != null) {
172                         setSaturation(newSaturation)
173                 }
174         }
175
176         def setColor(String color) {
177                 if (color != this.color) {
178                         this.currentColor = color
179                         this.color = color
180                         colorControls[0].setColor(color)                        
181                 }
182         }
183
184         def setHue(int hue) {
185                 if (hue != this.hue) {
186                         this.hue = hue
187                         this.currentHue = hue
188                         colorControls[0].setHue(hue)
189                 }
190         }
191
192         def setSaturation(int saturation) {
193                 if (saturation != this.saturation) {
194                         this.currentSaturation = saturation
195                         this.saturation = saturation
196                         colorControls[0].setSaturation(saturation)                      
197                 }       
198         }
199
200         def setLevel(int level) {
201                 if (level != this.level) {
202                         this.currentLevel = level
203                         this.level = level
204                         colorControls[0].setLevel(level)
205                 }
206         }
207
208         def setLevel(long level) {
209                 if (level != this.level) {
210                         this.currentLevel = level
211                         this.level = level
212                         colorControls[0].setLevel(level)
213                 }
214         }
215
216         def setColorTemperature(int colorTemperature) {
217                 if (colorTemperature != this.colorTemperature) {
218                         this.colorTemperature = colorTemperature
219                         colorControls[0].setColorTemperature(colorTemperature)                  
220                 }
221         }
222
223         def setColorTemperature(long colorTemperature) {
224                 if (colorTemperature != this.colorTemperature) {
225                         this.colorTemperature = colorTemperature
226                         colorControls[0].setColorTemperature(colorTemperature)                  
227                 }
228         }
229
230         def on() {
231                 if (this.currentSwitch != "on") {
232                         this.currentSwitch = "on"
233                         colorControls[0].on()                   
234                 }
235         }
236
237         def off() {
238                 if (this.currentSwitch != "off") {
239                         this.currentSwitch = "off"
240                         colorControls[0].off()
241                 }
242         }
243
244         def currentValue(String deviceFeature) {
245                 colorControls[0].currentValue(deviceFeature)
246         }
247
248         def latestValue(String deviceFeature) {
249                 colorControls[0].latestValue(deviceFeature)
250         }       
251
252         def getAt(int ix) {
253                 colorControls[ix]
254         }
255 }