Some minor changes in events' format
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevels.groovy
index 9e6e8a9b73d517d57f0265a5ce644241ee8e3ee3..35c4ad9669d21dbeab9a5b4e8b5b2de7c7836c7f 100644 (file)
@@ -14,6 +14,9 @@ public class SwitchLevels {
        private String displayName = "switchLevel0"
        private int level = 50
        private int rate = 50
+       private String switchState = "off"
+       private String currentSwitch = "off"
+       private String switchLatestValue = "off"
 
        SwitchLevels(Closure sendEvent, int deviceNumbers) {
                this.sendEvent = sendEvent
@@ -21,7 +24,7 @@ public class SwitchLevels {
                this.deviceNumbers = deviceNumbers
                this.switchLevels = []
 
-               switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level))
+               switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.switchState, this.switchLatestValue))
        }
 
        //Methods for closures
@@ -50,6 +53,38 @@ public class SwitchLevels {
                }
        }
 
+       def on() {
+               switchLevels[0].on()
+               switchLatestValue = switchState
+               switchState = "on"
+               currentSwitch = "on"
+       }
+
+       def on(LinkedHashMap metaData) {
+               def task = timers.runAfter(metaData["delay"]) {
+                       switchLevels[0].on()
+                       switchLatestValue = switchState
+                       switchState = "on"
+                       currentSwitch = "on"
+               }
+       }
+
+       def off() {
+               switchLevels[0].off()
+               switchLatestValue = switchState
+               switchState = "off"
+               currentSwitch = "off"
+       }
+
+       def off(LinkedHashMap metaData) {
+               def task = timers.runAfter(metaData["delay"]) {
+                       switchLevels[0].off()
+                       switchLatestValue = switchState
+                       switchState = "off"
+                       currentSwitch = "off"
+               }
+       }
+
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
                if (eventDataMap["value"] != switchLevels[0].level) {