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
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
29 this.saturation = saturation
30 this.switchState = switchState
31 this.currentSwitch = switchState
32 this.switchLatestValue = switchLatestValue
36 def setColor(LinkedHashMap metaData) {
37 if ((this.level != metaData["level"]) || (this.hue != metaData["hue"]) || (this.saturation != metaData["saturation"])) {
38 this.level = 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(int level) {
53 if (this.level != level) {
54 println("the switch with id:$id is setted to level $level!")
57 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
58 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
62 def setLevel(long level) {
63 if (this.level != level) {
64 println("the switch with id:$id is setted to level $level!")
67 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
68 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
73 if (this.switchState != "on") {
74 println("the switch with id:$id is on!")
75 this.switchLatestValue = "on"
76 this.switchState = "on"
77 this.currentSwitch = "on"
78 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
79 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
83 def on(LinkedHashMap metaData) {
84 if (this.switchState != "on") {
85 def task = timers.runAfter(metaData["delay"]) {
86 println("the switch with id:$id is on!")
87 this.switchLatestValue = "on"
88 this.switchState = "on"
89 this.currentSwitch = "on"
90 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
91 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
97 if (this.switchState != "off") {
98 println("the switch with id:$id is off!")
99 this.switchLatestValue = "off"
100 this.switchState = "off"
101 this.currentSwitch = "off"
102 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
103 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
107 def off(LinkedHashMap metaData) {
108 if (this.switchState != "off") {
109 def task = timers.runAfter(metaData["delay"]) {
110 println("the switch with id:$id is off!")
111 this.switchLatestValue = "off"
112 this.switchState = "off"
113 this.currentSwitch = "off"
114 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
115 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
121 def setValue(String value, String name) {
122 if (name == "switch") {
123 println("the switch with id:$id is $value!")
124 this.switchLatestValue = value
125 this.switchState = value
126 this.currentSwitch = value
127 } else if (name == "level") {
128 println("the switch with id:$id is setted to level $value!")
129 this.level = value.toInteger()
130 this.rate = value.toInteger()
135 def currentValue(String deviceFeature) {
136 if (deviceFeature == "level") {
138 } else if (deviceFeature == "switch") {
143 def latestValue(String deviceFeature) {
144 if (deviceFeature == "level") {
146 } else if (deviceFeature == "switch") {