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