3538447ce14bad689fd59d4e68a8e752d8d49219
[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 int hue = 50
19         private int saturation = 50
20         
21                 
22         ColorControls(Closure sendEvent, int deviceNumbers) {
23                 this.sendEvent = sendEvent
24                 this.deviceNumbers = deviceNumbers
25                 this.colorControls = []
26                 
27                 def initHue = Verify.getIntFromList(30, 50, 70)
28                 this.hue = initHue
29                 def initSat = Verify.getIntFromList(40, 50, 60)
30                 this.saturation = initSat
31                 def init = Verify.getInt(0,2)
32                 if (init == 0) {
33                         this.color = "red"
34                 } else if (init == 1) {
35                         this.color = "green"
36                 } else {
37                         this.color = "blue"
38                 }
39
40                 def initHue = Verify.getIntFromList(30, 50, 70)
41                 this.hue = initHue
42                 def initSat = Verify.getIntFromList(40, 50, 60)
43                 this.saturation = initSat
44                 def init = Verify.getInt(0,2)
45                 if (init == 0) {
46                         this.color = "red"
47                 } else if (init == 1) {
48                         this.color = "green"
49                 } else {
50                         this.color = "blue"
51                 }
52
53                 colorControls.add(new ColorControl(id, label, displayName, this.color, this.hue, this.saturation))
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 collect(Closure Input) {
70                 colorControls.collect(Input)
71         }
72
73         //By model checker
74         def setValue(LinkedHashMap eventDataMap) {
75                 if (eventDataMap["name"] == "color") {
76                         if (eventDataMap["value"] != colorControls[0].color) {
77                                 colorControls[0].setValue(eventDataMap["value"], "color")
78                                 this.color = colorControls[0].color
79                                 sendEvent(eventDataMap)
80                         }       
81                 } else if (eventDataMap["name"] == "hue") {
82                         if (eventDataMap["value"] != colorControls[0].hue) {
83                                 colorControls[0].setValue(eventDataMap["value"], "hue")
84                                 this.hue = colorControls[0].hue
85                                 sendEvent(eventDataMap)
86                         }
87                 } else {
88                         if (eventDataMap["value"] != colorControls[0].saturation) {
89                                 colorControls[0].setValue(eventDataMap["value"], "saturation")
90                                 this.saturation = colorControls[0].saturation
91                                 sendEvent(eventDataMap)
92                         }
93                 }
94         }
95
96
97         //methods
98         def setColor(String color) {
99                 if (color != this.color) {
100                         colorControls[0].setColor(color)
101                         this.color = color
102                 }
103         }
104
105         def setHue(int hue) {
106                 if (hue != this.hue) {          
107                         colorControls[0].setHue(hue)
108                         this.hue = hue
109                 }
110         }
111
112         def setSaturation(int saturation) {
113                 if (saturation != this.saturation) {
114                         colorControls[0].setSaturation(saturation)
115                         this.saturation = saturation
116                 }       
117         }
118
119         def currentValue(String deviceFeature) {
120                 colorControls[0].currentValue(deviceFeature)
121         }
122
123         def getAt(int ix) {
124                 colorControls[ix]
125         }
126 }