Fixing a bug in color control.
[smartthings-infrastructure.git] / ColorControl / ColorControl.groovy
1 //Create a class for color control
2 package ColorControl
3 import Timer.SimulatedTimer
4
5
6 public class ColorControl {
7         def sendEvent
8         private String id
9         private String label
10         private String displayName
11         private String color
12         private String currentColor
13         private String currentSwitch
14         private int level
15         private int currentLevel
16         private int hue
17         private int currentHue
18         private int saturation
19         private int currentSaturation
20         private int colorTemperature
21         
22         ColorControl(Closure sendEvent, String id, String label, String displayName, String color, int hue, int saturation, int level, String currentSwitch, int colorTemperature) {
23                 this.id = id
24                 this.label = label
25                 this.displayName = displayName
26                 this.color = color
27                 this.currentColor = color
28                 this.hue = hue
29                 this.currentHue = hue
30                 this.saturation = saturation
31                 this.currentSaturation = saturation
32                 this.level = level
33                 this.currentLevel = level
34                 this.currentSwitch = currentSwitch
35                 this.colorTemperature = colorTemperature
36                 this.sendEvent = sendEvent
37         }
38         
39         //By model checker
40         def setValue(String value, String name) {
41                 if ((name == "color") && (value != this.color)) {
42                         this.color = value
43                         this.currentColor = value
44                         println("the color of the light is changed to $value!")
45                 } else if ((name == "hue") && (value != this.hue)) {
46                         this.hue = value.toInteger()
47                         this.currentHue = value.toInteger()
48                         println("The hue level of the light is changed to $value!")
49                 } else if ((name == "saturation") && (value != this.saturation)) {
50                         this.saturation = value.toInteger()
51                         this.currentSaturation = value.toInteger()
52                         println("The saturation level of the light is changed to $value!")
53                 } else if ((name == "level") && (value != this.level)) {
54                         this.currentLevel = value.toInteger()
55                         this.level = value.toInteger()
56                         println("The level of the light is changed to $value!")
57                 } else if ((name == "currentSwitch") && (value != this.currentSwitch)) {
58                         this.currentSwitch = value
59                         println("The light is changed to $value!")
60                 } else if ((name == "colorTemperature") && (value != this.colorTemperature)) {
61                         this.colorTemperature = value.toInteger()
62                         println("The color temperature level of the light is changed to $value!")
63                 }
64         }
65
66         //methods
67         def setColor(LinkedHashMap metaData) {
68                 def hexColor = metaData.hex
69                 switch (hexColor) {
70                         case "#0000FF":
71                                 color = "Blue"
72                                 break;
73                         case "#00FF00":
74                                 color = "Green"
75                                 break;
76                         case "#FFFF00":
77                                 color = "Yellow"
78                                 break;
79                         case "#FF6000":
80                                 color = "Orange"
81                                 break;
82                         case "#BF7FBF":
83                                 color = "Purple"
84                                 break;
85                         case "#FF5F5F":
86                                 color = "Pink"
87                                 break;
88                         case "#FF0000":
89                                 color = "Red"
90                                 break;
91                         default:
92                                 color = "Blue"
93                                 break;
94                 }
95                 if (color != this.color) {
96                         this.currentColor = color
97                         this.color = color
98                         println("The color of the light is changed to $color!")
99                         sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
100                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
101                 }
102         }
103
104         def setColor(String color) {
105                 if (color != this.color) {
106                         this.currentColor = color
107                         this.color = color
108                         println("The color of the light is changed to $color!")
109                         sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
110                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
111                 }
112         }
113
114         def setHue(int hue) {
115                 if (hue != this.hue) {
116                         this.hue = hue
117                         this.currentHue = hue
118                         println("The hue level of the light is changed to $hue!")
119                         sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
120                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
121                 }
122         }
123
124         def setSaturation(int saturation) {
125                 if (saturation != this.saturation) {
126                         this.currentSaturation = saturation
127                         this.saturation = saturation
128                         println("The saturation level of the light is changed to $saturation!")
129                         sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
130                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
131                 }
132         }
133
134         def setLevel(int level) {
135                 if (level != this.level) {
136                         this.currentLevel = level
137                         this.level = level
138                         println("The level of the light is changed to $level!")
139                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
140                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
141                 }
142         }
143
144         def setColorTemperature(int colorTemperature) {
145                 if (colorTemperature != this.colorTemperature) {
146                         this.colorTemperature = colorTemperature
147                         println("The color temperature level of the light is changed to $colorTemperature!")
148                         sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
149                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
150                 }
151         }
152
153         def on() {
154                 if (this.currentSwitch != "on") {
155                         this.currentSwitch = currentSwitch
156                         println("The light is changed to on!")
157                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
158                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
159                         sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
160                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
161                 }
162         }
163
164         def off() {
165                 if (this.currentSwitch != "off") {
166                         this.currentSwitch = currentSwitch
167                         println("The light is changed to off!")
168                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
169                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
170                         sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
171                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
172                 }
173         }
174
175         def currentValue(String deviceFeature) {
176                 if (deviceFeature == "color") {
177                         return color
178                 } else if (deviceFeature == "saturation") {
179                         return saturation
180                 } else if (deviceFeature == "hue") {
181                         return hue
182                 } else if (deviceFeature == "level") {
183                         return level
184                 } else if (deviceFeature == "colorTemperature") {
185                         return colorTemperature
186                 } else if (deviceFeature == "switch") {
187                         return currentSwitch
188                 }
189         }
190
191         def latestValue(String deviceFeature) {
192                 if (deviceFeature == "color") {
193                         return color
194                 } else if (deviceFeature == "saturation") {
195                         return saturation
196                 } else if (deviceFeature == "hue") {
197                         return hue
198                 } else if (deviceFeature == "level") {
199                         return level
200                 } else if (deviceFeature == "colorTemperature") {
201                         return colorTemperature
202                 } else if (deviceFeature == "switch") {
203                         return currentSwitch
204                 }
205         }
206 }