Changes in classes: new concept for latest value + all types of events generated...
[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 String switchState = "off"
18         private String currentSwitch = "off"
19         private String switchLatestValue = "off"
20
21         SwitchLevels(Closure sendEvent, int deviceNumbers) {
22                 this.sendEvent = sendEvent
23                 this.timers = new SimulatedTimer()
24                 this.deviceNumbers = deviceNumbers
25                 this.switchLevels = []
26
27                 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.switchState, this.switchLatestValue))
28         }
29
30         //Methods for closures
31         def count(Closure Input) {
32                 switchLevels.count(Input)
33         }
34         def size() {
35                 switchLevels.size()
36         }
37         def each(Closure Input) {
38                 switchLevels.each(Input)
39         }
40         def find(Closure Input) {
41                 switchLevels.find(Input)
42         }
43         def sort(Closure Input) {
44                 switchLevels.sort(Input)
45         }
46         def collect(Closure Input) {
47                 switchLevels.collect(Input)
48         }
49
50         //By Apps
51         def setLevel(int level) {
52                 if (this.level != level) {
53                         switchLevels[0].setLevel(level)
54                         this.level = level
55                         this.rate = level
56                 }
57         }
58
59         def on() {
60                 switchLatestValue = "on"
61                 switchState = "on"
62                 currentSwitch = "on"
63                 switchLevels[0].on()
64         }
65
66         def on(LinkedHashMap metaData) {
67                 def task = timers.runAfter(metaData["delay"]) {
68                         switchLatestValue = "on"
69                         switchState = "on"
70                         currentSwitch = "on"
71                         switchLevels[0].on()
72                 }
73         }
74
75         def off() {
76                 switchLatestValue = "off"
77                 switchState = "off"
78                 currentSwitch = "off"
79                 switchLevels[0].off()
80         }
81
82         def off(LinkedHashMap metaData) {
83                 def task = timers.runAfter(metaData["delay"]) {
84                         switchLatestValue = "off"
85                         switchState = "off"
86                         currentSwitch = "off"
87                         switchLevels[0].off()
88                 }
89         }
90
91         //By Model Checker
92         def setValue(LinkedHashMap eventDataMap) {
93                 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
94                         this.level = eventDataMap["value"].toInteger()
95                         this.rate = eventDataMap["value"].toInteger()
96                         switchLevels[0].setValue(eventDataMap["value"])
97                         sendEvent(eventDataMap)
98                 }
99         }
100
101         def getAt(int ix) {
102                 switchLevels[ix]
103         }
104 }