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