mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-06 04:36:31 +02:00
Remove OtaHandler
constructor
This commit is contained in:
@ -82,7 +82,7 @@ static WifiConnector wifiConnector(oledDisplay, Serial, stateMachine,
|
||||
configuration);
|
||||
static OpenMetrics openMetrics(measurements, configuration, wifiConnector,
|
||||
apiClient);
|
||||
static OtaHandler otaHandler(stateMachine, configuration);
|
||||
static OtaHandler otaHandler;
|
||||
static LocalServer localServer(Serial, openMetrics, measurements, configuration,
|
||||
wifiConnector);
|
||||
|
||||
@ -93,6 +93,7 @@ static bool offlineMode = false;
|
||||
static AgFirmwareMode fwMode = FW_MODE_I_9PSL;
|
||||
|
||||
static bool ledBarButtonTest = false;
|
||||
static String fwNewVersion;
|
||||
|
||||
static void boardInit(void);
|
||||
static void failedHandler(String msg);
|
||||
@ -112,6 +113,7 @@ static void factoryConfigReset(void);
|
||||
static void wdgFeedUpdate(void);
|
||||
static void ledBarEnabledUpdate(void);
|
||||
static bool sgp41Init(void);
|
||||
static void otaHandlerCallback(StateMachine::OtaState state, String mesasge);
|
||||
|
||||
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, oledDisplayLedBarSchedule);
|
||||
AgSchedule configSchedule(SERVER_CONFIG_UPDATE_INTERVAL,
|
||||
@ -157,7 +159,6 @@ void setup() {
|
||||
apiClient.setAirGradient(ag);
|
||||
openMetrics.setAirGradient(ag);
|
||||
localServer.setAirGraident(ag);
|
||||
otaHandler.setAirGradient(ag);
|
||||
|
||||
/** Connecting wifi */
|
||||
bool connectToWifi = false;
|
||||
@ -434,6 +435,25 @@ static bool sgp41Init(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static void otaHandlerCallback(StateMachine::OtaState state, String mesasge) {
|
||||
switch (state) {
|
||||
case StateMachine::OtaState::OTA_STATE_BEGIN:
|
||||
stateMachine.executeOTA(state, fwNewVersion, 0);
|
||||
break;
|
||||
case StateMachine::OtaState::OTA_STATE_FAIL:
|
||||
stateMachine.executeOTA(state, "", 0);
|
||||
break;
|
||||
case StateMachine::OtaState::OTA_STATE_PROCESSING:
|
||||
stateMachine.executeOTA(state, "", mesasge.toInt());
|
||||
break;
|
||||
case StateMachine::OtaState::OTA_STATE_SUCCESS:
|
||||
stateMachine.executeOTA(state, "", mesasge.toInt());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void sendDataToAg() {
|
||||
/** Change oledDisplay and led state */
|
||||
if (ag->isOne()) {
|
||||
@ -734,9 +754,18 @@ static void configUpdateHandle() {
|
||||
String(configuration.getDisplayBrightness()));
|
||||
}
|
||||
|
||||
String newVer = configuration.newFirmwareVersion();
|
||||
if (newVer.length()) {
|
||||
otaHandler.updateFirmwareIfOutdated(newVer);
|
||||
fwNewVersion = configuration.newFirmwareVersion();
|
||||
if (fwNewVersion.length()) {
|
||||
int lastOta = configuration.getLastOta();
|
||||
if (lastOta != 0 && lastOta < (60 * 60 * 24)) {
|
||||
Serial.println("Ignore OTA cause last update is " + String(lastOta) +
|
||||
String("sec"));
|
||||
Serial.println("Retry again after 24h");
|
||||
} else {
|
||||
configuration.updateLastOta();
|
||||
otaHandler.setHandlerCallback(otaHandlerCallback);
|
||||
otaHandler.updateFirmwareIfOutdated(ag->deviceId());
|
||||
}
|
||||
}
|
||||
|
||||
appDispHandler();
|
||||
|
@ -1,13 +1,13 @@
|
||||
#ifndef _OTA_HANDLER_H_
|
||||
#define _OTA_HANDLER_H_
|
||||
|
||||
#include "AgConfigure.h"
|
||||
#include "AgStateMachine.h"
|
||||
#include "AirGradient.h"
|
||||
#include <Arduino.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_http_client.h>
|
||||
#include <esp_ota_ops.h>
|
||||
#include "AgConfigure.h"
|
||||
#include "AgStateMachine.h"
|
||||
#include "AirGradient.h"
|
||||
|
||||
#define OTA_BUF_SIZE 1024
|
||||
#define URL_BUF_SIZE 256
|
||||
@ -19,25 +19,14 @@ enum OtaUpdateOutcome {
|
||||
UDPATE_SKIPPED
|
||||
};
|
||||
|
||||
typedef void(*OtaHandlerCallback_t)(StateMachine::OtaState state,
|
||||
String message);
|
||||
|
||||
class OtaHandler {
|
||||
public:
|
||||
OtaHandler(StateMachine &sm, Configuration &config)
|
||||
: sm(sm), config(config) {}
|
||||
void setAirGradient(AirGradient *ag) { this->ag = ag; };
|
||||
|
||||
void updateFirmwareIfOutdated(String newVersion) {
|
||||
int lastOta = config.getLastOta();
|
||||
// Retry OTA after last udpate 24h
|
||||
if (lastOta != 0 && lastOta < (60 * 60 * 24)) {
|
||||
Serial.println("Ignore OTA cause last update is " + String(lastOta) +
|
||||
String("sec"));
|
||||
Serial.println("Retry again after 24h");
|
||||
return;
|
||||
}
|
||||
|
||||
String url =
|
||||
"http://hw.airgradient.com/sensors/airgradient:" + ag->deviceId() +
|
||||
"/generic/os/firmware.bin";
|
||||
void updateFirmwareIfOutdated(String deviceId) {
|
||||
String url = "http://hw.airgradient.com/sensors/airgradient:" + deviceId +
|
||||
"/generic/os/firmware.bin";
|
||||
url += "?current_firmware=";
|
||||
url += GIT_VERSION;
|
||||
char urlAsChar[URL_BUF_SIZE];
|
||||
@ -46,31 +35,30 @@ public:
|
||||
|
||||
esp_http_client_config_t config = {};
|
||||
config.url = urlAsChar;
|
||||
OtaUpdateOutcome ret = attemptToPerformOta(&config, newVersion);
|
||||
|
||||
// Update last OTA time whatever result.
|
||||
this->config.updateLastOta();
|
||||
|
||||
OtaUpdateOutcome ret = attemptToPerformOta(&config);
|
||||
Serial.println(ret);
|
||||
if (ret == OtaUpdateOutcome::UPDATE_PERFORMED) {
|
||||
Serial.println("OTA update performed, restarting ...");
|
||||
int i = 6;
|
||||
while (i != 0) {
|
||||
i = i - 1;
|
||||
sm.executeOTA(StateMachine::OtaState::OTA_STATE_SUCCESS, "", i);
|
||||
if (this->callback) {
|
||||
this->callback(StateMachine::OtaState::OTA_STATE_SUCCESS, String(i));
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
esp_restart();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
AirGradient *ag;
|
||||
StateMachine &sm;
|
||||
Configuration &config;
|
||||
void setHandlerCallback(OtaHandlerCallback_t callback) {
|
||||
this->callback = callback;
|
||||
}
|
||||
|
||||
OtaUpdateOutcome attemptToPerformOta(const esp_http_client_config_t *config,
|
||||
String newVersion) {
|
||||
private:
|
||||
OtaHandlerCallback_t callback;
|
||||
|
||||
OtaUpdateOutcome attemptToPerformOta(const esp_http_client_config_t *config) {
|
||||
esp_http_client_handle_t client = esp_http_client_init(config);
|
||||
if (client == NULL) {
|
||||
Serial.println("Failed to initialize HTTP connection");
|
||||
@ -129,7 +117,9 @@ private:
|
||||
Serial.println("File size: " + String(totalSize) + String(" bytes"));
|
||||
|
||||
// Show display start update new firmware.
|
||||
sm.executeOTA(StateMachine::OtaState::OTA_STATE_BEGIN, newVersion, 0);
|
||||
if (this->callback) {
|
||||
this->callback(StateMachine::OtaState::OTA_STATE_BEGIN, "");
|
||||
}
|
||||
|
||||
// Download file and write new firmware to OTA partition
|
||||
uint32_t lastUpdate = millis();
|
||||
@ -142,14 +132,18 @@ private:
|
||||
}
|
||||
if (data_read < 0) {
|
||||
Serial.println("Data read error");
|
||||
sm.executeOTA(StateMachine::OtaState::OTA_STATE_FAIL, "", 0);
|
||||
if (this->callback) {
|
||||
this->callback(StateMachine::OtaState::OTA_STATE_FAIL, "");
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (data_read > 0) {
|
||||
ota_write_err = esp_ota_write(
|
||||
update_handle, (const void *)upgrade_data_buf, data_read);
|
||||
if (ota_write_err != ESP_OK) {
|
||||
sm.executeOTA(StateMachine::OtaState::OTA_STATE_FAIL, "", 0);
|
||||
if (this->callback) {
|
||||
this->callback(StateMachine::OtaState::OTA_STATE_FAIL, "");
|
||||
}
|
||||
break;
|
||||
}
|
||||
binary_file_len += data_read;
|
||||
@ -157,8 +151,12 @@ private:
|
||||
int percent = (binary_file_len * 100) / totalSize;
|
||||
uint32_t ms = (uint32_t)(millis() - lastUpdate);
|
||||
if (ms >= 250) {
|
||||
sm.executeOTA(StateMachine::OtaState::OTA_STATE_PROCESSING, "",
|
||||
percent);
|
||||
// sm.executeOTA(StateMachine::OtaState::OTA_STATE_PROCESSING, "",
|
||||
// percent);
|
||||
if (this->callback) {
|
||||
this->callback(StateMachine::OtaState::OTA_STATE_PROCESSING,
|
||||
String(percent));
|
||||
}
|
||||
lastUpdate = millis();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user