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