Infrastructure that works for all the locks' group!
[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
21                 
22         SmokeDetectors(Closure sendEvent, int deviceNumbers) {
23                 this.sendEvent = sendEvent              
24                 this.deviceNumbers = deviceNumbers
25                 this.smokeDetectors = []
26
27                 def init = Verify.getInt(0,2)
28                 if (init == 0) {
29                         this.currentSmokeValue = "clear"
30                         this.smokeLatestValue = "clear"
31                 } else if (init == 1) {
32                         this.currentSmokeValue = "detected"
33                         this.smokeLatestValue = "detected"
34                 } else {
35                         this.currentSmokeValue = "tested"
36                         this.smokeLatestValue = "tested"                
37                 }
38                 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue))
39         }
40
41         //By Model Checker
42         def setValue(LinkedHashMap eventDataMap) {
43                 if (eventDataMap["value"] != smokeDetectors[0].currentSmokeValue) {
44                         smokeDetectors[0].setValue(eventDataMap["value"])
45                         this.smokeLatestValue = smokeDetectors[0].smokeLatestValue
46                         this.smoke = smokeDetectors[0].currentSmokeValue
47                         this.currentSmokeValue = smokeDetectors[0].currentSmokeValue
48                         sendEvent(eventDataMap)
49                 }
50         }
51
52         //Methods for closures
53         def count(Closure Input) {
54                 smokeDetectors.count(Input)
55         }
56         def size() {
57                 smokeDetectors.size()
58         }
59         def each(Closure Input) {
60                 smokeDetectors.each(Input)
61         }
62         def find(Closure Input) {
63                 smokeDetectors.find(Input)
64         }
65         def collect(Closure Input) {
66                 smokeDetectors.collect(Input)
67         }
68
69
70         def currentValue(String deviceFeature) {
71                 smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
72         }
73
74         def latestValue(String deviceFeature) {
75                 smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device
76         }
77
78         def getAt(int ix) {
79                 smokeDetectors[ix]
80         }
81 }