620aca10a296f54d5e91c1c1d75654fa763c4db7
[smartthings-infrastructure.git] / Thermostat / Thermostats.groovy
1 //Create a class for thermostat device
2 package Thermostat
3 import Timer.SimulatedTimer
4
5 public class Thermostats{
6         int deviceNumbers       
7         List thermostats        
8         def sendEvent   
9         def timers
10
11         //When we have only one device
12         private String id = "thermostatID0"
13         private String label = "thermostat0"
14         private String displayName = "thermostat0"
15         private int temperature = 66
16         private int currentTemperature = 66
17         private int currentCoolingSetpoint = 70
18         private int currentHeatingSetpoint = 50
19         private int coolingSetpoint = 70
20         private int thermostatSetpoint = 60
21         private int heatingSetpoint = 50
22         private coolingSetpointRange = [70, 90]
23         private thermostatSetpointRange = [50, 70]
24         private heatingSetpointRange = [20, 50]
25         private supportedThermostatFanModes = ["auto", "fanCirculate", "circulate", "fanOn", "on"]
26         private supportedThermostatModes = ["auto", "cool", "emergencyHeat", "heat", "off"]
27         private String thermostatOperatingState = "cooling"
28         private String thermostatFanMode = "off"
29         private String thermostatMode = "off"
30         private String currentThermostatMode = "off"
31         private String climateName = ""
32         private String thermostatLatestMode = "off"
33         private String thermostatLatestOperatingState = "cooling"
34         private String thermostatLatestFanMode = "off"
35         private int latestCoolingSetPoint = 70
36         private int latestThermostatSetPoint = 60
37         private int latestHeatingSetPoint = 50
38
39
40         Thermostats(Closure sendEvent, int deviceNumbers, boolean init) {
41                 this.sendEvent = sendEvent
42                 this.timers = new SimulatedTimer()
43                 this.deviceNumbers = deviceNumbers
44                 this.thermostats = []
45
46                 if (init) {
47                         this.temperature = 60
48                         this.currentTemperature = 60
49                         this.currentCoolingSetpoint = 70
50                         this.coolingSetpoint = 70
51                         this.currentHeatingSetpoint = 35
52                         this.heatingSetpoint = 35
53                         this.thermostatSetpoint = 50
54                         this.thermostatFanMode = "off"
55                         this.thermostatLatestFanMode = "off"
56                         this.thermostatMode = "off"
57                         this.currentThermostatMode = "off"
58                         this.thermostatLatestMode = "off"
59                 } else {
60                         this.temperature = 66
61                         this.currentTemperature = 66
62                         this.currentCoolingSetpoint = 80
63                         this.coolingSetpoint = 80
64                         this.currentHeatingSetpoint = 50
65                         this.heatingSetpoint = 50
66                         this.thermostatSetpoint = 60
67                         this.thermostatFanMode = "circulate"
68                         this.thermostatLatestFanMode = "circulate"
69                         this.thermostatMode = "off"
70                         this.currentThermostatMode = "off"
71                         this.thermostatLatestMode = "off"               
72                 }
73                 thermostats.add(new Thermostat(sendEvent, id, label, displayName, this.temperature, this.currentCoolingSetpoint, 
74                                                this.currentHeatingSetpoint, this.coolingSetpoint, this.thermostatSetpoint, this.heatingSetpoint, this.coolingSetpointRange,
75                                                this.thermostatSetpointRange, this.heatingSetpointRange, this.supportedThermostatFanModes, this.supportedThermostatModes,
76                                                this.thermostatOperatingState, this.thermostatFanMode,  this.thermostatMode, this.climateName, 
77                                                this.thermostatLatestMode, this.thermostatLatestOperatingState, this.thermostatLatestFanMode, this.latestCoolingSetPoint,
78                                                this.latestThermostatSetPoint, this.latestHeatingSetPoint))
79         }
80
81         //Methods for closures
82         def count(Closure Input) {
83                 thermostats.count(Input)
84         }
85         def size() {
86                 thermostats.size()
87         }
88         def each(Closure Input) {
89                 thermostats.each(Input)
90         }
91         def find(Closure Input) {
92                 thermostats.find(Input)
93         }
94         def sort(Closure Input) {
95                 thermostats.sort(Input)
96         }
97         def collect(Closure Input) {
98                 thermostats.collect(Input)
99         }
100
101         //By Apps
102         def setCoolingSetpoint(int coolingSetpoint) {
103                 if (coolingSetpoint != this.coolingSetpoint) {
104                         this.latestCoolingSetPoint = coolingSetpoint
105                         this.currentCoolingSetpoint = coolingSetpoint
106                         this.coolingSetpoint = coolingSetpoint
107                         thermostats[0].setCoolingSetpoint(coolingSetpoint)
108                 }
109         }
110
111         def setCoolingSetpoint(String coolingSetpoint) {
112                 setCoolingSetpoint(coolingSetpoint.toInteger())
113         }
114
115         def setHeatingSetpoint(int heatingSetpoint) {
116                 if (heatingSetpoint != this.heatingSetpoint) {
117                         this.latestHeatingSetPoint = heatingSetpoint
118                         this.currentHeatingSetpoint = heatingSetpoint
119                         this.heatingSetpoint = heatingSetpoint
120                         thermostats[0].setHeatingSetpoint(heatingSetpoint)
121                 }
122         }
123
124         def setHeatingSetpoint(String heatingSetpoint) {
125                 setHeatingSetpoint(heatingSetpoint.toInteger())
126         }
127
128         def setSchedule() {
129                 //Not implemented yet
130         }
131
132         def setThermostatFanMode(String thermostatFanMode) {
133                 if (thermostatFanMode != this.thermostatFanMode) {
134                         this.thermostatLatestFanMode = thermostatFanMode
135                         this.thermostatFanMode = thermostatFanMode
136                         thermostats[0].setThermostatFanMode(thermostatFanMode)
137                 }
138         }
139
140         def setThermostatMode(String thermostatMode) {
141                 if (thermostatMode != this.thermostatMode) {
142                         this.thermostatLatestMode = thermostatMode
143                         this.thermostatMode = thermostatMode
144                         this.currentThermostatMode = thermostatMode
145                         thermostats[0].setThermostatMode(thermostatMode)
146                 }
147         }
148
149         def cool() {
150                 if (thermostatMode != "cool") {
151                         this.thermostatLatestMode = "cool"
152                         this.thermostatMode = "cool"
153                         this.currentThermostatMode = "cool"
154                         thermostats[0].cool()
155                 }
156         }
157
158         def heat() {
159                 if (thermostatMode != "heat") {
160                         this.thermostatLatestMode = "heat"
161                         this.thermostatMode = "heat"
162                         this.currentThermostatMode = "heat"
163                         thermostats[0].heat()
164                 }
165         }
166         
167         def auto() {
168                 if (thermostatMode != "auto") {
169                         this.thermostatLatestMode = "auto"
170                         this.thermostatMode = "auto"
171                         this.currentThermostatMode = "auto"
172                         thermostats[0].auto()
173                 }
174         }
175
176         def emergencyHeat() {
177                 if (thermostatMode != "emergencyHeat") {
178                         this.thermostatLatestMode = "emergencyHeat"
179                         this.thermostatMode = "emergencyHeat"
180                         this.currentThermostatMode = "emergencyHeat"
181                         thermostats[0].emergencyHeat()
182                 }
183         }
184
185         def off() {
186                 if (thermostatMode != "off") {
187                         this.thermostatLatestMode = "off"
188                         this.thermostatMode = "off"
189                         this.currentThermostatMode = "off"
190                         thermostats[0].off()
191                 }
192         }
193
194         def setClimate(String info, String givenClimateName) {
195                 if (givenClimateName != climateName) {
196                         this.climateName = givenClimateName
197                         thermostats[0].setClimate(info, givenClimateName)
198                 }
199         }
200
201         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
202                 if ((coolingSetpoint != this.coolingSetpoint) || (heatingSetpoint != this.heatingSetpoint)) {
203                         this.currentCoolingSetpoint = coolingSetpoint
204                         this.coolingSetpoint = coolingSetpoint
205                         this.currentHeatingSetpoint = heatingSetpoint
206                         this.heatingSetpoint = heatingSetpoint
207                         this.latestCoolingSetPoint = coolingSetpoint
208                         this.latestHeatingSetPoint = heatingSetpoint
209                         thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
210                 }
211         }
212
213         //By Model Checker
214         def setValue(LinkedHashMap eventDataMap) {
215                 if (eventDataMap["name"] == "temperature") {
216                         if (eventDataMap["value"].toInteger() != thermostats[0].temperature) {
217                                 this.temperature = eventDataMap["value"].toInteger()
218                                 this.currentTemperature = eventDataMap["value"].toInteger()
219                                 thermostats[0].setValue(eventDataMap["value"], "temperature")
220                                 sendEvent(eventDataMap)
221                         }
222                 } else if (eventDataMap["name"] == "heatingSetpoint") {
223                         if (eventDataMap["value"].toInteger() != thermostats[0].heatingSetpoint) {
224                                 this.latestHeatingSetPoint = eventDataMap["value"].toInteger()
225                                 this.heatingSetpoint = eventDataMap["value"].toInteger()
226                                 thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
227                                 sendEvent(eventDataMap)
228                         }
229                 } else if (eventDataMap["name"] == "coolingSetpoint") {
230                         if (eventDataMap["value"].toInteger() != thermostats[0].coolingSetpoint) {
231                                 this.latestCoolingSetPoint = eventDataMap["value"].toInteger()
232                                 this.coolingSetpoint = eventDataMap["value"].toInteger()
233                                 thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
234                                 sendEvent(eventDataMap)
235                         }
236                 } else if (eventDataMap["name"] == "thermostatSetpoint") {
237                         if (eventDataMap["value"].toInteger() != thermostats[0].thermostatSetpoint) {
238                                 this.latestThermostatSetPoint = eventDataMap["value"].toInteger()
239                                 this.thermostatSetpoint = eventDataMap["value"].toInteger()
240                                 thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
241                                 sendEvent(eventDataMap)
242                         }
243                 } else if (eventDataMap["name"] == "thermostatMode") {
244                         if (eventDataMap["value"] != thermostats[0].thermostatMode) {
245                                 this.thermostatLatestMode = eventDataMap["value"]
246                                 this.thermostatMode = eventDataMap["value"]
247                                 this.currentThermostatMode = eventDataMap["value"]
248                                 thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
249                                 sendEvent(eventDataMap)
250                         }
251                 } else if (eventDataMap["name"] == "thermostatFanMode") {
252                         if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
253                                 this.thermostatLatestFanMode = eventDataMap["value"]
254                                 this.thermostatFanMode = eventDataMap["value"]
255                                 thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
256                                 sendEvent(eventDataMap)
257                         }
258                 } else if (eventDataMap["name"] == "thermostatOperatingState") {
259                         if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
260                                 this.thermostatLatestOperatingState = eventDataMap["value"]
261                                 this.thermostatOperatingState = eventDataMap["value"]
262                                 thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
263                                 sendEvent(eventDataMap)
264                         }
265                 }
266         }
267
268         def currentValue(String deviceFeature) {
269                 thermostats[0].currentValue(deviceFeature)
270         }
271
272         def latestValue(String deviceFeature) {
273                 thermostats[0].latestValue(deviceFeature)
274         }
275
276         def getAt(int ix) {
277                 thermostats[ix]
278         }
279 }
280