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 if (this.switchState != "on") {
37 println("the switch with id:$id is on!")
38 this.switchLatestValue = this.switchState
39 this.switchState = "on"
40 this.currentSwitch = "on"
41 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
42 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
46 def on(LinkedHashMap metaData) {
47 if (this.switchState != "on") {
48 def task = timers.runAfter(metaData["delay"]) {
49 println("the switch with id:$id is on!")
50 this.switchLatestValue = this.switchState
51 this.switchState = "on"
52 this.currentSwitch = "on"
53 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
54 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60 if (this.switchState != "off") {
61 println("the switch with id:$id is off!")
62 this.switchLatestValue = this.switchState
63 this.switchState = "off"
64 this.currentSwitch = "off"
65 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
66 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
70 def off(LinkedHashMap metaData) {
71 if (this.switchState != "off") {
72 def task = timers.runAfter(metaData["delay"]) {
73 println("the switch with id:$id is off!")
74 this.switchLatestValue = this.switchState
75 this.switchState = "off"
76 this.currentSwitch = "off"
77 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
78 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
84 def setValue(String value) {
85 println("the switch with id:$id is $value!")
86 this.switchLatestValue = this.switchState
87 this.switchState = value
88 this.currentSwitch = value
91 def currentValue(String deviceFeature) {
92 if (deviceFeature == "switch") {
97 def latestValue(String deviceFeature) {
98 if (deviceFeature == "switch") {
99 return switchLatestValue