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