Adding 2 types of 'unlocked' events both for subscribers to 'lock' and 'unlock' events.
[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                         sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
64                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
65                 }
66         }
67
68         def unlock(LinkedHashMap metaData) {
69                 if (lockState != "unlocked") {
70                         def task = timers.runAfter(metaData["delay"]) {
71                                 println("the door with id:$id is locked!")
72                                 //this.lockLatestValue = this.lockState
73                                 this.lockLatestValue = "locked"
74                                 this.lockState = "locked"
75                                 this.currentLock = "locked"
76                                 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
77                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78                                 sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
79                                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
80                         }
81                 }
82         }
83
84         //By Model Checker
85         def setValue(String value) {
86                 println("the door with id:$id is $value!")
87                 this.lockLatestValue = this.lockState
88                 this.lockState = value
89                 this.currentLock = value
90         }
91         
92         def currentValue(String deviceFeature) {
93                 if (deviceFeature == "lock") {
94                         return lockState
95                 }
96         }
97
98         def latestValue(String deviceFeature) {
99                 if (deviceFeature == "lock") {
100                         return lockLatestValue
101                 }
102         }
103 }