Added branch select
This commit is contained in:
@@ -18,7 +18,111 @@
|
||||
#include "esp_http_client.h"
|
||||
|
||||
#ifdef FEATURE_OTA
|
||||
|
||||
namespace buildserver {
|
||||
|
||||
uint16_t count_available_buildserver()
|
||||
{
|
||||
uint16_t count = 0;
|
||||
for (const auto &otaServer : stringSettings.otaServers) {
|
||||
if (!otaServer.url.empty()) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
namespace SelectBranch {
|
||||
cpputils::DelayedConstruction<AsyncHttpRequest> request;
|
||||
bool request_running{false};
|
||||
bool constructedMenu{false};
|
||||
std::string request_failed{};
|
||||
std::vector<std::string> branches{};
|
||||
|
||||
void setup_request()
|
||||
{
|
||||
if (!request.constructed())
|
||||
{
|
||||
request.construct("ota-descriptor-request", espcpputils::CoreAffinity::Core0);
|
||||
}
|
||||
}
|
||||
|
||||
void start_descriptor_request(std::string server_base_url)
|
||||
{
|
||||
if (!request.constructed())
|
||||
{
|
||||
ESP_LOGW("BOBBY", "request is im oarsch");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto url = fmt::format("{}/otaDescriptor?username={}&branches", server_base_url, OTA_USERNAME);
|
||||
ESP_LOGD("BOBBY", "requesting data...");
|
||||
if (const auto result = request->start(url); !result)
|
||||
{
|
||||
ESP_LOGW("BOBBY", "request start failed");
|
||||
return;
|
||||
}
|
||||
request_running = true;
|
||||
constructedMenu = false;
|
||||
}
|
||||
|
||||
|
||||
void check_descriptor_request()
|
||||
{
|
||||
if (!request.constructed())
|
||||
{
|
||||
ESP_LOGW("BOBBY", "request is im oarsch");
|
||||
request_running = false;
|
||||
request_failed = "request is im oarsch";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!request->finished())
|
||||
{
|
||||
// ESP_LOGW("BOBBY", "Request has not finished yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto helper = cpputils::makeCleanupHelper([](){ request->clearFinished(); });
|
||||
const std::string content = std::move(request->takeBuffer());
|
||||
|
||||
if (const auto result = request->result(); !result)
|
||||
{
|
||||
ESP_LOGW("BOBBY", "request failed: %.*s", result.error().size(), result.error().data());
|
||||
request_failed = result.error();
|
||||
return;
|
||||
}
|
||||
|
||||
const auto result = request->result();
|
||||
ESP_LOGW("BOBBY", "Request finished: %s", content.c_str());
|
||||
parse_response(content);
|
||||
request_running = false;
|
||||
request_failed = {};
|
||||
}
|
||||
|
||||
void parse_response(std::string response)
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
|
||||
if (const auto error = deserializeJson(doc, response))
|
||||
{
|
||||
ESP_LOGE("BOBBY", "Error parsing server-response => %s (%s)", error.c_str(), response.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
branches.resize(arr.size());
|
||||
|
||||
for(JsonVariant v : arr) {
|
||||
branches.push_back(v);
|
||||
}
|
||||
}
|
||||
|
||||
bool get_request_running()
|
||||
{
|
||||
return request_running;
|
||||
}
|
||||
} // namespace SelectBranch
|
||||
|
||||
namespace SelectBuild {
|
||||
void buildMenuFromJson(std::string json);
|
||||
void buildMenuRequestError(std::string error);
|
||||
|
||||
@@ -27,7 +131,7 @@ namespace buildserver {
|
||||
std::array<std::string, 10> availableVersions{};
|
||||
bool request_running{false};
|
||||
std::string request_failed{};
|
||||
bool parsing_finished{true};
|
||||
bool parsing_finished{false};
|
||||
cpputils::DelayedConstruction<AsyncHttpRequest> request;
|
||||
|
||||
std::string get_ota_url_from_index(uint16_t index)
|
||||
@@ -52,15 +156,6 @@ namespace buildserver {
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t count_available_buildserver()
|
||||
{
|
||||
uint16_t count = 0;
|
||||
for (const auto &otaServer : stringSettings.otaServers) {
|
||||
if (!otaServer.url.empty()) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
std::string get_hash_url(std::string hash)
|
||||
{
|
||||
return fmt::format(url_for_hashes, hash);
|
||||
@@ -73,7 +168,10 @@ namespace buildserver {
|
||||
|
||||
std::string get_descriptor_url(std::string base_url)
|
||||
{
|
||||
return fmt::format("{}/otaDescriptor?username={}", base_url, OTA_USERNAME);
|
||||
if (stringSettings.otaServerBranch.empty())
|
||||
return fmt::format("{}/otaDescriptor?username={}", base_url, OTA_USERNAME);
|
||||
else
|
||||
return fmt::format("{}/otaDescriptor?username={}&branch={}", base_url, OTA_USERNAME, stringSettings.otaServerBranch);
|
||||
}
|
||||
|
||||
void parse_response_into_variables(std::string response)
|
||||
@@ -172,5 +270,6 @@ namespace buildserver {
|
||||
{
|
||||
return request_running;
|
||||
}
|
||||
}
|
||||
} // namespace SelectBuild
|
||||
} // namespace buildserver
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user