63cdaf1e1d2fbb17a1e083f1c72953784be7a9c2
[smartthings-infrastructure.git] / ColorControl / ColorControl.groovy
1 //Create a class for color control
2 package ColorControl
3 import SmartThing.SmartThing
4
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>()
18
19         ColorControl(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentColor, MutableInteger currentHue, MutableInteger currentSaturation) {
20                 deviceValuesMap = deviceValueSmartThing
21                 deviceIntValuesMap = deviceIntValueSmartThing
22                 idSmartThing = id
23                 labelSmartThing = label
24                 displayNameSmartThing = displayName
25                 sendEventSmartThings = sendEvent
26
27                 // Initialization
28                 this.id = id
29                 this.label = label
30                 this.displayName = displayName
31                 this.currentHue = currentHue
32                 this.currentSaturation = currentSaturation
33                 this.currentColor = currentColor
34
35                 deviceValuesMap.put("color", currentColor)
36                 deviceIntValuesMap.put("hue", currentHue)
37                 deviceIntValuesMap.put("saturation", currentSaturation)
38         }
39
40         def setColor(LinkedHashMap metaData) {
41                 def hexColor = metaData.hex
42                 def newColor
43                 switch (hexColor) {
44                         case "#0000FF":
45                                 newColor = "Blue"
46                                 break;
47                         case "#00FF00":
48                                 newColor = "Green"
49                                 break;
50                         case "#FFFF00":
51                                 newColor = "Yellow"
52                                 break;
53                         case "#FF6000":
54                                 newColor = "Orange"
55                                 break;
56                         case "#BF7FBF":
57                                 newColor = "Purple"
58                                 break;
59                         case "#FF5F5F":
60                                 newColor = "Pink"
61                                 break;
62                         case "#FF0000":
63                                 newColor = "Red"
64                                 break;
65                         default:
66                                 newColor = "Blue"
67                                 break;
68                 }
69                 setColor(newColor)
70         }
71
72         // Methods to set values
73         def setColor(String newValue) {
74                 action(currentColor, newValue, "color")
75         }
76
77         def setHue(int newValue) {
78                 action(currentHue, newValue, "hue")
79         }
80         
81         def setHue(double newValue) {
82                 setHue((int) newValue)
83         }
84
85         def setSaturation(int newValue) {
86                 action(currentSaturation, newValue, "saturation")
87         }
88         
89         def setSaturation(double newValue) {
90                 setSaturation((int) newValue)
91         }
92
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"}'])
100                 }
101         }
102
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"}'])
110                 }
111         }
112
113         // Methods to return values
114         def getCurrentHue() {
115                 return currentHue.getValue()
116         }
117
118         def getCurrentSaturation() {
119                 return currentSaturation.getValue()
120         }
121         
122         def getCurrentColor() {
123                 return currentColor.toString()
124         }
125 }