Adding a mapper for 'position' which is basically 'location'
[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, boolean init) {
20                 this.sendEvent = sendEvent
21                 this.timers = new SimulatedTimer()
22                 this.deviceNumbers = deviceNumbers
23                 this.relaySwitches = []
24
25                 if (init) {
26                         this.switchState = "off"
27                         this.currentSwitch = "off"
28                         this.switchLatestValue = "off"
29                 } else {
30                         this.switchState = "on"
31                         this.currentSwitch = "on"
32                         this.switchLatestValue = "on"
33                 }
34                 relaySwitches.add(new RelaySwitch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.switchLatestValue))
35         }
36
37         //Methods for closures
38         def count(Closure Input) {
39                 relaySwitches.count(Input)
40         }
41         def size() {
42                 relaySwitches.size()
43         }
44         def each(Closure Input) {
45                 relaySwitches.each(Input)
46         }
47         def find(Closure Input) {
48                 relaySwitches.find(Input)
49         }
50         def sort(Closure Input) {
51                 relaySwitches.sort(Input)
52         }
53         def collect(Closure Input) {
54                 relaySwitches.collect(Input)
55         }
56
57         //By Apps
58         def on() {
59                 if (switchState != "on") {
60                         switchLatestValue = "on"
61                         switchState = "on"
62                         currentSwitch = "on"
63                         relaySwitches[0].on()
64                 }
65         }
66
67         def on(LinkedHashMap metaData) {
68                 if (switchState != "on") {
69                         def task = timers.runAfter(metaData["delay"]) {
70                                 switchLatestValue = "on"
71                                 switchState = "on"
72                                 currentSwitch = "on"
73                                 relaySwitches[0].on()
74                         }
75                 }
76         }
77
78         def off() {
79                 if (switchState != "off") {
80                         switchLatestValue = "off"
81                         switchState = "off"
82                         currentSwitch = "off"
83                         relaySwitches[0].off()
84                 }
85         }
86
87         def off(LinkedHashMap metaData) {
88                 if (switchState != "off") {
89                         def task = timers.runAfter(metaData["delay"]) {
90                                 switchLatestValue = "off"
91                                 switchState = "off"
92                                 currentSwitch = "off"
93                                 relaySwitches[0].off()
94                         }
95                 }
96         }
97
98         //By Model Checker
99         def setValue(LinkedHashMap eventDataMap) {
100                 if (eventDataMap["value"] != relaySwitches[0].switchState) {
101                         this.switchState = eventDataMap["value"]
102                         this.switchLatestValue = eventDataMap["value"]
103                         relaySwitches[0].setValue(eventDataMap["value"])
104                         sendEvent(eventDataMap)
105                 }
106         }
107
108
109         def currentValue(String deviceFeature) {
110                 relaySwitches[0].currentValue(deviceFeature)
111         }
112
113         def latestValue(String deviceFeature) {
114                 relaySwitches[0].latestValue(deviceFeature)
115         }
116
117         def getAt(int ix) {
118                 relaySwitches[ix]
119         }
120 }