LoRa_APRS_iGate/src/ota_utils.cpp

61 lines
1.9 KiB
C++
Raw Normal View History

2024-02-24 14:09:05 +01:00
#include <ESPAsyncWebServer.h>
#include <ElegantOTA.h>
#include <AsyncTCP.h>
#include "configuration.h"
#include "ota_utils.h"
2024-02-25 16:00:44 +01:00
#include "display.h"
2024-02-24 14:09:05 +01:00
extern Configuration Config;
extern uint32_t lastScreenOn;
2024-03-07 17:46:38 +01:00
extern bool isUpdatingOTA;
2024-02-24 14:09:05 +01:00
unsigned long ota_progress_millis = 0;
2024-02-25 16:00:44 +01:00
2024-02-24 14:09:05 +01:00
namespace OTA_Utils {
void setup(AsyncWebServer *server) {
if (Config.ota.username != "" && Config.ota.password != "") {
ElegantOTA.begin(server, Config.ota.username.c_str(), Config.ota.password.c_str());
} else {
ElegantOTA.begin(server);
}
ElegantOTA.setAutoReboot(true);
ElegantOTA.onStart(onOTAStart);
ElegantOTA.onProgress(onOTAProgress);
ElegantOTA.onEnd(onOTAEnd);
}
void onOTAStart() {
Serial.println("OTA update started!");
2024-08-14 18:32:34 +02:00
displayToggle(true);
2024-02-24 14:09:05 +01:00
lastScreenOn = millis();
2024-08-14 18:32:34 +02:00
displayShow("", "", "", " OTA update started!", "", "", "", 1000);
2024-03-07 17:46:38 +01:00
isUpdatingOTA = true;
2024-02-24 14:09:05 +01:00
}
void onOTAProgress(size_t current, size_t final) {
if (millis() - ota_progress_millis > 1000) {
2024-08-14 18:32:34 +02:00
displayToggle(true);
2024-02-24 14:09:05 +01:00
lastScreenOn = millis();
ota_progress_millis = millis();
Serial.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final);
2024-08-14 18:32:34 +02:00
displayShow("", "", " OTA Progress : " + String((current*100)/final) + "%", "", "", "", "", 100);
2024-02-24 14:09:05 +01:00
}
}
void onOTAEnd(bool success) {
2024-08-14 18:32:34 +02:00
displayToggle(true);
2024-02-24 14:09:05 +01:00
lastScreenOn = millis();
2025-02-12 19:28:48 +01:00
String statusMessage = success ? "OTA update success!" : "OTA update fail!";
String rebootMessage = success ? "Rebooting ..." : "";
Serial.println(success ? "OTA update finished successfully!" : "There was an error during OTA update!");
displayShow("", "", statusMessage, "", rebootMessage, "", "", 4000);
2024-03-07 17:46:38 +01:00
isUpdatingOTA = false;
2024-02-24 14:09:05 +01:00
}
2024-02-25 16:00:44 +01:00
2024-02-24 14:09:05 +01:00
}