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>
|
2018-05-23 10:47:39 +00:00
|
|
|
#include <boost/log/trivial.hpp>
|
2018-02-07 10:37:15 +00:00
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
#include <wx/progdlg.h>
|
|
|
|
|
2018-02-07 10:37:15 +00:00
|
|
|
#include "libslic3r/PrintConfig.hpp"
|
2018-12-11 09:33:11 +00:00
|
|
|
#include "slic3r/GUI/I18N.hpp"
|
2018-02-07 10:37:15 +00:00
|
|
|
#include "Http.hpp"
|
|
|
|
|
2018-11-26 13:41:58 +00:00
|
|
|
|
2018-05-23 10:47:39 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2018-02-07 10:37:15 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
OctoPrint::OctoPrint(DynamicPrintConfig *config) :
|
2018-07-08 12:32:48 +00:00
|
|
|
host(config->opt_string("print_host")),
|
|
|
|
apikey(config->opt_string("printhost_apikey")),
|
|
|
|
cafile(config->opt_string("printhost_cafile"))
|
2018-02-07 10:37:15 +00:00
|
|
|
{}
|
|
|
|
|
2018-08-21 09:10:32 +00:00
|
|
|
OctoPrint::~OctoPrint() {}
|
|
|
|
|
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;
|
2018-06-12 10:32:03 +00:00
|
|
|
auto url = make_url("api/version");
|
2018-05-23 10:47:39 +00:00
|
|
|
|
|
|
|
BOOST_LOG_TRIVIAL(info) << boost::format("Octoprint: Get version at: %1%") % url;
|
|
|
|
|
2018-03-15 17:06:26 +00:00
|
|
|
auto http = Http::get(std::move(url));
|
2018-02-20 14:37:26 +00:00
|
|
|
set_auth(http);
|
2018-06-27 15:00:20 +00:00
|
|
|
http.on_error([&](std::string body, std::string error, unsigned status) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << boost::format("Octoprint: Error getting version: %1%, HTTP %2%, body: `%3%`") % error % status % body;
|
2018-03-15 17:06:26 +00:00
|
|
|
res = false;
|
2018-06-27 15:00:20 +00:00
|
|
|
msg = format_error(body, error, status);
|
2018-02-20 14:37:26 +00:00
|
|
|
})
|
2018-05-23 10:47:39 +00:00
|
|
|
.on_complete([&](std::string body, unsigned) {
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << boost::format("Octoprint: Got version: %1%") % body;
|
|
|
|
})
|
2018-02-20 14:37:26 +00:00
|
|
|
.perform_sync();
|
|
|
|
|
|
|
|
return res;
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:32:48 +00:00
|
|
|
wxString OctoPrint::get_test_ok_msg () const
|
|
|
|
{
|
|
|
|
return wxString::Format("%s", _(L("Connection to OctoPrint works correctly.")));
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString OctoPrint::get_test_failed_msg (wxString &msg) const
|
|
|
|
{
|
|
|
|
return wxString::Format("%s: %s\n\n%s",
|
|
|
|
_(L("Could not connect to OctoPrint")), msg, _(L("Note: OctoPrint version at least 1.1.0 is required.")));
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
bool OctoPrint::upload(PrintHostUpload upload_data, Http::ProgressFn prorgess_fn, Http::ErrorFn error_fn) const
|
2018-02-07 10:37:15 +00:00
|
|
|
{
|
2018-12-14 14:27:34 +00:00
|
|
|
const auto upload_filename = upload_data.upload_path.filename();
|
|
|
|
const auto upload_parent_path = upload_data.upload_path.parent_path();
|
2018-05-23 10:47:39 +00:00
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
wxString test_msg;
|
|
|
|
if (! test(test_msg)) {
|
2018-04-04 09:18:22 +00:00
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
// TODO:
|
2018-04-04 09:18:22 +00:00
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
// auto errormsg = wxString::Format("%s: %s", errortitle, test_msg);
|
|
|
|
// GUI::show_error(&progress_dialog, std::move(errormsg));
|
|
|
|
// return false;
|
2018-04-04 09:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool res = true;
|
|
|
|
|
2018-05-23 10:47:39 +00:00
|
|
|
auto url = make_url("api/files/local");
|
|
|
|
|
|
|
|
BOOST_LOG_TRIVIAL(info) << boost::format("Octoprint: Uploading file %1% at %2%, filename: %3%, path: %4%, print: %5%")
|
2018-12-14 14:27:34 +00:00
|
|
|
% upload_data.source_path.string()
|
2018-05-23 10:47:39 +00:00
|
|
|
% url
|
2018-06-12 10:32:03 +00:00
|
|
|
% upload_filename.string()
|
|
|
|
% upload_parent_path.string()
|
2018-12-14 14:27:34 +00:00
|
|
|
% upload_data.start_print;
|
2018-05-23 10:47:39 +00:00
|
|
|
|
|
|
|
auto http = Http::post(std::move(url));
|
2018-02-20 14:37:26 +00:00
|
|
|
set_auth(http);
|
2018-12-14 14:27:34 +00:00
|
|
|
http.form_add("print", upload_data.start_print ? "true" : "false")
|
2018-12-11 09:33:11 +00:00
|
|
|
.form_add("path", upload_parent_path.string()) // XXX: slashes on windows ???
|
2018-12-14 14:27:34 +00:00
|
|
|
.form_add_file("file", upload_data.source_path.string(), upload_filename.string())
|
2018-04-04 09:18:22 +00:00
|
|
|
.on_complete([&](std::string body, unsigned status) {
|
2018-05-23 10:47:39 +00:00
|
|
|
BOOST_LOG_TRIVIAL(debug) << boost::format("Octoprint: File uploaded: HTTP %1%: %2%") % status % body;
|
2018-04-04 09:18:22 +00:00
|
|
|
})
|
|
|
|
.on_error([&](std::string body, std::string error, unsigned status) {
|
2018-06-27 15:00:20 +00:00
|
|
|
BOOST_LOG_TRIVIAL(error) << boost::format("Octoprint: Error uploading file: %1%, HTTP %2%, body: `%3%`") % error % status % body;
|
2018-12-14 14:27:34 +00:00
|
|
|
error_fn(std::move(body), std::move(error), status);
|
2018-04-04 09:18:22 +00:00
|
|
|
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) {
|
2018-12-14 14:27:34 +00:00
|
|
|
prorgess_fn(std::move(progress), cancel);
|
2018-04-04 09:18:22 +00:00
|
|
|
if (cancel) {
|
|
|
|
// Upload was canceled
|
2018-12-14 14:27:34 +00:00
|
|
|
BOOST_LOG_TRIVIAL(error) << "Octoprint: Upload canceled";
|
2018-04-04 09:18:22 +00:00
|
|
|
res = false;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-08-21 09:10:32 +00:00
|
|
|
bool OctoPrint::has_auto_discovery() const
|
2018-07-08 12:32:48 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OctoPrint::can_test() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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() == '/') {
|
2018-06-12 10:32:03 +00:00
|
|
|
return (boost::format("%1%%2%") % host % path).str();
|
2018-02-20 14:37:26 +00:00
|
|
|
} else {
|
2018-06-12 10:32:03 +00:00
|
|
|
return (boost::format("%1%/%2%") % host % path).str();
|
2018-02-20 14:37:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-12 10:32:03 +00:00
|
|
|
return (boost::format("http://%1%/%2%") % host % path).str();
|
2018-02-20 14:37:26 +00:00
|
|
|
}
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 15:00:20 +00:00
|
|
|
wxString OctoPrint::format_error(const std::string &body, const std::string &error, unsigned status)
|
2018-02-07 10:37:15 +00:00
|
|
|
{
|
2018-03-15 17:06:26 +00:00
|
|
|
if (status != 0) {
|
2018-06-27 15:00:20 +00:00
|
|
|
auto wxbody = wxString::FromUTF8(body.data());
|
|
|
|
return wxString::Format("HTTP %u: %s", status, wxbody);
|
2018-02-20 14:37:26 +00:00
|
|
|
} else {
|
2018-06-27 15:00:20 +00:00
|
|
|
return wxString::FromUTF8(error.data());
|
2018-02-20 14:37:26 +00:00
|
|
|
}
|
2018-02-07 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|