2018-02-07 10:37:15 +00:00
|
|
|
#include "OctoPrint.hpp"
|
|
|
|
|
2018-04-04 09:18:22 +00:00
|
|
|
#include <algorithm>
|
2018-02-07 10:37:15 +00:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
|
|
|
#include <wx/frame.h>
|
|
|
|
#include <wx/event.h>
|
2018-04-04 09:18:22 +00:00
|
|
|
#include <wx/progdlg.h>
|
2018-02-07 10:37:15 +00:00
|
|
|
|
|
|
|
#include "libslic3r/PrintConfig.hpp"
|
|
|
|
#include "slic3r/GUI/GUI.hpp"
|
|
|
|
#include "Http.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
|
|
|
|
OctoPrint::OctoPrint(DynamicPrintConfig *config) :
|
2018-02-20 14:37:26 +00:00
|
|
|
host(config->opt_string("octoprint_host")),
|
|
|
|
apikey(config->opt_string("octoprint_apikey")),
|
|
|
|
cafile(config->opt_string("octoprint_cafile"))
|
2018-02-07 10:37:15 +00:00
|
|
|
{}
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
bool OctoPrint::test(wxString &msg) const
|
2018-02-07 10:37:15 +00:00
|
|
|
{
|
2018-02-20 14:37:26 +00:00
|
|
|
// Since the request is performed synchronously here,
|
2018-03-15 17:06:26 +00:00
|
|
|
// it is ok to refer to `msg` from within the closure
|
2018-02-20 14:37:26 +00:00
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
bool res = true;
|
|
|
|
|
|
|
|
auto url = std::move(make_url("api/version"));
|
|
|
|
auto http = Http::get(std::move(url));
|
2018-02-20 14:37:26 +00:00
|
|
|
set_auth(http);
|
|
|
|
http.on_error([&](std::string, std::string error, unsigned status) {
|
2018-03-15 17:06:26 +00:00
|
|
|
res = false;
|
|
|
|
msg = format_error(error, status);
|
2018-02-20 14:37:26 +00:00
|
|
|
})
|
|
|
|
.perform_sync();
|
|
|
|
|
|
|
|
return res;
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 09:18:22 +00:00
|
|
|
bool OctoPrint::send_gcode(const std::string &filename, bool print) const
|
2018-02-07 10:37:15 +00:00
|
|
|
{
|
2018-04-04 09:18:22 +00:00
|
|
|
enum { PROGRESS_RANGE = 1000 };
|
|
|
|
|
|
|
|
const auto errortitle = _(L("Error while uploading to the OctoPrint server"));
|
|
|
|
|
|
|
|
wxProgressDialog progress_dialog(
|
|
|
|
_(L("OctoPrint upload")),
|
|
|
|
_(L("Sending G-code file to the OctoPrint server...")),
|
|
|
|
PROGRESS_RANGE, nullptr, wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
|
|
|
|
progress_dialog.Pulse();
|
|
|
|
|
|
|
|
wxString test_msg;
|
|
|
|
if (!test(test_msg)) {
|
|
|
|
auto errormsg = wxString::Format("%s: %s", errortitle, test_msg);
|
|
|
|
GUI::show_error(&progress_dialog, std::move(errormsg));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool res = true;
|
|
|
|
|
2018-02-20 14:37:26 +00:00
|
|
|
auto http = Http::post(std::move(make_url("api/files/local")));
|
|
|
|
set_auth(http);
|
|
|
|
http.form_add("print", print ? "true" : "false")
|
|
|
|
.form_add_file("file", filename)
|
2018-04-04 09:18:22 +00:00
|
|
|
.on_complete([&](std::string body, unsigned status) {
|
|
|
|
progress_dialog.Update(PROGRESS_RANGE);
|
|
|
|
})
|
|
|
|
.on_error([&](std::string body, std::string error, unsigned status) {
|
|
|
|
auto errormsg = wxString::Format("%s: %s", errortitle, format_error(error, status));
|
|
|
|
GUI::show_error(&progress_dialog, std::move(errormsg));
|
|
|
|
res = false;
|
2018-02-20 14:37:26 +00:00
|
|
|
})
|
2018-04-04 09:18:22 +00:00
|
|
|
.on_progress([&](Http::Progress progress, bool &cancel) {
|
|
|
|
if (cancel) {
|
|
|
|
// Upload was canceled
|
|
|
|
res = false;
|
|
|
|
} else if (progress.ultotal > 0) {
|
|
|
|
int value = PROGRESS_RANGE * progress.ulnow / progress.ultotal;
|
|
|
|
cancel = !progress_dialog.Update(std::min(value, PROGRESS_RANGE - 1)); // Cap the value to prevent premature dialog closing
|
|
|
|
} else {
|
|
|
|
cancel = !progress_dialog.Pulse();
|
|
|
|
}
|
2018-02-20 14:37:26 +00:00
|
|
|
})
|
2018-04-04 09:18:22 +00:00
|
|
|
.perform_sync();
|
|
|
|
|
|
|
|
return res;
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OctoPrint::set_auth(Http &http) const
|
|
|
|
{
|
2018-02-20 14:37:26 +00:00
|
|
|
http.header("X-Api-Key", apikey);
|
2018-02-07 10:37:15 +00:00
|
|
|
|
2018-02-20 14:37:26 +00:00
|
|
|
if (! cafile.empty()) {
|
|
|
|
http.ca_file(cafile);
|
|
|
|
}
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string OctoPrint::make_url(const std::string &path) const
|
|
|
|
{
|
2018-02-20 14:37:26 +00:00
|
|
|
if (host.find("http://") == 0 || host.find("https://") == 0) {
|
|
|
|
if (host.back() == '/') {
|
|
|
|
return std::move((boost::format("%1%%2%") % host % path).str());
|
|
|
|
} else {
|
|
|
|
return std::move((boost::format("%1%/%2%") % host % path).str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return std::move((boost::format("http://%1%/%2%") % host % path).str());
|
|
|
|
}
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
wxString OctoPrint::format_error(std::string error, unsigned status)
|
2018-02-07 10:37:15 +00:00
|
|
|
{
|
2018-03-15 17:06:26 +00:00
|
|
|
const wxString wxerror = error;
|
2018-02-20 14:37:26 +00:00
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
if (status != 0) {
|
|
|
|
return wxString::Format("HTTP %u: %s", status,
|
|
|
|
(status == 401 ? _(L("Invalid API key")) : wxerror));
|
2018-02-20 14:37:26 +00:00
|
|
|
} else {
|
2018-03-15 17:06:26 +00:00
|
|
|
return std::move(wxerror);
|
2018-02-20 14:37:26 +00:00
|
|
|
}
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|