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 String switchState = "on"
23 private String currentSwitch = "on"
24 private String switchLatestValue = "on"
26 SwitchLevels(Closure sendEvent, int deviceNumbers, boolean init) {
27 this.sendEvent = sendEvent
28 this.timers = new SimulatedTimer()
29 this.deviceNumbers = deviceNumbers
30 this.switchLevels = []
34 this.currentLevel = 50
40 this.switchState = "off"
41 this.currentSwitch = "off"
42 this.switchLatestValue = "off"
45 this.currentLevel = 60
51 this.switchState = "on"
52 this.currentSwitch = "on"
53 this.switchLatestValue = "on"
55 switchLevels.add(new SwitchLevel(sendEvent, id, label, displayName, this.level, this.hue, this.saturation, this.switchState, this.switchLatestValue))
58 //Methods for closures
59 def count(Closure Input) {
60 switchLevels.count(Input)
65 def each(Closure Input) {
66 switchLevels.each(Input)
68 def find(Closure Input) {
69 switchLevels.find(Input)
71 def sort(Closure Input) {
72 switchLevels.sort(Input)
74 def collect(Closure Input) {
75 switchLevels.collect(Input)
79 def setColor(LinkedHashMap metaData) {
80 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
81 this.level = metaData["level"]
82 this.currentLevel = metaData["level"]
83 this.rate = metaData["level"]
84 this.currentRate = metaData["level"]
85 this.hue = metaData["hue"]
86 this.currentHue = metaData["hue"]
87 this.saturation = metaData["saturation"]
88 switchLevels[0].setColor(metaData)
92 def setLevel(String level) {
93 def newLevel = level.toInteger()
97 def setLevel(int level) {
98 if (this.level != level) {
99 switchLevels[0].setLevel(level)
101 this.currentLevel = level
103 this.currentRate = level
107 def setLevel(long level) {
108 if (this.level != level) {
109 switchLevels[0].setLevel(level)
112 this.currentLevel = level
113 this.currentRate = level
118 switchLatestValue = "on"
124 def on(LinkedHashMap metaData) {
125 def task = timers.runAfter(metaData["delay"]) {
126 switchLatestValue = "on"
134 switchLatestValue = "off"
136 currentSwitch = "off"
137 switchLevels[0].off()
140 def off(LinkedHashMap metaData) {
141 def task = timers.runAfter(metaData["delay"]) {
142 switchLatestValue = "off"
144 currentSwitch = "off"
145 switchLevels[0].off()
150 def setValue(LinkedHashMap eventDataMap) {
151 if (eventDataMap["name"] == "switch") {
152 if (eventDataMap["value"] != switchLevels[0].switchState) {
153 this.switchState = eventDataMap["value"]
154 this.switchLatestValue = eventDataMap["value"]
155 this.currentSwitch = eventDataMap["value"]
156 switchLevels[0].setValue(eventDataMap["value"], "switch")
157 sendEvent(eventDataMap)
159 } else if (eventDataMap["name"] == "level") {
160 if (eventDataMap["value"].toInteger() != switchLevels[0].level) {
161 this.level = eventDataMap["value"].toInteger()
162 this.currentLevel = eventDataMap["value"].toInteger()
163 this.currentRate = eventDataMap["value"].toInteger()
164 this.rate = eventDataMap["value"].toInteger()
165 switchLevels[0].setValue(eventDataMap["value"], "level")
166 sendEvent(eventDataMap)
171 def currentValue(String deviceFeature) {
172 switchLevels[0].currentValue(deviceFeature)
175 def latestValue(String deviceFeature) {
176 switchLevels[0].latestValue(deviceFeature)