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()
18 this.displayName = displayName
19 this.switchCurrentValue = switchCurrentValue
20 this.switchLatestValue = switchLatestValue
25 println("the switch with id:$id is on!")
26 this.switchLatestValue = this.switchCurrentValue
27 this.switchCurrentValue = "on"
28 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
29 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
32 def on(LinkedHashMap metaData) {
33 def task = timers.runAfter(metaData["delay"]) {
34 println("the switch with id:$id is on!")
35 this.switchLatestValue = this.switchCurrentValue
36 this.switchCurrentValue = "on"
37 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
38 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
43 println("the switch with id:$id is off!")
44 this.switchLatestValue = this.switchCurrentValue
45 this.switchCurrentValue = "off"
46 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
47 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
50 def off(LinkedHashMap metaData) {
51 def task = timers.runAfter(metaData["delay"]) {
52 println("the switch with id:$id is off!")
53 this.switchLatestValue = this.switchCurrentValue
54 this.switchCurrentValue = "off"
55 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
56 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
61 def setValue(String value) {
62 println("the switch with id:$id is $value!")
63 this.switchLatestValue = this.switchCurrentValue
64 this.switchCurrentValue = value
67 def currentValue(String deviceFeature) {
68 if (deviceFeature == "switch") {
69 return switchCurrentValue
73 def latestValue(String deviceFeature) {
74 if (deviceFeature == "switch") {
75 return switchLatestValue