Fixing some bugs
[smartthings-infrastructure.git] / Switch / Switch.groovy
index b924d21d3ec3af148d91211fc041ad8db1c57253..e017b55182ada1f4d49b2a3c976bdbaa788d0f0b 100644 (file)
@@ -26,12 +26,50 @@ public class Switch {
                this.switchLatestValue = switchLatestValue
        }
 
+       def eventsSince() {
+               def evtOn = [[name: "switch", value: "on", deviceId: "switchID0", descriptionText: "",
+                               displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
+               def evtOff = [[name: "switch", value: "off", deviceId: "switchID0", descriptionText: "",
+                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
+               def init = Verify.getInt(0,4)
+               def evtToSend = []
+               if (init == 0) {//return empty set
+                       return evtToSend
+               } else if (init == 1) {//send one open event
+                       evtOn.each{
+                               evtToSend.add(it)
+                       }
+                       return evtToSend
+               } else if (init == 2) {//send two open events
+                       evtOn.each{
+                               evtToSend.add(it)
+                       }
+                       evtOn.each{
+                               evtToSend.add(it)
+                       }
+                       return evtToSend
+               } else if (init == 3) {//send one closed event
+                       evtOff.each{
+                               evtToSend.add(it)
+                       }
+                       return evtToSend
+               } else if (init == 4) {//send two closed events
+                       evtOff.each{
+                               evtToSend.add(it)
+                       }
+                       evtOff.each{
+                               evtToSend.add(it)
+                       }
+                       return evtToSend
+               }
+       }
+
        //By Apps
        def setLevel(int level) {
                if (this.currentLevel != level) {
                        println("the switch with id:$id is setted to level $level!")
                        this.currentLevel = level
-                       sendEvent([name: "level", value: "40", deviceId: this.id, descriptionText: "",
+                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
                            displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
@@ -39,7 +77,7 @@ public class Switch {
        def on() {
                if (this.switchState != "on") {
                        println("the switch with id:$id is on!")
-                       this.switchLatestValue = this.switchState
+                       this.switchLatestValue = "on"
                        this.switchState = "on"
                        this.currentSwitch = "on"
                        sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
@@ -51,7 +89,7 @@ public class Switch {
                if (this.switchState != "on") {
                        def task = timers.runAfter(metaData["delay"]) {
                                println("the switch with id:$id is on!")
-                               this.switchLatestValue = this.switchState
+                               this.switchLatestValue = "on"
                                this.switchState = "on"
                                this.currentSwitch = "on"
                                sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
@@ -63,7 +101,7 @@ public class Switch {
        def off() {
                if (this.switchState != "off") {
                        println("the switch with id:$id is off!")
-                       this.switchLatestValue = this.switchState
+                       this.switchLatestValue = "off"
                        this.switchState = "off"
                        this.currentSwitch = "off"
                        sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
@@ -75,7 +113,7 @@ public class Switch {
                if (this.switchState != "off") {
                        def task = timers.runAfter(metaData["delay"]) {
                                println("the switch with id:$id is off!")
-                               this.switchLatestValue = this.switchState
+                               this.switchLatestValue = "off"
                                this.switchState = "off"
                                this.currentSwitch = "off"
                                sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
@@ -87,7 +125,7 @@ public class Switch {
        //By Model Checker
        def setValue(String value) {
                println("the switch with id:$id is $value!")
-               this.switchLatestValue = this.switchState
+               this.switchLatestValue = value
                this.switchState = value
                this.currentSwitch = value
        }