NEW: add HMS to query hms info

display hms msg in HMS tab of Monitor

Change-Id: I4df1c63b104463ba62cdf7eb079a540a4176f8c5
Signed-off-by: Stone Li <stone.li@bambulab.com>
(cherry picked from commit f4635d18f3383e70177536dfbe5edf2e1370b746)
This commit is contained in:
Stone Li 2022-08-05 17:57:07 +08:00 committed by Lane.Wei
parent efd20d0a84
commit 73e4078348
17 changed files with 385 additions and 128 deletions

View File

@ -44,7 +44,7 @@ static const std::string MODELS_STR = "models";
const std::string AppConfig::SECTION_FILAMENTS = "filaments";
const std::string AppConfig::SECTION_MATERIALS = "sla_materials";
std::string AppConfig::get_langauge_code()
std::string AppConfig::get_language_code()
{
std::string get_lang = get("language");
if (get_lang.empty()) return "";

View File

@ -39,7 +39,7 @@ public:
this->reset();
}
std::string get_langauge_code();
std::string get_language_code();
std::string get_hms_host();
// Clear and reset to defaults.

View File

@ -213,8 +213,8 @@ set(SLIC3R_GUI_SOURCES
GUI/MonitorPage.hpp
GUI/StatusPanel.cpp
GUI/StatusPanel.hpp
GUI/UpdateErrorMessage.cpp
GUI/UpdateErrorMessage.hpp
GUI/HMS.hpp
GUI/HMS.cpp
GUI/SliceInfoPanel.cpp
GUI/SliceInfoPanel.hpp
GUI/CameraPopup.cpp

View File

@ -209,6 +209,18 @@ bool HMSItem::parse_hms_info(unsigned attr, unsigned code)
return result;
}
std::string HMSItem::get_long_error_code()
{
char buf[64];
::sprintf(buf, "%02X%02X%02X00000%1X%04X",
this->module_id,
this->module_num,
this->part_id,
(int)this->msg_level,
this->msg_code);
return std::string(buf);
}
wxString HMSItem::get_module_name(ModuleID module_id)
{
switch (module_id)

View File

@ -240,6 +240,8 @@ public:
HMSMessageLevel msg_level = HMS_UNKNOWN;
int msg_code = 0;
bool parse_hms_info(unsigned attr, unsigned code);
std::string get_long_error_code();
static wxString get_module_name(ModuleID module_id);
static wxString get_hms_msg_level_str(HMSMessageLevel level);
};

View File

@ -1087,6 +1087,12 @@ void GUI_App::post_init()
mainframe->refresh_plugin_tips();
});
// update hms info
CallAfter([this] {
if (hms_query)
hms_query->check_hms_info();
});
BOOST_LOG_TRIVIAL(info) << "finished post_init";
//BBS: remove the single instance currently
/*#ifdef _WIN32
@ -1109,6 +1115,7 @@ GUI_App::GUI_App()
, m_app_mode(EAppMode::Editor)
, m_em_unit(10)
, m_imgui(new ImGuiWrapper())
, hms_query(new HMSQuery())
//, m_removable_drive_manager(std::make_unique<RemovableDriveManager>())
//, m_other_instance_message_handler(std::make_unique<OtherInstanceMessageHandler>())
{
@ -2605,6 +2612,10 @@ void GUI_App::recreate_GUI(const wxString& msg_name)
obj_list()->set_min_height();
update_mode();
//check hms info for different language
if (hms_query)
hms_query->check_hms_info();
//BBS: trigger restore project logic here, and skip confirm
plater_->trigger_restore_project(1);

View File

@ -11,6 +11,7 @@
#include "slic3r/GUI/DeviceManager.hpp"
#include "slic3r/Utils/NetworkAgent.hpp"
#include "slic3r/GUI/WebViewDialog.hpp"
#include "slic3r/GUI/HMS.hpp"
#include "slic3r/GUI/Jobs/UpgradeNetworkJob.hpp"
#include <wx/app.h>
@ -59,6 +60,7 @@ class ParamsPanel;
class NotificationManager;
struct GUI_InitParams;
class ParamsDialog;
class HMSQuery;
enum FileType
@ -260,6 +262,7 @@ private:
std::shared_ptr<UpgradeNetworkJob> m_upgrade_network_job;
VersionInfo version_info;
HMSQuery *hms_query { nullptr };
boost::thread m_sync_update_thread;
bool enable_sync = false;
@ -276,6 +279,7 @@ public:
void show_message_box(std::string msg) { wxMessageBox(msg); }
EAppMode get_app_mode() const { return m_app_mode; }
Slic3r::DeviceManager* getDeviceManager() { return m_device_manager; }
HMSQuery* get_hms_query() { return hms_query; }
NetworkAgent* getAgent() { return m_agent; }
bool is_editor() const { return m_app_mode == EAppMode::Editor; }
bool is_gcode_viewer() const { return m_app_mode == EAppMode::GCodeViewer; }

View File

@ -12,6 +12,7 @@
#include "slic3r/GUI/Plater.hpp"
#include "slic3r/GUI/GUI_ObjectList.hpp"
#include "slic3r/GUI/format.hpp"
#include "slic3r/GUI/GUI.hpp"
#include "slic3r/Utils/UndoRedo.hpp"
@ -181,12 +182,6 @@ void GLGizmoFdmSupports::on_set_state()
}
}
static std::string into_u8(const wxString& str)
{
auto buffer_utf8 = str.utf8_str();
return std::string(buffer_utf8.data());
}
void GLGizmoFdmSupports::on_render_input_window(float x, float y, float bottom_limit)
{
init_print_instance();

View File

@ -9,6 +9,7 @@
#include "slic3r/GUI/format.hpp"
#include "slic3r/GUI/GUI_ObjectList.hpp"
#include "slic3r/GUI/NotificationManager.hpp"
#include "slic3r/GUI/GUI.hpp"
#include "libslic3r/PresetBundle.hpp"
#include "libslic3r/Model.hpp"
#include "slic3r/Utils/UndoRedo.hpp"
@ -291,12 +292,6 @@ static void render_extruders_combo(const std::string &labe
selection_idx = selection_out;
}
static std::string into_u8(const wxString& str)
{
auto buffer_utf8 = str.utf8_str();
return std::string(buffer_utf8.data());
}
void GLGizmoMmuSegmentation::show_tooltip_information(float caption_max, float x, float y)
{
ImTextureID normal_id = m_parent.get_gizmos_manager().get_icon_texture_id(GLGizmosManager::MENU_ICON_NAME::IC_TOOLBAR_TOOLTIP);

View File

@ -8,6 +8,7 @@
#include "slic3r/GUI/ImGuiWrapper.hpp"
#include "slic3r/GUI/Plater.hpp"
#include "slic3r/GUI/GUI_ObjectList.hpp"
#include "slic3r/GUI/GUI.hpp"
#include "slic3r/Utils/UndoRedo.hpp"
#include <GL/glew.h>
@ -155,12 +156,6 @@ void GLGizmoSeam::tool_changed(wchar_t old_tool, wchar_t new_tool)
}
}
static std::string into_u8(const wxString& str)
{
auto buffer_utf8 = str.utf8_str();
return std::string(buffer_utf8.data());
}
void GLGizmoSeam::on_render_input_window(float x, float y, float bottom_limit)
{
if (! m_c->selection_info()->model_object())

284
src/slic3r/GUI/HMS.cpp Normal file
View File

@ -0,0 +1,284 @@
#include "HMS.hpp"
namespace Slic3r {
namespace GUI {
int get_hms_info_version(std::string& version)
{
AppConfig* config = wxGetApp().app_config;
if (!config)
return -1;
std::string hms_host = config->get_hms_host();
if(hms_host.empty()) {
BOOST_LOG_TRIVIAL(error) << "hms_host is empty";
return -1;
}
int result = -1;
version = "";
std::string url = (boost::format("https://%1%/GetVersion.php") % hms_host).str();
Slic3r::Http http = Slic3r::Http::get(url);
http.timeout_max(10)
.on_complete([&result, &version](std::string body, unsigned status){
try {
json j = json::parse(body);
if (j.contains("ver")) {
version = std::to_string(j["ver"].get<long long>());
}
} catch (...) {
;
}
})
.on_error([&result](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << "get_hms_info_version: body = " << body << ", status = " << status << ", error = " << error;
result = -1;
})
.perform_sync();
return result;
}
int HMSQuery::download_hms_info()
{
AppConfig* config = wxGetApp().app_config;
if (!config) return -1;
std::string hms_host = wxGetApp().app_config->get_hms_host();
std::string lang_code = wxGetApp().app_config->get_language_code();
std::string url = (boost::format("https://%1%/query.php?lang=%2%") % hms_host % lang_code).str();
Slic3r::Http http = Slic3r::Http::get(url);
http.on_complete([this](std::string body, unsigned status) {
try {
json j = json::parse(body);
if (j.contains("result")) {
if (j["result"] == 0 && j.contains("data")) {
this->m_hms_json = j["data"];
if (j.contains("ver"))
m_hms_json["version"] = std::to_string(j["ver"].get<long long>());
} else {
this->m_hms_json.clear();
BOOST_LOG_TRIVIAL(info) << "HMSQuery: update hms info error = " << j["result"].get<int>();
}
}
} catch (...) {
;
}
})
.timeout_max(20)
.on_error([](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << "HMSQuery: update hms info error = " << error << ", body = " << body << ", status = " << status;
}).perform_sync();
save_to_local();
return 0;
}
int HMSQuery::load_from_local(std::string &version_info)
{
if (data_dir().empty()) {
version_info = "";
BOOST_LOG_TRIVIAL(error) << "HMS: load_from_local, data_dir() is empty";
return -1;
}
std::string filename = get_hms_file();
std::string dir_str = (boost::filesystem::path(data_dir()) / filename).make_preferred().string();
std::ifstream json_file(encode_path(dir_str.c_str()));
try {
if (json_file.is_open()) {
json_file >> m_hms_json;
if (m_hms_json.contains("version")) {
version_info = m_hms_json["version"].get<std::string>();
return 0;
} else {
BOOST_LOG_TRIVIAL(warning) << "HMS: load_from_local, no version info";
return 0;
}
}
} catch(...) {
version_info = "";
return -1;
}
version_info = "";
return 0;
}
int HMSQuery::save_to_local()
{
if (data_dir().empty()) {
BOOST_LOG_TRIVIAL(error) << "HMS: save_to_local, data_dir() is empty";
return -1;
}
std::string filename = get_hms_file();
std::string dir_str = (boost::filesystem::path(data_dir()) / filename).make_preferred().string();
std::ofstream json_file(encode_path(dir_str.c_str()));
if (json_file.is_open()) {
json_file << std::setw(4) << m_hms_json << std::endl;
json_file.close();
return 0;
}
return -1;
}
std::string HMSQuery::get_hms_file()
{
AppConfig* config = wxGetApp().app_config;
if (!config)
return HMS_INFO_FILE;
std::string lang_code = wxGetApp().app_config->get_language_code();
return (boost::format("hms_%1%.json") % lang_code).str();
}
wxString HMSQuery::query_hms_msg(std::string long_error_code)
{
if (long_error_code.empty())
return wxEmptyString;
AppConfig* config = wxGetApp().app_config;
if (!config) return wxEmptyString;
std::string hms_host = wxGetApp().app_config->get_hms_host();
std::string lang_code = wxGetApp().app_config->get_language_code();
if (m_hms_json.contains("device_hms")) {
if (m_hms_json["device_hms"].contains(lang_code)) {
for (auto item = m_hms_json["device_hms"][lang_code].begin(); item != m_hms_json["device_hms"][lang_code].end(); item++) {
if (item->contains("ecode") && (*item)["ecode"].get<std::string>() == long_error_code) {
if (item->contains("intro")) {
return wxString::FromUTF8((*item)["intro"].get<std::string>());
}
}
}
BOOST_LOG_TRIVIAL(info) << "hms: query_hms_msg, not found error_code = " << long_error_code;
}
} else {
return wxEmptyString;
}
return wxEmptyString;
}
wxString HMSQuery::query_error_msg(std::string error_code)
{
AppConfig* config = wxGetApp().app_config;
if (!config) return wxEmptyString;
std::string hms_host = wxGetApp().app_config->get_hms_host();
std::string lang_code = wxGetApp().app_config->get_language_code();
if (m_hms_json.contains("device_error")) {
if (m_hms_json["device_error"].contains(lang_code)) {
for (auto item = m_hms_json["device_error"][lang_code].begin(); item != m_hms_json["device_error"][lang_code].end(); item++) {
if (item->contains("ecode") && (*item)["ecode"].get<std::string>() == error_code) {
if (item->contains("intro")) {
return wxString::FromUTF8((*item)["intro"].get<std::string>());
}
}
}
BOOST_LOG_TRIVIAL(info) << "hms: query_error_msg, not found error_code = " << error_code;
}
}
else {
return wxEmptyString;
}
return wxEmptyString;
}
wxString HMSQuery::query_print_error_msg(int print_error)
{
char buf[32];
::sprintf(buf, "%08X", print_error);
return query_error_msg(std::string(buf));
}
int HMSQuery::check_hms_info()
{
int result = 0;
bool download_new_hms_info = true;
// load local hms json file
std::string version = "";
if (load_from_local(version) == 0) {
BOOST_LOG_TRIVIAL(info) << "HMS: check_hms_info current version = " << version;
std::string new_version;
get_hms_info_version(new_version);
BOOST_LOG_TRIVIAL(info) << "HMS: check_hms_info latest version = " << new_version;
if (!version.empty() && version == new_version) {
download_new_hms_info = false;
}
}
BOOST_LOG_TRIVIAL(info) << "HMS: check_hms_info need download new hms info = " << download_new_hms_info;
// download if version is update
if (download_new_hms_info) {
result = download_hms_info();
}
return result;
}
std::string get_hms_wiki_url(int code)
{
AppConfig* config = wxGetApp().app_config;
if (!config) return "";
char buf[32];
::sprintf(buf, "%08X", code);
std::string error_code = std::string(buf);
std::string hms_host = wxGetApp().app_config->get_hms_host();
std::string lang_code = wxGetApp().app_config->get_language_code();
std::string url = (boost::format("https://%1%/index.php?e=%2%&s=hms&lang=%3%")
% hms_host
% error_code
% lang_code).str();
return url;
}
std::string get_error_message(int error_code)
{
char buf[64];
std::string result_str = "";
std::sprintf(buf,"%08X",error_code);
std::string hms_host = wxGetApp().app_config->get_hms_host();
std::string get_lang = wxGetApp().app_config->get_language_code();
std::string url = (boost::format("https://%1%/query.php?lang=%2%&e=%3%")
%hms_host
%get_lang
%buf).str();
Slic3r::Http http = Slic3r::Http::get(url);
http.header("accept", "application/json")
.timeout_max(10)
.on_complete([get_lang, &result_str](std::string body, unsigned status) {
try {
json j = json::parse(body);
if (j.contains("result")) {
if (j["result"].get<int>() == 0) {
if (j.contains("data")) {
json jj = j["data"];
if (jj.contains("device_error")) {
if (jj["device_error"].contains(get_lang)) {
if (jj["device_error"][get_lang].size() > 0) {
if (!jj["device_error"][get_lang][0]["intro"].empty() || !jj["device_error"][get_lang][0]["ecode"].empty()) {
std::string error_info = jj["device_error"][get_lang][0]["intro"].get<std::string>();
std::string error_code = jj["device_error"][get_lang][0]["ecode"].get<std::string>();
error_code.insert(4, " ");
result_str = from_u8(error_info).ToStdString() + "[" + error_code + "]";
}
}
}
}
}
}
}
} catch (...) {
;
}
})
.on_error([](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(trace) << boost::format("[BBL ErrorMessage]: status=%1%, error=%2%, body=%3%") % status % error % body;
}).perform_sync();
return result_str;
}
}
}

45
src/slic3r/GUI/HMS.hpp Normal file
View File

@ -0,0 +1,45 @@
#ifndef slic3r_HMS_hpp_
#define slic3r_HMS_hpp_
#include "GUI_App.hpp"
#include "GUI.hpp"
#include "I18N.hpp"
#include "Widgets/Label.hpp"
#include "Widgets/Button.hpp"
#include "Widgets/StepCtrl.hpp"
#include "BitmapCache.hpp"
#include "slic3r/Utils/Http.hpp"
#include "libslic3r/Thread.hpp"
#include "nlohmann/json.hpp"
namespace Slic3r {
namespace GUI {
#define HMS_INFO_FILE "hms.json"
class HMSQuery {
protected:
json m_hms_json;
int download_hms_info();
int load_from_local(std::string& version_info);
int save_to_local();
std::string get_hms_file();
public:
HMSQuery() {}
int check_hms_info();
wxString query_hms_msg(std::string long_error_code);
wxString query_error_msg(std::string error_code);
wxString query_print_error_msg(int print_error);
};
int get_hms_info_version(std::string &version);
std::string get_hms_wiki_url(int code);
std::string get_error_message(int error_code);
}
}
#endif

View File

@ -2,6 +2,7 @@
#include <slic3r/GUI/Widgets/Label.hpp>
#include <slic3r/GUI/I18N.hpp>
#include "GUI.hpp"
#include "GUI_App.hpp"
namespace Slic3r {
namespace GUI {
@ -34,7 +35,7 @@ HMSPanel::HMSPanel(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wx
}
HMSPanel::~HMSPanel() {
;
}
void HMSPanel::update(MachineObject *obj)
@ -42,12 +43,8 @@ void HMSPanel::update(MachineObject *obj)
if (obj) {
wxString hms_text;
for (auto item : obj->hms_list) {
hms_text += wxString::Format("Module_ID = %s, module_num = %d,part_id = %d, msg level = %s msg code: 0x%x\n",
HMSItem::get_module_name(item.module_id),
item.module_num,
item.part_id,
HMSItem::get_hms_msg_level_str(item.msg_level),
(unsigned)item.msg_code);
if (wxGetApp().get_hms_query())
hms_text += wxGetApp().get_hms_query()->query_hms_msg(item.get_long_error_code()) + "\n";
}
m_hms_content->SetLabelText(hms_text);
} else {

View File

@ -1019,7 +1019,6 @@ StatusPanel::StatusPanel(wxWindow *parent, wxWindowID id, const wxPoint &pos, co
Bind(EVT_AMS_REFRESH_RFID, &StatusPanel::on_ams_refresh_rfid, this);
Bind(EVT_AMS_ON_SELECTED, &StatusPanel::on_ams_selected, this);
Bind(EVT_AMS_ON_FILAMENT_EDIT, &StatusPanel::on_filament_edit, this);
Bind(EVT_UPDATE_ERROR_MESSAGE, &StatusPanel::on_update_error_message, this);
m_switch_speed->Connect(wxEVT_LEFT_DOWN, wxCommandEventHandler(StatusPanel::on_switch_speed), NULL, this);
m_calibration_btn->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(StatusPanel::on_start_calibration), NULL, this);
@ -1246,9 +1245,9 @@ void StatusPanel::update(MachineObject *obj)
m_machine_ctrl_panel->Thaw();
}
void StatusPanel::on_update_error_message(wxCommandEvent &event)
void StatusPanel::show_error_message(wxString msg)
{
m_error_text->SetLabel(event.GetString());
m_error_text->SetLabel(msg);
m_staticline->Show();
m_panel_error_txt->Show();
}
@ -1264,13 +1263,18 @@ void StatusPanel::update_error_message()
}
if (before_error_code != obj->print_error) {
get_error_message_thread = new boost::thread(Slic3r::create_thread([&] {
std::string message = show_error_message(obj->print_error);
wxCommandEvent event(EVT_UPDATE_ERROR_MESSAGE);
event.SetString(wxString(message));
event.SetEventObject(this);
wxPostEvent(this, event);
}));
if (wxGetApp().get_hms_query()) {
char buf[32];
::sprintf(buf, "%08X", obj->print_error);
std::string print_error_str = std::string(buf);
if (print_error_str.size() > 4) {
print_error_str.insert(4, " ");
}
wxString error_msg = wxString::Format("%s[%s]",
wxGetApp().get_hms_query()->query_print_error_msg(obj->print_error),
print_error_str);
show_error_message(error_msg);
}
before_error_code = obj->print_error;
}
}

View File

@ -27,7 +27,7 @@
#include "Widgets/ProgressBar.hpp"
#include "Widgets/ImageSwitchButton.hpp"
#include "Widgets/AMSControl.hpp"
#include "UpdateErrorMessage.hpp"
#include "HMS.hpp"
#include "Widgets/wxStaticText2.hpp"
class StepIndicator;
@ -268,7 +268,7 @@ protected:
void on_subtask_pause_resume(wxCommandEvent &event);
void on_subtask_abort(wxCommandEvent &event);
void on_subtask_clean(wxCommandEvent &event);
void on_update_error_message(wxCommandEvent &event);
void show_error_message(wxString msg);
void error_info_reset();
/* axis control */
@ -352,7 +352,6 @@ public:
long last_ams_version { -1 };
std::vector<int> last_stage_list_info;
boost::thread * get_error_message_thread{nullptr};
bool is_stage_list_info_changed(MachineObject* obj);

View File

@ -1,60 +0,0 @@
#include "UpdateErrorMessage.hpp"
namespace Slic3r {
namespace GUI {
wxDEFINE_EVENT(EVT_UPDATE_ERROR_MESSAGE, wxCommandEvent);
std::string show_error_message(int error_code)
{
char buf[64];
std::string result_str = "";
std::sprintf(buf,"%08X",error_code);
std::string hms_host = wxGetApp().app_config->get_hms_host();
std::string get_lang = wxGetApp().app_config->get_langauge_code();
std::string url = (boost::format("https://%1%/query.php?lang=%2%&e=%3%")
%hms_host
%get_lang
%buf).str();
Slic3r::Http http = Slic3r::Http::get(url);
http.header("accept", "application/json")
.timeout_max(10)
.on_complete([get_lang, &result_str](std::string body, unsigned status) {
try {
json j = json::parse(body);
if (j.contains("result")) {
if (j["result"].get<int>() == 0) {
if (j.contains("data")) {
json jj = j["data"];
if (jj.contains("device_error")) {
if (jj["device_error"].contains(get_lang)) {
if (jj["device_error"][get_lang].size() > 0) {
if (!jj["device_error"][get_lang][0]["intro"].empty() || !jj["device_error"][get_lang][0]["ecode"].empty()) {
std::string error_info = jj["device_error"][get_lang][0]["intro"].get<std::string>();
std::string error_code = jj["device_error"][get_lang][0]["ecode"].get<std::string>();
error_code.insert(4, " ");
result_str = from_u8(error_info).ToStdString() + "[" + error_code + "]";
}
}
}
}
}
}
}
} catch (...) {
;
}
})
.on_error([](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(trace) << boost::format("[BBL ErrorMessage]: status=%1%, error=%2%, body=%3%") % status % error % body;
}).perform_sync();
return result_str;
}
}
}

View File

@ -1,26 +0,0 @@
#ifndef slic3r_UpdateErrorMessage_hpp_
#define slic3r_UpdateErrorMessage_hpp_
#include "GUI_App.hpp"
#include "GUI.hpp"
#include "I18N.hpp"
#include "Widgets/Label.hpp"
#include "Widgets/Button.hpp"
#include "Widgets/StepCtrl.hpp"
#include "BitmapCache.hpp"
#include "slic3r/Utils/Http.hpp"
#include "libslic3r/Thread.hpp"
namespace Slic3r {
namespace GUI {
std::string show_error_message(int error_code);
wxDECLARE_EVENT(EVT_UPDATE_ERROR_MESSAGE, wxCommandEvent);
}
}
#endif