X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=Lock%2FLock.groovy;h=1ceac32e84a3850693bbf0b25045d7430aef608e;hb=d0b538d93e64c63d2673796db08570953b57f947;hp=1b2e472c27a7504343bc4cedb15bb246a0c8e62c;hpb=2932def9bb947d617975235763f7338360f0e5a4;p=smartthings-infrastructure.git diff --git a/Lock/Lock.groovy b/Lock/Lock.groovy index 1b2e472..1ceac32 100644 --- a/Lock/Lock.groovy +++ b/Lock/Lock.groovy @@ -1,95 +1,64 @@ //Create a class for lock device package Lock -import Timer.SimulatedTimer +import SmartThing.SmartThing -public class Lock { - private String id - private String label - private String displayName - private String lockState - private String currentLock - private String lockLatestValue - def sendEvent - def timers +public class Lock 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 currentLock = new StringBuilder() + // Maps from features to values + HashMap deviceValuesMap = new HashMap() - Lock(Closure sendEvent, String id, String label, String displayName, String lockState, String lockLatestValue) { + Lock(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentLock) { + deviceValuesMap = deviceValueSmartThing + idSmartThing = id + labelSmartThing = label + displayNameSmartThing = displayName + sendEventSmartThings = sendEvent + + // Initialization this.id = id this.label = label - this.sendEvent = sendEvent this.displayName = displayName - this.lockState = lockState - this.currentLock = lockState - this.lockLatestValue = lockLatestValue - this.timers = new SimulatedTimer() + this.currentLock = currentLock + + deviceValuesMap.put("lock", currentLock) } - //By Apps + // Methods to set values def lock() { - if (lockState != "locked") { - println("the door with id:$id is locked!") - this.lockLatestValue = "locked" - this.lockState = "locked" - this.currentLock = "locked" - sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } + action(currentLock, "locked") } def lock(LinkedHashMap metaData) { - if (lockState != "locked") { - def task = timers.runAfter(metaData["delay"]) { - println("the door with id:$id is locked!") - this.lockLatestValue = "locked" - this.lockState = "locked" - this.currentLock = "locked" - sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } - } + lock() } def unlock() { - if (lockState != "unlocked") { - println("the door with id:$id is unlocked!") - this.lockLatestValue = "unlocked" - this.lockState = "unlocked" - this.currentLock = "unlocked" - sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } + action(currentLock, "unlocked") } def unlock(LinkedHashMap metaData) { - if (lockState != "unlocked") { - def task = timers.runAfter(metaData["delay"]) { - println("the door with id:$id is locked!") - this.lockLatestValue = "unlocked" - this.lockState = "unlocked" - this.currentLock = "unlocked" - sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "", - displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) - } - } + unlock() } - //By Model Checker - def setValue(String value) { - println("the door with id:$id is $value!") - this.lockLatestValue = value - this.lockState = value - this.currentLock = value - } - - def currentValue(String deviceFeature) { - if (deviceFeature == "lock") { - return lockState + def action(StringBuilder variable, String newValue) { + if (!variable.toString().equals(newValue)) { + String tmpID = id.toString() + variable.replace(0, variable.length(), newValue) + println("Lock with id:$tmpID is changed to $newValue!") + sendEvent([name: "lock", value: newValue, deviceId: tmpID, descriptionText: "", + displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}']) } } - def latestValue(String deviceFeature) { - if (deviceFeature == "lock") { - return lockLatestValue - } + // Methods to return values + def getCurrentLock() { + return currentLock.toString() } + }