Changes in classes: new concept for latest value + all types of events generated...
[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 rate
13         private String switchLatestValue
14         def sendEvent   
15         def timers
16         
17
18         SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, String switchState, String switchLatestValue) {
19                 this.sendEvent = sendEvent
20                 this.timers = new SimulatedTimer()
21                 this.id = id
22                 this.label = label
23                 this.displayName = displayName
24                 this.level = level
25                 this.rate = level
26                 this.switchState = switchState
27                 this.currentSwitch = switchState
28                 this.switchLatestValue = switchLatestValue
29         }
30
31         //By Apps
32         def setLevel(int level) {
33                 if (this.level != level) {
34                         println("the switch with id:$id is setted to level $level!")
35                         this.level = level
36                         this.rate = level
37                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
38                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
39                 }
40         }
41
42         def on() {
43                 if (this.switchState != "on") {
44                         println("the switch with id:$id is on!")
45                         this.switchLatestValue = "on"
46                         this.switchState = "on"
47                         this.currentSwitch = "on"
48                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
49                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50                         sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
51                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
52                 }
53         }
54
55         def on(LinkedHashMap metaData) {
56                 if (this.switchState != "on") {
57                         def task = timers.runAfter(metaData["delay"]) {
58                                 println("the switch with id:$id is on!")
59                                 this.switchLatestValue = "on"
60                                 this.switchState = "on"
61                                 this.currentSwitch = "on"
62                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
63                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64                                 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
65                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
66                         }
67                 }
68         }
69
70         def off() {
71                 if (this.switchState != "off") {
72                         println("the switch with id:$id is off!")
73                         this.switchLatestValue = "off"
74                         this.switchState = "off"
75                         this.currentSwitch = "off"
76                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
77                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78                         sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
79                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
80                 }
81         }
82
83         def off(LinkedHashMap metaData) {
84                 if (this.switchState != "off") {
85                         def task = timers.runAfter(metaData["delay"]) {
86                                 println("the switch with id:$id is off!")
87                                 this.switchLatestValue = "off"
88                                 this.switchState = "off"
89                                 this.currentSwitch = "off"
90                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
91                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
92                                 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
93                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
94                         }
95                 }
96         }
97
98         //By Model Checker
99         def setValue(String value) {
100                 println("the switch with id:$id is setted to level $value!")
101                 this.level = value.toInteger()
102                 this.rate = value.toInteger()
103         }
104 }