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