Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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 <<<<<<< HEAD
33                 def initBattery = Verify.getIntFromList(30, 50, 70)
34                 this.battery = initBattery
35                 this.batteryLatestValue = initBattery
36
37                 def initSmoke = Verify.getInt(0,2)
38                 if (initSmoke == 0) {
39 =======
40                 /*def init = Verify.getInt(0,2)
41                 if (init == 0) {
42 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
43                         this.currentSmokeValue = "clear"
44                         this.smokeLatestValue = "clear"
45                 } else if (initSmoke == 1) {
46                         this.currentSmokeValue = "detected"
47                         this.smokeLatestValue = "detected"
48                 } else {
49                         this.currentSmokeValue = "tested"
50                         this.smokeLatestValue = "tested"                
51 <<<<<<< HEAD
52                 }
53
54                 def initCarbonMonoxide = Verify.getInt(0,2)
55                 if (initCarbonMonoxide == 0) {
56                         this.currentCarbonMonoxideValue = "clear"
57                         this.carbonMonoxideLatestValue = "clear"
58                 } else if (initCarbonMonoxide == 1) {
59                         this.currentCarbonMonoxideValue = "detected"
60                         this.carbonMonoxideLatestValue = "detected"
61                 } else {
62                         this.currentCarbonMonoxideValue = "tested"
63                         this.carbonMonoxideLatestValue = "tested"               
64                 }
65                 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue, this.currentCarbonMonoxideValue,
66                                                      this.carbonMonoxideLatestValue, this.battery))
67 =======
68                 }*/
69                 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue))
70 >>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
71         }
72
73         //By Model Checker
74         def setValue(LinkedHashMap eventDataMap) {
75                 if (eventDataMap["name"].contains("smoke")) {
76                         if (eventDataMap["value"] != smokeDetectors[0].currentSmokeValue) {
77                                 this.smokeLatestValue = eventDataMap["value"]
78                                 this.smoke = eventDataMap["value"]
79                                 this.currentSmokeValue = eventDataMap["value"]
80                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
81                                 sendEvent(eventDataMap)
82                         }
83                 } else if (eventDataMap["name"].contains("carbonMonoxide")) {
84                         if (eventDataMap["value"] != smokeDetectors[0].currentCarbonMonoxideValue) {
85                                 this.carbonMonoxideLatestValue = eventDataMap["value"]
86                                 this.carbonMonoxide = eventDataMap["value"]
87                                 this.currentCarbonMonoxideValue = eventDataMap["value"]
88                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
89                                 sendEvent(eventDataMap)
90                         }
91                 } else if (eventDataMap["name"].contains("battery")) {
92                         if (eventDataMap["value"].toInteger() != smokeDetectors[0].battery) {
93                                 this.battery = eventDataMap["value"].toInteger()
94                                 this.batteryLatestValue = eventDataMap["value"].toInteger()
95                                 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
96                                 sendEvent(eventDataMap)
97                         }
98                 }
99         }
100
101         //Methods for closures
102         def count(Closure Input) {
103                 smokeDetectors.count(Input)
104         }
105         def size() {
106                 smokeDetectors.size()
107         }
108         def each(Closure Input) {
109                 smokeDetectors.each(Input)
110         }
111         def find(Closure Input) {
112                 smokeDetectors.find(Input)
113         }
114         def sort(Closure Input) {
115                 smokeDetectors.sort(Input)
116         }
117         def collect(Closure Input) {
118                 smokeDetectors.collect(Input)
119         }
120
121
122         def currentValue(String deviceFeature) {
123                 smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
124         }
125
126         def latestValue(String deviceFeature) {
127                 smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device
128         }
129
130         def getAt(int ix) {
131                 smokeDetectors[ix]
132         }
133 }