Fix UploadManager CSV bottleneck and measure() race condition

This commit is contained in:
2026-05-10 19:14:24 +02:00
parent 10d187eb37
commit 382433b91f
11 changed files with 58 additions and 242 deletions

View File

@@ -16,59 +16,34 @@ String UploadManager::getCurrentTimestamp() {
}
void UploadManager::appendLog(const String& filePath, const String& status) {
File f = SD.open(LOG_FILE, FILE_APPEND);
if (f) {
f.printf("%s,%s,%s\n", getCurrentTimestamp().c_str(), filePath.c_str(), status.c_str());
f.close();
}
// Legacy CSV log removed to fix O(N^2) SD card bottleneck
}
bool UploadManager::isAlreadyUploaded(const String& filePath) {
File f = SD.open(LOG_FILE, FILE_READ);
if (!f) return false;
bool found = false;
while (f.available()) {
String line = f.readStringUntil('\n');
line.trim();
if (line.length() == 0) continue;
int firstComma = line.indexOf(',');
if (firstComma < 0) continue;
int secondComma = line.indexOf(',', firstComma + 1);
if (secondComma < 0) continue;
String logPath = line.substring(firstComma + 1, secondComma);
String logStatus = line.substring(secondComma + 1);
if (logPath == filePath) {
if (logStatus == "OK") found = true;
else found = false; // A retry might be needed if last status wasn't OK
}
Watchdog::feed();
}
f.close();
return found;
// We now rely on file extensions (.wmt = pending, .upl = uploaded)
return false;
}
void UploadManager::uploadFile(const String& filePath) {
if (WiFi.status() != WL_CONNECTED) {
ESP_LOGE(TAG_UPLOAD, "No WiFi. Cannot upload %s", filePath.c_str());
appendLog(filePath, "ERROR: No WiFi");
return;
}
bool success = apiClient.uploadMeasurement(filePath);
if (success) {
appendLog(filePath, "OK");
String newPath = filePath;
newPath.replace(".wmt", ".upl");
SD.rename(filePath, newPath);
if (SD.rename(filePath, newPath)) {
ESP_LOGI(TAG_UPLOAD, "Renamed %s to .upl", filePath.c_str());
} else {
ESP_LOGE(TAG_UPLOAD, "Rename to .upl failed for %s", filePath.c_str());
}
} else {
if (WiFi.status() != WL_CONNECTED) {
appendLog(filePath, "ERROR: WiFi lost during upload");
ESP_LOGE(TAG_UPLOAD, "WiFi lost during upload");
} else {
appendLog(filePath, "ERROR: Upload Failed");
ESP_LOGE(TAG_UPLOAD, "Upload Failed");
}
}
}

View File

@@ -369,7 +369,7 @@ void measure(){
ESP_LOGI(TAG_MAIN, "MEASURE RUNNING");
xSemaphoreTake(sdMutex, portMAX_DELAY);
capture.captureAuto(config.duration, "/");
xSemaphoreGive(sdMutex);
testingNow = false;
Watchdog::feed();
if(!capture.isExit){
@@ -379,6 +379,7 @@ void measure(){
} else {
ESP_LOGI(TAG_MAIN, "MEASURE INTERRUPT");
}
xSemaphoreGive(sdMutex);
runMeasure = false;
ESP_LOGI(TAG_MAIN, "DISPLAY Offline");