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 if (this.currentLevel != level) {
32 println("the switch with id:$id is setted to level $level!")
33 this.currentLevel = level
34 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
35 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
40 if (this.switchState != "on") {
41 println("the switch with id:$id is on!")
42 this.switchLatestValue = "on"
43 this.switchState = "on"
44 this.currentSwitch = "on"
45 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
46 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
47 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
48 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
52 def on(LinkedHashMap metaData) {
53 if (this.switchState != "on") {
54 def task = timers.runAfter(metaData["delay"]) {
55 println("the switch with id:$id is on!")
56 this.switchLatestValue = "on"
57 this.switchState = "on"
58 this.currentSwitch = "on"
59 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
60 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
62 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
68 if (this.switchState != "off") {
69 println("the switch with id:$id is off!")
70 this.switchLatestValue = "off"
71 this.switchState = "off"
72 this.currentSwitch = "off"
73 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
74 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
75 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
76 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
80 def off(LinkedHashMap metaData) {
81 if (this.switchState != "off") {
82 def task = timers.runAfter(metaData["delay"]) {
83 println("the switch with id:$id is off!")
84 this.switchLatestValue = "off"
85 this.switchState = "off"
86 this.currentSwitch = "off"
87 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
88 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
89 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
90 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
96 def setValue(String value) {
97 println("the switch with id:$id is $value!")
98 this.switchLatestValue = value
99 this.switchState = value
100 this.currentSwitch = value
103 def currentValue(String deviceFeature) {
104 if (deviceFeature == "switch") {
109 def latestValue(String deviceFeature) {
110 if (deviceFeature == "switch") {
111 return switchLatestValue