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