4878c48b9f82c55e43a105068269d29e1b6f02dc
[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                 def newColor
70                 switch (hexColor) {
71                         case "#0000FF":
72                                 newColor = "Blue"
73                                 break;
74                         case "#00FF00":
75                                 newColor = "Green"
76                                 break;
77                         case "#FFFF00":
78                                 newColor = "Yellow"
79                                 break;
80                         case "#FF6000":
81                                 newColor = "Orange"
82                                 break;
83                         case "#BF7FBF":
84                                 newColor = "Purple"
85                                 break;
86                         case "#FF5F5F":
87                                 newColor = "Pink"
88                                 break;
89                         case "#FF0000":
90                                 newColor = "Red"
91                                 break;
92                         default:
93                                 newColor = "Blue"
94                                 break;
95                 }
96                 if (newColor != this.color) {
97                         this.currentColor = newColor
98                         this.color = newColor
99                         println("The color of the light is changed to $newColor!")
100                         sendEvent([name: "color", value: "$newColor", deviceId: this.id, descriptionText: "",
101                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
102                 }
103         }
104
105         def setColor(String color) {
106                 if (color != this.color) {
107                         this.currentColor = color
108                         this.color = color
109                         println("The color of the light is changed to $color!")
110                         sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
111                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
112                 }
113         }
114
115         def setHue(int hue) {
116                 if (hue != this.hue) {
117                         this.hue = hue
118                         this.currentHue = hue
119                         println("The hue level of the light is changed to $hue!")
120                         sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
121                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
122                 }
123         }
124
125         def setSaturation(int saturation) {
126                 if (saturation != this.saturation) {
127                         this.currentSaturation = saturation
128                         this.saturation = saturation
129                         println("The saturation level of the light is changed to $saturation!")
130                         sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
131                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
132                 }
133         }
134
135         def setLevel(int level) {
136                 if (level != this.level) {
137                         this.currentLevel = level
138                         this.level = level
139                         println("The level of the light is changed to $level!")
140                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
141                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
142                 }
143         }
144
145         def setColorTemperature(int colorTemperature) {
146                 if (colorTemperature != this.colorTemperature) {
147                         this.colorTemperature = colorTemperature
148                         println("The color temperature level of the light is changed to $colorTemperature!")
149                         sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
150                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
151                 }
152         }
153
154         def on() {
155                 if (this.currentSwitch != "on") {
156                         this.currentSwitch = currentSwitch
157                         println("The light is changed to on!")
158                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
159                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
160                 }
161         }
162
163         def off() {
164                 if (this.currentSwitch != "off") {
165                         this.currentSwitch = currentSwitch
166                         println("The light is changed to off!")
167                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
168                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
169                 }
170         }
171
172         def currentValue(String deviceFeature) {
173                 if (deviceFeature == "color") {
174                         return color
175                 } else if (deviceFeature == "saturation") {
176                         return saturation
177                 } else if (deviceFeature == "hue") {
178                         return hue
179                 } else if (deviceFeature == "level") {
180                         return level
181                 } else if (deviceFeature == "colorTemperature") {
182                         return colorTemperature
183                 } else if (deviceFeature == "switch") {
184                         return currentSwitch
185                 }
186         }
187
188         def latestValue(String deviceFeature) {
189                 if (deviceFeature == "color") {
190                         return color
191                 } else if (deviceFeature == "saturation") {
192                         return saturation
193                 } else if (deviceFeature == "hue") {
194                         return hue
195                 } else if (deviceFeature == "level") {
196                         return level
197                 } else if (deviceFeature == "colorTemperature") {
198                         return colorTemperature
199                 } else if (deviceFeature == "switch") {
200                         return currentSwitch
201                 }
202         }
203 }