Infrastructure that works for all the locks' group!
[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
18         SwitchLevels(Closure sendEvent, int deviceNumbers) {
19                 this.sendEvent = sendEvent
20                 this.timers = new SimulatedTimer()
21                 this.deviceNumbers = deviceNumbers
22                 this.switchLevels = []
23
24                 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level))
25         }
26
27         //Methods for closures
28         def count(Closure Input) {
29                 switchLevels.count(Input)
30         }
31         def size() {
32                 switchLevels.size()
33         }
34         def each(Closure Input) {
35                 switchLevels.each(Input)
36         }
37         def find(Closure Input) {
38                 switchLevels.find(Input)
39         }
40         def collect(Closure Input) {
41                 switchLevels.collect(Input)
42         }
43
44         //By Apps
45         def setLevel(int level) {
46                 if (this.level != level) {
47                         switchLevels[0].setLevel(level)
48                         this.level = level
49                         this.rate = level
50                 }
51         }
52
53         //By Model Checker
54         def setValue(LinkedHashMap eventDataMap) {
55                 if (eventDataMap["value"] != switchLevels[0].level) {
56                         switchLevels[0].setValue(eventDataMap["value"])
57                         this.level = switchLevels[0].level
58                         this.rate = switchLevels[0].level
59                         sendEvent(eventDataMap)
60                 }
61         }
62
63         def getAt(int ix) {
64                 switchLevels[ix]
65         }
66 }