From 7ad9431e3a2d80ec3e807c65e1ec0934a372ea08 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Tue, 12 Mar 2024 13:08:26 +0100 Subject: [PATCH] New connect endpoint slicer/status replacing slicer/printers move enum value to if 0 --- src/slic3r/GUI/UserAccount.cpp | 57 +++++++++++++-------- src/slic3r/GUI/UserAccountCommunication.cpp | 2 +- src/slic3r/GUI/UserAccountSession.cpp | 2 +- src/slic3r/GUI/UserAccountSession.hpp | 10 ++-- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/slic3r/GUI/UserAccount.cpp b/src/slic3r/GUI/UserAccount.cpp index 47d72a2026..efc32f9510 100644 --- a/src/slic3r/GUI/UserAccount.cpp +++ b/src/slic3r/GUI/UserAccount.cpp @@ -192,41 +192,54 @@ bool UserAccount::on_connect_printers_success(const std::string& data, AppConfig return false; } // fill m_printer_map with data from ptree - // tree string is in format {"printers": [{..}, {..}]} + // tree string is in format {"result": [{"printer_type": "1.2.3", "states": [{"printer_state": "OFFLINE", "count": 1}, ...]}, {..}]} ConnectPrinterStateMap new_printer_map; - assert(ptree.front().first == "printers"); + assert(ptree.front().first == "result"); for (const auto& printer_tree : ptree.front().second) { + // printer_tree is {"printer_type": "1.2.3", "states": [..]} std::string name; ConnectPrinterState state; - std::string type_string = parse_tree_for_param(printer_tree.second, "printer_type"); - std::string state_string = parse_tree_for_param(printer_tree.second, "connect_state"); - assert(!type_string.empty()); - assert(!state_string.empty()); - if (type_string.empty() || state_string.empty()) + const auto type_opt = printer_tree.second.get_optional("printer_type"); + if (!type_opt) { continue; - // name of printer needs to be taken from translate table, if missing - if (auto pair = printer_type_and_name_table.find(type_string); pair != printer_type_and_name_table.end()) { + } + + if (auto pair = printer_type_and_name_table.find(*type_opt); pair != printer_type_and_name_table.end()) { name = pair->second; - } else { + } + else { assert(true); // On this assert, printer_type_and_name_table needs to be updated with type_string and correct printer name continue; } - // translate state string to enum value - if (auto pair = printer_state_table.find(state_string); pair != printer_state_table.end()) { - state = pair->second; - } else { - assert(true); // On this assert, printer_state_table and ConnectPrinterState needs to be updated - continue; + // printer should not appear twice + assert(new_printer_map.find(name) == new_printer_map.end()); + // prepare all states on 0 + new_printer_map[name].reserve(static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT)); + for (size_t i = 0; i < static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT); i++) { + new_printer_map[name].push_back(0); } - if (auto counter = new_printer_map.find(name); counter != new_printer_map.end()) { - new_printer_map[name][static_cast(state)]++; - } else { - new_printer_map[name].reserve(static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT)); - for (size_t i = 0; i < static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT); i++) { - new_printer_map[name].push_back(i == static_cast(state) ? 1 : 0); + + for (const auto& section : printer_tree.second) { + // section is "printer_type": "1.2.3" OR "states": [..]} + if (section.first == "states") { + for (const auto& subsection : section.second) { + // subsection is {"printer_state": "OFFLINE", "count": 1} + const auto state_opt = subsection.second.get_optional("printer_state"); + const auto count_opt = subsection.second.get_optional("count"); + if (!state_opt || ! count_opt) { + continue; + } + if (auto pair = printer_state_table.find(*state_opt); pair != printer_state_table.end()) { + state = pair->second; + } else { + assert(true); // On this assert, printer_state_table needs to be updated with *state_opt and correct ConnectPrinterState + continue; + } + new_printer_map[name][static_cast(state)] = *count_opt; + } } } } diff --git a/src/slic3r/GUI/UserAccountCommunication.cpp b/src/slic3r/GUI/UserAccountCommunication.cpp index 85a628eeb8..56d234f9b4 100644 --- a/src/slic3r/GUI/UserAccountCommunication.cpp +++ b/src/slic3r/GUI/UserAccountCommunication.cpp @@ -320,7 +320,7 @@ void UserAccountCommunication::enqueue_connect_printers_action() BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in."; return; } - m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS, nullptr, nullptr, {}); + m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS, nullptr, nullptr, {}); } wakeup_session_thread(); } diff --git a/src/slic3r/GUI/UserAccountSession.cpp b/src/slic3r/GUI/UserAccountSession.cpp index 1d5563bad2..756f388a13 100644 --- a/src/slic3r/GUI/UserAccountSession.cpp +++ b/src/slic3r/GUI/UserAccountSession.cpp @@ -78,7 +78,7 @@ void UserAccountSession::process_action_queue() if (m_priority_action_queue.empty() && m_action_queue.empty()) { // update printers periodically if (m_polling_enabled) { - enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS, nullptr, nullptr, {}); + enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS, nullptr, nullptr, {}); } else { return; } diff --git a/src/slic3r/GUI/UserAccountSession.hpp b/src/slic3r/GUI/UserAccountSession.hpp index 3336f6a618..2a419ebd40 100644 --- a/src/slic3r/GUI/UserAccountSession.hpp +++ b/src/slic3r/GUI/UserAccountSession.hpp @@ -40,9 +40,10 @@ enum class UserAccountActionID { USER_ACCOUNT_ACTION_USER_ID, USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN, USER_ACCOUNT_ACTION_TEST_CONNECTION, - USER_ACCOUNT_ACTION_CONNECT_PRINTERS, + USER_ACCOUNT_ACTION_CONNECT_STATUS, // status of all printers USER_ACCOUNT_ACTION_AVATAR, #if 0 + USER_ACCOUNT_ACTION_CONNECT_PRINTERS, // all info about all printers USER_ACCOUNT_ACTION_CONNECT_USER_DATA, USER_ACCOUNT_ACTION_CONNECT_DUMMY, #endif // 0 @@ -117,11 +118,12 @@ public: m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID] = std::make_unique("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", EVT_UA_ID_USER_SUCCESS, EVT_UA_RESET); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN] = std::make_unique("TEST_ACCESS_TOKEN", "https://test-account.prusa3d.com/api/v1/me/", EVT_UA_ID_USER_SUCCESS, EVT_UA_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION] = std::make_unique("TEST_CONNECTION", "https://test-account.prusa3d.com/api/v1/me/", wxEVT_NULL, EVT_UA_RESET); - m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS] = std::make_unique("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers", EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, EVT_UA_FAIL); + m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS] = std::make_unique("CONNECT_STATUS", "https://dev.connect.prusa3d.com/slicer/status", EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, EVT_UA_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR] = std::make_unique("AVATAR", "https://test-media.printables.com/media/", EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL); #if 0 m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_USER_DATA] = std::make_unique("CONNECT_USER_DATA", "https://dev.connect.prusa3d.com/app/login", EVT_UA_CONNECT_USER_DATA_SUCCESS, EVT_UA_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DUMMY] = std::make_unique("CONNECT_DUMMY", "https://dev.connect.prusa3d.com/slicer/dummy", EVT_UA_SUCCESS, EVT_UA_FAIL); + m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS] = std::make_unique("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers", EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, EVT_UA_FAIL); #endif // 0 } @@ -133,11 +135,13 @@ public: m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION].reset(nullptr); - m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS].reset(nullptr); + m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR].reset(nullptr); #if 0 m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_USER_DATA].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DUMMY].reset(nullptr); + m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS].reset(nullptr); + #endif // 0 }