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