1 //Create a class for switch device
3 import Timer.SimulatedTimer
8 private String displayName
9 private String switchState
10 private String currentSwitch
11 private int currentLevel
12 private String switchLatestValue
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
24 this.displayName = displayName
25 this.switchState = switchState
26 this.switchLatestValue = switchLatestValue
30 def setLevel(int level) {
31 println("the switch with id:$id is setted to level $level!")
32 this.currentLevel = level
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: [value: "on"]])
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: [value: "on"]])
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: [value: "off"]])
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: [value: "off"]])
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
83 def currentValue(String deviceFeature) {
84 if (deviceFeature == "switch") {
89 def latestValue(String deviceFeature) {
90 if (deviceFeature == "switch") {
91 return switchLatestValue