Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[smartthings-infrastructure.git] / Thermostat / Thermostats.groovy
index cc955d17e74849d1c73c6b8c333f8827c6c13180..80a643b6a5ae0be8fbdaa4961f354e617254fab2 100644 (file)
@@ -2,6 +2,9 @@
 package Thermostat
 import Timer.SimulatedTimer
 
 package Thermostat
 import Timer.SimulatedTimer
 
+//JPF's Verify API
+import gov.nasa.jpf.vm.Verify
+
 public class Thermostats{
        int deviceNumbers       
        List thermostats        
 public class Thermostats{
        int deviceNumbers       
        List thermostats        
@@ -13,6 +16,7 @@ public class Thermostats{
        private String label = "thermostat0"
        private String displayName = "thermostat0"
        private int temperature = 66
        private String label = "thermostat0"
        private String displayName = "thermostat0"
        private int temperature = 66
+       private int currentTemperature = 66
        private int currentCoolingSetpoint = 70
        private int currentHeatingSetpoint = 50
        private int coolingSetpoint = 70
        private int currentCoolingSetpoint = 70
        private int currentHeatingSetpoint = 50
        private int coolingSetpoint = 70
@@ -28,6 +32,13 @@ public class Thermostats{
        private String thermostatMode = "auto"
        private String currentThermostatMode = "auto"
        private String climateName = ""
        private String thermostatMode = "auto"
        private String currentThermostatMode = "auto"
        private String climateName = ""
+       private String thermostatLatestMode = "auto"
+       private String thermostatLatestOperatingState = "cooling"
+       private String thermostatLatestFanMode = "auto"
+       private int latestCoolingSetPoint = 70
+       private int latestThermostatSetPoint = 60
+       private int latestHeatingSetPoint = 50
+
 
        Thermostats(Closure sendEvent, int deviceNumbers) {
                this.sendEvent = sendEvent
 
        Thermostats(Closure sendEvent, int deviceNumbers) {
                this.sendEvent = sendEvent
@@ -35,10 +46,72 @@ public class Thermostats{
                this.deviceNumbers = deviceNumbers
                this.thermostats = []
 
                this.deviceNumbers = deviceNumbers
                this.thermostats = []
 
+               /*def initTemperature = Verify.getIntFromList(60, 66, 70)
+               this.temperature = initTemperature
+               this.currentTemperature = initTemperature
+               
+               def initCoolingSetpoint = Verify.getIntFromList(70, 80, 90)
+               this.currentCoolingSetpoint = initCoolingSetpoint
+               this.coolingSetpoint = initCoolingSetpoint
+               
+               def initHeatingSetpoint = Verify.getIntFromList(20, 35, 50)
+               this.currentHeatingSetpoint = initHeatingSetpoint
+               this.heatingSetpoint = initHeatingSetpoint
+               
+               def initThermostatSetpoint = Verify.getIntFromList(50, 60, 70)
+               this.thermostatSetpoint = initThermostatSetpoint
+               
+               def initFanMode = Verify.getInt(0,4)
+               if (initFanMode == 0) {
+                       this.thermostatFanMode = "auto"
+                       this.thermostatLatestFanMode = "auto"
+               } else if (initFanMode == 1) {
+                       this.thermostatFanMode = "fanCirculate"
+                       this.thermostatLatestFanMode = "fanCirculate"
+               } else if (initFanMode == 2) {
+                       this.thermostatFanMode = "circulate"
+                       this.thermostatLatestFanMode = "circulate"
+               } else if (initFanMode == 3) {
+                       this.thermostatFanMode = "fanOn"
+                       this.thermostatLatestFanMode = "fanOn"
+               } else {
+                       this.thermostatFanMode = "on"
+                       this.thermostatLatestFanMode = "on"
+               }
+
+               def initMode = Verify.getInt(0,4)
+               if (initMode == 0) {
+                       this.thermostatMode = "auto"
+                       this.currentThermostatMode = "auto"
+                       this.thermostatLatestMode = "auto"
+               } else if (initMode == 1) {
+                       this.thermostatMode = "cool"
+                       this.currentThermostatMode = "cool"
+                       this.thermostatLatestMode = "cool"
+               } else if (initMode == 2) {
+                       this.thermostatMode = "emergencyHeat"
+                       this.currentThermostatMode = "emergencyHeat"
+                       this.thermostatLatestMode = "emergencyHeat"
+               } else if (initMode == 3) {
+                       this.thermostatMode = "heat"
+                       this.currentThermostatMode = "heat"
+                       this.thermostatLatestMode = "heat"
+               } else {
+                       this.thermostatMode = "off"
+                       this.currentThermostatMode = "off"
+<<<<<<< HEAD
+                       this.thermostatLatestMode = "off"
+               }
+=======
+               }*/
+>>>>>>> a02c9807815a35c0f57241ee6510a3d312499049
+
                thermostats.add(new Thermostat(sendEvent, id, label, displayName, this.temperature, this.currentCoolingSetpoint, 
                                                this.currentHeatingSetpoint, this.coolingSetpoint, this.thermostatSetpoint, this.heatingSetpoint, this.coolingSetpointRange,
                                                this.thermostatSetpointRange, this.heatingSetpointRange, this.supportedThermostatFanModes, this.supportedThermostatModes,
                thermostats.add(new Thermostat(sendEvent, id, label, displayName, this.temperature, this.currentCoolingSetpoint, 
                                                this.currentHeatingSetpoint, this.coolingSetpoint, this.thermostatSetpoint, this.heatingSetpoint, this.coolingSetpointRange,
                                                this.thermostatSetpointRange, this.heatingSetpointRange, this.supportedThermostatFanModes, this.supportedThermostatModes,
-                                               this.thermostatOperatingState, this.thermostatFanMode,  this.thermostatMode, this.climateName))
+                                               this.thermostatOperatingState, this.thermostatFanMode,  this.thermostatMode, this.climateName, 
+                                              this.thermostatLatestMode, this.thermostatLatestOperatingState, this.thermostatLatestFanMode, this.latestCoolingSetPoint,
+                                              this.latestThermostatSetPoint, this.latestHeatingSetPoint))
        }
 
        //Methods for closures
        }
 
        //Methods for closures
@@ -54,21 +127,38 @@ public class Thermostats{
        def find(Closure Input) {
                thermostats.find(Input)
        }
        def find(Closure Input) {
                thermostats.find(Input)
        }
+       def sort(Closure Input) {
+               thermostats.sort(Input)
+       }
        def collect(Closure Input) {
                thermostats.collect(Input)
        }
 
        //By Apps
        def setCoolingSetpoint(int coolingSetpoint) {
        def collect(Closure Input) {
                thermostats.collect(Input)
        }
 
        //By Apps
        def setCoolingSetpoint(int coolingSetpoint) {
-               thermostats[0].setCoolingSetpoint(coolingSetpoint)
-               this.currentCoolingSetpoint = coolingSetpoint
-               this.coolingSetpoint = coolingSetpoint
+               if (coolingSetpoint != this.coolingSetpoint) {
+                       this.latestCoolingSetPoint = coolingSetpoint
+                       this.currentCoolingSetpoint = coolingSetpoint
+                       this.coolingSetpoint = coolingSetpoint
+                       thermostats[0].setCoolingSetpoint(coolingSetpoint)
+               }
+       }
+
+       def setCoolingSetpoint(String coolingSetpoint) {
+               setCoolingSetpoint(coolingSetpoint.toInteger())
        }
 
        def setHeatingSetpoint(int heatingSetpoint) {
        }
 
        def setHeatingSetpoint(int heatingSetpoint) {
-               thermostats[0].setHeatingSetpoint(heatingSetpoint)
-               this.currentHeatingSetpoint = heatingSetpoint
-               this.heatingSetpoint = heatingSetpoint
+               if (heatingSetpoint != this.heatingSetpoint) {
+                       this.latestHeatingSetPoint = heatingSetpoint
+                       this.currentHeatingSetpoint = heatingSetpoint
+                       this.heatingSetpoint = heatingSetpoint
+                       thermostats[0].setHeatingSetpoint(heatingSetpoint)
+               }
+       }
+
+       def setHeatingSetpoint(String heatingSetpoint) {
+               setHeatingSetpoint(heatingSetpoint.toInteger())
        }
 
        def setSchedule() {
        }
 
        def setSchedule() {
@@ -76,101 +166,138 @@ public class Thermostats{
        }
 
        def setThermostatFanMode(String thermostatFanMode) {
        }
 
        def setThermostatFanMode(String thermostatFanMode) {
-               thermostats[0].setThermostatFanMode(thermostatFanMode)
-               this.thermostatFanMode = thermostatFanMode
+               if (thermostatFanMode != this.thermostatFanMode) {
+                       this.thermostatLatestFanMode = thermostatFanMode
+                       this.thermostatFanMode = thermostatFanMode
+                       thermostats[0].setThermostatFanMode(thermostatFanMode)
+               }
        }
 
        def setThermostatMode(String thermostatMode) {
        }
 
        def setThermostatMode(String thermostatMode) {
-               thermostats[0].setThermostatMode(thermostatMode)
-               this.thermostatMode = thermostatMode
-               this.currentThermostatMode = currentThermostatMode
+               if (thermostatMode != this.thermostatMode) {
+                       this.thermostatLatestMode = thermostatMode
+                       this.thermostatMode = thermostatMode
+                       this.currentThermostatMode = currentThermostatMode
+                       thermostats[0].setThermostatMode(thermostatMode)
+               }
        }
 
        def cool() {
        }
 
        def cool() {
-               thermostats[0].cool()
-               this.thermostatMode = "cool"
-               this.currentThermostatMode = "cool"
+               if (thermostatMode != "cool") {
+                       this.thermostatLatestMode = "cool"
+                       this.thermostatMode = "cool"
+                       this.currentThermostatMode = "cool"
+                       thermostats[0].cool()
+               }
        }
 
        def heat() {
        }
 
        def heat() {
-               thermostats[0].heat()
-               this.thermostatMode = "heat"
-               this.currentThermostatMode = "heat"
+               if (thermostatMode != "heat") {
+                       this.thermostatLatestMode = "heat"
+                       this.thermostatMode = "heat"
+                       this.currentThermostatMode = "heat"
+                       thermostats[0].heat()
+               }
        }
 
        def auto() {
        }
 
        def auto() {
-               thermostats[0].auto()
-               this.thermostatMode = "auto"
-               this.currentThermostatMode = "auto"
+               if (thermostatMode != "auto") {
+                       this.thermostatLatestMode = "auto"
+                       this.thermostatMode = "auto"
+                       this.currentThermostatMode = "auto"
+                       thermostats[0].auto()
+               }
        }
 
        def off() {
        }
 
        def off() {
-               thermostats[0].off()
-               this.thermostatMode = "off"
-               this.currentThermostatMode = "off"
+               if (thermostatMode != "off") {
+                       this.thermostatLatestMode = "off"
+                       this.thermostatMode = "off"
+                       this.currentThermostatMode = "off"
+                       thermostats[0].off()
+               }
        }
 
        def setClimate(String info, String givenClimateName) {
        }
 
        def setClimate(String info, String givenClimateName) {
-               thermostats[0].setClimate(info, givenClimateName)
-               this.climateName = givenClimateName
+               if (givenClimateName != climateName) {
+                       this.climateName = givenClimateName
+                       thermostats[0].setClimate(info, givenClimateName)
+               }
        }
 
        def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
        }
 
        def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
-               thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
-               this.currentCoolingSetpoint = coolingSetpoint
-               this.coolingSetpoint = coolingSetpoint
-               this.currentHeatingSetpoint = heatingSetpoint
-               this.heatingSetpoint = heatingSetpoint
+               if ((coolingSetpoint != this.coolingSetpoint) || (heatingSetpoint != this.heatingSetpoint)) {
+                       this.currentCoolingSetpoint = coolingSetpoint
+                       this.coolingSetpoint = coolingSetpoint
+                       this.currentHeatingSetpoint = heatingSetpoint
+                       this.heatingSetpoint = heatingSetpoint
+                       thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
+               }
        }
 
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
                if (eventDataMap["name"] == "temperature") {
        }
 
        //By Model Checker
        def setValue(LinkedHashMap eventDataMap) {
                if (eventDataMap["name"] == "temperature") {
-                       if (eventDataMap["value"] != thermostats[0].temperature) {
+                       if (eventDataMap["value"].toInteger() != thermostats[0].temperature) {
+                               this.temperature = eventDataMap["value"].toInteger()
+                               this.currentTemperature = eventDataMap["value"].toInteger()
                                thermostats[0].setValue(eventDataMap["value"], "temperature")
                                thermostats[0].setValue(eventDataMap["value"], "temperature")
-                               this.temperature = thermostats[0].temperature
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "heatingSetpoint") {
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "heatingSetpoint") {
-                       if (eventDataMap["value"] != thermostats[0].heatingSetpoint) {
+                       if (eventDataMap["value"].toInteger() != thermostats[0].heatingSetpoint) {
+                               this.latestHeatingSetpoint = eventDataMap["value"].toInteger()
+                               this.heatingSetpoint = eventDataMap["value"].toInteger()
                                thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
                                thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
-                               this.heatingSetpoint = thermostats[0].heatingSetpoint
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "coolingSetpoint") {
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "coolingSetpoint") {
-                       if (eventDataMap["value"] != thermostats[0].coolingSetpoint) {
+                       if (eventDataMap["value"].toInteger() != thermostats[0].coolingSetpoint) {
+                               this.latestCoolingSetPoint = eventDataMap["value"].toInteger()
+                               this.coolingSetpoint = eventDataMap["value"].toInteger()
                                thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
                                thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
-                               this.coolingSetpoint = thermostats[0].coolingSetpoint
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatSetpoint") {
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatSetpoint") {
-                       if (eventDataMap["value"] != thermostats[0].thermostatSetpoint) {
+                       if (eventDataMap["value"].toInteger() != thermostats[0].thermostatSetpoint) {
+                               this.latestThermostatSetpoint = eventDataMap["value"].toInteger()
+                               this.thermostatSetpoint = eventDataMap["value"].toInteger()
                                thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
                                thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
-                               this.thermostatSetpoint = thermostats[0].thermostatSetpoint
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatMode") {
                        if (eventDataMap["value"] != thermostats[0].thermostatMode) {
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatMode") {
                        if (eventDataMap["value"] != thermostats[0].thermostatMode) {
+                               this.thermostatLatestMode = eventDataMap["value"]
+                               this.thermostatMode = eventDataMap["value"]
+                               this.currentThermostatMode = eventDataMap["value"]
                                thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
                                thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
-                               this.thermostatMode = thermostats[0].thermostatMode
-                               this.currentThermostatMode = thermostats[0].currentThermostatMode
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatFanMode") {
                        if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatFanMode") {
                        if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
+                               this.thermostatLatestFanMode = eventDataMap["value"]
+                               this.thermostatFanMode = eventDataMap["value"]
                                thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
                                thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
-                               this.thermostatFanMode = thermostats[0].thermostatFanMode
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatOperatingState") {
                        if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
                                sendEvent(eventDataMap)
                        }
                } else if (eventDataMap["name"] == "thermostatOperatingState") {
                        if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
+                               this.thermostatLatestOperatingState = eventDataMap["value"]
+                               this.thermostatOperatingState = eventDataMap["value"]
                                thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
                                thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
-                               this.thermostatOperatingState = thermostats[0].thermostatOperatingState
                                sendEvent(eventDataMap)
                        }
                }
        }
 
                                sendEvent(eventDataMap)
                        }
                }
        }
 
+       def currentValue(String deviceFeature) {
+               thermostats[0].currentValue(deviceFeature)
+       }
+
+       def latestValue(String deviceFeature) {
+               thermostats[0].latestValue(deviceFeature)
+       }
+
        def getAt(int ix) {
                thermostats[ix]
        }
        def getAt(int ix) {
                thermostats[ix]
        }