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
18 private int saturation = 70
19 private String switchState = "on"
20 private String currentSwitch = "on"
21 private String switchLatestValue = "on"
23 SwitchLevels(Closure sendEvent, int deviceNumbers, boolean init) {
24 this.sendEvent = sendEvent
25 this.timers = new SimulatedTimer()
26 this.deviceNumbers = deviceNumbers
27 this.switchLevels = []
34 this.switchState = "off"
35 this.currentSwitch = "off"
36 this.switchLatestValue = "off"
42 this.switchState = "on"
43 this.currentSwitch = "on"
44 this.switchLatestValue = "on"
46 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
49 //Methods for closures
50 def count(Closure Input) {
51 switchLevels.count(Input)
56 def each(Closure Input) {
57 switchLevels.each(Input)
59 def find(Closure Input) {
60 switchLevels.find(Input)
62 def sort(Closure Input) {
63 switchLevels.sort(Input)
65 def collect(Closure Input) {
66 switchLevels.collect(Input)
70 def setColor(LinkedHashMap metaData) {
71 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
72 this.level = metaData["level"]
73 this.rate = metaData["level"]
74 this.hue = metaData["hue"]
75 this.saturation = metaData["saturation"]
76 switchLevels[0].setColor(metaData)
80 def setLevel(int level) {
81 if (this.level != level) {
82 switchLevels[0].setLevel(level)
88 def setLevel(long level) {
89 if (this.level != level) {
90 switchLevels[0].setLevel(level)
97 switchLatestValue = "on"
103 def on(LinkedHashMap metaData) {
104 def task = timers.runAfter(metaData["delay"]) {
105 switchLatestValue = "on"
113 switchLatestValue = "off"
115 currentSwitch = "off"
116 switchLevels[0].off()
119 def off(LinkedHashMap metaData) {
120 def task = timers.runAfter(metaData["delay"]) {
121 switchLatestValue = "off"
123 currentSwitch = "off"
124 switchLevels[0].off()
129 def setValue(LinkedHashMap eventDataMap) {
130 if (eventDataMap["name"] == "switch") {
131 if (eventDataMap["value"] != switchLevels[0].switchState) {
132 this.switchState = eventDataMap["value"]
133 this.switchLatestValue = eventDataMap["value"]
134 this.currentSwitch = eventDataMap["value"]
135 switchLevels[0].setValue(eventDataMap["value"], "switch")
136 sendEvent(eventDataMap)
138 } else if (eventDataMap["name"] == "level") {
139 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
140 this.level = eventDataMap["value"].toInteger()
141 this.rate = eventDataMap["value"].toInteger()
142 switchLevels[0].setValue(eventDataMap["value"], "level")
143 sendEvent(eventDataMap)
148 def currentValue(String deviceFeature) {
149 switchLevels[0].currentValue(deviceFeature)
152 def latestValue(String deviceFeature) {
153 switchLevels[0].latestValue(deviceFeature)