Implement regular OTA update attempt / indicate OTA processing on display

This commit is contained in:
Phat Nguyen
2024-05-02 10:19:49 +07:00
parent 0acb7d470d
commit 4493156739
10 changed files with 252 additions and 10 deletions

View File

@ -6,6 +6,7 @@
#else
#include "EEPROM.h"
#endif
#include <time.h>
#define EEPROM_CONFIG_SIZE 512
#define CONFIG_FILE_NAME "/cfg.bin"
@ -163,6 +164,7 @@ void Configuration::defaultConfig(void) {
config.temperatureUnit = 'c';
config.ledBarBrightness = 100;
config.displayBrightness = 100;
config.lastOta = 0;
saveConfig();
}
@ -171,7 +173,10 @@ void Configuration::defaultConfig(void) {
* @brief Show configuration as JSON string message over log
*
*/
void Configuration::printConfig(void) { logInfo(toString().c_str()); }
void Configuration::printConfig(void) {
logInfo(toString().c_str());
logInfo("Last OTA time: " + String(config.lastOta));
}
/**
* @brief Construct a new Ag Configure:: Ag Configure object
@ -638,6 +643,18 @@ bool Configuration::parse(String data, bool isLocal) {
}
}
if (JSON.typeof_(root["targetFirmware"]) == "string") {
String newVer = root["targetFirmware"];
String curVer = String(GIT_VERSION);
if (curVer != newVer) {
logInfo("Detected new firwmare version: " + newVer);
otaNewFirmwareVersion = newVer;
udpated = true;
} else {
otaNewFirmwareVersion = String("");
}
}
if (changed) {
udpated = true;
saveConfig();
@ -938,3 +955,52 @@ bool Configuration::isDisplayBrightnessChanged(void) {
displayBrightnessChanged = false;
return changed;
}
/**
* @brief Get number of sec from last OTA
*
* @return int < 0 is invalid, 0 mean there is no OTA trigger.
*/
int Configuration::getLastOta(void) {
struct tm timeInfo;
if (getLocalTime(&timeInfo) == false) {
logError("Get localtime failed");
return -1;
}
int curYear = timeInfo.tm_year + 1900;
if (curYear < 2024) {
logError("Current year " + String(curYear) + String(" invalid"));
return -1;
}
time_t curTime = mktime(&timeInfo);
logInfo("Last ota time: " + String(config.lastOta));
if (config.lastOta == 0) {
return 0;
}
int sec = curTime - config.lastOta;
logInfo("Last ota secconds: " + String(sec));
return sec;
}
void Configuration::updateLastOta(void) {
struct tm timeInfo;
if (getLocalTime(&timeInfo) == false) {
logError("updateLastOta: Get localtime failed");
return;
}
int curYear = timeInfo.tm_year + 1900;
if (curYear < 2024) {
logError("updateLastOta: lolcal time invalid");
return;
}
config.lastOta = mktime(&timeInfo);
logInfo("Last OTA: " + String(config.lastOta));
saveConfig();
}
String Configuration::newFirmwareVersion(void) {
String newFw = otaNewFirmwareVersion;
otaNewFirmwareVersion = String("");
return newFw;
}