1 //Create a class for switch level
3 import Timer.SimulatedTimer
5 public class SwitchLevel {
8 private String displayName
9 private String switchState
10 private String currentSwitch
12 private int currentLevel
14 private int currentRate
16 private int currentHue
17 private int saturation
18 private int currentSaturation
19 private String switchLatestValue
24 SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, int hue, int saturation, String switchState, String switchLatestValue) {
25 this.sendEvent = sendEvent
26 this.timers = new SimulatedTimer()
29 this.displayName = displayName
31 this.currentLevel = level
33 this.currentRate = level
36 this.saturation = saturation
37 this.currentSaturation = saturation
38 this.switchState = switchState
39 this.currentSwitch = switchState
40 this.switchLatestValue = switchLatestValue
44 def setColor(LinkedHashMap metaData) {
45 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
46 this.level = metaData["level"]
47 this.currentLevel = metaData["level"]
48 this.rate = metaData["level"]
49 this.currentRate = metaData["level"]
50 this.hue = metaData["hue"]
51 this.currentHue = metaData["hue"]
52 this.saturation = metaData["saturation"]
53 this.currentSaturation = metaData["saturation"]
54 println("the switch with id:$id is setted to level $level and hue to $hue and saturation to $saturation!")
55 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
56 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
57 sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
58 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59 sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
60 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64 def setLevel(String level) {
65 def newLevel = level.toInteger()
69 def setLevel(int level) {
70 if (this.level != level) {
71 println("the switch with id:$id is setted to level $level!")
73 this.currentLevel = level
75 this.currentRate = level
76 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
77 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
81 def setLevel(long level) {
82 if (this.level != level) {
83 println("the switch with id:$id is setted to level $level!")
85 this.currentLevel = level
87 this.currentRate = level
88 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
89 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
94 if (this.switchState != "on") {
95 println("the switch with id:$id is on!")
96 this.switchLatestValue = "on"
97 this.switchState = "on"
98 this.currentSwitch = "on"
99 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
100 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
104 def on(LinkedHashMap metaData) {
105 if (this.switchState != "on") {
106 def task = timers.runAfter(metaData["delay"]) {
107 println("the switch with id:$id is on!")
108 this.switchLatestValue = "on"
109 this.switchState = "on"
110 this.currentSwitch = "on"
111 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
112 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
118 if (this.switchState != "off") {
119 println("the switch with id:$id is off!")
120 this.switchLatestValue = "off"
121 this.switchState = "off"
122 this.currentSwitch = "off"
123 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
124 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
128 def off(LinkedHashMap metaData) {
129 if (this.switchState != "off") {
130 def task = timers.runAfter(metaData["delay"]) {
131 println("the switch with id:$id is off!")
132 this.switchLatestValue = "off"
133 this.switchState = "off"
134 this.currentSwitch = "off"
135 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
136 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
142 def setValue(String value, String name) {
143 if (name == "switch") {
144 println("the switch with id:$id is $value!")
145 this.switchLatestValue = value
146 this.switchState = value
147 this.currentSwitch = value
148 } else if (name == "level") {
149 println("the switch with id:$id is setted to level $value!")
150 this.level = value.toInteger()
151 this.currentLevel = value.toInteger()
152 this.rate = value.toInteger()
153 this.currentRate = value.toInteger()
158 def currentValue(String deviceFeature) {
159 if (deviceFeature == "level") {
161 } else if (deviceFeature == "switch") {
166 def latestValue(String deviceFeature) {
167 if (deviceFeature == "level") {
169 } else if (deviceFeature == "switch") {