1 //Create a class for lock device
3 import Timer.SimulatedTimer
8 private String displayName
9 private String lockState
10 private String currentLock
11 private String lockLatestValue
16 Lock(Closure sendEvent, String id, String label, String displayName, String lockState, String lockLatestValue) {
19 this.sendEvent = sendEvent
20 this.displayName = displayName
21 this.lockState = lockState
22 this.currentLock = lockState
23 this.lockLatestValue = lockLatestValue
24 this.timers = new SimulatedTimer()
29 if (lockState != "locked") {
30 println("the door with id:$id is locked!")
31 this.lockLatestValue = "locked"
32 this.lockState = "locked"
33 this.currentLock = "locked"
34 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
35 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
36 sendEvent([name: "lock.locked", value: "locked", deviceId: this.id, descriptionText: "",
37 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
41 def lock(LinkedHashMap metaData) {
42 if (lockState != "locked") {
43 def task = timers.runAfter(metaData["delay"]) {
44 println("the door with id:$id is locked!")
45 this.lockLatestValue = "locked"
46 this.lockState = "locked"
47 this.currentLock = "locked"
48 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
49 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
50 sendEvent([name: "lock.locked", value: "locked", deviceId: this.id, descriptionText: "",
51 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
57 if (lockState != "unlocked") {
58 println("the door with id:$id is unlocked!")
59 this.lockLatestValue = "unlocked"
60 this.lockState = "unlocked"
61 this.currentLock = "unlocked"
62 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
63 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64 sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
65 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
66 sendEvent([name: "lock.unlocked", value: "unlocked", deviceId: this.id, descriptionText: "",
67 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
71 def unlock(LinkedHashMap metaData) {
72 if (lockState != "unlocked") {
73 def task = timers.runAfter(metaData["delay"]) {
74 println("the door with id:$id is locked!")
75 this.lockLatestValue = "unlocked"
76 this.lockState = "unlocked"
77 this.currentLock = "unlocked"
78 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
79 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
80 sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
81 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
82 sendEvent([name: "lock.unlocked", value: "unlocked", deviceId: this.id, descriptionText: "",
83 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
89 def setValue(String value) {
90 println("the door with id:$id is $value!")
91 this.lockLatestValue = value
92 this.lockState = value
93 this.currentLock = value
96 def currentValue(String deviceFeature) {
97 if (deviceFeature == "lock") {
102 def latestValue(String deviceFeature) {
103 if (deviceFeature == "lock") {
104 return lockLatestValue