Merge pull request #170 from airgradienthq/feature/regularly-call-ota-handler

OTA update each 60 min
This commit is contained in:
Phat Nguyen
2024-06-05 13:38:05 +07:00
committed by GitHub
2 changed files with 22 additions and 32 deletions

View File

@ -64,6 +64,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
#define SENSOR_PM_UPDATE_INTERVAL 2000 /** ms */ #define SENSOR_PM_UPDATE_INTERVAL 2000 /** ms */
#define SENSOR_TEMP_HUM_UPDATE_INTERVAL 2000 /** ms */ #define SENSOR_TEMP_HUM_UPDATE_INTERVAL 2000 /** ms */
#define DISPLAY_DELAY_SHOW_CONTENT_MS 2000 /** ms */ #define DISPLAY_DELAY_SHOW_CONTENT_MS 2000 /** ms */
#define FIRMWARE_CHECK_FOR_UPDATE_MS (60*60*1000) /** ms */
/** I2C define */ /** I2C define */
#define I2C_SDA_PIN 7 #define I2C_SDA_PIN 7
@ -113,6 +114,7 @@ static void factoryConfigReset(void);
static void wdgFeedUpdate(void); static void wdgFeedUpdate(void);
static void ledBarEnabledUpdate(void); static void ledBarEnabledUpdate(void);
static bool sgp41Init(void); static bool sgp41Init(void);
static void firmwareCheckForUpdate(void);
static void otaHandlerCallback(OtaState state, String mesasge); static void otaHandlerCallback(OtaState state, String mesasge);
static void displayExecuteOta(OtaState state, String msg, static void displayExecuteOta(OtaState state, String msg,
int processing); int processing);
@ -126,6 +128,7 @@ AgSchedule pmsSchedule(SENSOR_PM_UPDATE_INTERVAL, updatePm);
AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumUpdate); AgSchedule tempHumSchedule(SENSOR_TEMP_HUM_UPDATE_INTERVAL, tempHumUpdate);
AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, updateTvoc); AgSchedule tvocSchedule(SENSOR_TVOC_UPDATE_INTERVAL, updateTvoc);
AgSchedule watchdogFeedSchedule(60000, wdgFeedUpdate); AgSchedule watchdogFeedSchedule(60000, wdgFeedUpdate);
AgSchedule checkForUpdateSchedule(FIRMWARE_CHECK_FOR_UPDATE_MS, firmwareCheckForUpdate);
void setup() { void setup() {
/** Serial for print debug message */ /** Serial for print debug message */
@ -219,11 +222,8 @@ void setup() {
#ifdef ESP8266 #ifdef ESP8266
// ota not supported // ota not supported
#else #else
otaHandler.setHandlerCallback(otaHandlerCallback); firmwareCheckForUpdate();
otaHandler.updateFirmwareIfOutdated(ag->deviceId()); checkForUpdateSchedule.update();
/** Update first OTA */
measurements.otaBootCount = 0;
#endif #endif
apiClient.fetchServerConfiguration(); apiClient.fetchServerConfiguration();
@ -313,6 +313,9 @@ void loop() {
/** check that local configura changed then do some action */ /** check that local configura changed then do some action */
configUpdateHandle(); configUpdateHandle();
/** Firmware check for update handle */
checkForUpdateSchedule.run();
} }
static void co2Update(void) { static void co2Update(void) {
@ -490,6 +493,20 @@ static bool sgp41Init(void) {
return false; return false;
} }
static void firmwareCheckForUpdate(void) {
Serial.println();
Serial.println("firmwareCheckForUpdate:");
if (wifiConnector.isConnected()) {
Serial.println("firmwareCheckForUpdate: Perform");
otaHandler.setHandlerCallback(otaHandlerCallback);
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
} else {
Serial.println("firmwareCheckForUpdate: Ignored");
}
Serial.println();
}
static void otaHandlerCallback(OtaState state, String mesasge) { static void otaHandlerCallback(OtaState state, String mesasge) {
Serial.println("OTA message: " + mesasge); Serial.println("OTA message: " + mesasge);
switch (state) { switch (state) {
@ -905,32 +922,6 @@ static void configUpdateHandle() {
stateMachine.executeLedBarTest(); stateMachine.executeLedBarTest();
} }
fwNewVersion = configuration.newFirmwareVersion();
if (fwNewVersion.length()) {
bool doOta = false;
if (measurements.otaBootCount < 0) {
doOta = true;
Serial.println("First OTA");
} else {
/** Only check for update each 1h*/
const float otaBootCount = 60.0f / (SERVER_SYNC_INTERVAL / 60000.0f);
if ((measurements.bootCount - measurements.otaBootCount) >= (int)otaBootCount) {
doOta = true;
} else {
Serial.println(
"OTA ignore, try again next " +
String(30 - (measurements.bootCount - measurements.otaBootCount)) +
String(" boots"));
}
}
if (doOta) {
measurements.otaBootCount = measurements.bootCount;
otaHandler.setHandlerCallback(otaHandlerCallback);
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
}
}
appDispHandler(); appDispHandler();
appLedHandler(); appLedHandler();
} }

View File

@ -69,7 +69,6 @@ public:
int countPosition; int countPosition;
const int targetCount = 20; const int targetCount = 20;
int bootCount; int bootCount;
int otaBootCount = -1;
String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void* _ag, void* _config); String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void* _ag, void* _config);
}; };