Adding path explorations for initializations.
[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                 locks[0].lock()
42                 lockLatestValue = lockState
43                 lockState = "locked"
44                 currentLock = "locked"
45         }
46
47         def lock(LinkedHashMap metaData) {
48                 def task = timers.runAfter(metaData["delay"]) {
49                         locks[0].lock()
50                         lockLatestValue = lockState
51                         lockState = "locked"
52                         currentLock = "locked"
53                 }
54         }
55
56         def unlock() {
57                 locks[0].unlock()
58                 lockLatestValue = lockState
59                 lockState = "unlocked"
60                 currentLock = "unlocked"
61         }
62
63
64         def unlock(LinkedHashMap metaData) {
65                 def task = timers.runAfter(metaData["delay"]) {
66                         locks[0].unlock()
67                         lockLatestValue = lockState
68                         lockState = "unlocked"
69                         currentLock = "unlocked"
70                 }
71         }
72
73         //Methods for closures
74         def count(Closure Input) {
75                 locks.count(Input)
76         }
77         def size() {
78                 locks.size()
79         }
80         def each(Closure Input) {
81                 locks.each(Input)
82         }
83         def find(Closure Input) {
84                 locks.find(Input)
85         }
86         def collect(Closure Input) {
87                 locks.collect(Input)
88         }
89
90         //By Model Checker
91         def setValue(LinkedHashMap eventDataMap) {
92                 if (eventDataMap["value"] != locks[0].lockState) {
93                         locks[0].setValue(eventDataMap["value"])
94                         this.lockState = locks[0].lockState
95                         this.currentLock = locks[0].lockState
96                         this.lockLatestValue = locks[0].lockLatestValue
97                         sendEvent(eventDataMap)
98                 }
99         }
100
101         def currentValue(String deviceFeature) {
102                 locks[0].currentValue(deviceFeature)
103         }
104
105         def latestValue(String deviceFeature) {
106                 locks[0].latestValue(deviceFeature)
107         }
108
109         def getAt(int ix) {
110                 locks[ix]
111         }
112 }
113