1 //Create a class for relay switch device
3 import Timer.SimulatedTimer
5 public class RelaySwitches {
11 //If we have only one device
12 private String id = "relaySwitchID0"
13 private String label = "relaySwitch0"
14 private String displayName = "relaySwitch0"
15 private String switchState = "off"
16 private String currentSwitch = "off"
17 private String switchLatestValue = "off"
19 RelaySwitches(Closure sendEvent, int deviceNumbers, boolean init) {
20 this.sendEvent = sendEvent
21 this.timers = new SimulatedTimer()
22 this.deviceNumbers = deviceNumbers
23 this.relaySwitches = []
26 this.switchState = "off"
27 this.currentSwitch = "off"
28 this.switchLatestValue = "off"
30 this.switchState = "on"
31 this.currentSwitch = "on"
32 this.switchLatestValue = "on"
34 relaySwitches.add(new RelaySwitch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.switchLatestValue))
37 //Methods for closures
38 def count(Closure Input) {
39 relaySwitches.count(Input)
44 def each(Closure Input) {
45 relaySwitches.each(Input)
47 def find(Closure Input) {
48 relaySwitches.find(Input)
50 def sort(Closure Input) {
51 relaySwitches.sort(Input)
53 def collect(Closure Input) {
54 relaySwitches.collect(Input)
59 if (switchState != "on") {
60 switchLatestValue = "on"
67 def on(LinkedHashMap metaData) {
68 if (switchState != "on") {
69 def task = timers.runAfter(metaData["delay"]) {
70 switchLatestValue = "on"
79 if (switchState != "off") {
80 switchLatestValue = "off"
83 relaySwitches[0].off()
87 def off(LinkedHashMap metaData) {
88 if (switchState != "off") {
89 def task = timers.runAfter(metaData["delay"]) {
90 switchLatestValue = "off"
93 relaySwitches[0].off()
99 def setValue(LinkedHashMap eventDataMap) {
100 if (eventDataMap["value"] != relaySwitches[0].switchState) {
101 this.switchState = eventDataMap["value"]
102 this.switchLatestValue = eventDataMap["value"]
103 relaySwitches[0].setValue(eventDataMap["value"])
104 sendEvent(eventDataMap)
109 def currentValue(String deviceFeature) {
110 relaySwitches[0].currentValue(deviceFeature)
113 def latestValue(String deviceFeature) {
114 relaySwitches[0].latestValue(deviceFeature)