More events to generate based on the subscriber's list.
[smartthings-infrastructure.git] / Lock / Locks.groovy
1 //Create a class for lock device
2 package Lock
3 import Timer.SimulatedTimer
4
5 public class Locks{
6         int deviceNumbers       
7         List locks      
8         def sendEvent   
9         def timers
10
11         //When we have only one device
12         private String id = "lockID0"
13         private String label = "lock0"
14         private String displayName = "lock0"
15         private String lockState = "locked"
16         private String currentLock = "locked"
17         private String lockLatestValue = "locked"
18
19         Locks(Closure sendEvent, int deviceNumbers) {
20                 this.sendEvent = sendEvent
21                 this.timers = new SimulatedTimer()
22                 this.deviceNumbers = deviceNumbers
23                 this.locks = []
24
25                 locks.add(new Lock(sendEvent,id, label, displayName, this.lockState, this.lockLatestValue))
26         }
27
28         //By Apps
29         def lock() {
30                 locks[0].lock()
31         }
32
33         def lock(LinkedHashMap metaData) {
34                 def task = timers.runAfter(metaData["delay"]) {
35                         locks[0].lock()
36                 }
37         }
38
39         def unlock() {
40                 locks[0].unlock()
41         }
42
43
44         def unlock(LinkedHashMap metaData) {
45                 def task = timers.runAfter(metaData["delay"]) {
46                         locks[0].unlock()
47                 }
48         }
49
50         //Methods for closures
51         def count(Closure Input) {
52                 locks.count(Input)
53         }
54         def size() {
55                 locks.size()
56         }
57         def each(Closure Input) {
58                 locks.each(Input)
59         }
60         def find(Closure Input) {
61                 locks.find(Input)
62         }
63         def collect(Closure Input) {
64                 locks.collect(Input)
65         }
66
67         //By Model Checker
68         def setValue(LinkedHashMap eventDataMap) {
69                 locks[0].setValue(eventDataMap["value"])
70                 this.lockState = locks[0].lockState
71                 this.currentLock = locks[0].lockState
72                 this.lockLatestValue = locks[0].lockLatestValue
73                 sendEvent(eventDataMap)
74         }
75
76         def currentValue(String deviceFeature) {
77                 locks[0].currentValue(deviceFeature)
78         }
79
80         def latestValue(String deviceFeature) {
81                 locks[0].latestValue(deviceFeature)
82         }
83
84         def getAt(int ix) {
85                 locks[ix]
86         }
87 }
88