Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::Moonraker Class Reference

#include <src/slic3r/Utils/Moonraker.hpp>

+ Inheritance diagram for Slic3r::Moonraker:
+ Collaboration diagram for Slic3r::Moonraker:

Public Types

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

Public Member Functions

 Moonraker (DynamicPrintConfig *config)
 
 ~Moonraker () override=default
 
const char * get_name () const override
 
virtual bool test (wxString &curl_msg) const override
 
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
 
PrintHostPostUploadActions get_post_upload_actions () const override
 
std::string get_host () const override
 
const std::string & get_apikey () const
 
const std::string & get_cafile () const
 
virtual bool supports_multiple_printers () const
 
virtual bool get_groups (wxArrayString &) const
 
virtual bool get_printers (wxArrayString &) const
 
virtual bool get_storage (wxArrayString &, wxArrayString &) const
 

Static Public Member Functions

static PrintHostget_print_host (DynamicPrintConfig *config)
 

Protected Member Functions

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

Protected Attributes

std::string m_host
 
std::string m_apikey
 
std::string m_cafile
 
bool m_ssl_revoke_best_effort
 

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

◆ Moonraker()

Slic3r::Moonraker::Moonraker ( DynamicPrintConfig config)
66 :
67 m_host(config->opt_string("print_host")),
68 m_apikey(config->opt_string("printhost_apikey")),
69 m_cafile(config->opt_string("printhost_cafile")),
70 m_ssl_revoke_best_effort(config->opt_bool("printhost_ssl_ignore_revoke"))
71{}
std::string m_cafile
Definition Moonraker.hpp:48
std::string m_host
Definition Moonraker.hpp:46
bool m_ssl_revoke_best_effort
Definition Moonraker.hpp:49
std::string m_apikey
Definition Moonraker.hpp:47

◆ ~Moonraker()

Slic3r::Moonraker::~Moonraker ( )
overridedefault

Member Function Documentation

◆ can_test()

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

Implements Slic3r::PrintHost.

32{ return true; }

◆ format_error()

wxString Slic3r::PrintHost::format_error ( const std::string &  body,
const std::string &  error,
unsigned  status 
) const
protectedvirtualinherited
67{
68 if (status != 0) {
69 auto wxbody = wxString::FromUTF8(body.data());
70 return wxString::Format("HTTP %u: %s", status, wxbody);
71 } else {
72 return wxString::FromUTF8(error.data());
73 }
74}
static char error[256]
Definition tga.cpp:50

References error.

Referenced by Slic3r::Duet::connect(), Slic3r::PrusaLink::post_inner(), Slic3r::PrusaLink::put_inner(), Slic3r::Duet::start_print(), Slic3r::AstroBox::test(), Slic3r::FlashAir::test(), test(), Slic3r::OctoPrint::test(), Slic3r::PrusaLink::test(), Slic3r::Repetier::test(), Slic3r::PrusaLink::test_with_method_check(), Slic3r::AstroBox::upload(), Slic3r::Duet::upload(), Slic3r::FlashAir::upload(), Slic3r::MKS::upload(), upload(), Slic3r::Repetier::upload(), and Slic3r::OctoPrint::upload_inner_with_host().

+ Here is the caller graph for this function:

◆ get_apikey()

const std::string & Slic3r::Moonraker::get_apikey ( ) const
inline
35{ return m_apikey; }

References m_apikey.

◆ get_cafile()

const std::string & Slic3r::Moonraker::get_cafile ( ) const
inline
36{ return m_cafile; }

References m_cafile.

◆ get_groups()

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

Reimplemented in Slic3r::Repetier.

65{ return false; }

◆ get_host()

std::string Slic3r::Moonraker::get_host ( ) const
inlineoverridevirtual

Implements Slic3r::PrintHost.

34{ return m_host; }

References m_host.

◆ get_name()

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

Implements Slic3r::PrintHost.

73{ return "Moonraker"; }

Referenced by test(), and upload().

+ Here is the caller graph for this function:

◆ get_post_upload_actions()

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

◆ get_print_host()

PrintHost * Slic3r::PrintHost::get_print_host ( DynamicPrintConfig config)
staticinherited
35{
37
38 {
39 const auto opt = config->option<ConfigOptionEnum<PrinterTechnology>>("printer_technology");
40 if (opt != nullptr) {
41 tech = opt->value;
42 }
43 }
44
45 if (tech == ptFFF) {
46 const auto opt = config->option<ConfigOptionEnum<PrintHostType>>("host_type");
47 const auto host_type = opt != nullptr ? opt->value : htOctoPrint;
48
49 switch (host_type) {
50 case htOctoPrint: return new OctoPrint(config);
51 case htDuet: return new Duet(config);
52 case htFlashAir: return new FlashAir(config);
53 case htAstroBox: return new AstroBox(config);
54 case htRepetier: return new Repetier(config);
55 case htPrusaLink: return new PrusaLink(config);
56 case htPrusaConnect: return new PrusaConnect(config);
57 case htMKS: return new MKS(config);
58 case htMoonraker: return new Moonraker(config);
59 default: return nullptr;
60 }
61 } else {
62 return new SL1Host(config);
63 }
64}
PrinterTechnology
Definition Config.hpp:205
@ ptFFF
Definition Config.hpp:207
@ htMoonraker
Definition PrintConfig.hpp:47
@ htPrusaLink
Definition PrintConfig.hpp:47
@ htRepetier
Definition PrintConfig.hpp:47
@ htMKS
Definition PrintConfig.hpp:47
@ htPrusaConnect
Definition PrintConfig.hpp:47
@ htDuet
Definition PrintConfig.hpp:47
@ htOctoPrint
Definition PrintConfig.hpp:47
@ htFlashAir
Definition PrintConfig.hpp:47
@ htAstroBox
Definition PrintConfig.hpp:47
ConfigOptionEnum< PrinterTechnology >
Definition PrintConfig.hpp:1079

References Slic3r::htAstroBox, Slic3r::htDuet, Slic3r::htFlashAir, Slic3r::htMKS, Slic3r::htMoonraker, Slic3r::htOctoPrint, Slic3r::htPrusaConnect, Slic3r::htPrusaLink, Slic3r::htRepetier, Slic3r::ConfigBase::option(), Slic3r::ptFFF, and Slic3r::ConfigOptionSingle< T >::value.

Referenced by Slic3r::GUI::PhysicalPrinterDialog::build_printhost_settings(), 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_printers()

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

Reimplemented in Slic3r::Repetier.

66{ return false; }

◆ get_storage()

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

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

69{ return false; }

◆ get_test_failed_msg()

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

Implements Slic3r::PrintHost.

81{
82 return GUI::format_wxstr("%s: %s"
83 , _L("Could not connect to Moonraker")
84 , msg);
85}
wxString format_wxstr(const char *fmt, TArgs &&... args)
Definition format.hpp:42
#define _L(s)
Definition I18N.hpp:3

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

+ Here is the call graph for this function:

◆ get_test_ok_msg()

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

Implements Slic3r::PrintHost.

76{
77 return _(L("Connection to Moonraker works correctly."));
78}
#define _(msgid)
Definition getopt.c:87
#define L(s)
Definition I18N.hpp:18

References _, and L.

◆ has_auto_discovery()

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

Implements Slic3r::PrintHost.

31{ return true; }

◆ make_url()

std::string Slic3r::Moonraker::make_url ( const std::string &  path) const
protected
244{
245 if (m_host.find("http://") == 0 || m_host.find("https://") == 0) {
246 if (m_host.back() == '/') {
247 return (boost::format("%1%%2%") % m_host % path).str();
248 } else {
249 return (boost::format("%1%/%2%") % m_host % path).str();
250 }
251 } else {
252 return (boost::format("http://%1%/%2%") % m_host % path).str();
253 }
254}

References m_host.

Referenced by test(), and upload().

+ Here is the caller graph for this function:

◆ set_auth()

void Slic3r::Moonraker::set_auth ( Http http) const
protectedvirtual
236{
237 if (!m_apikey.empty())
238 http.header("X-Api-Key", m_apikey);
239 if (!m_cafile.empty())
240 http.ca_file(m_cafile);
241}

References Slic3r::Http::ca_file(), Slic3r::Http::header(), m_apikey, and m_cafile.

Referenced by test(), 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.

60{ return false; }

◆ test()

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

Implements Slic3r::PrintHost.

88{
89 // GET /server/info
90
91 // Since the request is performed synchronously here,
92 // it is ok to refer to `msg` from within the closure
93 const char* name = get_name();
94
95 bool res = true;
96 auto url = make_url("server/info");
97
98 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url;
99
100 auto http = Http::get(std::move(url));
101 set_auth(http);
102 http.on_error([&](std::string body, std::string error, unsigned status) {
103 BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
104 res = false;
105 msg = format_error(body, error, status);
106 })
107 .on_complete([&](std::string body, unsigned) {
108 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got server/info: %2%") % name % body;
109
110 try {
111 // All successful HTTP requests will return a json encoded object in the form of :
112 // {result: <response data>}
113 std::stringstream ss(body);
114 pt::ptree ptree;
115 pt::read_json(ss, ptree);
116 if (ptree.front().first != "result") {
117 msg = "Could not parse server response";
118 res = false;
119 return;
120 }
121 if (!ptree.front().second.get_optional<std::string>("moonraker_version")) {
122 msg = "Could not parse server response";
123 res = false;
124 return;
125 }
126 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Got version: %2%") % name % ptree.front().second.get_optional<std::string>("moonraker_version");
127 } catch (const std::exception&) {
128 res = false;
129 msg = "Could not parse server response";
130 }
131 })
132#ifdef _WIN32
133 .ssl_revoke_best_effort(m_ssl_revoke_best_effort)
134 .on_ip_resolve([&](std::string address) {
135 // Workaround for Windows 10/11 mDNS resolve issue, where two mDNS resolves in succession fail.
136 // Remember resolved address to be reused at successive REST API call.
137 msg = GUI::from_u8(address);
138 })
139#endif // _WIN32
140 .perform_sync();
141
142 return res;
143}
static Http get(std::string url)
Definition Http.cpp:607
virtual void set_auth(Http &http) const
Definition Moonraker.cpp:235
const char * get_name() const override
Definition Moonraker.cpp:73
std::string make_url(const std::string &path) const
Definition Moonraker.cpp:243
virtual wxString format_error(const std::string &body, const std::string &error, unsigned status) const
Definition PrintHost.cpp:66
wxString from_u8(const std::string &str)
Definition GUI.cpp:437

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

Referenced by upload().

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

◆ upload()

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

Implements Slic3r::PrintHost.

146{
147 // POST /server/files/upload
148
149 const char* name = get_name();
150 const auto upload_filename = upload_data.upload_path.filename();
151 const auto upload_parent_path = upload_data.upload_path.parent_path();
152
153 // If test fails, test_msg_or_host_ip contains the error message.
154 wxString test_msg_or_host_ip;
155 if (!test(test_msg_or_host_ip)) {
156 error_fn(std::move(test_msg_or_host_ip));
157 return false;
158 }
159
160 std::string url;
161 bool res = true;
162
163#ifdef WIN32
164 // Workaround for Windows 10/11 mDNS resolve issue, where two mDNS resolves in succession fail.
165 if (m_host.find("https://") == 0 || test_msg_or_host_ip.empty() || !GUI::get_app_config()->get_bool("allow_ip_resolve"))
166#endif // _WIN32
167 {
168 // If https is entered we assume signed ceritificate is being used
169 // IP resolving will not happen - it could resolve into address not being specified in cert
170 url = make_url("server/files/upload");
171 }
172#ifdef WIN32
173 else {
174 // Workaround for Windows 10/11 mDNS resolve issue, where two mDNS resolves in succession fail.
175 // Curl uses easy_getinfo to get ip address of last successful transaction.
176 // If it got the address use it instead of the stored in "host" variable.
177 // This new address returns in "test_msg_or_host_ip" variable.
178 // Solves troubles of uploades failing with name address.
179 // in original address (m_host) replace host for resolved ip
180 info_fn(L"resolve", test_msg_or_host_ip);
181 url = substitute_host(make_url("server/files/upload"), GUI::into_u8(test_msg_or_host_ip));
182 BOOST_LOG_TRIVIAL(info) << "Upload address after ip resolve: " << url;
183 }
184#endif // _WIN32
185
186 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%")
187 % name
188 % upload_data.source_path
189 % url
190 % upload_filename.string()
191 % upload_parent_path.string()
192 % (upload_data.post_action == PrintHostPostUploadAction::StartPrint ? "true" : "false");
193 /*
194 The file must be uploaded in the request's body multipart/form-data (ie: <input type="file">). The following arguments may also be added to the form-data:
195 root: The root location in which to upload the file.Currently this may be gcodes or config.If not specified the default is gcodes.
196 path : This argument may contain a path(relative to the root) indicating a subdirectory to which the file is written.If a path is present the server will attempt to create any subdirectories that do not exist.
197 checksum : A SHA256 hex digest calculated by the client for the uploaded file.If this argument is supplied the server will compare it to its own checksum calculation after the upload has completed.A checksum mismatch will result in a 422 error.
198 Arguments available only for the gcodes root :
199 print: If set to "true", Klippy will attempt to start the print after uploading.Note that this value should be a string type, not boolean.This provides compatibility with OctoPrint's upload API.
200 */
201 auto http = Http::post(std::move(url));
202 set_auth(http);
203
204 http.form_add("root", "gcodes");
205 if (!upload_parent_path.empty())
206 http.form_add("path", upload_parent_path.string());
207 if (upload_data.post_action == PrintHostPostUploadAction::StartPrint)
208 http.form_add("print", "true");
209
210 http.form_add_file("file", upload_data.source_path.string(), upload_filename.string())
211 .on_complete([&](std::string body, unsigned status) {
212 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: File uploaded: HTTP %2%: %3%") % name % status % body;
213 })
214 .on_error([&](std::string body, std::string error, unsigned status) {
215 BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error uploading file: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
216 error_fn(format_error(body, error, status));
217 res = false;
218 })
219 .on_progress([&](Http::Progress progress, bool& cancel) {
220 prorgess_fn(std::move(progress), cancel);
221 if (cancel) {
222 // Upload was canceled
223 BOOST_LOG_TRIVIAL(info) << name << ": Upload canceled";
224 res = false;
225 }
226 })
227#ifdef WIN32
228 .ssl_revoke_best_effort(m_ssl_revoke_best_effort)
229#endif
230 .perform_sync();
231
232 return res;
233}
bool get_bool(const std::string &section, const std::string &key) const
Definition AppConfig.hpp:61
static Http post(std::string url)
Definition Http.cpp:612
virtual bool test(wxString &curl_msg) const override
Definition Moonraker.cpp:87
std::string into_u8(const wxString &str)
Definition GUI.cpp:442
AppConfig * get_app_config()
Definition GUI.cpp:432

References error, Slic3r::PrintHost::format_error(), Slic3r::GUI::get_app_config(), Slic3r::AppConfig::get_bool(), get_name(), Slic3r::GUI::into_u8(), L, m_host, m_ssl_revoke_best_effort, make_url(), Slic3r::Http::post(), Slic3r::PrintHostUpload::post_action, set_auth(), Slic3r::PrintHostUpload::source_path, Slic3r::StartPrint, test(), and Slic3r::PrintHostUpload::upload_path.

+ Here is the call graph for this function:

Member Data Documentation

◆ m_apikey

std::string Slic3r::Moonraker::m_apikey
protected

Referenced by get_apikey(), and set_auth().

◆ m_cafile

std::string Slic3r::Moonraker::m_cafile
protected

Referenced by get_cafile(), and set_auth().

◆ m_host

std::string Slic3r::Moonraker::m_host
protected

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

◆ m_ssl_revoke_best_effort

bool Slic3r::Moonraker::m_ssl_revoke_best_effort
protected

Referenced by test(), and upload().


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