Improvement of Printer status polling.

Define polling action that changes when uuid->printer_model map is filled. Also serves to turn off polling.
This commit is contained in:
David Kocik 2024-04-24 11:16:12 +02:00
parent fd8a580dee
commit 5e171ccfe7
5 changed files with 21 additions and 17 deletions

View File

@ -152,10 +152,6 @@ void UserAccount::on_communication_fail()
m_communication->enqueue_test_connection();
m_fail_counter = 0;
}
// Printer models are called only after login, if it fails, it should repeat
if (m_printer_uuid_map.empty()) {
enqueue_connect_printer_models_action();
}
}
namespace {
@ -299,6 +295,7 @@ bool UserAccount::on_connect_uiid_map_success(const std::string& data, AppConfig
}
m_printer_uuid_map[*printer_uuid] = *printer_model;
}
m_communication->on_uuid_map_success();
return on_connect_printers_success(data, app_config, out_printers_changed);
}

View File

@ -214,7 +214,15 @@ void UserAccountCommunication::set_polling_enabled(bool enabled)
{
{
std::lock_guard<std::mutex> lock(m_session_mutex);
return m_session->set_polling_enabled(enabled);
return m_session->set_polling_action(enabled ? UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS : UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY);
}
}
void UserAccountCommunication::on_uuid_map_success()
{
{
std::lock_guard<std::mutex> lock(m_session_mutex);
return m_session->set_polling_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS);
}
}

View File

@ -60,7 +60,8 @@ public:
std::string get_shared_session_key();
void set_polling_enabled(bool enabled);
// we have map of uuids and printer_models - set polling action to lightweight STATUS action
void on_uuid_map_success();
private:
std::unique_ptr<UserAccountSession> m_session;
std::thread m_thread;

View File

@ -75,11 +75,7 @@ void UserAccountSession::process_action_queue()
return;
if (m_priority_action_queue.empty() && m_action_queue.empty()) {
// update printers periodically
if (m_polling_enabled) {
enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS, nullptr, nullptr, {});
} else {
return;
}
enqueue_action(m_polling_action, nullptr, nullptr, {});
}
// priority queue works even when tokens are empty or broken
while (!m_priority_action_queue.empty()) {

View File

@ -38,8 +38,8 @@ enum class UserAccountActionID {
USER_ACCOUNT_ACTION_USER_ID,
USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN,
USER_ACCOUNT_ACTION_TEST_CONNECTION,
USER_ACCOUNT_ACTION_CONNECT_STATUS, // status of all printers
USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS,
USER_ACCOUNT_ACTION_CONNECT_STATUS, // status of all printers by UUID
USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS, // status of all printers by UUID with printer_model. Should be called once to save printer models.
USER_ACCOUNT_ACTION_AVATAR,
};
class UserAction
@ -101,7 +101,7 @@ public:
, m_access_token(access_token)
, m_refresh_token(refresh_token)
, m_shared_session_key(shared_session_key)
, m_polling_enabled(polling_enabled)
, m_polling_action(polling_enabled ? UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS : UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY)
{
@ -146,7 +146,8 @@ public:
std::string get_refresh_token() const { return m_refresh_token; }
std::string get_shared_session_key() const { return m_shared_session_key; }
void set_polling_enabled(bool enabled) {m_polling_enabled = enabled; }
//void set_polling_enabled(bool enabled) {m_polling_action = enabled ? UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS : UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY; }
void set_polling_action(UserAccountActionID action) { m_polling_action = action; }
private:
void enqueue_refresh(const std::string& body);
@ -159,8 +160,9 @@ private:
// false prevents action queu to be processed - no communication is done
// sets to true by init_with_code or enqueue_action call
bool m_proccessing_enabled {false};
// triggers CONNECT_PRINTERS action when woken up on idle
bool m_polling_enabled;
// action when woken up on idle - switches between USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS and USER_ACCOUNT_ACTION_CONNECT_STATUS
// set to USER_ACCOUNT_ACTION_DUMMY to switch off polling
UserAccountActionID m_polling_action;
std::string m_access_token;
std::string m_refresh_token;