1 //Create a class for color control
3 import SmartThing.SmartThing
5 public class ColorControl extends SmartThing {
6 // id, label, and display name of the device
7 StringBuilder id = new StringBuilder()
8 StringBuilder label = new StringBuilder()
9 StringBuilder displayName = new StringBuilder()
10 // Features with numberical values
11 MutableInteger currentHue = new MutableInteger()
12 MutableInteger currentSaturation = new MutableInteger()
13 // Features with string values
14 StringBuilder currentColor = new StringBuilder()
15 // Maps from features to values
16 HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
17 HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
19 ColorControl(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentColor, MutableInteger currentHue, MutableInteger currentSaturation) {
20 deviceValuesMap = deviceValueSmartThing
21 deviceIntValuesMap = deviceIntValueSmartThing
23 labelSmartThing = label
24 displayNameSmartThing = displayName
25 sendEventSmartThings = sendEvent
30 this.displayName = displayName
31 this.currentHue = currentHue
32 this.currentSaturation = currentSaturation
33 this.currentColor = currentColor
35 deviceValuesMap.put("color", currentColor)
36 deviceIntValuesMap.put("hue", currentHue)
37 deviceIntValuesMap.put("saturation", currentSaturation)
40 def setColor(LinkedHashMap metaData) {
41 def hexColor = metaData.hex
72 // Methods to set values
73 def setColor(String newValue) {
74 action(currentColor, newValue, "color")
77 def setHue(int newValue) {
78 action(currentHue, newValue, "hue")
81 def setHue(double newValue) {
82 setHue((int) newValue)
85 def setSaturation(int newValue) {
86 action(currentSaturation, newValue, "saturation")
89 def setSaturation(double newValue) {
90 setSaturation((int) newValue)
93 def action(StringBuilder variable, String newValue, String feature) {
94 if (!variable.toString().equals(newValue)) {
95 String tmpID = id.toString()
96 variable.replace(0, variable.length(), newValue)
97 println("$feature of the light with id:$id is changed to $newValue!")
98 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
99 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
103 def action(MutableInteger variable, int newValue, String feature) {
104 if (!variable.getValue().equals(newValue)) {
105 String tmpID = id.toString()
106 variable.setValue(newValue)
107 println("$feature of the light with id:$id is changed to $newValue!")
108 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
109 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
113 // Methods to return values
114 def getCurrentHue() {
115 return currentHue.getValue()
118 def getCurrentSaturation() {
119 return currentSaturation.getValue()
122 def getCurrentColor() {
123 return currentColor.toString()