Infrastruction modification
[smartthings-infrastructure.git] / Alarm / Alarm.groovy
index 6a0a24f1718acb4315f9a0d3e596940bd17f2bed..f03f5d6952897d43937bf20d6d0a2d42e4c2b4f1 100644 (file)
@@ -1,96 +1,66 @@
 //Create a class for alarm device
 package Alarm
-import Timer.SimulatedTimer
+import SmartThing.SmartThing
 
-public class Alarm {
-       private String id
-       private String label
-       private String displayName
-       private String alarm
-       private String currentAlarm
-       private String alarmLatestValue
-       def sendEvent   
-       def timers
-       
+public class Alarm extends SmartThing {
+       // id, label, and display name of the device
+       StringBuilder id = new StringBuilder()
+       StringBuilder label = new StringBuilder()
+       StringBuilder displayName = new StringBuilder()
+       // Features with string values
+       StringBuilder currentAlarm = new StringBuilder()
+       // Maps from features to values
+       HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
 
-       Alarm(Closure sendEvent, String id, String label, String displayName, String alarm, String currentAlarm, String alarmLatestValue) {
-               this.sendEvent = sendEvent
-               this.timers = new SimulatedTimer()
+       Alarm(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, StringBuilder currentAlarm) {
+               deviceValuesMap = deviceValueSmartThing
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
                this.displayName = displayName
-               this.alarm = alarm
                this.currentAlarm = currentAlarm
-               this.alarmLatestValue = alarmLatestValue
-       }
 
-       //By model checker
-       def setValue(String value) {
-               println("the alarm with id:$id is triggered to $value!")
-               this.alarmLatestValue = value
-               this.alarm = value
-               this.currentAlarm = value
+               deviceValuesMap.put("alarm", currentAlarm)
        }
 
-
-       //By Apps
+       // Methods to set values
        def both() {
-               if (alarm != "both") {
-                       println("the alarm with id:$id is changed to both!")
-                       this.alarmLatestValue = "both"
-                       this.alarm = "both"
-                       this.currentAlarm = "both"
-                       sendEvent([name: "alarm", value: "both", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action("both")
        }
 
        def on() {
-               both()
+               action("both")
        }
 
        def off() {
-               if (alarm != "off") {
-                       println("the alarm with id:$id is changed to off!")
-                       this.alarmLatestValue = "off"
-                       this.alarm = "off"
-                       this.currentAlarm = "off"
-                       sendEvent([name: "alarm", value: "off", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action("off")
        }
 
        def siren() {
-               if (alarm != "siren") {
-                       println("the alarm with id:$id is changed to siren!")
-                       this.alarmLatestValue = "siren"
-                       this.alarm = "siren"
-                       this.currentAlarm = "siren"
-                       sendEvent([name: "alarm", value: "siren", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action("siren")
        }
 
        def strobe() {
-               if (alarm != "strobe") {
-                       println("the alarm with id:$id is changed to strobe!")
-                       this.alarmLatestValue = "strobe"
-                       this.alarm = "strobe"
-                       this.currentAlarm = "strobe"
-                       sendEvent([name: "alarm", value: "strobe", deviceId: this.id, descriptionText: "",
-                           displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
-               }
+               action("strobe")
        }
 
-       def currentValue(String deviceFeature) {
-               if (deviceFeature == "alarm") {
-                       return currentAlarm
+       def action(String newValue) {
+               if (!currentAlarm.equals(newValue)) {
+                       String tmpID = id.toString()
+                       currentAlarm.replace(0, currentAlarm.length(), newValue)
+                       println("the alarm with id:$tmpID is changed to $newValue!")
+                       sendEvent([name: "alarm", value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
                }
        }
 
-       def latestValue(String deviceFeature) {
-               if (deviceFeature == "alarm") {
-                       return alarmLatestValue
-               }
+       // Methods to return values
+       def getCurrentAlarm() {
+               return currentAlarm.toString()
        }
 }