//Create a class for color control package ColorControl import SmartThing.SmartThing //Importing mutable integer class import MutableInteger.MutableInteger public class ColorControl 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 currentHue = new MutableInteger() MutableInteger currentSaturation = new MutableInteger() // Features with string values StringBuilder currentColor = new StringBuilder() // Maps from features to values HashMap deviceValuesMap = new HashMap() HashMap deviceIntValuesMap = new HashMap() ColorControl(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentColor, MutableInteger currentHue, MutableInteger currentSaturation) { deviceValuesMap = deviceValueSmartThing deviceIntValuesMap = deviceIntValueSmartThing idSmartThing = id labelSmartThing = label displayNameSmartThing = displayName sendEventSmartThings = sendEvent // Initialization this.id = id this.label = label this.displayName = displayName this.currentHue = currentHue this.currentSaturation = currentSaturation this.currentColor = currentColor deviceValuesMap.put("color", currentColor) deviceIntValuesMap.put("hue", currentHue) deviceIntValuesMap.put("saturation", currentSaturation) } 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 to set values def setColor(String newValue) { action(currentColor, newValue, "color") } def setHue(int newValue) { action(currentHue, newValue, "hue") } def setHue(double newValue) { setHue((int) newValue) } def setSaturation(int newValue) { action(currentSaturation, newValue, "saturation") } def setSaturation(double newValue) { setSaturation((int) newValue) } def action(StringBuilder variable, String newValue, String feature) { if (!variable.toString().equals(newValue)) { String tmpID = id.toString() variable.replace(0, variable.length(), newValue) println("$feature of the light with id:$id is changed to $newValue!") sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) } } def action(MutableInteger variable, int newValue, String feature) { if (!variable.getValue().equals(newValue)) { String tmpID = id.toString() variable.setValue(newValue) println("$feature of the light with id:$id is changed to $newValue!") sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) } } // Methods to return values def getCurrentHue() { return currentHue.getValue() } def getCurrentSaturation() { return currentSaturation.getValue() } def getCurrentColor() { return currentColor.toString() } }