From aa52c99076a78e2c8fd8d4e4b0b64de0bc761469 Mon Sep 17 00:00:00 2001 From: milk Date: Mon, 21 Apr 2025 21:00:14 +0800 Subject: [PATCH] ENH: Optimization of File Transfer System Part1 jira: [STUDIO-11777] Change-Id: I733fd3532caa19546763ab8a72eb7667d5ffec53 --- src/slic3r/GUI/DeviceManager.cpp | 23 ++++ src/slic3r/GUI/DeviceManager.hpp | 2 + src/slic3r/GUI/Printer/PrinterFileSystem.cpp | 8 +- src/slic3r/GUI/Printer/PrinterFileSystem.h | 1 + src/slic3r/GUI/SendToPrinter.cpp | 120 ++++++++++++++----- src/slic3r/GUI/SendToPrinter.hpp | 4 +- 6 files changed, 124 insertions(+), 34 deletions(-) diff --git a/src/slic3r/GUI/DeviceManager.cpp b/src/slic3r/GUI/DeviceManager.cpp index 315c1fc75..7fe9e8fbe 100644 --- a/src/slic3r/GUI/DeviceManager.cpp +++ b/src/slic3r/GUI/DeviceManager.cpp @@ -593,6 +593,13 @@ bool MachineObject::is_lan_mode_printer() return result; } +std::string MachineObject::convertToIp(long long ip) +{ + std::stringstream ss; + ss << ((ip >> 0) & 0xFF) << "." << ((ip >> 8) & 0xFF) << "." << ((ip >> 16) & 0xFF) << "." << ((ip >> 24) & 0xFF); + return ss.str(); +} + PrinterSeries MachineObject::get_printer_series() const { std::string series = DeviceManager::get_printer_series(printer_type); @@ -3797,8 +3804,24 @@ int MachineObject::parse_json(std::string payload, bool key_field_only) if (jj["net"].contains("conf")) { network_wired = (jj["net"]["conf"].get() & (0x1)) != 0; } + if (jj["net"].contains("info")) { + for (auto info_item = jj["net"]["info"].begin(); info_item != jj["net"]["info"].end(); info_item++) { + + if (info_item->contains("ip")) { + auto tmp_dev_ip = (*info_item)["ip"].get(); + if (tmp_dev_ip == 0) + continue ; + else { + set_dev_ip(convertToIp(tmp_dev_ip)); + } + } else { + break; + } + } + } } } + #pragma endregion #pragma region online diff --git a/src/slic3r/GUI/DeviceManager.hpp b/src/slic3r/GUI/DeviceManager.hpp index 54578ad23..f1ab4ab4a 100644 --- a/src/slic3r/GUI/DeviceManager.hpp +++ b/src/slic3r/GUI/DeviceManager.hpp @@ -647,6 +647,7 @@ public: int subscribe_counter{3}; std::string dev_connection_type; /* lan | cloud */ std::string connection_type() { return dev_connection_type; } + std::string dev_connection_name; /* lan | eth */ void set_dev_ip(std::string ip) {dev_ip = ip;} std::string get_ftp_folder(); @@ -658,6 +659,7 @@ public: void erase_user_access_code(); std::string get_user_access_code(); bool is_lan_mode_printer(); + std::string convertToIp(long long ip); //PRINTER_TYPE printer_type = PRINTER_3DPrinter_UKNOWN; std::string printer_type; /* model_id */ diff --git a/src/slic3r/GUI/Printer/PrinterFileSystem.cpp b/src/slic3r/GUI/Printer/PrinterFileSystem.cpp index b00c4e3a6..5abf58a45 100644 --- a/src/slic3r/GUI/Printer/PrinterFileSystem.cpp +++ b/src/slic3r/GUI/Printer/PrinterFileSystem.cpp @@ -1528,7 +1528,7 @@ void PrinterFileSystem::Reconnect(boost::unique_lock &l, int resul if (m_session.owner == nullptr) return; json r; - while (!m_callbacks.empty()) { + while(!m_callbacks.empty()) { auto c = m_callbacks.front(); m_callbacks.pop_front(); ++m_sequence; @@ -1541,6 +1541,8 @@ void PrinterFileSystem::Reconnect(boost::unique_lock &l, int resul while (m_stopped) { if (m_session.owner == nullptr) return; + m_status = Status::Stopped; + SendChangedEvent(EVT_STATUS_CHANGED, m_status); m_cond.wait(l); } wxLogMessage("PrinterFileSystem::Reconnect Initializing"); @@ -1567,6 +1569,7 @@ void PrinterFileSystem::Reconnect(boost::unique_lock &l, int resul Bambu_Tunnel tunnel = nullptr; int ret = Bambu_Create(&tunnel, url.c_str()); if (ret == 0) { + Bambu_SetLogger(tunnel, DumpLog, this); ret = Bambu_Open(tunnel); } @@ -1575,7 +1578,7 @@ void PrinterFileSystem::Reconnect(boost::unique_lock &l, int resul ret = Bambu_StartStreamEx ? Bambu_StartStreamEx(tunnel, CTRL_TYPE) : Bambu_StartStream(tunnel, false); - if (ret == Bambu_would_block) + if (ret == Bambu_would_block) {} boost::this_thread::sleep(boost::posix_time::milliseconds(100)); } while (ret == Bambu_would_block && !m_stopped); l.lock(); @@ -1595,6 +1598,7 @@ void PrinterFileSystem::Reconnect(boost::unique_lock &l, int resul } wxLogMessage("PrinterFileSystem::Reconnect Failed"); m_status = Status::Failed; + SendChangedEvent(EVT_STATUS_CHANGED, m_status, "", url.size() < 2 ? 1 : m_last_error); m_cond.timed_wait(l, boost::posix_time::seconds(10)); } diff --git a/src/slic3r/GUI/Printer/PrinterFileSystem.h b/src/slic3r/GUI/Printer/PrinterFileSystem.h index 86171d9e8..d9dbe1cdc 100644 --- a/src/slic3r/GUI/Printer/PrinterFileSystem.h +++ b/src/slic3r/GUI/Printer/PrinterFileSystem.h @@ -215,6 +215,7 @@ public: ListSyncing, ListReady, Failed, + Stopped, }; Status GetStatus() const { return m_status; } diff --git a/src/slic3r/GUI/SendToPrinter.cpp b/src/slic3r/GUI/SendToPrinter.cpp index 2ba9240b2..7855d683b 100644 --- a/src/slic3r/GUI/SendToPrinter.cpp +++ b/src/slic3r/GUI/SendToPrinter.cpp @@ -749,7 +749,7 @@ void SendToPrinterDialog::on_cancel(wxCloseEvent &event) } } #endif - + m_tcp_try_connect = true; this->EndModal(wxID_CANCEL); } @@ -778,7 +778,6 @@ void SendToPrinterDialog::on_ok(wxCommandEvent &event) } assert(obj_->dev_id == m_printer_last_select); - BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ", print_job: for send task, current printer id = " << m_printer_last_select << std::endl; show_status(PrintDialogStatus::PrintStatusSending); @@ -1127,7 +1126,7 @@ void SendToPrinterDialog::on_selection_changed(wxCommandEvent &event) auto selection = m_comboBox_printer->GetSelection(); DeviceManager* dev = Slic3r::GUI::wxGetApp().getDeviceManager(); if (!dev) return; - + m_tcp_try_connect = true; MachineObject* obj = nullptr; for (int i = 0; i < m_list.size(); i++) { if (i == selection) { @@ -1237,7 +1236,7 @@ void SendToPrinterDialog::update_show_status() return; } #else - if (obj_->connection_type() == "lan" || !obj_->is_support_brtc) { + if (!obj_->is_support_brtc) { if (m_file_sys) { m_device_select.clear(); m_file_sys->Stop(true); @@ -1245,7 +1244,7 @@ void SendToPrinterDialog::update_show_status() } show_status(PrintDialogStatus::PrintStatusReadingFinished); return; - } else if (obj_->connection_type() == "cloud") { + } else/* if (obj_->connection_type() == "cloud")*/ { Enable(obj_ && obj_->is_connected()); std::string dev_id = obj_->dev_ip; if (m_file_sys) { @@ -1270,6 +1269,7 @@ void SendToPrinterDialog::update_show_status() wxString msg; int status = e.GetInt(); int extra = e.GetExtraLong(); + switch (status) { case PrinterFileSystem::Initializing: case PrinterFileSystem::Connecting: show_status(PrintDialogStatus::PrintStatusReading); break; @@ -1291,7 +1291,21 @@ void SendToPrinterDialog::update_show_status() break; } - case PrinterFileSystem::Failed: msg = _L("Please check the network and try again, You can restart or update the printer if the issue persists."); break; + case PrinterFileSystem::Failed: { + m_tcp_try_connect = false; + /*static int i = 0; + OutputDebugStringA(std::to_string(i).c_str()); + OutputDebugStringA("\n"); + if (i++ < 2)*/ + /* msg = _L("Attempting TCP connection, please wait."); + else*/ + msg = _L("Please check the network and try again, You can restart or update the printer if the issue persists."); + //fs->Stop(); + break; + } + case PrinterFileSystem::Stopped: { + // fs->Retry(); + } } if (!msg.empty()) { @@ -1457,6 +1471,7 @@ void SendToPrinterDialog::show_status(PrintDialogStatus status, std::vectorStart(LIST_REFRESH_INTERVAL); } else { m_refresh_timer->Stop(); + m_tcp_try_connect = true; } Layout(); @@ -1732,31 +1750,71 @@ void SendToPrinterDialog::fetchUrl(boost::weak_ptr wfs) NetworkAgent *agent = wxGetApp().getAgent(); std::string agent_version = agent ? agent->get_version() : ""; - if (agent) { - std::string protocols[] = {"", "\"tutk\"", "\"agora\"", "\"tutk\",\"agora\""}; - agent->get_camera_url(obj->dev_id + "|" + dev_ver + "|" + protocols[remote_proto], - [this, wfs, m = dev_id, v = agent->get_version(), dv = dev_ver](std::string url) { - if (boost::algorithm::starts_with(url, "bambu:///")) { - url += "&device=" + m; - url += "&net_ver=" + v; - url += "&dev_ver=" + dv; - url += "&refresh_url=" + boost::lexical_cast(&refresh_agora_url); - url += "&cli_id=" + wxGetApp().app_config->get("slicer_uuid"); - url += "&cli_ver=" + std::string(SLIC3R_VERSION); - } - BOOST_LOG_TRIVIAL(info) << "SendToPrinter::fetchUrl: camera_url: " << hide_passwd(url, {"?uid=", "authkey=", "passwd="}); - std::cout << "SendToPrinter::fetchUrl: camera_url: " << hide_passwd(url, {"?uid=", "authkey=", "passwd="}); - CallAfter([=] { - boost::shared_ptr fs(wfs.lock()); - if (!fs) return; - if (boost::algorithm::starts_with(url, "bambu:///")) { - fs->SetUrl(url); - } else { - fs->SetUrl("3"); - } - }); - }); - } + + if (agent) { + if (obj->connection_type() == "lan") { + // In LAN mode, use TCP connection + std::string devIP = obj->dev_ip; + std::string accessCode = obj->get_access_code(); + std::string tcp_url = "bambu:///local/" + devIP + "?port=6000&user=" + "bblp" + "&passwd=" + accessCode; + + CallAfter([=] { + boost::shared_ptr fs(wfs.lock()); + if (!fs) return; + if (boost::algorithm::starts_with(tcp_url, "bambu:///")) { + fs->SetUrl(tcp_url); + } else { + fs->SetUrl("3"); + } + }); + } else { + // In non-LAN mode, try TCP connection first + if (m_tcp_try_connect) { + std::string devIP = obj->dev_ip; + std::string accessCode = obj->get_access_code(); + std::string tcp_url = "bambu:///local/" + devIP + "?port=6000&user=" + "bblp" + "&passwd=" + accessCode; + + CallAfter([=] { + boost::shared_ptr fs(wfs.lock()); + if (!fs) return; + + if (boost::algorithm::starts_with(tcp_url, "bambu:///")) { + fs->SetUrl(tcp_url); + } + }); + } + else { + // If the TCP connection fails, switch to TUTK connection + std::string protocols[] = {"", "\"tutk\"", "\"agora\"", "\"tutk\",\"agora\""}; + agent->get_camera_url(obj->dev_id + "|" + dev_ver + "|" + protocols[1], // สนำร "tutk" + [this, wfs, m = dev_id, v = agent->get_version(), dv = dev_ver](std::string url) { + if (boost::algorithm::starts_with(url, "bambu:///")) { + url += "&device=" + m; + url += "&net_ver=" + v; + url += "&dev_ver=" + dv; + url += "&refresh_url=" + boost::lexical_cast(&refresh_agora_url); + url += "&cli_id=" + wxGetApp().app_config->get("slicer_uuid"); + url += "&cli_ver=" + std::string(SLIC3R_VERSION); + } + BOOST_LOG_TRIVIAL(info) << "SendToPrinter::fetchUrl: camera_url: " << hide_passwd(url, {"?uid=", "authkey=", "passwd="}); + std::cout << "SendToPrinter::fetchUrl: camera_url: " << hide_passwd(url, {"?uid=", "authkey=", "passwd="}); + CallAfter([=] { + boost::shared_ptr fs(wfs.lock()); + if (!fs) return; + if (boost::algorithm::starts_with(url, "bambu:///")) { + fs->SetUrl(url); + } else { + fs->SetUrl("3"); + } + }); + }); + + } + + } + + } + return; } diff --git a/src/slic3r/GUI/SendToPrinter.hpp b/src/slic3r/GUI/SendToPrinter.hpp index ceaca658f..843945bdd 100644 --- a/src/slic3r/GUI/SendToPrinter.hpp +++ b/src/slic3r/GUI/SendToPrinter.hpp @@ -49,6 +49,7 @@ private: void init_bind(); void init_timer(); + int m_print_plate_idx; int m_current_filament_id; int m_print_error_code = 0; @@ -59,6 +60,7 @@ private: bool m_need_adaptation_screen{ false }; bool m_export_3mf_cancel{ false }; bool m_is_canceled{ false }; + bool m_tcp_try_connect{true}; std::string m_print_error_msg; std::string m_print_error_extra; std::string m_print_info; @@ -126,7 +128,7 @@ private: bool m_waiting_enable{ false }; boost::shared_ptr m_file_sys; std::vector m_ability_list; - + public: SendToPrinterDialog(Plater* plater = nullptr); ~SendToPrinterDialog();