From 62c66379766ce1cbb5b3a4ff1d51a1a0263b1256 Mon Sep 17 00:00:00 2001 From: Boris Pruessmann Date: Sun, 14 Jun 2020 11:01:55 +0200 Subject: [PATCH] Support for selecting printer (slug) --- src/libslic3r/PrintConfig.cpp | 1 + src/slic3r/GUI/PrintHostDialogs.cpp | 2 +- src/slic3r/GUI/Tab.cpp | 69 +++++++++++++++++---------- src/slic3r/GUI/Tab.hpp | 2 + src/slic3r/Utils/AstroBox.hpp | 5 +- src/slic3r/Utils/Duet.hpp | 5 +- src/slic3r/Utils/FlashAir.hpp | 5 +- src/slic3r/Utils/OctoPrint.hpp | 4 +- src/slic3r/Utils/PrintHost.hpp | 2 + src/slic3r/Utils/Repetier.cpp | 72 +++++++++++++++++++++++++---- src/slic3r/Utils/Repetier.hpp | 5 +- 11 files changed, 133 insertions(+), 39 deletions(-) diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index f0de4e798..a6b1add7c 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -152,6 +152,7 @@ void PrintConfigDef::init_common_params() def = this->add("repetier_slug", coString); def->label = L("Repetier Printer"); def->tooltip = L("Name of the Repetier printer"); + def->gui_type = "select_open"; def->mode = comAdvanced; def->set_default_value(new ConfigOptionString("")); diff --git a/src/slic3r/GUI/PrintHostDialogs.cpp b/src/slic3r/GUI/PrintHostDialogs.cpp index eee907a60..4942dcbd8 100644 --- a/src/slic3r/GUI/PrintHostDialogs.cpp +++ b/src/slic3r/GUI/PrintHostDialogs.cpp @@ -28,7 +28,7 @@ namespace GUI { static const char *CONFIG_KEY_PATH = "printhost_path"; static const char *CONFIG_KEY_PRINT = "printhost_print"; -static const char *CONFIG_KEY_GROUP = "repetier_group"; +static const char *CONFIG_KEY_GROUP = "printhost_group"; PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, bool can_start_print, wxArrayString& groups) : MsgDialog(nullptr, _(L("Send G-Code to printer host")), _(L("Upload to Printer Host with the following filename:")), wxID_NONE) diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 132ee9eca..bdda5c9a5 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1991,27 +1991,19 @@ void TabPrinter::build_printhost(ConfigOptionsGroup *optgroup) return sizer; }; - - auto repetier_slug_browse = [=](wxWindow* parent) { - add_scaled_button(parent, &m_repetier_slug_browse_btn, "printers", _(L("Printers")) + " "+ dots, wxBU_LEFT | wxBU_EXACTFIT); + + auto print_host_printers = [this](wxWindow* parent) { + add_scaled_button(parent, &m_repetier_slug_browse_btn, "browse", _(L("Refresh Printers")), wxBU_LEFT | wxBU_EXACTFIT); ScalableButton* btn = m_repetier_slug_browse_btn; btn->SetFont(Slic3r::GUI::wxGetApp().normal_font()); auto sizer = new wxBoxSizer(wxHORIZONTAL); sizer->Add(btn); - - btn->Bind(wxEVT_BUTTON, [=](wxCommandEvent &e) { - BonjourDialog dialog(parent, tech); - if (dialog.show_and_lookup()) { - optgroup->set_value("repetier_slug", std::move(dialog.get_selected()), true); - optgroup->get_field("repetier_slug")->field_changed(); - } - }); - + + btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent e) { update_printers(); }); return sizer; }; - - + // Set a wider width for a better alignment Option option = optgroup->get_option("print_host"); option.opt.width = Field::def_width_wider(); @@ -2025,7 +2017,9 @@ void TabPrinter::build_printhost(ConfigOptionsGroup *optgroup) option = optgroup->get_option("repetier_slug"); option.opt.width = Field::def_width_wider(); - optgroup->append_single_option_line(option); + Line slug_line = optgroup->create_single_option_line(option); + slug_line.append_widget(print_host_printers); + optgroup->append_line(slug_line); const auto ca_file_hint = _utf8(L("HTTPS CA file is optional. It is only needed if you use HTTPS with a self-signed certificate.")); @@ -2139,6 +2133,32 @@ void TabPrinter::update_serial_ports() choice->set_values(Utils::scan_serial_ports()); } +void TabPrinter::update_printers() +{ + std::unique_ptr host(PrintHost::get_print_host(m_config)); + + wxArrayString printers; + Field *rs = get_field("repetier_slug"); + if (!host->get_printers(printers)) { + std::vector slugs; + + Choice *choice = dynamic_cast(rs); + choice->set_values(slugs); + + rs->disable(); + } else { + std::vector slugs; + for (int i = 0; i < printers.size(); i++) { + slugs.push_back(printers[i].ToStdString()); + } + + Choice *choice = dynamic_cast(rs); + choice->set_values(slugs); + + rs->enable(); + } +} + void TabPrinter::extruders_count_changed(size_t extruders_count) { bool is_count_changed = false; @@ -2501,6 +2521,15 @@ void TabPrinter::update_fff() m_print_host_test_btn->Enable(!m_config->opt_string("print_host").empty() && host->can_test()); if(m_printhost_browse_btn) m_printhost_browse_btn->Enable(host->has_auto_discovery()); + m_repetier_slug_browse_btn->Enable(host->can_support_multiple_printers()); + + Field *rs = get_field("repetier_slug"); + if (host->can_support_multiple_printers()) { + update_printers(); + rs->enable(); + } else { + rs->disable(); + } } bool have_multiple_extruders = m_extruders_count > 1; @@ -2628,16 +2657,6 @@ void TabPrinter::update_fff() field->toggle(have_advanced_wipe_volume); } } - - bool is_repetier = m_config->option>("host_type")->value == htRepetier; - { - Field *rs = get_field("repetier_slug"); - if (is_repetier) { - rs->enable(); - } else { - rs->disable(); - } - } //z step checks { diff --git a/src/slic3r/GUI/Tab.hpp b/src/slic3r/GUI/Tab.hpp index 777c6043e..de152e162 100644 --- a/src/slic3r/GUI/Tab.hpp +++ b/src/slic3r/GUI/Tab.hpp @@ -392,6 +392,7 @@ public: wxButton* m_serial_test_btn = nullptr; ScalableButton* m_print_host_test_btn = nullptr; ScalableButton* m_printhost_browse_btn = nullptr; + ScalableButton* m_printhost_browse_printer_btn = nullptr; ScalableButton* m_repetier_slug_browse_btn = nullptr; ScalableButton* m_reset_to_filament_color = nullptr; @@ -419,6 +420,7 @@ public: void update_sla(); void update_pages(); // update m_pages according to printer technology void update_serial_ports(); + void update_printers(); void extruders_count_changed(size_t extruders_count); void milling_count_changed(size_t extruders_count); PageShp build_kinematics_page(); diff --git a/src/slic3r/Utils/AstroBox.hpp b/src/slic3r/Utils/AstroBox.hpp index 362857546..85430e893 100644 --- a/src/slic3r/Utils/AstroBox.hpp +++ b/src/slic3r/Utils/AstroBox.hpp @@ -27,8 +27,11 @@ public: bool has_auto_discovery() const override { return true; } bool can_test() const override { return true; } bool can_start_print() const override { return true; } + bool can_support_multiple_printers() const override { return false; } std::string get_host() const override { return host; } - bool get_groups(wxArrayString& groups) const override { return false; } + + bool get_groups(wxArrayString &groups) const override { return false; } + bool get_printers(wxArrayString &printers) const override { return false; } protected: bool validate_version_text(const boost::optional &version_text) const; diff --git a/src/slic3r/Utils/Duet.hpp b/src/slic3r/Utils/Duet.hpp index 5d403d647..654876b61 100644 --- a/src/slic3r/Utils/Duet.hpp +++ b/src/slic3r/Utils/Duet.hpp @@ -26,8 +26,11 @@ public: bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } bool can_start_print() const override { return true; } + bool can_support_multiple_printers() const override { return false; } std::string get_host() const override { return host; } - bool get_groups(wxArrayString& groups) const override { return false; } + + bool get_groups(wxArrayString &groups) const override { return false; } + bool get_printers(wxArrayString &printers) const override { return false; } private: std::string host; diff --git a/src/slic3r/Utils/FlashAir.hpp b/src/slic3r/Utils/FlashAir.hpp index 013941b0d..15efc0912 100644 --- a/src/slic3r/Utils/FlashAir.hpp +++ b/src/slic3r/Utils/FlashAir.hpp @@ -27,8 +27,11 @@ public: bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } bool can_start_print() const override { return false; } + bool can_support_multiple_printers() const override { return false; } std::string get_host() const override { return host; } - bool get_groups(wxArrayString& groups) const override { return false; } + + bool get_groups(wxArrayString &groups) const override { return false; } + bool get_printers(wxArrayString &printers) const override { return false; } private: std::string host; diff --git a/src/slic3r/Utils/OctoPrint.hpp b/src/slic3r/Utils/OctoPrint.hpp index ceed69cbb..720a47a66 100644 --- a/src/slic3r/Utils/OctoPrint.hpp +++ b/src/slic3r/Utils/OctoPrint.hpp @@ -28,8 +28,10 @@ public: bool has_auto_discovery() const override { return true; } bool can_test() const override { return true; } bool can_start_print() const override { return true; } + bool can_support_multiple_printers() const override { return false; } std::string get_host() const override { return host; } - bool get_groups(wxArrayString& groups) const override { return false; } + bool get_groups(wxArrayString &groups) const override { return false; } + bool get_printers(wxArrayString &printers) const override { return false; } protected: virtual bool validate_version_text(const boost::optional &version_text) const; diff --git a/src/slic3r/Utils/PrintHost.hpp b/src/slic3r/Utils/PrintHost.hpp index daa34966d..e160cbb35 100644 --- a/src/slic3r/Utils/PrintHost.hpp +++ b/src/slic3r/Utils/PrintHost.hpp @@ -44,8 +44,10 @@ public: virtual bool has_auto_discovery() const = 0; virtual bool can_test() const = 0; virtual bool can_start_print() const = 0; + virtual bool can_support_multiple_printers() const = 0; virtual std::string get_host() const = 0; virtual bool get_groups(wxArrayString& groups) const = 0; + virtual bool get_printers(wxArrayString &printers) const = 0; static PrintHost* get_print_host(DynamicPrintConfig *config); diff --git a/src/slic3r/Utils/Repetier.cpp b/src/slic3r/Utils/Repetier.cpp index 0b2f6eeba..fe6eae047 100644 --- a/src/slic3r/Utils/Repetier.cpp +++ b/src/slic3r/Utils/Repetier.cpp @@ -41,12 +41,13 @@ bool Repetier::test(wxString &msg) const const char *name = get_name(); bool res = true; - auto url = make_url("printer/info"); + auto url = make_url("printer/version"); - BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url; + BOOST_LOG_TRIVIAL(info) << boost::format("%1%: List version at: %2%") % name % url; auto http = Http::get(std::move(url)); set_auth(http); + http.on_error([&](std::string body, std::string error, unsigned status) { BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status % body; res = false; @@ -59,12 +60,21 @@ bool Repetier::test(wxString &msg) const std::stringstream ss(body); pt::ptree ptree; pt::read_json(ss, ptree); - - const auto text = ptree.get_optional("name"); - res = validate_version_text(text); - if (! res) { - msg = GUI::from_u8((boost::format(_utf8(L("Mismatched type of print host: %s"))) % (text ? *text : "Repetier")).str()); + + /* + const auto error = ptree.get_optional("error"); + if (error) { + msg = GUI::from_u8((boost::format(_utf8(L("%s"))) % error).str()); + res = false; + } else { + BOOST_FOREACH(boost::property_tree::ptree::value_type &v, ptree.get_child("data.")) { + const auto slug = v.second.get("slug"); + slugs.push_back(slug); + } + + //res = !printers.empty(); } + */ } catch (const std::exception &) { res = false; @@ -83,7 +93,7 @@ wxString Repetier::get_test_ok_msg () const wxString Repetier::get_test_failed_msg (wxString &msg) const { - return GUI::from_u8((boost::format("%s: %s\n\n%s") + return GUI::from_u8((boost::format("%s: %s\n\n%s") % _utf8(L("Could not connect to Repetier")) % std::string(msg.ToUTF8()) % _utf8(L("Note: Repetier version at least 0.90.0 is required."))).str()); @@ -213,4 +223,50 @@ bool Repetier::get_groups(wxArrayString& groups) const return res; } +bool Repetier::get_printers(wxArrayString& printers) const +{ + const char *name = get_name(); + + bool res = true; + auto url = make_url("printer/list"); + + BOOST_LOG_TRIVIAL(info) << boost::format("%1%: List printers at: %2%") % name % url; + + auto http = Http::get(std::move(url)); + set_auth(http); + + auto &slugs = printers; + http.on_error([&](std::string body, std::string error, unsigned status) { + BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error listing printers: %2%, HTTP %3%, body: `%4%`") % name % error % status % body; + res = false; + }) + .on_complete([&, this](std::string body, unsigned) { + BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got printers: %2%") % name % body; + + try { + std::stringstream ss(body); + pt::ptree ptree; + pt::read_json(ss, ptree); + + const auto error = ptree.get_optional("error"); + if (error) { + res = false; + } else { + BOOST_FOREACH(boost::property_tree::ptree::value_type &v, ptree.get_child("data.")) { + const auto slug = v.second.get("slug"); + printers.push_back(slug); + } + + //res = !printers.empty(); + } + } + catch (const std::exception &) { + res = false; + } + }) + .perform_sync(); + + return res; +} + } diff --git a/src/slic3r/Utils/Repetier.hpp b/src/slic3r/Utils/Repetier.hpp index 442415526..da58eafa3 100644 --- a/src/slic3r/Utils/Repetier.hpp +++ b/src/slic3r/Utils/Repetier.hpp @@ -28,8 +28,11 @@ public: bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } bool can_start_print() const override { return false; } + bool can_support_multiple_printers() const override { return true; } std::string get_host() const override { return host; } - bool get_groups(wxArrayString& groups) const override; + + bool get_groups(wxArrayString &groups) const override; + bool get_printers(wxArrayString &printers) const override; protected: virtual bool validate_version_text(const boost::optional &version_text) const;