Completing model-check automation script with result extraction.
[smartthings-infrastructure.git] / CarbonMonoxideDetector / CarbonMonoxideDetectors.groovy
1 //Create a class for carbon monoxide detector
2 package CarbonMonoxideDetector
3 import Timer.SimulatedTimer
4
5 //JPF's Verify API
6 import gov.nasa.jpf.vm.Verify
7
8 public class CarbonMonoxideDetectors {
9         private int deviceNumbers
10         private List carbonMonoxideDetectors
11         def sendEvent
12
13         //For one device(We cannot have obj.id)-> We should have obj[0].id
14         private String id = "carbonMonoxideDetectorID0"
15         private String label = "carbonMonoxideDetector0"
16         private String displayName = "carbonMonoxideDetector0"
17         private String carbonMonoxide = "clear"
18         private String currentCarbonMonoxideValue = "clear"
19         private String carbonMonoxideLatestValue = "clear"
20
21                 
22         CarbonMonoxideDetectors(Closure sendEvent, int deviceNumbers) {
23                 this.sendEvent = sendEvent              
24                 this.deviceNumbers = deviceNumbers
25                 this.carbonMonoxideDetectors = []
26                 
27                 def init = Verify.getInt(0,2)
28                 if (init == 0) {
29                         this.carbonMonoxide = "clear"
30                         this.carbonMonoxideLatestValue = "clear"
31                 } else if (init == 1) {
32                         this.carbonMonoxide = "detected"
33                         this.carbonMonoxideLatestValue = "detected"
34                 } else {
35                         this.carbonMonoxide = "tested"
36                         this.carbonMonoxideLatestValue = "tested"               
37                 }
38                 carbonMonoxideDetectors.add(new CarbonMonoxideDetector(id, label, displayName, this.carbonMonoxide, this.carbonMonoxideLatestValue))
39         }
40
41         //By Model Checker
42         def setValue(LinkedHashMap eventDataMap) {
43                 if (eventDataMap["value"] != carbonMonoxideDetectors[0].currentCarbonMonoxideValue) {
44                         carbonMonoxideDetectors[0].setValue(eventDataMap["value"])
45                         this.carbonMonoxideLatestValue = carbonMonoxideDetectors[0].carbonMonoxideLatestValue
46                         this.carbonMonoxide = carbonMonoxideDetectors[0].currentCarbonMonoxideValue
47                         this.currentCarbonMonoxideValue = carbonMonoxideDetectors[0].currentCarbonMonoxideValue
48                         sendEvent(eventDataMap)
49                 }
50         }
51
52         //Methods for closures
53         def count(Closure Input) {
54                 carbonMonoxideDetectors.count(Input)
55         }
56         def size() {
57                 carbonMonoxideDetectors.size()
58         }
59         def each(Closure Input) {
60                 carbonMonoxideDetectors.each(Input)
61         }
62         def find(Closure Input) {
63                 carbonMonoxideDetectors.find(Input)
64         }
65         def collect(Closure Input) {
66                 carbonMonoxideDetectors.collect(Input)
67         }
68
69
70         def currentValue(String deviceFeature) {
71                 carbonMonoxideDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
72         }
73
74         def latestValue(String deviceFeature) {
75                 carbonMonoxideDetectors[0].latestValue(deviceFeature)//It is called if we have only one device
76         }
77
78         def getAt(int ix) {
79                 carbonMonoxideDetectors[ix]
80         }
81 }