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