Commit #9: More classes + Extractor with Rahmadi's editions + Fixing some bugs
[smartthings-infrastructure.git] / SmokeDetector / SmokeDetectors.groovy
diff --git a/SmokeDetector/SmokeDetectors.groovy b/SmokeDetector/SmokeDetectors.groovy
new file mode 100644 (file)
index 0000000..95bcb3c
--- /dev/null
@@ -0,0 +1,67 @@
+//Create a class for smoke detector
+package SmokeDetector
+import Timer.SimulatedTimer
+
+public class SmokeDetectors {
+       private int deviceNumbers
+       private List smokeDetectors
+       def sendEvent
+
+       //For one device(We cannot have obj.id)-> We should have obj[0].id
+       private String id = "smokeDetectorID0"
+       private String label = "smokeDetector0"
+       private String displayName = "smokeDetector0"
+       private String smoke = "clear"
+       private String currentSmokeValue = "clear"
+       private String smokeLatestValue = "clear"
+
+               
+       SmokeDetectors(Closure sendEvent, int deviceNumbers) {
+               this.sendEvent = sendEvent              
+               this.deviceNumbers = deviceNumbers
+               this.smokeDetectors = []
+
+               smokeDetectors.add(new SmokeDetector(id, label, displayName, this.currentSmokeValue, this.smokeLatestValue))
+       }
+
+       //By Model Checker
+       def setValue(LinkedHashMap eventDataMap) {
+               if (eventDataMap["value"] != smokeDetectors[0].currentSmokeValue) {
+                       smokeDetectors[0].setValue(eventDataMap["value"])
+                       this.smokeLatestValue = smokeDetectors[0].smokeLatestValue
+                       this.smoke = smokeDetectors[0].currentSmokeValue
+                       this.currentSmokeValue = smokeDetectors[0].currentSmokeValue
+                       sendEvent(eventDataMap)
+               }
+       }
+
+       //Methods for closures
+       def count(Closure Input) {
+               smokeDetectors.count(Input)
+       }
+       def size() {
+               smokeDetectors.size()
+       }
+       def each(Closure Input) {
+               smokeDetectors.each(Input)
+       }
+       def find(Closure Input) {
+               smokeDetectors.find(Input)
+       }
+       def collect(Closure Input) {
+               smokeDetectors.collect(Input)
+       }
+
+
+       def currentValue(String deviceFeature) {
+               smokeDetectors[0].currentValue(deviceFeature)//It is called if we have only one device
+       }
+
+       def latestValue(String deviceFeature) {
+               smokeDetectors[0].latestValue(deviceFeature)//It is called if we have only one device
+       }
+
+       def getAt(int ix) {
+               smokeDetectors[ix]
+       }
+}