Commit #7: Events thread-based + new easier Extractor.py + our own Timer class
[smartthings-infrastructure.git] / Lock / Locks.groovy
1 //Create a class for lock device
2 package Lock
3 import Timer.SimulatedTimer
4
5 public class Locks {
6         private int id
7         private String displayName
8         private String lockState
9         private String lockLatestValue
10         def sendEvent   
11         def timers
12
13
14         Locks(Closure sendEvent, int id, String displayName, String lockState, String lockLatestValue) {
15                 this.id = id
16                 this.sendEvent = sendEvent
17                 this.displayName = displayName
18                 this.lockState = lockState
19                 this.lockLatestValue = lockLatestValue
20                 this.timers = new SimulatedTimer()
21         }
22
23         //By Apps
24         def lock() {
25                 println("the door with id:$id is locked!")
26                 this.lockLatestValue = this.lockState
27                 this.lockState = "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.lockState
36                         this.lockState = "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.lockState
45                 this.lockState = "unlocked"
46                 sendEvent([name: "unlock", 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.lockState
54                         this.lockState = "locked"
55                         sendEvent([name: "unlock", value: "unlocked", 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.lockState
64                 this.lockState = value
65         }
66         
67         def currentValue(String deviceFeature) {
68                 if (deviceFeature == "lock") {
69                         return lockState
70                 }
71         }
72
73         def latestValue(String deviceFeature) {
74                 if (deviceFeature == "lock") {
75                         return lockLatestValue
76                 }
77         }
78 }