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