Commit #9: More classes + Extractor with Rahmadi's editions + Fixing some bugs
[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) {
21                 this.sendEvent = sendEvent              
22                 this.deviceNumbers = deviceNumbers
23                 this.contacts = []
24
25                 contacts.add(new ContactSensor(id, label, displayName, this.contactState, this.currentContact, this.alarmState, this.latestValue))
26         }
27
28         //Methods for closures
29         def count(Closure Input) {
30                 contacts.count(Input)
31         }
32         def size() {
33                 contacts.size()
34         }
35         def each(Closure Input) {
36                 contacts.each(Input)
37         }
38         def find(Closure Input) {
39                 contacts.find(Input)
40         }
41         def collect(Closure Input) {
42                 contacts.collect(Input)
43         }
44
45         //By Model Checker
46         def setValue(LinkedHashMap eventDataMap) {
47                 if (eventDataMap["value"] != contacts[0].contactState) {
48                         contacts[0].setValue(eventDataMap["value"])
49                         this.latestValue = contacts[0].latestValue
50                         this.contactState = contacts[0].contactState
51                         this.currentContact = contacts[0].contactState
52                         sendEvent(eventDataMap)
53                 }
54         }
55
56         def on() {
57                 contacts[0].on()
58                 this.alarmState = "armed"
59         }
60
61         def off() {
62                 contacts[0].off()
63                 this.alarmState = "not armed"
64         }
65
66         def currentValue(String deviceFeature) {
67                 contacts[0].currentValue(deviceFeature)//It is called if we have only one device
68         }
69
70         def latestValue(String deviceFeature) {
71                 contacts[0].latestValue(deviceFeature)//It is called if we have only one device
72         }
73
74         def getAt(int ix) {
75                 contacts[ix]
76         }
77 }