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 sort(Closure Input) {
44 switchLevels.sort(Input)
46 def collect(Closure Input) {
47 switchLevels.collect(Input)
51 def setLevel(int level) {
52 if (this.level != level) {
53 switchLevels[0].setLevel(level)
60 switchLatestValue = "on"
66 def on(LinkedHashMap metaData) {
67 def task = timers.runAfter(metaData["delay"]) {
68 switchLatestValue = "on"
76 switchLatestValue = "off"
82 def off(LinkedHashMap metaData) {
83 def task = timers.runAfter(metaData["delay"]) {
84 switchLatestValue = "off"
92 def setValue(LinkedHashMap eventDataMap) {
93 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
94 this.level = eventDataMap["value"].toInteger()
95 this.rate = eventDataMap["value"].toInteger()
96 switchLevels[0].setValue(eventDataMap["value"])
97 sendEvent(eventDataMap)