Infrastruction modification
[smartthings-infrastructure.git] / ColorTemperature / ColorTemperature.groovy
index 250ac12ea9690c99978e688fe50076cab50c64a0..087ee8a0405565a65b498b07cb5b894d5a156b1c 100644 (file)
 //Create a class for color temperature
 package ColorTemperature
-import Timer.SimulatedTimer
-
-
-public class ColorTemperature {
-       def sendEvent
-       private String id
-       private String label
-       private String displayName
-       private String currentSwitch
-       private int level
-       private int currentLevel
-       private int colorTemperature
-       
-       ColorTemperature(Closure sendEvent, String id, String label, String displayName, int level, String currentSwitch, int colorTemperature) {
+import SmartThing.SmartThing
+
+public class ColorTemperature 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 currentColorTemperature = new MutableInteger()
+       // Maps from features to values
+       HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
+
+       ColorTemperature(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger colorTemperature) {
+               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.currentSwitch = currentSwitch
-               this.colorTemperature = colorTemperature
-               this.sendEvent = sendEvent
-       }
-       
-       //By model checker
-       def setValue(String value, String name) {
-               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!")
-               }
-       }
-
-       //methods
-       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"}'])
-               }
-       }
+               this.currentColorTemperature = currentColorTemperature
 
-       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"}'])
-               }
+               deviceIntValuesMap.put("colorTemperature", currentColorTemperature)
        }
 
-       def on(String currentSwitch) {
-               if (currentSwitch != this.currentSwitch) {
-                       this.currentSwitch = currentSwitch
-                       println("The light is changed to $currentSwitch!")
-                       sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
+       // Methods to set values
+       def setColorTemperature(int newValue) {
+               if (!currentColorTemperature.getValue().equals(newValue)) {
+                       String tmpID = id.toString()
+                       currentColorTemperature.setValue(newValue)
+                       println("The color temperature of the light with id $tmpID is changed to $newValue!")
+                       sendEvent([name: "colorTemperature", value: "$newValue", deviceId: tmpID, descriptionText: "",
                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
-       def off(String currentSwitch) {
-               if (currentSwitch != this.currentSwitch) {
-                       this.currentSwitch = currentSwitch
-                       println("The light is changed to $currentSwitch!")
-                       sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
-                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
-       }
-
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "level") {
-                       return level
-               } else if (deviceFeature == "colorTemperature") {
-                       return colorTemperature
-               } else if (deviceFeature == "switch") {
-                       return currentSwitch
-               }
-       }
-
-       def latestValue(String deviceFeature) {
-               if (deviceFeature == "level") {
-                       return level
-               } else if (deviceFeature == "colorTemperature") {
-                       return colorTemperature
-               } else if (deviceFeature == "switch") {
-                       return currentSwitch
-               }
+       // Methods to return values
+       def getCurrentColorTemperature() {
+               return currentColorTemperature.getValue()
        }
 }