Infrastruction modification
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevel.groovy
index ac7895733f874a70fe2af93a47784c10d2e39c68..5d3d5c1de75e2d57c6abbb3e4ce3d628b653c42b 100644 (file)
 //Create a class for switch level
 package SwitchLevel
-import Timer.SimulatedTimer
-
-public class SwitchLevel {
-       private String id
-       private String label
-       private String displayName
-       private String switchState
-       private String currentSwitch
-       private int level
-       private int currentLevel
-       private int rate
-       private int currentRate
-       private int hue
-       private int currentHue
-       private int saturation
-       private int currentSaturation
-       private String switchLatestValue
-       def sendEvent   
-       def timers
+import SmartThing.SmartThing
+
+public class SwitchLevel extends SmartThing {
+       // id, label, and display name of the device
+       StringBuilder id = new StringBuilder()
+       StringBuilder label = new StringBuilder()
+       StringBuilder displayName = new StringBuilder()
+       // Features with numberical values
+       MutableInteger currentLevel = new MutableInteger()
+       // Maps from features to values
+       HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
        
-
-       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()
+       SwitchLevel(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger currentLevel) {
+               deviceIntValuesMap = deviceIntValueSmartThing
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
                this.displayName = displayName
-               this.level = level
-               this.currentLevel = level
-               this.rate = level
-               this.currentRate = level
-               this.hue = hue
-               this.currentHue = hue
-               this.saturation = saturation
-               this.currentSaturation = 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.currentLevel = metaData["level"]
-                       this.rate = metaData["level"]
-                       this.currentRate = metaData["level"]
-                       this.hue = metaData["hue"]
-                       this.currentHue = metaData["hue"]
-                       this.saturation = metaData["saturation"]
-                       this.currentSaturation = 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.currentLevel = level
-                       this.rate = level
-                       this.currentRate = level
-                       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.currentLevel = level
-                       this.rate = level
-                       this.currentRate = level
-                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
-       }
-
-       def on() {
-               if (this.switchState != "on") {
-                       println("the switch with id:$id is on!")
-                       this.switchLatestValue = "on"
-                       this.switchState = "on"
-                       this.currentSwitch = "on"
-                       sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
-       }
-
-       def on(LinkedHashMap metaData) {
-               if (this.switchState != "on") {
-                       def task = timers.runAfter(metaData["delay"]) {
-                               println("the switch with id:$id is on!")
-                               this.switchLatestValue = "on"
-                               this.switchState = "on"
-                               this.currentSwitch = "on"
-                               sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-                       }
-               }
-       }
+               this.currentLevel = currentLevel
 
-       def off() {
-               if (this.switchState != "off") {
-                       println("the switch with id:$id is off!")
-                       this.switchLatestValue = "off"
-                       this.switchState = "off"
-                       this.currentSwitch = "off"
-                       sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               deviceIntValuesMap.put("level", currentLevel)
        }
 
-       def off(LinkedHashMap metaData) {
-               if (this.switchState != "off") {
-                       def task = timers.runAfter(metaData["delay"]) {
-                               println("the switch with id:$id is off!")
-                               this.switchLatestValue = "off"
-                               this.switchState = "off"
-                               this.currentSwitch = "off"
-                               sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-                       }
-               }
+       // Methods to set values
+       def setLevel(String newValue) {
+               setLevel(newValue.toInteger())
        }
 
-       //By Model Checker
-       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.currentLevel = value.toInteger()
-                       this.rate = value.toInteger()
-                       this.currentRate = value.toInteger()
-               }
+       def setLevel(long newValue) {
+               setLevel((int) newValue) {
        }
 
-
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "level") {
-                       return level
-               } else if (deviceFeature == "switch") {
-                       return switchState
+       def setLevel(int newValue) {
+               if (!currentLevel.getValue().equals(newValue)) {
+                       String tmpID = id.toString()
+                       variable.setValue(newValue)
+                       println("the switch with id:$tmpID is setted to level $level!")
+                       sendEvent([name: "level", value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
-
-       def latestValue(String deviceFeature) {
-               if (deviceFeature == "level") {
-                       return level
-               } else if (deviceFeature == "switch") {
-                       return switchState
-               }
+       
+       // Methods to return values
+       def getCurrentLevel() {
+               return currentLevel.getValue()
        }
 }