Changes in classes: new concept for latest value + all types of events generated...
[smartthings-infrastructure.git] / ContactSensor / ContactSensor.groovy
index d13993ae3231095369a8a0fdabe74bba220ae7eb..b4ed62906771be1655f70bcedfdffe17966a6687 100644 (file)
@@ -10,6 +10,8 @@ public class ContactSensor {
        private String currentContact   
        private String latestValue
        private String alarmState
+       private List events = []
+       private List timeOfEvents = []
        
 
        ContactSensor(String id, String label, String displayName, String contactState, String currentContact, String alarmState, String latestValue) {
@@ -23,10 +25,22 @@ public class ContactSensor {
        }
 
        def setValue(String value) {
-               this.latestValue = contactState
                println("the contact sensor with id:$id is triggered to $value!")
                this.contactState = value
                this.currentContact = value
+               this.latestValue = value
+               this.events.add(eventDataMap)
+               this.timeOfEvents.add(System.currentTimeMillis())
+       }
+
+       def eventsSince(Date dateObj) {
+               def List happenedEvents = []
+               def sinceThen = dateObj.time
+               for (int i = 0;i < timeOfEvents.size();i++) {
+                       if (timeOfEvents[i]>=sinceThen)
+                               happenedEvents.add(events[i])
+               }
+               return happenedEvents
        }
        
        def on() {
@@ -45,6 +59,12 @@ public class ContactSensor {
                }
        }
 
+       def currentState(String deviceFeature) {
+               if (deviceFeature == "contact") {
+                       return contactState
+               }
+       }
+
        def latestValue(String deviceFeature) {
                if (deviceFeature == "contact") {
                        return latestValue