Infrastruction modification
[smartthings-infrastructure.git] / Thermostat / Thermostat.groovy
index 720ba8270d078126988a82a9dfb37e5acdbe27be..40eea8d19b59bba0bdcf96ffadc8e32a1c4796f2 100644 (file)
 //Create a class for thermostat device
 package Thermostat
-import Timer.SimulatedTimer
-
-public class Thermostat {
-       private String id
-       private String label
-       private String displayName
-       private int temperature
-       private int currentCoolingSetpoint
-       private int currentHeatingSetpoint
-       private int coolingSetpoint
-       private int thermostatSetpoint
-       private int heatingSetpoint
-       private List coolingSetpointRange
-       private List thermostatSetpointRange
-       private List heatingSetpointRange
-       private List supportedThermostatFanModes
-       private List supportedThermostatModes
-       private String thermostatOperatingState
-       private String thermostatFanMode
-       private String thermostatMode
-       private String currentThermostatMode
-       private String climateName
-       def sendEvent
-       def timers
-
-
-       Thermostat(Closure sendEvent, String id, String label, String displayName, int temperature, int currentCoolingSetpoint, int currentHeatingSetpoint, int coolingSetpoint, 
-                   int thermostatSetpoint, int heatingSetpoint, List coolingSetpointRange, List thermostatSetpointRange, List heatingSetpointRange, 
-                   List supportedThermostatFanModes, List supportedThermostatModes, String thermostatOperatingState, String thermostatFanMode, String thermostatMode,
-                  String climateName) {
+import SmartThing.SmartThing
+
+class Thermostat extends SmartThing {
+       // id, label, and display name of the device
+       StringBuilder id = new StringBuilder()
+       StringBuilder label = new StringBuilder()
+       StringBuilder displayName = new StringBuilder()
+       // Features with numberical values
+       MutableInteger currentTemperature = new MutableInteger()
+       MutableInteger currentCoolingSetpoint = new MutableInteger()
+       MutableInteger currentHeatingSetpoint = new MutableInteger()
+       MutableInteger currentThermostatSetPoint = new MutableInteger()
+       // Features with string values
+       StringBuilder currentThermostatOperatingState = new StringBuilder()
+       StringBuilder currentThermostatFanMode = new StringBuilder()
+       StringBuilder currentThermostatMode = new StringBuilder()
+       StringBuilder currentClimateName = new StringBuilder()
+       // Maps from features to values
+       HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
+       HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
+
+       Thermostat(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger currentTemperature, MutableInteger currentCoolingSetpoint, 
+                   MutableInteger currentHeatingSetpoint, MutableInteger currentThermostatSetPoint, StringBuilder currentThermostatOperatingState, StringBuilder currentThermostatFanMode, 
+                  StringBuilder currentThermostatMode, StringBuilder currentClimateName) {
+               deviceValuesMap = deviceValueSmartThing
+               deviceIntValuesMap = deviceIntValueSmartThing
+               idSmartThing = id
+               labelSmartThing = label
+               displayNameSmartThing = displayName
+               sendEventSmartThings = sendEvent
+
+               // Initialization
                this.id = id
                this.label = label
-               this.sendEvent = sendEvent
-               this.temperature = temperature
+               this.displayName = displayName
+               this.currentTemperature = currentTemperature
                this.currentCoolingSetpoint = currentCoolingSetpoint
                this.currentHeatingSetpoint = currentHeatingSetpoint
-               this.coolingSetpoint = coolingSetpoint
-               this.thermostatSetpoint = thermostatSetpoint
-               this.heatingSetpoint = heatingSetpoint
-               this.coolingSetpointRange = coolingSetpointRange
-               this.thermostatSetpointRange = thermostatSetpointRange
-               this.heatingSetpointRange = heatingSetpointRange
-               this.supportedThermostatFanModes = supportedThermostatFanModes
-               this.supportedThermostatModes = supportedThermostatModes
-               this.thermostatOperatingState = thermostatOperatingState
-               this.thermostatFanMode = thermostatFanMode
-               this.thermostatMode = thermostatMode
-               this.currentThermostatMode = thermostatMode
-               this.climateName = climateName
-       }
-
-
-       //By Apps
-       def setCoolingSetpoint(int coolingSetpoint) {
-               this.coolingSetpoint = coolingSetpoint
-               this.currentCoolingSetpoint = currentCoolingSetpoint
-               println("Cooling set point for the thermostat with id:$id is changed to $coolingSetpoint!")
+               this.currentThermostatSetPoint = currentThermostatSetPoint
+               this.currentThermostatOperatingState = currentThermostatOperatingState
+               this.currentThermostatFanMode = currentThermostatFanMode
+               this.currentThermostatMode = currentThermostatMode
+               this.currentClimateName = currentClimateName
+
+               deviceValuesMap.put("temperature", currentTemperature)
+               deviceValuesMap.put("thermostatOperatingState", currentThermostatOperatingState)
+               deviceValuesMap.put("thermostatFanMode", currentThermostatFanMode)
+               deviceValuesMap.put("thermostatMode", currentThermostatMode)
+               deviceIntValuesMap.put("coolingSetpoint", currentCoolingSetpoint)
+               deviceIntValuesMap.put("heatingSetpoint", currentHeatingSetpoint)
+               deviceIntValuesMap.put("thermostatSetpoint", currentThermostatSetPoint)
        }
 
-       def setHeatingSetpoint(int heatingSetpoint) {
-               this.heatingSetpoint = heatingSetpoint
-               this.currentHeatingSetpoint = currentHeatingSetpoint
-               println("Heating set point for the thermostat with id:$id is changed to $heatingSetpoint!")
+       // Methods to set values
+       def setCoolingSetpoint(int newValue) {
+               action(this.currentCoolingSetpoint, newValue, "coolingSetpoint")
        }
 
-       def setSchedule() {
-               //Not implemented yet
+       def setCoolingSetpoint(String newValue) {
+               setCoolingSetpoint(newValue.toInteger())
        }
 
-       def setThermostatFanMode(String thermostatFanMode) {
-               this.thermostatFanMode = thermostatFanMode
-               println("Fan mode of the thermostat with id:$id is changed to $thermostatFanMode!")
+       def setHeatingSetpoint(int newValue) {
+               action(this.currentHeatingSetpoint, newValue, "heatingSetpoint")
        }
 
-       def setThermostatMode(String thermostatMode) {
-               this.thermostatMode = thermostatMode
-               this.currentThermostatMode = currentThermostatMode
-               println("Mode of the thermostat with id:$id is changed to $thermostatMode!")
+       def setHeatingSetpoint(String newValue) {
+               setHeatingSetpoint(newValue.toInteger())
+       }
+
+       def setThermostatFanMode(String newValue) {
+               action(this.currentThermostatFanMode, newValue, "thermostatFanMode")
+       }
+
+       def setThermostatMode(String newValue) {
+               action(this.currentThermostatMode, newValue, "thermostatMode")
        }
 
        def cool() {
-               this.thermostatMode = "cool"
-               this.currentThermostatMode = "cool"
-               println("Mode of the thermostat with id:$id is changed to cool!")
+               action(this.currentThermostatMode, "cool", "thermostatMode")
        }
 
        def heat() {
-               this.thermostatMode = "heat"
-               this.currentThermostatMode = "heat"
-               println("Mode of the thermostat with id:$id is changed to heat!")
+               action(this.currentThermostatMode, "heat", "thermostatMode")
        }
 
        def auto() {
-               this.thermostatMode = "auto"
-               this.currentThermostatMode = "auto"
-               println("Mode of the thermostat with id:$id is changed to auto!")
+               action(this.currentThermostatMode, "auto", "thermostatMode")
+       }
+       
+       def emergencyHeat() {
+               action(this.currentThermostatMode, "emergencyHeat", "thermostatMode")
        }
 
        def off() {
-               this.thermostatMode = "off"
-               this.currentThermostatMode = "off"
-               println("Mode of the thermostat with id:$id is changed to off!")
+               action(this.currentThermostatMode, "off", "thermostatMode")
        }
 
-       def setClimate(String info, String givenClimateName) {
-               this.climateName = givenClimateName
-               println("Climate name of the thermostat with id:$id is changed to $givenClimateName!")
+       def setClimate(String info, String newValue) {
+               action(currentClimateName, newValue, "climateName")
        }
 
        def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
-               this.coolingSetpoint = coolingSetpoint
-               this.currentCoolingSetpoint = currentCoolingSetpoint
-               println("Cooling set point for the thermostat with id:$id is changed to $coolingSetpoint!")
-               this.heatingSetpoint = heatingSetpoint
-               this.currentHeatingSetpoint = currentHeatingSetpoint
-               println("Heating set point for the thermostat with id:$id is changed to $heatingSetpoint!")
+               setHeatingSetpoint(heatingSetpoint)
+               setCoolingSetpoint(coolingSetpoint)
        }
 
-       //By Model Checker
-       def setValue(String value) {
-               println("the thermostat with id:$id is $value!")
-               this.thermostatMode = value
-               this.currentThermostatMode = value
+       def action(StringBuilder variable, String newValue, String feature) {
+               if (!variable.toString().equals(newValue)) {
+                       String tmpID = id.toString()
+                       variable.replace(0, variable.length(), newValue)
+                       println("$feature of the thermostat with id:$tmpID is changed to $newValue!")
+                       sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
        }
 
+       def action(MutableInteger variable, int newValue, String feature) {
+               if (!variable.getValue().equals(newValue)) {
+                       String tmpID = id.toString()
+                       variable.setValue(newValue)
+                       println("$feature for the thermostat with id:$tmpID is changed to $newValue!")
+                       sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
+                                  displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
+               }
+       }
+
+       // Methods to return values
+       def getCurrentTemperature() {
+               return currentTemperature.getValue()
+       }
+
+       def getCurrentCoolingSetpoint() {
+               return currentCoolingSetpoint.getValue()
+       }
+       
+       def getCurrentHeatingSetpoint() {
+               return currentHeatingSetpoint.getValue()
+       }
+
+       def getCurrentThermostatSetPoint() {
+               return currentThermostatSetPoint.getValue()
+       }
+
+       def getCurrentThermostatOperatingState() {
+               return currentThermostatOperatingState.toString()
+       }
+
+       def getCurrentThermostatFanMode() {
+               return currentThermostatFanMode.toString()
+       }
+
+       def getCurrentThermostatMode() {
+               return currentThermostatMode.toString()
+       }
+
+       def getCurrentClimateName() {
+               return currentClimateName.toString()
+       }
 }