Adding path explorations for initializations.
[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.currentHeatingSetpoint = 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                 thermostats[0].setCoolingSetpoint(coolingSetpoint)
112                 this.currentCoolingSetpoint = coolingSetpoint
113                 this.coolingSetpoint = coolingSetpoint
114         }
115
116         def setHeatingSetpoint(int heatingSetpoint) {
117                 thermostats[0].setHeatingSetpoint(heatingSetpoint)
118                 this.currentHeatingSetpoint = heatingSetpoint
119                 this.heatingSetpoint = heatingSetpoint
120         }
121
122         def setSchedule() {
123                 //Not implemented yet
124         }
125
126         def setThermostatFanMode(String thermostatFanMode) {
127                 thermostats[0].setThermostatFanMode(thermostatFanMode)
128                 this.thermostatFanMode = thermostatFanMode
129         }
130
131         def setThermostatMode(String thermostatMode) {
132                 thermostats[0].setThermostatMode(thermostatMode)
133                 this.thermostatMode = thermostatMode
134                 this.currentThermostatMode = currentThermostatMode
135         }
136
137         def cool() {
138                 thermostats[0].cool()
139                 this.thermostatMode = "cool"
140                 this.currentThermostatMode = "cool"
141         }
142
143         def heat() {
144                 thermostats[0].heat()
145                 this.thermostatMode = "heat"
146                 this.currentThermostatMode = "heat"
147         }
148
149         def auto() {
150                 thermostats[0].auto()
151                 this.thermostatMode = "auto"
152                 this.currentThermostatMode = "auto"
153         }
154
155         def off() {
156                 thermostats[0].off()
157                 this.thermostatMode = "off"
158                 this.currentThermostatMode = "off"
159         }
160
161         def setClimate(String info, String givenClimateName) {
162                 thermostats[0].setClimate(info, givenClimateName)
163                 this.climateName = givenClimateName
164         }
165
166         def setHold(String info1, int coolingSetpoint, int heatingSetpoint, String info2, String info3) {
167                 thermostats[0].setHold(info1, coolingSetpoint, heatingSetpoint, info2, info3)
168                 this.currentCoolingSetpoint = coolingSetpoint
169                 this.coolingSetpoint = coolingSetpoint
170                 this.currentHeatingSetpoint = heatingSetpoint
171                 this.heatingSetpoint = heatingSetpoint
172         }
173
174         //By Model Checker
175         def setValue(LinkedHashMap eventDataMap) {
176                 if (eventDataMap["name"] == "temperature") {
177                         if (eventDataMap["value"] != thermostats[0].temperature) {
178                                 thermostats[0].setValue(eventDataMap["value"], "temperature")
179                                 this.temperature = thermostats[0].temperature
180                                 sendEvent(eventDataMap)
181                         }
182                 } else if (eventDataMap["name"] == "heatingSetpoint") {
183                         if (eventDataMap["value"] != thermostats[0].heatingSetpoint) {
184                                 thermostats[0].setValue(eventDataMap["value"], "heatingSetpoint")
185                                 this.heatingSetpoint = thermostats[0].heatingSetpoint
186                                 sendEvent(eventDataMap)
187                         }
188                 } else if (eventDataMap["name"] == "coolingSetpoint") {
189                         if (eventDataMap["value"] != thermostats[0].coolingSetpoint) {
190                                 thermostats[0].setValue(eventDataMap["value"], "coolingSetpoint")
191                                 this.coolingSetpoint = thermostats[0].coolingSetpoint
192                                 sendEvent(eventDataMap)
193                         }
194                 } else if (eventDataMap["name"] == "thermostatSetpoint") {
195                         if (eventDataMap["value"] != thermostats[0].thermostatSetpoint) {
196                                 thermostats[0].setValue(eventDataMap["value"], "thermostatSetpoint")
197                                 this.thermostatSetpoint = thermostats[0].thermostatSetpoint
198                                 sendEvent(eventDataMap)
199                         }
200                 } else if (eventDataMap["name"] == "thermostatMode") {
201                         if (eventDataMap["value"] != thermostats[0].thermostatMode) {
202                                 thermostats[0].setValue(eventDataMap["value"], "thermostatMode")
203                                 this.thermostatMode = thermostats[0].thermostatMode
204                                 this.currentThermostatMode = thermostats[0].currentThermostatMode
205                                 sendEvent(eventDataMap)
206                         }
207                 } else if (eventDataMap["name"] == "thermostatFanMode") {
208                         if (eventDataMap["value"] != thermostats[0].thermostatFanMode) {
209                                 thermostats[0].setValue(eventDataMap["value"], "thermostatFanMode")
210                                 this.thermostatFanMode = thermostats[0].thermostatFanMode
211                                 sendEvent(eventDataMap)
212                         }
213                 } else if (eventDataMap["name"] == "thermostatOperatingState") {
214                         if (eventDataMap["value"] != thermostats[0].thermostatOperatingState) {
215                                 thermostats[0].setValue(eventDataMap["value"], "thermostatOperatingState")
216                                 this.thermostatOperatingState = thermostats[0].thermostatOperatingState
217                                 sendEvent(eventDataMap)
218                         }
219                 }
220         }
221
222         def getAt(int ix) {
223                 thermostats[ix]
224         }
225 }
226