Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / Switch / Switches.groovy
1 //Create a class for switch device
2 package Switch
3 import Timer.SimulatedTimer
4
5 public class Switches {
6         int deviceNumbers       
7         List switches
8         def timers
9         def sendEvent
10
11         //If we have only one device
12         private String id = "switchID0"
13         private String label = "switch0"
14         private String displayName = "switch0"
15         private String switchState = "off"
16         private String currentSwitch = "off"
17         private int currentLevel = 50
18         private String switchLatestValue = "off"
19
20         Switches(Closure sendEvent, int deviceNumbers) {
21                 this.sendEvent = sendEvent
22                 this.timers = new SimulatedTimer()
23                 this.deviceNumbers = deviceNumbers
24                 this.switches = []
25
26                 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
27         }
28
29         //Methods for closures
30         def count(Closure Input) {
31                 switches.count(Input)
32         }
33         def size() {
34                 switches.size()
35         }
36         def each(Closure Input) {
37                 switches.each(Input)
38         }
39         def find(Closure Input) {
40                 switches.find(Input)
41         }
42         def collect(Closure Input) {
43                 switches.collect(Input)
44         }
45
46         //By Apps
47         def setLevel(int level) {
48                 switches[0].setLevel(level)
49         }
50
51         def on() {
52                 switches[0].on()
53         }
54
55         def on(LinkedHashMap metaData) {
56                 def task = timers.runAfter(metaData["delay"]) {
57                         switches[0].on()
58                 }
59         }
60
61         def off() {
62                 switches[0].off()
63         }
64
65         def off(LinkedHashMap metaData) {
66                 def task = timers.runAfter(metaData["delay"]) {
67                         switches[0].off()
68                 }
69         }
70
71         //By Model Checker
72         def setValue(LinkedHashMap eventDataMap) {
73                 switches[0].setValue(eventDataMap["value"])
74                 this.switchState = switches[0].switchState
75                 this.switchLatestValue = switches[0].switchLatestValue
76                 sendEvent(eventDataMap)
77         }
78
79
80         def currentValue(String deviceFeature) {
81                 switches[0].currentValue(deviceFeature)
82         }
83
84         def latestValue(String deviceFeature) {
85                 switches[0].latestValue(deviceFeature)
86         }
87
88         def getAt(int ix) {
89                 switches[ix]
90         }
91 }