|
6 | 6 | #include "Configuration.h"
|
7 | 7 | #include "Datastore.h"
|
8 | 8 | #include <algorithm>
|
| 9 | +#include <Preferences.h> |
| 10 | + |
| 11 | + |
| 12 | +// Expected time after a restart all inverters are ready |
| 13 | +#define EXPECTED_SECS_INVERTERS_AVAILABLE_AFTER_RESTART 60 |
| 14 | + |
9 | 15 |
|
10 | 16 | DisplayGraphicDiagramClass::DisplayGraphicDiagramClass()
|
11 | 17 | : _averageTask(1 * TASK_SECOND, TASK_FOREVER, std::bind(&DisplayGraphicDiagramClass::averageLoop, this))
|
12 | 18 | , _dataPointTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&DisplayGraphicDiagramClass::dataPointLoop, this))
|
13 | 19 | {
|
14 | 20 | }
|
15 | 21 |
|
| 22 | + |
16 | 23 | void DisplayGraphicDiagramClass::init(Scheduler& scheduler, U8G2* display)
|
17 | 24 | {
|
| 25 | + bool delayUpdateTasks; |
| 26 | + |
18 | 27 | _display = display;
|
19 | 28 |
|
| 29 | + restoreGraphValuesAfterRestart(delayUpdateTasks); |
| 30 | + |
20 | 31 | scheduler.addTask(_averageTask);
|
21 |
| - _averageTask.enable(); |
| 32 | + if (delayUpdateTasks) { |
| 33 | + _averageTask.restartDelayed(EXPECTED_SECS_INVERTERS_AVAILABLE_AFTER_RESTART * TASK_SECOND); |
| 34 | + } else { |
| 35 | + _averageTask.enable(); |
| 36 | + } |
22 | 37 |
|
23 | 38 | scheduler.addTask(_dataPointTask);
|
24 | 39 | updatePeriod();
|
25 |
| - _dataPointTask.enable(); |
| 40 | + if (delayUpdateTasks) { |
| 41 | + _dataPointTask.restartDelayed(EXPECTED_SECS_INVERTERS_AVAILABLE_AFTER_RESTART * TASK_SECOND); |
| 42 | + } else { |
| 43 | + _dataPointTask.enable(); |
| 44 | + } |
26 | 45 | }
|
27 | 46 |
|
28 | 47 | void DisplayGraphicDiagramClass::averageLoop()
|
@@ -134,3 +153,59 @@ void DisplayGraphicDiagramClass::redraw(uint8_t screenSaverOffsetX, uint8_t xPos
|
134 | 153 | graphPosX + i / scaleFactorX, horizontal_line_y - std::max<int16_t>(0, _graphValues[i] / scaleFactorY - 0.5));
|
135 | 154 | }
|
136 | 155 | }
|
| 156 | + |
| 157 | +void DisplayGraphicDiagramClass::backupGraphValuesBeforeRestart() |
| 158 | +{ |
| 159 | + auto prefs = Preferences(); |
| 160 | + |
| 161 | + if (!prefs.begin("OPENDtuGraphVal")) { |
| 162 | + return; |
| 163 | + } |
| 164 | + _dataPointTask.disable(); |
| 165 | + |
| 166 | + // Find an easy way to wait till a possible current execution of _dataPointTask has ended |
| 167 | + // Or: Use a mutex here and in dataPointLoop() |
| 168 | + // For now: I assume restarting is very rare and we validate everything in restoreGraphValuesAfterRestart() |
| 169 | + prefs.putUChar("count", _graphValuesCount); |
| 170 | + prefs.putBytes("vars", &_graphValues[0], sizeof(_graphValues)); |
| 171 | + prefs.end(); |
| 172 | +} |
| 173 | + |
| 174 | +void DisplayGraphicDiagramClass::restoreGraphValuesAfterRestart(bool& delayUpdateTasks) |
| 175 | +{ |
| 176 | + auto prefs = Preferences(); |
| 177 | + |
| 178 | + delayUpdateTasks = false; |
| 179 | + |
| 180 | + if (!prefs.begin("OPENDtuGraphVal")) { |
| 181 | + return; |
| 182 | + } |
| 183 | + _graphValuesCount = prefs.getUChar("count", _graphValuesCount); |
| 184 | + if (_graphValuesCount >= std::size(_graphValues)) { |
| 185 | + _graphValuesCount = std::size(_graphValues) - 1; |
| 186 | + } |
| 187 | + prefs.getBytes("vars", &_graphValues[0], sizeof(_graphValues)); |
| 188 | + prefs.clear(); // clear - so only after a Utils::restartDtu() the variables are available |
| 189 | + prefs.end(); |
| 190 | + |
| 191 | + // Check if it was a restart due software reset |
| 192 | + if (esp_reset_reason() != ESP_RST_SW) { |
| 193 | + _graphValuesCount = 0; |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + // Delaying the data collection makes no sense if a dot is less than the time we need |
| 198 | + // to query all the inverters after a reboot. |
| 199 | + // At 60secs this equals a diagram period of ~7680sec (2h8m) on a 128 dot display |
| 200 | + _chartWidth = _display->getDisplayWidth(); // assume diagram use the whole display |
| 201 | + if (getSecondsPerDot() <= EXPECTED_SECS_INVERTERS_AVAILABLE_AFTER_RESTART) { |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + delayUpdateTasks = (_graphValuesCount != 0); |
| 206 | +} |
| 207 | + |
| 208 | +void DisplayGraphicDiagramClass::prepareDtuRestart() |
| 209 | +{ |
| 210 | + backupGraphValuesBeforeRestart(); |
| 211 | +} |
0 commit comments