2b5020eab7b4334234e02e550919768618d25b60
[smartthings-infrastructure.git] / SwitchLevel / SwitchLevel.groovy
1 //Create a class for switch level
2 package SwitchLevel
3 import Timer.SimulatedTimer
4
5 public class SwitchLevel {
6         private String id
7         private String label
8         private String displayName
9         private String switchState
10         private String currentSwitch
11         private int level
12         private int currentLevel
13         private int rate
14         private int currentRate
15         private int hue
16         private int currentHue
17         private int saturation
18         private String switchLatestValue
19         def sendEvent   
20         def timers
21         
22
23         SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, int hue, int saturation, String switchState, String switchLatestValue) {
24                 this.sendEvent = sendEvent
25                 this.timers = new SimulatedTimer()
26                 this.id = id
27                 this.label = label
28                 this.displayName = displayName
29                 this.level = level
30                 this.currentLevel = level
31                 this.rate = level
32                 this.currentRate = level
33                 this.hue = hue
34                 this.currentHue = hue
35                 this.saturation = saturation
36                 this.switchState = switchState
37                 this.currentSwitch = switchState
38                 this.switchLatestValue = switchLatestValue
39         }
40
41         //By Apps
42         def setColor(LinkedHashMap metaData) {
43                 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
44                         this.level = metaData["level"]
45                         this.currentLevel = metaData["level"]
46                         this.rate = metaData["level"]
47                         this.currentRate = metaData["level"]
48                         this.hue = metaData["hue"]
49                         this.currentHue = metaData["hue"]
50                         this.saturation = metaData["saturation"]
51                         println("the switch with id:$id is setted to level $level and hue to $hue and saturation to $saturation!")
52                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
53                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
54                         sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
55                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
56                         sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
57                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
58                 }
59         }
60
61         def setLevel(String level) {
62                 def newLevel = level.toInteger()
63                 setLevel(newLevel)
64         }       
65
66         def setLevel(int level) {
67                 if (this.level != level) {
68                         println("the switch with id:$id is setted to level $level!")
69                         this.level = level
70                         this.currentLevel = level
71                         this.rate = level
72                         this.currentRate = level
73                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
74                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
75                 }
76         }
77         
78         def setLevel(long level) {
79                 if (this.level != level) {
80                         println("the switch with id:$id is setted to level $level!")
81                         this.level = level
82                         this.currentLevel = level
83                         this.rate = level
84                         this.currentRate = level
85                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
86                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
87                 }
88         }
89
90         def on() {
91                 if (this.switchState != "on") {
92                         println("the switch with id:$id is on!")
93                         this.switchLatestValue = "on"
94                         this.switchState = "on"
95                         this.currentSwitch = "on"
96                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
97                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
98                 }
99         }
100
101         def on(LinkedHashMap metaData) {
102                 if (this.switchState != "on") {
103                         def task = timers.runAfter(metaData["delay"]) {
104                                 println("the switch with id:$id is on!")
105                                 this.switchLatestValue = "on"
106                                 this.switchState = "on"
107                                 this.currentSwitch = "on"
108                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
109                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
110                         }
111                 }
112         }
113
114         def off() {
115                 if (this.switchState != "off") {
116                         println("the switch with id:$id is off!")
117                         this.switchLatestValue = "off"
118                         this.switchState = "off"
119                         this.currentSwitch = "off"
120                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
121                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
122                 }
123         }
124
125         def off(LinkedHashMap metaData) {
126                 if (this.switchState != "off") {
127                         def task = timers.runAfter(metaData["delay"]) {
128                                 println("the switch with id:$id is off!")
129                                 this.switchLatestValue = "off"
130                                 this.switchState = "off"
131                                 this.currentSwitch = "off"
132                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
133                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
134                         }
135                 }
136         }
137
138         //By Model Checker
139         def setValue(String value, String name) {
140                 if (name == "switch") {
141                         println("the switch with id:$id is $value!")
142                         this.switchLatestValue = value
143                         this.switchState = value
144                         this.currentSwitch = value
145                 } else if (name == "level") {
146                         println("the switch with id:$id is setted to level $value!")
147                         this.level = value.toInteger()
148                         this.currentLevel = value.toInteger()
149                         this.rate = value.toInteger()
150                         this.currentRate = value.toInteger()
151                 }
152         }
153
154
155         def currentValue(String deviceFeature) {
156                 if (deviceFeature == "level") {
157                         return level
158                 } else if (deviceFeature == "switch") {
159                         return switchState
160                 }
161         }
162
163         def latestValue(String deviceFeature) {
164                 if (deviceFeature == "level") {
165                         return level
166                 } else if (deviceFeature == "switch") {
167                         return switchState
168                 }
169         }
170 }