Fixing bugs in infrastructure
[smartthings-infrastructure.git] / ColorControl / ColorControls.groovy
index 74964fe0cf131e6eff15e310f998ed85a6583a9e..d17f9e4333f94e02c8fb85fb361b0b0bee28afea 100644 (file)
 //Create a class for color control
 package ColorControl
-import Timer.SimulatedTimer
+import SmartThing.SmartThings
 
-//JPF's Verify API
-import gov.nasa.jpf.vm.Verify
-
-public class ColorControls {
-       private int deviceNumbers
-       private List colorControls
-       def sendEvent
-
-       //For one device(We cannot have obj.id)-> We should have obj[0].id
-       private String id = "colorControlID0"
-       private String label = "colorControl0"
-       private String displayName = "colorControl0"
-       private String color = "red"
-       private String currentSwitch = "off"
-       private int level = 50
-       private int hue = 50
-       private int saturation = 50
-       private int colorTemperature = 15000
+public class ColorControls extends SmartThings {
+       List colorControls = new ArrayList()
        
-
-       ColorControls(Closure sendEvent, int deviceNumbers) {
-               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) {
-                       this.color = "red"
-               } else if (init == 1) {
-                       this.color = "green"
-               } else {
-                       this.color = "blue"
+       ColorControls(Closure sendEvent, boolean init) {
+               // Only initialize one time since we only have one device for each capability
+               colorControls = smartThings
+
+               // Initialization
+               String id = "colorControlID0"
+               String label = "colorControl"
+               String displayName = "light"
+               String color
+               Integer hue
+               Integer saturation
+
+               if (init) {
+                       color = "Red"
+                       hue = 30
+                       saturation = 40
+               } else {                
+                       color = "Blue"
+                       hue = 50
+                       saturation = 50
                }
 
-               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, color, hue,
+                                                  saturation))
        }
 
-       //Methods for closures
-       def count(Closure Input) {
-               colorControls.count(Input)
-       }
-       def size() {
-               colorControls.size()
-       }
-       def each(Closure Input) {
-               colorControls.each(Input)
-       }
-       def find(Closure Input) {
-               colorControls.find(Input)
-       }
-       def sort(Closure Input) {
-               colorControls.sort(Input)
-       }
-       def collect(Closure Input) {
-               colorControls.collect(Input)
-       }
-
-       //By model checker
-       def setValue(LinkedHashMap eventDataMap) {
-               if (eventDataMap["name"] == "color") {
-                       if (eventDataMap["value"] != colorControls[0].color) {
-                               this.color = eventDataMap["value"]
-                               colorControls[0].setValue(eventDataMap["value"], "color")
-                               sendEvent(eventDataMap)
-                       }       
-               } else if (eventDataMap["name"] == "hue") {
-                       if (eventDataMap["value"].toInteger() != colorControls[0].hue) {
-                               this.hue = 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()
-                               colorControls[0].setValue(eventDataMap["value"], "saturation")
-                               sendEvent(eventDataMap)
-                       }
-               } else if (eventDataMap["name"] == "switch") {
-                       if (eventDataMap["value"] != colorControls[0].currentSwitch) {
-                               this.currentSwitch = eventDataMap["value"]
-                               colorControls[0].setValue(eventDataMap["value"], "switch")
-                               sendEvent(eventDataMap)
-                       }
-               } else if (eventDataMap["name"] == "colorTemperature") {
-                       if (eventDataMap["value"].toInteger() != colorControls[0].colorTemperature) {
-                               this.colorTemperature = eventDataMap["value"].toInteger()
-                               colorControls[0].setValue(eventDataMap["value"], "colorTemperature")
-                               sendEvent(eventDataMap)
-                       }
-               } else if (eventDataMap["name"] == "level") {
-                       if (eventDataMap["value"].toInteger() != colorControls[0].level) {
-                               this.level = eventDataMap["value"].toInteger()
-                               colorControls[0].setValue(eventDataMap["value"], "level")
-                               sendEvent(eventDataMap)
-                       }
-               }
+       // Methods to set values
+       def setColor(LinkedHashMap metaData) {
+               colorControls[0].setColor(metaData)
        }
 
-
-       //methods
        def setColor(String color) {
-               if (color != this.color) {
-                       this.color = color
-                       colorControls[0].setColor(color)                        
-               }
+               colorControls[0].setColor(color)
        }
 
        def setHue(int hue) {
-               if (hue != this.hue) {
-                       this.hue = hue  
-                       colorControls[0].setHue(hue)
-               }
+               colorControls[0].setHue(hue)
        }
-
-       def setSaturation(int saturation) {
-               if (saturation != this.saturation) {
-                       this.saturation = saturation
-                       colorControls[0].setSaturation(saturation)                      
-               }       
-       }
-
-       def setLevel(int level) {
-               if (level != this.level) {
-                       this.level = level
-                       colorControls[0].setLevel(level)
-               }
-       }
-
-       def setColorTemperature(String colorTemperature) {
-               if (colorTemperature != this.colorTemperature) {
-                       this.colorTemperature = colorTemperature
-                       colorControls[0].setColorTemperature(colorTemperature)                  
-               }
+       
+       def setHue(double hue) {
+               colorControls[0].setHue((int) hue)
        }
 
-       def on(String currentSwitch) {
-               if (currentSwitch != this.currentSwitch) {
-                       this.currentSwitch = currentSwitch
-                       colorControls[0].on(currentSwitch)                      
-               }
+       def setSaturation(int saturation) {
+               colorControls[0].setSaturation(saturation)      
        }
-
-       def off(String currentSwitch) {
-               if (currentSwitch != this.currentSwitch) {
-                       this.currentSwitch = currentSwitch
-                       colorControls[0].off(currentSwitch)                     
-               }
+       
+       def setSaturation(double saturation) {
+               colorControls[0].setSaturation((int) saturation)
        }
 
-       def currentValue(String deviceFeature) {
-               colorControls[0].currentValue(deviceFeature)
+       def on() {
+               colorControls[0].on()
        }
 
-       def latestValue(String deviceFeature) {
-               colorControls[0].latestValue(deviceFeature)
-       }       
-
-       def getAt(int ix) {
-               colorControls[ix]
+       def off() {
+               colorControls[0].off()
        }
 }