1 //Create a class for switch device
4 public class Switches {
6 private String displayName
7 private String switchCurrentValue
8 private String switchLatestValue
13 Switches(Closure sendEvent, int id, String displayName, String switchCurrentValue, String switchLatestValue) {
14 this.sendEvent = sendEvent
15 this.timers = new Timer()
17 this.displayName = displayName
18 this.switchCurrentValue = switchCurrentValue
19 this.switchLatestValue = switchLatestValue
24 println("the switch with id:$id is on!")
25 this.switchLatestValue = this.switchCurrentValue
26 this.switchCurrentValue = "on"
27 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
28 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
31 def on(LinkedHashMap metaData) {
32 def task = timers.runAfter(metaData["delay"]) {
33 println("the switch with id:$id is on!")
34 this.switchLatestValue = this.switchCurrentValue
35 this.switchCurrentValue = "on"
36 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
37 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
42 println("the switch with id:$id is off!")
43 this.switchLatestValue = this.switchCurrentValue
44 this.switchCurrentValue = "off"
45 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
46 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
49 def off(LinkedHashMap metaData) {
50 def task = timers.runAfter(metaData["delay"]) {
51 println("the switch with id:$id is off!")
52 this.switchLatestValue = this.switchCurrentValue
53 this.switchCurrentValue = "off"
54 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
55 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
60 def setValue(String value) {
61 println("the switch with id:$id is $value!")
62 this.switchLatestValue = this.switchCurrentValue
63 this.switchCurrentValue = value
66 def currentValue(String deviceFeature) {
67 if (deviceFeature == "switch") {
68 return switchCurrentValue
72 def latestValue(String deviceFeature) {
73 if (deviceFeature == "switch") {
74 return switchLatestValue