Falashforge 5M network support (#4546)

* build action changes

* Adding flashforge 5M series network support

* Update CMakeLists.txt

* Update Flashforge.cpp

* reverting files for pull request

---------

Co-authored-by: SoftFever <softfeverever@gmail.com>
This commit is contained in:
iherbak 2024-03-22 12:22:46 +01:00 committed by GitHub
parent 2bda9db207
commit f1c91dc551
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 256 additions and 16 deletions

View file

@ -103,7 +103,8 @@ static t_config_enum_values s_keys_map_PrintHostType {
{ "astrobox", htAstroBox },
{ "repetier", htRepetier },
{ "mks", htMKS },
{ "obico", htObico }
{ "obico", htObico },
{ "flashforge", htFlashforge}
};
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(PrintHostType)
@ -3066,6 +3067,7 @@ def = this->add("filament_loading_speed", coFloats);
def->enum_values.push_back("repetier");
def->enum_values.push_back("mks");
def->enum_values.push_back("obico");
def->enum_values.push_back("flashforge");
def->enum_labels.push_back("PrusaLink");
def->enum_labels.push_back("PrusaConnect");
def->enum_labels.push_back("Octo/Klipper");
@ -3075,6 +3077,7 @@ def = this->add("filament_loading_speed", coFloats);
def->enum_labels.push_back("Repetier");
def->enum_labels.push_back("MKS");
def->enum_labels.push_back("Obico");
def->enum_labels.push_back("Flashforge");
def->mode = comAdvanced;
def->cli = ConfigOptionDef::nocli;
def->set_default_value(new ConfigOptionEnum<PrintHostType>(htOctoPrint));

View file

@ -59,7 +59,7 @@ enum class FuzzySkinType {
};
enum PrintHostType {
htPrusaLink, htPrusaConnect, htOctoPrint, htDuet, htFlashAir, htAstroBox, htRepetier, htMKS, htObico
htPrusaLink, htPrusaConnect, htOctoPrint, htDuet, htFlashAir, htAstroBox, htRepetier, htMKS, htObico, htFlashforge
};
enum AuthorizationType {

View file

@ -508,6 +508,8 @@ set(SLIC3R_GUI_SOURCES
Utils/PrintHost.hpp
Utils/Serial.cpp
Utils/Serial.hpp
Utils/SerialMessageType.hpp
Utils/SerialMessage.hpp
Utils/MKS.cpp
Utils/MKS.hpp
Utils/WxFontUtils.cpp
@ -528,6 +530,8 @@ set(SLIC3R_GUI_SOURCES
GUI/PrinterCloudAuthDialog.hpp
Utils/Obico.cpp
Utils/Obico.hpp
Utils/Flashforge.cpp
Utils/Flashforge.hpp
)
if (WIN32)

View file

@ -513,6 +513,11 @@ void PhysicalPrinterDialog::update(bool printer_change)
}
}
}
if (opt->value == htFlashforge) {
m_optgroup->hide_field("printhost_apikey");
m_optgroup->hide_field("printhost_authorization_type");
}
}
else {
m_optgroup->set_value("host_type", int(PrintHostType::htOctoPrint), false);

View file

@ -0,0 +1,135 @@
#include "Flashforge.hpp"
#include <algorithm>
#include <ctime>
#include <chrono>
#include <thread>
#include <boost/filesystem/path.hpp>
#include <boost/format.hpp>
#include <boost/log/trivial.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include <wx/frame.h>
#include <wx/event.h>
#include <wx/progdlg.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/checkbox.h>
#include "libslic3r/PrintConfig.hpp"
#include "slic3r/GUI/GUI.hpp"
#include "slic3r/GUI/I18N.hpp"
#include "slic3r/GUI/MsgDialog.hpp"
#include "Http.hpp"
#include "TCPConsole.hpp"
#include "SerialMessage.hpp"
#include "SerialMessageType.hpp"
namespace fs = boost::filesystem;
namespace pt = boost::property_tree;
namespace Slic3r {
Flashforge::Flashforge(DynamicPrintConfig* config) : m_host(config->opt_string("print_host")), m_console_port("8899")
{
}
const char* Flashforge::get_name() const { return "Flashforge"; }
bool Flashforge::test(wxString& msg) const
{
BOOST_LOG_TRIVIAL(debug) << boost::format("[Flashforge] testing connection");
// Utils::TCPConsole console(m_host, m_console_port);
Utils::TCPConsole client(m_host, m_console_port);
client.enqueue_cmd(controlCommand);
bool res = client.run_queue();
if (!res) {
msg = wxString::FromUTF8(client.error_message().c_str());
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] testing connection failed");
} else {
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] testing connection success");
}
return res;
}
wxString Flashforge::get_test_ok_msg() const { return _(L("Connection to Flashforge works correctly.")); }
wxString Flashforge::get_test_failed_msg(wxString& msg) const
{
return GUI::from_u8((boost::format("%s: %s") % _utf8(L("Could not connect to Flashforge")) % std::string(msg.ToUTF8())).str());
}
bool Flashforge::upload(PrintHostUpload upload_data, ProgressFn progress_fn, ErrorFn error_fn, InfoFn info_fn) const
{
bool res = true;
Utils::TCPConsole client(m_host, m_console_port);
client.enqueue_cmd(controlCommand);
client.enqueue_cmd(connect5MCommand);
client.enqueue_cmd(statusCommand);
wxString errormsg;
try {
std::ifstream newfile;
newfile.open(upload_data.source_path.c_str(), std::ios::binary); // open a file to perform read operation using file object
if (newfile.is_open()) { // checking whether the file is open
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] Reading file...");
newfile.seekg(0, std::ios::end);
std::ifstream::pos_type pos = newfile.tellg();
std::vector<char> result(pos);
newfile.seekg(0, std::ios::beg);
newfile.read(&result[0], pos);
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] Reading file...done size is %1%") % result.size();
Slic3r::Utils::SerialMessage fileuploadCommand =
{(boost::format("~M28 %1% 0:/user/%2%") % result.size() % upload_data.upload_path.generic_string()).str(),
Slic3r::Utils::Command};
client.enqueue_cmd(fileuploadCommand);
Slic3r::Utils::SerialMessage dataCommand = {std::string(result.begin(), result.end()), Slic3r::Utils::Data};
client.enqueue_cmd(dataCommand);
newfile.close(); // close the file object.
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] Sent %1% ") % result.size();
}
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] Sending file save command ");
client.enqueue_cmd(saveFileCommand);
if (upload_data.post_action == PrintHostPostUploadAction::StartPrint) {
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] Starting print %1%") % upload_data.upload_path.string();
Slic3r::Utils::SerialMessage startPrintCommand = {(boost::format("~M23 0:/user/%1%") % upload_data.upload_path.string()).str(),
Slic3r::Utils::Command};
client.enqueue_cmd(startPrintCommand);
}
res = client.run_queue();
if (!res) {
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] error %1%") % client.error_message().c_str();
errormsg = wxString::FromUTF8(client.error_message().c_str());
}
if (!res) {
error_fn(std::move(errormsg));
}
} catch (const std::exception& e) {
BOOST_LOG_TRIVIAL(info) << boost::format("[Flashforge] error %1%") % e.what();
errormsg = wxString::FromUTF8(e.what());
error_fn(std::move(errormsg));
}
return res;
}
int Flashforge::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<int>("err", 0);
}
} // namespace Slic3r

View file

@ -0,0 +1,45 @@
#ifndef slic3r_FlashForge_hpp_
#define slic3r_FlashForge_hpp_
#include <string>
#include <wx/string.h>
#include "PrintHost.hpp"
#include "SerialMessage.hpp"
#include "SerialMessageType.hpp"
#include "../../libslic3r/PrintConfig.hpp"
namespace Slic3r {
class DynamicPrintConfig;
class Http;
class Flashforge : public PrintHost
{
public:
explicit Flashforge(DynamicPrintConfig *config);
~Flashforge() override = default;
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, InfoFn info_fn) const override;
bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return PrintHostPostUploadAction::StartPrint; }
std::string get_host() const override { return m_host; }
private:
std::string m_host;
std::string m_console_port;
Slic3r::Utils::SerialMessage controlCommand = {"~M601 S1\r\n",Slic3r::Utils::Command};
Slic3r::Utils::SerialMessage connect5MCommand = {"~M640\r\n",Slic3r::Utils::Command};
Slic3r::Utils::SerialMessage connectGuiderCommand = {"~M650\r\n",Slic3r::Utils::Command};
Slic3r::Utils::SerialMessage statusCommand = {"~M119\r\n",Slic3r::Utils::Command};
Slic3r::Utils::SerialMessage saveFileCommand = {"~M29\r\n",Slic3r::Utils::Command};
int get_err_code_from_body(const std::string &body) const;
};
} // namespace Slic3r
#endif

View file

@ -25,6 +25,8 @@
#include "slic3r/GUI/I18N.hpp"
#include "slic3r/GUI/MsgDialog.hpp"
#include "Http.hpp"
#include "SerialMessage.hpp"
#include "SerialMessageType.hpp"
namespace fs = boost::filesystem;
namespace pt = boost::property_tree;
@ -40,8 +42,8 @@ const char* MKS::get_name() const { return "MKS"; }
bool MKS::test(wxString& msg) const
{
Utils::TCPConsole console(m_host, m_console_port);
console.enqueue_cmd("M105");
Slic3r::Utils::SerialMessage s("M105", Slic3r::Utils::Command);
console.enqueue_cmd(s);
bool ret = console.run_queue();
if (!ret)
@ -126,9 +128,10 @@ bool MKS::start_print(wxString& msg, const std::string& filename) const
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
Utils::TCPConsole console(m_host, m_console_port);
console.enqueue_cmd(std::string("M23 ") + filename);
console.enqueue_cmd("M24");
Slic3r::Utils::SerialMessage s(std::string("M23 ") + filename, Slic3r::Utils::Command);
console.enqueue_cmd(s);
s.message = "M24";
console.enqueue_cmd(s);
bool ret = console.run_queue();

View file

@ -21,6 +21,7 @@
#include "MKS.hpp"
#include "../GUI/PrintHostDialogs.hpp"
#include "Obico.hpp"
#include "Flashforge.hpp"
namespace fs = boost::filesystem;
using boost::optional;
@ -56,6 +57,7 @@ PrintHost* PrintHost::get_print_host(DynamicPrintConfig *config)
case htPrusaConnect: return new PrusaConnect(config);
case htMKS: return new MKS(config);
case htObico: return new Obico(config);
case htFlashforge: return new Flashforge(config);
default: return nullptr;
}
} else {

View file

@ -0,0 +1,22 @@
#ifndef SERIALMESSAGE_H
#define SERIALMESSAGE_H
#include <string>
#include "SerialMessageType.hpp"
namespace Slic3r { namespace Utils {
struct SerialMessage
{
std::string message;
enum SerialMessageType messageType;
SerialMessage(std::string m, SerialMessageType mT)
{
message = m;
messageType = mT;
}
};
}}
#endif

View file

@ -0,0 +1,9 @@
#ifndef SERIALMESSAGE_TYPE_H
#define SERIALMESSAGE_TYPE_H
namespace Slic3r { namespace Utils {
enum SerialMessageType { Command, Data };
}} // namespace Slic3r::Utils
#endif

View file

@ -13,6 +13,8 @@
#include <string>
#include "TCPConsole.hpp"
#include "SerialMessage.hpp"
#include "SerialMessageType.hpp"
using boost::asio::steady_timer;
using boost::asio::ip::tcp;
@ -27,21 +29,25 @@ void TCPConsole::transmit_next_command()
return;
}
std::string cmd = m_cmd_queue.front();
SerialMessage cmd = m_cmd_queue.front();
m_cmd_queue.pop_front();
BOOST_LOG_TRIVIAL(debug) << boost::format("TCPConsole: transmitting '%3%' to %1%:%2%")
% m_host_name
% m_port_name
% cmd;
% cmd.message;
m_send_buffer = cmd + m_newline;
m_send_buffer = cmd.message;
if (cmd.messageType == Command) {
m_send_buffer += m_newline;
}
set_deadline_in(m_write_timeout);
boost::asio::async_write(
m_socket,
boost::asio::buffer(m_send_buffer),
boost::bind(&TCPConsole::handle_write, this, boost::placeholders::_1, boost::placeholders::_2)
boost::bind(&TCPConsole::handle_write, this, boost::placeholders::_1, boost::placeholders::_2, cmd.messageType)
);
}
@ -99,7 +105,7 @@ void TCPConsole::handle_read(
void TCPConsole::handle_write(
const boost::system::error_code& ec,
std::size_t)
std::size_t, SerialMessageType messageType)
{
m_error_code = ec;
if (ec) {
@ -111,8 +117,13 @@ void TCPConsole::handle_write(
m_io_context.stop();
}
else {
if(messageType == Command){
wait_next_line();
}
else{
transmit_next_command();
}
}
}
void TCPConsole::handle_connect(const boost::system::error_code& ec)

View file

@ -7,6 +7,7 @@
#include <boost/system/system_error.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/streambuf.hpp>
#include "SerialMessage.hpp"
namespace Slic3r {
namespace Utils {
@ -45,7 +46,7 @@ public:
m_port_name = port_name;
}
bool enqueue_cmd(const std::string& cmd) {
bool enqueue_cmd(const SerialMessage& cmd) {
// TODO: Add multithread protection to queue
m_cmd_queue.push_back(cmd);
return true;
@ -57,7 +58,7 @@ public:
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 handle_write(const boost::system::error_code& ec, std::size_t bytes_transferred, SerialMessageType messageType);
void transmit_next_command();
void wait_next_line();
@ -74,7 +75,7 @@ private:
std::chrono::steady_clock::duration m_write_timeout;
std::chrono::steady_clock::duration m_read_timeout;
std::deque<std::string> m_cmd_queue;
std::deque<SerialMessage> m_cmd_queue;
boost::asio::io_context m_io_context;
tcp::resolver m_resolver;