Commit #8: New version of extractor with running the preferences method make things...
[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 int id = 10
12         private String label = "contactSensor"
13         private String displayName = "contactSensor"
14         private String contactState = "closed"
15         private String contactLatestValue = "closed"
16
17                 
18         ContactSensors(Closure sendEvent, int deviceNumbers) {
19                 this.sendEvent = sendEvent              
20                 this.deviceNumbers = deviceNumbers
21                 this.contacts = []
22
23                 for (int i = 0;i < deviceNumbers;i++) {
24                         contacts.add(new ContactSensor(i+10, label+i.toString(), displayName+i.toString(), this.contactState, this.contactLatestValue))
25                 }
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
39         //By Model Checker
40         def setValue(LinkedHashMap eventDataMap) {
41                 contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
42                 if (deviceNumbers == 1)
43                         this.contactLatestValue = contacts[eventDataMap["deviceId"]].contactLatestValue
44                         this.contactState = contacts[eventDataMap["deviceId"]].contactState
45                 sendEvent(eventDataMap)
46         }
47
48         def currentValue(String deviceFeature) {
49                 if (deviceNumbers == 1)
50                         contacts[0].currentValue(deviceFeature)//It is called if we have only one device
51                 else
52                         contacts*.currentValue(deviceFeature)
53         }
54
55         def latestValue(String deviceFeature) {
56                 if (deviceNumbers == 1)
57                         contacts[0].latestValue(deviceFeature)//It is called if we have only one device
58                 else
59                         contacts*.latestValue(deviceFeature)
60         }
61
62         def getAt(int ix) {
63                 contacts[ix]
64         }
65 }