FIX: query hms crash while there is updating hms in other thread

jira: [STUDIO-9380]

Change-Id: I1b3d94008d4d03d3d41c10e1240fe832755ef9da
This commit is contained in:
xin.zhang 2024-12-26 15:08:07 +08:00 committed by lane.wei
parent 2b1f8887ef
commit 0d367638b5
2 changed files with 12 additions and 0 deletions

View File

@ -242,6 +242,7 @@ wxString HMSQuery::_query_hms_msg(std::string long_error_code, std::string lang_
return wxEmptyString;
}
std::unique_lock unique_lock(m_hms_mutex);
if (!m_hms_info_json.is_object())
{
BOOST_LOG_TRIVIAL(error) << "the hms info is not a valid json object";
@ -299,6 +300,8 @@ wxString HMSQuery::_query_hms_msg(std::string long_error_code, std::string lang_
bool HMSQuery::_query_error_msg(wxString &error_msg, std::string error_code, std::string lang_code)
{
std::unique_lock unique_lock(m_hms_mutex);
if (m_hms_info_json.contains("device_error")) {
if (m_hms_info_json["device_error"].contains(lang_code)) {
for (auto item = m_hms_info_json["device_error"][lang_code].begin(); item != m_hms_info_json["device_error"][lang_code].end(); item++) {
@ -338,6 +341,8 @@ bool HMSQuery::_query_error_msg(wxString &error_msg, std::string error_code, std
wxString HMSQuery::_query_error_url_action(std::string long_error_code, std::string dev_id, std::vector<int>& button_action)
{
std::unique_lock unique_lock(m_hms_mutex);
if (m_hms_action_json.contains("data")) {
for (auto item = m_hms_action_json["data"].begin(); item != m_hms_action_json["data"].end(); item++) {
if (item->contains("ecode") && boost::to_upper_copy((*item)["ecode"].get<std::string>()) == long_error_code) {
@ -394,6 +399,8 @@ int HMSQuery::check_hms_info(std::string dev_type)
dev_sn.push_back("094");
boost::thread check_thread = boost::thread([this, dev_type, dev_sn] {
std::unique_lock unique_lock(m_hms_mutex);
for (auto sn : dev_sn) {
download_hms_related(QUERY_HMS_INFO, &m_hms_info_json, sn);
download_hms_related(QUERY_HMS_ACTION, &m_hms_action_json, sn);

View File

@ -11,6 +11,7 @@
#include "slic3r/Utils/Http.hpp"
#include "libslic3r/Thread.hpp"
#include "nlohmann/json.hpp"
#include <mutex>
namespace Slic3r {
namespace GUI {
@ -20,9 +21,12 @@ namespace GUI {
#define QUERY_HMS_ACTION "query_hms_action"
class HMSQuery {
protected:
json m_hms_info_json;
json m_hms_action_json;
mutable std::mutex m_hms_mutex;
int download_hms_related(std::string hms_type, json *receive_json, std::string dev_type);
int load_from_local(std::string &version_info, std::string hms_type, json *load_json, std::string dev_type);
int save_to_local(std::string lang, std::string hms_type, std::string dev_type, json save_json);
@ -30,6 +34,7 @@ protected:
wxString _query_hms_msg(std::string long_error_code, std::string lang_code = std::string("en")) const;
bool _query_error_msg(wxString &error_msg, std::string long_error_code, std::string lang_code = std::string("en"));
wxString _query_error_url_action(std::string long_error_code, std::string dev_id, std::vector<int>& button_action);
public:
HMSQuery() {}
int check_hms_info(std::string dev_type = "00M");