edits and remove generated flag from repo
[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(String color) {
118                 if (color != this.color) {
119                         this.currentColor = color
120                         this.color = color
121                         colorControls[0].setColor(color)                        
122                 }
123         }
124
125         def setHue(int hue) {
126                 if (hue != this.hue) {
127                         this.hue = hue
128                         this.currentHue = hue
129                         colorControls[0].setHue(hue)
130                 }
131         }
132
133         def setSaturation(int saturation) {
134                 if (saturation != this.saturation) {
135                         this.currentSaturation = saturation
136                         this.saturation = saturation
137                         colorControls[0].setSaturation(saturation)                      
138                 }       
139         }
140
141         def setLevel(int level) {
142                 if (level != this.level) {
143                         this.currentLevel = level
144                         this.level = level
145                         colorControls[0].setLevel(level)
146                 }
147         }
148
149         def setColorTemperature(String colorTemperature) {
150                 if (colorTemperature != this.colorTemperature) {
151                         this.colorTemperature = colorTemperature
152                         colorControls[0].setColorTemperature(colorTemperature)                  
153                 }
154         }
155
156         def on(String currentSwitch) {
157                 if (currentSwitch != this.currentSwitch) {
158                         this.currentSwitch = currentSwitch
159                         colorControls[0].on(currentSwitch)                      
160                 }
161         }
162
163         def off(String currentSwitch) {
164                 if (currentSwitch != this.currentSwitch) {
165                         this.currentSwitch = currentSwitch
166                         colorControls[0].off(currentSwitch)                     
167                 }
168         }
169
170         def currentValue(String deviceFeature) {
171                 colorControls[0].currentValue(deviceFeature)
172         }
173
174         def latestValue(String deviceFeature) {
175                 colorControls[0].latestValue(deviceFeature)
176         }       
177
178         def getAt(int ix) {
179                 colorControls[ix]
180         }
181 }