From 84e2abae143e8be1a08a20a2bc5caa7ba8c66d54 Mon Sep 17 00:00:00 2001 From: R_Duszkiewicz <181826@stud.prz.edu.pl> Date: Sun, 10 May 2026 17:42:10 +0200 Subject: [PATCH] Fix Task Watchdog Timeout (WDT) due to O(N^2) SD card reads --- src/Measure.cpp | 6 ++++-- src/UploadManager.cpp | 16 ++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Measure.cpp b/src/Measure.cpp index 87bff02..556d937 100644 --- a/src/Measure.cpp +++ b/src/Measure.cpp @@ -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; } diff --git a/src/UploadManager.cpp b/src/UploadManager.cpp index b53ebe4..235ed6e 100644 --- a/src/UploadManager.cpp +++ b/src/UploadManager.cpp @@ -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();