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"}'])
39 def on(LinkedHashMap metaData) {
40 if (switchState != "on") {
41 def task = timers.runAfter(metaData["delay"]) {
42 println("the relay switch with id:$id is on!")
43 this.switchLatestValue = "on"
44 this.switchState = "on"
45 this.currentSwitch = "on"
46 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
47 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
53 if (switchState != "off") {
54 println("the relay switch with id:$id is off!")
55 this.switchLatestValue = "off"
56 this.switchState = "off"
57 this.currentSwitch = "off"
58 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
59 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
63 def off(LinkedHashMap metaData) {
64 if (switchState != "off") {
65 def task = timers.runAfter(metaData["delay"]) {
66 println("the relay switch with id:$id is off!")
67 this.switchLatestValue = "off"
68 this.switchState = "off"
69 this.currentSwitch = "off"
70 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
71 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
77 def setValue(String value) {
78 println("the relay switch with id:$id is $value!")
79 this.switchLatestValue = value
80 this.switchState = value
81 this.currentSwitch = value
84 def currentValue(String deviceFeature) {
85 if (deviceFeature == "switch") {
90 def latestValue(String deviceFeature) {
91 if (deviceFeature == "switch") {
92 return switchLatestValue