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