Fixing bug in Mobile Presence class
[smartthings-infrastructure.git] / ColorControl / ColorControl.groovy
index 1c2665c2a6357e04de5a0b8a4f38dd087ad84bc1..4380f24408c0c5534d994ae06440b5625101a79c 100644 (file)
@@ -1,68 +1,84 @@
 //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
+       String id
+       String label
+       String displayName
+       // Maps from features to values
+       HashMap<String, String> deviceValuesMap = new HashMap<String, String>()
+       HashMap<String, Integer> deviceIntValuesMap = new HashMap<String, Integer>()
 
-public class ColorControl {
-       private String id
-       private String label
-       private String displayName
-       private String color
-       private int hue
-       private int saturation
-       
-       ColorControl(String id, String label, String displayName, String color, int hue, int saturation) {
+       ColorControl(Closure sendEvent, String id, String label, String displayName, String currentColor, Integer currentHue, Integer currentSaturation) {
+               deviceValueSmartThing = deviceValuesMap
+               deviceIntValueSmartThing = deviceIntValuesMap
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
                this.displayName = displayName
-               this.color = color
-               this.hue = hue
-               this.saturation = saturation
+
+               deviceValuesMap.put("color", currentColor)
+               deviceIntValuesMap.put("hue", currentHue)
+               deviceIntValuesMap.put("saturation", currentSaturation)
        }
-       
-       //By model checker
-       def setValue(String value, String name) {
-               if (name == "color") {
-                       this.color = value
-                       println("the color of the light is changed to $value!")
-               } else if (name == "hue") {
-                       this.hue = value.toInteger()
-                       println("The hue level of the light is changed to $value!")
-               } else {
-                       this.saturation = value.toInteger()
-                       println("The saturation level of the light is changed to $value!")
+
+       def setColor(LinkedHashMap metaData) {
+               def hexColor = metaData.hex
+               def newColor
+               switch (hexColor) {
+                       case "#0000FF":
+                               newColor = "Blue"
+                               break;
+                       case "#00FF00":
+                               newColor = "Green"
+                               break;
+                       case "#FFFF00":
+                               newColor = "Yellow"
+                               break;
+                       case "#FF6000":
+                               newColor = "Orange"
+                               break;
+                       case "#BF7FBF":
+                               newColor = "Purple"
+                               break;
+                       case "#FF5F5F":
+                               newColor = "Pink"
+                               break;
+                       case "#FF0000":
+                               newColor = "Red"
+                               break;
+                       default:
+                               newColor = "Blue"
+                               break;
                }
+               setColor(newColor)
        }
 
-       //methods
-       def setColor(String 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: [value: "$color"]]) 
+       // Methods to set values
+       def setColor(String newValue) {
+               action(newValue, "color")
        }
 
-       def setHue(int hue) {
-               this.hue = 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: [value: "$hue"]]) 
+       def setHue(int newValue) {
+               action(newValue, "hue")
        }
-
-       def setSaturation(int 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: [value: "$saturation"]]) 
+       
+       def setHue(double newValue) {
+               setHue((int) newValue)
        }
 
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "color") {
-                       return color
-               } else if (deviceFeature == "saturation") {
-                       return saturation
-               } else if (deviceFeature == "hue") {
-                       return hue
-               }
+       def setSaturation(int newValue) {
+               action(newValue, "saturation")
+       }
+       
+       def setSaturation(double newValue) {
+               setSaturation((int) newValue)
        }
 }