Fix Task Watchdog Timeout (WDT) due to O(N^2) SD card reads

This commit is contained in:
2026-05-10 17:42:10 +02:00
parent a2c46f3438
commit 84e2abae14
2 changed files with 10 additions and 12 deletions

View File

@@ -512,7 +512,7 @@ const char *DataCapture::basenameFromPath(const char *full) { const char *slash
bool DataCapture::isWmtWithDigits(const char* name, uint32_t& idxOut) const {
size_t nlen = strlen(name), extLen = _ext.length();
if (nlen != (size_t)_digits + extLen) return false;
if (strncmp(name + _digits, _ext.c_str(), extLen) != 0) return false;
if (strncmp(name + _digits, _ext.c_str(), extLen) != 0 && strncmp(name + _digits, ".upl", 4) != 0) return false;
for (uint8_t i = 0; i < _digits; ++i) if (name[i] < '0' || name[i] > '9') return false;
char buf[16]; memcpy(buf, name, _digits); buf[_digits] = '\0';
idxOut = (uint32_t)strtoul(buf, nullptr, 10); return true;
@@ -536,6 +536,7 @@ uint32_t DataCapture::findHighestNumericDir() {
if (!root || !root.isDirectory()) return 0;
uint32_t maxDir = 0;
for (File f = root.openNextFile(); f; f = root.openNextFile()) {
Watchdog::feed();
if (f.isDirectory()) {
const char *nm = basenameFromPath(f.name());
if (isAllDigits(nm)) { uint32_t v = toUint(nm); if (v > maxDir) maxDir = v; }
@@ -551,7 +552,8 @@ void DataCapture::scanDirForWmt(uint32_t dirNum, uint32_t &count, uint32_t &high
File dir = _fs.open(path);
if (!dir || !dir.isDirectory()) return;
for (File f = dir.openNextFile(); f; f = dir.openNextFile()) {
if (f.isDirectory()) { Watchdog::feed(); f.close(); continue; }
Watchdog::feed();
if (f.isDirectory()) { f.close(); continue; }
const char* base = basenameFromPath(f.name());
uint32_t idx = 0;
if (isWmtWithDigits(base, idx)) { count++; if (idx > highestIdx) highestIdx = idx; }

View File

@@ -52,11 +52,6 @@ bool UploadManager::isAlreadyUploaded(const String& filePath) {
}
void UploadManager::uploadFile(const String& filePath) {
if (isAlreadyUploaded(filePath)) {
ESP_LOGI(TAG_UPLOAD, "File %s is already uploaded.", filePath.c_str());
return;
}
if (WiFi.status() != WL_CONNECTED) {
ESP_LOGE(TAG_UPLOAD, "No WiFi. Cannot upload %s", filePath.c_str());
appendLog(filePath, "ERROR: No WiFi");
@@ -66,6 +61,9 @@ void UploadManager::uploadFile(const String& filePath) {
bool success = apiClient.uploadMeasurement(filePath);
if (success) {
appendLog(filePath, "OK");
String newPath = filePath;
newPath.replace(".wmt", ".upl");
SD.rename(filePath, newPath);
} else {
if (WiFi.status() != WL_CONNECTED) {
appendLog(filePath, "ERROR: WiFi lost during upload");
@@ -99,11 +97,9 @@ void UploadManager::processPendingUploads() {
if (childPath.endsWith(".wmt")) {
if (childPath == capture_.getCurrentCapturePath()) continue;
if (!isAlreadyUploaded(childPath)) {
ESP_LOGI(TAG_UPLOAD, "Found pending file: %s", childPath.c_str());
uploadFile(childPath);
delay(1000);
}
ESP_LOGI(TAG_UPLOAD, "Found pending file: %s", childPath.c_str());
uploadFile(childPath);
delay(1000);
}
f.close();
Watchdog::feed();