Submodule components/espasynchttpreq updated: 719d1854ef...50487cf564
@ -8,16 +8,34 @@
|
||||
|
||||
using namespace espgui;
|
||||
|
||||
SwitchQrDisplayAction::SwitchQrDisplayAction(qraction::QrMenu qrmenu) : m_msg{qrmenu.message}, m_ver{qrmenu.ver} {}
|
||||
SwitchQrDisplayAction::SwitchQrDisplayAction(const qraction::QrMenu &qrmenu) :
|
||||
m_msg{qrmenu.message},
|
||||
m_ver{qrmenu.ver}
|
||||
{
|
||||
}
|
||||
|
||||
SwitchQrDisplayAction::SwitchQrDisplayAction(qraction::QrMenu &&qrmenu) :
|
||||
m_msg{std::move(qrmenu.message)},
|
||||
m_ver{qrmenu.ver}
|
||||
{
|
||||
}
|
||||
|
||||
void SwitchQrDisplayAction::triggered()
|
||||
{
|
||||
switchScreen<QrDisplay>(m_msg, m_ver);
|
||||
}
|
||||
|
||||
SwitchQrImportDisplayAction::SwitchQrImportDisplayAction(std::string nvskey) : m_nvskey{nvskey} {}
|
||||
SwitchQrImportDisplayAction::SwitchQrImportDisplayAction(const std::string &nvskey) :
|
||||
m_nvskey{nvskey}
|
||||
{
|
||||
}
|
||||
|
||||
SwitchQrImportDisplayAction::SwitchQrImportDisplayAction(std::string &&nvskey) :
|
||||
m_nvskey{std::move(nvskey)}
|
||||
{
|
||||
}
|
||||
|
||||
void SwitchQrImportDisplayAction::triggered()
|
||||
{
|
||||
switchScreen<QrImportDisplay>(m_nvskey);
|
||||
switchScreen<QrImportDisplay>(std::move(m_nvskey));
|
||||
}
|
||||
|
@ -16,7 +16,8 @@ struct QrMenu {
|
||||
class SwitchQrDisplayAction : public virtual espgui::ActionInterface
|
||||
{
|
||||
public:
|
||||
SwitchQrDisplayAction(qraction::QrMenu qrmenu);
|
||||
explicit SwitchQrDisplayAction(const qraction::QrMenu &qrmenu);
|
||||
explicit SwitchQrDisplayAction(qraction::QrMenu &&qrmenu);
|
||||
|
||||
void triggered() override;
|
||||
private:
|
||||
@ -27,7 +28,8 @@ private:
|
||||
class SwitchQrImportDisplayAction : public virtual espgui::ActionInterface
|
||||
{
|
||||
public:
|
||||
SwitchQrImportDisplayAction(std::string nvskey);
|
||||
explicit SwitchQrImportDisplayAction(const std::string &nvskey);
|
||||
explicit SwitchQrImportDisplayAction(std::string &&nvskey);
|
||||
|
||||
void triggered() override;
|
||||
private:
|
||||
|
@ -68,7 +68,7 @@ GreenPassMenu::GreenPassMenu()
|
||||
|
||||
for (uint8_t index = 0; index < 4; index++)
|
||||
{
|
||||
const std::string nvs_key = fmt::format("covidcert-{}", index);
|
||||
std::string nvs_key = fmt::format("covidcert-{}", index);
|
||||
ESP_LOGI("greenpassmenu", "Checking key %s", nvs_key.c_str());
|
||||
if (qrimport::has_qr_code(nvs_key))
|
||||
{
|
||||
@ -84,7 +84,7 @@ GreenPassMenu::GreenPassMenu()
|
||||
}
|
||||
else
|
||||
{
|
||||
constructMenuItem<makeComponentArgs<MenuItem, SwitchQrImportDisplayAction, StaticText<TEXT_ADDCERT>>>(nvs_key);
|
||||
constructMenuItem<makeComponentArgs<MenuItem, SwitchQrImportDisplayAction, StaticText<TEXT_ADDCERT>>>(std::move(nvs_key));
|
||||
}
|
||||
}
|
||||
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_DELCERT>, BobbyCheckbox, DeleteModeAccessor>>();
|
||||
|
@ -12,62 +12,79 @@ constexpr const char * const TAG = "qrimport";
|
||||
// local includes
|
||||
#include "qrimport.h"
|
||||
|
||||
// m_statuslabel needs redraw
|
||||
|
||||
using namespace espgui;
|
||||
|
||||
QrImportDisplay::QrImportDisplay(std::string nvs_key) :
|
||||
QrImportDisplay::QrImportDisplay(const std::string &nvs_key) :
|
||||
m_nvs_key{nvs_key}
|
||||
{
|
||||
}
|
||||
|
||||
QrImportDisplay::QrImportDisplay(std::string &&nvs_key) :
|
||||
m_nvs_key{std::move(nvs_key)}
|
||||
{
|
||||
}
|
||||
|
||||
void QrImportDisplay::start()
|
||||
{
|
||||
Base::start();
|
||||
|
||||
m_statuslabel.start();
|
||||
|
||||
qrimport::setup_request();
|
||||
m_statuslabel.redraw(fmt::format("Request not running."));
|
||||
|
||||
if (const auto result = qrimport::start_qr_request(); result)
|
||||
{
|
||||
ESP_LOGI(TAG, "started request, waiting for result");
|
||||
m_waitingForResult = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "could not start request: %.*s", result.error().size(), result.error().data());
|
||||
m_result = tl::make_unexpected(std::move(result).error());
|
||||
}
|
||||
}
|
||||
|
||||
void QrImportDisplay::update()
|
||||
{
|
||||
Base::update();
|
||||
|
||||
m_expected = qrimport::check_request();
|
||||
if (m_expected)
|
||||
if (!m_waitingForResult)
|
||||
return;
|
||||
|
||||
if (qrimport::get_request_running())
|
||||
return;
|
||||
|
||||
m_waitingForResult = false;
|
||||
|
||||
m_result = qrimport::check_request();
|
||||
if (m_result)
|
||||
{
|
||||
ESP_LOGI(TAG, "%s", fmt::format("{} => {}", m_nvs_key, *m_expected).c_str());
|
||||
if (const auto result = qrimport::set_qr_code(m_nvs_key, *m_expected); !result)
|
||||
{
|
||||
tft.setTextColor(TFT_RED);
|
||||
m_statuslabel.redraw(esp_err_to_name(result.error()));
|
||||
tft.setTextColor(TFT_WHITE);
|
||||
m_confirmLocked = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
switchScreen<GreenPassMenu>();
|
||||
}
|
||||
ESP_LOGI(TAG, "%.*s => %.*s", m_nvs_key.size(), m_nvs_key.data(), m_result->size(), m_result->data());
|
||||
if (const auto result = qrimport::set_qr_code(m_nvs_key, *m_result); !result)
|
||||
m_result = tl::make_unexpected(fmt::format("saving qr failed: {}", esp_err_to_name(result.error())));
|
||||
}
|
||||
else
|
||||
ESP_LOGW(TAG, "failed %.*s => %.*s", m_nvs_key.size(), m_nvs_key.data(), m_result.error().size(), m_result.error().data());
|
||||
}
|
||||
|
||||
void QrImportDisplay::redraw()
|
||||
{
|
||||
Base::redraw();
|
||||
|
||||
if (qrimport::get_request_running())
|
||||
if (m_waitingForResult)
|
||||
{
|
||||
if (!m_expected)
|
||||
{
|
||||
tft.setTextColor(TFT_RED);
|
||||
m_statuslabel.redraw(*m_expected);
|
||||
tft.setTextColor(TFT_WHITE);
|
||||
}
|
||||
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
|
||||
m_statuslabel.redraw("In progress");
|
||||
}
|
||||
else if (!m_result)
|
||||
{
|
||||
tft.setTextColor(TFT_RED, TFT_BLACK);
|
||||
m_statuslabel.redraw(m_result.error());
|
||||
}
|
||||
else
|
||||
{
|
||||
m_statuslabel.redraw("Request not running");
|
||||
tft.setTextColor(TFT_GREEN, TFT_BLACK);
|
||||
m_statuslabel.redraw("OK");
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,22 +96,10 @@ void QrImportDisplay::buttonPressed(espgui::Button button)
|
||||
{
|
||||
using espgui::Button;
|
||||
case Button::Left:
|
||||
if (!qrimport::get_request_running())
|
||||
{
|
||||
if (!m_waitingForResult)
|
||||
switchScreen<GreenPassMenu>();
|
||||
}
|
||||
break;
|
||||
case Button::Right:
|
||||
// start request
|
||||
if (!m_confirmLocked)
|
||||
{
|
||||
if (const auto result = qrimport::start_qr_request(); !result)
|
||||
{
|
||||
switchScreen<GreenPassMenu>();
|
||||
}
|
||||
else
|
||||
m_confirmLocked = true;
|
||||
}
|
||||
else
|
||||
ESP_LOGW(TAG, "tried to leave while waiting for result");
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
|
@ -13,7 +13,8 @@ class QrImportDisplay : public BobbyDisplay
|
||||
using Base = BobbyDisplay;
|
||||
|
||||
public:
|
||||
QrImportDisplay(std::string nvs_key);
|
||||
explicit QrImportDisplay(const std::string &nvs_key);
|
||||
explicit QrImportDisplay(std::string &&nvs_key);
|
||||
|
||||
void start() override;
|
||||
void update() override;
|
||||
@ -22,8 +23,9 @@ public:
|
||||
void buttonPressed(espgui::Button button) override;
|
||||
|
||||
private:
|
||||
bool m_confirmLocked{false};
|
||||
bool m_waitingForResult{false};
|
||||
espgui::Label m_statuslabel{5,(espgui::tft.height() / 2)-espgui::tft.fontHeight(4)};
|
||||
tl::expected<std::string, std::string> m_expected;
|
||||
|
||||
tl::expected<std::string, std::string> m_result;
|
||||
std::string m_nvs_key;
|
||||
};
|
||||
|
@ -135,6 +135,12 @@ tl::expected<std::string, std::string> check_request()
|
||||
{
|
||||
return tl::make_unexpected(result.error());
|
||||
}
|
||||
else if (http_request->statusCode() != 200)
|
||||
{
|
||||
return tl::make_unexpected(fmt::format("unexpected response status: {} {}",
|
||||
http_request->statusCode(),
|
||||
http_request->takeBuffer()));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "%.*s", http_request->buffer().size(), http_request->buffer().data());
|
||||
@ -146,9 +152,10 @@ bool get_request_running()
|
||||
{
|
||||
if (!http_request.constructed())
|
||||
{
|
||||
ESP_LOGW(TAG, "not constructed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return http_request->finished();
|
||||
return !http_request->finished();
|
||||
}
|
||||
} // namespace
|
||||
|
Reference in New Issue
Block a user