1 //Create a class for smoke detector
3 import Timer.SimulatedTimer
5 public class SmokeDetectors {
6 private int deviceNumbers
7 private List smokeDetectors
10 //For one device(We cannot have obj.id)-> We should have obj[0].id
11 private String id = "smokeDetectorID0"
12 private String label = "smokeDetector0"
13 private String displayName = "smokeDetector0"
14 private String smoke = "clear"
15 private String currentSmokeValue = "clear"
16 private String smokeLatestValue = "clear"
17 private String carbonMonoxide = "clear"
18 private String currentCarbonMonoxideValue = "clear"
19 private String carbonMonoxideLatestValue = "clear"
20 private int battery = 50
21 private int batteryValue = 50
22 private int batteryLatestValue = 50
25 SmokeDetectors(Closure sendEvent, int deviceNumbers, boolean init) {
26 this.sendEvent = sendEvent
27 this.deviceNumbers = deviceNumbers
28 this.smokeDetectors = []
31 this.currentSmokeValue = "clear"
32 this.smokeLatestValue = "clear"
33 this.carbonMonoxide = "clear"
34 this.currentCarbonMonoxideValue = "clear"
35 this.carbonMonoxideLatestValue = "clear"
37 this.batteryValue = 50
38 this.batteryLatestValue = 50
40 this.currentSmokeValue = "detected"
41 this.smokeLatestValue = "detected"
42 this.carbonMonoxide = "detected"
43 this.currentCarbonMonoxideValue = "detected"
44 this.carbonMonoxideLatestValue = "detected"
46 this.batteryValue = 60
47 this.batteryLatestValue = 60
49 smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue, this.carbonMonoxide, this.carbonMonoxideLatestValue, this.battery))
53 def setValue(LinkedHashMap eventDataMap) {
54 if (eventDataMap["name"].contains("smoke")) {
55 if (eventDataMap["value"] != smokeDetectors[0].currentSmokeValue) {
56 this.smokeLatestValue = eventDataMap["value"]
57 this.smoke = eventDataMap["value"]
58 this.currentSmokeValue = eventDataMap["value"]
59 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
60 sendEvent(eventDataMap)
62 } else if (eventDataMap["name"].contains("carbonMonoxide")) {
63 if (eventDataMap["value"] != smokeDetectors[0].currentCarbonMonoxideValue) {
64 this.carbonMonoxideLatestValue = eventDataMap["value"]
65 this.carbonMonoxide = eventDataMap["value"]
66 this.currentCarbonMonoxideValue = eventDataMap["value"]
67 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
68 sendEvent(eventDataMap)
70 } else if (eventDataMap["name"].contains("battery")) {
71 if (eventDataMap["value"].toInteger() != smokeDetectors[0].battery) {
72 this.battery = eventDataMap["value"].toInteger()
73 this.batteryLatestValue = eventDataMap["value"].toInteger()
74 smokeDetectors[0].setValue(eventDataMap["value"], eventDataMap["name"])
75 sendEvent(eventDataMap)
80 //Methods for closures
81 def count(Closure Input) {
82 smokeDetectors.count(Input)
87 def each(Closure Input) {
88 smokeDetectors.each(Input)
90 def find(Closure Input) {
91 smokeDetectors.find(Input)
93 def sort(Closure Input) {
94 smokeDetectors.sort(Input)
96 def collect(Closure Input) {
97 smokeDetectors.collect(Input)
101 def currentValue(String deviceFeature) {
102 smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
105 def latestValue(String deviceFeature) {
106 smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device