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 setLevel(long level) {
54                 if (level != this.level) {
55                         this.currentLevel = level
56                         this.level = level
57                         println("The level of the light is changed to $level!")
58                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
59                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                 }
61         }
62
63         def setColorTemperature(int colorTemperature) {
64                 if (colorTemperature != this.colorTemperature) {
65                         this.colorTemperature = colorTemperature
66                         println("The color temperature level of the light is changed to $colorTemperature!")
67                         sendEvent([name: "colorTemperature", value: "$colorTemperature", deviceId: this.id, descriptionText: "",
68                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
69                 }
70         }
71
72         def on(String currentSwitch) {
73                 if (currentSwitch != this.currentSwitch) {
74                         this.currentSwitch = currentSwitch
75                         println("The light is changed to $currentSwitch!")
76                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
77                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78                 }
79         }
80
81         def off(String currentSwitch) {
82                 if (currentSwitch != this.currentSwitch) {
83                         this.currentSwitch = currentSwitch
84                         println("The light is changed to $currentSwitch!")
85                         sendEvent([name: "switch", value: "$currentSwitch", deviceId: this.id, descriptionText: "",
86                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
87                 }
88         }
89
90         def currentValue(String deviceFeature) {
91                 if (deviceFeature == "level") {
92                         return level
93                 } else if (deviceFeature == "colorTemperature") {
94                         return colorTemperature
95                 } else if (deviceFeature == "switch") {
96                         return currentSwitch
97                 }
98         }
99
100         def latestValue(String deviceFeature) {
101                 if (deviceFeature == "level") {
102                         return level
103                 } else if (deviceFeature == "colorTemperature") {
104                         return colorTemperature
105                 } else if (deviceFeature == "switch") {
106                         return currentSwitch
107                 }
108         }
109 }