Adding remaining devices!
[smartthings-infrastructure.git] / ContactSensor / ContactSensors.groovy
1 //Create a class for contact sensor
2 package ContactSensor
3 import Timer.SimulatedTimer
4
5 public class ContactSensors {
6         private int deviceNumbers
7         private List contacts
8         def sendEvent
9
10         //For one device(We cannot have obj.id)-> We should have obj[0].id
11         private String id = "contactSensorID0"
12         private String label = "contactSensor0"
13         private String displayName = "contactSensor0"
14         private String contactState = "closed"
15         private String currentContact = "closed"
16         private String latestValue = "closed"
17         private String alarmState = "armed"
18
19                 
20         ContactSensors(Closure sendEvent, int deviceNumbers, boolean init) {
21                 this.sendEvent = sendEvent              
22                 this.deviceNumbers = deviceNumbers
23                 this.contacts = []
24                 
25                 if (init) {
26                         this.contactState = "closed"
27                         this.currentContact = "closed"
28                         this.latestValue = "closed"
29                         this.alarmState = "armed"
30                 } else {
31                         this.contactState = "open"
32                         this.currentContact = "open"
33                         this.latestValue = "open"
34                         this.alarmState = "not armed"
35                 }
36
37                 contacts.add(new ContactSensor(id, label, displayName, this.contactState, this.currentContact, this.alarmState, this.latestValue))
38         }
39
40         //Methods for closures
41         def count(Closure Input) {
42                 contacts.count(Input)
43         }
44         def size() {
45                 contacts.size()
46         }
47         def each(Closure Input) {
48                 contacts.each(Input)
49         }
50         def find(Closure Input) {
51                 contacts.find(Input)
52         }
53         def sort(Closure Input) {
54                 contacts.sort(Input)
55         }
56         def collect(Closure Input) {
57                 contacts.collect(Input)
58         }
59
60         //By Model Checker
61         def setValue(LinkedHashMap eventDataMap) {
62                 if (eventDataMap["value"] != contacts[0].contactState) {
63                         this.latestValue = eventDataMap["value"]
64                         this.contactState = eventDataMap["value"]
65                         this.currentContact = eventDataMap["value"]
66                         contacts[0].setValue(eventDataMap["value"])
67                         sendEvent(eventDataMap)
68                 }
69         }
70
71         def eventsSince(Date dateObj) {
72                 return contacts[0].eventsSince()
73         }
74
75         def on() {
76                 this.alarmState = "armed"
77                 contacts[0].on()
78         }
79
80         def off() {
81                 this.alarmState = "not armed"
82                 contacts[0].off()
83         }
84
85         def currentValue(String deviceFeature) {
86                 contacts[0].currentValue(deviceFeature)//It is called if we have only one device
87         }
88
89         def currentState(String deviceFeature) {
90                 contacts[0].currentState(deviceFeature)//It is called if we have only one device
91         }
92
93         def latestValue(String deviceFeature) {
94                 contacts[0].latestValue(deviceFeature)//It is called if we have only one device
95         }
96
97         def getAt(int ix) {
98                 contacts[ix]
99         }
100 }