From f879978e6226d691d2d92e4b4e767e63bcd91ed2 Mon Sep 17 00:00:00 2001 From: lnk Date: Fri, 8 May 2026 09:55:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=B0=E8=B4=A6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=92=8C=E6=B7=BB=E5=8A=A0=E5=8D=87=E7=BA=A7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFtid1056/client2.cpp | 2 + LFtid1056/client2.h | 1 + LFtid1056/cloudfront/code/cfg_parser.cpp | 45 +++++++++++++- LFtid1056/cloudfront/code/interface.cpp | 16 ++++- LFtid1056/cloudfront/code/interface.h | 2 +- LFtid1056/cloudfront/code/rocketmq.cpp | 77 +++++++++++++++++++++--- LFtid1056/dealMsg.cpp | 1 - 7 files changed, 128 insertions(+), 16 deletions(-) diff --git a/LFtid1056/client2.cpp b/LFtid1056/client2.cpp index 66485ab..c6c8407 100644 --- a/LFtid1056/client2.cpp +++ b/LFtid1056/client2.cpp @@ -1310,12 +1310,14 @@ bool ClientManager::set_cloud_status(const std::string& identifier, int status) connect_status_update(identifier, status); std::cout << "[Device " << identifier << "] ****Cloud status: " << ctx->cloudstatus << " updated to: " << status << std::endl; + DIY_INFOLOG_CODE(identifier,1,LOG_CODE_COMM,"设备登录状态更新为在线"); } else if (ctx->cloudstatus == 1 && status == 0) { //设备从在线转换至离线,通知前台状态发生翻转 connect_status_update(identifier, status); std::cout << "[Device " << identifier << "] ****Cloud status: " << ctx->cloudstatus << " updated to: " << status << std::endl; + DIY_INFOLOG_CODE(identifier,1,LOG_CODE_COMM,"设备登录状态更新为离线"); } // 修改云前置登录状态 diff --git a/LFtid1056/client2.h b/LFtid1056/client2.h index 8a8bf5c..c70d682 100644 --- a/LFtid1056/client2.h +++ b/LFtid1056/client2.h @@ -10,6 +10,7 @@ #include #include "dealMsg.h" #include "PQSMsg.h" + // Ϣṹ struct PointInfo { std::string point_id; // ID diff --git a/LFtid1056/cloudfront/code/cfg_parser.cpp b/LFtid1056/cloudfront/code/cfg_parser.cpp index a4d2f5e..25e959e 100644 --- a/LFtid1056/cloudfront/code/cfg_parser.cpp +++ b/LFtid1056/cloudfront/code/cfg_parser.cpp @@ -103,6 +103,10 @@ static std::map g_runninginfo_cache; static std::mutex g_versioninfo_cache_mtx; static std::map g_versioninfo_cache; +//装置升级 +std::map g_upgrade_file_map; +std::mutex g_upgrade_file_mutex; + //补招 std::list g_StatisticLackList; //日志补招结构类链表 std::mutex g_StatisticLackList_list_mutex; //补招队列数据锁 @@ -1641,12 +1645,12 @@ void parse_terminal_from_data(trigger_update_xml_t& trigger_update_xml, //work_terminal.dev_key = get_value("devKey"); //work_terminal.dev_series = get_value("series"); work_terminal.processNo = get_value("processNo"); - //work_terminal.addr_str = get_value("ip"); + work_terminal.addr_str = get_value("ip"); //work_terminal.port = get_value("port"); //work_terminal.timestamp = get_value("updateTime"); work_terminal.Righttime = get_value("Righttime"); - work_terminal.mac = get_value("mac"); + work_terminal.mac = get_value("ip"); for (tinyxml2::XMLElement* monitor = root->FirstChildElement("monitorData"); monitor; @@ -6594,8 +6598,43 @@ void on_device_response_minimal(int response_code, try { if(dev->isbusy == 2){ + + //获取升级文件 + std::string file_path; + { + std::lock_guard lk(g_upgrade_file_mutex); + auto it = g_upgrade_file_map.find(dev->terminal_id); + if (it != g_upgrade_file_map.end()) { + file_path = it->second; + } + } + + if (file_path.empty()) { + std::cerr << "upgrade file not found for devid=" << dev->terminal_id << std::endl; + send_reply_to_cloud(static_cast(ResponseCode::NOT_FOUND), id, device_state_int, dev->guid, dev->mac); + dev->guid.clear(); + dev->busytype = 0; + dev->isbusy = 0; + dev->busytimecount = 0; + return; + } + + std::cout << "upgrade file path=" << file_path << std::endl; + // 读取升级文件 - std::vector file_data = read_file_as_bytes("pqs_arm2.bin"); + std::vector file_data = read_file_as_bytes(file_path); + + if (file_data.empty()) { + std::cerr << "read file failed: " << file_path << std::endl; + send_reply_to_cloud(static_cast(ResponseCode::NOT_FOUND), id, device_state_int, dev->guid, dev->mac); + dev->guid.clear(); + dev->busytype = 0; + dev->isbusy = 0; + dev->busytimecount = 0; + return; + } + + std::cout << "file size=" << file_data.size() << std::endl; // 下发升级指令 ClientManager::instance().send_upgrade_action_to_device(id, file_data, 10240); diff --git a/LFtid1056/cloudfront/code/interface.cpp b/LFtid1056/cloudfront/code/interface.cpp index 0b62917..5b6681a 100644 --- a/LFtid1056/cloudfront/code/interface.cpp +++ b/LFtid1056/cloudfront/code/interface.cpp @@ -390,7 +390,7 @@ void Fileupload_test() // 下载文件:从远端路径下载到本地,并返回本地文件路径 // 入参:dev(设备)、remote_path(远端完整路径) // 返回:本地保存路径(失败返回空字符串) -std::string getfilefromweb(const std::string& devid, const std::string& remote_path) +std::string getfilefromweb(const std::string& devid, const std::string& remote_path,int type) { try { terminal_dev* dev = nullptr; @@ -428,7 +428,17 @@ std::string getfilefromweb(const std::string& devid, const std::string& remote_p //【3】构造本地保存路径 std::string mac = sanitize(normalize_mac(dev->addr_str)); - std::string save_dir = std::string(FRONT_PATH) + "/bin/upload/" + mac + "/"; + + std::string save_dir; + + if(type == 1) { + // 升级文件放在专门的upgrade目录下 + save_dir = std::string(FRONT_PATH) + "/bin/upgrade/" + mac + "/"; + } + else { + // 普通文件放在upload目录下 + save_dir = std::string(FRONT_PATH) + "/bin/upload/" + mac + "/"; + } if (!create_directory_recursive(save_dir)) { std::cerr << "[getfile][ERROR] create dir failed: " << save_dir << std::endl; @@ -824,7 +834,7 @@ int terminal_ledger_web(std::map& terminal_dev_map, dev.processNo = safe_str(item, "node"); dev.maxProcessNum = safe_str(item, "maxProcessNum"); - //dev.mac = safe_str(item, "mac");//添加mac + dev.mac = safe_str(item, "ip");//添加mac if (item.contains("monitorData") && item["monitorData"].is_array()) { for (auto& mon : item["monitorData"]) { diff --git a/LFtid1056/cloudfront/code/interface.h b/LFtid1056/cloudfront/code/interface.h index 0b4c364..5414b68 100644 --- a/LFtid1056/cloudfront/code/interface.h +++ b/LFtid1056/cloudfront/code/interface.h @@ -687,7 +687,7 @@ bool update_qvvr_file_download(const std::string& filename_with_mac, const std:: //上送文件列表接口 bool send_file_list(terminal_dev* dev, const std::vector &FileList); -std::string getfilefromweb(const std::string& devid, const std::string& remote_path); +std::string getfilefromweb(const std::string& devid, const std::string& remote_path,int type); //提取mac std::string normalize_mac(const std::string& mac); diff --git a/LFtid1056/cloudfront/code/rocketmq.cpp b/LFtid1056/cloudfront/code/rocketmq.cpp index 6501361..1da51b3 100644 --- a/LFtid1056/cloudfront/code/rocketmq.cpp +++ b/LFtid1056/cloudfront/code/rocketmq.cpp @@ -77,6 +77,9 @@ extern std::atomic INITFLAG; //测试用的终端数组 extern std::vector TESTARRAY; +extern std::map g_upgrade_file_map; +extern std::mutex g_upgrade_file_mutex; + ////////////////////////////////////////////////////////////////////////////////////////////////////////外部文件函数声明 extern void execute_bash(std::string fun,int process_num,std::string type); @@ -767,9 +770,10 @@ bool parseJsonMessageUD(const std::string& json_str, const std::string& output_d } json_data.processNo = std::to_string(procNo); - //int procNum = item.value("maxProcessNum", -1); - //json_data.maxProcessNum = std::to_string(procNum); + int procNum = item.value("maxProcessNum", -1); + json_data.maxProcessNum = std::to_string(procNum); + json_data.addr_str = item.value("ip", ""); json_data.mac = item.value("ip", ""); //json_data.port = item.value("port", ""); //json_data.timestamp = item.value("updateTime", ""); @@ -2595,7 +2599,7 @@ bool parsemsg(const std::string& devid, const std::string& guid, const nlohmann: return true; } - case 1115: { // 升级 + case 1120: { // 辅助升级 parsed.ok = true; std::cout << "[parsemsg] upgrade device, devid=" << devid @@ -2605,14 +2609,14 @@ bool parsemsg(const std::string& devid, const std::string& guid, const nlohmann: int ret = recordguid(devid, guid, static_cast(DeviceState::SET_PREUPGRADE), 2); if (-1 == ret) { send_reply_to_queue(guid, static_cast(ResponseCode::NOT_FOUND), - "未找到该装置,升级指令执行失败"); - DIY_ERRORLOG_CODE(devid, 1, LOG_CODE_CLOUD, "未找到该装置,升级指令执行失败"); + "未找到该装置,辅助升级指令执行失败"); + DIY_ERRORLOG_CODE(devid, 1, LOG_CODE_CLOUD, "未找到该装置,辅助升级指令执行失败"); return true; } else if (ret > 0) { send_reply_to_queue(guid, static_cast(ResponseCode::BUSY), - "该装置正忙,升级指令执行失败"); - DIY_WARNLOG_CODE(devid, 1, LOG_CODE_CLOUD, "该装置正忙,升级指令执行失败"); + "该装置正忙,辅助升级指令执行失败"); + DIY_WARNLOG_CODE(devid, 1, LOG_CODE_CLOUD, "该装置正忙,辅助升级指令执行失败"); return true; } else { @@ -2621,6 +2625,63 @@ bool parsemsg(const std::string& devid, const std::string& guid, const nlohmann: } } + //辅助升级就是文件替换 + //ClientManager::instance().set_preupgrade_action_to_device(devid, "");//尝试装置升级指令!第一步校验 + return true; + } + + case 1115: { + if (!msgObj.contains("Name") || !msgObj["Name"].is_string()) return false; + + parsed.ok = true; + + std::cout << "[parsemsg] update, devid=" << devid + << ", guid=" << guid << std::endl; + + //【1】recordguid + { + int ret = recordguid(devid, guid, static_cast(DeviceState::SET_PREUPGRADE), 2); + if (-1 == ret) { + send_reply_to_queue(guid, static_cast(ResponseCode::NOT_FOUND), + "未找到该装置,装置升级指令执行失败"); + DIY_ERRORLOG_CODE(devid, 1, LOG_CODE_CLOUD, "未找到该装置,装置升级指令执行失败"); + return true; + } + else if (ret > 0) { + send_reply_to_queue(guid, static_cast(ResponseCode::BUSY), + "该装置正忙,装置升级指令执行失败"); + DIY_WARNLOG_CODE(devid, 1, LOG_CODE_CLOUD, "该装置正忙,装置升级指令执行失败"); + return true; + } + else { + std::cout << "记录装置状态成功,准备执行装置升级" << std::endl; + DIY_INFOLOG_CODE(devid, 1, LOG_CODE_CLOUD, "记录装置状态成功,准备执行装置升级"); + } + } + + //【2】取参数(加 sanitize) + std::string remote_path = sanitize(msgObj["Name"].get()); // 云端路径 + + std::cout << "[parsemsg][1115] remote=" << remote_path + << std::endl; + + //【3】先下载到本地 + std::string local_path = getfilefromweb(devid, remote_path,1);//升级文件下载到本地的路径和普通文件不一样,放在专门的upgrade目录下 + + if (local_path.empty()) { + send_reply_to_queue(guid, static_cast(ResponseCode::BAD_REQUEST), + "文件上送失败,下载源文件失败: " + remote_path); + DIY_ERRORLOG_CODE(devid, 1, LOG_CODE_CLOUD, "装置升级失败,下载源文件失败"); + return true; + } + + { + std::lock_guard lk(g_upgrade_file_mutex); + g_upgrade_file_map[devid] = local_path; + } + + DIY_INFOLOG_CODE(devid, 1, LOG_CODE_CLOUD, "升级文件下载成功,准备执行装置升级"); + ClientManager::instance().set_preupgrade_action_to_device(devid, "");//尝试装置升级指令!第一步校验 return true; } @@ -2665,7 +2726,7 @@ bool parsemsg(const std::string& devid, const std::string& guid, const nlohmann: << ", dest=" << dest_file_path << std::endl; //【3】先下载到本地 - std::string local_path = getfilefromweb(devid, remote_path); + std::string local_path = getfilefromweb(devid, remote_path,0);//普通文件下载到本地的路径 if (local_path.empty()) { send_reply_to_queue(guid, static_cast(ResponseCode::BAD_REQUEST), diff --git a/LFtid1056/dealMsg.cpp b/LFtid1056/dealMsg.cpp index 3396b24..109cc80 100644 --- a/LFtid1056/dealMsg.cpp +++ b/LFtid1056/dealMsg.cpp @@ -15,7 +15,6 @@ #include "cloudfront/code/rocketmq.h" //lnk20250708 #include "cloudfront/code/log4.h" //lnk20250924 #include "client2.h" -#include "cloudfront/code/log4.h" using namespace std;