PrusaSlicer-NonPlainar/src/slic3r/Utils/OctoPrint.cpp

215 lines
6.1 KiB
C++
Raw Normal View History

2018-02-07 10:37:15 +00:00
#include "OctoPrint.hpp"
#include <algorithm>
2018-12-19 14:00:30 +00:00
#include <sstream>
2018-02-07 10:37:15 +00:00
#include <boost/format.hpp>
#include <boost/log/trivial.hpp>
2018-12-19 14:00:30 +00:00
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string/predicate.hpp>
2018-02-07 10:37:15 +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"
namespace fs = boost::filesystem;
2018-12-19 14:00:30 +00:00
namespace pt = boost::property_tree;
2018-02-07 10:37:15 +00:00
namespace Slic3r {
OctoPrint::OctoPrint(DynamicPrintConfig *config) :
2018-12-18 13:34:16 +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() {}
const char* OctoPrint::get_name() const { return "OctoPrint"; }
bool OctoPrint::test(wxString &msg) const
2018-02-07 10:37:15 +00:00
{
2018-12-18 13:34:16 +00:00
// Since the request is performed synchronously here,
// it is ok to refer to `msg` from within the closure
const char *name = get_name();
2018-12-18 13:34:16 +00:00
bool res = true;
auto url = make_url("api/version");
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url;
2018-12-18 13:34:16 +00:00
auto http = Http::get(std::move(url));
set_auth(http);
http.on_error([&](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
2018-12-18 13:34:16 +00:00
res = false;
msg = format_error(body, error, status);
})
2018-12-19 14:00:30 +00:00
.on_complete([&, this](std::string body, unsigned) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got version: %2%") % name % body;
2018-12-18 13:34:16 +00:00
2018-12-19 14:00:30 +00:00
try {
std::stringstream ss(body);
pt::ptree ptree;
pt::read_json(ss, ptree);
if (! ptree.get_optional<std::string>("api")) {
res = false;
return;
}
const auto text = ptree.get_optional<std::string>("text");
res = validate_version_text(text);
if (! res) {
msg = wxString::Format(_(L("Mismatched type of print host: %s")), text ? *text : "OctoPrint");
2018-12-19 14:00:30 +00:00
}
}
catch (...) {
res = false;
msg = "Could not parse server response";
}
2018-12-18 13:34:16 +00:00
})
.perform_sync();
return res;
2018-02-07 10:37:15 +00:00
}
wxString OctoPrint::get_test_ok_msg () const
{
return _(L("Connection to OctoPrint works correctly."));
}
wxString OctoPrint::get_test_failed_msg (wxString &msg) const
{
2018-12-18 13:34:16 +00:00
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.")));
}
bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
2018-02-07 10:37:15 +00:00
{
const char *name = get_name();
2018-12-18 13:34:16 +00:00
const auto upload_filename = upload_data.upload_path.filename();
const auto upload_parent_path = upload_data.upload_path.parent_path();
wxString test_msg;
if (! test(test_msg)) {
error_fn(std::move(test_msg));
return false;
2018-12-18 13:34:16 +00:00
}
bool res = true;
auto url = make_url("api/files/local");
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%")
% name
% upload_data.source_path
2018-12-18 13:34:16 +00:00
% url
% upload_filename.string()
% upload_parent_path.string()
% upload_data.start_print;
auto http = Http::post(std::move(url));
set_auth(http);
http.form_add("print", upload_data.start_print ? "true" : "false")
.form_add("path", upload_parent_path.string()) // XXX: slashes on windows ???
.form_add_file("file", upload_data.source_path.string(), upload_filename.string())
.on_complete([&](std::string body, unsigned status) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: File uploaded: HTTP %2%: %3%") % name % status % body;
2018-12-18 13:34:16 +00:00
})
.on_error([&](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error uploading file: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
error_fn(format_error(body, error, status));
2018-12-18 13:34:16 +00:00
res = false;
})
.on_progress([&](Http::Progress progress, bool &cancel) {
prorgess_fn(std::move(progress), cancel);
if (cancel) {
// Upload was canceled
BOOST_LOG_TRIVIAL(info) << "Octoprint: Upload canceled";
2018-12-18 13:34:16 +00:00
res = false;
}
})
.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-12-18 13:34:16 +00:00
return true;
}
bool OctoPrint::can_test() const
{
2018-12-18 13:34:16 +00:00
return true;
}
bool OctoPrint::can_start_print() const
{
return true;
}
2018-12-19 14:00:30 +00:00
bool OctoPrint::validate_version_text(const boost::optional<std::string> &version_text) const
2018-12-18 13:34:16 +00:00
{
2018-12-19 14:00:30 +00:00
return version_text ? boost::starts_with(*version_text, "OctoPrint") : true;
}
2018-02-07 10:37:15 +00:00
void OctoPrint::set_auth(Http &http) const
{
2018-12-18 13:34:16 +00:00
http.header("X-Api-Key", apikey);
2018-02-07 10:37:15 +00:00
2018-12-18 13:34:16 +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-12-18 13:34:16 +00:00
if (host.find("http://") == 0 || host.find("https://") == 0) {
if (host.back() == '/') {
return (boost::format("%1%%2%") % host % path).str();
} else {
return (boost::format("%1%/%2%") % host % path).str();
}
} else {
return (boost::format("http://%1%/%2%") % host % path).str();
}
2018-02-07 10:37:15 +00:00
}
2018-12-18 13:34:16 +00:00
// SL1Host
SL1Host::~SL1Host() {}
2018-12-18 13:34:16 +00:00
const char* SL1Host::get_name() const { return "SL1Host"; }
2018-12-18 13:34:16 +00:00
wxString SL1Host::get_test_ok_msg () const
2018-12-18 13:34:16 +00:00
{
return _(L("Connection to Prusa SL1 works correctly."));
2018-12-19 14:00:30 +00:00
}
wxString SL1Host::get_test_failed_msg (wxString &msg) const
2018-12-19 14:00:30 +00:00
{
return wxString::Format("%s: %s", _(L("Could not connect to Prusa SLA")), msg);
}
bool SL1Host::can_start_print() const
{
return false;
}
bool SL1Host::validate_version_text(const boost::optional<std::string> &version_text) const
2018-12-19 14:00:30 +00:00
{
return version_text ? boost::starts_with(*version_text, "Prusa SLA") : false;
2018-02-07 10:37:15 +00:00
}
}