Adding path explorations for initializations.
[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                 colorControls.add(new ColorControl(id, label, displayName, this.color, this.hue, this.saturation))
41         }
42
43         //Methods for closures
44         def count(Closure Input) {
45                 colorControls.count(Input)
46         }
47         def size() {
48                 colorControls.size()
49         }
50         def each(Closure Input) {
51                 colorControls.each(Input)
52         }
53         def find(Closure Input) {
54                 colorControls.find(Input)
55         }
56         def collect(Closure Input) {
57                 colorControls.collect(Input)
58         }
59
60         //By model checker
61         def setValue(LinkedHashMap eventDataMap) {
62                 if (eventDataMap["name"] == "color") {
63                         if (eventDataMap["value"] != colorControls[0].color) {
64                                 colorControls[0].setValue(eventDataMap["value"], "color")
65                                 this.color = colorControls[0].color
66                                 sendEvent(eventDataMap)
67                         }       
68                 } else if (eventDataMap["name"] == "hue") {
69                         if (eventDataMap["value"] != colorControls[0].hue) {
70                                 colorControls[0].setValue(eventDataMap["value"], "hue")
71                                 this.hue = colorControls[0].hue
72                                 sendEvent(eventDataMap)
73                         }
74                 } else {
75                         if (eventDataMap["value"] != colorControls[0].saturation) {
76                                 colorControls[0].setValue(eventDataMap["value"], "saturation")
77                                 this.saturation = colorControls[0].saturation
78                                 sendEvent(eventDataMap)
79                         }
80                 }
81         }
82
83
84         //methods
85         def setColor(String color) {
86                 colorControls[0].setColor(color)
87                 this.color = color      
88         }
89
90         def setHue(int hue) {
91                 colorControls[0].setHue(hue)
92                 this.hue = hue  
93         }
94
95         def setSaturation(int saturation) {
96                 colorControls[0].setSaturation(saturation)
97                 this.saturation = saturation    
98         }
99
100         def currentValue(String deviceFeature) {
101                 colorControls[0].currentValue(deviceFeature)
102         }
103
104         def getAt(int ix) {
105                 colorControls[ix]
106         }
107 }