move ota headers to ota.cc

This commit is contained in:
Terrence
2025-04-10 18:55:49 +08:00
parent f1ad29be3b
commit a5dfe67504
3 changed files with 17 additions and 23 deletions

View File

@@ -15,7 +15,6 @@
#include <cJSON.h>
#include <driver/gpio.h>
#include <arpa/inet.h>
#include <esp_app_desc.h>
#define TAG "Application"
@@ -63,11 +62,6 @@ Application::~Application() {
}
void Application::CheckNewVersion() {
auto& board = Board::GetInstance();
auto display = board.GetDisplay();
// Check if there is a new firmware version available
ota_.SetPostData(board.GetJson());
const int MAX_RETRY = 10;
int retry_count = 0;
@@ -450,13 +444,6 @@ void Application::Start() {
protocol_->Start();
// Check for new firmware version or get the MQTT broker address
ota_.SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
ota_.SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
ota_.SetHeader("Client-Id", board.GetUuid());
ota_.SetHeader("Accept-Language", Lang::CODE);
auto app_desc = esp_app_get_description();
ota_.SetHeader("User-Agent", std::string(BOARD_NAME "/") + app_desc->version);
xTaskCreate([](void* arg) {
Application* app = (Application*)arg;
app->CheckNewVersion();

View File

@@ -2,6 +2,7 @@
#include "system_info.h"
#include "board.h"
#include "settings.h"
#include "assets/lang_config.h"
#include <cJSON.h>
#include <esp_log.h>
@@ -18,6 +19,7 @@
Ota::Ota() {
SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
}
Ota::~Ota() {
@@ -31,12 +33,12 @@ void Ota::SetHeader(const std::string& key, const std::string& value) {
headers_[key] = value;
}
void Ota::SetPostData(const std::string& post_data) {
post_data_ = post_data;
}
bool Ota::CheckVersion() {
current_version_ = esp_app_get_description()->version;
auto& board = Board::GetInstance();
auto app_desc = esp_app_get_description();
// Check if there is a new firmware version available
current_version_ = app_desc->version;
ESP_LOGI(TAG, "Current version: %s", current_version_.c_str());
if (check_version_url_.length() < 10) {
@@ -44,14 +46,21 @@ bool Ota::CheckVersion() {
return false;
}
auto http = Board::GetInstance().CreateHttp();
auto http = board.CreateHttp();
for (const auto& header : headers_) {
http->SetHeader(header.first, header.second);
}
http->SetHeader("Ota-Version", "2");
http->SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
http->SetHeader("Client-Id", board.GetUuid());
http->SetHeader("User-Agent", std::string(BOARD_NAME "/") + app_desc->version);
http->SetHeader("Accept-Language", Lang::CODE);
http->SetHeader("Content-Type", "application/json");
std::string method = post_data_.length() > 0 ? "POST" : "GET";
if (!http->Open(method, check_version_url_, post_data_)) {
std::string post_data = board.GetJson();
std::string method = post_data.length() > 0 ? "POST" : "GET";
if (!http->Open(method, check_version_url_, post_data)) {
ESP_LOGE(TAG, "Failed to open HTTP connection");
delete http;
return false;

View File

@@ -12,7 +12,6 @@ public:
void SetCheckVersionUrl(std::string check_version_url);
void SetHeader(const std::string& key, const std::string& value);
void SetPostData(const std::string& post_data);
bool CheckVersion();
bool HasNewVersion() { return has_new_version_; }
bool HasMqttConfig() { return has_mqtt_config_; }
@@ -37,7 +36,6 @@ private:
std::string current_version_;
std::string firmware_version_;
std::string firmware_url_;
std::string post_data_;
std::map<std::string, std::string> headers_;
void Upgrade(const std::string& firmware_url);