Added qr import display

This commit is contained in:
CommanderRedYT
2021-12-18 20:09:25 +01:00
parent 80d141a109
commit a9798713ed
8 changed files with 219 additions and 0 deletions

View File

@ -123,6 +123,7 @@ set(headers
displays/poweroffdisplay.h
displays/powersupplydisplay.h
displays/qrcodedebug.h
displays/qrimportdisplay.h
displays/spirodisplay.h
displays/starfielddisplay.h
displays/statusdisplay.h
@ -179,6 +180,7 @@ set(headers
newsettings.h
ota.h
presets.h
qrimport.h
rotary.h
screens.h
serialhandler.h
@ -333,6 +335,7 @@ set(sources
displays/poweroffdisplay.cpp
displays/powersupplydisplay.cpp
displays/qrcodedebug.cpp
displays/qrimportdisplay.cpp
displays/spirodisplay.cpp
displays/starfielddisplay.cpp
displays/statusdisplay.cpp
@ -390,6 +393,7 @@ set(sources
newsettings.cpp
ota.cpp
presets.cpp
qrimport.cpp
rotary.cpp
screens.cpp
serialhandler.cpp

View File

@ -5,6 +5,7 @@
#include "actions/qraction.h"
#include "displays/menus/mainmenu.h"
#include "displays/qrdisplay.h"
#include "displays/qrimportdisplay.h"
#include "icons/back.h"
using namespace espgui;
@ -12,6 +13,7 @@ using namespace espgui;
GreenPassMenu::GreenPassMenu()
{
constructMenuItem<makeComponentArgs<MenuItem, SwitchQrDisplayAction, StaticText<TEXT_SHOWCERT>>>(qraction::QrMenu{.message="CORONA_PASS", .ver=15});
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ADDCERT>, SwitchScreenAction<QrImportDisplay>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<MainMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}

View File

@ -0,0 +1,76 @@
constexpr const char * const TAG = "qrimport";
#include "qrimportdisplay.h"
// 3rd party includes
#include <screenmanager.h>
#include <fmt/format.h>
#include <esp_log.h>
// displays/menus includes
#include "displays/menus/greenpassmenu.h"
// local includes
#include "qrimport.h"
// m_statuslabel needs redraw
using namespace espgui;
/*
* This display will be accessable via GreenPassMenu. If you click on it, it will ask if you want to download the qr code.
* If confirm is pressed, the display will lock the back action and send a request via qrimport.h. If an error occours, it will be displayed.
* If everything is okay, you will be redirected to GreenPassMenu.
* The update function will need to be overriden to check the web handler if it exists.
*/
QrImportDisplay::QrImportDisplay()
{
// constructor
}
void QrImportDisplay::start()
{
m_statuslabel.start();
qrimport::setup_request();
}
void QrImportDisplay::back()
{
if (!m_backLocked)
switchScreen<GreenPassMenu>();
}
void QrImportDisplay::confirm()
{
// start request
m_backLocked = true;
qrimport::start_qr_request();
}
void QrImportDisplay::update()
{
m_expected = qrimport::check_request();
if (m_expected)
{
ESP_LOGI(TAG, "%s", m_expected->c_str());
m_backLocked = false;
switchScreen<GreenPassMenu>();
}
}
void QrImportDisplay::redraw()
{
if (qrimport::get_request_running())
{
if (!m_expected)
{
tft.setTextColor(TFT_RED);
m_statuslabel.redraw(*m_expected);
tft.setTextColor(TFT_WHITE);
}
}
else
{
m_statuslabel.redraw(fmt::format("Request not running."));
}
}

View File

@ -0,0 +1,23 @@
#pragma once
// 3rd party includes
#include <display.h>
#include <widgets/label.h>
#include <tftinstance.h>
#include <tl/expected.hpp>
class QrImportDisplay : public espgui::Display
{
using Base = espgui::Display;
public:
QrImportDisplay();
void start() override;
void back() override;
void update() override;
void redraw() override;
void confirm() override;
private:
bool m_backLocked;
espgui::Label m_statuslabel{5,(espgui::tft.height() / 2)-espgui::tft.fontHeight(4)};
tl::expected<std::string, std::string> m_expected;
};

86
main/qrimport.cpp Normal file
View File

@ -0,0 +1,86 @@
#include "qrimport.h"
// system includes
// 3rd party includes
#include <asynchttprequest.h>
#include <cleanuphelper.h>
#include <cpputils.h>
#include <delayedconstruction.h>
// local includes
namespace qrimport {
namespace {
cpputils::DelayedConstruction<AsyncHttpRequest> http_request;
} // namespace
// nvs
tl::expected<std::string, std::string> get_qr_code(std::string_view key)
{
return "";
}
tl::expected<void, std::string> set_qr_code(std::string_view qrcode, std::string_view key)
{
return{};
}
// web request
void setup_request()
{
if (!http_request.constructed())
{
http_request.construct("qr_request", espcpputils::CoreAffinity::Core0);
}
}
tl::expected<void, std::string> start_qr_request()
{
if (!http_request.constructed())
{
return tl::make_unexpected("request im oarsch");
}
if (const auto res = http_request->start(fmt::format("http://qr.bobbycar.cloud/{}.qr", OTA_USERNAME)); !res)
{
return res;
}
return{};
}
tl::expected<std::string, std::string> check_request()
{
if (!http_request.constructed())
{
return tl::make_unexpected("request im oarsch");
}
if (!http_request->finished())
{
return tl::make_unexpected("request has not finished");
}
const auto helper = cpputils::makeCleanupHelper([](){ http_request->clearFinished(); });
if (const auto result = http_request->result(); !result)
{
return tl::make_unexpected(result.error());
}
else
{
return http_request->takeBuffer();
}
}
bool get_request_running()
{
if (!http_request.constructed())
{
return false;
}
return http_request->finished();
}
} // namespace

26
main/qrimport.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
/*
* In this file there will be
* - a web-handler to get qr as plain text from http://qr.bobbycar.cloud/files/[OTA_NAME].qr that can be later triggered via in qrimportdisplay.h
* - getter and setter that use NVS
*/
// system includes
#include <string>
// 3rd party includes
#include <tl/expected.hpp>
// local includes
namespace qrimport {
// nvs
tl::expected<std::string, std::string> get_qr_code(std::string_view key);
tl::expected<void, std::string> set_qr_code(std::string_view qrcode, std::string_view key);
// web request
void setup_request();
tl::expected<void, std::string> start_qr_request();
tl::expected<std::string, std::string> check_request();
bool get_request_running();
} // namespace

View File

@ -544,6 +544,7 @@ char TEXT_QRCODE_DEBUG[] = "QR Debug";
//GreenPassMenu
char TEXT_GREENPASS[] = "Green Pass";
char TEXT_SHOWCERT[] = "Show cert";
char TEXT_ADDCERT[] = "Add cert";
//EspNowMenu
char TEXT_ESPNOW[] = "ESP-Now";

View File

@ -544,6 +544,7 @@ extern char TEXT_QRCODE_DEBUG[];
//GreenPassMenu
extern char TEXT_GREENPASS[];
extern char TEXT_SHOWCERT[];
extern char TEXT_ADDCERT[];
//EspNowMenu
extern char TEXT_ESPNOW[];