-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.cpp
408 lines (321 loc) · 8.84 KB
/
Stats.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
CavyCave - A temperature controlled box for guinea pigs and other
small animals kept outside in winter
Copyright (C) 2020-2021 Flössie <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "Stats.hpp"
#include "Controller.hpp"
namespace
{
constexpr uint32_t period_ms = 5000;
constexpr bool timeAfter(uint32_t a, uint32_t b)
{
return static_cast<int32_t>(b - a) < 0;
}
void printTemperature(int16_t temperature_10th_c)
{
Serial.print(temperature_10th_c / 10);
Serial.print(F("."));
Serial.print(abs(temperature_10th_c) % 10);
Serial.println(F("°C"));
}
void printHumidity(int16_t humidity_per_mill)
{
Serial.print(humidity_per_mill / 10);
Serial.print(F("."));
Serial.print(abs(humidity_per_mill) % 10);
Serial.println(F("%"));
}
void print02(uint32_t value)
{
if (value < 10) {
Serial.print(F("0"));
}
Serial.print(value);
}
void printDuration(uint32_t seconds)
{
const uint32_t hours = seconds / 3600UL;
seconds -= hours * 3600UL;
const uint32_t minutes = seconds / 60UL;
seconds -= minutes * 60UL;
print02(hours);
Serial.print(F(":"));
print02(minutes);
Serial.print(F(":"));
print02(seconds);
Serial.println();
}
}
class Stats::Implementation final
{
public:
Implementation(const Controller& _controller) :
controller(_controller)
{
}
void begin()
{
reset();
}
void run()
{
const uint32_t now = millis();
if (timeAfter(now, next_update_timestamp)) {
constexpr uint32_t seconds = period_ms / 1000UL;
next_update_timestamp += period_ms;
seconds_since_reset += seconds;
const Controller::State state = controller.getState();
if (state.room_values_valid) {
min_room_temperature_10th_c = min(min_room_temperature_10th_c, state.temperature_10th_c);
max_room_temperature_10th_c = max(max_room_temperature_10th_c, state.temperature_10th_c);
min_humidity_per_mill = min(min_humidity_per_mill, state.humidity_per_mill);
max_humidity_per_mill = max(max_humidity_per_mill, state.humidity_per_mill);
}
if (state.floor_value_valid) {
min_floor_temperature_10th_c = min(min_floor_temperature_10th_c, state.floor_temperature_10th_c);
max_floor_temperature_10th_c = max(max_floor_temperature_10th_c, state.floor_temperature_10th_c);
}
if (!prev_lounge_heating && state.heating_lounge) {
++lounge_heating_count;
}
prev_lounge_heating = state.heating_lounge;
if (state.heating_lounge) {
lounge_heating_seconds += seconds;
}
if (!prev_vestibule_heating && state.heating_vestibule) {
++vestibule_heating_count;
}
prev_vestibule_heating = state.heating_vestibule;
if (state.heating_vestibule) {
vestibule_heating_seconds += seconds;
}
if (!prev_fan && state.fan_speed != Fan::Speed::OFF) {
++fan_count;
}
prev_fan = state.fan_speed != Fan::Speed::OFF;
if (state.fan_speed == Fan::Speed::LOW) {
fan_low_seconds += seconds;
}
else if (state.fan_speed == Fan::Speed::HIGH) {
fan_high_seconds += seconds;
}
}
}
void dump() const
{
Serial.println(F("Statistics:"));
Serial.print(F(" Counting for: "));
printDuration(seconds_since_reset);
Serial.print(F(" Minimum temperature: "));
printTemperature(min_room_temperature_10th_c);
Serial.print(F(" Maximum temperature: "));
printTemperature(max_room_temperature_10th_c);
Serial.print(F(" Minimum humidity: "));
printHumidity(min_humidity_per_mill);
Serial.print(F(" Maximum humidity: "));
printHumidity(max_humidity_per_mill);
Serial.print(F(" Minimum floor temperature: "));
printTemperature(min_floor_temperature_10th_c);
Serial.print(F(" Maximum floor temperature: "));
printTemperature(max_floor_temperature_10th_c);
Serial.print(F(" Lounge heating count: "));
Serial.println(lounge_heating_count);
Serial.print(F(" Lounge heating duration: "));
printDuration(lounge_heating_seconds);
Serial.print(F(" Vestibule heating count: "));
Serial.println(vestibule_heating_count);
Serial.print(F(" Vestibule heating duration: "));
printDuration(vestibule_heating_seconds);
Serial.print(F(" Fan run count: "));
Serial.println(fan_count);
Serial.print(F(" Fan LOW duration: "));
printDuration(fan_low_seconds);
Serial.print(F(" Fan HIGH duration: "));
printDuration(fan_high_seconds);
}
void reset()
{
next_update_timestamp = millis() + period_ms;
seconds_since_reset = 0;
min_room_temperature_10th_c = INT16_MAX;
max_room_temperature_10th_c = -INT16_MAX;
min_floor_temperature_10th_c = INT16_MAX;
max_floor_temperature_10th_c = -INT16_MAX;
min_humidity_per_mill = INT16_MAX;
max_humidity_per_mill = -INT16_MAX;
prev_lounge_heating = false;
lounge_heating_count = 0;
lounge_heating_seconds = 0;
prev_vestibule_heating = false;
vestibule_heating_count = 0;
vestibule_heating_seconds = 0;
prev_fan = false;
fan_count = 0;
fan_low_seconds = 0;
fan_high_seconds = 0;
}
uint32_t getSecondsSinceReset() const
{
return seconds_since_reset;
}
int16_t getMinRoomTemperature10thC() const
{
return min_room_temperature_10th_c;
}
int16_t getMaxRoomTemperature10thC() const
{
return max_room_temperature_10th_c;
}
int16_t getMinFloorTemperature10thC() const
{
return min_floor_temperature_10th_c;
}
int16_t getMaxFloorTemperature10thC() const
{
return max_floor_temperature_10th_c;
}
int16_t getMinHumidityPerMill() const
{
return min_humidity_per_mill;
}
int16_t getMaxHumidityPerMill() const
{
return max_humidity_per_mill;
}
uint16_t getLoungeHeatingCount() const
{
return lounge_heating_count;
}
uint32_t getLoungeHeatingSeconds() const
{
return lounge_heating_seconds;
}
uint16_t getVestibuleHeatingCount() const
{
return vestibule_heating_count;
}
uint32_t getVestibuleHeatingSeconds() const
{
return vestibule_heating_seconds;
}
uint16_t getFanCount() const
{
return fan_count;
}
uint32_t getFanLowSeconds() const
{
return fan_low_seconds;
}
uint32_t getFanHighSeconds() const
{
return fan_high_seconds;
}
private:
const Controller& controller;
uint32_t next_update_timestamp;
uint32_t seconds_since_reset;
int16_t min_room_temperature_10th_c;
int16_t max_room_temperature_10th_c;
int16_t min_floor_temperature_10th_c;
int16_t max_floor_temperature_10th_c;
int16_t min_humidity_per_mill;
int16_t max_humidity_per_mill;
bool prev_lounge_heating;
uint16_t lounge_heating_count;
uint32_t lounge_heating_seconds;
bool prev_vestibule_heating;
uint16_t vestibule_heating_count;
uint32_t vestibule_heating_seconds;
bool prev_fan;
uint16_t fan_count;
uint32_t fan_low_seconds;
uint32_t fan_high_seconds;
};
Stats::Stats(const Controller& _controller) :
implementation(new Implementation(_controller))
{
}
void Stats::reset()
{
implementation->reset();
}
void Stats::begin()
{
implementation->begin();
}
void Stats::run()
{
implementation->run();
}
void Stats::dump() const
{
implementation->dump();
}
uint32_t Stats::getSecondsSinceReset() const
{
return implementation->getSecondsSinceReset();
}
int16_t Stats::getMinRoomTemperature10thC() const
{
return implementation->getMinRoomTemperature10thC();
}
int16_t Stats::getMaxRoomTemperature10thC() const
{
return implementation->getMaxRoomTemperature10thC();
}
int16_t Stats::getMinFloorTemperature10thC() const
{
return implementation->getMinFloorTemperature10thC();
}
int16_t Stats::getMaxFloorTemperature10thC() const
{
return implementation->getMaxFloorTemperature10thC();
}
int16_t Stats::getMinHumidityPerMill() const
{
return implementation->getMinHumidityPerMill();
}
int16_t Stats::getMaxHumidityPerMill() const
{
return implementation->getMaxHumidityPerMill();
}
uint16_t Stats::getLoungeHeatingCount() const
{
return implementation->getLoungeHeatingCount();
}
uint32_t Stats::getLoungeHeatingSeconds() const
{
return implementation->getLoungeHeatingSeconds();
}
uint16_t Stats::getVestibuleHeatingCount() const
{
return implementation->getVestibuleHeatingCount();
}
uint32_t Stats::getVestibuleHeatingSeconds() const
{
return implementation->getVestibuleHeatingSeconds();
}
uint16_t Stats::getFanCount() const
{
return implementation->getFanCount();
}
uint32_t Stats::getFanLowSeconds() const
{
return implementation->getFanLowSeconds();
}
uint32_t Stats::getFanHighSeconds() const
{
return implementation->getFanHighSeconds();
}