diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 02eea387cc..2595cff490 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -5353,6 +5353,16 @@ Preset *PresetBundle::get_similar_printer_preset(std::string printer_model, std: return printer_presets.begin()->second; } +void PresetBundle::install_slot_filaments(AppConfig &config) +{ + // User presets have no vendor, so the filaments section never hides them. + for (const std::string &name : filament_presets) + if (Preset *preset = filaments.find_preset(name, false, true); preset != nullptr && preset->is_system) { + preset->is_visible = true; + config.set(AppConfig::SECTION_FILAMENTS, preset->name, "true"); + } +} + //BBS: check whether this is the only edited filament bool PresetBundle::is_the_only_edited_filament(unsigned int filament_index) { diff --git a/src/libslic3r/PresetBundle.hpp b/src/libslic3r/PresetBundle.hpp index af2a4302fa..5a660e839f 100644 --- a/src/libslic3r/PresetBundle.hpp +++ b/src/libslic3r/PresetBundle.hpp @@ -459,6 +459,10 @@ public: void get_ams_cobox_infos(AMSComboInfo &combox_info); unsigned int sync_ams_list(std::vector> &unknowns, bool use_map, std::map &maps, bool enable_append, MergeFilamentInfo &merge_info, bool color_only = false); + // Installs the system filament presets in the slots: visible now, and in config's filaments + // section, which load_selections() keeps a slot's preset by. After an AMS sync the slots hold the + // printer's filaments, which the user may never have ticked. + void install_slot_filaments(AppConfig &config); //BBS: check whether this is the only edited filament bool is_the_only_edited_filament(unsigned int filament_index); diff --git a/src/slic3r/GUI/DeviceCore/DevManager.cpp b/src/slic3r/GUI/DeviceCore/DevManager.cpp index d4282074ba..b84e5bf533 100644 --- a/src/slic3r/GUI/DeviceCore/DevManager.cpp +++ b/src/slic3r/GUI/DeviceCore/DevManager.cpp @@ -37,6 +37,11 @@ using namespace nlohmann; namespace { + // The printer last selected under each printer agent, keyed by agent id. + const std::string LAST_SELECTED_MACHINE_SECTION = "last_selected_machine"; + // Builds before that kept one printer for every agent in this app key. + const std::string LEGACY_LAST_SELECTED_MACHINE_KEY = "user_last_selected_machine"; + // Orca: access_code lives on BBLocalMachine::access_code (keyed by dev_id via // get_local_machines(), scoped by the record's own printer_agent_id field) - so binding a // printer under one agent doesn't silently appear as already-bound under a different, @@ -604,8 +609,15 @@ namespace Slic3r } } userMachineList.clear(); + // A machine list that arrives for the signed-out account must not reselect its printer. + m_restore_last_machine = false; if (!keep_selected_machine) { + // The printer signing out deselects is not reconnected at the next start either. Its + // connection type can't tell which that is: a Bambu account printer LAN discovery never + // saw has none. + if (!selected_machine.empty()) + record_user_last_machine(""); selected_machine = ""; local_selected_machine = ""; } @@ -635,6 +647,7 @@ namespace Slic3r bool DeviceManager::set_selected_machine(std::string dev_id) { + m_restore_last_machine = false; // any selection settles a pending restore_last_machine() BOOST_LOG_TRIVIAL(info) << "set_selected_machine=" << dev_id << " cur_selected=" << selected_machine; auto my_machine_list = get_my_machine_list(get_current_printer_agent_id()); @@ -691,6 +704,20 @@ namespace Slic3r // same dev_id, lan => disconnect and reconnect else { + // Orca: unless a Bambu printer's connect is still in flight. Tearing that down + // mid-flight is what the 02.08 network plugin does not survive, and re-picking the + // printer a restore just reconnected is the quickest way there. Only Bambu: the + // Moonraker agents report no failed connect, so their flag would never clear and + // re-picking the printer would no longer retry it. The Bambu plugin gives up on a + // connect it can't make after about 24 s; one still unanswered after 60 s stops + // counting, so a plugin version that drops the result can't block retries for good. + if (it->second->printer_agent_id == BBL_PRINTER_AGENT_ID && it->second->get_lan_mode_connection_state() && + std::chrono::steady_clock::now() - m_lan_connect_started < std::chrono::seconds(60)) { + BOOST_LOG_TRIVIAL(info) << "set_selected_machine: same lan machine, dev_id =" << dev_id + << ", its connect is still in flight, keep it"; + return true; + } + BOOST_LOG_TRIVIAL(info) << "set_selected_machine: same lan machine, dev_id =" << dev_id << ", disconnect and reconnect"; @@ -702,6 +729,7 @@ namespace Slic3r it->second->connect(); it->second->set_lan_mode_connection_state(true); + m_lan_connect_started = std::chrono::steady_clock::now(); } } } @@ -724,6 +752,7 @@ namespace Slic3r it->second->reset(); it->second->connect(); it->second->set_lan_mode_connection_state(true); + m_lan_connect_started = std::chrono::steady_clock::now(); } } } @@ -892,6 +921,16 @@ namespace Slic3r } void DeviceManager::parse_user_print_info(std::string body) + { + // A pending restore waits for the first accepted list, whoever fetched it. It selects a + // machine, so it runs once listMutex is released. + if (read_user_print_info(body) && m_restore_last_machine) { + m_restore_last_machine = false; + load_last_machine(); + } + } + + bool DeviceManager::read_user_print_info(const std::string& body) { BOOST_LOG_TRIVIAL(trace) << "DeviceManager::parse_user_print_info"; std::lock_guard lock(listMutex); @@ -923,14 +962,16 @@ namespace Slic3r BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ": ignoring stale response provider=" << provider << " agent_id=" << agent_id << " generation=" << generation; - return; + return false; } #if !BBL_RELEASE_TO_PUBLIC BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": " << j; #endif - if (j.contains("devices") && !j["devices"].is_null()) + // Without a device list the response leaves the list as it is, so it is not one. + const bool has_device_list = j.contains("devices") && !j["devices"].is_null(); + if (has_device_list) { for (auto& elem : j["devices"]) { @@ -1032,11 +1073,13 @@ namespace Slic3r << " parsed_count=" << new_list.size() << " stored_count=" << userMachineList.size(); } + return has_device_list; } catch (std::exception& e) { BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " exception=" << e.what(); } + return false; } void DeviceManager::update_user_machine_list_info(const std::string& provider) @@ -1055,42 +1098,72 @@ namespace Slic3r // parse_user_print_info and on_machine_alive (SSDP for discovery) both mutate the same userMachineList map. // on_machine_alive mutates the map on the UI thread, do the same for parse_user_print_info. BOOST_LOG_TRIVIAL(trace) << "Orca diagnostic: queueing parse_user_print_info on UI thread"; - Slic3r::GUI::wxGetApp().CallAfter([this, body]() { parse_user_print_info(body); }); + Slic3r::GUI::wxGetApp().CallAfter([this, body]() { + // The parse may restore a printer (restore_last_machine()), which needs a live app. + if (!Slic3r::GUI::wxGetApp().is_closing()) + parse_user_print_info(body); + }); } } + // The remembered printer is kept per printer agent: each agent lists its own machines, so a + // printer preset of another agent in between must not make Orca forget this agent's printer. void DeviceManager::record_user_last_machine(const std::string& dev_id) { - if (AppConfig* config = get_app_config()) { - config->set("user_last_selected_machine", dev_id); - } + AppConfig* config = get_app_config(); + const std::string agent_id = get_current_printer_agent_id(); + if (!config || agent_id.empty()) + return; + + // A forget leaves no entry, so the section lists only remembered printers. + if (!dev_id.empty()) + config->set(LAST_SELECTED_MACHINE_SECTION, agent_id, dev_id); + else + config->erase(LAST_SELECTED_MACHINE_SECTION, agent_id); + // The pre-per-agent key is only read until the Bambu agent records its own printer, a forget + // included, so it can't bring back a printer the user has left. + if (agent_id == BBL_PRINTER_AGENT_ID && config->has(LEGACY_LAST_SELECTED_MACHINE_KEY)) + config->erase("app", LEGACY_LAST_SELECTED_MACHINE_KEY); } std::string DeviceManager::get_user_last_machine() const { - 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) { - return m_agent->get_user_selected_machine(); + const std::string agent_id = get_current_printer_agent_id(); + if (AppConfig* config = get_app_config(); config && !agent_id.empty()) { + const std::string last_machine = config->get(LAST_SELECTED_MACHINE_SECTION, agent_id); + if (!last_machine.empty()) + return last_machine; + // The legacy key holds whichever agent's printer was selected last. Only Bambu printers + // are restored from it: the other agents select the preset's print_host. + if (agent_id == BBL_PRINTER_AGENT_ID) { + const std::string legacy_machine = config->get(LEGACY_LAST_SELECTED_MACHINE_KEY); + if (!legacy_machine.empty()) + return legacy_machine; } } - return ""; + return m_agent ? m_agent->get_user_selected_machine() : ""; } void DeviceManager::load_last_machine() { - // Only reconnect the remembered cloud machine. Do not select an arbitrary - // first machine: agent swaps intentionally leave the selection empty until - // the new agent explicitly selects its configured printer. - if (userMachineList.empty()) + // Reconnect only the printer the user last selected under this agent, LAN or cloud, never + // another one: an unrequested connection to a printer the user then switches away from is + // torn down mid-flight, which the 02.08 network plugin does not survive. + if (get_selected_machine()) return; - const auto& last_monitor_machine = get_user_last_machine(); - if (userMachineList.find(last_monitor_machine) != userMachineList.end()) - set_selected_machine(last_monitor_machine); + const std::string last_machine = get_user_last_machine(); + if (!last_machine.empty() && get_my_machine_list(get_current_printer_agent_id()).count(last_machine) != 0) + set_selected_machine(last_machine); + } + + void DeviceManager::restore_last_machine() + { + // A cloud printer is listed only once the account's machine list arrives, so until a list is + // accepted (parse_user_print_info) or a selection is made, the restore stays pending. + m_restore_last_machine = true; + load_last_machine(); } void DeviceManager::OnMachineBindStateChanged(MachineObject* obj, const std::string& new_state) diff --git a/src/slic3r/GUI/DeviceCore/DevManager.h b/src/slic3r/GUI/DeviceCore/DevManager.h index 1a669d0aee..820c594685 100644 --- a/src/slic3r/GUI/DeviceCore/DevManager.h +++ b/src/slic3r/GUI/DeviceCore/DevManager.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include "libslic3r/CommonDefs.hpp" @@ -34,6 +35,8 @@ private: bool m_enable_mutil_machine = false; std::mutex listMutex; + bool m_restore_last_machine{ false }; /* see restore_last_machine() */ + std::chrono::steady_clock::time_point m_lan_connect_started; /* the selected LAN machine's last connect */ std::string selected_machine; /* dev_id */ std::string local_selected_machine; /* dev_id */ std::map localMachineList; /* dev_id -> MachineObject*, localMachine SSDP */ @@ -86,6 +89,11 @@ public: void clear_other_devices(); void load_last_machine(); + // Selects the printer last selected under the live agent: now when it is one of the agent's + // machines, else once an accepted cloud machine list holds it. Any selection, and logging out, + // settle a pending restore. + void restore_last_machine(); + bool has_pending_last_machine_restore() const { return m_restore_last_machine; } void update_user_machine_list_info(const std::string& provider); void parse_user_print_info(std::string body); void reload_printer_settings(); @@ -126,6 +134,10 @@ private: // reload without an app restart. void load_local_machines_from_config(); + // Applies a cloud machine list under listMutex; false when the response is stale, holds no + // device list or can't be parsed. + bool read_user_print_info(const std::string& body); + void keep_alive(); void check_pushing(); std::string get_current_cloud_provider() const; diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index bd1f22d754..52c2fe5537 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -2420,6 +2420,10 @@ void GUI_App::init_networking_callbacks() if (is_closing()) { return; } + // Orca: ends the connect a selection started; with the selection logs this shows + // whether a later selection landed while that connect was in flight. + BOOST_LOG_TRIVIAL(info) << "local connect result: dev_id=" << dev_id << ", state=" << state + << ", msg=" << msg; /* request_pushing */ MachineObject* obj = m_device_manager->get_my_machine(dev_id); wxCommandEvent event(EVT_CONNECT_LAN_MODE_PRINT); @@ -4308,7 +4312,11 @@ void GUI_App::set_live_printer_agent(std::shared_ptr agent) // a standalone helper. Pass nullptr to clear the selection. if (DeviceManager* dev = getDeviceManager()) { + // why: the swap tears the connection down, it is not the user leaving that printer, so it + // stays remembered under the outgoing agent and reconnects when that agent is live again + const std::string last_machine = dev->get_user_last_machine(); dev->set_selected_machine(""); // why: empty id disconnects and deselects the current machine + dev->record_user_last_machine(last_machine); m_agent->set_user_selected_machine(""); // note: belt-and-suspenders (precedent: DeviceManagerRefresher::on_timer) dev->OnSelectedMachineLost(); // why: clear stale sidebar sync-status / AMS @@ -4430,14 +4438,17 @@ void GUI_App::switch_printer_agent_if_pending() void GUI_App::select_machine(const std::string& agent_id) { - // Skip for BBL agent for now - uses its own device discovery/selection - // Orca todo: revisit in future if we want to support auto-switching for BBL printers - if (agent_id == BBL_PRINTER_AGENT_ID) { + if (!m_device_manager || !preset_bundle) { + BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": no device manager or preset bundle"; return; } - if (!m_device_manager || !preset_bundle) { - BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": no device manager or preset bundle"; + // Orca: Bambu printers are not tied to the preset's print_host. Reconnect the one the user last + // selected, as BambuStudio does at startup: a saved LAN printer now, a cloud printer once the + // account's machine list arrives. Directly, not through the Device tab's selection flow, which + // would also queue a nozzle sync for the next visit to Prepare. + if (agent_id == BBL_PRINTER_AGENT_ID) { + m_device_manager->restore_last_machine(); return; } diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 88be694289..ace5c4b423 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -6466,6 +6466,8 @@ void Sidebar::sync_ams_list(bool is_from_big_sync_btn) // For full sync, preset selection/list update may rebuild combo widgets. // For color-only, keep current presets untouched and refresh colors only. if (!sync_color_only) { + // Keep the printer's filaments in their slots after a restart, ticked or not. + wxGetApp().preset_bundle->install_slot_filaments(*wxGetApp().app_config); wxGetApp().get_tab(Preset::TYPE_FILAMENT)->select_preset(wxGetApp().preset_bundle->filament_presets[0]); wxGetApp().preset_bundle->export_selections(*wxGetApp().app_config); update_dynamic_filament_list(); diff --git a/tests/libslic3r/test_preset_bundle_loading.cpp b/tests/libslic3r/test_preset_bundle_loading.cpp index 4c900b4d4b..ce5dc0d711 100644 --- a/tests/libslic3r/test_preset_bundle_loading.cpp +++ b/tests/libslic3r/test_preset_bundle_loading.cpp @@ -1002,6 +1002,47 @@ TEST_CASE("Selected printer uses its default or saved bed type", "[Preset][Bundl CHECK(app_config.get_printer_setting("Test Printer", "curr_bed_type") == std::to_string(static_cast(expected_bed_type))); } +TEST_CASE("Installed slot filaments keep their slots when selections reload", "[Preset][Bundle]") +{ + VendorProfile vendor("TestVendor"); + // A printer and two system filaments, as the vendor profiles load them. + auto add_presets = [&vendor](PresetBundle& bundle) { + add_inmemory_preset(bundle.printers, "Test Printer"); + for (const char* name : {"Filament A", "Filament B"}) { + Preset& filament = add_inmemory_preset(bundle.filaments, name); + filament.is_system = true; + filament.vendor = &vendor; + } + }; + + // Only A is installed, and the printer's slots hold A and B, as an AMS sync leaves them. + AppConfig app_config; + app_config.set(AppConfig::SECTION_FILAMENTS, "Filament A", "true"); + app_config.set("presets", PRESET_PRINTER_NAME, "Test Printer"); + app_config.set_printer_setting("Test Printer", PRESET_FILAMENT_NAME, "Filament A"); + app_config.set_printer_setting("Test Printer", "filament_01", "Filament B"); + const std::vector slots{"Filament A", "Filament B"}; + + PresetBundle uninstalled; + add_presets(uninstalled); + uninstalled.load_selections(app_config); + CHECK(uninstalled.filament_presets == std::vector{"Filament A", "Filament A"}); + + PresetBundle synced; + add_presets(synced); + synced.load_selections(app_config); + synced.filament_presets = slots; + synced.install_slot_filaments(app_config); + CHECK(app_config.get(AppConfig::SECTION_FILAMENTS, "Filament B") == "true"); + synced.export_selections(app_config); + + // A restart knows only what the config holds. + PresetBundle restarted; + add_presets(restarted); + restarted.load_selections(app_config); + CHECK(restarted.filament_presets == slots); +} + TEST_CASE("A directory of user presets loads with each one resolved against its parent", "[Preset][Bundle]") { ScopedTemporaryDir temp_dir; diff --git a/tests/slic3rutils/test_device_manager_integration.cpp b/tests/slic3rutils/test_device_manager_integration.cpp index 91bb3a3663..85b9170ba0 100644 --- a/tests/slic3rutils/test_device_manager_integration.cpp +++ b/tests/slic3rutils/test_device_manager_integration.cpp @@ -8,6 +8,7 @@ #include "slic3r/Utils/IPrinterAgent.hpp" #include "slic3r/Utils/CloudProvider.hpp" #include +#include #include #include @@ -178,6 +179,97 @@ TEST_CASE("Device manager filters and rehomes devices by printer-agent ownership CHECK(manager.get_my_machine_list("integration-agent-b").count(machine.dev_id) == 1); } +TEST_CASE("Device manager remembers the last selected machine per printer agent", "[DeviceManager][integration]") +{ + 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, false, &app_config.config); + + manager.record_user_last_machine("machine-of-a"); + network.set_printer_agent(agent_b); + manager.record_user_last_machine("machine-of-b"); + + network.set_printer_agent(agent_a); + CHECK(manager.get_user_last_machine() == "machine-of-a"); + network.set_printer_agent(agent_b); + CHECK(manager.get_user_last_machine() == "machine-of-b"); + + network.set_printer_agent(nullptr); + manager.record_user_last_machine("machine-without-agent"); + CHECK(app_config.config.get_section("last_selected_machine").size() == 2); + + // A forget leaves no entry behind, an empty one included. + network.set_printer_agent(agent_b); + manager.record_user_last_machine(""); + CHECK(app_config.config.get_section("last_selected_machine").count("integration-agent-b") == 0); + app_config.config.set("last_selected_machine", "integration-agent-b", ""); + manager.record_user_last_machine(""); + CHECK(app_config.config.get_section("last_selected_machine").count("integration-agent-b") == 0); + network.set_printer_agent(agent_a); + CHECK(manager.get_user_last_machine() == "machine-of-a"); +} + +TEST_CASE("Device manager reads the pre-per-agent last machine for Bambu printers until it records one", "[DeviceManager][integration]") +{ + ScopedAppConfig app_config; + app_config.config.set("user_last_selected_machine", "legacy-machine"); + auto other = std::make_shared("integration-agent"); + NetworkAgent network(nullptr, other); + DeviceManager manager(&network, false, &app_config.config); + + CHECK(manager.get_user_last_machine() != "legacy-machine"); + network.set_printer_agent(std::make_shared(BBL_PRINTER_AGENT_ID)); + CHECK(manager.get_user_last_machine() == "legacy-machine"); + + // A forget is a record too: the legacy machine must not come back after it. + manager.record_user_last_machine(""); + CHECK_FALSE(app_config.config.has("user_last_selected_machine")); + CHECK(manager.get_user_last_machine() != "legacy-machine"); +} + +TEST_CASE("Device manager keeps a pending last-machine restore until a cloud list is accepted", "[DeviceManager][integration]") +{ + ScopedAppConfig app_config; + NetworkAgent network(nullptr, std::make_shared("integration-agent")); + network.set_printer_agent(std::make_shared("integration-agent")); + DeviceManager manager(&network, false, &app_config.config); + // Remembered but in no list yet, as a cloud printer is until the account's list arrives. + manager.record_user_last_machine("remembered-cloud-machine"); + + manager.restore_last_machine(); + REQUIRE(manager.has_pending_last_machine_restore()); + + const std::uint64_t generation = network.get_user_machine_list_generation(); + manager.parse_user_print_info(machine_list_response(ORCA_CLOUD_PROVIDER, "integration-agent", generation + 1, "Superseded")); + CHECK(manager.has_pending_last_machine_restore()); + // Responses that leave the list as it is, with no device list or one that can't be read, don't settle it either. + manager.parse_user_print_info("{}"); + manager.parse_user_print_info(R"({"devices":null})"); + manager.parse_user_print_info(R"({"devices":"not a list"})"); + CHECK(manager.has_pending_last_machine_restore()); + + // Parsed directly, as the Device tab's printer list and the print dialogs do. + manager.parse_user_print_info(machine_list_response(ORCA_CLOUD_PROVIDER, "integration-agent", generation, "Accepted")); + CHECK(manager.get_user_machinelist().count("integration-device") == 1); + CHECK_FALSE(manager.has_pending_last_machine_restore()); +} + +TEST_CASE("Logging out cancels a pending last-machine restore", "[DeviceManager][integration]") +{ + ScopedAppConfig app_config; + NetworkAgent network(nullptr, std::make_shared("integration-agent")); + network.set_printer_agent(std::make_shared("integration-agent")); + DeviceManager manager(&network, false, &app_config.config); + manager.record_user_last_machine("remembered-cloud-machine"); + + manager.restore_last_machine(); + REQUIRE(manager.has_pending_last_machine_restore()); + manager.clean_user_info(true); + CHECK_FALSE(manager.has_pending_last_machine_restore()); +} + TEST_CASE("AMS filament settings keep legacy single-color commands compatible", "[DeviceManager][AMSMaterialsSetting][integration]") { ScopedAppConfig app_config;