Commit #5
[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                 timers.cancel() //Timer is ready to use
21         }
22
23         //By Apps
24         def lock() {
25                 println("the door with id:$id is locked!")
26                 this.lockLatestValue = this.lockCurrentValue
27                 this.lockCurrentValue = "locked"
28                 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
29                           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
30         }
31
32         def lock(LinkedHashMap metaData) {
33                 def task = timers.runAfter(metaData["delay"]) {
34                         println("the door with id:$id is locked!")
35                         this.lockLatestValue = this.lockCurrentValue
36                         this.lockCurrentValue = "locked"
37                         sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
38                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
39                 }
40         }
41         
42         def unlock() {
43                 println("the door with id:$id is unlocked!")
44                 this.lockLatestValue = this.lockCurrentValue
45                 this.lockCurrentValue = "unlocked"
46                 sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
47                           displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
48         }
49
50         def unlock(LinkedHashMap metaData) {
51                 def task = timers.runAfter(metaData["delay"]) {
52                         println("the door with id:$id is locked!")
53                         this.lockLatestValue = this.lockCurrentValue
54                         this.lockCurrentValue = "locked"
55                         sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
56                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: []])
57                 }
58         }
59
60         //By Model Checker
61         def setValue(String value) {
62                 println("the door with id:$id is $value!")
63                 this.lockLatestValue = this.lockCurrentValue
64                 this.lockCurrentValue = value
65         }
66         
67         def currentValue(String deviceFeature) {
68                 if (deviceFeature == "lock") {
69                         return lockCurrentValue
70                 }
71         }
72
73         def latestValue(String deviceFeature) {
74                 if (deviceFeature == "lock") {
75                         return lockLatestValue
76                 }
77         }
78 }