Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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 collect(Closure Input) {
44                 switchLevels.collect(Input)
45         }
46
47         //By Apps
48         def setLevel(int level) {
49                 if (this.level != level) {
50                         switchLevels[0].setLevel(level)
51                         this.level = level
52                         this.rate = level
53                 }
54         }
55
56         def on() {
57                 switchLevels[0].on()
58                 switchLatestValue = switchState
59                 switchState = "on"
60                 currentSwitch = "on"
61         }
62
63         def on(LinkedHashMap metaData) {
64                 def task = timers.runAfter(metaData["delay"]) {
65                         switchLevels[0].on()
66                         switchLatestValue = switchState
67                         switchState = "on"
68                         currentSwitch = "on"
69                 }
70         }
71
72         def off() {
73                 switchLevels[0].off()
74                 switchLatestValue = switchState
75                 switchState = "off"
76                 currentSwitch = "off"
77         }
78
79         def off(LinkedHashMap metaData) {
80                 def task = timers.runAfter(metaData["delay"]) {
81                         switchLevels[0].off()
82                         switchLatestValue = switchState
83                         switchState = "off"
84                         currentSwitch = "off"
85                 }
86         }
87
88         //By Model Checker
89         def setValue(LinkedHashMap eventDataMap) {
90                 if (eventDataMap["value"] != switchLevels[0].level) {
91                         switchLevels[0].setValue(eventDataMap["value"])
92                         this.level = switchLevels[0].level
93                         this.rate = switchLevels[0].level
94                         sendEvent(eventDataMap)
95                 }
96         }
97
98         def getAt(int ix) {
99                 switchLevels[ix]
100         }
101 }