Adding a mapper for 'position' which is basically 'location'
[smartthings-infrastructure.git] / RelaySwitch / RelaySwitches.groovy
index 07e1c305b7a4a28e2f94dcfef08d22bb03e59df1..fb5977b69dbb704303182e410ccc3e1456c5689e 100644 (file)
@@ -2,9 +2,6 @@
 package RelaySwitch
 import Timer.SimulatedTimer
 
-//JPF's Verify API
-import gov.nasa.jpf.vm.Verify
-
 public class RelaySwitches {
        int deviceNumbers       
        List relaySwitches
@@ -19,13 +16,12 @@ public class RelaySwitches {
        private String currentSwitch = "off"
        private String switchLatestValue = "off"
 
-       RelaySwitches(Closure sendEvent, int deviceNumbers) {
+       RelaySwitches(Closure sendEvent, int deviceNumbers, boolean init) {
                this.sendEvent = sendEvent
                this.timers = new SimulatedTimer()
                this.deviceNumbers = deviceNumbers
                this.relaySwitches = []
 
-               def init = Verify.getBoolean()
                if (init) {
                        this.switchState = "off"
                        this.currentSwitch = "off"
@@ -51,6 +47,9 @@ public class RelaySwitches {
        def find(Closure Input) {
                relaySwitches.find(Input)
        }
+       def sort(Closure Input) {
+               relaySwitches.sort(Input)
+       }
        def collect(Closure Input) {
                relaySwitches.collect(Input)
        }
@@ -58,40 +57,40 @@ public class RelaySwitches {
        //By Apps
        def on() {
                if (switchState != "on") {
-                       relaySwitches[0].on()
-                       switchLatestValue = switchState
+                       switchLatestValue = "on"
                        switchState = "on"
                        currentSwitch = "on"
+                       relaySwitches[0].on()
                }
        }
 
        def on(LinkedHashMap metaData) {
                if (switchState != "on") {
                        def task = timers.runAfter(metaData["delay"]) {
-                               relaySwitches[0].on()
-                               switchLatestValue = switchState
+                               switchLatestValue = "on"
                                switchState = "on"
                                currentSwitch = "on"
+                               relaySwitches[0].on()
                        }
                }
        }
 
        def off() {
                if (switchState != "off") {
-                       relaySwitches[0].off()
-                       switchLatestValue = switchState
+                       switchLatestValue = "off"
                        switchState = "off"
                        currentSwitch = "off"
+                       relaySwitches[0].off()
                }
        }
 
        def off(LinkedHashMap metaData) {
                if (switchState != "off") {
                        def task = timers.runAfter(metaData["delay"]) {
-                               relaySwitches[0].off()
-                               switchLatestValue = switchState
+                               switchLatestValue = "off"
                                switchState = "off"
                                currentSwitch = "off"
+                               relaySwitches[0].off()
                        }
                }
        }
@@ -99,9 +98,9 @@ public class RelaySwitches {
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
                if (eventDataMap["value"] != relaySwitches[0].switchState) {
+                       this.switchState = eventDataMap["value"]
+                       this.switchLatestValue = eventDataMap["value"]
                        relaySwitches[0].setValue(eventDataMap["value"])
-                       this.switchState = relaySwitches[0].switchState
-                       this.switchLatestValue = relaySwitches[0].switchLatestValue
                        sendEvent(eventDataMap)
                }
        }