1 //Create a class for switch device
3 import Timer.SimulatedTimer
5 public class Switches {
11 //If we have only one device
12 private String id = "switchID0"
13 private String label = "switch0"
14 private String displayName = "switch0"
15 private String switchState = "off"
16 private String currentSwitch = "off"
17 private int currentLevel = 50
18 private String switchLatestValue = "off"
20 Switches(Closure sendEvent, int deviceNumbers) {
21 this.sendEvent = sendEvent
22 this.timers = new SimulatedTimer()
23 this.deviceNumbers = deviceNumbers
26 switches.add(new Switch(sendEvent, id, label, displayName, this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
29 //Methods for closures
30 def count(Closure Input) {
36 def each(Closure Input) {
39 def find(Closure Input) {
42 def collect(Closure Input) {
43 switches.collect(Input)
47 def setLevel(int level) {
48 switches[0].setLevel(level)
54 switchLatestValue = switchState
59 def on(LinkedHashMap metaData) {
60 def task = timers.runAfter(metaData["delay"]) {
62 switchLatestValue = switchState
70 switchLatestValue = switchState
75 def off(LinkedHashMap metaData) {
76 def task = timers.runAfter(metaData["delay"]) {
78 switchLatestValue = switchState
85 def setValue(LinkedHashMap eventDataMap) {
86 if (eventDataMap["value"] != switches[0].switchState) {
87 switches[0].setValue(eventDataMap["value"])
88 this.switchState = switches[0].switchState
89 this.switchLatestValue = switches[0].switchLatestValue
90 sendEvent(eventDataMap)
95 def currentValue(String deviceFeature) {
96 switches[0].currentValue(deviceFeature)
99 def latestValue(String deviceFeature) {
100 switches[0].latestValue(deviceFeature)