Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / ColorControl / ColorControls.groovy
index 74964fe0cf131e6eff15e310f998ed85a6583a9e..6fc62aa14461788cf41218f7f2828ffaa9edd9b6 100644 (file)
@@ -2,9 +2,6 @@
 package ColorControl
 import Timer.SimulatedTimer
 
-//JPF's Verify API
-import gov.nasa.jpf.vm.Verify
-
 public class ColorControls {
        private int deviceNumbers
        private List colorControls
@@ -15,32 +12,35 @@ public class ColorControls {
        private String label = "colorControl0"
        private String displayName = "colorControl0"
        private String color = "red"
-       private String currentSwitch = "off"
+       private String currentColor = "red"
+       private String currentSwitch = "on"
        private int level = 50
+       private int currentLevel = 50
        private int hue = 50
+       private int currentHue = 50
        private int saturation = 50
+       private int currentSaturation = 50
        private int colorTemperature = 15000
        
 
-       ColorControls(Closure sendEvent, int deviceNumbers) {
+       ColorControls(Closure sendEvent, int deviceNumbers, boolean init) {
                this.sendEvent = sendEvent
                this.deviceNumbers = deviceNumbers
                this.colorControls = []
 
-               def initHue = Verify.getIntFromList(30, 50, 70)
-               this.hue = initHue
-               def initSat = Verify.getIntFromList(40, 50, 60)
-               this.saturation = initSat
-               def init = Verify.getInt(0,2)
-               if (init == 0) {
+               if (init) {
+                       this.hue = 30
+                       this.saturation = 40
                        this.color = "red"
-               } else if (init == 1) {
-                       this.color = "green"
+                       this.currentSwitch = "off"
                } else {
+                       this.hue = 50
+                       this.saturation = 50
                        this.color = "blue"
+                       this.currentSwitch = "on"
                }
 
-               colorControls.add(new ColorControl(id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
+               colorControls.add(new ColorControl(sendEvent, id, label, displayName, this.color, this.hue, this.saturation, this.level, this.currentSwitch, this.colorTemperature))
        }
 
        //Methods for closures
@@ -67,6 +67,7 @@ public class ColorControls {
        def setValue(LinkedHashMap eventDataMap) {
                if (eventDataMap["name"] == "color") {
                        if (eventDataMap["value"] != colorControls[0].color) {
+                               this.currentColor = eventDataMap["value"]
                                this.color = eventDataMap["value"]
                                colorControls[0].setValue(eventDataMap["value"], "color")
                                sendEvent(eventDataMap)
@@ -74,12 +75,14 @@ public class ColorControls {
                } else if (eventDataMap["name"] == "hue") {
                        if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
                                this.hue = eventDataMap["value"].toInteger()
+                               this.currentHue = eventDataMap["value"].toInteger()
                                colorControls[0].setValue(eventDataMap["value"], "hue")
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "saturation") {
                        if (eventDataMap["value"].toInteger() != colorControls[0].saturation) {
                                this.saturation = eventDataMap["value"].toInteger()
+                               this.currentSaturation = eventDataMap["value"].toInteger()
                                colorControls[0].setValue(eventDataMap["value"], "saturation")
                                sendEvent(eventDataMap)
                        }
@@ -97,6 +100,7 @@ public class ColorControls {
                        }
                } else if (eventDataMap["name"] == "level") {
                        if (eventDataMap["value"].toInteger() != colorControls[0].level) {
+                               this.currentLevel = eventDataMap["value"].toInteger() 
                                this.level = eventDataMap["value"].toInteger()
                                colorControls[0].setValue(eventDataMap["value"], "level")
                                sendEvent(eventDataMap)
@@ -106,8 +110,44 @@ public class ColorControls {
 
 
        //methods
+       def setColor(LinkedHashMap metaData) {
+               def hexColor = metaData.hex
+               switch (hexColor) {
+                       case "#0000FF":
+                               color = "Blue"
+                               break;
+                       case "#00FF00":
+                               color = "Green"
+                               break;
+                       case "#FFFF00":
+                               color = "Yellow"
+                               break;
+                       case "#FF6000":
+                               color = "Orange"
+                               break;
+                       case "#BF7FBF":
+                               color = "Purple"
+                               break;
+                       case "#FF5F5F":
+                               color = "Pink"
+                               break;
+                       case "#FF0000":
+                               color = "Red"
+                               break;
+                       default:
+                               color = "Blue"
+                               break;
+               }
+               if (color != this.color) {
+                       this.currentColor = color
+                       this.color = color
+                       colorControls[0].setColor(color)                        
+               }
+       }
+
        def setColor(String color) {
                if (color != this.color) {
+                       this.currentColor = color
                        this.color = color
                        colorControls[0].setColor(color)                        
                }
@@ -115,13 +155,15 @@ public class ColorControls {
 
        def setHue(int hue) {
                if (hue != this.hue) {
-                       this.hue = hue  
+                       this.hue = hue
+                       this.currentHue = hue
                        colorControls[0].setHue(hue)
                }
        }
 
        def setSaturation(int saturation) {
                if (saturation != this.saturation) {
+                       this.currentSaturation = saturation
                        this.saturation = saturation
                        colorControls[0].setSaturation(saturation)                      
                }       
@@ -129,29 +171,30 @@ public class ColorControls {
 
        def setLevel(int level) {
                if (level != this.level) {
+                       this.currentLevel = level
                        this.level = level
                        colorControls[0].setLevel(level)
                }
        }
 
-       def setColorTemperature(String colorTemperature) {
+       def setColorTemperature(int colorTemperature) {
                if (colorTemperature != this.colorTemperature) {
                        this.colorTemperature = colorTemperature
                        colorControls[0].setColorTemperature(colorTemperature)                  
                }
        }
 
-       def on(String currentSwitch) {
-               if (currentSwitch != this.currentSwitch) {
-                       this.currentSwitch = currentSwitch
-                       colorControls[0].on(currentSwitch)                      
+       def on() {
+               if (this.currentSwitch != "on") {
+                       this.currentSwitch = "on"
+                       colorControls[0].on()                   
                }
        }
 
-       def off(String currentSwitch) {
-               if (currentSwitch != this.currentSwitch) {
-                       this.currentSwitch = currentSwitch
-                       colorControls[0].off(currentSwitch)                     
+       def off() {
+               if (this.currentSwitch != "off") {
+                       this.currentSwitch = "off"
+                       colorControls[0].off()
                }
        }