//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 int id = 10
- private String label = "contactSensor"
- private String displayName = "contactSensor"
- private String contactState = "closed"
- private String contactLatestValue = "closed"
+ 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) {
- 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))
- }
- }
-
- //Methods for closures
- def count(Closure Input) {
- contacts.count(Input)
- }
- def size() {
- contacts.size()
- }
- def each(Closure Input) {
- contacts.each(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)
- }
-
- def currentValue(String deviceFeature) {
- if (deviceNumbers == 1)
- contacts[0].currentValue(deviceFeature)//It is called if we have only one device
+ if (init)
+ currentContact.append("closed")
else
- contacts*.currentValue(deviceFeature)
- }
+ currentContact.append("open")
- 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.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
}
}