2025-02-17 12:41:25 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "ota.h"
|
2025-02-18 11:42:52 +01:00
|
|
|
#include <Update.h>
|
|
|
|
#include <SPIFFS.h>
|
|
|
|
#include "commonFS.h"
|
2025-02-20 11:52:36 +01:00
|
|
|
#include "bambu.h"
|
|
|
|
#include "scale.h"
|
|
|
|
#include "nfc.h"
|
2025-02-18 11:42:52 +01:00
|
|
|
|
2025-02-20 14:08:17 +01:00
|
|
|
static bool tasksAreStopped = false;
|
2025-02-20 10:53:23 +01:00
|
|
|
|
2025-02-20 11:52:36 +01:00
|
|
|
void stopAllTasks() {
|
2025-02-20 14:08:17 +01:00
|
|
|
Serial.println("Stopping RFID Reader");
|
|
|
|
if (RfidReaderTask) vTaskSuspend(RfidReaderTask);
|
|
|
|
Serial.println("Stopping Bambu");
|
|
|
|
if (BambuMqttTask) vTaskSuspend(BambuMqttTask);
|
|
|
|
Serial.println("Stopping Scale");
|
|
|
|
if (ScaleTask) vTaskSuspend(ScaleTask);
|
2025-02-20 12:00:45 +01:00
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
Serial.println("All tasks stopped");
|
2025-02-20 11:42:36 +01:00
|
|
|
}
|
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
|
|
|
|
if (!index) {
|
2025-02-20 20:04:58 +01:00
|
|
|
bool isFullImage = filename.endsWith(".bin");
|
2025-02-20 17:04:56 +01:00
|
|
|
Serial.printf("Update Start: %s (type: %s)\n", filename.c_str(), isFullImage ? "full" : "OTA");
|
|
|
|
|
2025-02-20 16:14:49 +01:00
|
|
|
if (request->contentLength() == 0) {
|
2025-02-18 14:18:14 +01:00
|
|
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Invalid file size\"}");
|
2025-02-18 11:42:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-20 20:04:58 +01:00
|
|
|
// Berechne verfügbaren Speicherplatz
|
|
|
|
size_t updateSize = request->contentLength();
|
|
|
|
size_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
|
|
|
|
|
|
|
Serial.printf("Update size: %u bytes\n", updateSize);
|
|
|
|
Serial.printf("Available space: %u bytes\n", maxSketchSpace);
|
|
|
|
|
|
|
|
if (updateSize > maxSketchSpace) {
|
|
|
|
Serial.printf("Error: Not enough space. Need %u bytes but only have %u bytes available\n",
|
|
|
|
updateSize, maxSketchSpace);
|
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Not enough space for update\"}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:16:49 +01:00
|
|
|
if (!tasksAreStopped && (RfidReaderTask || BambuMqttTask || ScaleTask)) {
|
|
|
|
stopAllTasks();
|
|
|
|
tasksAreStopped = true;
|
|
|
|
}
|
|
|
|
|
2025-02-20 20:04:58 +01:00
|
|
|
// Ensure SPIFFS is ended before update
|
|
|
|
if (SPIFFS.begin()) {
|
|
|
|
SPIFFS.end();
|
|
|
|
}
|
|
|
|
|
2025-02-20 17:04:56 +01:00
|
|
|
bool success;
|
|
|
|
if (isFullImage) {
|
2025-02-20 20:04:58 +01:00
|
|
|
success = Update.begin(updateSize, U_FLASH);
|
2025-02-20 17:04:56 +01:00
|
|
|
} else {
|
|
|
|
if (data[0] != 0xE9) {
|
|
|
|
Serial.printf("Wrong magic byte: 0x%02X (expected 0xE9)\n", data[0]);
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Invalid firmware format\"}");
|
2025-02-20 17:04:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2025-02-20 20:04:58 +01:00
|
|
|
success = Update.begin(updateSize);
|
2025-02-20 17:04:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
2025-02-20 14:56:31 +01:00
|
|
|
Update.printError(Serial);
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Update initialization failed\"}");
|
2025-02-20 14:56:31 +01:00
|
|
|
return;
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
2025-02-20 15:07:13 +01:00
|
|
|
}
|
|
|
|
|
2025-02-20 14:56:31 +01:00
|
|
|
if (Update.write(data, len) != len) {
|
|
|
|
Update.printError(Serial);
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Write failed\"}");
|
2025-02-20 14:45:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-18 11:42:52 +01:00
|
|
|
if (final) {
|
2025-02-20 14:45:34 +01:00
|
|
|
if (!Update.end(true)) {
|
2025-02-20 14:56:31 +01:00
|
|
|
Update.printError(Serial);
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(400, "application/json",
|
|
|
|
"{\"status\":\"error\",\"message\":\"Update failed\"}");
|
2025-02-20 14:56:31 +01:00
|
|
|
return;
|
2025-02-20 14:45:34 +01:00
|
|
|
}
|
2025-02-20 15:07:13 +01:00
|
|
|
Serial.println("Update successful, restarting...");
|
2025-02-20 20:04:58 +01:00
|
|
|
request->send(200, "application/json",
|
|
|
|
"{\"status\":\"success\",\"message\":\"Update successful! Device will restart...\",\"restart\":true}");
|
2025-02-20 14:45:34 +01:00
|
|
|
delay(500);
|
|
|
|
ESP.restart();
|
2025-02-18 11:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-17 12:41:25 +01:00
|
|
|
|