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