//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<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
- 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()
}
+
}