cc5c820224096561a360009ca11ff1f5a39f0599
[smartthings-infrastructure.git] / PresenceSensor / PresenceSensors.groovy
1 //Create a class for presence sensor
2 package PresenceSensor
3 import Timer.SimulatedTimer
4
5 public class PresenceSensors {
6         private int deviceNumbers
7         private List presenceSensors
8         def sendEvent
9
10         //For one device(We cannot have obj.id)-> We should have obj[0].id
11         private String id = "presenceSensorID0"
12         private String label = "presenceSensor0"
13         private String displayName = "presenceSensor0"
14         private String presenceState = "not present"
15         private String currentPresence = "not present"
16         private String presenceLatestValue = "not present"
17
18                 
19         PresenceSensors(Closure sendEvent, int deviceNumbers) {
20                 this.sendEvent = sendEvent              
21                 this.deviceNumbers = deviceNumbers
22                 this.presenceSensors = []
23
24                 presenceSensors.add(new PresenceSensor(id, label, displayName, this.presenceState, this.presenceLatestValue))
25         }
26
27         //By Model Checker
28         def setValue(LinkedHashMap eventDataMap) {
29                 presenceSensors[0].setValue(eventDataMap["value"])
30                 if (deviceNumbers == 1)
31                         this.presenceLatestValue = presenceSensors[0].presenceLatestValue
32                         this.presenceState = presenceSensors[0].presenceState
33                         this.currentPresence = presenceSensors[0].presenceState
34                 sendEvent(eventDataMap)
35         }
36
37         //Methods for closures
38         def count(Closure Input) {
39                 presenceSensors.count(Input)
40         }
41         def size() {
42                 presenceSensors.size()
43         }
44         def each(Closure Input) {
45                 presenceSensors.each(Input)
46         }
47         def find(Closure Input) {
48                 presenceSensors.find(Input)
49         }
50         def collect(Closure Input) {
51                 presenceSensors.collect(Input)
52         }
53
54
55         def currentValue(String deviceFeature) {
56                 presenceSensors[0].currentValue(deviceFeature)//It is called if we have only one device
57         }
58
59         def latestValue(String deviceFeature) {
60                 presenceSensors[0].latestValue(deviceFeature)//It is called if we have only one device
61         }
62
63         def getAt(int ix) {
64                 presenceSensors[ix]
65         }
66 }