1 //Create a class for switch device
3 import Timer.SimulatedTimer
6 import gov.nasa.jpf.vm.Verify
12 private String displayName
13 private String switchState
14 private String currentSwitch
15 private int currentLevel
16 private String switchLatestValue
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
28 this.displayName = displayName
29 this.switchState = switchState
30 this.switchLatestValue = switchLatestValue
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)
40 if (init == 0) {//return empty set
42 } else if (init == 1) {//send one open event
47 } else if (init == 2) {//send two open events
55 } else if (init == 3) {//send one closed event
60 } else if (init == 4) {//send two closed events
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"}'])
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"}'])
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"}'])
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"}'])
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"}'])
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
137 def currentValue(String deviceFeature) {
138 if (deviceFeature == "switch") {
143 def latestValue(String deviceFeature) {
144 if (deviceFeature == "switch") {
145 return switchLatestValue