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