Repository navigation
Expand file tree
/
Copy pathCounter.cpp
More file actions
105 lines (94 loc) · 3.05 KB
/
Copy pathCounter.cpp
File metadata and controls
105 lines (94 loc) · 3.05 KB
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
#include <Arduino.h>
#include "Counter.h"
Counter::Counter(Handler* handler) {
_handler = handler;
}
void Counter::setTotal(long total) {
_total = total;
}
void Counter::count() {
_total++;
_partialLastMinute++;
_partialLastHour++;
_partialLastDay++;
}
void Counter::initTimers() {
long now = millis();
_nextMinute = now + T_MINUTE;
_nextHour = now + T_HOUR;
_nextDay = now + T_DAY;
}
void Counter::setup() {
initTimers();
_handler->setup();
}
void Counter::loop() {
long now = millis();
long lastMinuteValue = -1;
long lastHourValue = -1;
long lastDayValue = -1;
if (now > _nextMinute) {
lastMinuteValue = _partialLastMinute;
if (now > _nextHour) {
lastHourValue = _partialLastHour;
}
if (now > _nextDay) {
lastDayValue = _partialLastDay;
}
if (now > (_lastFail + T_FAIL_DELAY)) {
Serial.println("Vai tentar enviar...");
//noInterrupts(); // Parece boa pratica suspender interrupcoes nos momentos de tratamento, mas gerou bug com o AsyncElegantOTA
boolean sent = _handler->sendValues(_total, lastMinuteValue, lastHourValue, lastDayValue);
//interrupts(); // Volta a receber interrupcoes
if (sent) {
_sendCount++;
_lastSend = now / 1000;
_sequenceFailCount = 0;
// Se houve incremento durante o envio, recupera esse incremento
if (_partialLastMinute > lastMinuteValue) {
_partialLastMinute = _partialLastMinute - lastMinuteValue;
_incsDuringSend++;
} else
_partialLastMinute = 0;
_nextMinute = now + T_MINUTE;
if (lastHourValue > -1) {
// Se houve incremento durante o envio, recupera esse incremento
if (_partialLastHour > lastHourValue)
_partialLastHour = _partialLastHour - lastHourValue;
else
_partialLastHour = 0;
_nextHour = now + T_HOUR;
}
if (lastDayValue > -1) {
// Se houve incremento durante o envio, recupera esse incremento
if (_partialLastDay > lastDayValue)
_partialLastDay = _partialLastDay - lastDayValue;
else
_partialLastDay = 0;
_nextDay = now + T_DAY;
}
} else {
_failCount++;
_sequenceFailCount++;
if (_sequenceFailCount > FAIL_COUNT_LIMIT) {
Serial.println("Too many fails in sequence. Restarting..");
ESP.restart();
}
_lastFail = now;
}
}
}
_handler->loop();
}
String Counter::toString() {
String result = "";
return result + "Total: " + _total + "\r\n" +
"Partial last minute: " + _partialLastMinute + "\r\n" +
"Partial last hour: " + _partialLastHour + "\r\n" +
"Partial last day: " + _partialLastDay + "\r\n" +
"Last send: " + _lastSend + "\r\n"
"Sends: " + _sendCount + "\r\n"
"Fails: " + _failCount + "\r\n"
"Sequence fails: " + _sequenceFailCount + "\r\n"
"Increments during send: " + _incsDuringSend;
}