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 currentCoolingSetpoint = 70
20         private int currentHeatingSetpoint = 50
21         private int coolingSetpoint = 70
22         private int thermostatSetpoint = 60
23         private int heatingSetpoint = 50
24         private coolingSetpointRange = [70, 90]
25         private thermostatSetpointRange = [50, 70]
26         private heatingSetpointRange = [20, 50]
27         private supportedThermostatFanModes = ["auto", "fanCirculate", "circulate", "fanOn", "on"]
28         private supportedThermostatModes = ["auto", "cool", "emergencyHeat", "heat", "off"]
29         private String thermostatOperatingState = "cooling"
30         private String thermostatFanMode = "auto"
31         private String thermostatMode = "auto"
32         private String currentThermostatMode = "auto"
33         private String climateName = ""
34
35         Thermostats(Closure sendEvent, int deviceNumbers) {
36                 this.sendEvent = sendEvent
37                 this.timers = new SimulatedTimer()
38                 this.deviceNumbers = deviceNumbers
39                 this.thermostats = []
40
41                 def initTemperature = Verify.getIntFromList(60, 66, 70)
42                 this.temperature = initTemperature
43                 
44                 def initCoolingSetpoint = Verify.getIntFromList(70, 80, 90)
45                 this.currentCoolingSetpoint = initCoolingSetpoint
46                 this.coolingSetpoint = initCoolingSetpoint
47                 
48                 def initHeatingSetpoint = Verify.getIntFromList(20, 35, 50)
49                 this.currentHeatingSetpoint = initHeatingSetpoint
50                 this.heatingSetpoint = initHeatingSetpoint
51                 
52                 def initThermostatSetpoint = Verify.getIntFromList(50, 60, 70)
53 <<<<<<< HEAD
54                 this.thermostatSetpoint = initThermostatSetpoint
55 =======
56                 this.currentHeatingSetpoint = initThermostatSetpoint
57 >>>>>>> e378d6a65b25030f8914dc97f04b81ddff351d9c
58                 
59                 def initFanMode = Verify.getInt(0,4)
60                 if (initFanMode == 0) {
61                         this.thermostatFanMode = "auto"
62                 } else if (initFanMode == 1) {
63                         this.thermostatFanMode = "fanCirculate"
64                 } else if (initFanMode == 2) {
65                         this.thermostatFanMode = "circulate"
66                 } else if (initFanMode == 3) {
67                         this.thermostatFanMode = "fanOn"
68                 } else {
69                         this.thermostatFanMode = "on"
70                 }
71
72                 def initMode = Verify.getInt(0,4)
73                 if (initMode == 0) {
74                         this.thermostatMode = "auto"
75                         this.currentThermostatMode = "auto"
76                 } else if (initMode == 1) {
77                         this.thermostatMode = "cool"
78                         this.currentThermostatMode = "cool"
79                 } else if (initMode == 2) {
80                         this.thermostatMode = "emergencyHeat"
81                         this.currentThermostatMode = "emergencyHeat"
82                 } else if (initMode == 3) {
83                         this.thermostatMode = "heat"
84                         this.currentThermostatMode = "heat"
85                 } else {
86                         this.thermostatMode = "off"
87                         this.currentThermostatMode = "off"
88                 }
89
90                 thermostats.add(new Thermostat(sendEvent, id, label, displayName, this.temperature, this.currentCoolingSetpoint, 
91                                                this.currentHeatingSetpoint, this.coolingSetpoint, this.thermostatSetpoint, this.heatingSetpoint, this.coolingSetpointRange,
92                                                this.thermostatSetpointRange, this.heatingSetpointRange, this.supportedThermostatFanModes, this.supportedThermostatModes,
93                                                this.thermostatOperatingState, this.thermostatFanMode,  this.thermostatMode, this.climateName))
94         }
95
96         //Methods for closures
97         def count(Closure Input) {
98                 thermostats.count(Input)
99         }
100         def size() {
101                 thermostats.size()
102         }
103         def each(Closure Input) {
104                 thermostats.each(Input)
105         }
106         def find(Closure Input) {
107                 thermostats.find(Input)
108         }
109         def collect(Closure Input) {
110                 thermostats.collect(Input)
111         }
112
113         //By Apps
114         def setCoolingSetpoint(int coolingSetpoint) {
115                 if (coolingSetpoint != this.coolingSetpoint) {
116                         thermostats[0].setCoolingSetpoint(coolingSetpoint)
117                         this.currentCoolingSetpoint = coolingSetpoint
118                         this.coolingSetpoint = coolingSetpoint
119                 }
120         }
121
122         def setHeatingSetpoint(int heatingSetpoint) {
123                 if (heatingSetpoint != this.heatingSetpoint) {
124                         thermostats[0].setHeatingSetpoint(heatingSetpoint)
125                         this.currentHeatingSetpoint = heatingSetpoint
126                         this.heatingSetpoint = heatingSetpoint
127                 }
128         }
129
130         def setSchedule() {
131                 //Not implemented yet
132         }
133
134         def setThermostatFanMode(String thermostatFanMode) {
135                 if (thermostatFanMode != this.thermostatFanMode) {
136                         thermostats[0].setThermostatFanMode(thermostatFanMode)
137                         this.thermostatFanMode = thermostatFanMode
138                 }
139         }
140
141         def setThermostatMode(String thermostatMode) {
142                 if (thermostatMode != this.thermostatMode) {
143                         thermostats[0].setThermostatMode(thermostatMode)
144                         this.thermostatMode = thermostatMode
145                         this.currentThermostatMode = currentThermostatMode
146                 }
147         }
148
149         def cool() {
150                 if (thermostatMode != "cool") {
151                         thermostats[0].cool()
152                         this.thermostatMode = "cool"
153                         this.currentThermostatMode = "cool"
154                 }
155         }
156
157         def heat() {
158                 if (thermostatMode != "heat") {
159                         thermostats[0].heat()
160                         this.thermostatMode = "heat"
161                         this.currentThermostatMode = "heat"
162                 }
163         }
164
165         def auto() {
166                 if (thermostatMode != "auto") {
167                         thermostats[0].auto()
168                         this.thermostatMode = "auto"
169                         this.currentThermostatMode = "auto"
170                 }
171         }
172
173         def off() {
174                 if (thermostatMode != "off") {
175                         thermostats[0].off()
176                         this.thermostatMode = "off"
177                         this.currentThermostatMode = "off"
178                 }
179         }
180
181         def setClimate(String info, String givenClimateName) {
182                 if (givenClimateName != climateName) {
183                         thermostats[0].setClimate(info, givenClimateName)
184                         this.climateName = givenClimateName
185                 }
186         }
187
188         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
189                 if ((coolingSetpoint != this.coolingSetpoint) || (heatingSetpoint != this.heatingSetpoint)) {
190                         thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
191                         this.currentCoolingSetpoint = coolingSetpoint
192                         this.coolingSetpoint = coolingSetpoint
193                         this.currentHeatingSetpoint = heatingSetpoint
194                         this.heatingSetpoint = heatingSetpoint
195                 }
196         }
197
198         //By Model Checker
199         def setValue(LinkedHashMap eventDataMap) {
200                 if (eventDataMap["name"] == "temperature") {
201                         if (eventDataMap["value"] != thermostats[0].temperature) {
202                                 thermostats[0].setValue(eventDataMap["value"], "temperature")
203                                 this.temperature = thermostats[0].temperature
204                                 sendEvent(eventDataMap)
205                         }
206                 } else if (eventDataMap["name"] == "heatingSetpoint") {
207                         if (eventDataMap["value"] != thermostats[0].heatingSetpoint) {
208                                 thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
209                                 this.heatingSetpoint = thermostats[0].heatingSetpoint
210                                 sendEvent(eventDataMap)
211                         }
212                 } else if (eventDataMap["name"] == "coolingSetpoint") {
213                         if (eventDataMap["value"] != thermostats[0].coolingSetpoint) {
214                                 thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
215                                 this.coolingSetpoint = thermostats[0].coolingSetpoint
216                                 sendEvent(eventDataMap)
217                         }
218                 } else if (eventDataMap["name"] == "thermostatSetpoint") {
219                         if (eventDataMap["value"] != thermostats[0].thermostatSetpoint) {
220                                 thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
221                                 this.thermostatSetpoint = thermostats[0].thermostatSetpoint
222                                 sendEvent(eventDataMap)
223                         }
224                 } else if (eventDataMap["name"] == "thermostatMode") {
225                         if (eventDataMap["value"] != thermostats[0].thermostatMode) {
226                                 thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
227                                 this.thermostatMode = thermostats[0].thermostatMode
228                                 this.currentThermostatMode = thermostats[0].currentThermostatMode
229                                 sendEvent(eventDataMap)
230                         }
231                 } else if (eventDataMap["name"] == "thermostatFanMode") {
232                         if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
233                                 thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
234                                 this.thermostatFanMode = thermostats[0].thermostatFanMode
235                                 sendEvent(eventDataMap)
236                         }
237                 } else if (eventDataMap["name"] == "thermostatOperatingState") {
238                         if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
239                                 thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
240                                 this.thermostatOperatingState = thermostats[0].thermostatOperatingState
241                                 sendEvent(eventDataMap)
242                         }
243                 }
244         }
245
246         def getAt(int ix) {
247                 thermostats[ix]
248         }
249 }
250