1 //Create a class for switch level
3 import Timer.SimulatedTimer
5 public class SwitchLevel {
8 private String displayName
9 private String switchState
10 private String currentSwitch
13 private String switchLatestValue
18 SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, String switchState, String switchLatestValue) {
19 this.sendEvent = sendEvent
20 this.timers = new SimulatedTimer()
23 this.displayName = displayName
26 this.switchState = switchState
27 this.currentSwitch = switchState
28 this.switchLatestValue = switchLatestValue
32 def setLevel(int level) {
33 if (this.level != level) {
34 println("the switch with id:$id is setted to level $level!")
37 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
38 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
43 if (this.switchState != "on") {
44 println("the switch with id:$id is on!")
45 this.switchLatestValue = "on"
46 this.switchState = "on"
47 this.currentSwitch = "on"
48 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
49 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
51 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
55 def on(LinkedHashMap metaData) {
56 if (this.switchState != "on") {
57 def task = timers.runAfter(metaData["delay"]) {
58 println("the switch with id:$id is on!")
59 this.switchLatestValue = "on"
60 this.switchState = "on"
61 this.currentSwitch = "on"
62 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
63 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
65 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
71 if (this.switchState != "off") {
72 println("the switch with id:$id is off!")
73 this.switchLatestValue = "off"
74 this.switchState = "off"
75 this.currentSwitch = "off"
76 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
77 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
79 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
83 def off(LinkedHashMap metaData) {
84 if (this.switchState != "off") {
85 def task = timers.runAfter(metaData["delay"]) {
86 println("the switch with id:$id is off!")
87 this.switchLatestValue = "off"
88 this.switchState = "off"
89 this.currentSwitch = "off"
90 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
91 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
92 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
93 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
99 def setValue(String value) {
100 println("the switch with id:$id is setted to level $value!")
101 this.level = value.toInteger()
102 this.rate = value.toInteger()