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: "50", 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 = this.switchState
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"}'])
53 def on(LinkedHashMap metaData) {
54 if (this.switchState != "on") {
55 def task = timers.runAfter(metaData["delay"]) {
56 println("the switch with id:$id is on!")
57 this.switchLatestValue = this.switchState
58 this.switchState = "on"
59 this.currentSwitch = "on"
60 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
61 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
67 if (this.switchState != "off") {
68 println("the switch with id:$id is off!")
69 this.switchLatestValue = this.switchState
70 this.switchState = "off"
71 this.currentSwitch = "off"
72 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
73 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
77 def off(LinkedHashMap metaData) {
78 if (this.switchState != "off") {
79 def task = timers.runAfter(metaData["delay"]) {
80 println("the switch with id:$id is off!")
81 this.switchLatestValue = this.switchState
82 this.switchState = "off"
83 this.currentSwitch = "off"
84 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
85 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
91 def setValue(String value) {
92 println("the switch with id:$id is setted to level $value!")
93 this.level = value.toInteger()
94 this.rate = value.toInteger()