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