From 47a46010bdd9c0408429615e5284506dfe181424 Mon Sep 17 00:00:00 2001 From: tao wang Date: Wed, 28 Dec 2022 19:01:16 +0800 Subject: [PATCH] ENH:allows the user to enter a printed IP address Change-Id: I9d2617d0dd42f53e93d61e98bcf3cc97979a93f6 (cherry picked from commit 24ab56f8568e9637634e97a0632309cfd2762749) --- src/slic3r/GUI/DeviceManager.cpp | 10 ++ src/slic3r/GUI/DeviceManager.hpp | 1 + src/slic3r/GUI/Jobs/SendJob.cpp | 12 ++- src/slic3r/GUI/Jobs/SendJob.hpp | 2 + src/slic3r/GUI/MsgDialog.cpp | 150 ++++++++++++++++++++++++++ src/slic3r/GUI/MsgDialog.hpp | 26 ++++- src/slic3r/GUI/SendToPrinter.cpp | 58 ++++++++-- src/slic3r/GUI/SendToPrinter.hpp | 2 + src/slic3r/Utils/bambu_networking.hpp | 2 +- 9 files changed, 253 insertions(+), 10 deletions(-) diff --git a/src/slic3r/GUI/DeviceManager.cpp b/src/slic3r/GUI/DeviceManager.cpp index 159169d6c..07c73bcbf 100644 --- a/src/slic3r/GUI/DeviceManager.cpp +++ b/src/slic3r/GUI/DeviceManager.cpp @@ -3403,6 +3403,11 @@ void DeviceManager::on_machine_alive(std::string json_str) BOOST_LOG_TRIVIAL(debug) << "SsdpDiscovery:: Update Machine Info, printer_sn = " << dev_id << ", signal = " << printer_signal; obj->last_alive = Slic3r::Utils::get_current_time_utc(); obj->m_is_online = true; + + /* if (!obj->dev_ip.empty()) { + Slic3r::GUI::wxGetApp().app_config->set_str("ip_address", obj->dev_id, obj->dev_ip); + Slic3r::GUI::wxGetApp().app_config->save(); + }*/ } else { /* insert a new machine */ @@ -3419,6 +3424,11 @@ void DeviceManager::on_machine_alive(std::string json_str) } localMachineList.insert(std::make_pair(dev_id, obj)); + /* if (!obj->dev_ip.empty()) { + Slic3r::GUI::wxGetApp().app_config->set_str("ip_address", obj->dev_id, obj->dev_ip); + Slic3r::GUI::wxGetApp().app_config->save(); + }*/ + BOOST_LOG_TRIVIAL(debug) << "SsdpDiscovery::New Machine, ip = " << dev_ip << ", printer_name= " << dev_name << ", printer_type = " << printer_type_str << ", signal = " << printer_signal; } diff --git a/src/slic3r/GUI/DeviceManager.hpp b/src/slic3r/GUI/DeviceManager.hpp index 90245aa20..d72f1b43f 100644 --- a/src/slic3r/GUI/DeviceManager.hpp +++ b/src/slic3r/GUI/DeviceManager.hpp @@ -383,6 +383,7 @@ public: std::string access_code; std::string dev_connection_type; /* lan | cloud */ std::string connection_type() { return dev_connection_type; } + void set_dev_ip(std::string ip) {dev_ip = ip;}; bool has_access_right() { return !access_code.empty(); } void set_access_code(std::string code); bool is_lan_mode_printer(); diff --git a/src/slic3r/GUI/Jobs/SendJob.cpp b/src/slic3r/GUI/Jobs/SendJob.cpp index c15b9995e..bd16ec0c9 100644 --- a/src/slic3r/GUI/Jobs/SendJob.cpp +++ b/src/slic3r/GUI/Jobs/SendJob.cpp @@ -309,12 +309,16 @@ void SendJob::process() msg_text += wxString::Format("[%s]", error_text); } } + + if (result == BAMBU_NETWORK_ERR_WRONG_IP_ADDRESS) { + msg_text = _L("Failed uploading print file. Please enter ip address again."); + m_enter_ip_address_fun(); + } update_status(curr_percent, msg_text); BOOST_LOG_TRIVIAL(error) << "send_job: failed, result = " << result; } else { BOOST_LOG_TRIVIAL(error) << "send_job: send ok."; - //m_success_fun(); wxCommandEvent* evt = new wxCommandEvent(m_print_job_completed_id); evt->SetString(from_u8(params.project_name)); wxQueueEvent(m_plater, evt); @@ -327,6 +331,12 @@ void SendJob::on_success(std::function success) m_success_fun = success; } +void SendJob::on_enter_ip_address(std::function success) +{ + m_enter_ip_address_fun = success; +} + + void SendJob::finalize() { if (was_canceled()) return; diff --git a/src/slic3r/GUI/Jobs/SendJob.hpp b/src/slic3r/GUI/Jobs/SendJob.hpp index 53f920325..2866c94ae 100644 --- a/src/slic3r/GUI/Jobs/SendJob.hpp +++ b/src/slic3r/GUI/Jobs/SendJob.hpp @@ -21,6 +21,7 @@ class SendJob : public PlaterJob bool m_job_finished{ false }; int m_print_job_completed_id = 0; std::function m_success_fun{nullptr}; + std::function m_enter_ip_address_fun{nullptr}; protected: @@ -52,6 +53,7 @@ public: bool is_finished() { return m_job_finished; } void process() override; void on_success(std::function success); + void on_enter_ip_address(std::function success); void finalize() override; void set_project_name(std::string name); }; diff --git a/src/slic3r/GUI/MsgDialog.cpp b/src/slic3r/GUI/MsgDialog.cpp index 5cea4bd8a..596aed5bf 100644 --- a/src/slic3r/GUI/MsgDialog.cpp +++ b/src/slic3r/GUI/MsgDialog.cpp @@ -27,6 +27,7 @@ namespace Slic3r { namespace GUI { wxDEFINE_EVENT(EVT_CHECKBOX_CHANGE, wxCommandEvent); +wxDEFINE_EVENT(EVT_ENTER_IP_ADDRESS, wxCommandEvent); MsgDialog::MsgDialog(wxWindow *parent, const wxString &title, const wxString &headline, long style, wxBitmap bitmap) : DPIDialog(parent ? parent : dynamic_cast(wxGetApp().mainframe), wxID_ANY, title, wxDefaultPosition, wxSize(360, -1),wxDEFAULT_DIALOG_STYLE) @@ -421,4 +422,153 @@ void DownloadDialog::SetExtendedMessage(const wxString &extendedMessage) Fit(); } +InputIpAddressDialog::InputIpAddressDialog(wxWindow* parent, wxString name) + :DPIDialog(static_cast(wxGetApp().mainframe), wxID_ANY, _L("Unable to connect printer"), wxDefaultPosition, wxDefaultSize, wxCAPTION | wxCLOSE_BOX) +{ + std::string icon_path = (boost::format("%1%/images/BambuStudioTitle.ico") % resources_dir()).str(); + SetIcon(wxIcon(encode_path(icon_path.c_str()), wxBITMAP_TYPE_ICO)); + + SetBackgroundColour(*wxWHITE); + wxBoxSizer* m_sizer_main = new wxBoxSizer(wxVERTICAL); + auto m_line_top = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 1)); + m_line_top->SetBackgroundColour(wxColour(166, 169, 170)); + m_sizer_main->Add(m_line_top, 0, wxEXPAND, 0); + + comfirm_before_enter_text = wxString::Format(_L("Cannot detect the LAN IP address of %s. Are %s and Bambu Studio in the same LAN?"), name, name); + + comfirm_after_enter_text = _L("Please input the LAN IP address of your printer manually. You can find the IP address on device's screen, Settings > Network > IP."); + + + tip = new Label(this, comfirm_before_enter_text); + + tip->SetMinSize(wxSize(FromDIP(420), -1)); + tip->SetMaxSize(wxSize(FromDIP(420), -1)); + tip->Wrap(FromDIP(420)); + + m_input_ip = new TextInput(this, wxEmptyString, wxEmptyString); + m_input_ip->Bind(wxEVT_TEXT, &InputIpAddressDialog::on_text, this); + m_input_ip->SetMinSize(wxSize(FromDIP(420), FromDIP(28))); + m_input_ip->SetMaxSize(wxSize(FromDIP(420), FromDIP(28))); + m_input_ip->Hide(); + + auto sizer_button = new wxBoxSizer(wxHORIZONTAL); + + StateColor btn_bg_green(std::pair(wxColour(27, 136, 68), StateColor::Pressed), std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(AMS_CONTROL_BRAND_COLOUR, StateColor::Normal)); + + StateColor btn_bg_white(std::pair(wxColour(206, 206, 206), StateColor::Pressed), std::pair(wxColour(238, 238, 238), StateColor::Hovered), + std::pair(*wxWHITE, StateColor::Normal)); + + m_button_ok = new Button(this, _L("OK")); + m_button_ok->SetBackgroundColor(btn_bg_green); + m_button_ok->SetBorderColor(*wxWHITE); + m_button_ok->SetTextColor(wxColour(0xFFFFFE)); + m_button_ok->SetFont(Label::Body_12); + m_button_ok->SetSize(wxSize(FromDIP(58), FromDIP(24))); + m_button_ok->SetMinSize(wxSize(FromDIP(58), FromDIP(24))); + m_button_ok->SetCornerRadius(FromDIP(12)); + + + m_button_ok->Bind(wxEVT_LEFT_DOWN, &InputIpAddressDialog::on_ok, this); + + auto m_button_cancel = new Button(this, _L("Cancel")); + m_button_cancel->SetBackgroundColor(btn_bg_white); + m_button_cancel->SetBorderColor(wxColour(38, 46, 48)); + m_button_cancel->SetFont(Label::Body_12); + m_button_cancel->SetSize(wxSize(FromDIP(58), FromDIP(24))); + m_button_cancel->SetMinSize(wxSize(FromDIP(58), FromDIP(24))); + m_button_cancel->SetCornerRadius(FromDIP(12)); + + m_button_cancel->Bind(wxEVT_LEFT_DOWN, [this](wxMouseEvent& e) { + EndModal(wxID_CANCEL); + }); + + sizer_button->AddStretchSpacer(); + sizer_button->Add(m_button_ok, 0, wxALL, FromDIP(5)); + sizer_button->Add(m_button_cancel, 0, wxALL, FromDIP(5)); + + + m_sizer_main->Add(0, 0, 0, wxTOP, FromDIP(15)); + m_sizer_main->Add(tip, 1, wxLEFT|wxRIGHT|wxEXPAND, FromDIP(18)); + m_sizer_main->Add(0, 0, 0, wxTOP, FromDIP(5)); + m_sizer_main->Add(m_input_ip, 1, wxALIGN_CENTER, FromDIP(18)); + m_sizer_main->Add(0, 0, 0, wxTOP, FromDIP(10)); + m_sizer_main->Add(sizer_button, 1, wxLEFT|wxRIGHT|wxEXPAND, FromDIP(18)); + m_sizer_main->Add(0, 0, 0, wxTOP, FromDIP(5)); + SetSizer(m_sizer_main); + Layout(); + m_sizer_main->Fit(this); + + Centre(wxBOTH); + wxGetApp().UpdateDlgDarkUI(this); +} + +bool InputIpAddressDialog::isIp(std::string ipstr) +{ + istringstream ipstream(ipstr); + int num[4]; + char point[3]; + string end; + ipstream >> num[0] >> point[0] >> num[1] >> point[1] >> num[2] >> point[2] >> num[3] >> end; + for (int i = 0; i < 3; ++i) { + if (num[i] < 0 || num[i]>255) return false; + if (point[i] != '.') return false; + } + if (num[3] < 0 || num[3]>255) return false; + if (!end.empty()) return false; + return true; +} + +void InputIpAddressDialog::on_ok(wxMouseEvent& evt) +{ + if (!m_input_ip->IsShown()) { + tip->SetLabel(comfirm_after_enter_text); + tip->SetMinSize(wxSize(FromDIP(420), -1)); + tip->SetMaxSize(wxSize(FromDIP(420), -1)); + tip->Wrap(FromDIP(420)); + + m_input_ip->Show(); + m_button_ok->Enable(false); + m_button_ok->SetBackgroundColor(wxColour(0x90, 0x90, 0x90)); + m_button_ok->SetBorderColor(wxColour(0x90, 0x90, 0x90)); + Layout(); + Fit(); + } + else { + wxString ip = m_input_ip->GetTextCtrl()->GetValue(); + auto event = wxCommandEvent(EVT_ENTER_IP_ADDRESS); + event.SetString(ip); + event.SetEventObject(this); + wxPostEvent(this, event); + EndModal(wxID_YES); + } +} + +void InputIpAddressDialog::on_text(wxCommandEvent& evt) +{ + auto str_ip = m_input_ip->GetTextCtrl()->GetValue(); + if (isIp(str_ip.ToStdString())) { + m_button_ok->Enable(true); + StateColor btn_bg_green(std::pair(wxColour(27, 136, 68), StateColor::Pressed), std::pair(wxColour(61, 203, 115), StateColor::Hovered), + std::pair(AMS_CONTROL_BRAND_COLOUR, StateColor::Normal)); + m_button_ok->SetBorderColor(*wxWHITE); + m_button_ok->SetBackgroundColor(btn_bg_green); + } + else { + m_button_ok->Enable(false); + m_button_ok->SetBackgroundColor(wxColour(0x90, 0x90, 0x90)); + m_button_ok->SetBorderColor(wxColour(0x90, 0x90, 0x90)); + } +} + +InputIpAddressDialog::~InputIpAddressDialog() +{ + +} + +void InputIpAddressDialog::on_dpi_changed(const wxRect& suggested_rect) +{ + +} + }} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/MsgDialog.hpp b/src/slic3r/GUI/MsgDialog.hpp index 7cdcdeb4d..7f07cfbd6 100644 --- a/src/slic3r/GUI/MsgDialog.hpp +++ b/src/slic3r/GUI/MsgDialog.hpp @@ -3,7 +3,6 @@ #include #include - #include "GUI_Utils.hpp" #include #include @@ -14,6 +13,7 @@ #include #include "Widgets/Button.hpp" #include "Widgets/CheckBox.hpp" +#include "Widgets/TextInput.hpp" #include "BBLStatusBar.hpp" #include "BBLStatusBarSend.hpp" @@ -92,7 +92,7 @@ protected: MsgButtonsHash m_buttons; CheckBox* m_checkbox_dsa{nullptr}; }; -wxDECLARE_EVENT(EVT_CHECKBOX_CHANGE, wxCommandEvent); + // Generic error dialog, used for displaying exceptions class ErrorDialog : public MsgDialog @@ -371,6 +371,28 @@ private: wxString msg; }; +class InputIpAddressDialog: public DPIDialog +{ +public: + wxString comfirm_before_enter_text; + wxString comfirm_after_enter_text; + std::string m_ip; + Label* tip{nullptr}; + InputIpAddressDialog(wxWindow* parent = nullptr, wxString name = wxEmptyString); + ~InputIpAddressDialog(); + + Button* m_button_ok{nullptr}; + TextInput* m_input_ip{nullptr}; + bool isIp(std::string ipstr); + void on_ok(wxMouseEvent& evt); + void on_text(wxCommandEvent& evt); + void on_dpi_changed(const wxRect& suggested_rect) override; +}; + + +wxDECLARE_EVENT(EVT_CHECKBOX_CHANGE, wxCommandEvent); +wxDECLARE_EVENT(EVT_ENTER_IP_ADDRESS, wxCommandEvent); + } } diff --git a/src/slic3r/GUI/SendToPrinter.cpp b/src/slic3r/GUI/SendToPrinter.cpp index b5fa7ba4a..137b6779a 100644 --- a/src/slic3r/GUI/SendToPrinter.cpp +++ b/src/slic3r/GUI/SendToPrinter.cpp @@ -30,6 +30,7 @@ namespace GUI { wxDEFINE_EVENT(EVT_UPDATE_USER_MACHINE_LIST, wxCommandEvent); wxDEFINE_EVENT(EVT_PRINT_JOB_CANCEL, wxCommandEvent); wxDEFINE_EVENT(EVT_SEND_JOB_SUCCESS, wxCommandEvent); +wxDEFINE_EVENT(EVT_CLEAR_IPADDRESS, wxCommandEvent); void SendToPrinterDialog::stripWhiteSpace(std::string& str) @@ -531,6 +532,7 @@ void SendToPrinterDialog::init_model() void SendToPrinterDialog::init_bind() { Bind(wxEVT_TIMER, &SendToPrinterDialog::on_timer, this); + Bind(EVT_CLEAR_IPADDRESS, &SendToPrinterDialog::clear_ip_address_config, this); } void SendToPrinterDialog::init_timer() @@ -651,12 +653,9 @@ void SendToPrinterDialog::on_ok(wxCommandEvent &event) m_send_job->has_sdcard = obj_->has_sdcard(); m_send_job->set_project_name(m_current_project_name.utf8_string()); - - m_send_job->on_success([this]() { - //enable_prepare_mode = true;enable_prepare_mode - m_status_bar->reset(); - prepare_mode(); - //EndModal(wxID_CLOSE); + m_send_job->on_enter_ip_address([this, obj_]() { + wxCommandEvent* evt = new wxCommandEvent(EVT_CLEAR_IPADDRESS); + wxQueueEvent(this, evt); }); enable_prepare_mode = false; @@ -664,6 +663,29 @@ void SendToPrinterDialog::on_ok(wxCommandEvent &event) BOOST_LOG_TRIVIAL(info) << "send_job: send print job"; } +void SendToPrinterDialog::clear_ip_address_config(wxCommandEvent& e) +{ + DeviceManager* dev = Slic3r::GUI::wxGetApp().getDeviceManager(); + if (!dev) return; + if (!dev->get_selected_machine()) return; + auto obj = dev->get_selected_machine(); + Slic3r::GUI::wxGetApp().app_config->set_str("ip_address", obj->dev_id, ""); + Slic3r::GUI::wxGetApp().app_config->save(); + + InputIpAddressDialog dlg(this, from_u8(dev->get_selected_machine()->dev_name)); + dlg.Bind(EVT_ENTER_IP_ADDRESS, [this, obj](wxCommandEvent& e) { + auto ip_address = e.GetString(); + BOOST_LOG_TRIVIAL(info) << "User enter IP address is " << ip_address; + if (!ip_address.empty()) { + wxGetApp().app_config->set_str("ip_address", obj->dev_id, ip_address.ToStdString()); + wxGetApp().app_config->save(); + obj->dev_ip = ip_address.ToStdString(); + } + + }); + dlg.ShowModal(); +} + void SendToPrinterDialog::update_user_machine_list() { NetworkAgent* m_agent = wxGetApp().getAgent(); @@ -845,6 +867,30 @@ void SendToPrinterDialog::on_selection_changed(wxCommandEvent &event) return; } + //check ip address + if (obj->dev_ip.empty()) { + BOOST_LOG_TRIVIAL(info) << "MachineObject IP is empty "; + std::string app_config_dev_ip = Slic3r::GUI::wxGetApp().app_config->get("ip_address", obj->dev_id); + + if (app_config_dev_ip.empty()) { + InputIpAddressDialog dlg(this, from_u8(obj->dev_name)); + dlg.Bind(EVT_ENTER_IP_ADDRESS, [this, obj, app_config_dev_ip](wxCommandEvent& e) { + auto ip_address = e.GetString(); + BOOST_LOG_TRIVIAL(info) << "User enter IP address is "<< ip_address; + if (!ip_address.empty()) { + wxGetApp().app_config->set_str("ip_address", obj->dev_id, ip_address.ToStdString()); + wxGetApp().app_config->save(); + obj->dev_ip = ip_address.ToStdString(); + } + + }); + dlg.ShowModal(); + } + else { + obj->dev_ip = app_config_dev_ip; + } + } + update_show_status(); } diff --git a/src/slic3r/GUI/SendToPrinter.hpp b/src/slic3r/GUI/SendToPrinter.hpp index ced9ce2a4..85d200585 100644 --- a/src/slic3r/GUI/SendToPrinter.hpp +++ b/src/slic3r/GUI/SendToPrinter.hpp @@ -157,6 +157,7 @@ protected: void update_printer_combobox(wxCommandEvent& event); void on_cancel(wxCloseEvent& event); void on_ok(wxCommandEvent& event); + void clear_ip_address_config(wxCommandEvent& e); void on_refresh(wxCommandEvent& event); void on_print_job_cancel(wxCommandEvent& evt); void set_default(); @@ -170,6 +171,7 @@ protected: std::vector sort_string(std::vector strArray); }; +wxDECLARE_EVENT(EVT_CLEAR_IPADDRESS, wxCommandEvent); } } diff --git a/src/slic3r/Utils/bambu_networking.hpp b/src/slic3r/Utils/bambu_networking.hpp index 939f4f5d3..35e0c081d 100644 --- a/src/slic3r/Utils/bambu_networking.hpp +++ b/src/slic3r/Utils/bambu_networking.hpp @@ -35,7 +35,7 @@ namespace BBL { #define BAMBU_NETWORK_ERR_GET_MODEL_PUBLISH_PAGE -26 #define BAMBU_NETWORK_ERR_GET_MODEL_MALL_HOME_PAGE -27 #define BAMBU_NETWORK_ERR_GET_USER_INFO -28 - +#define BAMBU_NETWORK_ERR_WRONG_IP_ADDRESS -29 #define BAMBU_NETWORK_LIBRARY "bambu_networking" #define BAMBU_NETWORK_AGENT_NAME "bambu_network_agent"