1 //Create a class for relay switch device
3 import Timer.SimulatedTimer
5 public class RelaySwitch {
8 private String displayName
9 private String switchState
10 private String currentSwitch
11 private String switchLatestValue
16 RelaySwitch(Closure sendEvent, String id, String label, String displayName, String switchState, String currentSwitch, String switchLatestValue) {
17 this.sendEvent = sendEvent
18 this.timers = new SimulatedTimer()
19 this.currentSwitch = currentSwitch
22 this.displayName = displayName
23 this.switchState = switchState
24 this.switchLatestValue = switchLatestValue
29 if (switchState != "on") {
30 println("the relay switch with id:$id is on!")
31 this.switchLatestValue = "on"
32 this.switchState = "on"
33 this.currentSwitch = "on"
34 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
35 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
36 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
37 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
41 def on(LinkedHashMap metaData) {
42 if (switchState != "on") {
43 def task = timers.runAfter(metaData["delay"]) {
44 println("the relay switch with id:$id is on!")
45 this.switchLatestValue = "on"
46 this.switchState = "on"
47 this.currentSwitch = "on"
48 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
49 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50 sendEvent([name: "switch.on", value: "on", deviceId: this.id, descriptionText: "",
51 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
57 if (switchState != "off") {
58 println("the relay switch with id:$id is off!")
59 this.switchLatestValue = "off"
60 this.switchState = "off"
61 this.currentSwitch = "off"
62 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
63 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
65 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
69 def off(LinkedHashMap metaData) {
70 if (switchState != "off") {
71 def task = timers.runAfter(metaData["delay"]) {
72 println("the relay switch with id:$id is off!")
73 this.switchLatestValue = "off"
74 this.switchState = "off"
75 this.currentSwitch = "off"
76 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
77 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78 sendEvent([name: "switch.off", value: "off", deviceId: this.id, descriptionText: "",
79 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
85 def setValue(String value) {
86 println("the relay switch with id:$id is $value!")
87 this.switchLatestValue = value
88 this.switchState = value
89 this.currentSwitch = value
92 def currentValue(String deviceFeature) {
93 if (deviceFeature == "switch") {
98 def latestValue(String deviceFeature) {
99 if (deviceFeature == "switch") {
100 return switchLatestValue