Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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                         this.alarmLatestValue = eventDataMap["value"]
45                         this.alarm = eventDataMap["value"]
46                         this.currentAlarm = eventDataMap["value"]
47                         alarms[0].setValue(eventDataMap["value"])
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 sort(Closure Input) {
66                 alarms.sort(Input)
67         }
68         def collect(Closure Input) {
69                 alarms.collect(Input)
70         }
71
72         //By Apps
73         def both() {
74                 if (alarm != "both") {
75                         alarmLatestValue = "both"
76                         alarm = "both"
77                         currentAlarm = "both"
78                         alarms[0].both()
79                 }
80         }
81
82         def off() {
83                 if (alarm != "off") {
84                         alarmLatestValue = "off"
85                         alarm = "off"
86                         currentAlarm = "off"
87                         alarms[0].off()
88                 }
89         }
90
91         def on() {
92                 both()
93         }
94
95         def siren() {
96                 if (alarm != "siren") {
97                         alarmLatestValue = "siren"
98                         alarm = "siren"
99                         currentAlarm = "siren"
100                         alarms[0].siren()
101                 }
102         }
103
104         def strobe() {
105                 if (alarm != "strobe") {
106                         alarmLatestValue = "strobe"
107                         alarm = "strobe"
108                         currentAlarm = "strobe"
109                         alarms[0].strobe()
110                 }
111         }
112
113         def currentValue(String deviceFeature) {
114                 alarms[0].currentValue(deviceFeature)
115         }
116
117         def latestValue(String deviceFeature) {
118                 alarms[0].latestValue(deviceFeature)
119         }
120
121         def getAt(int ix) {
122                 alarms[ix]
123         }
124 }