Adding thermostats run.
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevels.groovy
index 35c4ad9669d21dbeab9a5b4e8b5b2de7c7836c7f..490e52dc66338184f01c8fef8b3a53a3098d3641 100644 (file)
@@ -2,6 +2,9 @@
 package SwitchLevel
 import Timer.SimulatedTimer
 
+//JPF's Verify API
+import gov.nasa.jpf.vm.Verify
+
 public class SwitchLevels {
        int deviceNumbers       
        List switchLevels
@@ -24,6 +27,18 @@ public class SwitchLevels {
                this.deviceNumbers = deviceNumbers
                this.switchLevels = []
 
+               /*def initLevel = Verify.getIntFromList(30, 50, 70)
+               this.level = initLevel
+               def init = Verify.getBoolean()
+               if (init) {
+                       this.switchState = "off"
+                       this.currentSwitch = "off"
+                       this.switchLatestValue = "off"
+               } else {
+                       this.switchState = "on"
+                       this.currentSwitch = "on"
+                       this.switchLatestValue = "on"
+               }*/
                switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.switchState, this.switchLatestValue))
        }
 
@@ -40,6 +55,9 @@ public class SwitchLevels {
        def find(Closure Input) {
                switchLevels.find(Input)
        }
+       def sort(Closure Input) {
+               switchLevels.sort(Input)
+       }
        def collect(Closure Input) {
                switchLevels.collect(Input)
        }
@@ -54,44 +72,54 @@ public class SwitchLevels {
        }
 
        def on() {
-               switchLevels[0].on()
-               switchLatestValue = switchState
+               switchLatestValue = "on"
                switchState = "on"
                currentSwitch = "on"
+               switchLevels[0].on()
        }
 
        def on(LinkedHashMap metaData) {
                def task = timers.runAfter(metaData["delay"]) {
-                       switchLevels[0].on()
-                       switchLatestValue = switchState
+                       switchLatestValue = "on"
                        switchState = "on"
                        currentSwitch = "on"
+                       switchLevels[0].on()
                }
        }
 
        def off() {
-               switchLevels[0].off()
-               switchLatestValue = switchState
+               switchLatestValue = "off"
                switchState = "off"
                currentSwitch = "off"
+               switchLevels[0].off()
        }
 
        def off(LinkedHashMap metaData) {
                def task = timers.runAfter(metaData["delay"]) {
-                       switchLevels[0].off()
-                       switchLatestValue = switchState
+                       switchLatestValue = "off"
                        switchState = "off"
                        currentSwitch = "off"
+                       switchLevels[0].off()
                }
        }
 
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
-               if (eventDataMap["value"] != switchLevels[0].level) {
-                       switchLevels[0].setValue(eventDataMap["value"])
-                       this.level = switchLevels[0].level
-                       this.rate = switchLevels[0].level
-                       sendEvent(eventDataMap)
+               if (eventDataMap["name"] == "switch") {
+                       if (eventDataMap["value"] != switchLevels[0].switchState) {
+                               this.switchState = eventDataMap["value"]
+                               this.switchLatestValue = eventDataMap["value"]
+                               this.currentSwitch = eventDataMap["value"]
+                               switchLevels[0].setValue(eventDataMap["value"], "switch")
+                               sendEvent(eventDataMap)
+                       }
+               } else if (eventDataMap["name"] == "level") {
+                       if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
+                               this.level = eventDataMap["value"].toInteger()
+                               this.rate = eventDataMap["value"].toInteger()
+                               switchLevels[0].setValue(eventDataMap["value"], "level")
+                               sendEvent(eventDataMap)
+                       }
                }
        }