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 String switchLatestValue
23 SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, int hue, int saturation, String switchState, String switchLatestValue) {
24 this.sendEvent = sendEvent
25 this.timers = new SimulatedTimer()
28 this.displayName = displayName
30 this.currentLevel = level
32 this.currentRate = level
35 this.saturation = saturation
36 this.switchState = switchState
37 this.currentSwitch = switchState
38 this.switchLatestValue = switchLatestValue
42 def setColor(LinkedHashMap metaData) {
43 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
44 this.level = metaData["level"]
45 this.currentLevel = metaData["level"]
46 this.rate = metaData["level"]
47 this.currentRate = metaData["level"]
48 this.hue = metaData["hue"]
49 this.currentHue = metaData["hue"]
50 this.saturation = metaData["saturation"]
51 println("the switch with id:$id is setted to level $level and hue to $hue and saturation to $saturation!")
52 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
53 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
54 sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
55 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
56 sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
57 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
61 def setLevel(String level) {
62 def newLevel = level.toInteger()
66 def setLevel(int level) {
67 if (this.level != level) {
68 println("the switch with id:$id is setted to level $level!")
70 this.currentLevel = level
72 this.currentRate = level
73 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
74 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78 def setLevel(long level) {
79 if (this.level != level) {
80 println("the switch with id:$id is setted to level $level!")
82 this.currentLevel = level
84 this.currentRate = level
85 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
86 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
91 if (this.switchState != "on") {
92 println("the switch with id:$id is on!")
93 this.switchLatestValue = "on"
94 this.switchState = "on"
95 this.currentSwitch = "on"
96 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
97 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
101 def on(LinkedHashMap metaData) {
102 if (this.switchState != "on") {
103 def task = timers.runAfter(metaData["delay"]) {
104 println("the switch with id:$id is on!")
105 this.switchLatestValue = "on"
106 this.switchState = "on"
107 this.currentSwitch = "on"
108 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
109 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
115 if (this.switchState != "off") {
116 println("the switch with id:$id is off!")
117 this.switchLatestValue = "off"
118 this.switchState = "off"
119 this.currentSwitch = "off"
120 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
121 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
125 def off(LinkedHashMap metaData) {
126 if (this.switchState != "off") {
127 def task = timers.runAfter(metaData["delay"]) {
128 println("the switch with id:$id is off!")
129 this.switchLatestValue = "off"
130 this.switchState = "off"
131 this.currentSwitch = "off"
132 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
133 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
139 def setValue(String value, String name) {
140 if (name == "switch") {
141 println("the switch with id:$id is $value!")
142 this.switchLatestValue = value
143 this.switchState = value
144 this.currentSwitch = value
145 } else if (name == "level") {
146 println("the switch with id:$id is setted to level $value!")
147 this.level = value.toInteger()
148 this.currentLevel = value.toInteger()
149 this.rate = value.toInteger()
150 this.currentRate = value.toInteger()
155 def currentValue(String deviceFeature) {
156 if (deviceFeature == "level") {
158 } else if (deviceFeature == "switch") {
163 def latestValue(String deviceFeature) {
164 if (deviceFeature == "level") {
166 } else if (deviceFeature == "switch") {