Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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                         //lockLatestValue = lockState
43                         lockLatestValue = "locked"
44                         lockState = "locked"
45                         currentLock = "locked"
46                         locks[0].lock()
47                 }
48         }
49
50         def lock(LinkedHashMap metaData) {
51                 if (lockState != "locked") {
52                         def task = timers.runAfter(metaData["delay"]) {
53                                 //lockLatestValue = lockState
54                                 lockLatestValue = "locked"
55                                 lockState = "locked"
56                                 currentLock = "locked"
57                                 locks[0].lock()
58                         }
59                 }
60         }
61
62         def unlock() {
63                 if (lockState != "unlocked") {
64                         //lockLatestValue = lockState
65                         lockLatestValue = "unlocked"
66                         lockState = "unlocked"
67                         currentLock = "unlocked"
68                         locks[0].unlock()
69                 }
70         }
71
72
73         def unlock(LinkedHashMap metaData) {
74                 if (lockState != "unlocked") {
75                         def task = timers.runAfter(metaData["delay"]) {
76                                 //lockLatestValue = lockState
77                                 lockLatestValue = "unlocked"
78                                 lockState = "unlocked"
79                                 currentLock = "unlocked"
80                                 locks[0].unlock()
81                         }
82                 }
83         }
84
85         //Methods for closures
86         def count(Closure Input) {
87                 locks.count(Input)
88         }
89         def size() {
90                 locks.size()
91         }
92         def each(Closure Input) {
93                 locks.each(Input)
94         }
95         def find(Closure Input) {
96                 locks.find(Input)
97         }
98         def sort(Closure Input) {
99                 locks.sort(Input)
100         }
101         def collect(Closure Input) {
102                 locks.collect(Input)
103         }
104
105         //By Model Checker
106         def setValue(LinkedHashMap eventDataMap) {
107                 if (eventDataMap["value"] != locks[0].lockState) {
108                         locks[0].setValue(eventDataMap["value"])
109                         this.lockState = locks[0].lockState
110                         this.currentLock = locks[0].lockState
111                         this.lockLatestValue = locks[0].lockLatestValue
112                         sendEvent(eventDataMap)
113                 }
114         }
115
116         def currentValue(String deviceFeature) {
117                 locks[0].currentValue(deviceFeature)
118         }
119
120         def latestValue(String deviceFeature) {
121                 locks[0].latestValue(deviceFeature)
122         }
123
124         def getAt(int ix) {
125                 locks[ix]
126         }
127 }
128