Adding the method runDaily.
[smartthings-infrastructure.git] / ContactSensor / ContactSensors.groovy
index e53d7d1f95a486c1b4a0f12241407cd605e34d6f..c4136e301a843a3f836dea903abddf68a4c1940f 100644 (file)
@@ -8,21 +8,33 @@ public class ContactSensors {
        def sendEvent
 
        //For one device(We cannot have obj.id)-> We should have obj[0].id
-       private int id = 10
-       private String label = "contactSensor"
-       private String displayName = "contactSensor"
+       private String id = "contactSensorID0"
+       private String label = "contactSensor0"
+       private String displayName = "contactSensor0"
        private String contactState = "closed"
-       private String contactLatestValue = "closed"
+       private String currentContact = "closed"
+       private String latestValue = "closed"
+       private String alarmState = "armed"
 
                
-       ContactSensors(Closure sendEvent, int deviceNumbers) {
+       ContactSensors(Closure sendEvent, int deviceNumbers, boolean init) {
                this.sendEvent = sendEvent              
                this.deviceNumbers = deviceNumbers
                this.contacts = []
-
-               for (int i = 0;i < deviceNumbers;i++) {
-                       contacts.add(new ContactSensor(i+10, label+i.toString(), displayName+i.toString(), this.contactState, this.contactLatestValue))
+               
+               if (init) {
+                       this.contactState = "closed"
+                       this.currentContact = "closed"
+                       this.latestValue = "closed"
+                       this.alarmState = "armed"
+               } else {
+                       this.contactState = "open"
+                       this.currentContact = "open"
+                       this.latestValue = "open"
+                       this.alarmState = "not armed"
                }
+
+               contacts.add(new ContactSensor(id, label, displayName, this.contactState, this.currentContact, this.alarmState, this.latestValue))
        }
 
        //Methods for closures
@@ -35,28 +47,51 @@ public class ContactSensors {
        def each(Closure Input) {
                contacts.each(Input)
        }
+       def find(Closure Input) {
+               contacts.find(Input)
+       }
+       def sort(Closure Input) {
+               contacts.sort(Input)
+       }
+       def collect(Closure Input) {
+               contacts.collect(Input)
+       }
 
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
-               contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
-               if (deviceNumbers == 1)
-                       this.contactLatestValue = contacts[eventDataMap["deviceId"]].contactLatestValue
-                       this.contactState = contacts[eventDataMap["deviceId"]].contactState
-               sendEvent(eventDataMap)
+               if (eventDataMap["value"] != contacts[0].contactState) {
+                       this.latestValue = eventDataMap["value"]
+                       this.contactState = eventDataMap["value"]
+                       this.currentContact = eventDataMap["value"]
+                       contacts[0].setValue(eventDataMap["value"])
+                       sendEvent(eventDataMap)
+               }
+       }
+
+       def eventsSince(Date dateObj) {
+               return contacts[0].eventsSince()
+       }
+
+       def on() {
+               this.alarmState = "armed"
+               contacts[0].on()
+       }
+
+       def off() {
+               this.alarmState = "not armed"
+               contacts[0].off()
        }
 
        def currentValue(String deviceFeature) {
-               if (deviceNumbers == 1)
-                       contacts[0].currentValue(deviceFeature)//It is called if we have only one device
-               else
-                       contacts*.currentValue(deviceFeature)
+               contacts[0].currentValue(deviceFeature)//It is called if we have only one device
+       }
+
+       def currentState(String deviceFeature) {
+               contacts[0].currentState(deviceFeature)//It is called if we have only one device
        }
 
        def latestValue(String deviceFeature) {
-               if (deviceNumbers == 1)
-                       contacts[0].latestValue(deviceFeature)//It is called if we have only one device
-               else
-                       contacts*.latestValue(deviceFeature)
+               contacts[0].latestValue(deviceFeature)//It is called if we have only one device
        }
 
        def getAt(int ix) {