Changes in classes: new concept for latest value + all types of events generated...
[smartthings-infrastructure.git] / Alarm / Alarm.groovy
1 //Create a class for alarm device
2 package Alarm
3 import Timer.SimulatedTimer
4
5 public class Alarm {
6         private String id
7         private String label
8         private String displayName
9         private String alarm
10         private String currentAlarm
11         private String alarmLatestValue
12         def sendEvent   
13         def timers
14         
15
16         Alarm(Closure sendEvent, String id, String label, String displayName, String alarm, String currentAlarm, String alarmLatestValue) {
17                 this.sendEvent = sendEvent
18                 this.timers = new SimulatedTimer()
19                 this.id = id
20                 this.label = label
21                 this.displayName = displayName
22                 this.alarm = alarm
23                 this.currentAlarm = currentAlarm
24                 this.alarmLatestValue = alarmLatestValue
25         }
26
27         //By model checker
28         def setValue(String value) {
29                 println("the alarm with id:$id is triggered to $value!")
30                 this.alarmLatestValue = value
31                 this.alarm = value
32                 this.currentAlarm = value
33         }
34
35
36         //By Apps
37         def both() {
38                 if (alarm != "both") {
39                         println("the alarm with id:$id is changed to both!")
40                         this.alarmLatestValue = "both"
41                         this.alarm = "both"
42                         this.currentAlarm = "both"
43                         sendEvent([name: "alarm", value: "both", deviceId: this.id, descriptionText: "",
44                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
45                         sendEvent([name: "alarm.both", value: "both", deviceId: this.id, descriptionText: "",
46                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
47                 }
48         }
49
50         def on() {
51                 both()
52         }
53
54         def off() {
55                 if (alarm != "off") {
56                         println("the alarm with id:$id is changed to off!")
57                         this.alarmLatestValue = "off"
58                         this.alarm = "off"
59                         this.currentAlarm = "off"
60                         sendEvent([name: "alarm", value: "off", deviceId: this.id, descriptionText: "",
61                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
62                         sendEvent([name: "alarm.off", value: "off", deviceId: this.id, descriptionText: "",
63                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
64                 }
65         }
66
67         def siren() {
68                 if (alarm != "siren") {
69                         println("the alarm with id:$id is changed to siren!")
70                         this.alarmLatestValue = "siren"
71                         this.alarm = "siren"
72                         this.currentAlarm = "siren"
73                         sendEvent([name: "alarm", value: "siren", deviceId: this.id, descriptionText: "",
74                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
75                         sendEvent([name: "alarm.siren", value: "siren", deviceId: this.id, descriptionText: "",
76                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
77                 }
78         }
79
80         def strobe() {
81                 if (alarm != "strobe") {
82                         println("the alarm with id:$id is changed to strobe!")
83                         this.alarmLatestValue = "strobe"
84                         this.alarm = "strobe"
85                         this.currentAlarm = "strobe"
86                         sendEvent([name: "alarm", value: "strobe", deviceId: this.id, descriptionText: "",
87                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
88                         sendEvent([name: "alarm.strobe", value: "strobe", deviceId: this.id, descriptionText: "",
89                             displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
90                 }
91         }
92
93         def currentValue(String deviceFeature) {
94                 if (deviceFeature == "alarm") {
95                         return currentAlarm
96                 }
97         }
98
99         def latestValue(String deviceFeature) {
100                 if (deviceFeature == "alarm") {
101                         return alarmLatestValue
102                 }
103         }
104 }