diff --git a/src/slic3r/GUI/DeviceCore/DevManager.cpp b/src/slic3r/GUI/DeviceCore/DevManager.cpp index 5b4746a968..f129700c5e 100644 --- a/src/slic3r/GUI/DeviceCore/DevManager.cpp +++ b/src/slic3r/GUI/DeviceCore/DevManager.cpp @@ -1,8 +1,9 @@ +#include "DevManager.h" #include #include -#include "DevManager.h" +#include #include "CloudProvider.hpp" #include "DevUtil.h" @@ -48,10 +49,12 @@ namespace { namespace Slic3r { - DeviceManager::DeviceManager(NetworkAgent* agent) + DeviceManager::DeviceManager(NetworkAgent* agent, bool enable_refresher, AppConfig* app_config) { - m_agent = agent; - m_refresher = new DeviceManagerRefresher(this); + m_agent = agent; + m_app_config = app_config; + if (enable_refresher) + m_refresher = new DeviceManagerRefresher(this); DevPrinterConfigUtil::InitFilePath(resources_dir()); @@ -62,9 +65,14 @@ namespace Slic3r } } + AppConfig* DeviceManager::get_app_config() const + { + return m_app_config ? m_app_config : GUI::wxGetApp().app_config; + } + void DeviceManager::load_local_machines_from_config() { - AppConfig* config = GUI::wxGetApp().app_config; + AppConfig* config = get_app_config(); if (!config) return; const auto local_machines = config->get_local_machines(); @@ -90,9 +98,8 @@ namespace Slic3r } } - void DeviceManager::update_local_machine(const MachineObject& m) + void DeviceManager::update_local_machine(const MachineObject& m, AppConfig* config) { - AppConfig* config = GUI::wxGetApp().app_config; if (config) { if (m.is_lan_mode_printer()) { if (m.has_access_right()) { @@ -113,7 +120,8 @@ namespace Slic3r DeviceManager::~DeviceManager() { - delete m_refresher; + if (m_refresher) + delete m_refresher; for (auto it = localMachineList.begin(); it != localMachineList.end(); it++) { @@ -173,14 +181,22 @@ namespace Slic3r return printer_agent ? printer_agent->get_agent_info().id : ""; } + std::string DeviceManager::get_current_cloud_provider() const + { + const std::string agent_id = get_current_printer_agent_id(); + if (!agent_id.empty()) + return agent_id == BBL_PRINTER_AGENT_ID ? BBL_CLOUD_PROVIDER : ORCA_CLOUD_PROVIDER; + return GUI::wxGetApp().get_printer_cloud_provider(); + } + void DeviceManager::EnableMultiMachine(bool enable) { m_agent->enable_multi_machine(enable); m_enable_mutil_machine = enable; } - void DeviceManager::start_refresher() { m_refresher->Start(); } - void DeviceManager::stop_refresher() { m_refresher->Stop(); } + void DeviceManager::start_refresher() { if (m_refresher) m_refresher->Start(); } + void DeviceManager::stop_refresher() { if (m_refresher) m_refresher->Stop(); } void DeviceManager::keep_alive() @@ -297,6 +313,8 @@ namespace Slic3r /* update localMachineList */ it = localMachineList.find(dev_id); + AppConfig* config = get_app_config(); + if (it != localMachineList.end()) { // update properties /* ip changed */ @@ -386,7 +404,6 @@ namespace Slic3r obj->m_is_online = true; //load access code - AppConfig* config = Slic3r::GUI::wxGetApp().app_config; if (config) { obj->set_access_code(get_access_code_with_legacy_fallback(config, dev_id, obj->printer_agent_id), false); } @@ -400,7 +417,7 @@ namespace Slic3r << ", ip = " << dev_ip <<", printer_name = " << dev_name << ", con_type= " << connect_type <<", signal= " << printer_signal << ", bind_state= " << bind_state; } - update_local_machine(*obj); + update_local_machine(*obj, config); } catch (...) { ; @@ -435,11 +452,16 @@ namespace Slic3r obj->last_alive = Slic3r::Utils::get_current_time_utc(); obj->set_access_code(access_code, false); - update_local_machine(*obj); + update_local_machine(*obj, get_app_config()); return obj; } + void DeviceManager::update_local_machine(const MachineObject& m) + { + update_local_machine(m, GUI::wxGetApp().app_config); + } + int DeviceManager::query_bind_status(std::string& msg, const std::string& provider) { if (!m_agent) @@ -635,7 +657,8 @@ namespace Slic3r it->second->reset(); #if !BBL_RELEASE_TO_PUBLIC - it->second->connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false); + AppConfig* config = get_app_config(); + it->second->connect(config && config->get("enable_ssl_for_mqtt") == "true"); #else it->second->connect(it->second->local_use_ssl); #endif @@ -661,7 +684,8 @@ namespace Slic3r BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new lan machine, dev_id =" << dev_id; it->second->reset(); #if !BBL_RELEASE_TO_PUBLIC - it->second->connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false); + AppConfig* config = get_app_config(); + it->second->connect(config && config->get("enable_ssl_for_mqtt") == "true"); #else it->second->connect(it->second->local_use_ssl); #endif @@ -851,14 +875,15 @@ namespace Slic3r json j = json::parse(body); const bool has_request_context = j.contains("provider") && j.contains("agent_id") && j.contains("generation"); + const std::string current_provider = get_current_cloud_provider(); const std::string provider = j.contains("provider") ? j["provider"].get() - : GUI::wxGetApp().get_printer_cloud_provider(); + : current_provider; const std::string agent_id = j.contains("agent_id") ? j["agent_id"].get() : get_current_printer_agent_id(); const std::uint64_t generation = j.value("generation", std::uint64_t(0)); if (has_request_context && - (provider != GUI::wxGetApp().get_printer_cloud_provider() || + (provider != current_provider || agent_id != get_current_printer_agent_id() || generation != (m_agent ? m_agent->get_user_machine_list_generation() : 0))) { BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": ignoring stale response provider=" @@ -903,7 +928,8 @@ namespace Slic3r if (obj->get_dev_ip().empty()) { - obj->get_dev_ip() = Slic3r::GUI::wxGetApp().app_config->get("ip_address", dev_id); + if (AppConfig* config = get_app_config()) + obj->get_dev_ip() = config->get("ip_address", dev_id); } userMachineList.insert(std::make_pair(dev_id, obj)); } @@ -1001,15 +1027,15 @@ namespace Slic3r void DeviceManager::record_user_last_machine(const std::string& dev_id) { - if (Slic3r::GUI::wxGetApp().app_config) { - Slic3r::GUI::wxGetApp().app_config->set("user_last_selected_machine", dev_id); + if (AppConfig* config = get_app_config()) { + config->set("user_last_selected_machine", dev_id); } } std::string DeviceManager::get_user_last_machine() const { - if (Slic3r::GUI::wxGetApp().app_config) { - const auto& user_last_machine = Slic3r::GUI::wxGetApp().app_config->get("user_last_selected_machine"); + if (AppConfig* config = get_app_config()) { + const auto& user_last_machine = config->get("user_last_selected_machine"); if (!user_last_machine.empty()) { return user_last_machine; } else if (m_agent) { diff --git a/src/slic3r/GUI/DeviceCore/DevManager.h b/src/slic3r/GUI/DeviceCore/DevManager.h index 32f93f3a1a..1ba4631881 100644 --- a/src/slic3r/GUI/DeviceCore/DevManager.h +++ b/src/slic3r/GUI/DeviceCore/DevManager.h @@ -12,6 +12,7 @@ namespace Slic3r struct BBLocalMachine; class MachineObject; class NetworkAgent; +class AppConfig; namespace GUI { class GUI_App; @@ -24,6 +25,7 @@ class DeviceManager friend class DeviceManagerRefresher; private: NetworkAgent* m_agent{ nullptr }; + AppConfig* m_app_config{ nullptr }; DeviceManagerRefresher* m_refresher{ nullptr }; bool m_enable_mutil_machine = false; @@ -35,11 +37,13 @@ private: std::map userMachineList; /* dev_id -> MachineObject* cloudMachine of User */ public: - DeviceManager(NetworkAgent* agent = nullptr); + DeviceManager(NetworkAgent* agent = nullptr, bool enable_refresher = true, + AppConfig* app_config = nullptr); ~DeviceManager(); public: NetworkAgent* get_agent() const { return m_agent; } + AppConfig* get_app_config() const; void set_agent(NetworkAgent* agent); void start_refresher(); @@ -121,6 +125,7 @@ private: void keep_alive(); void check_pushing(); + std::string get_current_cloud_provider() const; void OnMachineBindStateChanged(MachineObject* obj, const std::string& new_state); void OnSelectedMachineChanged(const std::string& pre_dev_id, const std::string& new_dev_id); @@ -133,14 +138,15 @@ public: std::string connection_type, std::string bind_state, std::string version, std::string access_code); static void update_local_machine(const MachineObject& m); + static void update_local_machine(const MachineObject& m, AppConfig* config); }; class DeviceManagerRefresher : public wxObject { - wxTimer* m_timer{ nullptr }; - int m_timer_interval_msec = 5000; + wxTimer* m_timer{nullptr}; + int m_timer_interval_msec = 5000; - DeviceManager* m_manager{ nullptr }; + DeviceManager* m_manager{nullptr}; public: DeviceManagerRefresher(DeviceManager* manger); diff --git a/src/slic3r/GUI/DeviceManager.cpp b/src/slic3r/GUI/DeviceManager.cpp index 976492d81d..b8db5c97d7 100644 --- a/src/slic3r/GUI/DeviceManager.cpp +++ b/src/slic3r/GUI/DeviceManager.cpp @@ -477,7 +477,7 @@ void MachineObject::set_access_code(std::string code, bool only_refresh) { this->access_code = code; if (only_refresh) { - AppConfig* config = GUI::wxGetApp().app_config; + AppConfig* config = m_manager ? m_manager->get_app_config() : GUI::wxGetApp().app_config; if (config) { if (is_lan_mode_printer()) { // why: LAN codes are scoped via BBLocalMachine::access_code, keyed by dev_id and @@ -490,7 +490,7 @@ void MachineObject::set_access_code(std::string code, bool only_refresh) // fresh from the cloud API's current response, so there's no cross-agent leakage // risk to guard against there. if (!code.empty()) { - DeviceManager::update_local_machine(*this); + DeviceManager::update_local_machine(*this, config); } else { // Only patch an existing record's code - don't persist a brand-new // never-bound entry just because set_access_code("") was called on it. @@ -4632,7 +4632,7 @@ int MachineObject::parse_json(std::string tunnel, std::string payload, bool key_ if (diff.count() > 10.0f) { BOOST_LOG_TRIVIAL(trace) << "parse_json timeout = " << diff.count(); } - DeviceManager::update_local_machine(*this); + DeviceManager::update_local_machine(*this, m_manager ? m_manager->get_app_config() : GUI::wxGetApp().app_config); return 0; } diff --git a/tests/slic3rutils/test_device_manager_integration.cpp b/tests/slic3rutils/test_device_manager_integration.cpp index 15f6e721cb..2b042d4f8e 100644 --- a/tests/slic3rutils/test_device_manager_integration.cpp +++ b/tests/slic3rutils/test_device_manager_integration.cpp @@ -2,14 +2,12 @@ #include #include -#include #include #include #include #include #include -#include #include #include @@ -55,9 +53,6 @@ private: struct ScopedAppConfig { AppConfig config; - - ScopedAppConfig() { GUI::wxGetApp().app_config = &config; } - ~ScopedAppConfig() { GUI::wxGetApp().app_config = nullptr; } }; std::string machine_list_response(const std::string& provider, const std::string& agent_id, @@ -99,12 +94,10 @@ TEST_CASE("Network agent stamps user-machine responses with request context", "[ TEST_CASE("Device manager ignores stale cloud machine responses", "[DeviceManager][integration]") { - wxInitializer wx_init; - REQUIRE(wx_init.IsOk()); ScopedAppConfig app_config; NetworkAgent network(nullptr, std::make_shared("integration-agent")); network.set_printer_agent(std::make_shared("integration-agent")); - DeviceManager manager(&network); + DeviceManager manager(&network, false, &app_config.config); const std::uint64_t current_generation = network.get_user_machine_list_generation(); manager.parse_user_print_info(machine_list_response(ORCA_CLOUD_PROVIDER, "integration-agent", @@ -129,12 +122,11 @@ TEST_CASE("Device manager ignores stale cloud machine responses", "[DeviceManage TEST_CASE("Device manager filters and rehomes devices by printer-agent ownership", "[DeviceManager][integration]") { - wxInitializer wx_init; - REQUIRE(wx_init.IsOk()); + ScopedAppConfig app_config; auto agent_a = std::make_shared("integration-agent-a"); auto agent_b = std::make_shared("integration-agent-b"); NetworkAgent network(nullptr, agent_a); - DeviceManager manager(&network); + DeviceManager manager(&network, false, &app_config.config); BBLocalMachine machine; machine.dev_id = "integration-lan-device"; diff --git a/tests/slic3rutils/test_web_media_controller.cpp b/tests/slic3rutils/test_web_media_controller.cpp index 2933145d8d..cdf756ca3a 100644 --- a/tests/slic3rutils/test_web_media_controller.cpp +++ b/tests/slic3rutils/test_web_media_controller.cpp @@ -2,7 +2,6 @@ #include -#include #include #include @@ -84,9 +83,6 @@ public: TEST_CASE("Web media controller tears down a snapshot lifecycle", "[WebMediaController][integration]") { - wxInitializer wx_init; - REQUIRE(wx_init.IsOk()); - StubWebView view; WebMediaController controller(&view);