Interleaving events.
[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 currentSwitch = "off"
19         private int level = 50
20         private int hue = 50
21         private int saturation = 50
22         private int colorTemperature = 15000
23         
24
25         ColorControls(Closure sendEvent, int deviceNumbers) {
26                 this.sendEvent = sendEvent
27                 this.deviceNumbers = deviceNumbers
28                 this.colorControls = []
29
30                 /*def initHue = Verify.getIntFromList(30, 50, 70)
31                 this.hue = initHue
32                 def initSat = Verify.getIntFromList(40, 50, 60)
33                 this.saturation = initSat
34                 def init = Verify.getInt(0,2)
35                 if (init == 0) {
36                         this.color = "red"
37                 } else if (init == 1) {
38                         this.color = "green"
39                 } else {
40                         this.color = "blue"
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.color = eventDataMap["value"]
71                                 colorControls[0].setValue(eventDataMap["value"], "color")
72                                 sendEvent(eventDataMap)
73                         }       
74                 } else if (eventDataMap["name"] == "hue") {
75                         if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
76                                 this.hue = eventDataMap["value"].toInteger()
77                                 colorControls[0].setValue(eventDataMap["value"], "hue")
78                                 sendEvent(eventDataMap)
79                         }
80                 } else if (eventDataMap["name"] == "saturation") {
81                         if (eventDataMap["value"].toInteger() != colorControls[0].saturation) {
82                                 this.saturation = eventDataMap["value"].toInteger()
83                                 colorControls[0].setValue(eventDataMap["value"], "saturation")
84                                 sendEvent(eventDataMap)
85                         }
86                 } else if (eventDataMap["name"] == "switch") {
87                         if (eventDataMap["value"] != colorControls[0].currentSwitch) {
88                                 this.currentSwitch = eventDataMap["value"]
89                                 colorControls[0].setValue(eventDataMap["value"], "switch")
90                                 sendEvent(eventDataMap)
91                         }
92                 } else if (eventDataMap["name"] == "colorTemperature") {
93                         if (eventDataMap["value"].toInteger() != colorControls[0].colorTemperature) {
94                                 this.colorTemperature = eventDataMap["value"].toInteger()
95                                 colorControls[0].setValue(eventDataMap["value"], "colorTemperature")
96                                 sendEvent(eventDataMap)
97                         }
98                 } else if (eventDataMap["name"] == "level") {
99                         if (eventDataMap["value"].toInteger() != colorControls[0].level) {
100                                 this.level = eventDataMap["value"].toInteger()
101                                 colorControls[0].setValue(eventDataMap["value"], "level")
102                                 sendEvent(eventDataMap)
103                         }
104                 }
105         }
106
107
108         //methods
109         def setColor(String color) {
110                 if (color != this.color) {
111                         this.color = color
112                         colorControls[0].setColor(color)                        
113                 }
114         }
115
116         def setHue(int hue) {
117                 if (hue != this.hue) {
118                         this.hue = hue  
119                         colorControls[0].setHue(hue)
120                 }
121         }
122
123         def setSaturation(int saturation) {
124                 if (saturation != this.saturation) {
125                         this.saturation = saturation
126                         colorControls[0].setSaturation(saturation)                      
127                 }       
128         }
129
130         def setLevel(int level) {
131                 if (level != this.level) {
132                         this.level = level
133                         colorControls[0].setLevel(level)
134                 }
135         }
136
137         def setColorTemperature(String colorTemperature) {
138                 if (colorTemperature != this.colorTemperature) {
139                         this.colorTemperature = colorTemperature
140                         colorControls[0].setColorTemperature(colorTemperature)                  
141                 }
142         }
143
144         def on(String currentSwitch) {
145                 if (currentSwitch != this.currentSwitch) {
146                         this.currentSwitch = currentSwitch
147                         colorControls[0].on(currentSwitch)                      
148                 }
149         }
150
151         def off(String currentSwitch) {
152                 if (currentSwitch != this.currentSwitch) {
153                         this.currentSwitch = currentSwitch
154                         colorControls[0].off(currentSwitch)                     
155                 }
156         }
157
158         def currentValue(String deviceFeature) {
159                 colorControls[0].currentValue(deviceFeature)
160         }
161
162         def latestValue(String deviceFeature) {
163                 colorControls[0].latestValue(deviceFeature)
164         }       
165
166         def getAt(int ix) {
167                 colorControls[ix]
168         }
169 }