Infrastruction modification
[smartthings-infrastructure.git] / Alarm / Alarm.groovy
1 //Create a class for alarm device
2 package Alarm
3 import SmartThing.SmartThing
4
5 public class Alarm extends SmartThing {
6         // id, label, and display name of the device
7         StringBuilder id = new StringBuilder()
8         StringBuilder label = new StringBuilder()
9         StringBuilder displayName = new StringBuilder()
10         // Features with string values
11         StringBuilder currentAlarm = new StringBuilder()
12         // Maps from features to values
13         HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
14
15         Alarm(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentAlarm) {
16                 deviceValuesMap = deviceValueSmartThing
17                 idSmartThing = id
18                 labelSmartThing = label
19                 displayNameSmartThing = displayName
20                 sendEventSmartThings = sendEvent
21
22                 // Initialization
23                 this.id = id
24                 this.label = label
25                 this.displayName = displayName
26                 this.currentAlarm = currentAlarm
27
28                 deviceValuesMap.put("alarm", currentAlarm)
29         }
30
31         // Methods to set values
32         def both() {
33                 action("both")
34         }
35
36         def on() {
37                 action("both")
38         }
39
40         def off() {
41                 action("off")
42         }
43
44         def siren() {
45                 action("siren")
46         }
47
48         def strobe() {
49                 action("strobe")
50         }
51
52         def action(String newValue) {
53                 if (!currentAlarm.equals(newValue)) {
54                         String tmpID = id.toString()
55                         currentAlarm.replace(0, currentAlarm.length(), newValue)
56                         println("the alarm with id:$tmpID is changed to $newValue!")
57                         sendEvent([name: "alarm", value: newValue, deviceId: tmpID, descriptionText: "",
58                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
59                 }
60         }
61
62         // Methods to return values
63         def getCurrentAlarm() {
64                 return currentAlarm.toString()
65         }
66 }