//Create a class for switch device package Switch import Timer.SimulatedTimer public class Switch { private int id private String label private String displayName private String switchState private String currentSwitch private int currentLevel private String switchLatestValue def sendEvent def timers Switch(Closure sendEvent, int id, String label, String displayName, String switchState, String currentSwitch, int currentLevel, String switchLatestValue) { this.sendEvent = sendEvent this.timers = new SimulatedTimer() this.currentSwitch = currentSwitch this.currentLevel = currentLevel this.id = id this.label = label this.displayName = displayName this.switchState = switchState this.switchLatestValue = switchLatestValue } //By Apps def setLevel(int level) { println("the switch with id:$id is setted to level $level!") this.currentLevel = level } def on() { println("the switch with id:$id is on!") this.switchLatestValue = this.switchState this.switchState = "on" this.currentSwitch = "on" sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } def on(LinkedHashMap metaData) { def task = timers.runAfter(metaData["delay"]) { println("the switch with id:$id is on!") this.switchLatestValue = this.switchState this.switchState = "on" this.currentSwitch = "on" sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } } def off() { println("the switch with id:$id is off!") this.switchLatestValue = this.switchState this.switchState = "off" this.currentSwitch = "off" sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } def off(LinkedHashMap metaData) { def task = timers.runAfter(metaData["delay"]) { println("the switch with id:$id is off!") this.switchLatestValue = this.switchState this.switchState = "off" this.currentSwitch = "off" sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", displayed: true, linkText: "", isStateChange: false, unit: "", data: []]) } } //By Model Checker def setValue(String value) { println("the switch with id:$id is $value!") this.switchLatestValue = this.switchState this.switchState = value this.currentSwitch = value } def currentValue(String deviceFeature) { if (deviceFeature == "switch") { return switchState } } def latestValue(String deviceFeature) { if (deviceFeature == "switch") { return switchLatestValue } } }