Remove dependency from StateMachine

This commit is contained in:
Phat Nguyen
2024-05-03 17:27:05 +07:00
parent d2723de0f8
commit 8eb8d4a1ec
4 changed files with 68 additions and 72 deletions

View File

@ -113,7 +113,9 @@ 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 otaHandlerCallback(StateMachine::OtaState state, String mesasge); static void otaHandlerCallback(OtaState state, String mesasge);
static void displayExecuteOta(OtaState state, String msg,
int processing);
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, oledDisplayLedBarSchedule); AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, oledDisplayLedBarSchedule);
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL, AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL,
@ -435,25 +437,68 @@ static bool sgp41Init(void) {
return false; return false;
} }
static void otaHandlerCallback(StateMachine::OtaState state, String mesasge) { static void otaHandlerCallback(OtaState state, String mesasge) {
switch (state) { switch (state) {
case StateMachine::OtaState::OTA_STATE_BEGIN: case OtaState::OTA_STATE_BEGIN:
stateMachine.executeOTA(state, fwNewVersion, 0); displayExecuteOta(state, fwNewVersion, 0);
break; break;
case StateMachine::OtaState::OTA_STATE_FAIL: case OtaState::OTA_STATE_FAIL:
stateMachine.executeOTA(state, "", 0); displayExecuteOta(state, "", 0);
break; break;
case StateMachine::OtaState::OTA_STATE_PROCESSING: case OtaState::OTA_STATE_PROCESSING:
stateMachine.executeOTA(state, "", mesasge.toInt()); displayExecuteOta(state, "", mesasge.toInt());
break; break;
case StateMachine::OtaState::OTA_STATE_SUCCESS: case OtaState::OTA_STATE_SUCCESS:
stateMachine.executeOTA(state, "", mesasge.toInt()); displayExecuteOta(state, "", mesasge.toInt());
break; break;
default: default:
break; break;
} }
} }
static void displayExecuteOta(OtaState state, String msg, int processing) {
switch (state) {
case OtaState::OTA_STATE_BEGIN: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareVersion(msg);
} else {
Serial.println("New firmware: " + msg);
}
delay(2500);
break;
}
case OtaState::OTA_STATE_FAIL: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareFailed();
} else {
Serial.println("Error: Firmware update: failed");
}
delay(2500);
break;
}
case OtaState::OTA_STATE_PROCESSING: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareUpdating(String(processing));
} else {
Serial.println("Firmware update: " + String(processing) + String("%"));
}
break;
}
case OtaState::OTA_STATE_SUCCESS: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareSuccess(String(processing));
} else {
Serial.println("Rebooting... " + String(processing));
}
break;
}
default:
break;
}
}
static void sendDataToAg() { static void sendDataToAg() {
/** Change oledDisplay and led state */ /** Change oledDisplay and led state */
if (ag->isOne()) { if (ag->isOne()) {

View File

@ -1,9 +1,5 @@
#ifndef _OTA_HANDLER_H_ #ifndef _OTA_HANDLER_H_
#define _OTA_HANDLER_H_ #define _OTA_HANDLER_H_
#include "AgConfigure.h"
#include "AgStateMachine.h"
#include "AirGradient.h"
#include <Arduino.h> #include <Arduino.h>
#include <esp_err.h> #include <esp_err.h>
#include <esp_http_client.h> #include <esp_http_client.h>
@ -19,7 +15,14 @@ enum OtaUpdateOutcome {
UDPATE_SKIPPED UDPATE_SKIPPED
}; };
typedef void(*OtaHandlerCallback_t)(StateMachine::OtaState state, enum OtaState {
OTA_STATE_BEGIN,
OTA_STATE_FAIL,
OTA_STATE_PROCESSING,
OTA_STATE_SUCCESS
};
typedef void(*OtaHandlerCallback_t)(OtaState state,
String message); String message);
class OtaHandler { class OtaHandler {
@ -43,7 +46,7 @@ public:
while (i != 0) { while (i != 0) {
i = i - 1; i = i - 1;
if (this->callback) { if (this->callback) {
this->callback(StateMachine::OtaState::OTA_STATE_SUCCESS, String(i)); this->callback(OtaState::OTA_STATE_SUCCESS, String(i));
} }
delay(1000); delay(1000);
} }
@ -118,7 +121,7 @@ private:
// Show display start update new firmware. // Show display start update new firmware.
if (this->callback) { if (this->callback) {
this->callback(StateMachine::OtaState::OTA_STATE_BEGIN, ""); this->callback(OtaState::OTA_STATE_BEGIN, "");
} }
// Download file and write new firmware to OTA partition // Download file and write new firmware to OTA partition
@ -133,7 +136,7 @@ private:
if (data_read < 0) { if (data_read < 0) {
Serial.println("Data read error"); Serial.println("Data read error");
if (this->callback) { if (this->callback) {
this->callback(StateMachine::OtaState::OTA_STATE_FAIL, ""); this->callback(OtaState::OTA_STATE_FAIL, "");
} }
break; break;
} }
@ -142,7 +145,7 @@ private:
update_handle, (const void *)upgrade_data_buf, data_read); update_handle, (const void *)upgrade_data_buf, data_read);
if (ota_write_err != ESP_OK) { if (ota_write_err != ESP_OK) {
if (this->callback) { if (this->callback) {
this->callback(StateMachine::OtaState::OTA_STATE_FAIL, ""); this->callback(OtaState::OTA_STATE_FAIL, "");
} }
break; break;
} }
@ -154,7 +157,7 @@ private:
// sm.executeOTA(StateMachine::OtaState::OTA_STATE_PROCESSING, "", // sm.executeOTA(StateMachine::OtaState::OTA_STATE_PROCESSING, "",
// percent); // percent);
if (this->callback) { if (this->callback) {
this->callback(StateMachine::OtaState::OTA_STATE_PROCESSING, this->callback(OtaState::OTA_STATE_PROCESSING,
String(percent)); String(percent));
} }
lastUpdate = millis(); lastUpdate = millis();

View File

@ -759,47 +759,3 @@ void StateMachine::executeCo2Calibration(void) {
void StateMachine::executeLedBarTest(void) { void StateMachine::executeLedBarTest(void) {
handleLeds(AgStateMachineLedBarTest); handleLeds(AgStateMachineLedBarTest);
} }
void StateMachine::executeOTA(StateMachine::OtaState state, String msg,
int processing) {
switch (state) {
case OtaState::OTA_STATE_BEGIN: {
if (ag->isOne()) {
disp.showNewFirmwareVersion(msg);
} else {
logInfo("New firmware: " + msg);
}
delay(2500);
break;
}
case OtaState::OTA_STATE_FAIL: {
if (ag->isOne()) {
disp.showNewFirmwareFailed();
} else {
logError("Firmware update: failed");
}
delay(2500);
break;
}
case OtaState::OTA_STATE_PROCESSING: {
if (ag->isOne()) {
disp.showNewFirmwareUpdating(String(processing));
} else {
logInfo("Firmware update: " + String(processing) + String("%"));
}
break;
}
case OtaState::OTA_STATE_SUCCESS: {
if (ag->isOne()) {
disp.showNewFirmwareSuccess(String(processing));
} else {
logInfo("Rebooting... " + String(processing));
}
break;
}
default:
break;
}
}

View File

@ -49,14 +49,6 @@ public:
AgStateMachineState getLedState(void); AgStateMachineState getLedState(void);
void executeCo2Calibration(void); void executeCo2Calibration(void);
void executeLedBarTest(void); void executeLedBarTest(void);
enum OtaState {
OTA_STATE_BEGIN,
OTA_STATE_FAIL,
OTA_STATE_PROCESSING,
OTA_STATE_SUCCESS
};
void executeOTA(OtaState state, String msg, int processing);
}; };
#endif /** _AG_STATE_MACHINE_H_ */ #endif /** _AG_STATE_MACHINE_H_ */