Files
OrcaSlicer/tests/slic3rutils/test_device_access.cpp
HanifKoh dda4a89315 Discover Printers on Every Printer Agent and List Them All in the Printer Picker (#17970)
* Discover Printers on Every Printer Agent at Once

Every printer agent that scans the LAN now discovers in the background, not
only the live one, and each device is recorded under the agent that found it.
A discovery no longer takes over a device another agent owns. The
disabled_discovery_agents setting turns an agent's scan off.

* List Every Printer Agent's Printers in the Printer Picker, Greying Out the Ones the Preset Cannot Use

The picker now lists the printers of every agent under My Device and Other Device, the live agent's first and the others grouped in registration order, without headings. Printers of other agents are greyed out, ignore hover and clicks, and tell the user to select a preset to use them.

* Turn Each Printer Agent's Network Discovery On or Off in Preferences

The Online tab gets a Printer discovery section with a "Find <agent> printers" checkbox for each printer agent that scans the LAN: the Orca agent, the Bambu Lab agent while its network plug-in is loaded, and any agent already created that scans, such as a Python agent once it is in use. Unticking one adds its id to disabled_discovery_agents and restarts discovery at once; the printer list still searches with the live agent while it is open. The setting is also in the preference table, so preferences_get lists it and preferences_set writes it as text.

* Discover on Every Registered Printer Agent From Startup

A printer agent that scans now discovers from startup without a preset having selected it. Discovery used to consider only the agents registered with a scans_on_lan flag (Orca and Bambu Lab) plus the ones already created, so a Python agent that returned True from discovers_on_lan(), or a C++ agent registered without the flag, found nothing until it had been selected once. Every registered agent is now a candidate and discovers_on_lan() alone decides, so the flag is gone and cannot disagree with it. Creating the candidates is cheap: the built-in constructors only store their arguments, and a Python agent's factory returns the capability its plugin already loaded.

* Stop a Python Printer Agent From Calling Into Its Plugin Once It Starts to Unload

When a plugin is unloaded off the UI thread, as a plugin update does, its unload callbacks are deferred to the UI thread while its module is released at once, so the deferred deregistration and any discovery refresh in between could still call the printer agent. A capability is now marked as unloading before its module is released, and every Python printer-agent call made after the mark answers as if no agent were set instead of reaching the plugin. A call takes its reference before checking the mark, so the unload's drain waits for any call that got past it.

* Disconnect Only the Printer Agents That Were Selected

Discovery now creates every registered agent, so the agent cache holds agents that never connected. At shutdown and when a Python plugin is reloaded or unloaded, the cache disconnects only the agents that create_printer_agent_by_id() handed out to be selected, so a plugin is not asked to disconnect a printer it never connected to.

* Disconnect a Re-Registered Python Agent's Old Selected Agent and Find the Live Agent by Its Stored Id

When a Python printer agent registers again under a new id, the agent cached under its old id is taken out through the same helper as the new one, so its "selected" mark goes with it and both are disconnected if they were ever selected; before, the old one could be dropped without a disconnect and its mark could pass to a later agent under that id. Deregistration now finds the live agent by the id NetworkAgent stored when the agent went live, since an unloading agent no longer reports its own. The lifecycle test plugin now answers discovers_on_lan and disconnect_printer, so all three calls after the unload show the guard at work.

* Create Every Printer Agent When Networking Starts Instead of When Discovery Asks

get_discovery_candidates() created any registered agent that was not cached yet, so creating agents was a side effect of asking for the list. The built-in agents are now created by NetworkAgentFactory::create_printer_agents() when on_init_network() builds networking, which also hands the cached agents the new cloud agents when networking is rebuilt, and a Python agent is cached when its plugin registers it. get_discovery_candidates() only reads the cache. Behaviour is the same: every registered agent exists from startup, and discovers_on_lan() decides which ones scan.
2026-11-30 16:26:29 +08:00

160 lines
5.6 KiB
C++

#ifdef WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#endif
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <memory>
#include "libslic3r/AppConfig.hpp"
#include "slic3r/GUI/DeviceCore/DevManager.h"
#include "slic3r/GUI/DeviceManager.hpp"
#include "slic3r/Utils/IPrinterAgent.hpp"
#include "slic3r/Utils/NetworkAgentFactory.hpp"
#include "slic3r/Utils/MoonrakerPrinterAgent.hpp"
#include "slic3r/Utils/NetworkAgent.hpp"
using namespace Slic3r;
namespace {
class RecordingAgent : public MoonrakerPrinterAgent
{
public:
RecordingAgent() : MoonrakerPrinterAgent("") {}
int connections = 0;
std::string port;
int connect_printer(const PrinterConnectionParams& params) override
{
++connections;
port = params.port;
return 0;
}
int disconnect_printer() override { return 0; }
};
} // namespace
TEST_CASE("Moonraker permits an empty access code", "[DeviceAccess]")
{
MachineObject machine(nullptr, nullptr, "test", "test_dev", "127.0.0.1");
machine.printer_agent_id = MOONRAKER_PRINTER_AGENT_ID;
machine.set_access_code("", false);
REQUIRE(machine.has_access_right());
}
TEST_CASE("Moonraker logout revokes access until explicitly rebound", "[DeviceAccess]")
{
MachineObject machine(nullptr, nullptr, "test", "test_dev", "127.0.0.1");
machine.printer_agent_id = MOONRAKER_PRINTER_AGENT_ID;
const std::string code = GENERATE("", "configured-key");
machine.set_access_code(code, false);
REQUIRE(machine.has_access_right());
machine.revoke_access();
REQUIRE(machine.get_access_code().empty());
REQUIRE_FALSE(machine.has_access_right());
machine.set_access_code(code, false);
REQUIRE(machine.has_access_right());
REQUIRE(machine.get_access_code() == code);
}
TEST_CASE("Revoked access prevents calls to the printer agent", "[DeviceAccess]")
{
auto agent = std::make_shared<RecordingAgent>();
NetworkAgent network(nullptr, agent);
MachineObject machine(nullptr, &network, "test", "test_dev", "127.0.0.1");
machine.printer_agent_id = GENERATE(BBL_PRINTER_AGENT_ID, ORCA_PRINTER_AGENT_ID, MOONRAKER_PRINTER_AGENT_ID);
machine.set_access_code("configured-key", false);
REQUIRE(machine.connect() == 0);
REQUIRE(agent->connections == 1);
machine.revoke_access();
REQUIRE(machine.connect() == -1);
REQUIRE(agent->connections == 1);
machine.set_access_code("configured-key", false);
REQUIRE(machine.connect() == 0);
REQUIRE(agent->connections == 2);
}
TEST_CASE("A saved print host without a port connects on the port in its dev_id", "[DeviceAccess]")
{
auto agent = std::make_shared<RecordingAgent>();
NetworkAgent network(nullptr, agent);
MachineObject machine(nullptr, &network, "test", "192.0.2.10:7125", "192.0.2.10");
machine.set_access_code("configured-key", false);
SECTION("Agents that connect like Moonraker repair the record")
{
machine.printer_agent_id = GENERATE(as<std::string>{}, MOONRAKER_PRINTER_AGENT_ID, QIDI_PRINTER_AGENT_ID,
SNAPMAKER_PRINTER_AGENT_ID, CREALITYPRINT_PRINTER_AGENT_ID);
REQUIRE(machine.connect() == 0);
REQUIRE(agent->port == "7125");
}
SECTION("Other agents keep the saved address as it is")
{
machine.printer_agent_id = BBL_PRINTER_AGENT_ID;
REQUIRE(machine.connect() == 0);
REQUIRE(agent->port.empty());
}
}
TEST_CASE("Other printer agents require an access code", "[DeviceAccess]")
{
MachineObject machine(nullptr, nullptr, "test", "test_dev", "127.0.0.1");
machine.printer_agent_id = GENERATE(
BBL_PRINTER_AGENT_ID, ORCA_PRINTER_AGENT_ID, QIDI_PRINTER_AGENT_ID, SNAPMAKER_PRINTER_AGENT_ID, CREALITYPRINT_PRINTER_AGENT_ID, "plugin",
"unknown");
machine.set_access_code("", false);
REQUIRE_FALSE(machine.has_access_right());
machine.set_access_code("88888888", false);
REQUIRE(machine.has_access_right());
}
TEST_CASE("Rebinding Moonraker with an empty key saves the machine", "[DeviceAccess]")
{
AppConfig config;
DeviceManager manager(nullptr, false, &config);
MachineObject machine(&manager, nullptr, "test", "test_dev", "127.0.0.1");
machine.printer_agent_id = MOONRAKER_PRINTER_AGENT_ID;
machine.dev_connection_type = "lan";
machine.revoke_access();
machine.set_access_code("");
const auto& machines = config.get_local_machines();
REQUIRE(machines.count("test_dev") == 1);
REQUIRE(machines.at("test_dev").printer_agent_id == MOONRAKER_PRINTER_AGENT_ID);
REQUIRE(machines.at("test_dev").access_code.empty());
}
TEST_CASE("Moonraker discovery leaves another agent's saved machine alone", "[DeviceAccess]")
{
AppConfig config;
NetworkAgent network(nullptr, std::make_shared<MoonrakerPrinterAgent>(""));
DeviceManager manager(&network, false, &config);
const std::string owner = GENERATE(as<std::string>{}, ORCA_PRINTER_AGENT_ID, MOONRAKER_PRINTER_AGENT_ID);
BBLocalMachine saved;
saved.dev_id = "192.0.2.10:7125";
saved.dev_ip = saved.dev_id;
saved.printer_agent_id = owner;
config.update_local_machine(saved);
manager.on_machine_alive(R"({"dev_name":"Klipper","dev_id":"192.0.2.10:7125","dev_ip":"192.0.2.10:7125",
"dev_type":"","dev_signal":"0","connect_type":"lan","bind_state":"free"})",
MOONRAKER_PRINTER_AGENT_ID);
REQUIRE((manager.get_local_machine(saved.dev_id) != nullptr) == (owner == MOONRAKER_PRINTER_AGENT_ID));
}