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, boolean init) {
21                 this.sendEvent = sendEvent
22                 this.timers = new SimulatedTimer()
23                 this.deviceNumbers = deviceNumbers
24                 this.switches = []
25
26                 if (init) {
27                         this.switchState = "off"
28                         this.currentSwitch = "off"
29                         this.switchLatestValue = "off"
30                         this.currentLevel = 50
31                 } else {
32                         this.switchState = "on"
33                         this.currentSwitch = "on"
34                         this.switchLatestValue = "on"
35                         this.currentLevel = 60
36                 }
37                 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
38         }
39
40         //Methods for closures
41         def count(Closure Input) {
42                 switches.count(Input)
43         }
44         def size() {
45                 switches.size()
46         }
47         def each(Closure Input) {
48                 switches.each(Input)
49         }
50         def find(Closure Input) {
51                 switches.find(Input)
52         }
53         def sort(Closure Input) {
54                 switches.sort(Input)
55         }
56         def collect(Closure Input) {
57                 switches.collect(Input)
58         }
59
60         //By Apps
61         def eventsSince(Date dateObj, LinkedHashMap metaData) {
62                 return switches[0].eventsSince()
63         }
64
65         def setLevel(int level) {
66                 currentLevel = level
67                 switches[0].setLevel(level)
68         }
69
70         def on() {
71                 switchLatestValue = "on"
72                 switchState = "on"
73                 currentSwitch = "on"
74                 switches[0].on()
75         }
76
77         def on(LinkedHashMap metaData) {
78                 def task = timers.runAfter(metaData["delay"]) {
79                         switchLatestValue = "on"
80                         switchState = "on"
81                         currentSwitch = "on"
82                         switches[0].on()
83                 }
84         }
85
86         def off() {
87                 switchLatestValue = "off"
88                 switchState = "off"
89                 currentSwitch = "off"
90                 switches[0].off()
91         }
92
93         def off(LinkedHashMap metaData) {
94                 def task = timers.runAfter(metaData["delay"]) {
95                         switchLatestValue = "off"
96                         switchState = "off"
97                         currentSwitch = "off"
98                         switches[0].off()
99                 }
100         }
101
102         //By Model Checker
103         def setValue(LinkedHashMap eventDataMap) {
104                 if (eventDataMap["value"] != switches[0].switchState) {
105                         this.switchState = eventDataMap["value"]
106                         this.switchLatestValue = eventDataMap["value"]
107                         switches[0].setValue(eventDataMap["value"])
108                         sendEvent(eventDataMap)
109                 }
110         }
111
112
113         def currentValue(String deviceFeature) {
114                 switches[0].currentValue(deviceFeature)
115         }
116
117         def latestValue(String deviceFeature) {
118                 switches[0].latestValue(deviceFeature)
119         }
120
121         def getAt(int ix) {
122                 switches[ix]
123         }
124 }