1 //Create a class for switch device
3 import Timer.SimulatedTimer
5 public class Switches {
7 private String displayName
8 private String switchState
9 private String switchLatestValue
14 Switches(Closure sendEvent, int id, String displayName, String switchState, String switchLatestValue) {
15 this.sendEvent = sendEvent
16 this.timers = new SimulatedTimer()
19 this.displayName = displayName
20 this.switchState = switchState
21 this.switchLatestValue = switchLatestValue
26 println("the switch with id:$id is on!")
27 this.switchLatestValue = this.switchState
28 this.switchState = "on"
29 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
30 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
33 def on(LinkedHashMap metaData) {
34 def task = timers.runAfter(metaData["delay"]) {
35 println("the switch with id:$id is on!")
36 this.switchLatestValue = this.switchState
37 this.switchState = "on"
38 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
39 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
44 println("the switch with id:$id is off!")
45 this.switchLatestValue = this.switchState
46 this.switchState = "off"
47 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
48 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
51 def off(LinkedHashMap metaData) {
52 def task = timers.runAfter(metaData["delay"]) {
53 println("the switch with id:$id is off!")
54 this.switchLatestValue = this.switchState
55 this.switchState = "off"
56 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
57 displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
62 def setValue(String value) {
63 println("the switch with id:$id is $value!")
64 this.switchLatestValue = this.switchState
65 this.switchState = value
68 def currentValue(String deviceFeature) {
69 if (deviceFeature == "switch") {
74 def latestValue(String deviceFeature) {
75 if (deviceFeature == "switch") {
76 return switchLatestValue