Adding missing methods; setting default value to on.
[smartthings-infrastructure.git] / Switch / Switches.groovy
1 //Create a class for switch device
2 package Switch
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class Switches {
9         int deviceNumbers       
10         List switches
11         def timers
12         def sendEvent
13
14         //If we have only one device
15         private String id = "switchID0"
16         private String label = "switch0"
17         private String displayName = "switch0"
18         private String switchState = "off"
19         private String currentSwitch = "off"
20         private int currentLevel = 50
21         private String switchLatestValue = "off"
22
23         Switches(Closure sendEvent, int deviceNumbers) {
24                 this.sendEvent = sendEvent
25                 this.timers = new SimulatedTimer()
26                 this.deviceNumbers = deviceNumbers
27                 this.switches = []
28
29                 def initLevel = Verify.getIntFromList(30, 50, 70)
30                 this.currentLevel = initLevel
31                 def init = Verify.getBoolean()
32                 /*if (init) {
33                         this.switchState = "off"
34                         this.currentSwitch = "off"
35                         this.switchLatestValue = "off"
36                 } else {
37                         this.switchState = "on"
38                         this.currentSwitch = "on"
39                         this.switchLatestValue = "on"
40                 }*/
41
42                 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
43         }
44
45         //Methods for closures
46         def count(Closure Input) {
47                 switches.count(Input)
48         }
49         def size() {
50                 switches.size()
51         }
52         def each(Closure Input) {
53                 switches.each(Input)
54         }
55         def find(Closure Input) {
56                 switches.find(Input)
57         }
58         def sort(Closure Input) {
59                 switches.sort(Input)
60         }
61         def collect(Closure Input) {
62                 switches.collect(Input)
63         }
64
65         //By Apps
66         def setLevel(int level) {
67                 currentLevel = level
68                 switches[0].setLevel(level)
69         }
70
71         def on() {
72                 switchLatestValue = "on"
73                 switchState = "on"
74                 currentSwitch = "on"
75                 switches[0].on()
76         }
77
78         def on(LinkedHashMap metaData) {
79                 def task = timers.runAfter(metaData["delay"]) {
80                         switchLatestValue = "on"
81                         switchState = "on"
82                         currentSwitch = "on"
83                         switches[0].on()
84                 }
85         }
86
87         def off() {
88                 switchLatestValue = "off"
89                 switchState = "off"
90                 currentSwitch = "off"
91                 switches[0].off()
92         }
93
94         def off(LinkedHashMap metaData) {
95                 def task = timers.runAfter(metaData["delay"]) {
96                         switchLatestValue = "off"
97                         switchState = "off"
98                         currentSwitch = "off"
99                         switches[0].off()
100                 }
101         }
102
103         //By Model Checker
104         def setValue(LinkedHashMap eventDataMap) {
105                 if (eventDataMap["value"] != switches[0].switchState) {
106                         this.switchState = eventDataMap["value"]
107                         this.switchLatestValue = eventDataMap["value"]
108                         switches[0].setValue(eventDataMap["value"])
109                         sendEvent(eventDataMap)
110                 }
111         }
112
113
114         def currentValue(String deviceFeature) {
115                 switches[0].currentValue(deviceFeature)
116         }
117
118         def latestValue(String deviceFeature) {
119                 switches[0].latestValue(deviceFeature)
120         }
121
122         def getAt(int ix) {
123                 switches[ix]
124         }
125 }