From 857b68a82ac4772e4cbc3292b0f9f8968619b2d0 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Tue, 5 Mar 2019 15:15:41 +0100 Subject: [PATCH] PrintHost: Fix host type configuration, refactor, fix #1687 --- src/libslic3r/PrintConfig.cpp | 48 +++++++++++++++++----------------- src/libslic3r/PrintConfig.hpp | 2 +- src/slic3r/GUI/Plater.cpp | 3 --- src/slic3r/GUI/Tab.cpp | 2 -- src/slic3r/Utils/Duet.cpp | 2 ++ src/slic3r/Utils/Duet.hpp | 2 ++ src/slic3r/Utils/OctoPrint.cpp | 33 ++++++++++++++--------- src/slic3r/Utils/OctoPrint.hpp | 10 ++++--- src/slic3r/Utils/PrintHost.cpp | 28 ++++++++++++++------ src/slic3r/Utils/PrintHost.hpp | 5 ++-- 10 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 081057f88..1c78a3e31 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -70,6 +70,30 @@ void PrintConfigDef::init_common_params() def->min = 0; def->mode = comAdvanced; def->default_value = new ConfigOptionFloat(0.049); + + def = this->add("print_host", coString); + def->label = L("Hostname, IP or URL"); + def->tooltip = L("Slic3r can upload G-code files to a printer host. This field should contain " + "the hostname, IP address or URL of the printer host instance."); + def->cli = "print-host=s"; + def->mode = comAdvanced; + def->default_value = new ConfigOptionString(""); + + def = this->add("printhost_apikey", coString); + def->label = L("API Key / Password"); + def->tooltip = L("Slic3r can upload G-code files to a printer host. This field should contain " + "the API Key or the password required for authentication."); + def->cli = "printhost-apikey=s"; + def->mode = comAdvanced; + def->default_value = new ConfigOptionString(""); + + def = this->add("printhost_cafile", coString); + def->label = "HTTPS CA File"; + def->tooltip = "Custom CA certificate file can be specified for HTTPS OctoPrint connections, in crt/pem format. " + "If left blank, the default OS CA certificate repository is used."; + def->cli = "printhost-cafile=s"; + def->mode = comAdvanced; + def->default_value = new ConfigOptionString(""); } void PrintConfigDef::init_fff_params() @@ -1331,30 +1355,6 @@ void PrintConfigDef::init_fff_params() def->mode = comAdvanced; def->default_value = new ConfigOptionEnum(htOctoPrint); - def = this->add("printhost_apikey", coString); - def->label = L("API Key / Password"); - def->tooltip = L("Slic3r can upload G-code files to a printer host. This field should contain " - "the API Key or the password required for authentication."); - def->cli = "printhost-apikey=s"; - def->mode = comAdvanced; - def->default_value = new ConfigOptionString(""); - - def = this->add("printhost_cafile", coString); - def->label = "HTTPS CA File"; - def->tooltip = "Custom CA certificate file can be specified for HTTPS OctoPrint connections, in crt/pem format. " - "If left blank, the default OS CA certificate repository is used."; - def->cli = "printhost-cafile=s"; - def->mode = comAdvanced; - def->default_value = new ConfigOptionString(""); - - def = this->add("print_host", coString); - def->label = L("Hostname, IP or URL"); - def->tooltip = L("Slic3r can upload G-code files to a printer host. This field should contain " - "the hostname, IP address or URL of the printer host instance."); - def->cli = "print-host=s"; - def->mode = comAdvanced; - def->default_value = new ConfigOptionString(""); - def = this->add("only_retract_when_crossing_perimeters", coBool); def->label = L("Only retract when crossing perimeters"); def->tooltip = L("Disables retraction when the travel path does not exceed the upper layer's perimeters " diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index d72c48339..f4d9053a6 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -38,7 +38,7 @@ enum GCodeFlavor { }; enum PrintHostType { - htOctoPrint, htDuet, htSL1, + htOctoPrint, htDuet }; enum InfillPattern { diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 3062741eb..95537771f 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -3217,9 +3217,6 @@ void Plater::on_config_change(const DynamicPrintConfig &config) bed_shape_changed = true; update_scheduled = true; } - else if (opt_key == "host_type" && this->p->printer_technology == ptSLA) { - p->config->option>(opt_key)->value = htSL1; - } } { diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 632627eba..060eb1383 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1626,8 +1626,6 @@ void TabPrinter::build_printhost(ConfigOptionsGroup *optgroup) // Only offer the host type selection for FFF, for SLA it's always the SL1 printer (at the moment) if (! sla) { optgroup->append_single_option_line("host_type"); - } else { - m_config->option>("host_type", true)->value = htSL1; } auto printhost_browse = [this, optgroup] (wxWindow* parent) { diff --git a/src/slic3r/Utils/Duet.cpp b/src/slic3r/Utils/Duet.cpp index c85bd90e8..b0d8c0b8d 100644 --- a/src/slic3r/Utils/Duet.cpp +++ b/src/slic3r/Utils/Duet.cpp @@ -34,6 +34,8 @@ Duet::Duet(DynamicPrintConfig *config) : Duet::~Duet() {} +const char* Duet::get_name() const { return "Duet"; } + bool Duet::test(wxString &msg) const { bool connected = connect(msg); diff --git a/src/slic3r/Utils/Duet.hpp b/src/slic3r/Utils/Duet.hpp index 0b40fe8b3..04ceec36f 100644 --- a/src/slic3r/Utils/Duet.hpp +++ b/src/slic3r/Utils/Duet.hpp @@ -19,6 +19,8 @@ public: Duet(DynamicPrintConfig *config); virtual ~Duet(); + virtual const char* get_name() const; + virtual bool test(wxString &curl_msg) const; virtual wxString get_test_ok_msg () const; virtual wxString get_test_failed_msg (wxString &msg) const; diff --git a/src/slic3r/Utils/OctoPrint.cpp b/src/slic3r/Utils/OctoPrint.cpp index 594c2e3da..7cea198bf 100644 --- a/src/slic3r/Utils/OctoPrint.cpp +++ b/src/slic3r/Utils/OctoPrint.cpp @@ -29,25 +29,29 @@ OctoPrint::OctoPrint(DynamicPrintConfig *config) : OctoPrint::~OctoPrint() {} +const char* OctoPrint::get_name() const { return "OctoPrint"; } + bool OctoPrint::test(wxString &msg) const { // Since the request is performed synchronously here, // it is ok to refer to `msg` from within the closure + const char *name = get_name(); + bool res = true; auto url = make_url("api/version"); - BOOST_LOG_TRIVIAL(info) << boost::format("Octoprint: Get version at: %1%") % url; + BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get 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("Octoprint: Error getting version: %1%, HTTP %2%, body: `%3%`") % error % status % body; + BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status % body; res = false; msg = format_error(body, error, status); }) .on_complete([&, this](std::string body, unsigned) { - BOOST_LOG_TRIVIAL(debug) << boost::format("Octoprint: Got version: %1%") % body; + BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got version: %2%") % name % body; try { std::stringstream ss(body); @@ -88,6 +92,8 @@ wxString OctoPrint::get_test_failed_msg (wxString &msg) const bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const { + const char *name = get_name(); + const auto upload_filename = upload_data.upload_path.filename(); const auto upload_parent_path = upload_data.upload_path.parent_path(); @@ -101,7 +107,8 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro auto url = make_url("api/files/local"); - BOOST_LOG_TRIVIAL(info) << boost::format("Octoprint: Uploading file %1% at %2%, filename: %3%, path: %4%, print: %5%") + BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%") + % name % upload_data.source_path % url % upload_filename.string() @@ -114,10 +121,10 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro .form_add("path", upload_parent_path.string()) // XXX: slashes on windows ??? .form_add_file("file", upload_data.source_path.string(), upload_filename.string()) .on_complete([&](std::string body, unsigned status) { - BOOST_LOG_TRIVIAL(debug) << boost::format("Octoprint: File uploaded: HTTP %1%: %2%") % status % body; + BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: File uploaded: HTTP %2%: %3%") % name % status % body; }) .on_error([&](std::string body, std::string error, unsigned status) { - BOOST_LOG_TRIVIAL(error) << boost::format("Octoprint: Error uploading file: %1%, HTTP %2%, body: `%3%`") % error % status % body; + BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error uploading file: %2%, HTTP %3%, body: `%4%`") % name % error % status % body; error_fn(format_error(body, error, status)); res = false; }) @@ -177,26 +184,28 @@ std::string OctoPrint::make_url(const std::string &path) const } -// SLAHost +// SL1Host -SLAHost::~SLAHost() {} +SL1Host::~SL1Host() {} -wxString SLAHost::get_test_ok_msg () const +const char* SL1Host::get_name() const { return "SL1Host"; } + +wxString SL1Host::get_test_ok_msg () const { return _(L("Connection to Prusa SLA works correctly.")); } -wxString SLAHost::get_test_failed_msg (wxString &msg) const +wxString SL1Host::get_test_failed_msg (wxString &msg) const { return wxString::Format("%s: %s", _(L("Could not connect to Prusa SLA")), msg); } -bool SLAHost::can_start_print() const +bool SL1Host::can_start_print() const { return false; } -bool SLAHost::validate_version_text(const boost::optional &version_text) const +bool SL1Host::validate_version_text(const boost::optional &version_text) const { return version_text ? boost::starts_with(*version_text, "Prusa SLA") : false; } diff --git a/src/slic3r/Utils/OctoPrint.hpp b/src/slic3r/Utils/OctoPrint.hpp index a7caa7129..0e372c7c9 100644 --- a/src/slic3r/Utils/OctoPrint.hpp +++ b/src/slic3r/Utils/OctoPrint.hpp @@ -20,6 +20,8 @@ public: OctoPrint(DynamicPrintConfig *config); virtual ~OctoPrint(); + virtual const char* get_name() const; + virtual bool test(wxString &curl_msg) const; virtual wxString get_test_ok_msg () const; virtual wxString get_test_failed_msg (wxString &msg) const; @@ -42,11 +44,13 @@ private: }; -class SLAHost: public OctoPrint +class SL1Host: public OctoPrint { public: - SLAHost(DynamicPrintConfig *config) : OctoPrint(config) {} - virtual ~SLAHost(); + SL1Host(DynamicPrintConfig *config) : OctoPrint(config) {} + virtual ~SL1Host(); + + virtual const char* get_name() const; virtual wxString get_test_ok_msg () const; virtual wxString get_test_failed_msg (wxString &msg) const; diff --git a/src/slic3r/Utils/PrintHost.cpp b/src/slic3r/Utils/PrintHost.cpp index 5c39f8542..e9e39e695 100644 --- a/src/slic3r/Utils/PrintHost.cpp +++ b/src/slic3r/Utils/PrintHost.cpp @@ -25,16 +25,28 @@ namespace Slic3r { PrintHost::~PrintHost() {} -PrintHost* PrintHost::get_print_host(DynamicPrintConfig *config, Slic3r::PrinterTechnology pt_fallback) +PrintHost* PrintHost::get_print_host(DynamicPrintConfig *config) { - const auto opt = config->option>("host_type"); - const auto host_type = opt != nullptr ? opt->value : (pt_fallback == ptFFF ? htOctoPrint : htSL1); + PrinterTechnology tech = ptFFF; - switch (host_type) { - case htOctoPrint: return new OctoPrint(config); - case htDuet: return new Duet(config); - case htSL1: return new SLAHost(config); - default: return nullptr; + { + const auto opt = config->option>("printer_technology"); + if (opt != nullptr) { + tech = opt->value; + } + } + + if (tech == ptFFF) { + const auto opt = config->option>("host_type"); + const auto host_type = opt != nullptr ? opt->value : htOctoPrint; + + switch (host_type) { + case htOctoPrint: return new OctoPrint(config); + case htDuet: return new Duet(config); + default: return nullptr; + } + } else { + return new SL1Host(config); } } diff --git a/src/slic3r/Utils/PrintHost.hpp b/src/slic3r/Utils/PrintHost.hpp index 010889642..e0aeb463c 100644 --- a/src/slic3r/Utils/PrintHost.hpp +++ b/src/slic3r/Utils/PrintHost.hpp @@ -8,7 +8,6 @@ #include -#include "libslic3r/PrintConfig.hpp" #include "Http.hpp" @@ -33,6 +32,8 @@ public: typedef Http::ProgressFn ProgressFn; typedef std::function ErrorFn; + virtual const char* get_name() const = 0; + virtual bool test(wxString &curl_msg) const = 0; virtual wxString get_test_ok_msg () const = 0; virtual wxString get_test_failed_msg (wxString &msg) const = 0; @@ -42,7 +43,7 @@ public: virtual bool can_start_print() const = 0; virtual std::string get_host() const = 0; - static PrintHost* get_print_host(DynamicPrintConfig *config, Slic3r::PrinterTechnology pt_fallback = ptFFF); + static PrintHost* get_print_host(DynamicPrintConfig *config); protected: virtual wxString format_error(const std::string &body, const std::string &error, unsigned status) const;