Commit #6
[smartthings-infrastructure.git] / Lock / Locks.groovy
1 //Create a class for lock device
2 package Lock
3
4 public class Locks {
5         private int id
6         private String displayName
7         private String lockCurrentValue
8         private String lockLatestValue
9         def sendEvent   
10         def timers
11
12
13         Locks(Closure sendEvent, int id, String displayName, String lockCurrentValue, String lockLatestValue) {
14                 this.id = id
15                 this.sendEvent = sendEvent
16                 this.displayName = displayName
17                 this.lockCurrentValue = lockCurrentValue
18                 this.lockLatestValue = lockLatestValue
19                 this.timers = new Timer()
20         }
21
22         //By Apps
23         def lock() {
24                 println("the door with id:$id is locked!")
25                 this.lockLatestValue = this.lockCurrentValue
26                 this.lockCurrentValue = "locked"
27                 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
28                           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
29         }
30
31         def lock(LinkedHashMap metaData) {
32                 def task = timers.runAfter(metaData["delay"]) {
33                         println("the door with id:$id is locked!")
34                         this.lockLatestValue = this.lockCurrentValue
35                         this.lockCurrentValue = "locked"
36                         sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
37                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
38                 }
39         }
40         
41         def unlock() {
42                 println("the door with id:$id is unlocked!")
43                 this.lockLatestValue = this.lockCurrentValue
44                 this.lockCurrentValue = "unlocked"
45                 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
46                           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
47         }
48
49         def unlock(LinkedHashMap metaData) {
50                 def task = timers.runAfter(metaData["delay"]) {
51                         println("the door with id:$id is locked!")
52                         this.lockLatestValue = this.lockCurrentValue
53                         this.lockCurrentValue = "locked"
54                         sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
55                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
56                 }
57         }
58
59         //By Model Checker
60         def setValue(String value) {
61                 println("the door with id:$id is $value!")
62                 this.lockLatestValue = this.lockCurrentValue
63                 this.lockCurrentValue = value
64         }
65         
66         def currentValue(String deviceFeature) {
67                 if (deviceFeature == "lock") {
68                         return lockCurrentValue
69                 }
70         }
71
72         def latestValue(String deviceFeature) {
73                 if (deviceFeature == "lock") {
74                         return lockLatestValue
75                 }
76         }
77 }