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 11e1eb6..3ef7ae6 100644 --- a/LFtid1056/cloudfront/code/cfg_parser.cpp +++ b/LFtid1056/cloudfront/code/cfg_parser.cpp @@ -2750,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}, @@ -2758,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, //鎶ユ枃澶勭悊鐨勪紭鍏堢骇 @@ -2769,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; @@ -2792,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; @@ -2814,8 +2817,9 @@ std::vector GenerateDeviceInfoFromLedger(const std::vector GenerateDeviceInfoFromLedger(const std::vector lock(ledgermtx); // 鍘婚櫎 mac 璺緞鍓嶇紑锛屼粎淇濈暀鏂囦欢鍚 - std::string filename = extract_filename(filename_with_mac); + std::string filename = extract_filename1(filename_with_mac); // 鎻愬彇閫昏緫搴忓彿锛堝 PQM1 鈫 1锛 size_t under_pos = filename.find('_'); @@ -2992,7 +2999,7 @@ bool update_qvvr_file_download(const std::string& filename_with_mac, const std:: std::set s_name(qfile.file_name.begin(), qfile.file_name.end()); std::set s_down; for (const auto& path : qfile.file_download) { - s_down.insert(extract_filename(path)); // 鎻愬彇姣忎釜璺緞涓殑鏂囦欢鍚 + s_down.insert(extract_filename1(path)); // 鎻愬彇姣忎釜璺緞涓殑鏂囦欢鍚 } // 妫鏌 file_download 鏄惁涓 file_name 瀹屽叏涓鑷达紙闆嗗悎鐩稿悓锛 @@ -3001,7 +3008,7 @@ bool update_qvvr_file_download(const std::string& filename_with_mac, const std:: // 鎵惧埌鍏朵腑鐨 .cfg 鏂囦欢杩涜鍖归厤 for (const auto& fpath : qfile.file_download) { - std::string fname = extract_filename(fpath); + std::string fname = extract_filename1(fpath); if (fname.size() >= 4 && fname.substr(fname.size() - 4) == ".cfg") { // 鎻愬彇鏂囦欢鏃舵爣鍜岀洃娴嬬偣浜嬩欢鐨勬椂鏍囧尮閰 qvvr_data matched; @@ -3064,3 +3071,10 @@ bool update_qvvr_file_download(const std::string& filename_with_mac, const std:: } 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 bb7f173..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锛屽悗缁牴鎹崗璁彴璐︿慨鏀 @@ -1199,21 +1199,21 @@ int transfer_json_qvvr_data(const std::string& dev_id, ushort monitor_id, // 鏈夋晥鍝嶅簲锛岀暐杩 } catch (...) { // 鍝嶅簲寮傚父锛屼繚瀛 json - DIY_ERRORLOG(full_key_m_d.c_str(), "銆怑RROR銆戞殏鎬佹帴鍙e搷搴斿紓甯,鏃犳硶涓婇佺洃娴嬬偣%s鐨勬殏鎬佷簨浠", monitor_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 + monitor_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鐨勬殏鎬佷簨浠", monitor_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 + monitor_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; @@ -1234,7 +1234,9 @@ int transfer_json_qvvr_data(const std::string& dev_id, ushort monitor_id, void qvvr_test() { - + 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 14e9889..3f4008a 100644 --- a/LFtid1056/cloudfront/code/interface.h +++ b/LFtid1056/cloudfront/code/interface.h @@ -429,6 +429,7 @@ struct MsgObj { // 鏁翠綋 struct FullObj { + std::string mac; int Mid; int Did; int Pri; @@ -446,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, //鎶ユ枃澶勭悊鐨勪紭鍏堢骇 @@ -461,6 +463,8 @@ std::string generate_json( //鏋勯犺缃富鍔ㄤ笂閫佹暟鎹殑鎶ユ枃 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); @@ -468,6 +472,9 @@ bool assign_qvvr_file_list(const std::string& id, ushort nCpuNo, const std::vect //褰曟尝鏂囦欢涓嬭浇瀹屾垚閫氱煡鎺ュ彛 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); + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #endif 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 a1a8612..10f3599 100644 --- a/LFtid1056/dealMsg.cpp +++ b/LFtid1056/dealMsg.cpp @@ -354,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 //数据数组 ); @@ -543,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 diff --git a/LFtid1056/main_thread.cpp b/LFtid1056/main_thread.cpp index f3c17c1..2ca393c 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) { @@ -123,17 +154,17 @@ void* client_manager_thread(void* arg) { printf("Started client connections\n"); // 创建测点数据 - std::vector points1 = { + /*std::vector points1 = { {"P001", "Main Voltage", "D001",1 ,1, 1, 1, 1,"0.38k",0}, {"P002", "Backup Voltage", "D001",2 ,1, 1, 1, 1,"0.38k",0} }; std::vector points2 = { {"P101", "Generator Output", "D002",1 ,1, 1, 1, 1,"0.38k",0} - }; + };*/ //00B78DA800D6 00-B7-8D-01-79-06 // 创建装置列表 - std::vector devices = { + /*std::vector devices = { { "D001", "Primary Device", "Model-X", "00-B7-8D-01-79-06", 1, points1 @@ -142,12 +173,16 @@ void* client_manager_thread(void* arg) { "D002", "Backup Device", "Model-Y", "00-B7-8D-A8-00-D6", 1, points2 } - }; + };*/ // 生成100个测试装置 - std::vector test_devices = generate_test_devices(100); + //std::vector test_devices = generate_test_devices(100); - //std::vector devices = GenerateDeviceInfoFromLedger(terminal_devlist);//lnk添加 + //lnk从台账读取设备 + std::vector devices = GenerateDeviceInfoFromLedger(terminal_devlist);//lnk添加 + + //台账打印 + PrintDevices(devices); // 启动客户端连接 start_client_connect(devices); @@ -215,7 +250,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); @@ -225,7 +260,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); @@ -235,7 +270,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}; @@ -259,6 +294,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)){ @@ -275,34 +320,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 { // 其他工作线程