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"}'])
42 def setLevel(long level) {
43 if (this.level != level) {
44 println("the switch with id:$id is setted to level $level!")
47 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
48 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
53 if (this.switchState != "on") {
54 println("the switch with id:$id is on!")
55 this.switchLatestValue = "on"
56 this.switchState = "on"
57 this.currentSwitch = "on"
58 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
59 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
61 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
65 def on(LinkedHashMap metaData) {
66 if (this.switchState != "on") {
67 def task = timers.runAfter(metaData["delay"]) {
68 println("the switch with id:$id is on!")
69 this.switchLatestValue = "on"
70 this.switchState = "on"
71 this.currentSwitch = "on"
72 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
73 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
75 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
81 if (this.switchState != "off") {
82 println("the switch with id:$id is off!")
83 this.switchLatestValue = "off"
84 this.switchState = "off"
85 this.currentSwitch = "off"
86 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
87 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
88 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
89 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
93 def off(LinkedHashMap metaData) {
94 if (this.switchState != "off") {
95 def task = timers.runAfter(metaData["delay"]) {
96 println("the switch with id:$id is off!")
97 this.switchLatestValue = "off"
98 this.switchState = "off"
99 this.currentSwitch = "off"
100 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
101 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
102 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
103 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
109 def setValue(String value, String name) {
110 if (name == "switch") {
111 println("the switch with id:$id is $value!")
112 this.switchLatestValue = value
113 this.switchState = value
114 this.currentSwitch = value
115 } else if (name == "level") {
116 println("the switch with id:$id is setted to level $value!")
117 this.level = value.toInteger()
118 this.rate = value.toInteger()
123 def currentValue(String deviceFeature) {
124 if (deviceFeature == "level") {
126 } else if (deviceFeature == "switch") {
131 def latestValue(String deviceFeature) {
132 if (deviceFeature == "level") {
134 } else if (deviceFeature == "switch") {