refactor: connect_printer api and dialog

This commit is contained in:
Ian Chua committed 2026-09-22 21:54:10 +08:00
1 parent 9e34ca536b
commit cda25c3715
16 files changed
+125 -32

No files matched your search

+10 -7
View File
@@ -1,5 +1,6 @@
#include "MoonrakerPrinterAgent.hpp"
#include "Http.hpp"
#include "IPrinterAgent.hpp"
#include "libslic3r/Preset.hpp"
#include "libslic3r/PresetBundle.hpp"
#include "slic3r/GUI/GUI_App.hpp"
@@ -136,9 +137,9 @@ int MoonrakerPrinterAgent::send_message_to_printer(std::string dev_id, std::stri
return handle_request(dev_id, json_str);
}
int MoonrakerPrinterAgent::connect_printer(std::string dev_id, std::string dev_ip, std::string username, std::string password, bool use_ssl)
int MoonrakerPrinterAgent::connect_printer(const PrinterConnectionParams& params)
{
if (dev_id.empty() || dev_ip.empty()) {
if (params.dev_id.empty() || params.host.empty()) {
BOOST_LOG_TRIVIAL(error) << "MoonrakerPrinterAgent: connect_printer missing dev_id or dev_ip";
return BAMBU_NETWORK_ERR_INVALID_HANDLE;
}
@@ -148,7 +149,7 @@ int MoonrakerPrinterAgent::connect_printer(std::string dev_id, std::string dev_i
uint64_t gen;
{
std::lock_guard<std::recursive_mutex> lock(connect_mutex);
init_device_info(dev_id, dev_ip, username, password, use_ssl);
init_device_info(params.dev_id, params.host, params.username, params.password, params.use_ssl, params.port);
gen = ++connect_generation;
base_url = device_info.base_url;
api_key = device_info.api_key;
@@ -170,7 +171,7 @@ int MoonrakerPrinterAgent::connect_printer(std::string dev_id, std::string dev_i
// Launch connection in background thread (capture by value to avoid data races)
{
std::lock_guard<std::recursive_mutex> lock(connect_mutex);
connect_thread = std::thread([this, dev_id, base_url, api_key, gen]() { perform_connection_async(dev_id, base_url, api_key, gen); });
connect_thread = std::thread([this, params, base_url, api_key, gen]() { perform_connection_async(params.dev_id, base_url, api_key, gen); });
}
return BAMBU_NETWORK_SUCCESS;
@@ -1040,7 +1041,7 @@ int MoonrakerPrinterAgent::handle_request(const std::string& dev_id, const std::
return BAMBU_NETWORK_SUCCESS;
}
bool MoonrakerPrinterAgent::init_device_info(std::string dev_id, std::string dev_ip, std::string username, std::string password, bool use_ssl)
bool MoonrakerPrinterAgent::init_device_info(const std::string& dev_id, const std::string& dev_ip, const std::string& username, const std::string& password, bool use_ssl, const std::string& port)
{
device_info = MoonrakerDeviceInfo{};
auto* preset_bundle = GUI::wxGetApp().preset_bundle;
@@ -1050,12 +1051,14 @@ bool MoonrakerPrinterAgent::init_device_info(std::string dev_id, std::string dev
auto& preset = preset_bundle->printers.get_edited_preset();
const auto& printer_cfg = preset.config;
device_info.dev_ip = dev_ip;
device_info.dev_ip = dev_ip;
device_info.api_key = password;
device_info.model_name = printer_cfg.opt_string("printer_model");
device_info.model_id = preset.get_printer_type(preset_bundle);
device_info.base_url = use_ssl ? "https://" + dev_ip : "http://" + dev_ip;
device_info.base_url = normalize_base_url(dev_ip, port);
if (use_ssl && boost::istarts_with(device_info.base_url, "http://"))
device_info.base_url.replace(0, 7, "https://");
device_info.dev_id = dev_id;
device_info.version = "";
device_info.dev_name = device_info.dev_id;