Infrastruction modification
[smartthings-infrastructure.git] / ColorControl / ColorControl.groovy
index f24d274ad61ed0bf98736a9e1d5679e3378ab099..63cdaf1e1d2fbb17a1e083f1c72953784be7a9c2 100644 (file)
@@ -1,69 +1,42 @@
 //Create a class for color control
 package ColorControl
-import Timer.SimulatedTimer
+import SmartThing.SmartThing
 
+public class ColorControl 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 currentHue = new MutableInteger()
+       MutableInteger currentSaturation = new MutableInteger()
+       // Features with string values
+       StringBuilder currentColor = new StringBuilder()
+       // Maps from features to values
+       HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
+       HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
 
-public class ColorControl {
-       def sendEvent
-       private String id
-       private String label
-       private String displayName
-       private String color
-       private String currentColor
-       private String currentSwitch
-       private int level
-       private int currentLevel
-       private int hue
-       private int currentHue
-       private int saturation
-       private int currentSaturation
-       private int colorTemperature
-       
-       ColorControl(Closure sendEvent, String id, String label, String displayName, String color, int hue, int saturation, int level, String currentSwitch, int colorTemperature) {
+       ColorControl(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentColor, MutableInteger currentHue, MutableInteger currentSaturation) {
+               deviceValuesMap = deviceValueSmartThing
+               deviceIntValuesMap = deviceIntValueSmartThing
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
                this.displayName = displayName
-               this.color = color
-               this.currentColor = color
-               this.hue = hue
-               this.currentHue = hue
-               this.saturation = saturation
-               this.currentSaturation = saturation
-               this.level = level
-               this.currentLevel = level
-               this.currentSwitch = currentSwitch
-               this.colorTemperature = colorTemperature
-               this.sendEvent = sendEvent
-       }
-       
-       //By model checker
-       def setValue(String value, String name) {
-               if ((name == "color") && (value != this.color)) {
-                       this.color = value
-                       this.currentColor = value
-                       println("the color of the light is changed to $value!")
-               } else if ((name == "hue") && (value != this.hue)) {
-                       this.hue = value.toInteger()
-                       this.currentHue = value.toInteger()
-                       println("The hue level of the light is changed to $value!")
-               } else if ((name == "saturation") && (value != this.saturation)) {
-                       this.saturation = value.toInteger()
-                       this.currentSaturation = value.toInteger()
-                       println("The saturation level of the light is changed to $value!")
-               } else if ((name == "level") && (value != this.level)) {
-                       this.currentLevel = value.toInteger()
-                       this.level = value.toInteger()
-                       println("The level of the light is changed to $value!")
-               } else if ((name == "currentSwitch") && (value != this.currentSwitch)) {
-                       this.currentSwitch = value
-                       println("The light is changed to $value!")
-               } else if ((name == "colorTemperature") && (value != this.colorTemperature)) {
-                       this.colorTemperature = value.toInteger()
-                       println("The color temperature level of the light is changed to $value!")
-               }
+               this.currentHue = currentHue
+               this.currentSaturation = currentSaturation
+               this.currentColor = currentColor
+
+               deviceValuesMap.put("color", currentColor)
+               deviceIntValuesMap.put("hue", currentHue)
+               deviceIntValuesMap.put("saturation", currentSaturation)
        }
 
-       //methods
        def setColor(LinkedHashMap metaData) {
                def hexColor = metaData.hex
                def newColor
@@ -93,142 +66,60 @@ public class ColorControl {
                                newColor = "Blue"
                                break;
                }
-               if (newColor != this.color) {
-                       this.currentColor = newColor
-                       this.color = newColor
-                       println("The color of the light is changed to $newColor!")
-                       sendEvent([name: "color", value: "$newColor", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               setColor(newColor)
        }
 
-       def setColor(String color) {
-               if (color != this.color) {
-                       this.currentColor = color
-                       this.color = color
-                       println("The color of the light is changed to $color!")
-                       sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+       // Methods to set values
+       def setColor(String newValue) {
+               action(currentColor, newValue, "color")
        }
 
-       def setHue(int hue) {
-               if (hue != this.hue) {
-                       this.hue = hue
-                       this.currentHue = hue
-                       println("The hue level of the light is changed to $hue!")
-                       sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+       def setHue(int newValue) {
+               action(currentHue, newValue, "hue")
        }
        
-       def setHue(double hue) {
-               if (hue != this.hue) {
-                       this.hue = hue
-                       this.currentHue = hue
-                       println("The hue level of the light is changed to $hue!")
-                       sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+       def setHue(double newValue) {
+               setHue((int) newValue)
        }
 
-       def setSaturation(int saturation) {
-               if (saturation != this.saturation) {
-                       this.currentSaturation = saturation
-                       this.saturation = saturation
-                       println("The saturation level of the light is changed to $saturation!")
-                       sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+       def setSaturation(int newValue) {
+               action(currentSaturation, newValue, "saturation")
        }
        
-       def setSaturation(double saturation) {
-               if (saturation != this.saturation) {
-                       this.currentSaturation = saturation
-                       this.saturation = saturation
-                       println("The saturation level of the light is changed to $saturation!")
-                       sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+       def setSaturation(double newValue) {
+               setSaturation((int) newValue)
        }
 
-
-       def setLevel(int level) {
-               if (level != this.level) {
-                       this.currentLevel = level
-                       this.level = level
-                       println("The level of the light is changed to $level!")
-                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
-       }
-
-       def setLevel(long level) {
-               if (level != this.level) {
-                       this.currentLevel = level
-                       this.level = level
-                       println("The level of the light is changed to $level!")
-                       sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
-       }
-
-       def setColorTemperature(int colorTemperature) {
-               if (colorTemperature != this.colorTemperature) {
-                       this.colorTemperature = colorTemperature
-                       println("The color temperature level of the light is changed to $colorTemperature!")
-                       sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+       def action(StringBuilder variable, String newValue, String feature) {
+               if (!variable.toString().equals(newValue)) {
+                       String tmpID = id.toString()
+                       variable.replace(0, variable.length(), newValue)
+                       println("$feature of the light with id:$id is changed to $newValue!")
+                       sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
-       def on() {
-               if (this.currentSwitch != "on") {
-                       this.currentSwitch = currentSwitch
-                       println("The light is changed to on!")
-                       sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+       def action(MutableInteger variable, int newValue, String feature) {
+               if (!variable.getValue().equals(newValue)) {
+                       String tmpID = id.toString()
+                       variable.setValue(newValue)
+                       println("$feature of the light with id:$id is changed to $newValue!")
+                       sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
-       def off() {
-               if (this.currentSwitch != "off") {
-                       this.currentSwitch = currentSwitch
-                       println("The light is changed to off!")
-                       sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+       // Methods to return values
+       def getCurrentHue() {
+               return currentHue.getValue()
        }
 
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "color") {
-                       return color
-               } else if (deviceFeature == "saturation") {
-                       return saturation
-               } else if (deviceFeature == "hue") {
-                       return hue
-               } else if (deviceFeature == "level") {
-                       return level
-               } else if (deviceFeature == "colorTemperature") {
-                       return colorTemperature
-               } else if (deviceFeature == "switch") {
-                       return currentSwitch
-               }
+       def getCurrentSaturation() {
+               return currentSaturation.getValue()
        }
-
-       def latestValue(String deviceFeature) {
-               if (deviceFeature == "color") {
-                       return color
-               } else if (deviceFeature == "saturation") {
-                       return saturation
-               } else if (deviceFeature == "hue") {
-                       return hue
-               } else if (deviceFeature == "level") {
-                       return level
-               } else if (deviceFeature == "colorTemperature") {
-                       return colorTemperature
-               } else if (deviceFeature == "switch") {
-                       return currentSwitch
-               }
+       
+       def getCurrentColor() {
+               return currentColor.toString()
        }
 }