76482b4f3262191209360437f60a9343cc8e0337
[smartthings-infrastructure.git] / Lock / Lock.groovy
1 //Create a class for lock device
2 package Lock
3 import Timer.SimulatedTimer
4
5 public class Lock {
6         private String id
7         private String label
8         private String displayName
9         private String lockState
10         private String currentLock
11         private String lockLatestValue
12         def sendEvent   
13         def timers
14
15
16         Lock(Closure sendEvent, String id, String label, String displayName, String lockState, String lockLatestValue) {
17                 this.id = id
18                 this.label = label
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()
25         }
26
27         //By Apps
28         def lock() {
29                 if (lockState != "locked") {
30                         println("the door with id:$id is locked!")
31                         this.lockLatestValue = this.lockState
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                 }
37         }
38
39         def lock(LinkedHashMap metaData) {
40                 if (lockState != "locked") {
41                         def task = timers.runAfter(metaData["delay"]) {
42                                 println("the door with id:$id is locked!")
43                                 this.lockLatestValue = this.lockState
44                                 this.lockState = "locked"
45                                 this.currentLock = "locked"
46                                 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
47                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
48                         }
49                 }
50         }
51         
52         def unlock() {
53                 if (lockState != "unlocked") {
54                         println("the door with id:$id is unlocked!")
55                         this.lockLatestValue = this.lockState
56                         this.lockState = "unlocked"
57                         this.currentLock = "unlocked"
58                         sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
59                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                 }
61         }
62
63         def unlock(LinkedHashMap metaData) {
64                 if (lockState != "unlocked") {
65                         def task = timers.runAfter(metaData["delay"]) {
66                                 println("the door with id:$id is locked!")
67                                 this.lockLatestValue = this.lockState
68                                 this.lockState = "locked"
69                                 this.currentLock = "locked"
70                                 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
71                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
72                         }
73                 }
74         }
75
76         //By Model Checker
77         def setValue(String value) {
78                 println("the door with id:$id is $value!")
79                 this.lockLatestValue = this.lockState
80                 this.lockState = value
81                 this.currentLock = value
82         }
83         
84         def currentValue(String deviceFeature) {
85                 if (deviceFeature == "lock") {
86                         return lockState
87                 }
88         }
89
90         def latestValue(String deviceFeature) {
91                 if (deviceFeature == "lock") {
92                         return lockLatestValue
93                 }
94         }
95 }