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 evtOn = [[name: "switch", value: "on", deviceId: "switchID0", descriptionText: "",
31 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
32 def evtOff = [[name: "switch", value: "off", deviceId: "switchID0", descriptionText: "",
33 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']]
34 def init = Verify.getInt(0,4)
36 if (init == 0) {//return empty set
38 } else if (init == 1) {//send one open event
43 } else if (init == 2) {//send two open events
51 } else if (init == 3) {//send one closed event
56 } else if (init == 4) {//send two closed events
68 def setLevel(int level) {
69 if (this.currentLevel != level) {
70 println("the switch with id:$id is setted to level $level!")
71 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) {
127 println("the switch with id:$id is $value!")
128 this.switchLatestValue = value
129 this.switchState = value
130 this.currentSwitch = value
133 def currentValue(String deviceFeature) {
134 if (deviceFeature == "switch") {
139 def latestValue(String deviceFeature) {
140 if (deviceFeature == "switch") {
141 return switchLatestValue