ENH:auto switch to using local broker for data transmission

jira:[STUDIO-11616]

Change-Id: I91066a629684dcff4efc73f310c2e0a7843954f4
This commit is contained in:
tao wang 2025-05-10 13:28:49 +08:00 committed by lane.wei
parent b1ca6c21c3
commit ac805e7d91
6 changed files with 95 additions and 68 deletions

View File

@ -681,7 +681,7 @@ std::string MachineObject::get_printer_series_str() const{ return DeviceManager:
void MachineObject::reload_printer_settings()
{
print_json.load_compatible_settings("", "");
parse_json("{}");
parse_json("cloud", "{}");
}
MachineObject::MachineObject(NetworkAgent* agent, std::string name, std::string id, std::string ip)
@ -3079,6 +3079,7 @@ void MachineObject::reset()
{
BOOST_LOG_TRIVIAL(trace) << "reset dev_id=" << dev_id;
last_update_time = std::chrono::system_clock::now();
subscribe_counter = SUBSCRIBE_RETRY_COUNT;
m_push_count = 0;
m_full_msg_count = 0;
is_220V_voltage = false;
@ -3096,10 +3097,11 @@ void MachineObject::reset()
extruder_axis_status = LOAD;
network_wired = false;
dev_connection_name = "";
subscribe_counter = SUBSCRIBE_RETRY_COUNT;
job_id_ = "";
m_plate_index = -1;
nt_reset_data();
// reset print_json
json empty_j;
print_json.diff2all_base_reset(empty_j);
@ -3111,10 +3113,15 @@ void MachineObject::reset()
vt_slot.erase(vt_slot.begin() + 1);
}
}
subtask_ = nullptr;
}
void MachineObject::nt_reset_data()
{
nt_try_local_tunnel = false;
nt_use_local_tunnel = false;
nt_cloud_full_msg_count = 0;
nt_local_full_msg_count = 0;
}
void MachineObject::set_print_state(std::string status)
@ -3122,15 +3129,12 @@ void MachineObject::set_print_state(std::string status)
print_status = status;
}
int MachineObject::connect(bool is_anonymous, bool use_openssl)
int MachineObject::connect(bool use_openssl)
{
if (dev_ip.empty()) return -1;
std::string username;
std::string password;
if (!is_anonymous) {
username = "bblp";
password = get_access_code();
}
std::string username = "bblp";
std::string password = get_access_code();
if (m_agent) {
try {
return m_agent->connect_printer(dev_id, dev_ip, username, password, use_openssl);
@ -3276,7 +3280,7 @@ static ENUM enum_index_of(char const *key, char const **enum_names, int enum_cou
return defl;
}
int MachineObject::parse_json(std::string payload, bool key_field_only)
int MachineObject::parse_json(std::string tunnel, std::string payload, bool key_field_only)
{
parse_msg_count++;
std::chrono::system_clock::time_point clock_start = std::chrono::system_clock::now();
@ -3319,6 +3323,11 @@ int MachineObject::parse_json(std::string payload, bool key_field_only)
BOOST_LOG_TRIVIAL(trace) << "static: get push_all msg, dev_id=" << dev_id;
m_push_count++;
m_full_msg_count++;
if (tunnel == "cloud") {nt_cloud_full_msg_count++;}
if (tunnel == "lan") {nt_local_full_msg_count++;}
nt_condition_local_tunnel();
if (!printer_type.empty())
print_json.load_compatible_settings(printer_type, "");
print_json.diff2all_base_reset(j_pre);
@ -3342,9 +3351,10 @@ int MachineObject::parse_json(std::string payload, bool key_field_only)
}
}
else {
if (!printer_type.empty() && connection_type() == "lan")
{
if (!printer_type.empty()) {
nt_local_full_msg_count++;
m_full_msg_count++;/* all message package is full at LAN mode*/
nt_condition_local_tunnel();
print_json.load_compatible_settings(printer_type, "");
}
@ -3450,7 +3460,7 @@ int MachineObject::parse_json(std::string payload, bool key_field_only)
if (Slic3r::get_logging_level() < level_string_to_boost("trace")) {
BOOST_LOG_TRIVIAL(info) << "parse_json: dev_id=" << dev_id << ", origin playload=" << j_pre.dump(0);
} else {
BOOST_LOG_TRIVIAL(trace) << "parse_json: dev_id=" << dev_id << ", merged playload=" << j.dump(0);
BOOST_LOG_TRIVIAL(trace) << "parse_json: dev_id=" << dev_id << ", tunnel is=" << tunnel << ", merged playload=" << j.dump(0);
}
// Parse version info first, as if version arrive or change, 'print' need parse again with new compatible settings
@ -5548,17 +5558,20 @@ int MachineObject::parse_json(std::string payload, bool key_field_only)
}
catch (...) {}
if (!key_field_only) {
if (m_active_state == Active && !module_vers.empty() && check_version_valid()
&& !is_camera_busy_off()) {
m_active_state = UpdateToDate;
parse_version_func();
if (is_support_tunnel_mqtt && connection_type() != "lan") {
m_agent->start_subscribe("tunnel");
}
parse_state_changed_event();
}
}
if (!key_field_only) {
BOOST_LOG_TRIVIAL(trace) << "parse_json m_active_state =" << m_active_state;
//if (m_active_state == Active && !is_camera_busy_off()) {
// if (is_support_tunnel_mqtt && connection_type() != "lan") {
// m_active_state = UpdateToDate;
// m_agent->start_subscribe("tunnel");
// }
//} else if (m_active_state == UpdateToDate && is_camera_busy_off()) {
// m_active_state = Active;
// m_agent->stop_subscribe("tunnel");
//}
parse_state_changed_event();
}
}
catch (...) {
BOOST_LOG_TRIVIAL(trace) << "parse_json failed! dev_id=" << this->dev_id <<", payload = " << payload;
@ -5950,6 +5963,31 @@ std::string MachineObject::get_string_from_fantype(int type)
return "";
}
void MachineObject::nt_condition_local_tunnel()
{
int full_msg_count_limit = 2;
if (!nt_try_local_tunnel && nt_cloud_full_msg_count == full_msg_count_limit) {
connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
nt_try_local_tunnel = true;
}
if (!nt_use_local_tunnel && nt_try_local_tunnel && nt_local_full_msg_count == full_msg_count_limit) {
std::vector<std::string> dev_list{dev_id};
Slic3r::GUI::wxGetApp().getAgent()->del_subscribe(dev_list);
nt_use_local_tunnel = true;
}
}
void MachineObject::nt_restore_cloud_tunnel()
{
if (is_connected()) {
disconnect();
std::vector<std::string> dev_list{dev_id};
Slic3r::GUI::wxGetApp().getAgent()->add_subscribe(dev_list);
nt_use_local_tunnel = false;
}
}
NozzleFlowType MachineObject::get_nozzle_flow_type(int extruder_id) const
{
if (is_nozzle_flow_type_supported() && m_extder_data.extders.size() > extruder_id)
@ -7017,14 +7055,22 @@ void DeviceManager::check_pushing()
{
keep_alive();
MachineObject* obj = this->get_selected_machine();
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
auto internal = std::chrono::duration_cast<std::chrono::milliseconds>(start - obj->last_update_time);
if (obj && !obj->is_support_mqtt_alive) {
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
auto internal = std::chrono::duration_cast<std::chrono::milliseconds>(start - obj->last_update_time);
if (internal.count() > TIMEOUT_FOR_STRAT && internal.count() < 1000 * 60 * 60 * 300) {
BOOST_LOG_TRIVIAL(info) << "command_pushing: diff = " << internal.count();
obj->command_pushing("start");
}
}
/*check local tunnel state*/
if (obj && obj->nt_use_local_tunnel && internal.count() > PUSHINFO_TIMEOUT) {
obj->nt_restore_cloud_tunnel();
BOOST_LOG_TRIVIAL(info) << "Unable to receive more data in LAN tunnel";
}
}
void DeviceManager::on_machine_alive(std::string json_str)
@ -7346,9 +7392,9 @@ bool DeviceManager::set_selected_machine(std::string dev_id, bool need_disconnec
if (!need_disconnect) {m_agent->disconnect_printer();}
it->second->reset();
#if !BBL_RELEASE_TO_PUBLIC
it->second->connect(false, Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
it->second->connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
#else
it->second->connect(false, it->second->local_use_ssl_for_mqtt);
it->second->connect(it->second->local_use_ssl_for_mqtt);
#endif
it->second->set_lan_mode_connection_state(true);
}
@ -7358,6 +7404,7 @@ bool DeviceManager::set_selected_machine(std::string dev_id, bool need_disconnec
if (it->second->connection_type() != "lan" || it->second->connection_type().empty()) {
if (m_agent->get_user_selected_machine() == dev_id) {
it->second->reset_update_time();
it->second->nt_reset_data();
}
else {
BOOST_LOG_TRIVIAL(info) << "static: set_selected_machine: same dev_id = " << dev_id;
@ -7368,9 +7415,9 @@ bool DeviceManager::set_selected_machine(std::string dev_id, bool need_disconnec
BOOST_LOG_TRIVIAL(info) << "static: set_selected_machine: same dev_id = empty";
it->second->reset();
#if !BBL_RELEASE_TO_PUBLIC
it->second->connect(false, Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
it->second->connect(Slic3r::GUI::wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
#else
it->second->connect(false, it->second->local_use_ssl_for_mqtt);
it->second->connect(it->second->local_use_ssl_for_mqtt);
#endif
m_agent->set_user_selected_machine(dev_id);
it->second->set_lan_mode_connection_state(true);

View File

@ -1109,7 +1109,7 @@ public:
std::vector<HMSItem> hms_list;
/* machine mqtt apis */
int connect(bool is_anonymous = false, bool use_openssl = true);
int connect(bool use_openssl = true);
int disconnect();
json_diff print_json;
@ -1305,7 +1305,7 @@ public:
int publish_json(std::string json_str, int qos = 0, int flag = 0);
int cloud_publish_json(std::string json_str, int qos = 0, int flag = 0);
int local_publish_json(std::string json_str, int qos = 0, int flag = 0);
int parse_json(std::string payload, bool key_filed_only = false);
int parse_json(std::string tunnel, std::string payload, bool key_filed_only = false);
int publish_gcode(std::string gcode_str);
std::string setting_id_to_type(std::string setting_id, std::string tray_type);
@ -1324,6 +1324,15 @@ public:
bool is_firmware_info_valid();
std::string get_string_from_fantype(int type);
/*for local mqtt tunnel try*/
bool nt_try_local_tunnel { false };
bool nt_use_local_tunnel { false };
int nt_cloud_full_msg_count { 0 };
int nt_local_full_msg_count { 0 };
void nt_condition_local_tunnel();
void nt_restore_cloud_tunnel();
void nt_reset_data();
/*for more extruder*/
bool is_enable_np{ false };
bool is_enable_ams_np{ false };

View File

@ -2196,10 +2196,10 @@ void GUI_App::init_networking_callbacks()
auto sel = this->m_device_manager->get_selected_machine();
if (sel && sel->dev_id == dev_id) {
obj->parse_json(msg);
obj->parse_json("cloud", msg);
}
else {
obj->parse_json(msg, true);
obj->parse_json("cloud", msg, true);
}
@ -2245,20 +2245,13 @@ void GUI_App::init_networking_callbacks()
this->process_network_msg(dev_id, msg);
MachineObject* obj = m_device_manager->get_my_machine(dev_id);
if (!obj || !obj->is_lan_mode_printer()) {
obj = m_device_manager->get_local_machine(dev_id);
}
if (obj) {
obj->parse_json(msg, DeviceManager::key_field_only);
obj->parse_json("lan", msg, DeviceManager::key_field_only);
if (this->m_device_manager->get_selected_machine() == obj && obj->is_ams_need_update) {
GUI::wxGetApp().sidebar().load_ams_list(obj->dev_id, obj);
}
}
obj = m_device_manager->get_local_machine(dev_id);
if (obj) {
obj->parse_json(msg, DeviceManager::key_field_only);
}
if (GUI::wxGetApp().plater())
GUI::wxGetApp().plater()->update_machine_sync_status();

View File

@ -2356,9 +2356,9 @@ void SelectMachineDialog::connect_printer_mqtt()
m_status_bar->disable_cancel_button();
m_status_bar->set_status_text(_L("Connecting to the printer. Unable to cancel during the connection process."));
#if !BBL_RELEASE_TO_PUBLIC
obj_->connect(false, wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
obj_->connect(wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
#else
obj_->connect(false, obj_->local_use_ssl_for_mqtt);
obj_->connect(obj_->local_use_ssl_for_mqtt);
#endif
}
else {

View File

@ -1096,7 +1096,6 @@ void SyncAmsInfoDialog::reinit_dialog()
void SyncAmsInfoDialog::init_bind()
{
Bind(wxEVT_TIMER, &SyncAmsInfoDialog::on_timer, this);
Bind(EVT_CLEAR_IPADDRESS, &SyncAmsInfoDialog::clear_ip_address_config, this);
Bind(EVT_SHOW_ERROR_INFO, [this](auto &e) { show_print_failed_info(true); });
Bind(EVT_UPDATE_USER_MACHINE_LIST, &SyncAmsInfoDialog::update_printer_combobox, this);
Bind(EVT_PRINT_JOB_CANCEL, &SyncAmsInfoDialog::on_print_job_cancel, this);
@ -2033,25 +2032,6 @@ void SyncAmsInfoDialog::Enable_Auto_Refill(bool enable)
m_ams_backup_tip->Refresh();
}
void SyncAmsInfoDialog::connect_printer_mqtt()
{
DeviceManager *dev = Slic3r::GUI::wxGetApp().getDeviceManager();
if (!dev) return;
MachineObject *obj_ = dev->get_selected_machine();
if (obj_->connection_type() == "cloud") {
show_status(PrintDialogStatus::PrintStatusSending);
#if !BBL_RELEASE_TO_PUBLIC
obj_->connect(false, wxGetApp().app_config->get("enable_ssl_for_mqtt") == "true" ? true : false);
#else
obj_->connect(false, obj_->local_use_ssl_for_mqtt);
#endif
}
}
void SyncAmsInfoDialog::clear_ip_address_config(wxCommandEvent &e) { prepare_mode(); }
void SyncAmsInfoDialog::update_user_machine_list()
{
NetworkAgent *m_agent = wxGetApp().getAgent();

View File

@ -175,8 +175,6 @@ public:
void on_cancel(wxCloseEvent &event);
void show_errors(wxString &info);
void Enable_Auto_Refill(bool enable);
void connect_printer_mqtt();
void clear_ip_address_config(wxCommandEvent &e);
void on_refresh(wxCommandEvent &event);
void on_set_finish_mapping(wxCommandEvent &evt);
void on_print_job_cancel(wxCommandEvent &evt);