Some minor changes!
[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         private String carbonMonoxide = "clear"
18         private String currentCarbonMonoxideValue = "clear"
19         private String carbonMonoxideLatestValue = "clear"
20         private int battery = 50
21     private int batteryValue = 50
22         private int batteryLatestValue = 50
23
24                 
25         SmokeDetectors(Closure sendEvent, int deviceNumbers, boolean init) {
26                 this.sendEvent = sendEvent              
27                 this.deviceNumbers = deviceNumbers
28                 this.smokeDetectors = []
29
30                 if (init) {
31                         this.currentSmokeValue = "clear"
32                         this.smokeLatestValue = "clear"
33                         this.carbonMonoxide = "clear"
34                         this.currentCarbonMonoxideValue = "clear"
35                         this.carbonMonoxideLatestValue = "clear"
36                         this.battery = 50
37                         this.batteryValue = 50
38                         this.batteryLatestValue = 50
39                 } else {
40                         this.currentSmokeValue = "detected"
41                         this.smokeLatestValue = "detected"
42                         this.carbonMonoxide = "detected"
43                         this.currentCarbonMonoxideValue = "detected"
44                         this.carbonMonoxideLatestValue = "detected"
45                         this.battery = 60
46                         this.batteryValue = 60
47                         this.batteryLatestValue = 60
48                 }
49                 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue, this.carbonMonoxide, this.carbonMonoxideLatestValue, this.battery))
50         }
51
52         //By Model Checker
53         def setValue(LinkedHashMap eventDataMap) {
54                 if (eventDataMap["name"].contains("smoke")) {
55                         if (eventDataMap["value"] != smokeDetectors[0].currentSmokeValue) {
56                                 this.smokeLatestValue = eventDataMap["value"]
57                                 this.smoke = eventDataMap["value"]
58                                 this.currentSmokeValue = eventDataMap["value"]
59                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
60                                 sendEvent(eventDataMap)
61                         }
62                 } else if (eventDataMap["name"].contains("carbonMonoxide")) {
63                         if (eventDataMap["value"] != smokeDetectors[0].currentCarbonMonoxideValue) {
64                                 this.carbonMonoxideLatestValue = eventDataMap["value"]
65                                 this.carbonMonoxide = eventDataMap["value"]
66                                 this.currentCarbonMonoxideValue = eventDataMap["value"]
67                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
68                                 sendEvent(eventDataMap)
69                         }
70                 } else if (eventDataMap["name"].contains("battery")) {
71                         if (eventDataMap["value"].toInteger() != smokeDetectors[0].battery) {
72                                 this.battery = eventDataMap["value"].toInteger()
73                                 this.batteryLatestValue = eventDataMap["value"].toInteger()
74                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
75                                 sendEvent(eventDataMap)
76                         }
77                 }
78         }
79
80         //Methods for closures
81         def count(Closure Input) {
82                 smokeDetectors.count(Input)
83         }
84         def size() {
85                 smokeDetectors.size()
86         }
87         def each(Closure Input) {
88                 smokeDetectors.each(Input)
89         }
90         def find(Closure Input) {
91                 smokeDetectors.find(Input)
92         }
93         def sort(Closure Input) {
94                 smokeDetectors.sort(Input)
95         }
96         def collect(Closure Input) {
97                 smokeDetectors.collect(Input)
98         }
99
100
101         def currentValue(String deviceFeature) {
102                 smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
103         }
104
105         def latestValue(String deviceFeature) {
106                 smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device
107         }
108
109         def getAt(int ix) {
110                 smokeDetectors[ix]
111         }
112 }