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"}'])
63 def on(LinkedHashMap metaData) {
64 if (this.switchState != "on") {
65 def task = timers.runAfter(metaData["delay"]) {
66 println("the switch with id:$id is on!")
67 this.switchLatestValue = "on"
68 this.switchState = "on"
69 this.currentSwitch = "on"
70 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
71 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
77 if (this.switchState != "off") {
78 println("the switch with id:$id is off!")
79 this.switchLatestValue = "off"
80 this.switchState = "off"
81 this.currentSwitch = "off"
82 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
83 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
87 def off(LinkedHashMap metaData) {
88 if (this.switchState != "off") {
89 def task = timers.runAfter(metaData["delay"]) {
90 println("the switch with id:$id is off!")
91 this.switchLatestValue = "off"
92 this.switchState = "off"
93 this.currentSwitch = "off"
94 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
95 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
101 def setValue(String value, String name) {
102 if (name == "switch") {
103 println("the switch with id:$id is $value!")
104 this.switchLatestValue = value
105 this.switchState = value
106 this.currentSwitch = value
107 } else if (name == "level") {
108 println("the switch with id:$id is setted to level $value!")
109 this.level = value.toInteger()
110 this.rate = value.toInteger()
115 def currentValue(String deviceFeature) {
116 if (deviceFeature == "level") {
118 } else if (deviceFeature == "switch") {
123 def latestValue(String deviceFeature) {
124 if (deviceFeature == "level") {
126 } else if (deviceFeature == "switch") {