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
16 private int currentLevel = 50
18 private int currentRate = 50
20 private int currentHue = 30
21 private int saturation = 70
22 private int currentSaturation = 70
23 private String switchState = "on"
24 private String currentSwitch = "on"
25 private String switchLatestValue = "on"
27 SwitchLevels(Closure sendEvent, int deviceNumbers, boolean init) {
28 this.sendEvent = sendEvent
29 this.timers = new SimulatedTimer()
30 this.deviceNumbers = deviceNumbers
31 this.switchLevels = []
35 this.currentLevel = 50
41 this.currentSaturation = 70
42 this.switchState = "off"
43 this.currentSwitch = "off"
44 this.switchLatestValue = "off"
47 this.currentLevel = 60
53 this.currentSaturation = 90
54 this.switchState = "on"
55 this.currentSwitch = "on"
56 this.switchLatestValue = "on"
58 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
61 //Methods for closures
62 def count(Closure Input) {
63 switchLevels.count(Input)
68 def each(Closure Input) {
69 switchLevels.each(Input)
71 def find(Closure Input) {
72 switchLevels.find(Input)
74 def sort(Closure Input) {
75 switchLevels.sort(Input)
77 def collect(Closure Input) {
78 switchLevels.collect(Input)
82 def setColor(LinkedHashMap metaData) {
83 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
84 this.level = metaData["level"]
85 this.currentLevel = metaData["level"]
86 this.rate = metaData["level"]
87 this.currentRate = metaData["level"]
88 this.hue = metaData["hue"]
89 this.currentHue = metaData["hue"]
90 this.saturation = metaData["saturation"]
91 this.currentSaturation = metaData["saturation"]
92 switchLevels[0].setColor(metaData)
96 def setLevel(String level) {
97 def newLevel = level.toInteger()
101 def setLevel(int level) {
102 if (this.level != level) {
103 switchLevels[0].setLevel(level)
105 this.currentLevel = level
107 this.currentRate = level
111 def setLevel(long level) {
112 if (this.level != level) {
113 switchLevels[0].setLevel(level)
116 this.currentLevel = level
117 this.currentRate = level
122 switchLatestValue = "on"
128 def on(LinkedHashMap metaData) {
129 def task = timers.runAfter(metaData["delay"]) {
130 switchLatestValue = "on"
138 switchLatestValue = "off"
140 currentSwitch = "off"
141 switchLevels[0].off()
144 def off(LinkedHashMap metaData) {
145 def task = timers.runAfter(metaData["delay"]) {
146 switchLatestValue = "off"
148 currentSwitch = "off"
149 switchLevels[0].off()
154 def setValue(LinkedHashMap eventDataMap) {
155 if (eventDataMap["name"] == "switch") {
156 if (eventDataMap["value"] != switchLevels[0].switchState) {
157 this.switchState = eventDataMap["value"]
158 this.switchLatestValue = eventDataMap["value"]
159 this.currentSwitch = eventDataMap["value"]
160 switchLevels[0].setValue(eventDataMap["value"], "switch")
161 sendEvent(eventDataMap)
163 } else if (eventDataMap["name"] == "level") {
164 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
165 this.level = eventDataMap["value"].toInteger()
166 this.currentLevel = eventDataMap["value"].toInteger()
167 this.currentRate = eventDataMap["value"].toInteger()
168 this.rate = eventDataMap["value"].toInteger()
169 switchLevels[0].setValue(eventDataMap["value"], "level")
170 sendEvent(eventDataMap)
175 def currentValue(String deviceFeature) {
176 switchLevels[0].currentValue(deviceFeature)
179 def latestValue(String deviceFeature) {
180 switchLevels[0].latestValue(deviceFeature)