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

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

Inheritance diagram for Slic3r::C3DPrinterOS:
Collaboration diagram for Slic3r::C3DPrinterOS:

Public Types

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

Public Member Functions

 C3DPrinterOS (DynamicPrintConfig *config)
 ~C3DPrinterOS () override=default
const char * get_name () const override
bool test (wxString &curl_msg) const override
bool login (wxString &msg) const
wxString get_test_ok_msg () const override
wxString get_test_failed_msg (wxString &msg) const override
bool upload (PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, InfoFn info_fn) const override
bool has_auto_discovery () const override
bool can_test () const override
bool is_cloud () const override
void log_out () const override
bool is_logged_in () const override
PrintHostPostUploadActions get_post_upload_actions () const override
std::string get_host () const override
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 get_login_url (wxString &auth_url) const

Static Public Member Functions

static std::string default_host ()
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

bool validate_version_text (const boost::optional< std::string > &version_text) 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 ()

Private Member Functions

void load_api_session ()
bool save_api_session (const std::string &session, const std::string &email) const
std::string parse_printer_model (const std::string &input) const
std::string make_url (const std::string &path) const
std::string get_api_auth_token (wxString &err) const
void login_with_token (boost::property_tree::ptree &resp, const std::string &token) const
bool check_session (wxString &msg) const
void send_form (const std::string &endpoint, const std::string &postBody, boost::property_tree::ptree &responseTree) const
void get_cloud_projects_list (boost::property_tree::ptree &response) const
void get_cloud_printer_types (boost::property_tree::ptree &response, const std::string &querry) const
void update_file (boost::property_tree::ptree &response, const std::string &file_id, const std::string &ptype, const std::string &gtype) const

Private Attributes

std::string m_host
std::string m_apikey
std::string m_cafile
std::string m_username
std::string m_host_type
std::string m_preset_name
std::string m_api_session_file_path

Detailed Description

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

◆ C3DPrinterOS()

Slic3r::C3DPrinterOS::C3DPrinterOS ( DynamicPrintConfig * config)
316 : m_host(config->opt_string("print_host"))
317 , m_apikey(config->opt_string("printhost_apikey"))
318 , m_preset_name(config->opt_string("printer_model"))
319{
320 m_api_session_file_path = (boost::filesystem::path(Slic3r::data_dir()) / API_CREDENTIALS_PATH)
321 .make_preferred()
322 .string();
324}
std::string m_apikey
Definition 3DPrinterOS.hpp:49
std::string m_api_session_file_path
Definition 3DPrinterOS.hpp:54
std::string m_preset_name
Definition 3DPrinterOS.hpp:53
void load_api_session()
Definition 3DPrinterOS.cpp:610
std::string m_host
Definition 3DPrinterOS.hpp:48
const std::string & data_dir()
Definition utils.cpp:365
static const std::string API_CREDENTIALS_PATH
Definition 3DPrinterOS.cpp:313

References Slic3r::API_CREDENTIALS_PATH, Slic3r::data_dir(), load_api_session(), m_api_session_file_path, m_apikey, m_host, and m_preset_name.

Here is the call graph for this function:

◆ ~C3DPrinterOS()

Slic3r::C3DPrinterOS::~C3DPrinterOS ( )
overridedefault

Member Function Documentation

◆ can_test()

bool Slic3r::C3DPrinterOS::can_test ( ) const
inlineoverridevirtual

Implements Slic3r::PrintHost.

36{ return true; }

◆ check_session()

bool Slic3r::C3DPrinterOS::check_session ( wxString & msg) const
private
573 {
574 std::string postBody = "session=" + m_apikey;
575 pt::ptree resp;
576 send_form("apiglobal/check_session", postBody, resp);
577 try {
578 if (resp.get<bool>("result")) {
579 return true;
580 } else {
581 msg = wxString::FromUTF8(resp.get<std::string>("message"));
582 return false;
583 }
584
585 } catch (const std::exception &) {
586 msg = _L("Could not parse server response.");
587 return false;
588 }
589 return false;
590}
#define _L(s)
Definition OBJ.cpp:34
void send_form(const std::string &endpoint, const std::string &postBody, boost::property_tree::ptree &responseTree) const
Definition 3DPrinterOS.cpp:629

References _L, m_apikey, and send_form().

Referenced by test(), and upload().

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

◆ default_host()

std::string Slic3r::C3DPrinterOS::default_host ( )
inlinestatic
42{ return "https://cloud.3dprinteros.com"; }

Referenced by Slic3r::GUI::PhysicalPrinterDialog::update().

Here is the caller graph for this function:

◆ 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}
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_api_auth_token()

std::string Slic3r::C3DPrinterOS::get_api_auth_token ( wxString & err) const
private
550{
551 std::string result;
552 pt::ptree resp;
553 std::string postBody = "app_type=plugin&app_name=" + Http::url_encode("OrcaSlicer");
554 send_form("apiglobal/generate_login_token", postBody, resp);
555 try {
556 if (resp.get<bool>("result")) {
557 result = resp.get<std::string>("message");
558 } else {
559 err = wxString::FromUTF8(resp.get<std::string>("message"));
560 }
561 } catch (const std::exception &) {
562 err = _L("Could not parse server response.");
563 }
564 return result;
565}
static std::string url_encode(const std::string &str)
Definition Http.cpp:969

References _L, send_form(), and Slic3r::Http::url_encode().

Referenced by login().

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

◆ get_cloud_printer_types()

void Slic3r::C3DPrinterOS::get_cloud_printer_types ( boost::property_tree::ptree & response,
const std::string & querry ) const
private
667{
668 std::string postBody = std::string("session=" + m_apikey);
669 if (!query.empty()) {
670 postBody += "&description=" + Http::url_encode(query) + "&software_version=" + Http::url_encode("OrcaSlicer");
671 }
672 send_form("apiglobal/get_printer_types", postBody, response);
673}

References m_apikey, send_form(), and Slic3r::Http::url_encode().

Referenced by upload().

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

◆ get_cloud_projects_list()

void Slic3r::C3DPrinterOS::get_cloud_projects_list ( boost::property_tree::ptree & response) const
private
661{
662 std::string postBody = std::string("session=" + m_apikey);
663 send_form("apiglobal/get_projects", postBody, response);
664}

References m_apikey, and send_form().

Referenced by upload().

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::C3DPrinterOS::get_host ( ) const
inlineoverridevirtual

Implements Slic3r::PrintHost.

41{ return m_host; }

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::C3DPrinterOS::get_name ( ) const
overridevirtual

Implements Slic3r::PrintHost.

326{ return "3DPrinterOS"; }

Referenced by upload().

Here is the caller graph for this function:

◆ get_post_upload_actions()

PrintHostPostUploadActions Slic3r::C3DPrinterOS::get_post_upload_actions ( ) const
inlineoverridevirtual

◆ 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::C3DPrinterOS::get_test_failed_msg ( wxString & msg) const
overridevirtual

Implements Slic3r::PrintHost.

377{
378 return GUI::format_wxstr("%s: %s\n\n", _L("Error session check"), msg);
379}
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::C3DPrinterOS::get_test_ok_msg ( ) const
overridevirtual

Implements Slic3r::PrintHost.

368{
369 wxString msg = _L("Connection to 3DPrinterOS cloud works correctly.");
370 if (!m_username.empty())
371 // TRN %1% is the 3DPrinterOS account (e-mail address).
372 msg += " " + GUI::format_wxstr(_L("Logged in as user: %1%"), m_username);
373 return msg;
374}
std::string m_username
Definition 3DPrinterOS.hpp:51

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

Here is the call graph for this function:

◆ has_auto_discovery()

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

Implements Slic3r::PrintHost.

35{ return false; }

◆ is_cloud()

bool Slic3r::C3DPrinterOS::is_cloud ( ) const
inlineoverridevirtual

Reimplemented from Slic3r::PrintHost.

37{ return true; }

◆ is_logged_in()

bool Slic3r::C3DPrinterOS::is_logged_in ( ) const
inlineoverridevirtual

Reimplemented from Slic3r::PrintHost.

39{ return !m_apikey.empty(); }

References m_apikey.

◆ load_api_session()

void Slic3r::C3DPrinterOS::load_api_session ( )
private
611{
612 m_apikey.clear();
613 if (boost::filesystem::exists(m_api_session_file_path)) {
614 pt::ptree j;
615 try {
616 pt::read_json(m_api_session_file_path, j);
617 m_apikey = j.get<std::string>("session");
618 m_username = j.get<std::string>("email");
619 } catch (const std::exception &err) {
620 BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": load_api_session failed, reason = " << err.what();
621 // remove corrupted file to avoid repeated failures
622 try {
623 boost::filesystem::remove(m_api_session_file_path);
624 } catch (...) {}
625 }
626 };
627}

References m_api_session_file_path, m_apikey, and m_username.

Referenced by C3DPrinterOS().

Here is the caller graph for this function:

◆ log_out()

void Slic3r::C3DPrinterOS::log_out ( ) const
overridevirtual

Reimplemented from Slic3r::PrintHost.

527{
528 boost::filesystem::remove(m_api_session_file_path.c_str());
529}

References m_api_session_file_path.

◆ login()

bool Slic3r::C3DPrinterOS::login ( wxString & msg) const
334{
335 // Get token for auth
336 msg.clear();
337 std::string token = get_api_auth_token(msg);
338 if (token.empty()) {
339 msg = _L("Error. Can't get API token for authorization");
340 return false;
341 }
342
343 auto login_url = make_url("noauth/apiglobal_login_with_token/" + token);
344 wxLaunchDefaultBrowser(login_url);
345 pt::ptree login_resp;
346 login_with_token(login_resp, token);
347 std::string session, email;
348 try {
349 if (login_resp.get<bool>("result")) {
350 session = login_resp.get<std::string>("message.session");
351 email = login_resp.get<std::string>("message.email");
352 } else {
353 msg = wxString::FromUTF8(login_resp.get<std::string>("message"));
354 return false;
355 }
356 } catch (const std::exception&) {
357 msg = _L("Could not parse server response.");
358 return false;
359 }
360 bool res = save_api_session(session, email);
361 if (!res) {
362 msg = _L("Error saving session to file");
363 }
364 return res;
365}
bool save_api_session(const std::string &session, const std::string &email) const
Definition 3DPrinterOS.cpp:592
std::string get_api_auth_token(wxString &err) const
Definition 3DPrinterOS.cpp:549
void login_with_token(boost::property_tree::ptree &resp, const std::string &token) const
Definition 3DPrinterOS.cpp:567
std::string make_url(const std::string &path) const
Definition 3DPrinterOS.cpp:536

References _L, get_api_auth_token(), login_with_token(), make_url(), and save_api_session().

Here is the call graph for this function:

◆ login_with_token()

void Slic3r::C3DPrinterOS::login_with_token ( boost::property_tree::ptree & resp,
const std::string & token ) const
private
567 {
568 auto url = make_url("apiglobal/login_with_token");
569 TokenAuthDialog dlg(GUI::wxGetApp().GetTopWindow(), url, token, m_cafile, resp);
570 dlg.ShowModal();
571}
std::string m_cafile
Definition 3DPrinterOS.hpp:50

References m_cafile, and make_url().

Referenced by login().

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

◆ make_url()

std::string Slic3r::C3DPrinterOS::make_url ( const std::string & path) const
private
537{
538 if (m_host.find("http://") == 0 || m_host.find("https://") == 0) {
539 if (m_host.back() == '/') {
540 return (boost::format("%1%%2%") % m_host % path).str();
541 } else {
542 return (boost::format("%1%/%2%") % m_host % path).str();
543 }
544 } else {
545 return (boost::format("https://%1%/%2%") % m_host % path).str();
546 }
547}

References m_host.

Referenced by login(), login_with_token(), send_form(), and upload().

Here is the caller graph for this function:

◆ parse_printer_model()

std::string Slic3r::C3DPrinterOS::parse_printer_model ( const std::string & input) const
private

◆ save_api_session()

bool Slic3r::C3DPrinterOS::save_api_session ( const std::string & session,
const std::string & email ) const
private
592 {
593 pt::ptree j;
594 j.put("session", session);
595 j.put("email", email);
596 try {
597 std::ostringstream json;
598 pt::write_json(json, j);
599 if (const std::error_code ec = write_file_atomically(m_api_session_file_path, json.str()))
600 throw std::system_error(ec);
601 } catch (const std::exception &err) {
602 BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": failed to write json to file. Path = "
604 << " Reason = " << err.what();
605 return false;
606 }
607 return true;
608}
std::error_code write_file_atomically(const std::string &path, std::initializer_list< std::string_view > chunks, bool binary)
Definition utils.cpp:796
nlohmann::json json
Definition AssembleList.cpp:42

References m_api_session_file_path, and Slic3r::write_file_atomically().

Referenced by login().

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

◆ send_form()

void Slic3r::C3DPrinterOS::send_form ( const std::string & endpoint,
const std::string & postBody,
boost::property_tree::ptree & responseTree ) const
private
634{
635 responseTree.clear();
636 auto url = make_url(endpoint);
637 auto http = Http::post(std::move(url));
638 if (!m_cafile.empty()) {
639 http.ca_file(m_cafile);
640 }
641 http.header("Content-length", std::to_string(postBody.size()));
642 http.set_post_body(postBody);
643 http.on_error([&](std::string body, std::string error, unsigned status) {
644 BOOST_LOG_TRIVIAL(error) << boost::format("Error sending form: %1%") % error;
645 responseTree.put("result", false);
646 responseTree.put("message", error);
647 })
648 .on_complete([&](std::string body, unsigned) {
649 std::stringstream ss(body);
650 try {
651 pt::read_json(ss, responseTree);
652 } catch (const std::exception &) {
653 responseTree.put("result", false);
654 responseTree.put("message", _u8L("Could not parse server response."));
655 }
656 })
657 .perform_sync();
658}
static Http post(std::string url)
Definition Http.cpp:891
@ http
Definition IPrinterAgent.hpp:64
#define _u8L(s)
Definition I18N.hpp:11

References _u8L, Slic3r::http, m_cafile, make_url(), and Slic3r::Http::post().

Referenced by check_session(), get_api_auth_token(), get_cloud_printer_types(), get_cloud_projects_list(), and update_file().

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::C3DPrinterOS::test ( wxString & curl_msg) const
overridevirtual

Implements Slic3r::PrintHost.

329{
330 return check_session(msg);
331}
bool check_session(wxString &msg) const
Definition 3DPrinterOS.cpp:573

References check_session().

Here is the call 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:

◆ update_file()

void Slic3r::C3DPrinterOS::update_file ( boost::property_tree::ptree & response,
const std::string & file_id,
const std::string & ptype,
const std::string & gtype ) const
private
676{
677 std::string postBody = "session=" + m_apikey
678 + "&updates[" + file_id + "][ptype]=" + ptype
679 + "&updates[" + file_id + "][gtype]=" + Http::url_encode(gtype)
680 + "&updates[" + file_id + "][zip]=false";
681 send_form("apiglobal/file_update", postBody, response);
682}

References m_apikey, send_form(), and Slic3r::Http::url_encode().

Referenced by upload().

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

◆ upload()

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

Implements Slic3r::PrintHost.

384{
385 const char *name = get_name();
386 const auto upload_filename = upload_data.upload_path.filename();
387 const auto upload_parent_path = upload_data.upload_path.parent_path();
388 wxString test_msg;
389 if (!check_session(test_msg)) {
390 error_fn(std::move(test_msg));
391 return false;
392 }
393
394 pt::ptree cloud_project_resp;
395 pt::ptree cloud_printer_types_resp;
396
397 get_cloud_projects_list(cloud_project_resp);
398 get_cloud_printer_types(cloud_printer_types_resp, m_preset_name);
399 wxArrayString cloud_projects_list;
400 wxArrayString cloud_printer_types_list;
401
402 try {
403 if (cloud_project_resp.get<bool>("result")) {
404 for (const auto &messageItem : cloud_project_resp.get_child("message")) {
405 cloud_projects_list.Add(messageItem.second.get<std::string>("name"));
406 }
407 }
408
409 if (cloud_printer_types_resp.get<bool>("result")) {
410 for (const auto &messageItem : cloud_printer_types_resp.get_child("message")) {
411 cloud_printer_types_list.Add(messageItem.second.get<std::string>("description"));
412 }
413 }
414 } catch (const std::exception &) {
415 error_fn(_L("Could not parse server response."));
416 return false;
417 }
418
419 // Show "Confirm cloud printer type and project for 3DPrinterOS upload
420
421 UploadOptionsDialog dlg(GUI::wxGetApp().GetTopWindow(), cloud_projects_list, cloud_printer_types_list, m_preset_name);
422
423 if (dlg.ShowModal() != wxID_OK) {
424 error_fn(_L("Canceled"));
425 return false;
426 }
427
428 std::string selected_project;
429 std::string selected_printer_type;
430 dlg.GetValues(selected_project, selected_printer_type);
431 std::string project_id;
432 std::string printer_type_id;
433
434 // search for cloud project_id by name
435 if (!selected_project.empty()) {
436 for (const auto& messageItem : cloud_project_resp.get_child("message")) {
437 if (messageItem.second.get<std::string>("name", "") == selected_project) {
438 project_id = messageItem.second.get<std::string>("id", "");
439 break;
440 }
441 }
442 }
443
444 // search for cloud printer_type_id by name
445 for (const auto& messageItem : cloud_printer_types_resp.get_child("message")) {
446 if (messageItem.second.get<std::string>("description", "") == selected_printer_type) {
447 printer_type_id = messageItem.second.get<std::string>("id", "");
448 break;
449 }
450 }
451
452 bool res = true;
453 auto url = make_url("apiglobal/upload");
454 std::string file_id;
455 pt::ptree uploadResponse;
456 auto http = Http::post(std::move(url));
457 if (!m_cafile.empty()) {
458 http.ca_file(m_cafile);
459 }
460 http.form_add("session", m_apikey)
461 .form_add("upload_type_id", "7")
462 .form_add("upload_soft_name", "OrcaSlicer")
463 .form_add("zip", "false")
464 .form_add_file("file", upload_data.source_path.string(), upload_filename.string());
465
466 if (!project_id.empty()) {
467 http.form_add("project_id", project_id);
468 } else if (!selected_project.empty()) {
469 http.form_add("project_name", selected_project);
470 http.form_add("project_color", "grey");
471 }
472
473 http.on_complete([&](std::string body, unsigned status) {
474 std::stringstream ss(body);
475 try {
476 pt::read_json(ss, uploadResponse);
477 } catch (const std::exception &) {
478 uploadResponse.put("result", false);
479 uploadResponse.put("message", _u8L("Could not parse server response."));
480 }
481 })
482 .on_error([&](std::string body, std::string error, unsigned status) {
483 error_fn(format_error(body, error, status));
484 res = false;
485 })
486 .on_progress([&](Http::Progress progress, bool &cancel) {
487 prorgess_fn(std::move(progress), cancel);
488 if (cancel) {
489 res = false;
490 }
491 })
492 .perform_sync();
493
494 try {
495 if (uploadResponse.get<bool>("result")) {
496 file_id = uploadResponse.get<std::string>("message.file_id");
497 } else {
498 res = false;
499 error_fn(wxString::FromUTF8(uploadResponse.get<std::string>("message")));
500 }
501 } catch (const std::exception &) {
502 res = false;
503 error_fn(_L("Error during file upload"));
504 }
505 // set printer type for uploaded gcode
506 if (res) {
507 pt::ptree update_file_response;
508 update_file(update_file_response, file_id, printer_type_id, "OrcaSlicer");
509 try {
510 if (!update_file_response.get<bool>("result")) {
511 const std::string msg = update_file_response.get<std::string>("message", "Unknown update error");
512 BOOST_LOG_TRIVIAL(warning) << "Failed to update uploaded file: " << msg;
513 }
514 } catch (const std::exception& ex) {
515 BOOST_LOG_TRIVIAL(warning) << "Could not parse update response: " << ex.what();
516 }
517 if (upload_data.post_action == PrintHostPostUploadAction::StartPrint && !upload_data.use_3mf) {
518 auto quick_print_url = make_url("quickprint?file_id=" + file_id);
519 wxLaunchDefaultBrowser(quick_print_url);
520 }
521 }
522
523 return res;
524}
const char * get_name() const override
Definition 3DPrinterOS.cpp:326
void get_cloud_printer_types(boost::property_tree::ptree &response, const std::string &querry) const
Definition 3DPrinterOS.cpp:666
void update_file(boost::property_tree::ptree &response, const std::string &file_id, const std::string &ptype, const std::string &gtype) const
Definition 3DPrinterOS.cpp:675
void get_cloud_projects_list(boost::property_tree::ptree &response) const
Definition 3DPrinterOS.cpp:660
virtual wxString format_error(const std::string &body, const std::string &error, unsigned status) const
Definition PrintHost.cpp:202

References _L, _u8L, check_session(), Slic3r::PrintHost::format_error(), get_cloud_printer_types(), get_cloud_projects_list(), get_name(), Slic3r::http, m_apikey, m_cafile, m_preset_name, make_url(), Slic3r::Http::post(), Slic3r::PrintHostUpload::post_action, Slic3r::PrintHostUpload::source_path, Slic3r::StartPrint, update_file(), Slic3r::PrintHostUpload::upload_path, and Slic3r::PrintHostUpload::use_3mf.

Here is the call graph for this function:

◆ validate_version_text()

bool Slic3r::C3DPrinterOS::validate_version_text ( const boost::optional< std::string > & version_text) const
protected
532{
533 return version_text ? boost::starts_with(*version_text, "3DPrinterOS") : true;
534}

Member Data Documentation

◆ m_api_session_file_path

std::string Slic3r::C3DPrinterOS::m_api_session_file_path
private

◆ m_apikey

◆ m_cafile

std::string Slic3r::C3DPrinterOS::m_cafile
private

Referenced by login_with_token(), send_form(), and upload().

◆ m_host

std::string Slic3r::C3DPrinterOS::m_host
private

Referenced by C3DPrinterOS(), get_host(), and make_url().

◆ m_host_type

std::string Slic3r::C3DPrinterOS::m_host_type
private

◆ m_preset_name

std::string Slic3r::C3DPrinterOS::m_preset_name
private

Referenced by C3DPrinterOS(), and upload().

◆ m_username

std::string Slic3r::C3DPrinterOS::m_username
private

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