X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=Switch%2FSwitch.groovy;h=b95bca122f2d69127e43079c8956e1eb38aabc75;hb=d0b538d93e64c63d2673796db08570953b57f947;hp=4e28c344fd9e2849f6f5eca1decc575d9c570dc6;hpb=2932def9bb947d617975235763f7338360f0e5a4;p=smartthings-infrastructure.git diff --git a/Switch/Switch.groovy b/Switch/Switch.groovy index 4e28c34..b95bca1 100644 --- a/Switch/Switch.groovy +++ b/Switch/Switch.groovy @@ -1,148 +1,67 @@ //Create a class for switch device package Switch -import Timer.SimulatedTimer +import SmartThing.SmartThing -//JPF's Verify API -import gov.nasa.jpf.vm.Verify +public class Switch extends SmartThing { + // id, label, and display name of the device + StringBuilder id = new StringBuilder() + StringBuilder label = new StringBuilder() + StringBuilder displayName = new StringBuilder() + // Features with string values + StringBuilder currentSwitch = new StringBuilder() + // Maps from features to values + HashMap deviceValuesMap = new HashMap() + // Possible values for eventsSince method + List possibleValues = new ArrayList(); + Switch(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentSwitch) { + deviceValuesMap = deviceValueSmartThing + idSmartThing = id + labelSmartThing = label + displayNameSmartThing = displayName + sendEventSmartThings = sendEvent + possibleValuesSmartThings = possibleValues -public class Switch { - private String 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, String 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 + // Initialization this.id = id this.label = label this.displayName = displayName - this.switchState = switchState - this.switchLatestValue = switchLatestValue - } - - def eventsSince() { - def evtOn = [[name: "switch", value: "on", deviceId: "switchID0", descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']] - def evtOff = [[name: "switch", value: "off", deviceId: "switchID0", descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']] - def init = Verify.getInt(0,4) - def evtToSend = [] - if (init == 0) {//return empty set - return evtToSend - } else if (init == 1) {//send one open event - evtOn.each{ - evtToSend.add(it) - } - return evtToSend - } else if (init == 2) {//send two open events - evtOn.each{ - evtToSend.add(it) - } - evtOn.each{ - evtToSend.add(it) - } - return evtToSend - } else if (init == 3) {//send one closed event - evtOff.each{ - evtToSend.add(it) - } - return evtToSend - } else if (init == 4) {//send two closed events - evtOff.each{ - evtToSend.add(it) - } - evtOff.each{ - evtToSend.add(it) - } - return evtToSend - } - } + this.currentSwitch = currentSwitch + possibleValues.add("on") + possibleValues.add("off") - //By Apps - def setLevel(int level) { - if (this.currentLevel != level) { - println("the switch with id:$id is setted to level $level!") - this.currentLevel = level - sendEvent([name: "level", value: "$level", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } + deviceValuesMap.put("switch", currentSwitch) } + // Methods to set values def on() { - if (this.switchState != "on") { - println("the switch with id:$id is on!") - this.switchLatestValue = "on" - this.switchState = "on" - this.currentSwitch = "on" - sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } + action(currentSwitch, "on", "switch") } def on(LinkedHashMap metaData) { - if (this.switchState != "on") { - def task = timers.runAfter(metaData["delay"]) { - println("the switch with id:$id is on!") - this.switchLatestValue = "on" - this.switchState = "on" - this.currentSwitch = "on" - sendEvent([name: "switch", value: "on", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } - } + on() } def off() { - if (this.switchState != "off") { - println("the switch with id:$id is off!") - this.switchLatestValue = "off" - this.switchState = "off" - this.currentSwitch = "off" - sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } + action(currentSwitch, "off", "switch") } def off(LinkedHashMap metaData) { - if (this.switchState != "off") { - def task = timers.runAfter(metaData["delay"]) { - println("the switch with id:$id is off!") - this.switchLatestValue = "off" - this.switchState = "off" - this.currentSwitch = "off" - sendEvent([name: "switch", value: "off", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } - } + off() } - //By Model Checker - def setValue(String value) { - println("the switch with id:$id is $value!") - this.switchLatestValue = value - this.switchState = value - this.currentSwitch = value - } - - def currentValue(String deviceFeature) { - if (deviceFeature == "switch") { - return switchState + def action(StringBuilder variable, String newValue, String feature) { + if (!variable.toString().equals(newValue)) { + String tmpID = id.toString() + variable.replace(0, variable.length(), newValue) + println("$feature of the light with id:$tmpID is changed to $newValue!") + sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) } } - def latestValue(String deviceFeature) { - if (deviceFeature == "switch") { - return switchLatestValue - } + // Methods to return values + def getCurrentSwitch() { + return currentSwitch.toString() } }