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