1 //Create a class for switch device
3 import Timer.SimulatedTimer
8 private String displayName
9 private String switchState
10 private String currentSwitch
11 private int currentLevel
12 private String switchLatestValue
17 Switch(Closure sendEvent, String id, String label, String displayName, String switchState, String currentSwitch, int currentLevel, String switchLatestValue) {
18 this.sendEvent = sendEvent
19 this.timers = new SimulatedTimer()
20 this.currentSwitch = currentSwitch
21 this.currentLevel = currentLevel
24 this.displayName = displayName
25 this.switchState = switchState
26 this.switchLatestValue = switchLatestValue
30 def setLevel(int level) {
31 if (this.currentLevel != level) {
32 println("the switch with id:$id is setted to level $level!")
33 this.currentLevel = level
34 sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "",
35 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
40 if (this.switchState != "on") {
41 println("the switch with id:$id is on!")
42 this.switchLatestValue = "on"
43 this.switchState = "on"
44 this.currentSwitch = "on"
45 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
46 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50 def on(LinkedHashMap metaData) {
51 if (this.switchState != "on") {
52 def task = timers.runAfter(metaData["delay"]) {
53 println("the switch with id:$id is on!")
54 this.switchLatestValue = "on"
55 this.switchState = "on"
56 this.currentSwitch = "on"
57 sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "",
58 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64 if (this.switchState != "off") {
65 println("the switch with id:$id is off!")
66 this.switchLatestValue = "off"
67 this.switchState = "off"
68 this.currentSwitch = "off"
69 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
70 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
74 def off(LinkedHashMap metaData) {
75 if (this.switchState != "off") {
76 def task = timers.runAfter(metaData["delay"]) {
77 println("the switch with id:$id is off!")
78 this.switchLatestValue = "off"
79 this.switchState = "off"
80 this.currentSwitch = "off"
81 sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "",
82 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
88 def setValue(String value) {
89 println("the switch with id:$id is $value!")
90 this.switchLatestValue = value
91 this.switchState = value
92 this.currentSwitch = value
95 def currentValue(String deviceFeature) {
96 if (deviceFeature == "switch") {
101 def latestValue(String deviceFeature) {
102 if (deviceFeature == "switch") {
103 return switchLatestValue