Commit #7: eventHandler and event queue are unique between two apps now. (Similar...
[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         Contacting(Closure sendEvent, int deviceNumbers) {
10                 this.sendEvent = sendEvent              
11                 this.deviceNumbers = deviceNumbers
12                 this.contacts = []
13                 if (deviceNumbers == 1) {
14                         contacts = [new Contacts(0, "contact0", "closed", "closed")]
15                 } else if (deviceNumbers == 2) {
16                         contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed")]
17                 } else if (deviceNumbers == 3) {
18                         contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed")
19                                    ,new Contacts(2, "contact2", "closed", "closed")]
20                 }
21         }
22
23         //By Model Checker
24         def setValue(LinkedHashMap eventDataMap) {
25                 contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
26                 sendEvent(eventDataMap)
27         }
28
29         def currentValue(String deviceFeature) {
30                 if (deviceNumbers == 1)
31                         contacts[0].currentValue(deviceFeature)//It is called if we have only one device
32                 else
33                         contacts*.currentValue(deviceFeature)
34         }
35
36         def latestValue(String deviceFeature) {
37                 if (deviceNumbers == 1)
38                         contacts[0].latestValue(deviceFeature)//It is called if we have only one device
39                 else
40                         contacts*.latestValue(deviceFeature)
41         }
42
43         def getAt(int ix) {
44                 contacts[ix]
45         }
46 }