From f1ae74f9d3969a8c0235c5b7943a4e2a7660a468 Mon Sep 17 00:00:00 2001 From: Sergey Kovalev Date: Tue, 29 Dec 2020 01:01:39 +0700 Subject: [PATCH] Small adaptation and formatting --- src/slic3r/Utils/MKS.cpp | 170 ++++++++-------- src/slic3r/Utils/MKS.hpp | 48 ++--- src/slic3r/Utils/TCPConsole.cpp | 330 ++++++++++++++++---------------- src/slic3r/Utils/TCPConsole.hpp | 106 +++++----- 4 files changed, 331 insertions(+), 323 deletions(-) diff --git a/src/slic3r/Utils/MKS.cpp b/src/slic3r/Utils/MKS.cpp index c25ae9d0d..1a60c5a3e 100644 --- a/src/slic3r/Utils/MKS.cpp +++ b/src/slic3r/Utils/MKS.cpp @@ -31,51 +31,51 @@ namespace pt = boost::property_tree; namespace Slic3r { -MKS::MKS(DynamicPrintConfig *config) : - host(config->opt_string("print_host")), console(config->opt_string("print_host"), "8080") -{} + MKS::MKS(DynamicPrintConfig* config) : + host(config->opt_string("print_host")), console(config->opt_string("print_host"), "8080") + {} -const char* MKS::get_name() const { return "MKS"; } + const char* MKS::get_name() const { return "MKS"; } -bool MKS::test(wxString &msg) const -{ - console.enqueue_cmd("M105"); - bool ret = console.run_queue(); + bool MKS::test(wxString& msg) const + { + console.enqueue_cmd("M105"); + bool ret = console.run_queue(); - if (!ret) { - msg = console.error_message(); - } + if (!ret) { + msg = console.error_message(); + } - return ret; -} + return ret; + } -wxString MKS::get_test_ok_msg () const -{ - return _(L("Connection to MKS works correctly.")); -} + wxString MKS::get_test_ok_msg() const + { + return _(L("Connection to MKS works correctly.")); + } -wxString MKS::get_test_failed_msg (wxString &msg) const -{ - return GUI::from_u8((boost::format("%s: %s") - % _utf8(L("Could not connect to MKS")) - % std::string(msg.ToUTF8())).str()); -} + wxString MKS::get_test_failed_msg(wxString& msg) const + { + return GUI::from_u8((boost::format("%s: %s") + % _utf8(L("Could not connect to MKS")) + % std::string(msg.ToUTF8())).str()); + } -bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const -{ - bool res = true; + bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const + { + bool res = true; - auto upload_cmd = get_upload_url(upload_data.upload_path.string()); - BOOST_LOG_TRIVIAL(info) << boost::format("MKS: Uploading file %1%, filepath: %2%, print: %3%, command: %4%") - % upload_data.source_path - % upload_data.upload_path - % upload_data.start_print - % upload_cmd; + auto upload_cmd = get_upload_url(upload_data.upload_path.string()); + BOOST_LOG_TRIVIAL(info) << boost::format("MKS: Uploading file %1%, filepath: %2%, print: %3%, command: %4%") + % upload_data.source_path + % upload_data.upload_path + % upload_data.start_print + % upload_cmd; - auto http = Http::post(std::move(upload_cmd)); - http.set_post_body(upload_data.source_path); + auto http = Http::post(std::move(upload_cmd)); + http.set_post_body(upload_data.source_path); - http.on_complete([&](std::string body, unsigned status) { + http.on_complete([&](std::string body, unsigned status) { BOOST_LOG_TRIVIAL(debug) << boost::format("MKS: File uploaded: HTTP %1%: %2%") % status % body; int err_code = get_err_code_from_body(body); @@ -83,69 +83,73 @@ bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn er BOOST_LOG_TRIVIAL(error) << boost::format("MKS: Request completed but error code was received: %1%") % err_code; error_fn(format_error(body, L("Unknown error occured"), 0)); res = false; - } else if (upload_data.start_print) { + } + else if (upload_data.start_print) { wxString errormsg; res = start_print(errormsg, upload_data.upload_path.string()); - if (! res) { + if (!res) { error_fn(std::move(errormsg)); } } - }) - .on_error([&](std::string body, std::string error, unsigned status) { - BOOST_LOG_TRIVIAL(error) << boost::format("MKS: Error uploading file: %1%, HTTP %2%, body: `%3%`") % error % status % body; - error_fn(format_error(body, error, status)); - res = false; - }) - .on_progress([&](Http::Progress progress, bool &cancel) { - prorgess_fn(std::move(progress), cancel); - if (cancel) { - // Upload was canceled - BOOST_LOG_TRIVIAL(info) << "MKS: Upload canceled"; + }) + .on_error([&](std::string body, std::string error, unsigned status) { + BOOST_LOG_TRIVIAL(error) << boost::format("MKS: Error uploading file: %1%, HTTP %2%, body: `%3%`") % error % status % body; + error_fn(format_error(body, error, status)); res = false; - } - }) - .perform_sync(); + }) + .on_progress([&](Http::Progress progress, bool& cancel) { + prorgess_fn(std::move(progress), cancel); + if (cancel) { + // Upload was canceled + BOOST_LOG_TRIVIAL(info) << "MKS: Upload canceled"; + res = false; + } + }) + .perform_sync(); - if (res && upload_data.start_print) { - start_print(upload_data.upload_path); + if (res && upload_data.start_print) { + wxString msg; + if (!start_print(msg, upload_data.upload_path.string())) { + error_fn(wxString("Can't start printing: ") + msg); + } + } + + return res; } - return res; -} + std::string MKS::get_upload_url(const std::string& filename) const + { + return (boost::format("http://%1%/upload?X-Filename=%2%") + % host + % Http::url_encode(filename)).str(); + } -std::string MKS::get_upload_url(const std::string &filename) const -{ - return (boost::format("http://%1%/upload?X-Filename=%2%") - % host - % Http::url_encode(filename)).str(); -} + bool MKS::start_print(wxString& msg, const std::string& filename) const + { + // For some reason printer firmware does not want to respond on gcode commands immediately after file upload. + // So we just introduce artificial delay to workaround it. + // TODO: Inspect reasons + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); -bool MKS::start_print(wxString &msg, const std::string &filename) const -{ - // For some reason printer firmware does not want to respond on gcode commands immediately after file upload. - // So we just introduce artificial delay to workaround it. - // TODO: Inspect reasons - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + console.enqueue_cmd(std::string("M23 ") + filename); + console.enqueue_cmd("M24"); - console.enqueue_cmd("M23 " + upload_data.upload_path.string()); - console.enqueue_cmd("M24"); + bool ret = console.run_queue(); - bool ret = console.run_queue(); + if (!ret) { + msg = console.error_message(); + } - if (!ret) { - msg = console.error_message(); - } + return ret; + } - return ret; -} + int MKS::get_err_code_from_body(const std::string& body) const + { + pt::ptree root; + std::istringstream iss(body); // wrap returned json to istringstream + pt::read_json(iss, root); -int MKS::get_err_code_from_body(const std::string& body) const -{ - pt::ptree root; - std::istringstream iss(body); // wrap returned json to istringstream - pt::read_json(iss, root); - - return root.get("err", 0); -} + return root.get("err", 0); + } } // Slic3r diff --git a/src/slic3r/Utils/MKS.hpp b/src/slic3r/Utils/MKS.hpp index 0414e3335..67c655293 100644 --- a/src/slic3r/Utils/MKS.hpp +++ b/src/slic3r/Utils/MKS.hpp @@ -9,35 +9,35 @@ namespace Slic3r { -class DynamicPrintConfig; -class Http; + class DynamicPrintConfig; + class Http; -class MKS : public PrintHost -{ -public: - explicit MKS(DynamicPrintConfig *config); - ~MKS() override = default; + class MKS : public PrintHost + { + public: + explicit MKS(DynamicPrintConfig* config); + ~MKS() override = default; - const char* get_name() const override; + 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) const override; - bool has_auto_discovery() const override { return false; } - bool can_test() const override { return true; } - bool can_start_print() const override { return true; } - std::string get_host() const override { return host; } + 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) const override; + bool has_auto_discovery() const override { return false; } + bool can_test() const override { return true; } + bool can_start_print() const override { return true; } + std::string get_host() const override { return host; } -private: - std::string host; - Utils::TCPConsole console; + private: + std::string host; + Utils::TCPConsole console; - std::string get_upload_url(const std::string &filename) const; - std::string timestamp_str() const; - bool start_print(wxString &msg, const std::string &filename) const; - int get_err_code_from_body(const std::string &body) const; -}; + std::string get_upload_url(const std::string& filename) const; + std::string timestamp_str() const; + bool start_print(wxString& msg, const std::string& filename) const; + int get_err_code_from_body(const std::string& body) const; + }; } diff --git a/src/slic3r/Utils/TCPConsole.cpp b/src/slic3r/Utils/TCPConsole.cpp index 4e1ca1f7c..bea991945 100644 --- a/src/slic3r/Utils/TCPConsole.cpp +++ b/src/slic3r/Utils/TCPConsole.cpp @@ -18,174 +18,178 @@ using boost::asio::steady_timer; using boost::asio::ip::tcp; namespace Slic3r { -namespace Utils { + namespace Utils { -void TCPConsole::transmit_next_command() -{ - if (cmd_queue_.empty()) { - io_context_.stop(); - return; - } + void TCPConsole::transmit_next_command() + { + if (cmd_queue_.empty()) { + io_context_.stop(); + return; + } - std::string cmd = cmd_queue_.front(); - cmd_queue_.pop_front(); + std::string cmd = cmd_queue_.front(); + cmd_queue_.pop_front(); - BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: transmitting '%3%' to %1%:%2%") - % host_name_ - % port_name_ - % cmd; + BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: transmitting '%3%' to %1%:%2%") + % host_name_ + % port_name_ + % cmd; - auto data = boost::asio::buffer(cmd + newline_); + auto data = boost::asio::buffer(cmd + newline_); - boost::asio::async_write( - socket_, - data, - boost::bind(&TCPConsole::handle_write, this, _1, _2) - ); -} - -void TCPConsole::wait_next_line() -{ - boost::asio::async_read_until( - socket_, - recv_buffer_, - newline_, - boost::bind(&TCPConsole::handle_read, this, _1, _2) - ); -} - -// TODO: Use std::optional here -std::string TCPConsole::extract_next_line() -{ - char linebuf[1024]; - - std::istream is(&recv_buffer_); - is.getline(linebuf, sizeof(linebuf)); - if (is.good()) { - return linebuf; - } - - return ""; -} - -void TCPConsole::handle_read( - const boost::system::error_code& ec, - std::size_t bytes_transferred) -{ - error_code_ = ec; - - if (ec) { - BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't read from %1%:%2%: %3%") - % host_name_ - % port_name_ - % ec.message(); - - io_context_.stop(); - } else { - std::string line = extract_next_line(); - boost::trim(line); - - BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: received '%3%' from %1%:%2%") - % host_name_ - % port_name_ - % line; - - boost::to_lower(line); - - if (line == done_string_) { - transmit_next_command(); - } else { - wait_next_line(); - } - } -} - -void TCPConsole::handle_write( - const boost::system::error_code& ec, - std::size_t) -{ - error_code_ = ec; - if (ec) { - BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't write to %1%:%2%: %3%") - % host_name_ - % port_name_ - % ec.message(); - - io_context_.stop(); - } else { - wait_next_line(); - } -} - -void TCPConsole::handle_connect(const boost::system::error_code& ec) -{ - error_code_ = ec; - - if (ec) { - BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't connect to %1%:%2%: %3%") - % host_name_ - % port_name_ - % ec.message(); - - io_context_.stop(); - } else { - BOOST_LOG_TRIVIAL(info) << boost::format("TCPConsole: connected to %1%:%2%") - % host_name_ - % port_name_; - - - transmit_next_command(); - } -} - -bool TCPConsole::run_queue() -{ - try { - // TODO: Add more resets and initializations after previous run - - auto endpoints = resolver_.resolve(host_name_, port_name_); - - socket_.async_connect(endpoints->endpoint(), - boost::bind(&TCPConsole::handle_connect, this, _1) - ); - - // TODO: Add error and timeout processing - io_context_.restart(); - while (!io_context_.stopped()) { - BOOST_LOG_TRIVIAL(debug) << ".\n"; - if (error_code_) { - io_context_.stop(); + boost::asio::async_write( + socket_, + data, + boost::bind(&TCPConsole::handle_write, this, _1, _2) + ); } - io_context_.run_for(boost::asio::chrono::milliseconds(100)); + + void TCPConsole::wait_next_line() + { + boost::asio::async_read_until( + socket_, + recv_buffer_, + newline_, + boost::bind(&TCPConsole::handle_read, this, _1, _2) + ); + } + + // TODO: Use std::optional here + std::string TCPConsole::extract_next_line() + { + char linebuf[1024]; + + std::istream is(&recv_buffer_); + is.getline(linebuf, sizeof(linebuf)); + if (is.good()) { + return linebuf; + } + + return ""; + } + + void TCPConsole::handle_read( + const boost::system::error_code& ec, + std::size_t bytes_transferred) + { + error_code_ = ec; + + if (ec) { + BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't read from %1%:%2%: %3%") + % host_name_ + % port_name_ + % ec.message(); + + io_context_.stop(); + } + else { + std::string line = extract_next_line(); + boost::trim(line); + + BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: received '%3%' from %1%:%2%") + % host_name_ + % port_name_ + % line; + + boost::to_lower(line); + + if (line == done_string_) { + transmit_next_command(); + } + else { + wait_next_line(); + } + } + } + + void TCPConsole::handle_write( + const boost::system::error_code& ec, + std::size_t) + { + error_code_ = ec; + if (ec) { + BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't write to %1%:%2%: %3%") + % host_name_ + % port_name_ + % ec.message(); + + io_context_.stop(); + } + else { + wait_next_line(); + } + } + + void TCPConsole::handle_connect(const boost::system::error_code& ec) + { + error_code_ = ec; + + if (ec) { + BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Can't connect to %1%:%2%: %3%") + % host_name_ + % port_name_ + % ec.message(); + + io_context_.stop(); + } + else { + BOOST_LOG_TRIVIAL(info) << boost::format("TCPConsole: connected to %1%:%2%") + % host_name_ + % port_name_; + + + transmit_next_command(); + } + } + + bool TCPConsole::run_queue() + { + try { + // TODO: Add more resets and initializations after previous run + + auto endpoints = resolver_.resolve(host_name_, port_name_); + + socket_.async_connect(endpoints->endpoint(), + boost::bind(&TCPConsole::handle_connect, this, _1) + ); + + // TODO: Add error and timeout processing + io_context_.restart(); + while (!io_context_.stopped()) { + BOOST_LOG_TRIVIAL(debug) << ".\n"; + if (error_code_) { + io_context_.stop(); + } + io_context_.run_for(boost::asio::chrono::milliseconds(100)); + } + + // Socket is not closed automatically by boost + socket_.close(); + + if (error_code_) { + // We expect that message is logged in handler + return false; + } + + // It's expected to have empty queue after successful exchange + if (!cmd_queue_.empty()) { + BOOST_LOG_TRIVIAL(error) << "TCPConsole: command queue is not empty after end of exchange"; + return false; + } + } + catch (std::exception& e) + { + BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Exception while talking with %1%:%2%: %3%") + % host_name_ + % port_name_ + % e.what(); + + return false; + } + + return true; + } + + } - - // Socket is not closed automatically by boost - socket_.close(); - - if (error_code_) { - // We expect that message is logged in handler - return false; - } - - // It's expected to have empty queue after successful exchange - if (!cmd_queue_.empty()) { - BOOST_LOG_TRIVIAL(error) << "TCPConsole: command queue is not empty after end of exchange"; - return false; - } - } - catch (std::exception& e) - { - BOOST_LOG_TRIVIAL(error) << boost::format("TCPConsole: Exception while talking with %1%:%2%: %3%") - % host_name_ - % port_name_ - % e.what(); - - return false; - } - - return true; -} - - -} } diff --git a/src/slic3r/Utils/TCPConsole.hpp b/src/slic3r/Utils/TCPConsole.hpp index 6ff65ad8a..aa3b7da6f 100644 --- a/src/slic3r/Utils/TCPConsole.hpp +++ b/src/slic3r/Utils/TCPConsole.hpp @@ -8,74 +8,74 @@ #include namespace Slic3r { -namespace Utils { + namespace Utils { -const char * default_newline = "\n"; -const char * default_done_string = "ok"; + const char* default_newline = "\n"; + const char* default_done_string = "ok"; -using boost::asio::ip::tcp; + using boost::asio::ip::tcp; -class TCPConsole -{ -public: - TCPConsole(): resolver_(io_context_), socket_(io_context_), newline_(default_newline), done_string_(default_done_string) {} + class TCPConsole + { + public: + TCPConsole() : resolver_(io_context_), socket_(io_context_), newline_(default_newline), done_string_(default_done_string) {} - TCPConsole(const std::string &host_name, const std::string &port_name): - resolver_(io_context_), socket_(io_context_), newline_(default_newline), done_string_(default_done_string) - { - set_remote(host_name, port_name); - } - ~TCPConsole(){} + TCPConsole(const std::string& host_name, const std::string& port_name) : + resolver_(io_context_), socket_(io_context_), newline_(default_newline), done_string_(default_done_string) + { + set_remote(host_name, port_name); + } + ~TCPConsole() {} - void set_line_delimiter(const std::string &newline) { - newline_ = newline; - } - void set_command_done_string(const std::string &done_string) { - done_string_ = done_string; - } + void set_line_delimiter(const std::string& newline) { + newline_ = newline; + } + void set_command_done_string(const std::string& done_string) { + done_string_ = done_string; + } - void set_remote(const std::string &host_name, const std::string &port_name) - { - host_name_ = host_name; - port_name_ = port_name; - } + void set_remote(const std::string& host_name, const std::string& port_name) + { + host_name_ = host_name; + port_name_ = port_name; + } - bool enqueue_cmd(const std::string &cmd) { - // TODO: Add multithread protection to queue - cmd_queue_.push_back(cmd); - return true; - } + bool enqueue_cmd(const std::string& cmd) { + // TODO: Add multithread protection to queue + cmd_queue_.push_back(cmd); + return true; + } - bool run_queue(); - std::string error_message() { - return error_code_.message(); - } + bool run_queue(); + std::string error_message() { + return error_code_.message(); + } -private: - void handle_connect(const boost::system::error_code& ec); - void handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred); - void handle_write(const boost::system::error_code& ec, std::size_t bytes_transferred); + private: + void handle_connect(const boost::system::error_code& ec); + void handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred); + void handle_write(const boost::system::error_code& ec, std::size_t bytes_transferred); - void transmit_next_command(); - void wait_next_line(); - std::string extract_next_line(); + void transmit_next_command(); + void wait_next_line(); + std::string extract_next_line(); - std::string host_name_; - std::string port_name_; - std::string newline_; - std::string done_string_; + std::string host_name_; + std::string port_name_; + std::string newline_; + std::string done_string_; - std::list cmd_queue_; + std::list cmd_queue_; - boost::asio::io_context io_context_; - tcp::resolver resolver_; - tcp::socket socket_; - boost::asio::streambuf recv_buffer_; + boost::asio::io_context io_context_; + tcp::resolver resolver_; + tcp::socket socket_; + boost::asio::streambuf recv_buffer_; - boost::system::error_code error_code_; -}; + boost::system::error_code error_code_; + }; -} // Utils + } // Utils } // Slic3r #endif