Infrastruction modification
[smartthings-infrastructure.git] / ContactSensor / ContactSensors.groovy
index c4136e301a843a3f836dea903abddf68a4c1940f..9889793eb3bdd6296434fe51da5d06089897dcfd 100644 (file)
 //Create a class for contact sensor
 package ContactSensor
-import Timer.SimulatedTimer
+import SmartThing.SmartThings
 
-public class ContactSensors {
-       private int deviceNumbers
-       private List contacts
-       def sendEvent
+public class ContactSensors extends SmartThings {
+       List contacts = new ArrayList()
 
-       //For one device(We cannot have obj.id)-> We should have obj[0].id
-       private String id = "contactSensorID0"
-       private String label = "contactSensor0"
-       private String displayName = "contactSensor0"
-       private String contactState = "closed"
-       private String currentContact = "closed"
-       private String latestValue = "closed"
-       private String alarmState = "armed"
+       ContactSensors(Closure sendEvent, boolean init) {
+               // Only initialize one time since we only have one device for each capability
+               contacts = smartThings
 
+               // Initialization
+               StringBuilder id = new StringBuilder("contactID0")
+               StringBuilder label = new StringBuilder("contact")
+               StringBuilder displayName = new StringBuilder("contact0")
+               StringBuilder currentContact = new StringBuilder()
                
-       ContactSensors(Closure sendEvent, int deviceNumbers, boolean init) {
-               this.sendEvent = sendEvent              
-               this.deviceNumbers = deviceNumbers
-               this.contacts = []
-               
-               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
-       def count(Closure Input) {
-               contacts.count(Input)
-       }
-       def size() {
-               contacts.size()
-       }
-       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) {
-               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) {
-               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
-       }
+               if (init)
+                       currentContact.append("closed")
+               else
+                       currentContact.append("open")
 
-       def latestValue(String deviceFeature) {
-               contacts[0].latestValue(deviceFeature)//It is called if we have only one device
+               contacts.add(new ContactSensor(sendEvent, id, label, displayName, currentContact))
        }
 
-       def getAt(int ix) {
-               contacts[ix]
+       // Methods to return values
+       def getCurrentContact() {
+               List tmpValues = new ArrayList()
+               tmpValues.add(contacts[0].getCurrentContact())
+               return tmpValues
        }
 }