aea527995d14b286e5f23ca21ebca1574d0cab9b
[smartthings-infrastructure.git] / ColorTemperature / ColorTemperatures.groovy
1 //Create a class for color temperature
2 package ColorTemperature
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class ColorTemperatures {
9         private int deviceNumbers
10         private List colorTemperatues
11         def sendEvent
12
13         //For one device(We cannot have obj.id)-> We should have obj[0].id
14         private String id = "colorTemperatureID0"
15         private String label = "colorTemperature0"
16         private String displayName = "colorTemperature0"
17         private String currentSwitch = "off"
18         private int level = 50
19         private int currentLevel = 50
20         private int colorTemperature = 15000
21         
22
23         ColorTemperatures(Closure sendEvent, int deviceNumbers) {
24                 this.sendEvent = sendEvent
25                 this.deviceNumbers = deviceNumbers
26                 this.colorTemperatues = []
27
28                 def initLevel = Verify.getIntFromList(50, 70)
29                 this.level = initLevel 
30                 def initTemp = Verify.getIntFromList(10000, 15000)
31                 this.colorTemperature = initTemp 
32                 def init = Verify.getBoolean
33                 if (init) {
34                         this.currentSwitch = "off"
35                 } else {
36                         this.currentSwitch = "on"
37                 }
38
39                 colorTemperatues.add(new ColorTemperature(sendEvent, id, label, displayName, this.level, this.currentSwitch, this.colorTemperature))
40         }
41
42         //Methods for closures
43         def count(Closure Input) {
44                 colorTemperatues.count(Input)
45         }
46         def size() {
47                 colorTemperatues.size()
48         }
49         def each(Closure Input) {
50                 colorTemperatues.each(Input)
51         }
52         def find(Closure Input) {
53                 colorTemperatues.find(Input)
54         }
55         def sort(Closure Input) {
56                 colorTemperatues.sort(Input)
57         }
58         def collect(Closure Input) {
59                 colorTemperatues.collect(Input)
60         }
61
62         //By model checker
63         def setValue(LinkedHashMap eventDataMap) {
64                 if (eventDataMap["name"] == "switch") {
65                         if (eventDataMap["value"] != colorTemperatues[0].currentSwitch) {
66                                 this.currentSwitch = eventDataMap["value"]
67                                 colorTemperatues[0].setValue(eventDataMap["value"], "switch")
68                                 sendEvent(eventDataMap)
69                         }
70                 } else if (eventDataMap["name"] == "colorTemperature") {
71                         if (eventDataMap["value"].toInteger() != colorTemperatues[0].colorTemperature) {
72                                 this.colorTemperature = eventDataMap["value"].toInteger()
73                                 colorTemperatues[0].setValue(eventDataMap["value"], "colorTemperature")
74                                 sendEvent(eventDataMap)
75                         }
76                 } else if (eventDataMap["name"] == "level") {
77                         if (eventDataMap["value"].toInteger() != colorTemperatues[0].level) {
78                                 this.currentLevel = eventDataMap["value"].toInteger() 
79                                 this.level = eventDataMap["value"].toInteger()
80                                 colorTemperatues[0].setValue(eventDataMap["value"], "level")
81                                 sendEvent(eventDataMap)
82                         }
83                 }
84         }
85
86
87         //methods
88         def setLevel(int level) {
89                 if (level != this.level) {
90                         this.currentLevel = level
91                         this.level = level
92                         colorTemperatues[0].setLevel(level)
93                 }
94         }
95
96         def setColorTemperature(String colorTemperature) {
97                 if (colorTemperature != this.colorTemperature) {
98                         this.colorTemperature = colorTemperature
99                         colorTemperatues[0].setColorTemperature(colorTemperature)                       
100                 }
101         }
102
103         def on(String currentSwitch) {
104                 if (currentSwitch != this.currentSwitch) {
105                         this.currentSwitch = currentSwitch
106                         colorTemperatues[0].on(currentSwitch)                   
107                 }
108         }
109
110         def off(String currentSwitch) {
111                 if (currentSwitch != this.currentSwitch) {
112                         this.currentSwitch = currentSwitch
113                         colorTemperatues[0].off(currentSwitch)                  
114                 }
115         }
116
117         def currentValue(String deviceFeature) {
118                 colorTemperatues[0].currentValue(deviceFeature)
119         }
120
121         def latestValue(String deviceFeature) {
122                 colorTemperatues[0].latestValue(deviceFeature)
123         }       
124
125         def getAt(int ix) {
126                 colorTemperatues[ix]
127         }
128 }