Merge pull request #104 from airgradienthq/feature/show-ota-process-on-display

Feature/show ota process on display
This commit is contained in:
Phat Nguyen
2024-05-09 06:23:03 +07:00
committed by GitHub
8 changed files with 283 additions and 9 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;
}

View File

@ -28,6 +28,7 @@ private:
int tvocLearningOffset;
int noxLearningOffset;
char temperatureUnit; // 'f' or 'c'
time_t lastOta;
uint32_t _check;
};
@ -40,6 +41,7 @@ private:
bool _tvocLearningOffsetChanged;
bool ledBarBrightnessChanged = false;
bool displayBrightnessChanged = false;
String otaNewFirmwareVersion;
AirGradient* ag;
@ -97,6 +99,9 @@ public:
int getLedBarBrightness(void);
bool isDisplayBrightnessChanged(void);
int getDisplayBrightness(void);
int getLastOta(void);
void updateLastOta(void);
String newFirmwareVersion(void);
};
#endif /** _AG_CONFIG_H_ */

View File

@ -49,6 +49,16 @@ void OledDisplay::showTempHum(bool hasStatus) {
}
}
void OledDisplay::setCentralText(int y, String text) {
setCentralText(y, text.c_str());
}
void OledDisplay::setCentralText(int y, const char *text) {
int x = (DISP()->getWidth() - DISP()->getStrWidth(text)) / 2;
DISP()->drawStr(x, y, text);
}
/**
* @brief Construct a new Ag Oled Display:: Ag Oled Display object
*
@ -291,3 +301,42 @@ void OledDisplay::showDashboard(const char *status) {
void OledDisplay::setBrightness(int percent) {
DISP()->setContrast((127 * percent) / 100);
}
void OledDisplay::showNewFirmwareVersion(String version) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "New version");
setCentralText(60, version.c_str());
} while (DISP()->nextPage());
}
void OledDisplay::showNewFirmwareUpdating(String percent) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(50, String("Updating... ") + percent + String("%"));
} while (DISP()->nextPage());
}
void OledDisplay::showNewFirmwareSuccess(String count) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "Success");
setCentralText(60, String("Rebooting... ") + count);
} while (DISP()->nextPage());
}
void OledDisplay::showNewFirmwareFailed(void) {
DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "Failed");
setCentralText(60, String("Retry after 24h"));
} while (DISP()->nextPage());
}

View File

@ -16,6 +16,9 @@ private:
Measurements &value;
void showTempHum(bool hasStatus);
void setCentralText(int y, String text);
void setCentralText(int y, const char *text);
public:
OledDisplay(Configuration &config, Measurements &value,
Stream &log);
@ -32,6 +35,10 @@ public:
void showDashboard(void);
void showDashboard(const char *status);
void setBrightness(int percent);
void showNewFirmwareVersion(String version);
void showNewFirmwareUpdating(String percent);
void showNewFirmwareSuccess(String count);
void showNewFirmwareFailed(void);
};
#endif /** _AG_OLED_DISPLAY_H_ */

View File

@ -1,5 +1,6 @@
#include "AgWiFiConnector.h"
#include "Libraries/WiFiManager/WiFiManager.h"
#include <time.h>
#define WIFI_CONNECT_COUNTDOWN_MAX 180
#define WIFI_HOTSPOT_PASSWORD_DEFAULT "cleanair"
@ -158,6 +159,11 @@ bool WifiConnector::connect(void) {
config.setPostToAirGradient(result != "T");
}
hasPortalConfig = false;
/** Configure internet time */
const char *ntp_server = "pool.ntp.org";
configTime(0, 0, ntp_server);
logInfo("Set internet time server: " + String(ntp_server));
}
#else
_wifiProcess();

View File

@ -61,6 +61,9 @@ enum AgStateMachineState {
AgStateMachineLedBarTest,
AgStateMachineLedBarPowerUpTest,
/** OTA perform, show display status */
AgStateMachineOtaPerform,
/** LED: Show working state.
* Display: Show dashboard */
AgStateMachineNormal,