1 //Create a class for color control
3 import SmartThing.SmartThing
5 //Importing mutable integer class
6 import MutableInteger.MutableInteger
8 public class ColorControl extends SmartThing {
9 // id, label, and display name of the device
10 StringBuilder id = new StringBuilder()
11 StringBuilder label = new StringBuilder()
12 StringBuilder displayName = new StringBuilder()
13 // Features with numberical values
14 MutableInteger currentHue = new MutableInteger()
15 MutableInteger currentSaturation = new MutableInteger()
16 // Features with string values
17 StringBuilder currentColor = new StringBuilder()
18 // Maps from features to values
19 HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
20 HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
22 ColorControl(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentColor, MutableInteger currentHue, MutableInteger currentSaturation) {
23 deviceValuesMap = deviceValueSmartThing
24 deviceIntValuesMap = deviceIntValueSmartThing
26 labelSmartThing = label
27 displayNameSmartThing = displayName
28 sendEventSmartThings = sendEvent
33 this.displayName = displayName
34 this.currentHue = currentHue
35 this.currentSaturation = currentSaturation
36 this.currentColor = currentColor
38 deviceValuesMap.put("color", currentColor)
39 deviceIntValuesMap.put("hue", currentHue)
40 deviceIntValuesMap.put("saturation", currentSaturation)
43 def setColor(LinkedHashMap metaData) {
44 def hexColor = metaData.hex
75 // Methods to set values
76 def setColor(String newValue) {
77 action(currentColor, newValue, "color")
80 def setHue(int newValue) {
81 action(currentHue, newValue, "hue")
84 def setHue(double newValue) {
85 setHue((int) newValue)
88 def setSaturation(int newValue) {
89 action(currentSaturation, newValue, "saturation")
92 def setSaturation(double newValue) {
93 setSaturation((int) newValue)
96 def action(StringBuilder variable, String newValue, String feature) {
97 if (!variable.toString().equals(newValue)) {
98 String tmpID = id.toString()
99 variable.replace(0, variable.length(), newValue)
100 println("$feature of the light with id:$id is changed to $newValue!")
101 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
102 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
106 def action(MutableInteger variable, int newValue, String feature) {
107 if (!variable.getValue().equals(newValue)) {
108 String tmpID = id.toString()
109 variable.setValue(newValue)
110 println("$feature of the light with id:$id is changed to $newValue!")
111 sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
112 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
116 // Methods to return values
117 def getCurrentHue() {
118 return currentHue.getValue()
121 def getCurrentSaturation() {
122 return currentSaturation.getValue()
125 def getCurrentColor() {
126 return currentColor.toString()