Adding missing methods.
[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 presence = "not present"
15         private String currentPresence = "not present"
16         private String presenceLatestValue = "not present"
17
18                 
19         PresenceSensors(Closure sendEvent, int deviceNumbers, boolean init) {
20                 this.sendEvent = sendEvent              
21                 this.deviceNumbers = deviceNumbers
22                 this.presenceSensors = []
23
24                 if (init) {
25                         this.presence = "not present"
26                         this.currentPresence = "not present"
27                         this.presenceLatestValue = "not present"
28                 } else {
29                         this.presence = "present"
30                         this.currentPresence = "present"
31                         this.presenceLatestValue = "present"
32                 }
33                 presenceSensors.add(new PresenceSensor(id, label, displayName, this.presence, this.presenceLatestValue))
34         }
35
36         //By Model Checker
37         def setValue(LinkedHashMap eventDataMap) {
38                 if (eventDataMap["value"] != presenceSensors[0].presence) {
39                         this.presenceLatestValue = eventDataMap["value"]
40                         this.presence = eventDataMap["value"]
41                         this.currentPresence = eventDataMap["value"]
42                         presenceSensors[0].setValue(eventDataMap["value"])
43                         sendEvent(eventDataMap)
44                 }
45         }
46
47         //Methods for closures
48         def count(Closure Input) {
49                 presenceSensors.count(Input)
50         }
51         def size() {
52                 presenceSensors.size()
53         }
54         def each(Closure Input) {
55                 presenceSensors.each(Input)
56         }
57         def find(Closure Input) {
58                 presenceSensors.find(Input)
59         }
60         def sort(Closure Input) {
61                 presenceSensors.sort(Input)
62         }
63         def collect(Closure Input) {
64                 presenceSensors.collect(Input)
65         }
66
67         def currentState(String deviceFeature) {
68                 presenceSensors[0].currentState(deviceFeature)  
69         }
70
71         def currentValue(String deviceFeature) {
72                 presenceSensors[0].currentValue(deviceFeature)//It is called if we have only one device
73         }
74
75         def latestValue(String deviceFeature) {
76                 presenceSensors[0].latestValue(deviceFeature)//It is called if we have only one device
77         }
78
79         def statesSince(String info, Date dateObj) {
80                 return presenceSensors[0].statesSince()
81         }
82
83         def eventsSince(Date dateObj) {
84                 return presenceSensors[0].statesSince()
85         }
86
87         def getAt(int ix) {
88                 presenceSensors[ix]
89         }
90 }