cc4b466ef22b96ce16409f865f750bd00c18b5fd
[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 int 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, int 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                 println("the door with id:$id is locked!")
30                 this.lockLatestValue = this.lockState
31                 this.lockState = "locked"
32                 this.currentLock = "locked"
33                 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
34                           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
35         }
36
37         def lock(LinkedHashMap metaData) {
38                 def task = timers.runAfter(metaData["delay"]) {
39                         println("the door with id:$id is locked!")
40                         this.lockLatestValue = this.lockState
41                         this.lockState = "locked"
42                         this.currentLock = "locked"
43                         sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
44                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
45                 }
46         }
47         
48         def unlock() {
49                 println("the door with id:$id is unlocked!")
50                 this.lockLatestValue = this.lockState
51                 this.lockState = "unlocked"
52                 this.currentLock = "unlocked"
53                 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
54                           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
55         }
56
57         def unlock(LinkedHashMap metaData) {
58                 def task = timers.runAfter(metaData["delay"]) {
59                         println("the door with id:$id is locked!")
60                         this.lockLatestValue = this.lockState
61                         this.lockState = "locked"
62                         this.currentLock = "locked"
63                         sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
64                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
65                 }
66         }
67
68         //By Model Checker
69         def setValue(String value) {
70                 println("the door with id:$id is $value!")
71                 this.lockLatestValue = this.lockState
72                 this.lockState = value
73                 this.currentLock = value
74         }
75         
76         def currentValue(String deviceFeature) {
77                 if (deviceFeature == "lock") {
78                         return lockState
79                 }
80         }
81
82         def latestValue(String deviceFeature) {
83                 if (deviceFeature == "lock") {
84                         return lockLatestValue
85                 }
86         }
87 }