diff --git a/LFtid1056/PQSMsg.cpp b/LFtid1056/PQSMsg.cpp index 51e94ba..ebf0ce4 100644 --- a/LFtid1056/PQSMsg.cpp +++ b/LFtid1056/PQSMsg.cpp @@ -7,6 +7,8 @@ #include #include #include "client2.h" +#include + // 辅助转换函数 float IntToFloat(int num) { return static_cast(num) / 65536.0f; @@ -32,7 +34,62 @@ void ReversalBuff(uint8_t* buff, int start, int length) { } // 辅助函数:解析MAC地址并填充到缓冲区 -void GetMAC(const std::string& strMAC, std::vector& packet, size_t startIndex) { +//lnk20250808 +static inline int hex_nibble(char c) { + if (c >= '0' && c <= '9') return c - '0'; + c = static_cast(std::tolower(static_cast(c))); + if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); + return -1; +} + +// 安全版:不抛异常,返回 true/false +bool GetMAC(const std::string& strMAC, + std::vector& packet, + size_t startIndex, + std::string* err = nullptr) { + // 1) 规范化:去掉 '-', ':', ' ' + std::string s; + s.reserve(strMAC.size()); + for (char c : strMAC) { + if (c == '-' || c == ':' || c == ' ') continue; + s.push_back(c); + } + + // 2) 长度校验 + if (s.size() != 12) { + if (err) *err = "MAC长度必须为12个十六进制字符"; + return false; + } + + // 3) 十六进制校验 + 解析 + std::array mac{}; + for (int i = 0; i < 6; ++i) { + int hi = hex_nibble(s[2*i]); + int lo = hex_nibble(s[2*i + 1]); + if (hi < 0 || lo < 0) { + if (err) *err = "MAC含非法字符(必须是0-9或A-F)"; + return false; + } + mac[i] = static_cast((hi << 4) | lo); + } + + // 4) 确保缓冲区足够写入64字节(MAC 6字节 + 其后补零) + const size_t need = startIndex + 64; + if (packet.size() < need) packet.resize(need, 0); + + // 5) 写入MAC + for (int i = 0; i < 6; ++i) { + packet[startIndex + i] = mac[i]; + } + + // 6) 其余位置补零(若 resize 过,这里基本已是0,但保证一下) + for (size_t i = 6; i < 64; ++i) { + packet[startIndex + i] = 0; + } + + return true; +} +/*void GetMAC(const std::string& strMAC, std::vector& packet, size_t startIndex) { // 移除所有空格和短横线 std::string cleanedMAC = strMAC; cleanedMAC.erase(std::remove(cleanedMAC.begin(), cleanedMAC.end(), ' '), cleanedMAC.end()); @@ -70,7 +127,7 @@ void GetMAC(const std::string& strMAC, std::vector& packet, size_ catch (const std::exception& e) { throw std::invalid_argument("无效的MAC地址: " + std::string(e.what())); } -} +}*/ // CRC计算函数 unsigned char GetCrcSum(const std::vector& Check, int nOffset, int nLen) { @@ -292,7 +349,13 @@ std::vector generate_frontlogin_message(const std::string& strMac // [16-19] 数据体2 (保持为0) // 填充MAC地址 (从位置20开始,64字节) - GetMAC(strMac, packet, 20); + //GetMAC(strMac, packet, 20); + //lnk20250808 + std::string err; + if (!GetMAC(strMac, packet, 0, &err)) { + std::cerr << "[GetMAC] parse failed: " << err << "\n"; + // 做降级或返回 + } // 计算校验和 (从偏移8到137) unsigned char checksum = 0; diff --git a/LFtid1056/cloudfront/code/cfg_parser.cpp b/LFtid1056/cloudfront/code/cfg_parser.cpp index d3b9a56..3ef7ae6 100644 --- a/LFtid1056/cloudfront/code/cfg_parser.cpp +++ b/LFtid1056/cloudfront/code/cfg_parser.cpp @@ -2703,7 +2703,27 @@ bool extract_timestamp_from_cfg_file(const std::string& cfg_path, long long& sta return start_tm > 0 && trig_tm > 0; } +bool compare_qvvr_and_file(const std::string& cfg_path, const std::vector& data_list,qvvr_data& matched_data) { + long long start_tm = 0; + long long trig_tm = 0; + // 鎻愬彇 .cfg 鏂囦欢涓殑鏃堕棿鎴 + if (!extract_timestamp_from_cfg_file(cfg_path, start_tm, trig_tm)) { + std::cerr << "Failed to extract timestamp from cfg file: " << cfg_path << "\n"; + return false; + } + + // 閬嶅巻鎵鏈夋殏鎬佷簨浠讹紝鏌ユ壘涓 trig_tm 鍖归厤鐨 + for (const auto& data : data_list) { + long long diff = static_cast(data.QVVR_time) - trig_tm; + if (std::abs(diff) <= 1) { + matched_data = data; // 杩斿洖鍖归厤鍒扮殑浜嬩欢 + return true; + } + } + + return false; +} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////鏁版嵁杞崲鍑芥暟 // DataArrayItem to_json void to_json(nlohmann::json& j, const DataArrayItem& d) { @@ -2730,6 +2750,7 @@ void to_json(nlohmann::json& j, const MsgObj& m) { // FullObj to_json void to_json(nlohmann::json& j, const FullObj& f) { j = nlohmann::json{ + {"Id", f.mac}, {"Mid", f.Mid}, {"Did", f.Did}, {"Pri", f.Pri}, @@ -2738,6 +2759,7 @@ void to_json(nlohmann::json& j, const FullObj& f) { }; } std::string generate_json( + const std::string mac, int Mid, //闇搴旂瓟鐨勬姤鏂囪闃呰呮敹鍒板悗闇浠ユID搴旂瓟锛屾棤闇搴旂瓟濉叆鈥-1鈥 int Did, //璁惧鍞竴鏍囪瘑Ldid锛屽~鍏0浠h〃Ndid銆 int Pri, //鎶ユ枃澶勭悊鐨勪紭鍏堢骇 @@ -2749,6 +2771,7 @@ std::string generate_json( const std::vector& dataArray //鏁版嵁鏁扮粍銆 ) { FullObj fobj; + fobj.mac = mac; fobj.Mid = Mid; fobj.Did = Did; fobj.Pri = Pri; @@ -2772,7 +2795,7 @@ void upload_data_test(){ arr.push_back({2, 1691741340, 0, 1, "yyyy"}); std::string js = generate_json( - -1, 2, 1, 4866, 1, 0, 2, 1, arr + "123",-1, 1, 1, 4866, 1, 0, 2, 1, arr ); std::cout << js << std::endl; @@ -2794,8 +2817,9 @@ std::vector GenerateDeviceInfoFromLedger(const std::vector GenerateDeviceInfoFromLedger(const std::vector wavepaths; + std::string first_wavepath; + bool send_success = true; + + for (const auto& file_localpath : qfile.file_download) { + std::string file_cloudpath = "comtrade/" + file_localpath; + std::string wavepath_result; + + // 鍙戦佹湰鍦版枃浠跺埌杩滅锛岃繑鍥 wavepath + SOEFileWeb(const_cast(file_localpath), file_cloudpath, wavepath_result); + + // 濡傛灉澶辫触锛岄噸鍙戜竴娆 + if (wavepath_result.empty()) { + std::cerr << "[SOEFileWeb] Warning: first send failed for file: " << file_localpath << ", retrying...\n"; + SOEFileWeb(const_cast(file_localpath), file_cloudpath, wavepath_result); + } + + if (wavepath_result.empty()) { + send_success = false; + std::cerr << "[SOEFileWeb] Failed: wavepath empty for file: " << file_localpath << "\n"; + break; + } + + if (wavepaths.empty()) { + first_wavepath = wavepath_result; + } else if (wavepath_result != first_wavepath) { + send_success = false; + std::cerr << "[SOEFileWeb] Mismatch wavepath: " << wavepath_result + << " vs " << first_wavepath << "\n"; + break; + } + + wavepaths.push_back(wavepath_result); + } + + // 妫鏌ユ暟閲忔槸鍚︿竴鑷 + if (!send_success || wavepaths.size() != qfile.file_download.size()) { + std::cerr << "[SOEFileWeb] Failed to send all qvvr files. " + << "Sent: " << wavepaths.size() + << ", Expected: " << qfile.file_download.size() << "\n"; + return false; + } + + out_wavepath = first_wavepath; // 杩斿洖缁熶竴鐨 wavepath + + std::cout << "[SOEFileWeb] Success: all files sent for qfile. Wavepath = " + << first_wavepath << "\n"; + return true; +} + +//鏂囦欢涓嬭浇缁撴潫鎺ュ彛 bool update_qvvr_file_download(const std::string& filename_with_mac, const std::string& terminal_id) { - // 鍘婚櫎 mac 璺緞鍓嶇紑 - size_t pos = filename_with_mac.find_last_of("/\\"); - std::string filename = (pos != std::string::npos) ? filename_with_mac.substr(pos + 1) : filename_with_mac; + + //鍙拌处鍔犻攣 + std::lock_guard lock(ledgermtx); + + // 鍘婚櫎 mac 璺緞鍓嶇紑锛屼粎淇濈暀鏂囦欢鍚 + std::string filename = extract_filename1(filename_with_mac); // 鎻愬彇閫昏緫搴忓彿锛堝 PQM1 鈫 1锛 size_t under_pos = filename.find('_'); if (under_pos == std::string::npos) return false; - std::string type_part = filename.substr(0, under_pos); //PQMonitor_PQM1 + std::string type_part = filename.substr(0, under_pos); // PQMonitor_PQM1 size_t num_start = type_part.find_last_not_of("0123456789"); if (num_start == std::string::npos || num_start + 1 >= type_part.size()) return false; std::string seq_str = type_part.substr(num_start + 1); - ushort logical_seq = static_cast(std::stoi(seq_str)); + ushort logical_seq = static_cast(std::stoi(seq_str)); // 閫昏緫搴忓彿 + //鎵剧粓绔 for (auto& dev : terminal_devlist) { if (dev.terminal_id != terminal_id) continue; + //鎵剧洃娴嬬偣 for (auto& monitor : dev.line) { try { + // 灏嗙洃娴嬬偣鍙拌处涓殑 logical_device_seq 杞崲涓烘暟瀛楄繘琛屽尮閰 ushort monitor_seq = static_cast(std::stoi(monitor.logical_device_seq)); if (monitor_seq != logical_seq) continue; } catch (...) { - continue; + continue; // logical_device_seq 闈炴硶锛岃烦杩 } // 鍖归厤鐩戞祴鐐逛笅 qvvrfile 涓殑 file_name - for (auto& qfile : monitor.qvvrevent.qvvrfile) { + for (size_t i = 0; i < monitor.qvvrevent.qvvrfile.size(); ++i) { + auto& qfile = monitor.qvvrevent.qvvrfile[i]; + // file_name 涓槸鏂囦欢鍚嶏紝闇涓庢彁鍙栫殑 filename 姣旇緝 auto it = std::find(qfile.file_name.begin(), qfile.file_name.end(), filename); + + //鎵惧埌鍖归厤鏂囦欢鍚 if (it != qfile.file_name.end()) { - // 娣诲姞鍒 file_download锛堝幓閲嶏級 - if (std::find(qfile.file_download.begin(), qfile.file_download.end(), filename) == qfile.file_download.end()) { - qfile.file_download.push_back(filename); + // 娣诲姞鍒 file_download锛堣褰曞畬鏁磋矾寰勶紝閬垮厤閲嶅锛 + if (std::find(qfile.file_download.begin(), qfile.file_download.end(), filename_with_mac) == qfile.file_download.end()) { + qfile.file_download.push_back(filename_with_mac); } - qfile.file_time_count = 0; - qfile.file_start = true; //寮濮嬩笅杞芥枃浠 + qfile.file_time_count = 0; // 浠讳竴褰曟尝鏂囦欢涓嬭浇鍚庯紝璁℃椂褰掗浂 - // 妫鏌 file_download 鏄惁涓 file_name 瀹屽叏涓鑷达紙闆嗗悎鐩稿悓锛//姣忔涓嬭浇閮戒細瀵规瘮 + // file_download 涓槸瀹屾暣璺緞锛岄渶鎻愬彇鏂囦欢鍚嶅悗涓 file_name 鍋氶泦鍚堟瘮杈 std::set s_name(qfile.file_name.begin(), qfile.file_name.end()); - std::set s_down(qfile.file_download.begin(), qfile.file_download.end()); + std::set s_down; + for (const auto& path : qfile.file_download) { + s_down.insert(extract_filename1(path)); // 鎻愬彇姣忎釜璺緞涓殑鏂囦欢鍚 + } + + // 妫鏌 file_download 鏄惁涓 file_name 瀹屽叏涓鑷达紙闆嗗悎鐩稿悓锛 if (s_name == s_down) { - qfile.is_download = true; //鍏ㄩ儴涓嬭浇瀹屾垚 + qfile.is_download = true; // 鍏ㄩ儴涓嬭浇瀹屾垚 // 鎵惧埌鍏朵腑鐨 .cfg 鏂囦欢杩涜鍖归厤 - for (const auto& f : qfile.file_download) { - if (f.size() >= 4 && f.substr(f.size() - 4) == ".cfg") { - if (compare_qvvr_and_file(f)) {//鎻愬彇鏂囦欢鏃舵爣鍜岀洃娴嬬偣浜嬩欢鐨勬椂鏍囧尮閰 - qfile.is_pair = true; - //鍙戦佹墍鏈夋枃浠 + for (const auto& fpath : qfile.file_download) { + std::string fname = extract_filename1(fpath); + if (fname.size() >= 4 && fname.substr(fname.size() - 4) == ".cfg") { + // 鎻愬彇鏂囦欢鏃舵爣鍜岀洃娴嬬偣浜嬩欢鐨勬椂鏍囧尮閰 + qvvr_data matched; + if (compare_qvvr_and_file(fpath, monitor.qvvrevent.qvvrdata,matched)) { + qfile.is_pair = true; // 鏂囦欢涓庝簨浠跺尮閰嶆垚鍔 - //鍙戦佹殏鎬佷簨浠 - + // 鍙戦佹墍鏈夋枃浠讹紙宸蹭笅杞藉畬鎴愶級 + std::string wavepath; + if (SendAllQvvrFiles(qfile, wavepath)) { + //鏂囦欢鍙戦佹垚鍔熷悗鏇存柊浜嬩欢 + transfer_json_qvvr_data(terminal_id, + logical_seq, + matched.QVVR_Amg, + matched.QVVR_PerTime, + matched.QVVR_time, + matched.QVVR_type, + matched.phase, + wavepath); + + // 鍒犻櫎涓婁紶鎴愬姛鐨勬枃浠 + for (const auto& uploaded_file : qfile.file_download) { + if (std::remove(uploaded_file.c_str()) != 0) { + std::cerr << "[Cleanup] Failed to delete file: " << uploaded_file << "\n"; + } else { + std::cout << "[Cleanup] Deleted uploaded file: " << uploaded_file << "\n"; + } + } + + // 娓呴櫎宸插彂閫佺殑鏆傛佹枃浠 + monitor.qvvrevent.qvvrfile.erase(monitor.qvvrevent.qvvrfile.begin() + i); + + //娓呴櫎鏆傛佷簨浠 + auto it = std::find_if( + monitor.qvvrevent.qvvrdata.begin(), + monitor.qvvrevent.qvvrdata.end(), + [&](const qvvr_data& d) { + return d.QVVR_time == matched.QVVR_time; + }); + + if (it != monitor.qvvrevent.qvvrdata.end()) { + monitor.qvvrevent.qvvrdata.erase(it); + } + } } break; // 鍙鐞嗙涓涓 cfg 鏂囦欢 } } } + else{ + std::cout << "qvvr file still imcomplete!!!" << std::endl; + } - return true; - } + return true; // 褰撳墠鏂囦欢澶勭悊鎴愬姛 + } } + + std::cout << "file name doesnt match any file in this monitor!!!" << std::endl; + } } - - return false; + return false; // 鏈尮閰嶅埌缁堢ID鎴栭昏緫搴忓彿瀵瑰簲鐨勭洃娴嬬偣 } + +////////////////////////////////////////////////////////////////////////////////////////鎻愬彇mac +std::string normalize_mac(const std::string& mac) { + std::string result = mac; + result.erase(std::remove(result.begin(), result.end(), '-'), result.end()); + return result; +} \ No newline at end of file diff --git a/LFtid1056/cloudfront/code/interface.cpp b/LFtid1056/cloudfront/code/interface.cpp index edac700..f0a8117 100644 --- a/LFtid1056/cloudfront/code/interface.cpp +++ b/LFtid1056/cloudfront/code/interface.cpp @@ -609,27 +609,27 @@ int terminal_ledger_web(std::map& terminal_dev_map, dev.terminal_id = safe_str(item, "id"); dev.addr_str = safe_str(item, "ip"); dev.terminal_name = safe_str(item, "name"); - dev.org_name = safe_str(item, "org_name"); - dev.maint_name = safe_str(item, "maint_name"); - dev.station_name = safe_str(item, "stationName"); - dev.tmnl_factory = safe_str(item, "manufacturer"); - dev.tmnl_status = safe_str(item, "status"); + //dev.org_name = safe_str(item, "org_name"); + //dev.maint_name = safe_str(item, "maint_name"); + //dev.station_name = safe_str(item, "stationName"); + //dev.tmnl_factory = safe_str(item, "manufacturer"); + //dev.tmnl_status = safe_str(item, "status"); dev.dev_type = safe_str(item, "devType"); - dev.dev_key = safe_str(item, "devKey"); - dev.dev_series = safe_str(item, "series"); - dev.port = safe_str(item, "port"); - dev.timestamp = safe_str(item, "updateTime"); - dev.processNo = safe_str(item, "processNo"); - dev.maxProcessNum = safe_str(item, "maxProcessNum"); + //dev.dev_key = safe_str(item, "devKey"); + //dev.dev_series = safe_str(item, "series"); + //dev.port = safe_str(item, "port"); + //dev.timestamp = safe_str(item, "updateTime"); + dev.processNo = safe_str(item, "node"); + //dev.maxProcessNum = safe_str(item, "maxProcessNum"); - dev.mac = safe_str(item, "mac");//娣诲姞mac + //dev.mac = safe_str(item, "mac");//娣诲姞mac if (item.contains("monitorData") && item["monitorData"].is_array()) { for (auto& mon : item["monitorData"]) { if (dev.line.size() >= 10) break; ledger_monitor m; m.monitor_id = safe_str(mon, "id"); - m.terminal_id = safe_str(mon, "terminal_id"); + m.terminal_id = safe_str(mon, "deviceId"); m.monitor_name = safe_str(mon, "name"); m.logical_device_seq = safe_str(mon, "lineNo"); m.voltage_level = safe_str(mon, "voltageLevel"); @@ -637,10 +637,10 @@ int terminal_ledger_web(std::map& terminal_dev_map, m.timestamp = safe_str(mon, "updateTime"); m.status = safe_str(mon, "status"); - m.CT1 = mon.value("CT1", 0.0); - m.CT2 = mon.value("CT2", 0.0); - m.PT1 = mon.value("PT1", 0.0); - m.PT2 = mon.value("PT2", 0.0); + m.CT1 = mon.value("ct1", 0.0); + m.CT2 = mon.value("ct2", 0.0); + m.PT1 = mon.value("pt1", 0.0); + m.PT2 = mon.value("pt2", 0.0); dev.line.push_back(m); } @@ -736,9 +736,9 @@ int parse_device_cfg_web() if (IED_COUNT < count_cfg) { std::cout << "!!!!!!!!!!single process can not add any ledger unless reboot!!!!!!!" << std::endl; - DIY_WARNLOG("process","銆怶ARN銆戝墠缃殑%d鍙疯繘绋嬭幏鍙栧埌鐨勫彴璐︾殑鏁伴噺澶т簬閰嶇疆鏂囦欢涓粰鍗曚釜杩涚▼閰嶇疆鐨勫彴璐︽暟閲:%d,杩欎釜杩涚▼灏嗘寜鐓ц幏鍙栧埌鐨勫彴璐︾殑鏁伴噺鏉ュ垱寤哄彴璐︾┖闂达紝杩欎釜杩涚▼涓嶈兘鐩存帴閫氳繃鍙拌处娣诲姞鏉ユ柊澧炲彴璐︼紝鍙兘閫氳繃閲嶅惎杩涚▼鎴栬呭厛鍒犻櫎宸叉湁鍙拌处鍐嶆坊鍔犲彴璐︾殑鏂瑰紡鏉ユ坊鍔犳柊鍙拌处", g_front_seg_index, IED_COUNT); + //DIY_WARNLOG("process","銆怶ARN銆戝墠缃殑%d鍙疯繘绋嬭幏鍙栧埌鐨勫彴璐︾殑鏁伴噺澶т簬閰嶇疆鏂囦欢涓粰鍗曚釜杩涚▼閰嶇疆鐨勫彴璐︽暟閲:%d,杩欎釜杩涚▼灏嗘寜鐓ц幏鍙栧埌鐨勫彴璐︾殑鏁伴噺鏉ュ垱寤哄彴璐︾┖闂达紝杩欎釜杩涚▼涓嶈兘鐩存帴閫氳繃鍙拌处娣诲姞鏉ユ柊澧炲彴璐︼紝鍙兘閫氳繃閲嶅惎杩涚▼鎴栬呭厛鍒犻櫎宸叉湁鍙拌处鍐嶆坊鍔犲彴璐︾殑鏂瑰紡鏉ユ坊鍔犳柊鍙拌处", g_front_seg_index, IED_COUNT); } else { - DIY_INFOLOG("process","銆怤ORMAL銆戝墠缃殑%d鍙疯繘绋嬫牴鎹厤缃枃浠朵腑缁欏崟涓繘绋嬮厤缃殑鍙拌处鏁伴噺:%d鏉ュ垱寤哄彴璐︾┖闂", g_front_seg_index, IED_COUNT); + //DIY_INFOLOG("process","銆怤ORMAL銆戝墠缃殑%d鍙疯繘绋嬫牴鎹厤缃枃浠朵腑缁欏崟涓繘绋嬮厤缃殑鍙拌处鏁伴噺:%d鏉ュ垱寤哄彴璐︾┖闂", g_front_seg_index, IED_COUNT); } ///////////////////////////////////////////////////////////////////////////////鐢ㄤ緥杩欓噷灏嗗眬閮ㄧ殑map鎷疯礉鍒板叏灞map锛屽悗缁牴鎹崗璁彴璐︿慨鏀 @@ -1149,32 +1149,24 @@ static void scanAndResendOfflineFiles(const std::string& dirPath) } } -int transfer_json_qvvr_data(unsigned int func_type, int monitor_id, - double mag, double dur, long long start_tm, long long end_tm, int dis_kind, - const std::string& uuid_cfg, const std::string& uuid_dat, - const std::string& mp_id, const std::string& Qvvr_rptname, const std::string& devtype) { +int transfer_json_qvvr_data(const std::string& dev_id, ushort monitor_id, + double mag, double dur, long long start_tm, int dis_kind,int phase, + const std::string& wavepath) { // 鐩戞祴鐐规棩蹇楃殑 key, lnk20250526 - std::string full_key_m_c = "monitor." + mp_id + ".COM"; - std::string full_key_m_d = "monitor." + mp_id + ".DATA"; + std::string full_key_m_c = "monitor." + dev_id + "." + std::to_string(monitor_id) + ".COM"; + std::string full_key_m_d = "monitor." + dev_id + "." + std::to_string(monitor_id) + ".DATA"; // 鐩戞祴鐐规棩蹇楃殑 key, lnk20250526 - // 鑾峰彇瑁呯疆绫诲瀷鐨勬槧灏勯厤缃 - XmlConfig c_xmlcfg; - if (xmlinfo_list.count(devtype)) { - c_xmlcfg = xmlinfo_list[devtype]->xmlcfg; - } else { - c_xmlcfg = xmlcfg; - } - - if (mp_id.empty()) { - std::cout << "mp_id is null" << std::endl; + if (dev_id.empty()) { + std::cout << "dev_id is null" << std::endl; return 0; } // 鏋勯 JSON 瀵硅薄 json root; - root["monitorId"] = mp_id; + root["devId"] = dev_id; + root["CpuNo"] = monitor_id; root["amplitude"] = mag; root["duration"] = dur; root["eventType"] = dis_kind; @@ -1189,21 +1181,9 @@ int transfer_json_qvvr_data(unsigned int func_type, int monitor_id, std::string start_time_str = start_time_stream.str(); root["startTime"] = start_time_str; - root["wavePath"] = uuid_dat; //鎺ュ彛鎻愪緵浜嗕袱涓枃浠跺悕鍏ュ弬锛屽疄闄呬笂鍚嶅瓧涓鏍峰彧鐢ㄤ竴涓紝鍙紭鍖 + root["wavePath"] = wavepath; + root["phase"] = phase; - if (c_xmlcfg.WavePhasicFlag == "1") { //鏄犲皠閰嶇疆鍒嗙浉 - if (Qvvr_rptname.find(c_xmlcfg.WavePhasicA) != std::string::npos) { - root["phase"] = "A"; - } else if (Qvvr_rptname.find(c_xmlcfg.WavePhasicB) != std::string::npos) { - root["phase"] = "B"; - } else if (Qvvr_rptname.find(c_xmlcfg.WavePhasicC) != std::string::npos) { - root["phase"] = "C"; - } else { - root["phase"] = "unknow"; - } - } else { - root["phase"] = "unknow"; //涓嶅垎鐩 - } std::string json_string = root.dump(4); std::cout << json_string << std::endl; @@ -1212,28 +1192,28 @@ int transfer_json_qvvr_data(unsigned int func_type, int monitor_id, std::string response; SendJsonAPI_web(WEB_EVENT, "", json_string, response); - // ================ 鎻掑叆鏂板姛鑳 ========================= + // ================ 鏆傛侀噸鍙戝姛鑳 ========================= if (!response.empty()) { try { json j_r = json::parse(response); // 鏈夋晥鍝嶅簲锛岀暐杩 } catch (...) { // 鍝嶅簲寮傚父锛屼繚瀛 json - DIY_ERRORLOG(full_key_m_d.c_str(), "銆怑RROR銆戞殏鎬佹帴鍙e搷搴斿紓甯,鏃犳硶涓婇佺洃娴嬬偣%s鐨勬殏鎬佷簨浠", mp_id.c_str()); + DIY_ERRORLOG(full_key_m_d.c_str(), "銆怑RROR銆戞殏鎬佹帴鍙e搷搴斿紓甯,鏃犳硶涓婇佽缃%s鐩戞祴鐐%s鐨勬殏鎬佷簨浠",dev_id, monitor_id); std::cout << "qvvr send fail ,store in local" << std::endl; std::string qvvrDir = FRONT_PATH + "/dat/qvvr/"; - std::string fileName = qvvrDir + mp_id + "-" + FormatTimeForFilename(start_time_str) + "-" + std::to_string(dis_kind) + ".txt"; + std::string fileName = qvvrDir + dev_id + "-" + std::to_string(monitor_id) + "-" + FormatTimeForFilename(start_time_str) + "-" + std::to_string(dis_kind) + ".txt"; writeJsonToFile(fileName, json_string); checkAndRemoveOldestIfNeeded(qvvrDir, 10LL * 1024 * 1024); } } else { // 鏃犲搷搴旓紝淇濆瓨 json - DIY_ERRORLOG(full_key_m_d.c_str(), "銆怑RROR銆戞殏鎬佹帴鍙f棤鍝嶅簲,鏃犳硶涓婇佺洃娴嬬偣%s鐨勬殏鎬佷簨浠", mp_id.c_str()); + DIY_ERRORLOG(full_key_m_d.c_str(), "銆怑RROR銆戞殏鎬佹帴鍙f棤鍝嶅簲,鏃犳硶涓婇佽缃%s鐩戞祴鐐%s鐨勬殏鎬佷簨浠",dev_id, monitor_id); std::cout << "qvvr send fail ,store in local" << std::endl; std::string qvvrDir = FRONT_PATH + "/dat/qvvr/"; - std::string fileName = qvvrDir + mp_id + "-" + FormatTimeForFilename(start_time_str) + "-" + std::to_string(dis_kind) + ".txt"; + std::string fileName = qvvrDir + dev_id + "-" + std::to_string(monitor_id) + "-" + FormatTimeForFilename(start_time_str) + "-" + std::to_string(dis_kind) + ".txt"; writeJsonToFile(fileName, json_string); checkAndRemoveOldestIfNeeded(qvvrDir, 10LL * 1024 * 1024); return 1; @@ -1254,13 +1234,9 @@ int transfer_json_qvvr_data(unsigned int func_type, int monitor_id, void qvvr_test() { - char uuid_cfg[] = {"/comtrade/"}; - char uuid_dat[] = {"/comtrade/"}; - char mp_id[] = {"qvvrtest123"}; - char Qvvr_rptname[] = {"unknow"}; - char devtype[] = {"01"}; - - transfer_json_qvvr_data(1, 123456789, 220, 180, 1730894400.123, 1730894580, 1210001,uuid_cfg,uuid_dat,mp_id,Qvvr_rptname,devtype); + transfer_json_qvvr_data("qvvrtest123", 6, + 10.98, 1234, 1754566628692, 1,1, + "testwavepath"); } /////////////////////////////////////////////////////////////////////////////////////////////////////////閫氱敤鎺ュ彛鍝嶅簲 diff --git a/LFtid1056/cloudfront/code/interface.h b/LFtid1056/cloudfront/code/interface.h index e172148..3f4008a 100644 --- a/LFtid1056/cloudfront/code/interface.h +++ b/LFtid1056/cloudfront/code/interface.h @@ -52,28 +52,31 @@ public: //褰曟尝鏂囦欢鍜屾殏鎬佷簨浠 class qvvr_data { - int used_status; //鏄惁鍗犵敤 - int QVVR_type; //鏆傛佺被鍨 - uint64_t QVVR_time; //鏆傛佸紑濮嬫椂闂 unsigned longlong - double QVVR_PerTime; //鏆傛佹寔缁椂闂 - double QVVR_Amg; //鏆傛佸箙鍊 - int phase; //鐩稿埆(浠呯灛鎬佷笂閫)0-A 1-B 2-C 3-AB 4-BC 5-CA 鍏朵粬-ABC/寮傚父 + public: + bool used_status; //鏄惁鍗犵敤 + int QVVR_type; //鏆傛佺被鍨 + uint64_t QVVR_time; //鏆傛佸紑濮嬫椂闂 unsigned longlong + double QVVR_PerTime; //鏆傛佹寔缁椂闂 + double QVVR_Amg; //鏆傛佸箙鍊 + int phase; //鐩稿埆(浠呯灛鎬佷笂閫)0-A 1-B 2-C 3-AB 4-BC 5-CA 鍏朵粬-ABC/寮傚父 }; class qvvr_file { - bool file_start; - int file_time_count; //缁勫唴鏂囦欢涓嬭浇鏃堕棿璁℃暟锛堢涓涓枃浠朵笅杞藉悗鍗佸垎閽熷唴濡傛灉鍏朵粬鏂囦欢娌′笅杞藉叏鎴栬呬笅杞藉叏浜嗘病鍖归厤浜嬩欢鍒欏皢宸蹭笅杞界殑鏂囦欢閮界Щ鍒板浠藉尯comtrade_bak锛 - bool is_download; //鏂囦欢鏄惁涓嬭浇瀹屽叏锛屾渶鍚庝竴涓枃浠朵笅杞芥垚鍔熷悗瀵规瘮鎴愬姛鍒欐洿鏂拌繖涓爣蹇 - bool is_pair; //鏂囦欢鏄惁鍜屼簨浠跺尮閰嶏紝浠巆omtrade/mac/璺緞涓嬪彇file_download涓殑cfg鏂囦欢鎻愬彇鏃堕棿鍜屾寔缁椂闂存潵鍖归厤锛屽尮閰嶅悗鎺ュ彛鍙戦佽繖缁刦ile_download鍏ㄩ儴鏂囦欢锛屽彂閫佹垚鍔熷悗鍒犻櫎杩欑粍鏂囦欢锛岀劧鍚庢洿鏂颁簨浠朵腑鐨勬枃浠跺垪琛 - std::list file_name; //鏂囦欢鍒楄〃锛堟枃浠跺垪琛ㄤ笂閫佸悗灏辫褰曪級 - std::list file_download; //鏂囦欢宸蹭笅杞藉垪琛(姣忔鍒楄〃涓婇佷細鏈夊涓枃浠讹紝澶氫釜鏂囦欢閮戒笅杞藉畬鍏ㄥ垯寮濮嬪尮閰嶏紝姣忔鏇存柊閮藉幓閲嶅苟瀵规瘮file_name锛 + public: + bool used_status; + int file_time_count; //缁勫唴鏂囦欢涓嬭浇鏃堕棿璁℃暟锛堢涓涓枃浠朵笅杞藉悗鍗佸垎閽熷唴濡傛灉鍏朵粬鏂囦欢娌′笅杞藉叏鎴栬呬笅杞藉叏浜嗘病鍖归厤浜嬩欢鍒欏皢宸蹭笅杞界殑鏂囦欢閮界Щ鍒板浠藉尯comtrade_bak锛 + bool is_download; //鏂囦欢鏄惁涓嬭浇瀹屽叏锛屾渶鍚庝竴涓枃浠朵笅杞芥垚鍔熷悗瀵规瘮鎴愬姛鍒欐洿鏂拌繖涓爣蹇 + bool is_pair; //鏂囦欢鏄惁鍜屼簨浠跺尮閰嶏紝浠巆omtrade/mac/璺緞涓嬪彇file_download涓殑cfg鏂囦欢鎻愬彇鏃堕棿鍜屾寔缁椂闂存潵鍖归厤锛屽尮閰嶅悗鎺ュ彛鍙戦佽繖缁刦ile_download鍏ㄩ儴鏂囦欢锛屽彂閫佹垚鍔熷悗鍒犻櫎杩欑粍鏂囦欢锛岀劧鍚庢洿鏂颁簨浠朵腑鐨勬枃浠跺垪琛 + std::list file_name; //鏂囦欢鍒楄〃锛堟枃浠跺垪琛ㄤ笂閫佸悗灏辫褰曪級 + std::list file_download; //鏂囦欢宸蹭笅杞藉垪琛(姣忔鍒楄〃涓婇佷細鏈夊涓枃浠讹紝澶氫釜鏂囦欢閮戒笅杞藉畬鍏ㄥ垯寮濮嬪尮閰嶏紝姣忔鏇存柊閮藉幓閲嶅苟瀵规瘮file_name锛 }; class qvvr_event { - std::vector qvvrdata; //鏆傛佷簨浠跺垪琛 - std::vector qvvrfile; //鏆傛佹枃浠剁粍鍒楄〃 + public: + std::vector qvvrdata; //鏆傛佷簨浠跺垪琛 + std::vector qvvrfile; //鏆傛佹枃浠剁粍鍒楄〃 }; //鐩戞祴鐐瑰彴璐 @@ -426,6 +429,7 @@ struct MsgObj { // 鏁翠綋 struct FullObj { + std::string mac; int Mid; int Did; int Pri; @@ -443,6 +447,7 @@ void to_json(nlohmann::json& j, const FullObj& f); std::vector GenerateDeviceInfoFromLedger(const std::vector& terminal_devlist);//鎺ュ彛璇诲彇鍙拌处鍚庯紝鍐嶈皟鐢ㄨ繖涓皢鍙拌处鎷疯礉杩囨潵 std::string generate_json( //鏋勯犺缃富鍔ㄤ笂閫佹暟鎹殑鎶ユ枃 + const std::string mac, int Mid, //闇搴旂瓟鐨勬姤鏂囪闃呰呮敹鍒板悗闇浠ユID搴旂瓟锛屾棤闇搴旂瓟濉叆鈥-1鈥 int Did, //璁惧鍞竴鏍囪瘑Ldid锛屽~鍏0浠h〃Ndid銆 int Pri, //鎶ユ枃澶勭悊鐨勪紭鍏堢骇 @@ -454,14 +459,21 @@ std::string generate_json( //鏋勯犺缃富鍔ㄤ笂閫佹暟鎹殑鎶ユ枃 const std::vector& dataArray //鏁版嵁鏁扮粍銆 ); -int transfer_json_qvvr_data(unsigned int func_type, int monitor_id, //鏆傛佷簨浠剁殑鎺ュ彛 - double mag, double dur, long long start_tm, long long end_tm, int dis_kind, - const std::string& uuid_cfg, const std::string& uuid_dat, - const std::string& mp_id, const std::string& Qvvr_rptname, const std::string& devtype); +//鏆傛佷簨浠舵帴鍙 +int transfer_json_qvvr_data(const std::string& dev_id, ushort monitor_id, + double mag, double dur, long long start_tm, int dis_kind,int phase, + const std::string& wavepath); +//褰曟尝鏂囦欢涓婁紶鎺ュ彛 +void SOEFileWeb(std::string& localpath,std::string& cloudpath, std::string& wavepath); //褰曟尝鏂囦欢鐩綍鎺ュ彛 +bool assign_qvvr_file_list(const std::string& id, ushort nCpuNo, const std::vector& file_list_raw); //褰曟尝鏂囦欢涓嬭浇瀹屾垚閫氱煡鎺ュ彛 +bool update_qvvr_file_download(const std::string& filename_with_mac, const std::string& terminal_id); + +//鎻愬彇mac +std::string normalize_mac(const std::string& mac); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/LFtid1056/cloudfront/code/log4.cpp b/LFtid1056/cloudfront/code/log4.cpp index 0d4ed55..544e14d 100644 --- a/LFtid1056/cloudfront/code/log4.cpp +++ b/LFtid1056/cloudfront/code/log4.cpp @@ -334,8 +334,8 @@ void init_loggers_bydevid(const std::string& dev_id) const ledger_monitor& monitor = term.line[j]; if (!monitor.monitor_id.empty()) { std::ostringstream mon_key_c, mon_key_d, mon_path, mon_name; - mon_key_c << "monitor." << monitor.monitor_id << ".COM"; - mon_key_d << "monitor." << monitor.monitor_id << ".DATA"; + mon_key_c << "monitor." << term.terminal_id << "." << monitor.logical_device_seq << ".COM"; + mon_key_d << "monitor." << term.terminal_id << "." << monitor.logical_device_seq << ".DATA"; mon_path << device_dir << "/monitor" << j; mon_name << monitor.monitor_id; @@ -355,7 +355,7 @@ void init_loggers_bydevid(const std::string& dev_id) logger_map[mon_key_c.str()] = TypedLogger(mon_logger_c, LOGTYPE_COM); logger_map[mon_key_d.str()] = TypedLogger(mon_logger_d, LOGTYPE_DATA); - DIY_WARNLOG(mon_key_d.str().c_str(), "銆怶ARN銆戠洃娴嬬偣:%s - id:%s鐩戞祴鐐圭骇鏃ュ織鍒濆鍖栧畬姣", monitor.monitor_name.c_str(), monitor.monitor_id.c_str()); + DIY_WARNLOG(mon_key_d.str().c_str(), "銆怶ARN銆戠洃娴嬬偣:%s - id:%s鐩戞祴鐐圭骇鏃ュ織鍒濆鍖栧畬姣", monitor.monitor_name.c_str(), monitor.logical_device_seq.c_str()); } } } @@ -406,8 +406,8 @@ void init_loggers() if (!monitor.monitor_id.empty()) { std::ostringstream mon_key_c, mon_key_d, mon_path, mon_name; - mon_key_c << "monitor." << monitor.monitor_id << ".COM"; - mon_key_d << "monitor." << monitor.monitor_id << ".DATA"; + mon_key_c << "monitor." << term.terminal_id << "." << monitor.logical_device_seq << ".COM"; + mon_key_d << "monitor." << term.terminal_id << "." << monitor.logical_device_seq << ".DATA"; mon_path << device_dir << "/monitor" << i; // 鐢╩onitor+搴忓彿浣滀负鐩綍 mon_name << monitor.monitor_id; @@ -425,7 +425,7 @@ void init_loggers() logger_map[mon_key_d.str()] = TypedLogger(mon_logger_d, LOGTYPE_DATA); DIY_WARNLOG(mon_key_d.str().c_str(), "銆怶ARN銆戠洃娴嬬偣:%s - id:%s鐩戞祴鐐圭骇鏃ュ織鍒濆鍖栧畬姣", - monitor.monitor_name.c_str(), monitor.monitor_id.c_str()); + monitor.monitor_name.c_str(), monitor.logical_device_seq.c_str()); } } } diff --git a/LFtid1056/cloudfront/code/main.cpp b/LFtid1056/cloudfront/code/main.cpp index 074555a..c5f9599 100644 --- a/LFtid1056/cloudfront/code/main.cpp +++ b/LFtid1056/cloudfront/code/main.cpp @@ -215,7 +215,7 @@ std::string get_parent_directory() { init_loggers(); //璇诲彇妯″瀷,涓嬭浇妯℃澘鏂囦欢 - parse_model_cfg_web(); + //parse_model_cfg_web(); //瑙f瀽妯℃澘鏂囦欢 //Set_xml_nodeinfo(); @@ -518,16 +518,35 @@ void Front::mqproducerThread() extern thread_info_t thread_info[THREAD_CONNECTIONS]; +void cleanup_args(ThreadArgs* args) { + for (int i = 0; i < args->argc; ++i) { + free(args->argv[i]); // strdup 鍒嗛厤鐨 + } + delete[] args->argv; + delete args; +} + void* cloudfrontthread(void* arg) { /////////////////////////////////////// ThreadArgs* args = static_cast(arg); int argc = args->argc; char **argv = args->argv; - printf("argc = %d, argv[0] = %s\n", argc, argv[0]); + printf("[cloudfrontthread] argc = %d\n", argc); + for (int i = 0; i < argc; ++i) { + printf(" argv[%d] = %s\n", i, argv[i]); + } - //娣诲姞绾跨▼澶勭悊 - int index = *(int*)argv[0]; + // 鍔ㄦ佽В鏋愮嚎绋 index + int index = 0; + if (argc > 0 && argv[0]) { + try { + index = std::stoi(argv[0]); + } catch (...) { + std::cerr << "[cloudfrontthread] Failed to parse index from argv[0]: " << argv[0] << "\n"; + return nullptr; + } + } // 鏇存柊绾跨▼鐘舵佷负杩愯涓 pthread_mutex_lock(&thread_info[index].lock); @@ -539,11 +558,12 @@ void* cloudfrontthread(void* arg) { // 瑙f瀽鍛戒护琛屽弬鏁 if(!parse_param(argc,argv)){ std::cerr << "process param error,exit" << std::endl; + cleanup_args(args); return nullptr; } // 绾跨▼浣跨敤瀹屽悗娓呯悊鍙傛暟 - delete args; + cleanup_args(args); //璺緞鑾峰彇 FRONT_PATH = get_parent_directory(); diff --git a/LFtid1056/dealMsg.cpp b/LFtid1056/dealMsg.cpp index 058cd82..10f3599 100644 --- a/LFtid1056/dealMsg.cpp +++ b/LFtid1056/dealMsg.cpp @@ -120,6 +120,11 @@ void process_received_message(string mac, string id,const char* data, size_t len << ", 特征幅值: " << record.fMagntitude << " pu" << ", 时间戳: " << record.triggerTimeMs << "ms" << std::endl; + //lnk20250805 事件上送先记录,录波文件上传结束后再更新文件 + transfer_json_qvvr_data(id,event.head.name, + record.fMagntitude,record.fPersisstime,record.triggerTimeMs,record.nType,record.phase, + ""); + } else { // 处理获取失败的情况 @@ -197,6 +202,9 @@ void process_received_message(string mac, string id,const char* data, size_t len } std::cout << std::endl; + //lnk20250805录波文件目录接口 + assign_qvvr_file_list(id, line_id, fullFilenames); + // ========== 新增:为每个文件生成下载请求 ========== for (const auto& filename : fullFilenames) { // 生成下载请求报文 (帧序号固定为1,代表开始新文件的下载) @@ -346,22 +354,23 @@ void process_received_message(string mac, string id,const char* data, size_t len std::vector arr; arr.push_back({1, //数据属性 -1-无, 0-“Rt”,1-“Max”,2-“Min”,3-“Avg”,4-“Cp95” data_time, //数据转换出来的时间,数据时标,相对1970年的秒,无效填入“-1” - -1, //数据时标,微秒钟,无效填入“-1” + 0, //数据时标,微秒钟,无效填入“-1” 0, //数据标识,1-标识数据异常 max_base64Str}); - arr.push_back({2, data_time, -1, 0, min_base64Str}); - arr.push_back({3, data_time, -1, 0, avg_base64Str}); - arr.push_back({4, data_time, -1, 0, cp95_base64Str}); + arr.push_back({2, data_time, 0, 0, min_base64Str}); + arr.push_back({3, data_time, 0, 0, avg_base64Str}); + arr.push_back({4, data_time, 0, 0, cp95_base64Str}); std::string js = generate_json( + normalize_mac(mac), -1, //需应答的报文订阅者收到后需以此ID应答,无需应答填入“-1” - 123456, //设备唯一标识Ldid,填入0代表Ndid,后续根据商议决定填id还是数字 - 3, //报文处理的优先级:1 I类紧急请求/响应 2 II类紧急请求/响应 3 普通请求/响应 4 广播报文 + 1, //设备唯一标识Ldid,填入0代表Ndid,后续根据商议决定填id还是数字 + 1, //报文处理的优先级:1 I类紧急请求/响应 2 II类紧急请求/响应 3 普通请求/响应 4 广播报文 0x1302, //设备数据主动上送的数据类型 avg_data.name, //逻辑子设备ID,0-逻辑设备本身,无填-1 0x04, //数据类型固定为电能质量 2, //数据属性:无“0”、实时“1”、统计“2”等 - -1, //数据集序号(以数据集方式上送),无填-1 + 1, //数据集序号(以数据集方式上送),无填-1 arr //数据数组 ); @@ -535,18 +544,19 @@ void process_received_message(string mac, string id,const char* data, size_t len std::vector arr; arr.push_back({1, //数据属性 -1-无, 0-“Rt”,1-“Max”,2-“Min”,3-“Avg”,4-“Cp95” data_time, //数据转换出来的时间,数据时标,相对1970年的秒,无效填入“-1” - -1, //数据时标,微秒钟,无效填入“-1” + 0, //数据时标,微秒钟,无效填入“-1” 0, //数据标识,1-标识数据异常 base64}); std::string js = generate_json( + normalize_mac(mac), -1, //需应答的报文订阅者收到后需以此ID应答,无需应答填入“-1” - 123456, //设备唯一标识Ldid,填入0代表Ndid,后续根据商议决定填id还是数字 - 3, //报文处理的优先级:1 I类紧急请求/响应 2 II类紧急请求/响应 3 普通请求/响应 4 广播报文 + 1, //设备唯一标识Ldid,填入0代表Ndid,后续根据商议决定填id还是数字 + 1, //报文处理的优先级:1 I类紧急请求/响应 2 II类紧急请求/响应 3 普通请求/响应 4 广播报文 0x1302, //设备数据主动上送的数据类型 cid, //逻辑子设备ID,0-逻辑设备本身,无填-1 0x04, //数据类型固定为电能质量数据 1, //数据属性:无“0”、实时“1”、统计“2”等 - -1, //数据集序号(以数据集方式上送),无填-1 + 1, //数据集序号(以数据集方式上送),无填-1 arr //数据数组 ); //std::cout << js << std::en @@ -644,6 +654,9 @@ void process_received_message(string mac, string id,const char* data, size_t len out_file.write(reinterpret_cast(file_data.data()), file_data.size()); std::cout << "File saved: " << file_path << std::endl; + + //lnk20250805文件保存成功调用录波文件上送接口 + update_qvvr_file_download(file_path, id); } else { std::cerr << "Failed to save file: " << file_path diff --git a/LFtid1056/main_thread.cpp b/LFtid1056/main_thread.cpp index e1f20fc..193e58e 100644 --- a/LFtid1056/main_thread.cpp +++ b/LFtid1056/main_thread.cpp @@ -8,6 +8,8 @@ #include "cloudfront/code/interface.h" #include +#include +#include using namespace std; #if 0 @@ -32,6 +34,9 @@ typedef struct { } thread_info_t; #endif +extern int INITFLAG;//台账等初始化完成标志 +extern void cleanup_args(ThreadArgs* args); + void init_daemon(void) { int pid; @@ -108,6 +113,32 @@ std::vector generate_test_devices(int count) { return devices; } +void PrintDevices(const std::vector& devices) { + std::cout << "==== Devices List (" << devices.size() << ") ====\n"; + for (const auto& dev : devices) { + std::cout << "Device ID : " << dev.device_id << "\n"; + std::cout << "Name : " << dev.name << "\n"; + std::cout << "Model : " << dev.model << "\n"; + std::cout << "MAC : " << dev.mac << "\n"; + std::cout << "Status : " << dev.status << "\n"; + std::cout << "Points (" << dev.points.size() << "):\n"; + for (const auto& pt : dev.points) { + std::cout << " Point ID : " << pt.point_id << "\n"; + std::cout << " Name : " << pt.name << "\n"; + std::cout << " Device ID : " << pt.device_id << "\n"; + std::cout << " Cpu No : " << pt.nCpuNo << "\n"; + std::cout << " PT1 : " << pt.PT1 << "\n"; + std::cout << " PT2 : " << pt.PT2 << "\n"; + std::cout << " CT1 : " << pt.CT1 << "\n"; + std::cout << " CT2 : " << pt.CT2 << "\n"; + std::cout << " Scale : " << pt.strScale << "\n"; + std::cout << " PTType : " << pt.nPTType << "\n"; + std::cout << "----------------------\n"; + } + std::cout << "==========================\n"; + } +} + /* 线程工作函数 0号子线程*/ /* 客户端连接管理线程函数*/ void* client_manager_thread(void* arg) { @@ -207,7 +238,7 @@ void restart_thread(int index) { int* new_index = (int*)malloc(sizeof(int)); *new_index = index; - if (index == 0) { + if (index == 1) { // 客户端管理线程 if (pthread_create(&thread_info[index].tid, NULL, client_manager_thread, new_index) != 0) { pthread_mutex_lock(&global_lock); @@ -217,7 +248,7 @@ void restart_thread(int index) { free(new_index); } } - else if (index == 1) { + else if (index == 2) { // 消息处理线程 if (pthread_create(&thread_info[index].tid, NULL, message_processor_thread, new_index) != 0) { pthread_mutex_lock(&global_lock); @@ -227,7 +258,7 @@ void restart_thread(int index) { free(new_index); } } - else if (index == 2) { + else if (index == 0) { // 接口,mq char* argv[] = { (char*)new_index };//这里需要构造进程号参数传入 ThreadArgs* args = new ThreadArgs{1, argv}; @@ -251,6 +282,16 @@ int is_thread_alive(pthread_t tid) { return pthread_tryjoin_np(tid, NULL) == EBUSY; // EBUSY表示线程仍在运行 } +//lnk参数 +ThreadArgs* make_thread_args_from_strs(const std::vector& args_vec) { + char** argv = new char*[args_vec.size() + 1]; // 多一个 nullptr 结尾 + for (size_t i = 0; i < args_vec.size(); ++i) { + argv[i] = strdup(args_vec[i].c_str()); // strdup 是 malloc 出来的 + } + argv[args_vec.size()] = nullptr; + return new ThreadArgs{static_cast(args_vec.size()), argv}; +} + /* 主函数 */ int main(int argc ,char** argv) {//多进程添加参数 if(!parse_param(argc,argv)){ @@ -267,34 +308,46 @@ int main(int argc ,char** argv) {// pthread_mutex_init(&thread_info[i].lock, NULL); // 初始化每个线程的锁 } + //接口和mq + ThreadArgs* args = make_thread_args_from_strs({ "0" }); + if (pthread_create(&thread_info[0].tid, NULL, cloudfrontthread, args) != 0) { + printf("Failed to create message processor thread 0\n"); + cleanup_args(args); + } + + while(!INITFLAG){ + std::this_thread::sleep_for(std::chrono::seconds(3)); + std::cout << "waiting cloudfront initialize ..." << std::endl; + } + // 创建初始线程组 - for (int i = 0; i < THREAD_CONNECTIONS; i++) { + for (int i = 1; i < THREAD_CONNECTIONS; i++) { int* index = (int*)malloc(sizeof(int)); *index = i; - if (i == 0) { + if (i == 1) { // 客户端管理线程 if (pthread_create(&thread_info[i].tid, NULL, client_manager_thread, index) != 0) { printf("Failed to create client manager thread %d\n", i); free(index); } } - else if (i == 1) { + else if (i == 2) { // 消息处理线程 if (pthread_create(&thread_info[i].tid, NULL, message_processor_thread, index) != 0) { printf("Failed to create message processor thread %d\n", i); free(index); } } - else if (i == 2){ - //接口和mq + else if (i == 3){ + /*//接口和mq char* argv[] = { (char*)index };//这里需要构造进程号参数传入 ThreadArgs* args = new ThreadArgs{1, argv}; if (pthread_create(&thread_info[i].tid, NULL, cloudfrontthread, args) != 0) { printf("Failed to create message processor thread %d\n", i); delete args; // 如果线程没创建成功就手动释放 free(index); - } + }*/ } else { // 其他工作线程