Commit #7: Events thread-based + new easier Extractor.py + our own Timer class
[smartthings-infrastructure.git] / ContactSensor / Contacting.groovy
1 //Create a class for contact sensor
2 package ContactSensor
3 import Timer.SimulatedTimer
4
5 public class Contacting {
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 = 0
12         private String displayName = "contact0"
13         private String contactState = "closed"
14         private String contactLatestValue = "closed"
15
16                 
17         Contacting(Closure sendEvent, int deviceNumbers) {
18                 this.sendEvent = sendEvent              
19                 this.deviceNumbers = deviceNumbers
20                 this.contacts = []
21
22
23                 if (deviceNumbers == 1) {
24                         contacts = [new Contacts(this.id, this.displayName, this.contactState, this.contactLatestValue)]
25                 } else if (deviceNumbers == 2) {
26                         contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed")]
27                 } else if (deviceNumbers == 3) {
28                         contacts = [new Contacts(0, "contact0", "closed", "closed"), new Contacts(1, "contact1", "closed", "closed")
29                                    ,new Contacts(2, "contact2", "closed", "closed")]
30                 }
31         }
32
33         //By Model Checker
34         def setValue(LinkedHashMap eventDataMap) {
35                 contacts[eventDataMap["deviceId"]].setValue(eventDataMap["value"])
36                 if (deviceNumbers == 1)
37                         this.contactLatestValue = contacts[eventDataMap["deviceId"]].contactLatestValue
38                         this.contactState = contacts[eventDataMap["deviceId"]].contactState
39                 sendEvent(eventDataMap)
40         }
41
42         def currentValue(String deviceFeature) {
43                 if (deviceNumbers == 1)
44                         contacts[0].currentValue(deviceFeature)//It is called if we have only one device
45                 else
46                         contacts*.currentValue(deviceFeature)
47         }
48
49         def latestValue(String deviceFeature) {
50                 if (deviceNumbers == 1)
51                         contacts[0].latestValue(deviceFeature)//It is called if we have only one device
52                 else
53                         contacts*.latestValue(deviceFeature)
54         }
55
56         def getAt(int ix) {
57                 contacts[ix]
58         }
59 }