Changing Verify API call scheme for device handlers.
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevels.groovy
1 //Create a class for switch level
2 package SwitchLevel
3 import Timer.SimulatedTimer
4
5 public class SwitchLevels {
6         int deviceNumbers       
7         List switchLevels
8         def timers
9         def sendEvent
10
11         //If we have only one device
12         private String id = "switchLevelID0"
13         private String label = "switchLevel0"
14         private String displayName = "switchLevel0"
15         private int level = 50
16         private int rate = 50
17         private int hue = 30
18         private int saturation = 70
19         private String switchState = "on"
20         private String currentSwitch = "on"
21         private String switchLatestValue = "on"
22
23         SwitchLevels(Closure sendEvent, int deviceNumbers, boolean init) {
24                 this.sendEvent = sendEvent
25                 this.timers = new SimulatedTimer()
26                 this.deviceNumbers = deviceNumbers
27                 this.switchLevels = []
28
29                 if (init) {
30                         this.level = 50
31                         this.rate = 50
32                         this.hue = 30
33                         this.saturation = 70
34                         this.switchState = "off"
35                         this.currentSwitch = "off"
36                         this.switchLatestValue = "off"
37                 } else {
38                         this.level = 60
39                         this.rate = 60
40                         this.hue = 50
41                         this.saturation = 90
42                         this.switchState = "on"
43                         this.currentSwitch = "on"
44                         this.switchLatestValue = "on"
45                 }
46                 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
47         }
48
49         //Methods for closures
50         def count(Closure Input) {
51                 switchLevels.count(Input)
52         }
53         def size() {
54                 switchLevels.size()
55         }
56         def each(Closure Input) {
57                 switchLevels.each(Input)
58         }
59         def find(Closure Input) {
60                 switchLevels.find(Input)
61         }
62         def sort(Closure Input) {
63                 switchLevels.sort(Input)
64         }
65         def collect(Closure Input) {
66                 switchLevels.collect(Input)
67         }
68
69         //By Apps
70         def setColor(LinkedHashMap metaData) {
71                 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
72                         this.level = metaData["level"]
73                         this.rate = metaData["level"]
74                         this.hue = metaData["hue"]
75                         this.saturation = metaData["saturation"]
76                         switchLevels[0].setColor(metaData)
77                 }
78         }
79
80         def setLevel(int level) {
81                 if (this.level != level) {
82                         switchLevels[0].setLevel(level)
83                         this.level = level
84                         this.rate = level
85                 }
86         }
87         
88         def setLevel(long level) {
89                 if (this.level != level) {
90                         switchLevels[0].setLevel(level)
91                         this.level = level
92                         this.rate = level
93                 }
94         }
95
96         def on() {
97                 switchLatestValue = "on"
98                 switchState = "on"
99                 currentSwitch = "on"
100                 switchLevels[0].on()
101         }
102
103         def on(LinkedHashMap metaData) {
104                 def task = timers.runAfter(metaData["delay"]) {
105                         switchLatestValue = "on"
106                         switchState = "on"
107                         currentSwitch = "on"
108                         switchLevels[0].on()
109                 }
110         }
111
112         def off() {
113                 switchLatestValue = "off"
114                 switchState = "off"
115                 currentSwitch = "off"
116                 switchLevels[0].off()
117         }
118
119         def off(LinkedHashMap metaData) {
120                 def task = timers.runAfter(metaData["delay"]) {
121                         switchLatestValue = "off"
122                         switchState = "off"
123                         currentSwitch = "off"
124                         switchLevels[0].off()
125                 }
126         }
127
128         //By Model Checker
129         def setValue(LinkedHashMap eventDataMap) {
130                 if (eventDataMap["name"] == "switch") {
131                         if (eventDataMap["value"] != switchLevels[0].switchState) {
132                                 this.switchState = eventDataMap["value"]
133                                 this.switchLatestValue = eventDataMap["value"]
134                                 this.currentSwitch = eventDataMap["value"]
135                                 switchLevels[0].setValue(eventDataMap["value"], "switch")
136                                 sendEvent(eventDataMap)
137                         }
138                 } else if (eventDataMap["name"] == "level") {
139                         if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
140                                 this.level = eventDataMap["value"].toInteger()
141                                 this.rate = eventDataMap["value"].toInteger()
142                                 switchLevels[0].setValue(eventDataMap["value"], "level")
143                                 sendEvent(eventDataMap)
144                         }
145                 }
146         }
147
148         def currentValue(String deviceFeature) {
149                 switchLevels[0].currentValue(deviceFeature)
150         }
151         
152         def latestValue(String deviceFeature) {
153                 switchLevels[0].latestValue(deviceFeature)
154         }
155         
156
157         def getAt(int ix) {
158                 switchLevels[ix]
159         }
160 }