Add a method to SwitchLevel class!
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevel.groovy
index 4b7acbf9d7ce495ffb2957c7f9e33a48179e0209..7db6a168d8cecdbe1ab33404aa6ab33b1e54ec08 100644 (file)
@@ -10,12 +10,14 @@ public class SwitchLevel {
        private String currentSwitch
        private int level
        private int rate
+       private int hue
+       private int saturation
        private String switchLatestValue
        def sendEvent   
        def timers
        
 
-       SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, String switchState, String switchLatestValue) {
+       SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, int hue, int saturation, String switchState, String switchLatestValue) {
                this.sendEvent = sendEvent
                this.timers = new SimulatedTimer()
                this.id = id
@@ -23,18 +25,51 @@ public class SwitchLevel {
                this.displayName = displayName
                this.level = level
                this.rate = level
+               this.hue = hue
+               this.saturation = saturation
                this.switchState = switchState
                this.currentSwitch = switchState
                this.switchLatestValue = switchLatestValue
        }
 
        //By Apps
+       def setColor(LinkedHashMap metaData) {
+               if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
+                       this.level = metaData["level"]
+                       this.rate = metaData["level"]
+                       this.hue = metaData["hue"]
+                       this.saturation = metaData["saturation"]
+                       println("the switch with id:$id is setted to level $level and hue to $hue and saturation to $saturation!")
+                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+                       sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+
+       def setLevel(String level) {
+               def newLevel = level.toInteger()
+               setLevel(newLevel)
+       }       
+
        def setLevel(int level) {
                if (this.level != level) {
                        println("the switch with id:$id is setted to level $level!")
                        this.level = level
                        this.rate = level
-                       sendEvent([name: "level", value: "50", deviceId: this.id, descriptionText: "",
+                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+       
+       def setLevel(long level) {
+               if (this.level != level) {
+                       println("the switch with id:$id is setted to level $level!")
+                       this.level = level
+                       this.rate = level
+                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
@@ -42,7 +77,7 @@ public class SwitchLevel {
        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: "",
@@ -54,7 +89,7 @@ public class SwitchLevel {
                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: "",
@@ -66,7 +101,7 @@ public class SwitchLevel {
        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: "",
@@ -78,7 +113,7 @@ public class SwitchLevel {
                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: "",
@@ -88,9 +123,33 @@ public class SwitchLevel {
        }
 
        //By Model Checker
-       def setValue(String value) {
-               println("the switch with id:$id is setted to level $value!")
-               this.level = value.toInteger()
-               this.rate = value.toInteger()
+       def setValue(String value, String name) {
+               if (name == "switch") {
+                       println("the switch with id:$id is $value!")
+                       this.switchLatestValue = value
+                       this.switchState = value
+                       this.currentSwitch = value
+               } else if (name == "level") {
+                       println("the switch with id:$id is setted to level $value!")
+                       this.level = value.toInteger()
+                       this.rate = value.toInteger()
+               }
+       }
+
+
+       def currentValue(String deviceFeature) {
+               if (deviceFeature == "level") {
+                       return level
+               } else if (deviceFeature == "switch") {
+                       return switchState
+               }
+       }
+
+       def latestValue(String deviceFeature) {
+               if (deviceFeature == "level") {
+                       return level
+               } else if (deviceFeature == "switch") {
+                       return switchState
+               }
        }
 }