From 188782518d94230440aab65e1b29ca1efb97f7f7 Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Thu, 26 Nov 2026 00:03:22 +0800 Subject: [PATCH] fix: bind printer agent printers using the entered address (#17857) - Moonraker's `bind_detect` now builds the device ID from the entered address instead of the connected printer's cached identity, so binding a second printer by IP no longer reselects and reconnects the first one. - The C11 model fallback for a device bound without a model now applies only to Bambu printers. --- src/slic3r/GUI/DeviceCore/DevManager.cpp | 8 +- src/slic3r/Utils/MoonrakerPrinterAgent.cpp | 18 ++-- .../test_device_manager_integration.cpp | 94 +++++++++++++++++++ .../test_moonraker_printer_agent.cpp | 61 ++++++++++++ 4 files changed, 172 insertions(+), 9 deletions(-) diff --git a/src/slic3r/GUI/DeviceCore/DevManager.cpp b/src/slic3r/GUI/DeviceCore/DevManager.cpp index 52e238fd1a..08a219bc1f 100644 --- a/src/slic3r/GUI/DeviceCore/DevManager.cpp +++ b/src/slic3r/GUI/DeviceCore/DevManager.cpp @@ -502,10 +502,12 @@ namespace Slic3r obj->printer_agent_id = get_current_printer_agent_id(); localMachineList.insert(std::make_pair(machine.dev_id, obj)); } - if (machine.printer_type.empty()) - obj->printer_type = _parse_printer_type("C11"); - else + // Only Bambu printers use the C11 fallback. Other agents may not report a model, + // so leave their existing model unchanged when none is provided. + if (!machine.printer_type.empty()) obj->printer_type = _parse_printer_type(machine.printer_type); + else if (obj->printer_agent_id == BBL_PRINTER_AGENT_ID) + obj->printer_type = _parse_printer_type("C11"); obj->dev_connection_type = connection_type == "farm" ? "lan":connection_type; obj->bind_state = connection_type == "farm" ? "free":bind_state; obj->bind_sec_link = "secure"; diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp index 68be2dc7ee..081c4998db 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp @@ -396,12 +396,18 @@ int MoonrakerPrinterAgent::bind_detect(std::string dev_ip, std::string sec_link, { (void) sec_link; - detect.dev_id = device_info.dev_id.empty() ? dev_ip : device_info.dev_id; - detect.model_id = device_info.model_id.empty() ? device_info.model_name : device_info.model_id; - // Prefer fetched hostname, then preset model name, then generic fallback - detect.dev_name = device_info.dev_name.empty() ? dev_ip : device_info.dev_name; - detect.model_id = device_info.model_id; - detect.version = device_info.version; + // The binding target is the address the user entered. device_info may still describe the + // printer the agent was connected to before, so its identity must not be used for another + // address. Split it the way the input dialog stores the print host and port, then derive + // the dev_id the same way GUI_App::select_machine does. + std::string port; + std::string host = Http::get_host_from_url(dev_ip, &port); + const std::string dev_id = MachineObject::dev_id_from_address(host, port); + + detect.dev_id = dev_id; + detect.dev_name = dev_id; + detect.model_id.clear(); + detect.version.clear(); detect.connect_type = "lan"; detect.bind_state = "free"; diff --git a/tests/slic3rutils/test_device_manager_integration.cpp b/tests/slic3rutils/test_device_manager_integration.cpp index d64b141317..11bd32cef1 100644 --- a/tests/slic3rutils/test_device_manager_integration.cpp +++ b/tests/slic3rutils/test_device_manager_integration.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "catch2/catch_message.hpp" #include "slic3r/Utils/IPrinterAgent.hpp" #include "slic3r/Utils/CloudProvider.hpp" #include @@ -193,6 +194,99 @@ 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("A non-Bambu device without a model is not given Bambu's C11 model", "[DeviceManager][integration]") +{ + const std::string agent_id = GENERATE(std::string(MOONRAKER_PRINTER_AGENT_ID), + std::string(QIDI_PRINTER_AGENT_ID), + std::string(SNAPMAKER_PRINTER_AGENT_ID), + std::string(CREALITYPRINT_PRINTER_AGENT_ID), + std::string("integration-agent")); + CAPTURE(agent_id); + + ScopedAppConfig app_config; + auto agent = std::make_shared(agent_id); + NetworkAgent network(nullptr, agent); + DeviceManager manager(&network, false, &app_config.config); + + BBLocalMachine machine; + machine.dev_id = "moonraker-device"; + machine.dev_name = "Moonraker printer"; + machine.dev_ip = "192.0.2.20"; + + MachineObject* object = manager.insert_local_device(machine, "lan", "free", "", "access-code"); + REQUIRE(object != nullptr); + CHECK(object->printer_agent_id == agent_id); + CHECK(object->printer_type.empty()); +} + +TEST_CASE("A Bambu device without a model still gets the C11 fallback", "[DeviceManager][integration]") +{ + ScopedAppConfig app_config; + auto agent = std::make_shared(BBL_PRINTER_AGENT_ID); + NetworkAgent network(nullptr, agent); + DeviceManager manager(&network, false, &app_config.config); + + BBLocalMachine machine; + machine.dev_id = "bambu-device"; + machine.dev_name = "Bambu printer"; + machine.dev_ip = "192.0.2.21"; + + MachineObject* object = manager.insert_local_device(machine, "lan", "free", "", "access-code"); + REQUIRE(object != nullptr); + CHECK(object->printer_type == "C11"); +} + +TEST_CASE("A known model survives a non-Bambu rebind that does not name one", "[DeviceManager][integration]") +{ + const std::string agent_id = GENERATE(std::string(MOONRAKER_PRINTER_AGENT_ID), + std::string(QIDI_PRINTER_AGENT_ID), + std::string(SNAPMAKER_PRINTER_AGENT_ID), + std::string(CREALITYPRINT_PRINTER_AGENT_ID), + std::string("integration-agent")); + CAPTURE(agent_id); + + ScopedAppConfig app_config; + auto agent = std::make_shared(agent_id); + NetworkAgent network(nullptr, agent); + DeviceManager manager(&network, false, &app_config.config); + + BBLocalMachine machine; + machine.dev_id = "known-model-device"; + machine.dev_name = "Known model printer"; + machine.dev_ip = "192.0.2.22"; + machine.printer_type = "test_model"; + + MachineObject* object = manager.insert_local_device(machine, "lan", "free", "", "access-code"); + REQUIRE(object != nullptr); + REQUIRE(object->printer_type == "test_model"); + + machine.printer_type.clear(); + REQUIRE(manager.insert_local_device(machine, "lan", "free", "", "access-code") == object); + CHECK(object->printer_type == "test_model"); +} + +TEST_CASE("A Bambu rebind without a model keeps the C11 fallback", "[DeviceManager][integration]") +{ + ScopedAppConfig app_config; + auto agent = std::make_shared(BBL_PRINTER_AGENT_ID); + NetworkAgent network(nullptr, agent); + DeviceManager manager(&network, false, &app_config.config); + + BBLocalMachine machine; + machine.dev_id = "known-model-device"; + machine.dev_name = "Known model printer"; + machine.dev_ip = "192.0.2.22"; + machine.printer_type = "BL-P001"; + + MachineObject* object = manager.insert_local_device(machine, "lan", "free", "", "access-code"); + REQUIRE(object != nullptr); + REQUIRE(object->printer_type == "BL-P001"); + + machine.printer_type.clear(); + REQUIRE(manager.insert_local_device(machine, "lan", "free", "", "access-code") == object); + CHECK(object->printer_type == "C11"); +} + TEST_CASE("Device manager remembers the last selected machine per printer agent", "[DeviceManager][integration]") { ScopedAppConfig app_config; diff --git a/tests/slic3rutils/test_moonraker_printer_agent.cpp b/tests/slic3rutils/test_moonraker_printer_agent.cpp index 1f6f80df0b..1e608f00d5 100644 --- a/tests/slic3rutils/test_moonraker_printer_agent.cpp +++ b/tests/slic3rutils/test_moonraker_printer_agent.cpp @@ -655,6 +655,67 @@ TEST_CASE("Moonraker discovery waits for a complete device identity", "[Moonrake REQUIRE(announcement.at("dev_name") == "Test model"); } +TEST_CASE("Binding another address does not reuse the connected printer's identity", "[MoonrakerPrinterAgent][Bind]") +{ + // The agent was last connected to .5; binding .23 must not return .5's cached identity. + DiscoveryAgent agent(""); + agent.device_info.dev_id = "192.168.1.5"; + agent.device_info.dev_ip = "192.168.1.5"; + agent.device_info.base_url = "http://192.168.1.5"; + agent.device_info.dev_name = "Qidi Q2 at .5"; + agent.device_info.model_id = "qidi_q2"; + agent.device_info.model_name = "Qidi Q2"; + agent.device_info.version = "01.02.03"; + + detectResult detect; + REQUIRE(agent.bind_detect("192.168.1.23", "secure", detect) == BAMBU_NETWORK_SUCCESS); + + CHECK(detect.dev_id == "192.168.1.23"); + CHECK(detect.dev_name == "192.168.1.23"); + CHECK(detect.model_id.empty()); + CHECK(detect.version.empty()); + CHECK(detect.connect_type == "lan"); + CHECK(detect.bind_state == "free"); +} + +TEST_CASE("A binding address is normalized like a preset's print host", "[MoonrakerPrinterAgent][Bind]") +{ + const auto [entered, expected] = GENERATE(table({ + {"192.168.1.23", "192.168.1.23"}, + {"192.168.1.23:7125", "192.168.1.23:7125"}, + {"http://192.168.1.23", "192.168.1.23"}, + {"https://192.168.1.23:7126", "192.168.1.23:7126"}, + {"http://192.168.1.23:7125/printer", "192.168.1.23:7125"}, + })); + CAPTURE(entered); + + DiscoveryAgent agent(""); + detectResult detect; + REQUIRE(agent.bind_detect(entered, "secure", detect) == BAMBU_NETWORK_SUCCESS); + CHECK(detect.dev_id == expected); +} + +TEST_CASE("Binding the connected address does not reuse cached metadata", "[MoonrakerPrinterAgent][Bind]") +{ + DiscoveryAgent agent(""); + agent.device_info.dev_id = "192.168.1.23:7125"; + agent.device_info.dev_ip = "192.168.1.23"; + agent.device_info.dev_name = "Qidi Q2"; + agent.device_info.model_id = "qidi_q2"; + agent.device_info.version = "01.02.03"; + + detectResult detect; + REQUIRE(agent.bind_detect("http://192.168.1.23:7125/printer", "secure", detect) == BAMBU_NETWORK_SUCCESS); + + CHECK(detect.dev_id == "192.168.1.23:7125"); + CHECK(detect.dev_name == "192.168.1.23:7125"); + CHECK(detect.model_id.empty()); + CHECK(detect.version.empty()); + CHECK(agent.device_info.dev_name == "Qidi Q2"); + CHECK(agent.device_info.model_id == "qidi_q2"); + CHECK(agent.device_info.version == "01.02.03"); +} + TEST_CASE("Stopping the status stream gives a silent printer no more than its timeouts", "[MoonrakerPrinterAgent][StatusStream]") { // An open websocket gets the 200 ms close timeout, and a stalled object list request up to 1 s of curl progress to see the stop.