Changing Verify API call scheme for device handlers.
[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 = "auto"
29         private String thermostatMode = "auto"
30         private String currentThermostatMode = "auto"
31         private String climateName = ""
32         private String thermostatLatestMode = "auto"
33         private String thermostatLatestOperatingState = "cooling"
34         private String thermostatLatestFanMode = "auto"
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 = "auto"
55                         this.thermostatLatestFanMode = "auto"
56                         this.thermostatMode = "auto"
57                         this.currentThermostatMode = "auto"
58                         this.thermostatLatestMode = "auto"
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 = currentThermostatMode
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 off() {
177                 if (thermostatMode != "off") {
178                         this.thermostatLatestMode = "off"
179                         this.thermostatMode = "off"
180                         this.currentThermostatMode = "off"
181                         thermostats[0].off()
182                 }
183         }
184
185         def setClimate(String info, String givenClimateName) {
186                 if (givenClimateName != climateName) {
187                         this.climateName = givenClimateName
188                         thermostats[0].setClimate(info, givenClimateName)
189                 }
190         }
191
192         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
193                 if ((coolingSetpoint != this.coolingSetpoint) || (heatingSetpoint != this.heatingSetpoint)) {
194                         this.currentCoolingSetpoint = coolingSetpoint
195                         this.coolingSetpoint = coolingSetpoint
196                         this.currentHeatingSetpoint = heatingSetpoint
197                         this.heatingSetpoint = heatingSetpoint
198                         this.latestCoolingSetPoint = coolingSetpoint
199                         this.latestHeatingSetPoint = heatingSetpoint
200                         thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
201                 }
202         }
203
204         //By Model Checker
205         def setValue(LinkedHashMap eventDataMap) {
206                 if (eventDataMap["name"] == "temperature") {
207                         if (eventDataMap["value"].toInteger() != thermostats[0].temperature) {
208                                 this.temperature = eventDataMap["value"].toInteger()
209                                 this.currentTemperature = eventDataMap["value"].toInteger()
210                                 thermostats[0].setValue(eventDataMap["value"], "temperature")
211                                 sendEvent(eventDataMap)
212                         }
213                 } else if (eventDataMap["name"] == "heatingSetpoint") {
214                         if (eventDataMap["value"].toInteger() != thermostats[0].heatingSetpoint) {
215                                 this.latestHeatingSetPoint = eventDataMap["value"].toInteger()
216                                 this.heatingSetpoint = eventDataMap["value"].toInteger()
217                                 thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
218                                 sendEvent(eventDataMap)
219                         }
220                 } else if (eventDataMap["name"] == "coolingSetpoint") {
221                         if (eventDataMap["value"].toInteger() != thermostats[0].coolingSetpoint) {
222                                 this.latestCoolingSetPoint = eventDataMap["value"].toInteger()
223                                 this.coolingSetpoint = eventDataMap["value"].toInteger()
224                                 thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
225                                 sendEvent(eventDataMap)
226                         }
227                 } else if (eventDataMap["name"] == "thermostatSetpoint") {
228                         if (eventDataMap["value"].toInteger() != thermostats[0].thermostatSetpoint) {
229                                 this.latestThermostatSetPoint = eventDataMap["value"].toInteger()
230                                 this.thermostatSetpoint = eventDataMap["value"].toInteger()
231                                 thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
232                                 sendEvent(eventDataMap)
233                         }
234                 } else if (eventDataMap["name"] == "thermostatMode") {
235                         if (eventDataMap["value"] != thermostats[0].thermostatMode) {
236                                 this.thermostatLatestMode = eventDataMap["value"]
237                                 this.thermostatMode = eventDataMap["value"]
238                                 this.currentThermostatMode = eventDataMap["value"]
239                                 thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
240                                 sendEvent(eventDataMap)
241                         }
242                 } else if (eventDataMap["name"] == "thermostatFanMode") {
243                         if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
244                                 this.thermostatLatestFanMode = eventDataMap["value"]
245                                 this.thermostatFanMode = eventDataMap["value"]
246                                 thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
247                                 sendEvent(eventDataMap)
248                         }
249                 } else if (eventDataMap["name"] == "thermostatOperatingState") {
250                         if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
251                                 this.thermostatLatestOperatingState = eventDataMap["value"]
252                                 this.thermostatOperatingState = eventDataMap["value"]
253                                 thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
254                                 sendEvent(eventDataMap)
255                         }
256                 }
257         }
258
259         def currentValue(String deviceFeature) {
260                 thermostats[0].currentValue(deviceFeature)
261         }
262
263         def latestValue(String deviceFeature) {
264                 thermostats[0].latestValue(deviceFeature)
265         }
266
267         def getAt(int ix) {
268                 thermostats[ix]
269         }
270 }
271