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