Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/smartthings-infrastructure
[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)
50                 this.temperature = initTemperature
51                 this.currentTemperature = initTemperature
52                 
53                 def initCoolingSetpoint = Verify.getIntFromList(70, 80)
54                 this.currentCoolingSetpoint = initCoolingSetpoint
55                 this.coolingSetpoint = initCoolingSetpoint
56                 
57                 def initHeatingSetpoint = Verify.getIntFromList(35, 50)
58                 this.currentHeatingSetpoint = initHeatingSetpoint
59                 this.heatingSetpoint = initHeatingSetpoint
60                 
61                 def initThermostatSetpoint = Verify.getIntFromList(50, 60)
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                         this.thermostatLatestMode = "off"
103                 }
104
105                 thermostats.add(new Thermostat(sendEvent, id, label, displayName, this.temperature, this.currentCoolingSetpoint, 
106                                                this.currentHeatingSetpoint, this.coolingSetpoint, this.thermostatSetpoint, this.heatingSetpoint, this.coolingSetpointRange,
107                                                this.thermostatSetpointRange, this.heatingSetpointRange, this.supportedThermostatFanModes, this.supportedThermostatModes,
108                                                this.thermostatOperatingState, this.thermostatFanMode,  this.thermostatMode, this.climateName, 
109                                                this.thermostatLatestMode, this.thermostatLatestOperatingState, this.thermostatLatestFanMode, this.latestCoolingSetPoint,
110                                                this.latestThermostatSetPoint, this.latestHeatingSetPoint))
111         }
112
113         //Methods for closures
114         def count(Closure Input) {
115                 thermostats.count(Input)
116         }
117         def size() {
118                 thermostats.size()
119         }
120         def each(Closure Input) {
121                 thermostats.each(Input)
122         }
123         def find(Closure Input) {
124                 thermostats.find(Input)
125         }
126         def sort(Closure Input) {
127                 thermostats.sort(Input)
128         }
129         def collect(Closure Input) {
130                 thermostats.collect(Input)
131         }
132
133         //By Apps
134         def setCoolingSetpoint(int coolingSetpoint) {
135                 if (coolingSetpoint != this.coolingSetpoint) {
136                         this.latestCoolingSetPoint = coolingSetpoint
137                         this.currentCoolingSetpoint = coolingSetpoint
138                         this.coolingSetpoint = coolingSetpoint
139                         thermostats[0].setCoolingSetpoint(coolingSetpoint)
140                 }
141         }
142
143         def setCoolingSetpoint(String coolingSetpoint) {
144                 setCoolingSetpoint(coolingSetpoint.toInteger())
145         }
146
147         def setHeatingSetpoint(int heatingSetpoint) {
148                 if (heatingSetpoint != this.heatingSetpoint) {
149                         this.latestHeatingSetPoint = heatingSetpoint
150                         this.currentHeatingSetpoint = heatingSetpoint
151                         this.heatingSetpoint = heatingSetpoint
152                         thermostats[0].setHeatingSetpoint(heatingSetpoint)
153                 }
154         }
155
156         def setHeatingSetpoint(String heatingSetpoint) {
157                 setHeatingSetpoint(heatingSetpoint.toInteger())
158         }
159
160         def setSchedule() {
161                 //Not implemented yet
162         }
163
164         def setThermostatFanMode(String thermostatFanMode) {
165                 if (thermostatFanMode != this.thermostatFanMode) {
166                         this.thermostatLatestFanMode = thermostatFanMode
167                         this.thermostatFanMode = thermostatFanMode
168                         thermostats[0].setThermostatFanMode(thermostatFanMode)
169                 }
170         }
171
172         def setThermostatMode(String thermostatMode) {
173                 if (thermostatMode != this.thermostatMode) {
174                         this.thermostatLatestMode = thermostatMode
175                         this.thermostatMode = thermostatMode
176                         this.currentThermostatMode = currentThermostatMode
177                         thermostats[0].setThermostatMode(thermostatMode)
178                 }
179         }
180
181         def cool() {
182                 if (thermostatMode != "cool") {
183                         this.thermostatLatestMode = "cool"
184                         this.thermostatMode = "cool"
185                         this.currentThermostatMode = "cool"
186                         thermostats[0].cool()
187                 }
188         }
189
190         def heat() {
191                 if (thermostatMode != "heat") {
192                         this.thermostatLatestMode = "heat"
193                         this.thermostatMode = "heat"
194                         this.currentThermostatMode = "heat"
195                         thermostats[0].heat()
196                 }
197         }
198
199         def auto() {
200                 if (thermostatMode != "auto") {
201                         this.thermostatLatestMode = "auto"
202                         this.thermostatMode = "auto"
203                         this.currentThermostatMode = "auto"
204                         thermostats[0].auto()
205                 }
206         }
207
208         def off() {
209                 if (thermostatMode != "off") {
210                         this.thermostatLatestMode = "off"
211                         this.thermostatMode = "off"
212                         this.currentThermostatMode = "off"
213                         thermostats[0].off()
214                 }
215         }
216
217         def setClimate(String info, String givenClimateName) {
218                 if (givenClimateName != climateName) {
219                         this.climateName = givenClimateName
220                         thermostats[0].setClimate(info, givenClimateName)
221                 }
222         }
223
224         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
225                 if ((coolingSetpoint != this.coolingSetpoint) || (heatingSetpoint != this.heatingSetpoint)) {
226                         this.currentCoolingSetpoint = coolingSetpoint
227                         this.coolingSetpoint = coolingSetpoint
228                         this.currentHeatingSetpoint = heatingSetpoint
229                         this.heatingSetpoint = heatingSetpoint
230                         this.latestCoolingSetPoint = coolingSetpoint
231                         this.latestHeatingSetPoint = heatingSetpoint
232                         thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
233                 }
234         }
235
236         //By Model Checker
237         def setValue(LinkedHashMap eventDataMap) {
238                 if (eventDataMap["name"] == "temperature") {
239                         if (eventDataMap["value"].toInteger() != thermostats[0].temperature) {
240                                 this.temperature = eventDataMap["value"].toInteger()
241                                 this.currentTemperature = eventDataMap["value"].toInteger()
242                                 thermostats[0].setValue(eventDataMap["value"], "temperature")
243                                 sendEvent(eventDataMap)
244                         }
245                 } else if (eventDataMap["name"] == "heatingSetpoint") {
246                         if (eventDataMap["value"].toInteger() != thermostats[0].heatingSetpoint) {
247                                 this.latestHeatingSetPoint = eventDataMap["value"].toInteger()
248                                 this.heatingSetpoint = eventDataMap["value"].toInteger()
249                                 thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
250                                 sendEvent(eventDataMap)
251                         }
252                 } else if (eventDataMap["name"] == "coolingSetpoint") {
253                         if (eventDataMap["value"].toInteger() != thermostats[0].coolingSetpoint) {
254                                 this.latestCoolingSetPoint = eventDataMap["value"].toInteger()
255                                 this.coolingSetpoint = eventDataMap["value"].toInteger()
256                                 thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
257                                 sendEvent(eventDataMap)
258                         }
259                 } else if (eventDataMap["name"] == "thermostatSetpoint") {
260                         if (eventDataMap["value"].toInteger() != thermostats[0].thermostatSetpoint) {
261                                 this.latestThermostatSetPoint = eventDataMap["value"].toInteger()
262                                 this.thermostatSetpoint = eventDataMap["value"].toInteger()
263                                 thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
264                                 sendEvent(eventDataMap)
265                         }
266                 } else if (eventDataMap["name"] == "thermostatMode") {
267                         if (eventDataMap["value"] != thermostats[0].thermostatMode) {
268                                 this.thermostatLatestMode = eventDataMap["value"]
269                                 this.thermostatMode = eventDataMap["value"]
270                                 this.currentThermostatMode = eventDataMap["value"]
271                                 thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
272                                 sendEvent(eventDataMap)
273                         }
274                 } else if (eventDataMap["name"] == "thermostatFanMode") {
275                         if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
276                                 this.thermostatLatestFanMode = eventDataMap["value"]
277                                 this.thermostatFanMode = eventDataMap["value"]
278                                 thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
279                                 sendEvent(eventDataMap)
280                         }
281                 } else if (eventDataMap["name"] == "thermostatOperatingState") {
282                         if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
283                                 this.thermostatLatestOperatingState = eventDataMap["value"]
284                                 this.thermostatOperatingState = eventDataMap["value"]
285                                 thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
286                                 sendEvent(eventDataMap)
287                         }
288                 }
289         }
290
291         def currentValue(String deviceFeature) {
292                 thermostats[0].currentValue(deviceFeature)
293         }
294
295         def latestValue(String deviceFeature) {
296                 thermostats[0].latestValue(deviceFeature)
297         }
298
299         def getAt(int ix) {
300                 thermostats[ix]
301         }
302 }
303