Infrastructure that works for all the locks' group!
[smartthings-infrastructure.git] / Lock / Locks.groovy
1 //Create a class for lock device
2 package Lock
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class Locks{
9         int deviceNumbers       
10         List locks      
11         def sendEvent   
12         def timers
13
14         //When we have only one device
15         private String id = "lockID0"
16         private String label = "lock0"
17         private String displayName = "lock0"
18         private String lockState = "locked"
19         private String currentLock = "locked"
20         private String lockLatestValue = "locked"
21
22         Locks(Closure sendEvent, int deviceNumbers) {
23                 this.sendEvent = sendEvent
24                 this.timers = new SimulatedTimer()
25                 this.deviceNumbers = deviceNumbers
26                 this.locks = []
27
28                 def init = Verify.getBoolean()
29                 if (init) {
30                         this.lockState = "locked"
31                         this.lockLatestValue = "locked"
32                 } else {
33                         this.lockState = "unlocked"
34                         this.lockLatestValue = "unlocked"
35                 }
36                 locks.add(new Lock(sendEvent,id, label, displayName, this.lockState, this.lockLatestValue))
37         }
38
39         //By Apps
40         def lock() {
41                 if (lockState != "locked") {
42                         locks[0].lock()
43                         lockLatestValue = lockState
44                         lockState = "locked"
45                         currentLock = "locked"
46                 }
47         }
48
49         def lock(LinkedHashMap metaData) {
50                 if (lockState != "locked") {
51                         def task = timers.runAfter(metaData["delay"]) {
52                                 locks[0].lock()
53                                 lockLatestValue = lockState
54                                 lockState = "locked"
55                                 currentLock = "locked"
56                         }
57                 }
58         }
59
60         def unlock() {
61                 if (lockState != "unlocked") {
62                         locks[0].unlock()
63                         lockLatestValue = lockState
64                         lockState = "unlocked"
65                         currentLock = "unlocked"
66                 }
67         }
68
69
70         def unlock(LinkedHashMap metaData) {
71                 if (lockState != "unlocked") {
72                         def task = timers.runAfter(metaData["delay"]) {
73                                 locks[0].unlock()
74                                 lockLatestValue = lockState
75                                 lockState = "unlocked"
76                                 currentLock = "unlocked"
77                         }
78                 }
79         }
80
81         //Methods for closures
82         def count(Closure Input) {
83                 locks.count(Input)
84         }
85         def size() {
86                 locks.size()
87         }
88         def each(Closure Input) {
89                 locks.each(Input)
90         }
91         def find(Closure Input) {
92                 locks.find(Input)
93         }
94         def collect(Closure Input) {
95                 locks.collect(Input)
96         }
97
98         //By Model Checker
99         def setValue(LinkedHashMap eventDataMap) {
100                 if (eventDataMap["value"] != locks[0].lockState) {
101                         locks[0].setValue(eventDataMap["value"])
102                         this.lockState = locks[0].lockState
103                         this.currentLock = locks[0].lockState
104                         this.lockLatestValue = locks[0].lockLatestValue
105                         sendEvent(eventDataMap)
106                 }
107         }
108
109         def currentValue(String deviceFeature) {
110                 locks[0].currentValue(deviceFeature)
111         }
112
113         def latestValue(String deviceFeature) {
114                 locks[0].latestValue(deviceFeature)
115         }
116
117         def getAt(int ix) {
118                 locks[ix]
119         }
120 }
121