2018-12-14 14:27:34 +00:00
|
|
|
#include "PrintHost.hpp"
|
2018-07-08 12:32:48 +00:00
|
|
|
|
2018-12-11 09:33:11 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <thread>
|
2018-12-19 17:43:03 +00:00
|
|
|
#include <exception>
|
2018-12-11 09:33:11 +00:00
|
|
|
#include <boost/optional.hpp>
|
2018-12-18 17:40:35 +00:00
|
|
|
#include <boost/log/trivial.hpp>
|
2018-12-14 14:27:34 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2018-12-19 17:43:03 +00:00
|
|
|
#include <wx/string.h>
|
2018-12-14 14:27:34 +00:00
|
|
|
#include <wx/app.h>
|
2020-10-28 08:51:05 +00:00
|
|
|
#include <wx/arrstr.h>
|
2018-12-11 09:33:11 +00:00
|
|
|
|
2018-07-08 12:32:48 +00:00
|
|
|
#include "libslic3r/PrintConfig.hpp"
|
2018-12-11 09:33:11 +00:00
|
|
|
#include "libslic3r/Channel.hpp"
|
2018-12-14 14:27:34 +00:00
|
|
|
#include "OctoPrint.hpp"
|
|
|
|
#include "Duet.hpp"
|
2019-05-07 17:51:14 +00:00
|
|
|
#include "FlashAir.hpp"
|
2019-12-28 17:41:48 +00:00
|
|
|
#include "AstroBox.hpp"
|
2020-10-28 08:51:05 +00:00
|
|
|
#include "Repetier.hpp"
|
2018-12-14 14:27:34 +00:00
|
|
|
#include "../GUI/PrintHostDialogs.hpp"
|
2018-12-11 09:33:11 +00:00
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
namespace fs = boost::filesystem;
|
2018-12-11 09:33:11 +00:00
|
|
|
using boost::optional;
|
2018-12-14 14:27:34 +00:00
|
|
|
using Slic3r::GUI::PrintHostQueueDialog;
|
2018-07-08 12:32:48 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
|
2018-08-21 09:10:32 +00:00
|
|
|
PrintHost::~PrintHost() {}
|
|
|
|
|
2019-03-05 14:15:41 +00:00
|
|
|
PrintHost* PrintHost::get_print_host(DynamicPrintConfig *config)
|
2018-07-08 12:32:48 +00:00
|
|
|
{
|
2019-03-05 14:15:41 +00:00
|
|
|
PrinterTechnology tech = ptFFF;
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto opt = config->option<ConfigOptionEnum<PrinterTechnology>>("printer_technology");
|
|
|
|
if (opt != nullptr) {
|
|
|
|
tech = opt->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tech == ptFFF) {
|
|
|
|
const auto opt = config->option<ConfigOptionEnum<PrintHostType>>("host_type");
|
|
|
|
const auto host_type = opt != nullptr ? opt->value : htOctoPrint;
|
|
|
|
|
|
|
|
switch (host_type) {
|
|
|
|
case htOctoPrint: return new OctoPrint(config);
|
|
|
|
case htDuet: return new Duet(config);
|
2019-05-07 17:51:14 +00:00
|
|
|
case htFlashAir: return new FlashAir(config);
|
2019-12-28 17:41:48 +00:00
|
|
|
case htAstroBox: return new AstroBox(config);
|
2020-10-28 08:51:05 +00:00
|
|
|
case htRepetier: return new Repetier(config);
|
2019-03-05 14:15:41 +00:00
|
|
|
default: return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return new SL1Host(config);
|
2018-12-11 09:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 15:00:51 +00:00
|
|
|
wxString PrintHost::format_error(const std::string &body, const std::string &error, unsigned status) const
|
|
|
|
{
|
|
|
|
if (status != 0) {
|
|
|
|
auto wxbody = wxString::FromUTF8(body.data());
|
|
|
|
return wxString::Format("HTTP %u: %s", status, wxbody);
|
|
|
|
} else {
|
|
|
|
return wxString::FromUTF8(error.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 09:33:11 +00:00
|
|
|
|
|
|
|
struct PrintHostJobQueue::priv
|
|
|
|
{
|
2018-12-14 14:27:34 +00:00
|
|
|
// XXX: comment on how bg thread works
|
|
|
|
|
|
|
|
PrintHostJobQueue *q;
|
|
|
|
|
|
|
|
Channel<PrintHostJob> channel_jobs;
|
|
|
|
Channel<size_t> channel_cancels;
|
|
|
|
size_t job_id = 0;
|
|
|
|
int prev_progress = -1;
|
2018-12-20 12:36:49 +00:00
|
|
|
fs::path source_to_remove;
|
2018-12-11 09:33:11 +00:00
|
|
|
|
|
|
|
std::thread bg_thread;
|
2018-12-14 14:27:34 +00:00
|
|
|
bool bg_exit = false;
|
|
|
|
|
|
|
|
PrintHostQueueDialog *queue_dialog;
|
|
|
|
|
|
|
|
priv(PrintHostJobQueue *q) : q(q) {}
|
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
void emit_progress(int progress);
|
|
|
|
void emit_error(wxString error);
|
|
|
|
void emit_cancel(size_t id);
|
2018-12-14 14:27:34 +00:00
|
|
|
void start_bg_thread();
|
2018-12-21 13:36:05 +00:00
|
|
|
void stop_bg_thread();
|
2018-12-14 14:27:34 +00:00
|
|
|
void bg_thread_main();
|
|
|
|
void progress_fn(Http::Progress progress, bool &cancel);
|
2018-12-20 12:36:49 +00:00
|
|
|
void remove_source(const fs::path &path);
|
|
|
|
void remove_source();
|
2018-12-14 14:27:34 +00:00
|
|
|
void perform_job(PrintHostJob the_job);
|
2018-12-11 09:33:11 +00:00
|
|
|
};
|
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
PrintHostJobQueue::PrintHostJobQueue(PrintHostQueueDialog *queue_dialog)
|
|
|
|
: p(new priv(this))
|
2018-12-11 09:33:11 +00:00
|
|
|
{
|
2018-12-14 14:27:34 +00:00
|
|
|
p->queue_dialog = queue_dialog;
|
2018-12-11 09:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PrintHostJobQueue::~PrintHostJobQueue()
|
|
|
|
{
|
2018-12-21 13:36:05 +00:00
|
|
|
if (p) { p->stop_bg_thread(); }
|
2018-12-14 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
void PrintHostJobQueue::priv::emit_progress(int progress)
|
|
|
|
{
|
|
|
|
auto evt = new PrintHostQueueDialog::Event(GUI::EVT_PRINTHOST_PROGRESS, queue_dialog->GetId(), job_id, progress);
|
|
|
|
wxQueueEvent(queue_dialog, evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintHostJobQueue::priv::emit_error(wxString error)
|
|
|
|
{
|
|
|
|
auto evt = new PrintHostQueueDialog::Event(GUI::EVT_PRINTHOST_ERROR, queue_dialog->GetId(), job_id, std::move(error));
|
|
|
|
wxQueueEvent(queue_dialog, evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintHostJobQueue::priv::emit_cancel(size_t id)
|
|
|
|
{
|
|
|
|
auto evt = new PrintHostQueueDialog::Event(GUI::EVT_PRINTHOST_CANCEL, queue_dialog->GetId(), id);
|
|
|
|
wxQueueEvent(queue_dialog, evt);
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
void PrintHostJobQueue::priv::start_bg_thread()
|
|
|
|
{
|
|
|
|
if (bg_thread.joinable()) { return; }
|
|
|
|
|
|
|
|
std::shared_ptr<priv> p2 = q->p;
|
|
|
|
bg_thread = std::thread([p2]() {
|
|
|
|
p2->bg_thread_main();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-21 13:36:05 +00:00
|
|
|
void PrintHostJobQueue::priv::stop_bg_thread()
|
|
|
|
{
|
|
|
|
if (bg_thread.joinable()) {
|
|
|
|
bg_exit = true;
|
|
|
|
channel_jobs.push(PrintHostJob()); // Push an empty job to wake up bg_thread in case it's sleeping
|
|
|
|
bg_thread.detach(); // Let the background thread go, it should exit on its own
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
void PrintHostJobQueue::priv::bg_thread_main()
|
|
|
|
{
|
|
|
|
// bg thread entry point
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Pick up jobs from the job channel:
|
|
|
|
while (! bg_exit) {
|
|
|
|
auto job = channel_jobs.pop(); // Sleeps in a cond var if there are no jobs
|
2018-12-21 13:36:05 +00:00
|
|
|
if (job.empty()) {
|
|
|
|
// This happens when the thread is being stopped
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
source_to_remove = job.upload_data.source_path;
|
|
|
|
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << boost::format("PrintHostJobQueue/bg_thread: Received job: [%1%]: `%2%` -> `%3%`, cancelled: %4%")
|
|
|
|
% job_id
|
|
|
|
% job.upload_data.upload_path
|
|
|
|
% job.printhost->get_host()
|
|
|
|
% job.cancelled;
|
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
if (! job.cancelled) {
|
|
|
|
perform_job(std::move(job));
|
|
|
|
}
|
2018-12-20 12:36:49 +00:00
|
|
|
|
|
|
|
remove_source();
|
2018-12-14 14:27:34 +00:00
|
|
|
job_id++;
|
|
|
|
}
|
2018-12-19 17:43:03 +00:00
|
|
|
} catch (const std::exception &e) {
|
2018-12-20 12:36:49 +00:00
|
|
|
emit_error(e.what());
|
2018-12-14 14:27:34 +00:00
|
|
|
}
|
2018-12-20 12:36:49 +00:00
|
|
|
|
|
|
|
// Cleanup leftover files, if any
|
|
|
|
remove_source();
|
|
|
|
auto jobs = channel_jobs.lock_rw();
|
|
|
|
for (const PrintHostJob &job : *jobs) {
|
|
|
|
remove_source(job.upload_data.source_path);
|
|
|
|
}
|
2018-12-14 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintHostJobQueue::priv::progress_fn(Http::Progress progress, bool &cancel)
|
|
|
|
{
|
2018-12-20 12:36:49 +00:00
|
|
|
if (cancel) {
|
|
|
|
// When cancel is true from the start, Http indicates request has been cancelled
|
|
|
|
emit_cancel(job_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-14 14:27:34 +00:00
|
|
|
if (bg_exit) {
|
|
|
|
cancel = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel_cancels.size_hint() > 0) {
|
|
|
|
// Lock both queues
|
|
|
|
auto cancels = channel_cancels.lock_rw();
|
|
|
|
auto jobs = channel_jobs.lock_rw();
|
|
|
|
|
|
|
|
for (size_t cancel_id : *cancels) {
|
|
|
|
if (cancel_id == job_id) {
|
|
|
|
cancel = true;
|
|
|
|
} else if (cancel_id > job_id) {
|
2018-12-20 12:36:49 +00:00
|
|
|
const size_t idx = cancel_id - job_id - 1;
|
|
|
|
if (idx < jobs->size()) {
|
|
|
|
jobs->at(idx).cancelled = true;
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << boost::format("PrintHostJobQueue: Job id %1% cancelled") % cancel_id;
|
|
|
|
emit_cancel(cancel_id);
|
|
|
|
}
|
2018-12-14 14:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancels->clear();
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
if (! cancel) {
|
|
|
|
int gui_progress = progress.ultotal > 0 ? 100*progress.ulnow / progress.ultotal : 0;
|
|
|
|
if (gui_progress != prev_progress) {
|
|
|
|
emit_progress(gui_progress);
|
|
|
|
prev_progress = gui_progress;
|
|
|
|
}
|
2018-12-14 14:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
void PrintHostJobQueue::priv::remove_source(const fs::path &path)
|
2018-12-14 14:27:34 +00:00
|
|
|
{
|
2018-12-20 12:36:49 +00:00
|
|
|
if (! path.empty()) {
|
|
|
|
boost::system::error_code ec;
|
|
|
|
fs::remove(path, ec);
|
|
|
|
if (ec) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << boost::format("PrintHostJobQueue: Error removing file `%1%`: %2%") % path % ec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 14:27:34 +00:00
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
void PrintHostJobQueue::priv::remove_source()
|
|
|
|
{
|
|
|
|
remove_source(source_to_remove);
|
|
|
|
source_to_remove.clear();
|
|
|
|
}
|
2018-12-18 17:40:35 +00:00
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
void PrintHostJobQueue::priv::perform_job(PrintHostJob the_job)
|
|
|
|
{
|
2018-12-20 15:42:46 +00:00
|
|
|
emit_progress(0); // Indicate the upload is starting
|
|
|
|
|
2018-12-19 17:43:03 +00:00
|
|
|
bool success = the_job.printhost->upload(std::move(the_job.upload_data),
|
2018-12-14 14:27:34 +00:00
|
|
|
[this](Http::Progress progress, bool &cancel) { this->progress_fn(std::move(progress), cancel); },
|
2018-12-19 17:43:03 +00:00
|
|
|
[this](wxString error) {
|
2018-12-20 12:36:49 +00:00
|
|
|
emit_error(std::move(error));
|
2018-12-19 17:43:03 +00:00
|
|
|
}
|
2018-12-14 14:27:34 +00:00
|
|
|
);
|
|
|
|
|
2018-12-19 17:43:03 +00:00
|
|
|
if (success) {
|
2018-12-20 12:36:49 +00:00
|
|
|
emit_progress(100);
|
2018-12-18 17:40:35 +00:00
|
|
|
}
|
2018-12-14 14:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintHostJobQueue::enqueue(PrintHostJob job)
|
|
|
|
{
|
|
|
|
p->start_bg_thread();
|
|
|
|
p->queue_dialog->append_job(job);
|
|
|
|
p->channel_jobs.push(std::move(job));
|
2018-07-08 12:32:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:36:49 +00:00
|
|
|
void PrintHostJobQueue::cancel(size_t id)
|
|
|
|
{
|
|
|
|
p->channel_cancels.push(id);
|
|
|
|
}
|
2018-08-21 09:10:32 +00:00
|
|
|
|
2018-07-08 12:32:48 +00:00
|
|
|
}
|