//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 String currentSwitch
- private int level
- private int hue
- private int saturation
- private int colorTemperature
-
- ColorControl(String id, String label, String displayName, String color, int hue, int saturation, int level, String currentSwitch, int colorTemperature) {
+ 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
- this.level = level
- this.currentSwitch = currentSwitch
- this.colorTemperature = colorTemperature
- }
-
- //By model checker
- def setValue(String value, String name) {
- if ((name == "color") && (value != this.color)) {
- this.color = value
- println("the color of the light is changed to $value!")
- } else if ((name == "hue") && (value != this.hue)) {
- this.hue = value.toInteger()
- println("The hue level of the light is changed to $value!")
- } else if ((name == "saturation") && (value != this.saturation)) {
- this.saturation = value.toInteger()
- println("The saturation level of the light is changed to $value!")
- } else if ((name == "level") && (value != this.level)) {
- 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 setColor(String color) {
- if (color != this.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"}'])
- }
- }
-
- def setHue(int hue) {
- if (hue != this.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: '{"info": "info"}'])
- }
- }
- def setSaturation(int saturation) {
- if (saturation != this.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"}'])
- }
+ deviceValuesMap.put("color", currentColor)
+ deviceIntValuesMap.put("hue", currentHue)
+ deviceIntValuesMap.put("saturation", currentSaturation)
}
- def setLevel(int level) {
- if (level != this.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 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)
}
- 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"}'])
- }
+ // Methods to set values
+ def setColor(String newValue) {
+ action(newValue, "color")
}
- 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: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- sendEvent([name: "switch.on", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
+ def setHue(int newValue) {
+ action(newValue, "hue")
}
-
- 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"}'])
- sendEvent([name: "switch.off", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
- displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
- }
+
+ 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
- } else if (deviceFeature == "level") {
- return level
- } else if (deviceFeature == "colorTemperature") {
- return colorTemperature
- } else if (deviceFeature == "switch") {
- return currentSwitch
- }
+ def setSaturation(int newValue) {
+ action(newValue, "saturation")
}
-
- 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 setSaturation(double newValue) {
+ setSaturation((int) newValue)
}
}