1 //Create a class for smoke detector
3 import Timer.SimulatedTimer
6 import gov.nasa.jpf.vm.Verify
8 public class SmokeDetectors {
9 private int deviceNumbers
10 private List smokeDetectors
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
27 SmokeDetectors(Closure sendEvent, int deviceNumbers) {
28 this.sendEvent = sendEvent
29 this.deviceNumbers = deviceNumbers
30 this.smokeDetectors = []
32 def initBattery = Verify.getIntFromList(30, 50, 70)
33 this.battery = initBattery
34 this.batteryLatestValue = initBattery
36 def initSmoke = Verify.getInt(0,2)
38 this.currentSmokeValue = "clear"
39 this.smokeLatestValue = "clear"
40 } else if (initSmoke == 1) {
41 this.currentSmokeValue = "detected"
42 this.smokeLatestValue = "detected"
44 this.currentSmokeValue = "tested"
45 this.smokeLatestValue = "tested"
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"
56 this.currentCarbonMonoxideValue = "tested"
57 this.carbonMonoxideLatestValue = "tested"
59 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue, this.currentCarbonMonoxideValue,
60 this.carbonMonoxideLatestValue, this.battery))
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)
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)
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)
91 //Methods for closures
92 def count(Closure Input) {
93 smokeDetectors.count(Input)
98 def each(Closure Input) {
99 smokeDetectors.each(Input)
101 def find(Closure Input) {
102 smokeDetectors.find(Input)
104 def sort(Closure Input) {
105 smokeDetectors.sort(Input)
107 def collect(Closure Input) {
108 smokeDetectors.collect(Input)
112 def currentValue(String deviceFeature) {
113 smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
116 def latestValue(String deviceFeature) {
117 smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device