4fa2c789ada95e7a6a85c0cc68769ced2e599aed
[smartthings-infrastructure.git] / Thermostat / Thermostat.groovy
1 //Create a class for thermostat device
2 package Thermostat
3 import SmartThing.SmartThing
4
5 //Importing mutable integer class
6 import MutableInteger.MutableInteger
7
8 class Thermostat extends SmartThing {
9         // id, label, and display name of the device
10         StringBuilder id = new StringBuilder()
11         StringBuilder label = new StringBuilder()
12         StringBuilder displayName = new StringBuilder()
13         // Features with numberical values
14         MutableInteger currentTemperature = new MutableInteger()
15         MutableInteger currentCoolingSetpoint = new MutableInteger()
16         MutableInteger currentHeatingSetpoint = new MutableInteger()
17         MutableInteger currentThermostatSetPoint = new MutableInteger()
18         // Features with string values
19         StringBuilder currentThermostatOperatingState = new StringBuilder()
20         StringBuilder currentThermostatFanMode = new StringBuilder()
21         StringBuilder currentThermostatMode = new StringBuilder()
22         StringBuilder currentClimateName = new StringBuilder()
23         // Maps from features to values
24         HashMap<String, StringBuilder> deviceValuesMap = new HashMap<String, StringBuilder>()
25         HashMap<String, MutableInteger> deviceIntValuesMap = new HashMap<String, MutableInteger>()
26
27         Thermostat(Closure sendEvent, StringBuilder id, StringBuilder label, StringBuilder displayName, MutableInteger currentTemperature, MutableInteger currentCoolingSetpoint, 
28                    MutableInteger currentHeatingSetpoint, MutableInteger currentThermostatSetPoint, StringBuilder currentThermostatOperatingState, StringBuilder currentThermostatFanMode, 
29                    StringBuilder currentThermostatMode, StringBuilder currentClimateName) {
30                 deviceValuesMap = deviceValueSmartThing
31                 deviceIntValuesMap = deviceIntValueSmartThing
32                 idSmartThing = id
33                 labelSmartThing = label
34                 displayNameSmartThing = displayName
35                 sendEventSmartThings = sendEvent
36
37                 // Initialization
38                 this.id = id
39                 this.label = label
40                 this.displayName = displayName
41                 this.currentTemperature = currentTemperature
42                 this.currentCoolingSetpoint = currentCoolingSetpoint
43                 this.currentHeatingSetpoint = currentHeatingSetpoint
44                 this.currentThermostatSetPoint = currentThermostatSetPoint
45                 this.currentThermostatOperatingState = currentThermostatOperatingState
46                 this.currentThermostatFanMode = currentThermostatFanMode
47                 this.currentThermostatMode = currentThermostatMode
48                 this.currentClimateName = currentClimateName
49
50                 deviceValuesMap.put("temperature", currentTemperature)
51                 deviceValuesMap.put("thermostatOperatingState", currentThermostatOperatingState)
52                 deviceValuesMap.put("thermostatFanMode", currentThermostatFanMode)
53                 deviceValuesMap.put("thermostatMode", currentThermostatMode)
54                 deviceIntValuesMap.put("coolingSetpoint", currentCoolingSetpoint)
55                 deviceIntValuesMap.put("heatingSetpoint", currentHeatingSetpoint)
56                 deviceIntValuesMap.put("thermostatSetpoint", currentThermostatSetPoint)
57         }
58
59         // Methods to set values
60         def setCoolingSetpoint(int newValue) {
61                 action(this.currentCoolingSetpoint, newValue, "coolingSetpoint")
62         }
63
64         def setCoolingSetpoint(String newValue) {
65                 setCoolingSetpoint(newValue.toInteger())
66         }
67
68         def setHeatingSetpoint(int newValue) {
69                 action(this.currentHeatingSetpoint, newValue, "heatingSetpoint")
70         }
71
72         def setHeatingSetpoint(String newValue) {
73                 setHeatingSetpoint(newValue.toInteger())
74         }
75
76         def setThermostatFanMode(String newValue) {
77                 action(this.currentThermostatFanMode, newValue, "thermostatFanMode")
78         }
79
80         def setThermostatMode(String newValue) {
81                 action(this.currentThermostatMode, newValue, "thermostatMode")
82         }
83
84         def cool() {
85                 action(this.currentThermostatMode, "cool", "thermostatMode")
86         }
87
88         def heat() {
89                 action(this.currentThermostatMode, "heat", "thermostatMode")
90         }
91
92         def auto() {
93                 action(this.currentThermostatMode, "auto", "thermostatMode")
94         }
95         
96         def emergencyHeat() {
97                 action(this.currentThermostatMode, "emergencyHeat", "thermostatMode")
98         }
99
100         def off() {
101                 action(this.currentThermostatMode, "off", "thermostatMode")
102         }
103
104         def setClimate(String info, String newValue) {
105                 action(currentClimateName, newValue, "climateName")
106         }
107
108         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
109                 setHeatingSetpoint(heatingSetpoint)
110                 setCoolingSetpoint(coolingSetpoint)
111         }
112
113         def action(StringBuilder variable, String newValue, String feature) {
114                 if (!variable.toString().equals(newValue)) {
115                         String tmpID = id.toString()
116                         variable.replace(0, variable.length(), newValue)
117                         println("$feature of the thermostat with id:$tmpID is changed to $newValue!")
118                         sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
119                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
120                 }
121         }
122
123         def action(MutableInteger variable, int newValue, String feature) {
124                 if (!variable.getValue().equals(newValue)) {
125                         String tmpID = id.toString()
126                         variable.setValue(newValue)
127                         println("$feature for the thermostat with id:$tmpID is changed to $newValue!")
128                         sendEvent([name: feature, value: newValue, deviceId: tmpID, descriptionText: "",
129                                    displayed: true, linkText: "", isStateChange: false, unit: "", data: '{"info": "info"}'])
130                 }
131         }
132
133         // Methods to return values
134         def getCurrentTemperature() {
135                 return currentTemperature.getValue()
136         }
137
138         def getCurrentCoolingSetpoint() {
139                 return currentCoolingSetpoint.getValue()
140         }
141         
142         def getCurrentHeatingSetpoint() {
143                 return currentHeatingSetpoint.getValue()
144         }
145
146         def getCurrentThermostatSetPoint() {
147                 return currentThermostatSetPoint.getValue()
148         }
149
150         def getCurrentThermostatOperatingState() {
151                 return currentThermostatOperatingState.toString()
152         }
153
154         def getCurrentThermostatFanMode() {
155                 return currentThermostatFanMode.toString()
156         }
157
158         def getCurrentThermostatMode() {
159                 return currentThermostatMode.toString()
160         }
161
162         def getCurrentClimateName() {
163                 return currentClimateName.toString()
164         }
165 }