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                 currentLevel = level
50         }
51
52         def on() {
53                 switches[0].on()
54                 switchLatestValue = switchState
55                 switchState = "on"
56                 currentSwitch = "on"
57         }
58
59         def on(LinkedHashMap metaData) {
60                 def task = timers.runAfter(metaData["delay"]) {
61                         switches[0].on()
62                         switchLatestValue = switchState
63                         switchState = "on"
64                         currentSwitch = "on"
65                 }
66         }
67
68         def off() {
69                 switches[0].off()
70                 switchLatestValue = switchState
71                 switchState = "off"
72                 currentSwitch = "off"
73         }
74
75         def off(LinkedHashMap metaData) {
76                 def task = timers.runAfter(metaData["delay"]) {
77                         switches[0].off()
78                         switchLatestValue = switchState
79                         switchState = "off"
80                         currentSwitch = "off"
81                 }
82         }
83
84         //By Model Checker
85         def setValue(LinkedHashMap eventDataMap) {
86                 if (eventDataMap["value"] != switches[0].switchState) {
87                         switches[0].setValue(eventDataMap["value"])
88                         this.switchState = switches[0].switchState
89                         this.switchLatestValue = switches[0].switchLatestValue
90                         sendEvent(eventDataMap)
91                 }
92         }
93
94
95         def currentValue(String deviceFeature) {
96                 switches[0].currentValue(deviceFeature)
97         }
98
99         def latestValue(String deviceFeature) {
100                 switches[0].latestValue(deviceFeature)
101         }
102
103         def getAt(int ix) {
104                 switches[ix]
105         }
106 }