Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / ColorTemperature / ColorTemperature.groovy
1 //Create a class for color temperature
2 package ColorTemperature
3 import Timer.SimulatedTimer
4
5
6 public class ColorTemperature {
7         def sendEvent
8         private String id
9         private String label
10         private String displayName
11         private String currentSwitch
12         private int level
13         private int currentLevel
14         private int colorTemperature
15         
16         ColorTemperature(Closure sendEvent, String id, String label, String displayName, int level, String currentSwitch, int colorTemperature) {
17                 this.id = id
18                 this.label = label
19                 this.displayName = displayName
20                 this.level = level
21                 this.currentLevel = level
22                 this.currentSwitch = currentSwitch
23                 this.colorTemperature = colorTemperature
24                 this.sendEvent = sendEvent
25         }
26         
27         //By model checker
28         def setValue(String value, String name) {
29                 if ((name == "level") && (value != this.level)) {
30                         this.currentLevel = value.toInteger()
31                         this.level = value.toInteger()
32                         println("The level of the light is changed to $value!")
33                 } else if ((name == "currentSwitch") && (value != this.currentSwitch)) {
34                         this.currentSwitch = value
35                         println("The light is changed to $value!")
36                 } else if ((name == "colorTemperature") && (value != this.colorTemperature)) {
37                         this.colorTemperature = value.toInteger()
38                         println("The color temperature level of the light is changed to $value!")
39                 }
40         }
41
42         //methods
43         def setLevel(int level) {
44                 if (level != this.level) {
45                         this.currentLevel = level
46                         this.level = level
47                         println("The level of the light is changed to $level!")
48                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
49                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50                 }
51         }
52
53         def setColorTemperature(int colorTemperature) {
54                 if (colorTemperature != this.colorTemperature) {
55                         this.colorTemperature = colorTemperature
56                         println("The color temperature level of the light is changed to $colorTemperature!")
57                         sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
58                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59                 }
60         }
61
62         def on(String currentSwitch) {
63                 if (currentSwitch != this.currentSwitch) {
64                         this.currentSwitch = currentSwitch
65                         println("The light is changed to $currentSwitch!")
66                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
67                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
68                         sendEvent([name: "switch.on", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
69                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
70                 }
71         }
72
73         def off(String currentSwitch) {
74                 if (currentSwitch != this.currentSwitch) {
75                         this.currentSwitch = currentSwitch
76                         println("The light is changed to $currentSwitch!")
77                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
78                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
79                         sendEvent([name: "switch.off", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
80                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
81                 }
82         }
83
84         def currentValue(String deviceFeature) {
85                 if (deviceFeature == "level") {
86                         return level
87                 } else if (deviceFeature == "colorTemperature") {
88                         return colorTemperature
89                 } else if (deviceFeature == "switch") {
90                         return currentSwitch
91                 }
92         }
93
94         def latestValue(String deviceFeature) {
95                 if (deviceFeature == "level") {
96                         return level
97                 } else if (deviceFeature == "colorTemperature") {
98                         return colorTemperature
99                 } else if (deviceFeature == "switch") {
100                         return currentSwitch
101                 }
102         }
103 }