2025-07-15 22:28:23 +02:00
|
|
|
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
|
|
|
|
|
*
|
|
|
|
|
* This file is part of LoRa APRS iGate.
|
|
|
|
|
*
|
|
|
|
|
* LoRa APRS iGate is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* LoRa APRS iGate is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
2025-03-10 07:02:48 +01:00
|
|
|
|
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 {
|
2025-05-18 14:44:46 +02:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2025-05-18 14:44:46 +02:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
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);
|
2025-05-18 14:44:46 +02:00
|
|
|
|
2024-03-07 17:46:38 +01:00
|
|
|
isUpdatingOTA = false;
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|
2025-05-18 14:44:46 +02:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|