1 //Create a class for switch level
3 import Timer.SimulatedTimer
5 public class SwitchLevels {
11 //If we have only one device
12 private String id = "switchLevelID0"
13 private String label = "switchLevel0"
14 private String displayName = "switchLevel0"
15 private int level = 50
17 private String switchState = "off"
18 private String currentSwitch = "off"
19 private String switchLatestValue = "off"
21 SwitchLevels(Closure sendEvent, int deviceNumbers) {
22 this.sendEvent = sendEvent
23 this.timers = new SimulatedTimer()
24 this.deviceNumbers = deviceNumbers
25 this.switchLevels = []
27 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.switchState, this.switchLatestValue))
30 //Methods for closures
31 def count(Closure Input) {
32 switchLevels.count(Input)
37 def each(Closure Input) {
38 switchLevels.each(Input)
40 def find(Closure Input) {
41 switchLevels.find(Input)
43 def collect(Closure Input) {
44 switchLevels.collect(Input)
48 def setLevel(int level) {
49 if (this.level != level) {
50 switchLevels[0].setLevel(level)
58 switchLatestValue = switchState
63 def on(LinkedHashMap metaData) {
64 def task = timers.runAfter(metaData["delay"]) {
66 switchLatestValue = switchState
74 switchLatestValue = switchState
79 def off(LinkedHashMap metaData) {
80 def task = timers.runAfter(metaData["delay"]) {
82 switchLatestValue = switchState
89 def setValue(LinkedHashMap eventDataMap) {
90 if (eventDataMap["value"] != switchLevels[0].level) {
91 switchLevels[0].setValue(eventDataMap["value"])
92 this.level = switchLevels[0].level
93 this.rate = switchLevels[0].level
94 sendEvent(eventDataMap)