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