2408a5aa5c726535b7efd4bc5267bc600b8545a3
[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) {
20                 this.sendEvent = sendEvent              
21                 this.deviceNumbers = deviceNumbers
22                 this.presenceSensors = []
23
24                 presenceSensors.add(new PresenceSensor(id, label, displayName, this.presence, this.presenceLatestValue))
25         }
26
27         //By Model Checker
28         def setValue(LinkedHashMap eventDataMap) {
29                 if (eventDataMap["value"] != presenceSensors[0].presence) {
30                         presenceSensors[0].setValue(eventDataMap["value"])
31                         this.presenceLatestValue = presenceSensors[0].presenceLatestValue
32                         this.presence = presenceSensors[0].presence
33                         this.currentPresence = presenceSensors[0].presence
34                         sendEvent(eventDataMap)
35                 }
36         }
37
38         //Methods for closures
39         def count(Closure Input) {
40                 presenceSensors.count(Input)
41         }
42         def size() {
43                 presenceSensors.size()
44         }
45         def each(Closure Input) {
46                 presenceSensors.each(Input)
47         }
48         def find(Closure Input) {
49                 presenceSensors.find(Input)
50         }
51         def collect(Closure Input) {
52                 presenceSensors.collect(Input)
53         }
54
55
56         def currentValue(String deviceFeature) {
57                 presenceSensors[0].currentValue(deviceFeature)//It is called if we have only one device
58         }
59
60         def latestValue(String deviceFeature) {
61                 presenceSensors[0].latestValue(deviceFeature)//It is called if we have only one device
62         }
63
64         def getAt(int ix) {
65                 presenceSensors[ix]
66         }
67 }