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