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

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

+ Inheritance diagram for Slic3r::AstroBox:
+ Collaboration diagram for Slic3r::AstroBox:

Public Types

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

Public Member Functions

 AstroBox (DynamicPrintConfig *config)
 
 ~AstroBox () override=default
 
const char * get_name () const override
 
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
 
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

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
 

Private Member Functions

void set_auth (Http &http) const
 
std::string make_url (const std::string &path) const
 

Private Attributes

std::string host
 
std::string apikey
 
std::string cafile
 

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

◆ AstroBox()

Slic3r::AstroBox::AstroBox ( DynamicPrintConfig config)
27 :
28 host(config->opt_string("print_host")),
29 apikey(config->opt_string("printhost_apikey")),
30 cafile(config->opt_string("printhost_cafile"))
31{}
std::string cafile
Definition AstroBox.hpp:38
std::string apikey
Definition AstroBox.hpp:37
std::string host
Definition AstroBox.hpp:36

◆ ~AstroBox()

Slic3r::AstroBox::~AstroBox ( )
overridedefault

Member Function Documentation

◆ can_test()

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

Implements Slic3r::PrintHost.

28{ 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(), test(), Slic3r::FlashAir::test(), Slic3r::Moonraker::test(), Slic3r::OctoPrint::test(), Slic3r::PrusaLink::test(), Slic3r::Repetier::test(), Slic3r::PrusaLink::test_with_method_check(), upload(), Slic3r::Duet::upload(), Slic3r::FlashAir::upload(), Slic3r::MKS::upload(), Slic3r::Moonraker::upload(), Slic3r::Repetier::upload(), and Slic3r::OctoPrint::upload_inner_with_host().

+ Here is the caller graph for this function:

◆ get_groups()

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

Reimplemented in Slic3r::Repetier.

65{ return false; }

◆ get_host()

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

Implements Slic3r::PrintHost.

30{ return host; }

References host.

◆ get_name()

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

Implements Slic3r::PrintHost.

33{ return "AstroBox"; }

Referenced by test(), and upload().

+ Here is the caller graph for this function:

◆ get_post_upload_actions()

PrintHostPostUploadActions Slic3r::AstroBox::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::AstroBox::get_test_failed_msg ( wxString &  msg) const
overridevirtual

Implements Slic3r::PrintHost.

89{
90 return GUI::format_wxstr("%s: %s\n\n%s"
91 , _L("Could not connect to AstroBox")
92 , msg
93 , _L("Note: AstroBox version at least 1.1.0 is required."));
94}
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::AstroBox::get_test_ok_msg ( ) const
overridevirtual

Implements Slic3r::PrintHost.

84{
85 return _(L("Connection to AstroBox works correctly."));
86}
#define _(msgid)
Definition getopt.c:87
#define L(s)
Definition I18N.hpp:18

References _, and L.

◆ has_auto_discovery()

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

Implements Slic3r::PrintHost.

27{ return true; }

◆ make_url()

std::string Slic3r::AstroBox::make_url ( const std::string &  path) const
private
162{
163 if (host.find("http://") == 0 || host.find("https://") == 0) {
164 if (host.back() == '/') {
165 return (boost::format("%1%%2%") % host % path).str();
166 } else {
167 return (boost::format("%1%/%2%") % host % path).str();
168 }
169 } else {
170 return (boost::format("http://%1%/%2%") % host % path).str();
171 }
172}

References host.

Referenced by test(), and upload().

+ Here is the caller graph for this function:

◆ set_auth()

void Slic3r::AstroBox::set_auth ( Http http) const
private
153{
154 http.header("X-Api-Key", apikey);
155
156 if (! cafile.empty()) {
157 http.ca_file(cafile);
158 }
159}

References apikey, Slic3r::Http::ca_file(), cafile, and Slic3r::Http::header().

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

Implements Slic3r::PrintHost.

36{
37 // Since the request is performed synchronously here,
38 // it is ok to refer to `msg` from within the closure
39
40 const char *name = get_name();
41
42 bool res = true;
43 auto url = make_url("api/version");
44
45 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url;
46
47 auto http = Http::get(std::move(url));
48 set_auth(http);
49 http.on_error([&](std::string body, std::string error, unsigned status) {
50 BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
51 res = false;
52 msg = format_error(body, error, status);
53 })
54 .on_complete([&, this](std::string body, unsigned) {
55 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got version: %2%") % name % body;
56
57 try {
58 std::stringstream ss(body);
59 pt::ptree ptree;
60 pt::read_json(ss, ptree);
61
62 if (! ptree.get_optional<std::string>("api")) {
63 res = false;
64 return;
65 }
66
67 const auto text = ptree.get_optional<std::string>("text");
68 res = validate_version_text(text);
69 if (! res) {
70 msg = GUI::format_wxstr(_L("Mismatched type of print host: %s"), (text ? *text : "AstroBox"));
71 }
72 }
73 catch (const std::exception &) {
74 res = false;
75 msg = "Could not parse server response";
76 }
77 })
78 .perform_sync();
79
80 return res;
81}
void set_auth(Http &http) const
Definition AstroBox.cpp:152
std::string make_url(const std::string &path) const
Definition AstroBox.cpp:161
bool validate_version_text(const boost::optional< std::string > &version_text) const
Definition AstroBox.cpp:147
const char * get_name() const override
Definition AstroBox.cpp:33
static Http get(std::string url)
Definition Http.cpp:607
virtual wxString format_error(const std::string &body, const std::string &error, unsigned status) const
Definition PrintHost.cpp:66

References _L, error, Slic3r::PrintHost::format_error(), Slic3r::GUI::format_wxstr(), Slic3r::Http::get(), get_name(), make_url(), set_auth(), and validate_version_text().

Referenced by upload().

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

◆ upload()

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

Implements Slic3r::PrintHost.

97{
98 const char *name = get_name();
99
100 const auto upload_filename = upload_data.upload_path.filename();
101 const auto upload_parent_path = upload_data.upload_path.parent_path();
102
103 wxString test_msg;
104 if (! test(test_msg)) {
105 error_fn(std::move(test_msg));
106 return false;
107 }
108
109 bool res = true;
110
111 auto url = make_url("api/files/local");
112
113 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%")
114 % name
115 % upload_data.source_path
116 % url
117 % upload_filename.string()
118 % upload_parent_path.string()
119 % (upload_data.post_action == PrintHostPostUploadAction::StartPrint ? "true" : "false");
120
121 auto http = Http::post(std::move(url));
122 set_auth(http);
123 http.form_add("print", upload_data.post_action == PrintHostPostUploadAction::StartPrint ? "true" : "false")
124 .form_add("path", upload_parent_path.string()) // XXX: slashes on windows ???
125 .form_add_file("file", upload_data.source_path.string(), upload_filename.string())
126 .on_complete([&](std::string body, unsigned status) {
127 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: File uploaded: HTTP %2%: %3%") % name % status % body;
128 })
129 .on_error([&](std::string body, std::string error, unsigned status) {
130 BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error uploading file: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
131 error_fn(format_error(body, error, status));
132 res = false;
133 })
134 .on_progress([&](Http::Progress progress, bool &cancel) {
135 prorgess_fn(std::move(progress), cancel);
136 if (cancel) {
137 // Upload was canceled
138 BOOST_LOG_TRIVIAL(info) << "AstroBox: Upload canceled";
139 res = false;
140 }
141 })
142 .perform_sync();
143
144 return res;
145}
bool test(wxString &curl_msg) const override
Definition AstroBox.cpp:35
static Http post(std::string url)
Definition Http.cpp:612
TOKEN * string(char *text)
Definition config.c:233
STL namespace.

References error, Slic3r::PrintHost::format_error(), get_name(), 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:

◆ validate_version_text()

bool Slic3r::AstroBox::validate_version_text ( const boost::optional< std::string > &  version_text) const
protected
148{
149 return version_text ? boost::starts_with(*version_text, "AstroBox") : true;
150}

Referenced by test().

+ Here is the caller graph for this function:

Member Data Documentation

◆ apikey

std::string Slic3r::AstroBox::apikey
private

Referenced by set_auth().

◆ cafile

std::string Slic3r::AstroBox::cafile
private

Referenced by set_auth().

◆ host

std::string Slic3r::AstroBox::host
private

Referenced by get_host(), and make_url().


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