Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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 <<<<<<< HEAD
32 =======
33                         //this.lockLatestValue = this.lockState
34 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
35                         this.lockLatestValue = "locked"
36                         this.lockState = "locked"
37                         this.currentLock = "locked"
38                         sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
39                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
40                         sendEvent([name: "lock.locked", value: "locked", deviceId: this.id, descriptionText: "",
41                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
42                 }
43         }
44
45         def lock(LinkedHashMap metaData) {
46                 if (lockState != "locked") {
47                         def task = timers.runAfter(metaData["delay"]) {
48                                 println("the door with id:$id is locked!")
49 <<<<<<< HEAD
50 =======
51                                 //this.lockLatestValue = this.lockState
52 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
53                                 this.lockLatestValue = "locked"
54                                 this.lockState = "locked"
55                                 this.currentLock = "locked"
56                                 sendEvent([name: "lock", value: "locked", deviceId: this.id, descriptionText: "",
57                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
58                                 sendEvent([name: "lock.locked", value: "locked", deviceId: this.id, descriptionText: "",
59                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
60                         }
61                 }
62         }
63         
64         def unlock() {
65                 if (lockState != "unlocked") {
66                         println("the door with id:$id is unlocked!")
67 <<<<<<< HEAD
68 =======
69                         //this.lockLatestValue = this.lockState
70 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
71                         this.lockLatestValue = "unlocked"
72                         this.lockState = "unlocked"
73                         this.currentLock = "unlocked"
74                         sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
75                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
76                         sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
77                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
78 <<<<<<< HEAD
79                         sendEvent([name: "lock.unlocked", value: "unlocked", deviceId: this.id, descriptionText: "",
80                                   displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
81 =======
82 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
83                 }
84         }
85
86         def unlock(LinkedHashMap metaData) {
87                 if (lockState != "unlocked") {
88                         def task = timers.runAfter(metaData["delay"]) {
89                                 println("the door with id:$id is locked!")
90 <<<<<<< HEAD
91                                 this.lockLatestValue = "unlocked"
92                                 this.lockState = "unlocked"
93                                 this.currentLock = "unlocked"
94                                 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
95                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
96                                 sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
97                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
98                                 sendEvent([name: "lock.unlocked", value: "unlocked", deviceId: this.id, descriptionText: "",
99                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])                             
100 =======
101                                 //this.lockLatestValue = this.lockState
102                                 this.lockLatestValue = "locked"
103                                 this.lockState = "locked"
104                                 this.currentLock = "locked"
105                                 sendEvent([name: "unlock", value: "unlocked", deviceId: this.id, descriptionText: "",
106                                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
107                                 sendEvent([name: "lock", value: "unlocked", deviceId: this.id, descriptionText: "",
108                                                 displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
109 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
110                         }
111                 }
112         }
113
114         //By Model Checker
115         def setValue(String value) {
116                 println("the door with id:$id is $value!")
117                 this.lockLatestValue = value
118                 this.lockState = value
119                 this.currentLock = value
120         }
121         
122         def currentValue(String deviceFeature) {
123                 if (deviceFeature == "lock") {
124                         return lockState
125                 }
126         }
127
128         def latestValue(String deviceFeature) {
129                 if (deviceFeature == "lock") {
130                         return lockLatestValue
131                 }
132         }
133 }