Changes in classes: new concept for latest value + all types of events generated...
[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 sort(Closure Input) {
42                 relaySwitches.sort(Input)
43         }
44         def collect(Closure Input) {
45                 relaySwitches.collect(Input)
46         }
47
48         //By Apps
49         def on() {
50                 if (switchState != "on") {
51                         switchLatestValue = "on"
52                         switchState = "on"
53                         currentSwitch = "on"
54                         relaySwitches[0].on()
55                 }
56         }
57
58         def on(LinkedHashMap metaData) {
59                 if (switchState != "on") {
60                         def task = timers.runAfter(metaData["delay"]) {
61                                 switchLatestValue = "on"
62                                 switchState = "on"
63                                 currentSwitch = "on"
64                                 relaySwitches[0].on()
65                         }
66                 }
67         }
68
69         def off() {
70                 if (switchState != "off") {
71                         switchLatestValue = "off"
72                         switchState = "off"
73                         currentSwitch = "off"
74                         relaySwitches[0].off()
75                 }
76         }
77
78         def off(LinkedHashMap metaData) {
79                 if (switchState != "off") {
80                         def task = timers.runAfter(metaData["delay"]) {
81                                 switchLatestValue = "off"
82                                 switchState = "off"
83                                 currentSwitch = "off"
84                                 relaySwitches[0].off()
85                         }
86                 }
87         }
88
89         //By Model Checker
90         def setValue(LinkedHashMap eventDataMap) {
91                 if (eventDataMap["value"] != relaySwitches[0].switchState) {
92                         this.switchState = eventDataMap["value"]
93                         this.switchLatestValue = eventDataMap["value"]
94                         relaySwitches[0].setValue(eventDataMap["value"])
95                         sendEvent(eventDataMap)
96                 }
97         }
98
99
100         def currentValue(String deviceFeature) {
101                 relaySwitches[0].currentValue(deviceFeature)
102         }
103
104         def latestValue(String deviceFeature) {
105                 relaySwitches[0].latestValue(deviceFeature)
106         }
107
108         def getAt(int ix) {
109                 relaySwitches[ix]
110         }
111 }