Load saved LAN printers when the device manager first gets an agent

When the network plugin is not yet installed at startup, on_init_network builds
the DeviceManager without an agent, so its constructor skips loading the persisted
LAN printers. After the plugin is installed and the network stack hot-reloads, the
manager is reused via set_agent(), which never loaded them - so previously paired
printers stayed missing from the device list until an app restart. Load them once a
real agent first arrives.
This commit is contained in:
SoftFever
2026-07-18 15:16:06 +08:00
parent 01f2af94a1
commit 5028a5000e
2 changed files with 46 additions and 20 deletions

View File

@@ -24,27 +24,37 @@ namespace Slic3r
DevPrinterConfigUtil::InitFilePath(resources_dir());
// Load saved local machines
// Load saved local machines (needs an agent; when built without one the load is
// deferred to set_agent()).
if (agent) {
AppConfig* config = GUI::wxGetApp().app_config;
const auto local_machines = config->get_local_machines();
for (auto& it : local_machines) {
const auto& m = it.second;
MachineObject* obj = new MachineObject(this, m_agent, m.dev_name, m.dev_id, m.dev_ip);
obj->printer_type = m.printer_type;
obj->dev_connection_type = "lan";
obj->bind_state = "free";
obj->bind_sec_link = "secure";
obj->m_is_online = true;
obj->last_alive = Slic3r::Utils::get_current_time_utc();
obj->set_access_code(config->get("access_code", m.dev_id), false);
obj->set_user_access_code(config->get("user_access_code", m.dev_id), false);
if (obj->has_access_right()) {
localMachineList.insert(std::make_pair(m.dev_id, obj));
} else {
config->erase_local_machine(m.dev_id);
delete obj;
}
load_local_machines_from_config();
}
}
void DeviceManager::load_local_machines_from_config()
{
AppConfig* config = GUI::wxGetApp().app_config;
if (!config)
return;
const auto& local_machines = config->get_local_machines();
for (auto& it : local_machines) {
const auto& m = it.second;
if (localMachineList.count(m.dev_id))
continue;
MachineObject* obj = new MachineObject(this, m_agent, m.dev_name, m.dev_id, m.dev_ip);
obj->printer_type = m.printer_type;
obj->dev_connection_type = "lan";
obj->bind_state = "free";
obj->bind_sec_link = "secure";
obj->m_is_online = true;
obj->last_alive = Slic3r::Utils::get_current_time_utc();
obj->set_access_code(config->get("access_code", m.dev_id), false);
obj->set_user_access_code(config->get("user_access_code", m.dev_id), false);
if (obj->has_access_right()) {
localMachineList.insert(std::make_pair(m.dev_id, obj));
} else {
config->erase_local_machine(m.dev_id);
delete obj;
}
}
}
@@ -98,6 +108,11 @@ namespace Slic3r
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": updating agent for "
<< localMachineList.size() << " local and "
<< userMachineList.size() << " user machines";
// First real agent after an agent-less construction (network plugin wasn't ready at
// startup): run the persisted-LAN-printer load the constructor had to skip. See
// load_local_machines_from_config().
const bool first_real_agent = (m_agent == nullptr && agent != nullptr);
m_agent = agent;
std::lock_guard<std::mutex> lock(listMutex);
@@ -111,6 +126,10 @@ namespace Slic3r
it.second->set_agent(agent);
}
}
if (first_real_agent) {
load_local_machines_from_config();
}
}
void DeviceManager::EnableMultiMachine(bool enable)

View File

@@ -96,6 +96,13 @@ public:
std::map<std::string, std::vector<std::string>> device_subseries;
private:
// Load the LAN printers persisted in AppConfig into localMachineList. Runs from the
// constructor when an agent is available and, for the case where the DeviceManager was
// first built without one (network plugin not yet installed at startup), from set_agent()
// once a real agent finally arrives - so paired printers survive a plugin install/hot
// reload without an app restart.
void load_local_machines_from_config();
void keep_alive();
void check_pushing();