Fixing a bug: should have been pushed, not push.
[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         
25
26         ColorControls(Closure sendEvent, int deviceNumbers, boolean init) {
27                 this.sendEvent = sendEvent
28                 this.deviceNumbers = deviceNumbers
29                 this.colorControls = []
30
31                 if (init) {
32                         this.hue = 30
33                         this.saturation = 40
34                         this.color = "red"
35                         this.currentSwitch = "off"
36                 } else {
37                         this.hue = 50
38                         this.saturation = 50
39                         this.color = "blue"
40                         this.currentSwitch = "on"
41                 }
42
43                 colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
44         }
45
46         //Methods for closures
47         def count(Closure Input) {
48                 colorControls.count(Input)
49         }
50         def size() {
51                 colorControls.size()
52         }
53         def each(Closure Input) {
54                 colorControls.each(Input)
55         }
56         def find(Closure Input) {
57                 colorControls.find(Input)
58         }
59         def sort(Closure Input) {
60                 colorControls.sort(Input)
61         }
62         def collect(Closure Input) {
63                 colorControls.collect(Input)
64         }
65
66         //By model checker
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)
74                         }       
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)
81                         }
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)
88                         }
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)
94                         }
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)
100                         }
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)
107                         }
108                 }
109         }
110
111
112         //methods
113         def setColor(LinkedHashMap metaData) {
114                 def hexColor = metaData.hex
115                 switch (hexColor) {
116                         case "#0000FF":
117                                 color = "Blue"
118                                 break;
119                         case "#00FF00":
120                                 color = "Green"
121                                 break;
122                         case "#FFFF00":
123                                 color = "Yellow"
124                                 break;
125                         case "#FF6000":
126                                 color = "Orange"
127                                 break;
128                         case "#BF7FBF":
129                                 color = "Purple"
130                                 break;
131                         case "#FF5F5F":
132                                 color = "Pink"
133                                 break;
134                         case "#FF0000":
135                                 color = "Red"
136                                 break;
137                         default:
138                                 color = "Blue"
139                                 break;
140                 }
141                 if (color != this.color) {
142                         this.currentColor = color
143                         this.color = color
144                         colorControls[0].setColor(color)                        
145                 }
146         }
147
148         def setColor(String color) {
149                 if (color != this.color) {
150                         this.currentColor = color
151                         this.color = color
152                         colorControls[0].setColor(color)                        
153                 }
154         }
155
156         def setHue(int hue) {
157                 if (hue != this.hue) {
158                         this.hue = hue
159                         this.currentHue = hue
160                         colorControls[0].setHue(hue)
161                 }
162         }
163
164         def setSaturation(int saturation) {
165                 if (saturation != this.saturation) {
166                         this.currentSaturation = saturation
167                         this.saturation = saturation
168                         colorControls[0].setSaturation(saturation)                      
169                 }       
170         }
171
172         def setLevel(int level) {
173                 if (level != this.level) {
174                         this.currentLevel = level
175                         this.level = level
176                         colorControls[0].setLevel(level)
177                 }
178         }
179
180         def setColorTemperature(int colorTemperature) {
181                 if (colorTemperature != this.colorTemperature) {
182                         this.colorTemperature = colorTemperature
183                         colorControls[0].setColorTemperature(colorTemperature)                  
184                 }
185         }
186
187         def on() {
188                 if (this.currentSwitch != "on") {
189                         this.currentSwitch = "on"
190                         colorControls[0].on()                   
191                 }
192         }
193
194         def off() {
195                 if (this.currentSwitch != "off") {
196                         this.currentSwitch = "off"
197                         colorControls[0].off()
198                 }
199         }
200
201         def currentValue(String deviceFeature) {
202                 colorControls[0].currentValue(deviceFeature)
203         }
204
205         def latestValue(String deviceFeature) {
206                 colorControls[0].latestValue(deviceFeature)
207         }       
208
209         def getAt(int ix) {
210                 colorControls[ix]
211         }
212 }