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