Commit #4
[smartthings-infrastructure.git] / ContactSensor / Contacting.groovy
1 //Create a class for contact sensor
2 package ContactSensor
3
4 public class Contacting{
5         private int deviceNumbers
6         private List contacts
7         def sendEvent
8         
9         
10         
11         Contacting(Closure sendEvent, int deviceNumbers) {
12                 this.sendEvent = sendEvent              
13                 this.deviceNumbers = deviceNumbers
14                 this.contacts = []
15                 int id = 0
16                 for (int i = 0;i < deviceNumbers;i++) {
17                         contacts.add(new Contacts(id, "contact"+id.toString(), "closed", "closed"))//By default closed
18                         id = id+1
19                 }
20         }
21
22         //By Model Checker
23         def setValue(LinkedHashMap eventDataMap) {
24                 contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
25                 sendEvent(eventDataMap)
26         }
27
28         def currentValue(String deviceFeature) {
29                 if (deviceNumbers == 1)
30                         contacts[0].currentValue(deviceFeature)//It is called if we have only one device
31                 else
32                         contacts*.currentValue(deviceFeature)
33         }
34
35         def latestValue(String deviceFeature) {
36                 if (deviceNumbers == 1)
37                         contacts[0].latestValue(deviceFeature)//It is called if we have only one device
38                 else
39                         contacts*.latestValue(deviceFeature)
40         }
41
42         def getAt(int ix) {
43                 contacts[ix]
44         }
45 }