1 //Create a class for switch device
3 import Timer.SimulatedTimer
5 public class Switches {
11 //If we have only one device
13 private String label = "switch"
14 private String displayName = "switch"
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
25 for (int i = 0;i < deviceNumbers;i++) {
26 switches.add(new Switch(sendEvent, i+40, label+i.toString(), displayName+i.toString(), this.switchState, this.currentSwitch, this.currentLevel, this.switchLatestValue))
30 //Methods for closures
31 def count(Closure Input) {
37 def each(Closure Input) {
42 def setLevel(int level) {
43 switches*.setLevel(level)
50 def on(LinkedHashMap metaData) {
51 def task = timers.runAfter(metaData["delay"]) {
60 def off(LinkedHashMap metaData) {
61 def task = timers.runAfter(metaData["delay"]) {
67 def setValue(LinkedHashMap eventDataMap) {
68 switches[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
69 if (deviceNumbers == 1)
70 this.switchState = switches[eventDataMap["deviceId"]].switchState
71 this.switchLatestValue = switches[eventDataMap["deviceId"]].switchLatestValue
72 sendEvent(eventDataMap)
76 def currentValue(String deviceFeature) {
77 if (deviceNumbers == 1)
78 switches[0].currentValue(deviceFeature)
80 switches*.currentValue(deviceFeature)
83 def latestValue(String deviceFeature) {
84 if (deviceNumbers == 1)
85 switches[0].latestValue(deviceFeature)
87 switches*.latestValue(deviceFeature)