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
11 private int currentLevel
14 private int saturation
15 private String switchLatestValue
20 SwitchLevel(Closure sendEvent, String id, String label, String displayName, int level, int hue, int saturation, String switchState, String switchLatestValue) {
21 this.sendEvent = sendEvent
22 this.timers = new SimulatedTimer()
25 this.displayName = displayName
26 this.currentLevel = level
29 this.saturation = saturation
30 this.switchState = switchState
31 this.currentSwitch = switchState
32 this.switchLatestValue = switchLatestValue
36 def setColor(LinkedHashMap metaData) {
37 if ((this.currentLevel != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
38 this.currentLevel = metaData["level"]
39 this.rate = metaData["level"]
40 this.hue = metaData["hue"]
41 this.saturation = metaData["saturation"]
42 println("the switch with id:$id is setted to level $level and hue to $hue and saturation to $saturation!")
43 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
44 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
45 sendEvent([name: "hue", value: "$hue", deviceId: this.id, descriptionText: "",
46 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
47 sendEvent([name: "saturation", value: "$saturation", deviceId: this.id, descriptionText: "",
48 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
52 def setLevel(String level) {
53 def newLevel = level.toInteger()
57 def setLevel(int level) {
58 if (this.currentLevel != level) {
59 println("the switch with id:$id is setted to level $level!")
60 this.currentLevel = level
62 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
63 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
67 def setLevel(long level) {
68 if (this.currentLevel != level) {
69 println("the switch with id:$id is setted to level $level!")
70 this.currentLevel = level
72 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
73 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78 if (this.switchState != "on") {
79 println("the switch with id:$id is on!")
80 this.switchLatestValue = "on"
81 this.switchState = "on"
82 this.currentSwitch = "on"
83 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
84 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
88 def on(LinkedHashMap metaData) {
89 if (this.switchState != "on") {
90 def task = timers.runAfter(metaData["delay"]) {
91 println("the switch with id:$id is on!")
92 this.switchLatestValue = "on"
93 this.switchState = "on"
94 this.currentSwitch = "on"
95 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
96 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
102 if (this.switchState != "off") {
103 println("the switch with id:$id is off!")
104 this.switchLatestValue = "off"
105 this.switchState = "off"
106 this.currentSwitch = "off"
107 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
108 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
112 def off(LinkedHashMap metaData) {
113 if (this.switchState != "off") {
114 def task = timers.runAfter(metaData["delay"]) {
115 println("the switch with id:$id is off!")
116 this.switchLatestValue = "off"
117 this.switchState = "off"
118 this.currentSwitch = "off"
119 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
120 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
126 def setValue(String value, String name) {
127 if (name == "switch") {
128 println("the switch with id:$id is $value!")
129 this.switchLatestValue = value
130 this.switchState = value
131 this.currentSwitch = value
132 } else if (name == "level") {
133 println("the switch with id:$id is setted to level $value!")
134 this.currentLevel = value.toInteger()
135 this.rate = value.toInteger()
140 def currentValue(String deviceFeature) {
141 if (deviceFeature == "level") {
143 } else if (deviceFeature == "switch") {
148 def latestValue(String deviceFeature) {
149 if (deviceFeature == "level") {
151 } else if (deviceFeature == "switch") {