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