Adding jpf.jar to enable the compilation of apps with the gov.nasa.jpf.vm.Verify...
[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 int 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, int 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         //By Apps
30         def setLevel(int level) {
31                 println("the switch with id:$id is setted to level $level!")
32                 this.currentLevel = level
33         }
34
35         def on() {
36                 println("the switch with id:$id is on!")
37                 this.switchLatestValue = this.switchState
38                 this.switchState = "on"
39                 this.currentSwitch = "on"
40                 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
41                     displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
42         }
43
44         def on(LinkedHashMap metaData) {
45                 def task = timers.runAfter(metaData["delay"]) {
46                         println("the switch with id:$id is on!")
47                         this.switchLatestValue = this.switchState
48                         this.switchState = "on"
49                         this.currentSwitch = "on"
50                         sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
51                             displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
52                 }
53         }
54
55         def off() {
56                 println("the switch with id:$id is off!")
57                 this.switchLatestValue = this.switchState
58                 this.switchState = "off"
59                 this.currentSwitch = "off"
60                 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
61                     displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
62         }
63
64         def off(LinkedHashMap metaData) {
65                 def task = timers.runAfter(metaData["delay"]) {
66                         println("the switch with id:$id is off!")
67                         this.switchLatestValue = this.switchState
68                         this.switchState = "off"
69                         this.currentSwitch = "off"
70                         sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
71                             displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
72                 }
73         }
74
75         //By Model Checker
76         def setValue(String value) {
77                 println("the switch with id:$id is $value!")
78                 this.switchLatestValue = this.switchState
79                 this.switchState = value
80                 this.currentSwitch = value
81         }
82         
83         def currentValue(String deviceFeature) {
84                 if (deviceFeature == "switch") {
85                         return switchState
86                 }
87         }
88
89         def latestValue(String deviceFeature) {
90                 if (deviceFeature == "switch") {
91                         return switchLatestValue
92                 }
93         }
94 }