Adding event for Power as variation is needed in power values.
[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, boolean init) {
20                 this.sendEvent = sendEvent
21                 this.timers = new SimulatedTimer()
22                 this.deviceNumbers = deviceNumbers
23                 this.locks = []
24
25                 if (init) {
26                         this.lockState = "locked"
27                         this.currentLock = "locked"
28                         this.lockLatestValue = "locked"
29                 } else {
30                         this.lockState = "unlocked"
31                         this.currentLock = "unlocked"
32                         this.lockLatestValue = "unlocked"
33                 }
34                 locks.add(new Lock(sendEvent,id, label, displayName, this.lockState, this.lockLatestValue))
35         }
36
37         //By Apps
38         def lock() {
39                 if (lockState != "locked") {
40                         //lockLatestValue = lockState
41                         lockLatestValue = "locked"
42                         lockState = "locked"
43                         currentLock = "locked"
44                         locks[0].lock()
45                 }
46         }
47
48         def lock(LinkedHashMap metaData) {
49                 if (lockState != "locked") {
50                         def task = timers.runAfter(metaData["delay"]) {
51                                 //lockLatestValue = lockState
52                                 lockLatestValue = "locked"
53                                 lockState = "locked"
54                                 currentLock = "locked"
55                                 locks[0].lock()
56                         }
57                 }
58         }
59
60         def unlock() {
61                 if (lockState != "unlocked") {
62                         //lockLatestValue = lockState
63                         lockLatestValue = "unlocked"
64                         lockState = "unlocked"
65                         currentLock = "unlocked"
66                         locks[0].unlock()
67                 }
68         }
69
70
71         def unlock(LinkedHashMap metaData) {
72                 if (lockState != "unlocked") {
73                         def task = timers.runAfter(metaData["delay"]) {
74                                 //lockLatestValue = lockState
75                                 lockLatestValue = "unlocked"
76                                 lockState = "unlocked"
77                                 currentLock = "unlocked"
78                                 locks[0].unlock()
79                         }
80                 }
81         }
82
83         //Methods for closures
84         def count(Closure Input) {
85                 locks.count(Input)
86         }
87         def size() {
88                 locks.size()
89         }
90         def each(Closure Input) {
91                 locks.each(Input)
92         }
93         def find(Closure Input) {
94                 locks.find(Input)
95         }
96         def sort(Closure Input) {
97                 locks.sort(Input)
98         }
99         def collect(Closure Input) {
100                 locks.collect(Input)
101         }
102
103         //By Model Checker
104         def setValue(LinkedHashMap eventDataMap) {
105                 if (eventDataMap["value"] != locks[0].lockState) {
106                         locks[0].setValue(eventDataMap["value"])
107                         this.lockState = locks[0].lockState
108                         this.currentLock = locks[0].lockState
109                         this.lockLatestValue = locks[0].lockLatestValue
110                         sendEvent(eventDataMap)
111                 }
112         }
113
114         def currentValue(String deviceFeature) {
115                 locks[0].currentValue(deviceFeature)
116         }
117
118         def latestValue(String deviceFeature) {
119                 locks[0].latestValue(deviceFeature)
120         }
121
122         def getAt(int ix) {
123                 locks[ix]
124         }
125 }
126