Fixing some bugs
[smartthings-infrastructure.git] / Switch / Switch.groovy
1 //Create a class for switch device
2 package Switch
3 import Timer.SimulatedTimer
4
5 public class Switch {
6         private String id
7         private String label
8         private String displayName
9         private String switchState
10         private String currentSwitch
11         private int currentLevel
12         private String switchLatestValue
13         def sendEvent   
14         def timers
15         
16
17         Switch(Closure sendEvent, String id, String label, String displayName, String switchState, String currentSwitch, int currentLevel, String switchLatestValue) {
18                 this.sendEvent = sendEvent
19                 this.timers = new SimulatedTimer()
20                 this.currentSwitch = currentSwitch
21                 this.currentLevel = currentLevel
22                 this.id = id
23                 this.label = label
24                 this.displayName = displayName
25                 this.switchState = switchState
26                 this.switchLatestValue = switchLatestValue
27         }
28
29         def eventsSince() {
30                 def evtOn = [[name: "switch", value: "on", deviceId: "switchID0", descriptionText: "",
31                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
32                 def evtOff = [[name: "switch", value: "off", deviceId: "switchID0", descriptionText: "",
33                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
34                 def init = Verify.getInt(0,4)
35                 def evtToSend = []
36                 if (init == 0) {//return empty set
37                         return evtToSend
38                 } else if (init == 1) {//send one open event
39                         evtOn.each{
40                                 evtToSend.add(it)
41                         }
42                         return evtToSend
43                 } else if (init == 2) {//send two open events
44                         evtOn.each{
45                                 evtToSend.add(it)
46                         }
47                         evtOn.each{
48                                 evtToSend.add(it)
49                         }
50                         return evtToSend
51                 } else if (init == 3) {//send one closed event
52                         evtOff.each{
53                                 evtToSend.add(it)
54                         }
55                         return evtToSend
56                 } else if (init == 4) {//send two closed events
57                         evtOff.each{
58                                 evtToSend.add(it)
59                         }
60                         evtOff.each{
61                                 evtToSend.add(it)
62                         }
63                         return evtToSend
64                 }
65         }
66
67         //By Apps
68         def setLevel(int level) {
69                 if (this.currentLevel != level) {
70                         println("the switch with id:$id is setted to level $level!")
71                         this.currentLevel = level
72                         sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
73                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74                 }
75         }
76
77         def on() {
78                 if (this.switchState != "on") {
79                         println("the switch with id:$id is on!")
80                         this.switchLatestValue = "on"
81                         this.switchState = "on"
82                         this.currentSwitch = "on"
83                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
84                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
85                 }
86         }
87
88         def on(LinkedHashMap metaData) {
89                 if (this.switchState != "on") {
90                         def task = timers.runAfter(metaData["delay"]) {
91                                 println("the switch with id:$id is on!")
92                                 this.switchLatestValue = "on"
93                                 this.switchState = "on"
94                                 this.currentSwitch = "on"
95                                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
96                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
97                         }
98                 }
99         }
100
101         def off() {
102                 if (this.switchState != "off") {
103                         println("the switch with id:$id is off!")
104                         this.switchLatestValue = "off"
105                         this.switchState = "off"
106                         this.currentSwitch = "off"
107                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
108                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
109                 }
110         }
111
112         def off(LinkedHashMap metaData) {
113                 if (this.switchState != "off") {
114                         def task = timers.runAfter(metaData["delay"]) {
115                                 println("the switch with id:$id is off!")
116                                 this.switchLatestValue = "off"
117                                 this.switchState = "off"
118                                 this.currentSwitch = "off"
119                                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
120                                     displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
121                         }
122                 }
123         }
124
125         //By Model Checker
126         def setValue(String value) {
127                 println("the switch with id:$id is $value!")
128                 this.switchLatestValue = value
129                 this.switchState = value
130                 this.currentSwitch = value
131         }
132         
133         def currentValue(String deviceFeature) {
134                 if (deviceFeature == "switch") {
135                         return switchState
136                 }
137         }
138
139         def latestValue(String deviceFeature) {
140                 if (deviceFeature == "switch") {
141                         return switchLatestValue
142                 }
143         }
144 }