Interleaving events.
[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 currentSwitch
13         private int level
14         private int hue
15         private int saturation
16         private int colorTemperature
17         
18         ColorControl(Closure sendEvent, String id, String label, String displayName, String color, int hue, int saturation, int level, String currentSwitch, int colorTemperature) {
19                 this.id = id
20                 this.label = label
21                 this.displayName = displayName
22                 this.color = color
23                 this.hue = hue
24                 this.saturation = saturation
25                 this.level = level
26                 this.currentSwitch = currentSwitch
27                 this.colorTemperature = colorTemperature
28                 this.sendEvent = sendEvent
29         }
30         
31         //By model checker
32         def setValue(String value, String name) {
33                 if ((name == "color") && (value != this.color)) {
34                         this.color = value
35                         println("the color of the light is changed to $value!")
36                 } else if ((name == "hue") && (value != this.hue)) {
37                         this.hue = value.toInteger()
38                         println("The hue level of the light is changed to $value!")
39                 } else if ((name == "saturation") && (value != this.saturation)) {
40                         this.saturation = value.toInteger()
41                         println("The saturation level of the light is changed to $value!")
42                 } else if ((name == "level") && (value != this.level)) {
43                         this.level = value.toInteger()
44                         println("The level of the light is changed to $value!")
45                 } else if ((name == "currentSwitch") && (value != this.currentSwitch)) {
46                         this.currentSwitch = value
47                         println("The light is changed to $value!")
48                 } else if ((name == "colorTemperature") && (value != this.colorTemperature)) {
49                         this.colorTemperature = value.toInteger()
50                         println("The color temperature level of the light is changed to $value!")
51                 }
52         }
53
54         //methods
55         def setColor(String color) {
56                 if (color != this.color) {
57                         this.color = color
58                         println("The color of the light is changed to $color!")
59                         sendEvent([name: "color", value: "$color", deviceId: this.id, descriptionText: "",
60                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61                 }
62         }
63
64         def setHue(int hue) {
65                 if (hue != this.hue) {
66                         this.hue = hue
67                         println("The hue level of the light is changed to $hue!")
68                         sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
69                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
70                 }
71         }
72
73         def setSaturation(int saturation) {
74                 if (saturation != this.saturation) {
75                         this.saturation = saturation
76                         println("The saturation level of the light is changed to $saturation!")
77                         sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
78                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
79                 }
80         }
81
82         def setLevel(int level) {
83                 if (level != this.level) {
84                         this.level = level
85                         println("The level of the light is changed to $level!")
86                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
87                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
88                 }
89         }
90
91         def setColorTemperature(int colorTemperature) {
92                 if (colorTemperature != this.colorTemperature) {
93                         this.colorTemperature = colorTemperature
94                         println("The color temperature level of the light is changed to $colorTemperature!")
95                         sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
96                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
97                 }
98         }
99
100         def on(String currentSwitch) {
101                 if (currentSwitch != this.currentSwitch) {
102                         this.currentSwitch = currentSwitch
103                         println("The light is changed to $currentSwitch!")
104                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
105                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
106                         sendEvent([name: "switch.on", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
107                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
108                 }
109         }
110
111         def off(String currentSwitch) {
112                 if (currentSwitch != this.currentSwitch) {
113                         this.currentSwitch = currentSwitch
114                         println("The light is changed to $currentSwitch!")
115                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
116                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
117                         sendEvent([name: "switch.off", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
118                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
119                 }
120         }
121
122         def currentValue(String deviceFeature) {
123                 if (deviceFeature == "color") {
124                         return color
125                 } else if (deviceFeature == "saturation") {
126                         return saturation
127                 } else if (deviceFeature == "hue") {
128                         return hue
129                 } else if (deviceFeature == "level") {
130                         return level
131                 } else if (deviceFeature == "colorTemperature") {
132                         return colorTemperature
133                 } else if (deviceFeature == "switch") {
134                         return currentSwitch
135                 }
136         }
137
138         def latestValue(String deviceFeature) {
139                 if (deviceFeature == "color") {
140                         return color
141                 } else if (deviceFeature == "saturation") {
142                         return saturation
143                 } else if (deviceFeature == "hue") {
144                         return hue
145                 } else if (deviceFeature == "level") {
146                         return level
147                 } else if (deviceFeature == "colorTemperature") {
148                         return colorTemperature
149                 } else if (deviceFeature == "switch") {
150                         return currentSwitch
151                 }
152         }
153 }