Importing verify library for switch class!
[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 find(Closure Input) {
55                 switches.find(Input)
56         }
57         def sort(Closure Input) {
58                 switches.sort(Input)
59         }
60         def collect(Closure Input) {
61                 switches.collect(Input)
62         }
63
64         //By Apps
65         def eventsSince(Date dateObj, LinkedHashMap metaData) {
66                 return switches[0].eventsSince()
67         }
68
69         def setLevel(int level) {
70                 currentLevel = level
71                 switches[0].setLevel(level)
72         }
73
74         def on() {
75                 switchLatestValue = "on"
76                 switchState = "on"
77                 currentSwitch = "on"
78                 switches[0].on()
79         }
80
81         def on(LinkedHashMap metaData) {
82                 def task = timers.runAfter(metaData["delay"]) {
83                         switchLatestValue = "on"
84                         switchState = "on"
85                         currentSwitch = "on"
86                         switches[0].on()
87                 }
88         }
89
90         def off() {
91                 switchLatestValue = "off"
92                 switchState = "off"
93                 currentSwitch = "off"
94                 switches[0].off()
95         }
96
97         def off(LinkedHashMap metaData) {
98                 def task = timers.runAfter(metaData["delay"]) {
99                         switchLatestValue = "off"
100                         switchState = "off"
101                         currentSwitch = "off"
102                         switches[0].off()
103                 }
104         }
105
106         //By Model Checker
107         def setValue(LinkedHashMap eventDataMap) {
108                 if (eventDataMap["value"] != switches[0].switchState) {
109                         this.switchState = eventDataMap["value"]
110                         this.switchLatestValue = eventDataMap["value"]
111                         switches[0].setValue(eventDataMap["value"])
112                         sendEvent(eventDataMap)
113                 }
114         }
115
116
117         def currentValue(String deviceFeature) {
118                 switches[0].currentValue(deviceFeature)
119         }
120
121         def latestValue(String deviceFeature) {
122                 switches[0].latestValue(deviceFeature)
123         }
124
125         def getAt(int ix) {
126                 switches[ix]
127         }
128 }