OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::CrealityPrint Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Utils/CrealityPrint.hpp>

Inheritance diagram for Slic3r::CrealityPrint:
Collaboration diagram for Slic3r::CrealityPrint:

Classes

struct  WsSettings

Public Types

typedef Http::ProgressFn ProgressFn
typedef std::function< void(wxString)> ErrorFn
typedef std::function< void(wxString, wxString)> InfoFn

Public Member Functions

 CrealityPrint (DynamicPrintConfig *config)
 ~CrealityPrint () override=default
const char * get_name () const override
virtual bool can_test () const override
std::string get_host () const override
bool has_auto_discovery () const override
wxString get_test_ok_msg () const override
wxString get_test_failed_msg (wxString &msg) const override
virtual bool test (wxString &curl_msg) const override
PrintHostPostUploadActions get_post_upload_actions () const override
bool upload (PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, InfoFn info_fn) const override
bool supports_multi_color_print () const
std::string query_boxes_info () const
std::string model_name () const
virtual bool supports_multiple_printers () const
virtual std::string get_sn () const
virtual bool get_groups (wxArrayString &) const
virtual bool get_printers (wxArrayString &) const
virtual bool get_storage (wxArrayString &, wxArrayString &) const
virtual bool is_cloud () const
virtual bool is_logged_in () const
virtual void log_out () const
virtual bool get_login_url (wxString &auth_url) const

Static Public Member Functions

static bool model_supports_multi_color (const std::string &model)
static std::string model_display_name (const std::string &model)
static PrintHost * get_print_host (DynamicPrintConfig *config)
static std::string get_print_host_webui (DynamicPrintConfig *config)
static int get_err_code_from_body (const std::string &body)

Protected Member Functions

virtual void set_auth (Http &http) const
virtual wxString format_error (const std::string &body, const std::string &error, unsigned status) const

Static Protected Member Functions

static wxString timed_out_message ()

Protected Attributes

struct Slic3r::CrealityPrint::WsSettings ws_settings

Private Member Functions

std::string make_url (const std::string &path) const
bool start_print (wxString &msg, const std::string &filename, const std::map< std::string, std::string > &extended_info) const
std::string safe_filename (const std::string &filename) const
void query_model () const

Private Attributes

std::string m_host
std::string m_port
std::string m_apikey
std::string m_cafile
std::string m_web_ui
bool m_ssl_revoke_best_effort
std::string m_model

Detailed Description


Class Documentation

◆ Slic3r::CrealityPrint::WsSettings

struct Slic3r::CrealityPrint::WsSettings
Collaboration diagram for Slic3r::CrealityPrint::WsSettings:
Class Members
milliseconds close {std::chrono::seconds(3)}
milliseconds connect {std::chrono::seconds(5)}
string port {"9999"}
milliseconds reply {std::chrono::seconds(10)}

Member Typedef Documentation

◆ ErrorFn

typedef std::function<void(wxString )> Slic3r::PrintHost::ErrorFn
inherited

◆ InfoFn

typedef std::function<void(wxString , wxString )> Slic3r::PrintHost::InfoFn
inherited

◆ ProgressFn

Constructor & Destructor Documentation

◆ CrealityPrint()

Slic3r::CrealityPrint::CrealityPrint ( DynamicPrintConfig * config)
65 :
66 m_host(config->opt_string("print_host")),
67 m_web_ui(config->opt_string("print_host_webui")),
68 m_cafile(config->opt_string("printhost_cafile")),
69 m_port(config->opt_string("printhost_port")),
70 m_apikey(config->opt_string("printhost_apikey")),
71 m_ssl_revoke_best_effort(config->opt_bool("printhost_ssl_ignore_revoke"))
72{}
bool m_ssl_revoke_best_effort
Definition CrealityPrint.hpp:61
std::string m_host
Definition CrealityPrint.hpp:56
std::string m_web_ui
Definition CrealityPrint.hpp:60
std::string m_cafile
Definition CrealityPrint.hpp:59
std::string m_port
Definition CrealityPrint.hpp:57
std::string m_apikey
Definition CrealityPrint.hpp:58

References m_apikey, m_cafile, m_host, m_port, m_ssl_revoke_best_effort, and m_web_ui.

◆ ~CrealityPrint()

Slic3r::CrealityPrint::~CrealityPrint ( )
overridedefault

Member Function Documentation

◆ can_test()

virtual bool Slic3r::CrealityPrint::can_test ( ) const
inlineoverridevirtual

Implements Slic3r::PrintHost.

25{ return true; };

◆ format_error()

wxString Slic3r::PrintHost::format_error ( const std::string & body,
const std::string & error,
unsigned status ) const
protectedvirtualinherited
203{
204 if (status != 0) {
205 const std::string reason = moonraker_error_reason(body);
206 auto wxbody = wxString::FromUTF8(reason.empty() ? body : reason);
207 return wxString::Format("HTTP %u: %s", status, wxbody);
208 } else {
209 if (error.find("curl:Timeout was reached") != std::string::npos) {
210 return timed_out_message();
211 }else if(error.find("curl:Couldn't resolve host name")!= std::string::npos){
212 return _L("The Hostname/IP/URL could not be parsed, please check it and try again.");
213 } else if (error.find("Connection was reset") != std::string::npos){
214 return _L("File/data transfer interrupted. Please check the printer and network, then try it again.");
215 }
216 else {
217 return wxString::FromUTF8(error.data());
218 }
219 }
220}
#define _L(s)
Definition OBJ.cpp:34
static wxString timed_out_message()
Definition PrintHost.cpp:222
std::string moonraker_error_reason(const std::string &body)
Definition PrintHost.cpp:137

References _L, and timed_out_message().

Referenced by Slic3r::Duet::connect(), Slic3r::UltiMaker::connect(), Slic3r::SimplyPrint::do_chunk_upload(), Slic3r::SimplyPrint::do_temp_upload(), Slic3r::ElegooLink::elegoo_cc2_test(), Slic3r::ElegooLink::elegoo_test(), Slic3r::PrusaLink::post_inner(), Slic3r::PrusaLink::put_inner(), Slic3r::Flashforge::request_local_api_json(), Slic3r::Duet::start_print(), Slic3r::Moonraker::start_print(), Slic3r::UltiMaker::start_print(), Slic3r::AstroBox::test(), Slic3r::CrealityPrint::test(), Slic3r::ESP3D::test(), Slic3r::FlashAir::test(), Slic3r::Moonraker::test(), Slic3r::Obico::test(), Slic3r::OctoPrint::test(), Slic3r::PrusaLink::test(), Slic3r::Repetier::test(), Slic3r::UltiMaker::test(), Slic3r::PrusaLink::test_with_method_check(), Slic3r::AstroBox::upload(), Slic3r::C3DPrinterOS::upload(), Slic3r::CrealityPrint::upload(), Slic3r::Duet::upload(), Slic3r::ESP3D::upload(), Slic3r::FlashAir::upload(), Slic3r::MKS::upload(), Slic3r::Moonraker::upload(), Slic3r::Obico::upload(), Slic3r::Repetier::upload(), Slic3r::UltiMaker::upload(), Slic3r::OctoPrint::upload_inner_with_host(), Slic3r::Flashforge::upload_local_api(), Slic3r::ElegooLink::uploadPart(), and Slic3r::ElegooLink::uploadPartCC2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_err_code_from_body()

int Slic3r::PrintHost::get_err_code_from_body ( const std::string & body)
staticinherited
189{
190 boost::property_tree::ptree root;
191 std::istringstream iss(body);
192 try {
193 boost::property_tree::read_json(iss, root);
194 } catch (const std::exception &ex) {
195 BOOST_LOG_TRIVIAL(error) << "PrintHost: response is not valid JSON: " << ex.what();
196 return -1;
197 }
198
199 return root.get<int>("err", 0);
200}

Referenced by Slic3r::Duet::connect(), Slic3r::UltiMaker::connect(), Slic3r::Duet::upload(), Slic3r::MKS::upload(), and Slic3r::UltiMaker::upload().

Here is the caller graph for this function:

◆ get_groups()

virtual bool Slic3r::PrintHost::get_groups ( wxArrayString & ) const
inlinevirtualinherited

Reimplemented in Slic3r::Repetier.

84{ return false; }

◆ get_host()

std::string Slic3r::CrealityPrint::get_host ( ) const
overridevirtual

Implements Slic3r::PrintHost.

76 {
77 return m_host;
78}

References m_host.

◆ get_login_url()

virtual bool Slic3r::PrintHost::get_login_url ( wxString & auth_url) const
inlinevirtualinherited

Reimplemented in Slic3r::Obico.

99{ return false; }

Referenced by Slic3r::GUI::PrinterCloudAuthDialog::PrinterCloudAuthDialog().

Here is the caller graph for this function:

◆ get_name()

const char * Slic3r::CrealityPrint::get_name ( ) const
overridevirtual

Implements Slic3r::PrintHost.

74{ return "Creality Print"; }

Referenced by test(), and upload().

Here is the caller graph for this function:

◆ get_post_upload_actions()

PrintHostPostUploadActions Slic3r::CrealityPrint::get_post_upload_actions ( ) const
overridevirtual

Implements Slic3r::PrintHost.

136 {
138}
@ StartPrint
Definition PrintHost.hpp:25

References Slic3r::StartPrint.

◆ get_print_host()

PrintHost * Slic3r::PrintHost::get_print_host ( DynamicPrintConfig * config)
staticinherited
59{
61
62 {
63 const auto opt = config->option<ConfigOptionEnum<PrinterTechnology>>("printer_technology");
64 if (opt != nullptr) {
65 tech = opt->value;
66 }
67 }
68
69 if (tech == ptFFF) {
70 const auto opt = config->option<ConfigOptionEnum<PrintHostType>>("host_type");
71 const auto host_type = opt != nullptr ? opt->value : htOctoPrint;
72
73 switch (host_type) {
74 case htOctoPrint: return new OctoPrint(config);
75 case htDuet: return new Duet(config);
76 case htUltiMaker: return new UltiMaker(config);
77 case htFlashAir: return new FlashAir(config);
78 case htAstroBox: return new AstroBox(config);
79 case htRepetier: return new Repetier(config);
80 case htPrusaLink: return new PrusaLink(config);
81 case htPrusaConnect: return new PrusaConnect(config);
82 case htMKS: return new MKS(config);
83 case htESP3D: return new ESP3D(config);
84 case htCrealityPrint: return new CrealityPrint(config);
85 case htObico: return new Obico(config);
86 case htFlashforge: return new Flashforge(config);
87 case htSimplyPrint: return new SimplyPrint(config);
88 case htElegooLink: return new ElegooLink(config);
89 case ht3DPrinterOS: return new C3DPrinterOS(config);
90 case htMoonraker: return new Moonraker(config);
91 default: return nullptr;
92 }
93 } else {
94 return new SL1Host(config);
95 }
96}
PrinterTechnology
Definition Config.hpp:229
@ ptFFF
Definition Config.hpp:231
@ htSimplyPrint
Definition PrintConfig.hpp:119
@ htMoonraker
Definition PrintConfig.hpp:119
@ htElegooLink
Definition PrintConfig.hpp:119
@ htCrealityPrint
Definition PrintConfig.hpp:119
@ ht3DPrinterOS
Definition PrintConfig.hpp:119
@ htPrusaLink
Definition PrintConfig.hpp:119
@ htRepetier
Definition PrintConfig.hpp:119
@ htMKS
Definition PrintConfig.hpp:119
@ htPrusaConnect
Definition PrintConfig.hpp:119
@ htDuet
Definition PrintConfig.hpp:119
@ htUltiMaker
Definition PrintConfig.hpp:119
@ htObico
Definition PrintConfig.hpp:119
@ htOctoPrint
Definition PrintConfig.hpp:119
@ htFlashforge
Definition PrintConfig.hpp:119
@ htFlashAir
Definition PrintConfig.hpp:119
@ htAstroBox
Definition PrintConfig.hpp:119
@ htESP3D
Definition PrintConfig.hpp:119
ConfigOptionEnum< PrinterTechnology >
Definition PrintConfig.hpp:2358

References Slic3r::ht3DPrinterOS, Slic3r::htAstroBox, Slic3r::htCrealityPrint, Slic3r::htDuet, Slic3r::htElegooLink, Slic3r::htESP3D, Slic3r::htFlashAir, Slic3r::htFlashforge, Slic3r::htMKS, Slic3r::htMoonraker, Slic3r::htObico, Slic3r::htOctoPrint, Slic3r::htPrusaConnect, Slic3r::htPrusaLink, Slic3r::htRepetier, Slic3r::htSimplyPrint, Slic3r::htUltiMaker, Slic3r::ConfigBase::option(), and Slic3r::ptFFF.

Referenced by Slic3r::GUI::PhysicalPrinterDialog::build_printhost_settings(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::handle_get_sn_request(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::handle_upload_request(), Slic3r::GUI::PhysicalPrinterDialog::update_printers(), and Slic3r::GUI::PhysicalPrinterDialog::update_printhost_buttons().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_print_host_webui()

std::string Slic3r::PrintHost::get_print_host_webui ( DynamicPrintConfig * config)
staticinherited
99{
100 if (config == nullptr)
101 return {};
102
103 std::string webui_url;
104 const auto* host_type_opt = config->option<ConfigOptionEnum<PrintHostType>>("host_type");
105 const auto host_type = host_type_opt != nullptr ? host_type_opt->value : htOctoPrint;
106
107 switch (host_type) {
108 case htElegooLink: {
109 webui_url = ElegooLink::get_print_host_webui(config);
110 break;
111 }
112 default: break;
113 }
114
115 if (webui_url.empty()) {
116 webui_url = config->opt_string("print_host_webui");
117 if (webui_url.empty())
118 webui_url = config->opt_string("print_host");
119 if (webui_url.empty())
120 return webui_url;
121 }
122
123 const bool has_http_scheme = boost::algorithm::istarts_with(webui_url, "http");
124 const bool has_file_scheme = boost::algorithm::istarts_with(webui_url, "file:");
125
126 if (!has_http_scheme && !has_file_scheme)
127 webui_url = "http://" + webui_url;
128
129 return webui_url;
130}

References Slic3r::ElegooLink::get_print_host_webui(), Slic3r::htElegooLink, Slic3r::htOctoPrint, Slic3r::DynamicConfig::opt_string(), Slic3r::ConfigBase::option(), and Slic3r::ConfigOptionSingle< T >::value.

Referenced by Slic3r::GUI::MainFrame::load_printer_url(), Slic3r::GUI::Plater::priv::on_tab_selection_changing(), and Slic3r::GUI::Sidebar::update_all_preset_comboboxes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_printers()

virtual bool Slic3r::PrintHost::get_printers ( wxArrayString & ) const
inlinevirtualinherited

Reimplemented in Slic3r::Obico, and Slic3r::Repetier.

85{ return false; }

◆ get_sn()

virtual std::string Slic3r::PrintHost::get_sn ( ) const
inlinevirtualinherited

Get the serial number for connecting to the printer. For Elegoo CC2, the device details connection to the printer requires the serial number. Other print hosts do not need to implement this interface, and it returns an empty string by default.

Reimplemented in Slic3r::ElegooLink.

80{ return ""; }

◆ get_storage()

virtual bool Slic3r::PrintHost::get_storage ( wxArrayString & ,
wxArrayString &  ) const
inlinevirtualinherited

Reimplemented in Slic3r::Moonraker, Slic3r::PrusaConnect, and Slic3r::PrusaLink.

88{ return false; }

◆ get_test_failed_msg()

wxString Slic3r::CrealityPrint::get_test_failed_msg ( wxString & msg) const
overridevirtual

Implements Slic3r::PrintHost.

90{
91 return GUI::format_wxstr("%s: %s", _L("Could not connect to CrealityPrint"), msg.Truncate(256));
92}
wxString format_wxstr(const char *fmt, TArgs &&... args)
Definition format.hpp:22

References _L, and Slic3r::GUI::format_wxstr().

Here is the call graph for this function:

◆ get_test_ok_msg()

wxString Slic3r::CrealityPrint::get_test_ok_msg ( ) const
overridevirtual

Implements Slic3r::PrintHost.

87{ return _(L("Connected to CrealityPrint successfully!")); }
#define _(s)
Definition 3mf.cpp:306
#define L(s)
Definition ExtrusionEntity.cpp:25

References _, and L.

◆ has_auto_discovery()

bool Slic3r::CrealityPrint::has_auto_discovery ( ) const
inlineoverridevirtual

Implements Slic3r::PrintHost.

27{ return true; }

◆ is_cloud()

virtual bool Slic3r::PrintHost::is_cloud ( ) const
inlinevirtualinherited

Reimplemented in Slic3r::C3DPrinterOS, Slic3r::Obico, and Slic3r::SimplyPrint.

96{ return false; }

◆ is_logged_in()

virtual bool Slic3r::PrintHost::is_logged_in ( ) const
inlinevirtualinherited

Reimplemented in Slic3r::C3DPrinterOS, and Slic3r::SimplyPrint.

97{ return false; }

◆ log_out()

virtual void Slic3r::PrintHost::log_out ( ) const
inlinevirtualinherited

Reimplemented in Slic3r::C3DPrinterOS, and Slic3r::SimplyPrint.

98{}

◆ make_url()

std::string Slic3r::CrealityPrint::make_url ( const std::string & path) const
private
195{
196 if (m_host.find("http://") == 0 || m_host.find("https://") == 0) {
197 if (m_host.back() == '/') {
198 return (boost::format("%1%%2%") % m_host % path).str();
199 } else {
200 return (boost::format("%1%/%2%") % m_host % path).str();
201 }
202 } else {
203 return (boost::format("http://%1%/%2%") % m_host % path).str();
204 }
205}

References m_host.

Referenced by test(), and upload().

Here is the caller graph for this function:

◆ model_display_name()

std::string Slic3r::CrealityPrint::model_display_name ( const std::string & model)
static
336{
337 auto& names = cfs_capable_models();
338 auto it = names.find(model);
339 return it != names.end() ? it->second : std::string{};
340}
static const std::map< std::string, std::string > & cfs_capable_models()
Definition CrealityPrint.cpp:315

References Slic3r::cfs_capable_models().

Referenced by model_name(), and Slic3r::anonymous_namespace{CrealityHostDiscovery.cpp}::model_name_for().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ model_name()

std::string Slic3r::CrealityPrint::model_name ( ) const
349{
350 query_model();
351 if (m_model.empty())
352 return "unreachable";
353 std::string name = model_display_name(m_model);
354 return !name.empty() ? name : "unknown (" + m_model + ")";
355}
static std::string model_display_name(const std::string &model)
Definition CrealityPrint.cpp:335
void query_model() const
Definition CrealityPrint.cpp:304
std::string m_model
Definition CrealityPrint.hpp:62

References m_model, model_display_name(), and query_model().

Referenced by Slic3r::CrealityPrintAgent::fetch_filament_info().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ model_supports_multi_color()

bool Slic3r::CrealityPrint::model_supports_multi_color ( const std::string & model)
static
331{
332 return cfs_capable_models().count(model) > 0;
333}

References Slic3r::cfs_capable_models().

Referenced by Slic3r::anonymous_namespace{CrealityHostDiscovery.cpp}::is_cfs_capable(), and supports_multi_color_print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ query_boxes_info()

std::string Slic3r::CrealityPrint::query_boxes_info ( ) const
358{
359 try {
360 net::io_context ioc;
361 websocket::stream<beast::tcp_stream> ws{ioc};
362 ws_connect(ioc, ws, m_host, ws_settings.port, ws_settings.connect);
363
364 beast::get_lowest_layer(ws).expires_after(ws_settings.reply);
365 json boxs_query = {{"method", "get"}, {"params", {{"boxsInfo", 1}}}};
366 std::string result = ws_send_and_read(ioc, ws, boxs_query, "boxsInfo");
367
368 ws_close(ioc, ws, ws_settings.close);
369 return result;
370 } catch (std::exception const& e) {
371 BOOST_LOG_TRIVIAL(error) << "CrealityPrint: Failed to query boxsInfo: " << e.what();
372 return {};
373 }
374}
struct Slic3r::CrealityPrint::WsSettings ws_settings
static void ws_connect(net::io_context &ioc, websocket::stream< beast::tcp_stream > &ws, const std::string &host_url, const std::string &port, std::chrono::milliseconds timeout)
Definition CrealityPrint.cpp:216
static void ws_close(net::io_context &ioc, websocket::stream< beast::tcp_stream > &ws, std::chrono::milliseconds timeout)
Definition CrealityPrint.cpp:285
nlohmann::json json
Definition AssembleList.cpp:42
static std::string ws_send_and_read(net::io_context &ioc, websocket::stream< beast::tcp_stream > &ws, const json &cmd, const std::string &expected_key, int max_reads=20)
Definition CrealityPrint.cpp:255

References m_host, Slic3r::ws_close(), Slic3r::ws_connect(), Slic3r::ws_send_and_read(), and ws_settings.

Referenced by Slic3r::CrealityPrintAgent::fetch_filament_info().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ query_model()

void Slic3r::CrealityPrint::query_model ( ) const
private
305{
306 if (!m_model.empty())
307 return;
308
309 wxString msg;
310 test(msg);
311}
virtual bool test(wxString &curl_msg) const override
Definition CrealityPrint.cpp:94

References m_model, and test().

Referenced by model_name(), and supports_multi_color_print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_filename()

std::string Slic3r::CrealityPrint::safe_filename ( const std::string & filename) const
private
208{
209 std::string safe_filename = filename;
210 std::replace(safe_filename.begin(), safe_filename.end(), ' ', '_');
211
212 return safe_filename;
213}
std::string safe_filename(const std::string &filename) const
Definition CrealityPrint.cpp:207

References safe_filename().

Referenced by safe_filename(), and upload().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_auth()

void Slic3r::CrealityPrint::set_auth ( Http & http) const
protectedvirtual
80{
81 http.header("Authorization", "Bearer " + m_apikey);
82 if (!m_cafile.empty()) {
83 http.ca_file(m_cafile);
84 }
85}
@ http
Definition IPrinterAgent.hpp:64

References Slic3r::http, m_apikey, and m_cafile.

Referenced by test(), and upload().

Here is the caller graph for this function:

◆ start_print()

bool Slic3r::CrealityPrint::start_print ( wxString & msg,
const std::string & filename,
const std::map< std::string, std::string > & extended_info ) const
private
377{
378 try {
379 const std::string data_root = is_k1_family(m_model) ? "/usr/data" : "/mnt/UDISK";
380 const std::string gcode_path = data_root + "/printer_data/gcodes/" + filename;
381
382 net::io_context ioc;
383 websocket::stream<beast::tcp_stream> ws{ioc};
384 ws_connect(ioc, ws, m_host, ws_settings.port, ws_settings.connect);
385
386 // Runs before the reply deadline starts, since it can query the printer over HTTP.
387 const bool multi_color = supports_multi_color_print();
388 beast::get_lowest_layer(ws).expires_after(ws_settings.reply);
389 if (multi_color) {
390 // Build colorMatch list from the mapping provided by the dialog
391 bool use_spool_holder = false;
392 json color_list = json::array();
393 for (int i = 0; ; i++) {
394 auto it = extended_info.find("colorMatch_" + std::to_string(i));
395 if (it == extended_info.end())
396 break;
397 // Value format: "toolId\ttype\tcolor\tboxId\tmaterialId"
398 auto val = it->second;
399 std::vector<std::string> parts;
400 std::istringstream iss(val);
401 std::string part;
402 while (std::getline(iss, part, '\t'))
403 parts.push_back(part);
404 if (parts.size() >= 5) {
405 int box_id = std::stoi(parts[3]);
406 if (box_id == 0)
407 use_spool_holder = true;
408 color_list.push_back({
409 {"id", parts[0]},
410 {"type", parts[1]},
411 {"color", parts[2]},
412 {"boxId", box_id},
413 {"materialId", std::stoi(parts[4])}
414 });
415 }
416 }
417
418 int enable_self_test = 0;
419 {
420 auto it = extended_info.find("enableSelfTest");
421 if (it != extended_info.end())
422 enable_self_test = std::stoi(it->second);
423 }
424
425 if (use_spool_holder) {
426 json cmd = {
427 {"method", "set"},
428 {"params", {
429 {"opGcodeFile", "printprt:" + gcode_path},
430 {"enableSelfTest", enable_self_test}
431 }}
432 };
433 ws_send(ioc, ws, cmd);
434 } else {
435 json color_match = {
436 {"method", "set"},
437 {"params", {
438 {"colorMatch", {
439 {"path", gcode_path},
440 {"list", color_list}
441 }}
442 }}
443 };
444 ws_send(ioc, ws, color_match);
445
446 json multi_color_print = {
447 {"method", "set"},
448 {"params", {
449 {"multiColorPrint", {
450 {"gcode", gcode_path},
451 {"enableSelfTest", enable_self_test}
452 }}
453 }}
454 };
455 ws_send(ioc, ws, multi_color_print);
456 }
457 } else {
458 json cmd = {
459 {"method", "set"},
460 {"params", {
461 {"opGcodeFile", "printprt:" + gcode_path}
462 }}
463 };
464 ws_send(ioc, ws, cmd);
465
466 // K1-family firmware closes the WebSocket right after accepting the
467 // start command, so a blocking read here surfaces a benign
468 // "End of file [asio.misc:2]" even though the print already started
469 // (ws_send() delivers the command). Read best-effort, ignore errors.
470 beast::flat_buffer buffer;
471 ws_read(ioc, ws, buffer);
472 }
473
474 ws_close(ioc, ws, ws_settings.close);
475 return true;
476 } catch(std::exception const& e) {
477 BOOST_LOG_TRIVIAL(error) << "CrealityPrint: Error starting print: " << e.what();
478 // Shows a websocket error without Boost's source location, and a timeout the same way as an HTTP one
479 if (const auto* system_error = dynamic_cast<const beast::system_error*>(&e))
480 msg = system_error->code() == beast::error::timeout ? timed_out_message() : wxString::FromUTF8(system_error->code().message());
481 else
482 msg = wxString::FromUTF8(e.what());
483 return false;
484 }
485}
bool supports_multi_color_print() const
Definition CrealityPrint.cpp:342
static void ws_send(net::io_context &ioc, websocket::stream< beast::tcp_stream > &ws, const json &cmd)
Definition CrealityPrint.cpp:244
static bool is_k1_family(const std::string &model)
Definition CrealityPrint.cpp:293
static beast::error_code ws_read(net::io_context &ioc, websocket::stream< beast::tcp_stream > &ws, beast::flat_buffer &buffer)
Definition CrealityPrint.cpp:250

References Slic3r::is_k1_family(), m_host, m_model, supports_multi_color_print(), Slic3r::PrintHost::timed_out_message(), Slic3r::ws_close(), Slic3r::ws_connect(), Slic3r::ws_read(), Slic3r::ws_send(), and ws_settings.

Referenced by upload().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ supports_multi_color_print()

bool Slic3r::CrealityPrint::supports_multi_color_print ( ) const
343{
344 query_model();
346}
static bool model_supports_multi_color(const std::string &model)
Definition CrealityPrint.cpp:330

References m_model, model_supports_multi_color(), and query_model().

Referenced by Slic3r::CrealityPrintAgent::fetch_filament_info(), start_print(), and upload().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ supports_multiple_printers()

virtual bool Slic3r::PrintHost::supports_multiple_printers ( ) const
inlinevirtualinherited

Reimplemented in Slic3r::Repetier.

73{ return false; }

◆ test()

bool Slic3r::CrealityPrint::test ( wxString & curl_msg) const
overridevirtual

Implements Slic3r::PrintHost.

95{
96 bool res = true;
97 const char* name = get_name();
98 auto url = make_url("info");
99
100 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url;
101 // Here we do not have to add custom "Host" header - the url contains host filled by user and libCurl will set the header by itself.
102 auto http = Http::get(std::move(url));
103 set_auth(http);
104 http.timeout_max(5)
105 .on_error([&](std::string body, std::string error, unsigned status) {
106 BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status %
107 body;
108 res = false;
109 msg = format_error(body, error, status);
110 })
111 .on_complete([&, this](std::string body, unsigned) {
112 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got version: %2%") % name % body;
113 try {
114 auto info = json::parse(body);
115 if (info.contains("model")) {
116 m_model = info["model"].get<std::string>();
117 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Detected model: %2%") % name % m_model;
118 }
119 } catch (const json::exception& e) {
120 BOOST_LOG_TRIVIAL(warning) << boost::format("%1%: Failed to parse /info response: %2%") % name % e.what();
121 }
122 })
123#ifdef WIN32
124 .ssl_revoke_best_effort(m_ssl_revoke_best_effort)
125 .on_ip_resolve([&](std::string address) {
126 // Workaround for Windows 10/11 mDNS resolve issue, where two mDNS resolves in succession fail.
127 // Remember resolved address to be reused at successive REST API call.
128 msg = GUI::from_u8(address);
129 })
130#endif // WIN32
131 .perform_sync();
132
133 return res;
134}
virtual void set_auth(Http &http) const
Definition CrealityPrint.cpp:79
const char * get_name() const override
Definition CrealityPrint.cpp:74
std::string make_url(const std::string &path) const
Definition CrealityPrint.cpp:194
static Http get(std::string url)
Definition Http.cpp:886
virtual wxString format_error(const std::string &body, const std::string &error, unsigned status) const
Definition PrintHost.cpp:202
wxString from_u8(const std::string &str)
Definition GUI.cpp:544

References Slic3r::PrintHost::format_error(), Slic3r::GUI::from_u8(), Slic3r::Http::get(), get_name(), Slic3r::http, m_model, m_ssl_revoke_best_effort, make_url(), and set_auth().

Referenced by query_model(), and upload().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timed_out_message()

wxString Slic3r::PrintHost::timed_out_message ( )
staticprotectedinherited
223{
224 return _L("Connection timed out. Please check if the printer and computer network are functioning properly, and confirm that they are on the same network.");
225}

References _L.

Referenced by format_error(), and Slic3r::CrealityPrint::start_print().

Here is the caller graph for this function:

◆ upload()

bool Slic3r::CrealityPrint::upload ( PrintHostUpload upload_data,
ProgressFn prorgess_fn,
ErrorFn error_fn,
InfoFn info_fn ) const
overridevirtual

Implements Slic3r::PrintHost.

141{
142 const char* name = get_name();
143 const auto upload_filename = upload_data.upload_path.filename();
144 const auto upload_parent_path = upload_data.upload_path.parent_path();
145 wxString test_msg;
146 if (!test(test_msg)) {
147 error_fn(std::move(test_msg));
148 return false;
149 }
150
151 bool res = true;
152 const auto safe_upload_filename = safe_filename(upload_filename.string());
153 // Only encode the URL path segment; keep the multipart filename and start-print path as the stored filename.
154 auto url = make_url("upload/" + Http::url_encode(safe_upload_filename));
155
156 auto http = Http::post(url); // std::move(url));
157 set_auth(http);
159 http.form_add("path", upload_parent_path.string());
160 http.form_add_file("file", upload_data.source_path.string(), safe_upload_filename)
161
162 .on_complete([&](std::string body, unsigned status) {
163 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: File uploaded: HTTP %2%: %3%") % name % status % body;
164
165 if (upload_data.post_action == PrintHostPostUploadAction::StartPrint) {
166 wxString errormsg;
167 if (!start_print(errormsg, safe_upload_filename, upload_data.extended_info)) {
168 error_fn(std::move(errormsg));
169 res = false;
170 }
171 }
172 })
173 .on_error([&](std::string body, std::string error, unsigned status) {
174 BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error uploading file to %2%: %3%, HTTP %4%, body: `%5%`") % name % url % error %
175 status % body;
176 error_fn(format_error(body, error, status));
177 res = false;
178 })
179 .on_progress([&](Http::Progress progress, bool& cancel) {
180 prorgess_fn(std::move(progress), cancel);
181 if (cancel) {
182 // Upload was canceled
183 BOOST_LOG_TRIVIAL(info) << name << ": Upload canceled";
184 res = false;
185 }
186 })
187#ifdef WIN32
188 .ssl_revoke_best_effort(m_ssl_revoke_best_effort)
189#endif
190 .perform_sync();
191 return res;
192}
bool start_print(wxString &msg, const std::string &filename, const std::map< std::string, std::string > &extended_info) const
Definition CrealityPrint.cpp:376
static std::string url_encode(const std::string &str)
Definition Http.cpp:969
static Http post(std::string url)
Definition Http.cpp:891

References Slic3r::PrintHostUpload::extended_info, Slic3r::PrintHost::format_error(), get_name(), Slic3r::http, m_ssl_revoke_best_effort, make_url(), Slic3r::Http::post(), Slic3r::PrintHostUpload::post_action, safe_filename(), set_auth(), Slic3r::PrintHostUpload::source_path, start_print(), Slic3r::StartPrint, supports_multi_color_print(), test(), Slic3r::PrintHostUpload::upload_path, and Slic3r::Http::url_encode().

Here is the call graph for this function:

Member Data Documentation

◆ m_apikey

std::string Slic3r::CrealityPrint::m_apikey
private

Referenced by CrealityPrint(), and set_auth().

◆ m_cafile

std::string Slic3r::CrealityPrint::m_cafile
private

Referenced by CrealityPrint(), and set_auth().

◆ m_host

std::string Slic3r::CrealityPrint::m_host
private

◆ m_model

std::string Slic3r::CrealityPrint::m_model
mutableprivate

◆ m_port

std::string Slic3r::CrealityPrint::m_port
private

Referenced by CrealityPrint().

◆ m_ssl_revoke_best_effort

bool Slic3r::CrealityPrint::m_ssl_revoke_best_effort
private

Referenced by CrealityPrint(), test(), and upload().

◆ m_web_ui

std::string Slic3r::CrealityPrint::m_web_ui
private

Referenced by CrealityPrint().

◆ ws_settings

struct Slic3r::CrealityPrint::WsSettings Slic3r::CrealityPrint::ws_settings
protected

Referenced by query_boxes_info(), and start_print().


The documentation for this class was generated from the following files: