Adding commands in run.sh.
[smartthings-infrastructure.git] / Battery / Batteries.groovy
index ce4a187d3a4b06040fc940f902b73b39b6805fa6..732b6bdec6dc49a42b1b72e5031713029644c2ad 100644 (file)
@@ -12,23 +12,35 @@ public class Batteries {
        private String label = "battery0"
        private String displayName = "battery0"
        private int battery = 50
-       private String currentBattery = 50
+       private int currentBattery = 50
+       private int batteryLatestValue = 50
 
                
-       Batteries(Closure sendEvent, int deviceNumbers) {
+       Batteries(Closure sendEvent, int deviceNumbers, boolean init) {
                this.sendEvent = sendEvent              
                this.deviceNumbers = deviceNumbers
                this.batteries = []
 
+               if (init) {
+                       this.battery = 50
+                       this.currentBattery = 50
+                       this.batteryLatestValue = 50
+               } else {
+                       this.battery = 35
+                       this.currentBattery = 35
+                       this.batteryLatestValue = 35
+               }
+
                batteries.add(new Battery(id, label, displayName, this.battery))
        }
 
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
-               if (eventDataMap["value"] != batteries[0].battery) {
+               if (eventDataMap["value"].toInteger() != batteries[0].battery) {
+                       this.battery = eventDataMap["value"].toInteger()
+                       this.currentBattery = eventDataMap["value"].toInteger()
+                       this.batteryLatestValue = eventDataMap["value"].toInteger()
                        batteries[0].setValue(eventDataMap["value"])
-                       this.battery = batteries[0].battery
-                       this.currentBattery = batteries[0].battery
                        sendEvent(eventDataMap)
                }
        }
@@ -43,6 +55,9 @@ public class Batteries {
        def each(Closure Input) {
                batteries.each(Input)
        }
+       def sort(Closure Input) {
+               batteries.sort(Input)
+       }
        def find(Closure Input) {
                batteries.find(Input)
        }
@@ -55,6 +70,10 @@ public class Batteries {
                batteries[0].currentValue(deviceFeature)//It is called if we have only one device
        }
 
+       def latestValue(String deviceFeature) {
+               batteries[0].latestValue(deviceFeature)//It is called if we have only one device
+       }
+
        def getAt(int ix) {
                batteries[ix]
        }