Skip to content

Commit

Permalink
Merge pull request #137 from airgradienthq/hotfix/correct-ota-update-…
Browse files Browse the repository at this point in the history
…message-on-display

Correct OTA process message show on display
  • Loading branch information
pnt325 authored Jun 4, 2024
2 parents 7569b11 + 14b152a commit fde510b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 23 deletions.
33 changes: 28 additions & 5 deletions examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ void setup() {
#ifdef ESP8266
// ota not supported
#else
otaHandler.setHandlerCallback(otaHandlerCallback);
otaHandler.updateFirmwareIfOutdated(ag->deviceId());

/** Update first OTA */
Expand Down Expand Up @@ -489,6 +490,7 @@ static bool sgp41Init(void) {
}

static void otaHandlerCallback(OtaState state, String mesasge) {
Serial.println("OTA message: " + mesasge);
switch (state) {
case OtaState::OTA_STATE_BEGIN:
displayExecuteOta(state, fwNewVersion, 0);
Expand All @@ -511,7 +513,7 @@ static void displayExecuteOta(OtaState state, String msg, int processing) {
switch (state) {
case OtaState::OTA_STATE_BEGIN: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareVersion(msg);
oledDisplay.showFirmwareUpdateVersion(msg);
} else {
Serial.println("New firmware: " + msg);
}
Expand All @@ -520,17 +522,37 @@ static void displayExecuteOta(OtaState state, String msg, int processing) {
}
case OtaState::OTA_STATE_FAIL: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareFailed();
oledDisplay.showFirmwareUpdateFailed();
} else {
Serial.println("Error: Firmware update: failed");
}

delay(2500);
break;
}
case OtaState::OTA_STATE_SKIP: {
if (ag->isOne()) {
oledDisplay.showFirmwareUpdateSkipped();
} else {
Serial.println("Firmware update: Skipped");
}

delay(2500);
break;
}
case OtaState::OTA_STATE_UP_TO_DATE: {
if (ag->isOne()) {
oledDisplay.showFirmwareUpdateUpToDate();
} else {
Serial.println("Firmware update: up to date");
}

delay(2500);
break;
}
case OtaState::OTA_STATE_PROCESSING: {
if (ag->isOne()) {
oledDisplay.showNewFirmwareUpdating(String(processing));
oledDisplay.showFirmwareUpdateProgress(processing);
} else {
Serial.println("Firmware update: " + String(processing) + String("%"));
}
Expand All @@ -546,13 +568,14 @@ static void displayExecuteOta(OtaState state, String msg, int processing) {
while (i != 0) {
i = i - 1;
if (ag->isOne()) {
oledDisplay.showNewFirmwareSuccess(String(i));
oledDisplay.showFirmwareUpdateSuccess(i);
} else {
Serial.println("Rebooting... " + String(i));
}

delay(1000);
}
oledDisplay.setBrightness(0);
esp_restart();
}
break;
Expand Down Expand Up @@ -884,7 +907,7 @@ static void configUpdateHandle() {
fwNewVersion = configuration.newFirmwareVersion();
if (fwNewVersion.length()) {
bool doOta = false;
if (measurements.otaBootCount == 0) {
if (measurements.otaBootCount < 0) {
doOta = true;
Serial.println("First OTA");
} else {
Expand Down
24 changes: 19 additions & 5 deletions examples/OneOpenAir/OtaHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum OtaUpdateOutcome {
enum OtaState {
OTA_STATE_BEGIN,
OTA_STATE_FAIL,
OTA_STATE_SKIP,
OTA_STATE_UP_TO_DATE,
OTA_STATE_PROCESSING,
OTA_STATE_SUCCESS
};
Expand All @@ -40,13 +42,22 @@ class OtaHandler {
config.url = urlAsChar;
OtaUpdateOutcome ret = attemptToPerformOta(&config);
Serial.println(ret);
if (ret == OtaUpdateOutcome::UPDATE_PERFORMED) {
if (this->callback) {
if (this->callback) {
switch (ret) {
case OtaUpdateOutcome::UPDATE_PERFORMED:
this->callback(OtaState::OTA_STATE_SUCCESS, "");
}
} else {
if(this->callback) {
break;
case OtaUpdateOutcome::UDPATE_SKIPPED:
this->callback(OtaState::OTA_STATE_SKIP, "");
break;
case OtaUpdateOutcome::ALREADY_UP_TO_DATE:
this->callback(OtaState::OTA_STATE_UP_TO_DATE, "");
break;
case OtaUpdateOutcome::UPDATE_FAILED:
this->callback(OtaState::OTA_STATE_FAIL, "");
break;
default:
break;
}
}
}
Expand Down Expand Up @@ -127,6 +138,9 @@ class OtaHandler {
int data_read =
esp_http_client_read(client, upgrade_data_buf, OTA_BUF_SIZE);
if (data_read == 0) {
if (this->callback) {
this->callback(OtaState::OTA_STATE_PROCESSING, String(100));
}
Serial.println("Connection closed, all data received");
break;
}
Expand Down
42 changes: 34 additions & 8 deletions src/AgOledDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void OledDisplay::setBrightness(int percent) {
}
}

void OledDisplay::showNewFirmwareVersion(String version) {
void OledDisplay::showFirmwareUpdateVersion(String version) {
if (isDisplayOff) {
return;
}
Expand All @@ -344,7 +344,7 @@ void OledDisplay::showNewFirmwareVersion(String version) {
} while (DISP()->nextPage());
}

void OledDisplay::showNewFirmwareUpdating(String percent) {
void OledDisplay::showFirmwareUpdateProgress(int percent) {
if (isDisplayOff) {
return;
}
Expand All @@ -353,11 +353,11 @@ void OledDisplay::showNewFirmwareUpdating(String percent) {
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(50, String("Updating... ") + percent + String("%"));
setCentralText(50, String("Updating... ") + String(percent) + String("%"));
} while (DISP()->nextPage());
}

void OledDisplay::showNewFirmwareSuccess(String count) {
void OledDisplay::showFirmwareUpdateSuccess(int count) {
if (isDisplayOff) {
return;
}
Expand All @@ -367,11 +367,11 @@ void OledDisplay::showNewFirmwareSuccess(String count) {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "Success");
setCentralText(60, String("Rebooting... ") + count);
setCentralText(60, String("Rebooting... ") + String(count));
} while (DISP()->nextPage());
}

void OledDisplay::showNewFirmwareFailed(void) {
void OledDisplay::showFirmwareUpdateFailed(void) {
if (isDisplayOff) {
return;
}
Expand All @@ -380,8 +380,34 @@ void OledDisplay::showNewFirmwareFailed(void) {
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "Failed");
setCentralText(60, String("Retry after 24h"));
setCentralText(40, "fail, will retry");
// setCentralText(60, "will retry");
} while (DISP()->nextPage());
}

void OledDisplay::showFirmwareUpdateSkipped(void) {
if (isDisplayOff) {
return;
}

DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "skipped");
} while (DISP()->nextPage());
}

void OledDisplay::showFirmwareUpdateUpToDate(void) {
if (isDisplayOff) {
return;
}

DISP()->firstPage();
do {
DISP()->setFont(u8g2_font_t0_16_tf);
setCentralText(20, "Firmware Update");
setCentralText(40, "up to date");
} while (DISP()->nextPage());
}

Expand Down
10 changes: 6 additions & 4 deletions src/AgOledDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ class OledDisplay : public PrintLog {
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);
void showFirmwareUpdateVersion(String version);
void showFirmwareUpdateProgress(int percent);
void showFirmwareUpdateSuccess(int count);
void showFirmwareUpdateFailed(void);
void showFirmwareUpdateSkipped(void);
void showFirmwareUpdateUpToDate(void);
void showRebooting(void);
};

Expand Down
2 changes: 1 addition & 1 deletion src/AgValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Measurements {
int countPosition;
const int targetCount = 20;
int bootCount;
int otaBootCount;
int otaBootCount = -1;

String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void* _ag, void* _config);
};
Expand Down

0 comments on commit fde510b

Please sign in to comment.