PrintHost: Fix host type configuration, refactor, fix #1687

This commit is contained in:
Vojtech Kral 2019-03-05 15:15:41 +01:00
parent bd725a47f4
commit 857b68a82a
10 changed files with 80 additions and 55 deletions

View File

@ -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<PrintHostType>(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 "

View File

@ -38,7 +38,7 @@ enum GCodeFlavor {
};
enum PrintHostType {
htOctoPrint, htDuet, htSL1,
htOctoPrint, htDuet
};
enum InfillPattern {

View File

@ -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<ConfigOptionEnum<PrintHostType>>(opt_key)->value = htSL1;
}
}
{

View File

@ -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<ConfigOptionEnum<PrintHostType>>("host_type", true)->value = htSL1;
}
auto printhost_browse = [this, optgroup] (wxWindow* parent) {

View File

@ -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);

View File

@ -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;

View File

@ -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<std::string> &version_text) const
bool SL1Host::validate_version_text(const boost::optional<std::string> &version_text) const
{
return version_text ? boost::starts_with(*version_text, "Prusa SLA") : false;
}

View File

@ -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;

View File

@ -25,17 +25,29 @@ namespace Slic3r {
PrintHost::~PrintHost() {}
PrintHost* PrintHost::get_print_host(DynamicPrintConfig *config, Slic3r::PrinterTechnology pt_fallback)
PrintHost* PrintHost::get_print_host(DynamicPrintConfig *config)
{
PrinterTechnology tech = ptFFF;
{
const auto opt = config->option<ConfigOptionEnum<PrinterTechnology>>("printer_technology");
if (opt != nullptr) {
tech = opt->value;
}
}
if (tech == ptFFF) {
const auto opt = config->option<ConfigOptionEnum<PrintHostType>>("host_type");
const auto host_type = opt != nullptr ? opt->value : (pt_fallback == ptFFF ? htOctoPrint : htSL1);
const auto host_type = opt != nullptr ? opt->value : htOctoPrint;
switch (host_type) {
case htOctoPrint: return new OctoPrint(config);
case htDuet: return new Duet(config);
case htSL1: return new SLAHost(config);
default: return nullptr;
}
} else {
return new SL1Host(config);
}
}
wxString PrintHost::format_error(const std::string &body, const std::string &error, unsigned status) const

View File

@ -8,7 +8,6 @@
#include <wx/string.h>
#include "libslic3r/PrintConfig.hpp"
#include "Http.hpp"
@ -33,6 +32,8 @@ public:
typedef Http::ProgressFn ProgressFn;
typedef std::function<void(wxString /* error */)> 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;