Changes in classes: new concept for latest value + all types of events generated...
[smartthings-infrastructure.git] / SmokeDetector / SmokeDetectors.groovy
1 //Create a class for smoke detector
2 package SmokeDetector
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class SmokeDetectors {
9         private int deviceNumbers
10         private List smokeDetectors
11         def sendEvent
12
13         //For one device(We cannot have obj.id)-> We should have obj[0].id
14         private String id = "smokeDetectorID0"
15         private String label = "smokeDetector0"
16         private String displayName = "smokeDetector0"
17         private String smoke = "clear"
18         private String currentSmokeValue = "clear"
19         private String smokeLatestValue = "clear"
20         private String carbonMonoxide = "clear"
21         private String currentCarbonMonoxideValue = "clear"
22         private String carbonMonoxideLatestValue = "clear"
23         private int battery = 50
24         private int batteryLatestValue = 50
25
26                 
27         SmokeDetectors(Closure sendEvent, int deviceNumbers) {
28                 this.sendEvent = sendEvent              
29                 this.deviceNumbers = deviceNumbers
30                 this.smokeDetectors = []
31
32                 def initBattery = Verify.getIntFromList(30, 50, 70)
33                 this.battery = initBattery
34                 this.batteryLatestValue = initBattery
35
36                 def initSmoke = Verify.getInt(0,2)
37                 if (initSmoke == 0) {
38                         this.currentSmokeValue = "clear"
39                         this.smokeLatestValue = "clear"
40                 } else if (initSmoke == 1) {
41                         this.currentSmokeValue = "detected"
42                         this.smokeLatestValue = "detected"
43                 } else {
44                         this.currentSmokeValue = "tested"
45                         this.smokeLatestValue = "tested"                
46                 }
47
48                 def initCarbonMonoxide = Verify.getInt(0,2)
49                 if (initCarbonMonoxide == 0) {
50                         this.currentCarbonMonoxideValue = "clear"
51                         this.carbonMonoxideLatestValue = "clear"
52                 } else if (initCarbonMonoxide == 1) {
53                         this.currentCarbonMonoxideValue = "detected"
54                         this.carbonMonoxideLatestValue = "detected"
55                 } else {
56                         this.currentCarbonMonoxideValue = "tested"
57                         this.carbonMonoxideLatestValue = "tested"               
58                 }
59                 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue, this.currentCarbonMonoxideValue,
60                                                      this.carbonMonoxideLatestValue, this.battery))
61         }
62
63         //By Model Checker
64         def setValue(LinkedHashMap eventDataMap) {
65                 if (eventDataMap["name"].contains("smoke")) {
66                         if (eventDataMap["value"] != smokeDetectors[0].currentSmokeValue) {
67                                 this.smokeLatestValue = eventDataMap["value"]
68                                 this.smoke = eventDataMap["value"]
69                                 this.currentSmokeValue = eventDataMap["value"]
70                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
71                                 sendEvent(eventDataMap)
72                         }
73                 } else if (eventDataMap["name"].contains("carbonMonoxide")) {
74                         if (eventDataMap["value"] != smokeDetectors[0].currentCarbonMonoxideValue) {
75                                 this.carbonMonoxideLatestValue = eventDataMap["value"]
76                                 this.carbonMonoxide = eventDataMap["value"]
77                                 this.currentCarbonMonoxideValue = eventDataMap["value"]
78                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
79                                 sendEvent(eventDataMap)
80                         }
81                 } else if (eventDataMap["name"].contains("battery")) {
82                         if (eventDataMap["value"].toInteger() != smokeDetectors[0].battery) {
83                                 this.battery = eventDataMap["value"].toInteger()
84                                 this.batteryLatestValue = eventDataMap["value"].toInteger()
85                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
86                                 sendEvent(eventDataMap)
87                         }
88                 }
89         }
90
91         //Methods for closures
92         def count(Closure Input) {
93                 smokeDetectors.count(Input)
94         }
95         def size() {
96                 smokeDetectors.size()
97         }
98         def each(Closure Input) {
99                 smokeDetectors.each(Input)
100         }
101         def find(Closure Input) {
102                 smokeDetectors.find(Input)
103         }
104         def sort(Closure Input) {
105                 smokeDetectors.sort(Input)
106         }
107         def collect(Closure Input) {
108                 smokeDetectors.collect(Input)
109         }
110
111
112         def currentValue(String deviceFeature) {
113                 smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
114         }
115
116         def latestValue(String deviceFeature) {
117                 smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device
118         }
119
120         def getAt(int ix) {
121                 smokeDetectors[ix]
122         }
123 }