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