Infrastructure that works for all the locks' group!
[smartthings-infrastructure.git] / Lock / Locks.groovy
index f792c6ccfda41d3f5d36d8ec77ae1436ffc85a7d..ea75ef3e4863611aa4ed92cd989eebd382cf8c96 100644 (file)
@@ -2,6 +2,9 @@
 package Lock
 import Timer.SimulatedTimer
 
+//JPF's Verify API
+import gov.nasa.jpf.vm.Verify
+
 public class Locks{
        int deviceNumbers       
        List locks      
@@ -22,19 +25,20 @@ public class Locks{
                this.deviceNumbers = deviceNumbers
                this.locks = []
 
+               def init = Verify.getBoolean()
+               if (init) {
+                       this.lockState = "locked"
+                       this.lockLatestValue = "locked"
+               } else {
+                       this.lockState = "unlocked"
+                       this.lockLatestValue = "unlocked"
+               }
                locks.add(new Lock(sendEvent,id, label, displayName, this.lockState, this.lockLatestValue))
        }
 
        //By Apps
        def lock() {
-               locks[0].lock()
-               lockLatestValue = lockState
-               lockState = "locked"
-               currentLock = "locked"
-       }
-
-       def lock(LinkedHashMap metaData) {
-               def task = timers.runAfter(metaData["delay"]) {
+               if (lockState != "locked") {
                        locks[0].lock()
                        lockLatestValue = lockState
                        lockState = "locked"
@@ -42,16 +46,19 @@ public class Locks{
                }
        }
 
-       def unlock() {
-               locks[0].unlock()
-               lockLatestValue = lockState
-               lockState = "unlocked"
-               currentLock = "unlocked"
+       def lock(LinkedHashMap metaData) {
+               if (lockState != "locked") {
+                       def task = timers.runAfter(metaData["delay"]) {
+                               locks[0].lock()
+                               lockLatestValue = lockState
+                               lockState = "locked"
+                               currentLock = "locked"
+                       }
+               }
        }
 
-
-       def unlock(LinkedHashMap metaData) {
-               def task = timers.runAfter(metaData["delay"]) {
+       def unlock() {
+               if (lockState != "unlocked") {
                        locks[0].unlock()
                        lockLatestValue = lockState
                        lockState = "unlocked"
@@ -59,6 +66,18 @@ public class Locks{
                }
        }
 
+
+       def unlock(LinkedHashMap metaData) {
+               if (lockState != "unlocked") {
+                       def task = timers.runAfter(metaData["delay"]) {
+                               locks[0].unlock()
+                               lockLatestValue = lockState
+                               lockState = "unlocked"
+                               currentLock = "unlocked"
+                       }
+               }
+       }
+
        //Methods for closures
        def count(Closure Input) {
                locks.count(Input)