From 040b1bd5f6a3d8afd4d5920f01486d3bb0b3d64f Mon Sep 17 00:00:00 2001 From: lnk Date: Thu, 16 Jan 2025 19:16:26 +0800 Subject: [PATCH] modify for ledgerupdate --- cfg_parse/cfg_parser.cpp | 8 +- json/save2json.cpp | 20 ++- mms/mms_process.c | 256 ++++----------------------------------- 3 files changed, 43 insertions(+), 241 deletions(-) diff --git a/cfg_parse/cfg_parser.cpp b/cfg_parse/cfg_parser.cpp index da05606..efea5b4 100644 --- a/cfg_parse/cfg_parser.cpp +++ b/cfg_parse/cfg_parser.cpp @@ -14207,11 +14207,11 @@ int update_one_terminal_ledger(terminal* update, int i,ied_t* ied,int terminal_i } line_info.line_id = count_real_monitor; // 记录终端排号 printf("line_id: %d\n", line_info.line_id); - printf("mp_id: %d\n", line_info.mp_id); + printf("mp_id: %s\n", line_info.mp_id); printf("terminal_code: %s\n", line_info.terminal_code); - printf("voltage_level: %d\n", line_info.voltage_level); - printf("v_wiring_type: %d\n", line_info.v_wiring_type); - printf("monitor_status: %d\n", line_info.monitor_status); + printf("voltage_level: %s\n", line_info.voltage_level); + printf("v_wiring_type: %s\n", line_info.v_wiring_type); + printf("monitor_status: %s\n", line_info.monitor_status); printf("name: %s\n", line_info.name); // 填充时间戳 if (update[i].timestamp[0] != '\0') { diff --git a/json/save2json.cpp b/json/save2json.cpp index 1b5862b..173e347 100644 --- a/json/save2json.cpp +++ b/json/save2json.cpp @@ -46,6 +46,7 @@ extern std::string intToString(int number); int StringToInt(const std::string& str); extern pthread_mutex_t mtx;//lnk20250115 + #ifdef __cplusplus extern "C" { #include "../mms/rdb_client.h" @@ -59,6 +60,7 @@ extern "C" { extern int comtrade_remain_file_num; extern node_t* g_node; //lnk20241223 extern LD_info_t* find_LD_info_only_from_mp_id(char* mp_id);//lnk20241223 + extern void print_terminal(const terminal* tmnl); #ifdef __cplusplus } #endif @@ -1126,14 +1128,16 @@ std::string prepare_update(const std::string& code_str, const terminal& json_dat // 结束 modify 或 add 标签 if (code_str == "ledger_modify") { + indentLevel--; add_indent(xmlStream, indentLevel); xmlStream << "" << std::endl; - indentLevel--; + } else { + indentLevel--; add_indent(xmlStream, indentLevel); xmlStream << "" << std::endl; - indentLevel--; + } } else if (code_str == "delete_terminal") { @@ -1350,16 +1354,18 @@ void parse_control(const std::string& json_str, const std::string& output_dir) { else std::strncpy(monitor_data.status, "N/A", sizeof(monitor_data.status) - 1); - cJSON* lineNo = cJSON_GetObjectItem(item, "lineNo"); // logical_device_seq + cJSON* lineNo = cJSON_GetObjectItem(monitor_item, "lineNo"); // logical_device_seq if (lineNo && lineNo->type == cJSON_String) + std::strncpy(monitor_data.logical_device_seq, lineNo->valuestring, sizeof(monitor_data.logical_device_seq) - 1); - else + else std::strncpy(monitor_data.logical_device_seq, "N/A", sizeof(monitor_data.logical_device_seq) - 1); - cJSON* ptType = cJSON_GetObjectItem(item, "ptType"); // terminal_connect + cJSON* ptType = cJSON_GetObjectItem(monitor_item, "ptType"); // terminal_connect if (ptType && ptType->type == cJSON_String) + std::strncpy(monitor_data.terminal_connect, ptType->valuestring, sizeof(monitor_data.terminal_connect) - 1); - else + else std::strncpy(monitor_data.terminal_connect, "N/A", sizeof(monitor_data.terminal_connect) - 1); std::strncpy(monitor_data.timestamp, json_data.timestamp, sizeof(monitor_data.timestamp) - 1); @@ -1370,6 +1376,8 @@ void parse_control(const std::string& json_str, const std::string& output_dir) { } } + print_terminal(&json_data); + // 准备 XML 内容并写入文件 std::string xmlContent = prepare_update(code_str, json_data); if (xmlContent != "") { diff --git a/mms/mms_process.c b/mms/mms_process.c index a1a526d..7b183d8 100644 --- a/mms/mms_process.c +++ b/mms/mms_process.c @@ -807,30 +807,31 @@ void process_ledger_update(trigger_update_xml_t *ledger_update_xml) printf("add ledger num:%d\n",update_num); for (i=0; imodify_update_num < MAX_UPDATEA_NUM) { - // 添加到 modify_updates 数组 - ledger_update_xml->modify_updates[ledger_update_xml->modify_update_num] = update[i]; - ledger_update_xml->modify_update_num++; // 更新 modify 数组的计数器 + // 将当前终端从 new_updates 数组中移到 modify_updates 数组 + if (ledger_update_xml->modify_update_num < MAX_UPDATEA_NUM) { + // 添加到 modify_updates 数组 + ledger_update_xml->modify_updates[ledger_update_xml->modify_update_num] = update[i]; + ledger_update_xml->modify_update_num++; // 更新 modify 数组的计数器 - // 删除该终端(从 new_updates 数组中移除) - for (j = i; j < ledger_update_xml->new_update_num - 1; j++) { - ledger_update_xml->new_updates[j] = ledger_update_xml->new_updates[j + 1]; // 向前移动元素 - } + // 删除该终端(从 new_updates 数组中移除) + for (j = i; j < ledger_update_xml->new_update_num - 1; j++) { + ledger_update_xml->new_updates[j] = ledger_update_xml->new_updates[j + 1]; // 向前移动元素 + } - ledger_update_xml->new_update_num--; // 更新 new_update_num,减少一个元素 - i--; // 重新检查新数组中的当前元素,避免跳过 - } else { - fprintf(stderr, "Exceeded MAX_UPDATEA_NUM limit for modify_updates!\n"); - } - - } + ledger_update_xml->new_update_num--; // 更新 new_update_num,减少一个元素 + continue; + } else { + fprintf(stderr, "Exceeded MAX_UPDATEA_NUM limit for modify_updates!\n"); + } + } + } if (!new_in_work_found) { //这是全新的台账,不是正在工作的台账 @@ -876,215 +877,6 @@ void process_ledger_update(trigger_update_xml_t *ledger_update_xml) //3-写入台账内容////////////////////////////// int ret = update_one_terminal_ledger(update,i,ied,g_node->n_clients); -#if 0 - // 将 update[i] 中的数据写入到 ied_usr 中 - if (strlen(update[i].terminal_id) != 0) { - apr_snprintf(ied_usr->terminal_id, sizeof(ied_usr->terminal_id), "%s", update[i].terminal_id); - printf("ied_usr->terminal_id: %s\n", ied_usr->terminal_id); - } - - if (update[i].terminal_code != NULL) { - apr_snprintf(ied_usr->terminal_code, sizeof(ied_usr->terminal_code), "%s", update[i].terminal_code); - printf("ied_usr->terminal_code: %s\n", ied_usr->terminal_code); - } - - if (update[i].tmnl_factory != NULL) { - apr_snprintf(ied_usr->tmnl_factory, sizeof(ied_usr->tmnl_factory), "%s", update[i].tmnl_factory); - printf("ied_usr->tmnl_factory: %s\n", ied_usr->tmnl_factory); - } - - if (update[i].tmnl_status != NULL) { - apr_snprintf(ied_usr->tmnl_status, sizeof(ied_usr->tmnl_status), "%s", update[i].tmnl_status); - printf("ied_usr->tmnl_status: %s\n", ied_usr->tmnl_status); - } - - if (update[i].dev_type != NULL) { - apr_snprintf(ied_usr->dev_type, sizeof(ied_usr->dev_type), "%s", update[i].dev_type); - printf("ied_usr->dev_type: %s\n", ied_usr->dev_type); - } - - if (update[i].dev_series != NULL) { - apr_snprintf(ied_usr->dev_series, sizeof(ied_usr->dev_series), "%s", update[i].dev_series); - printf("ied_usr->dev_series: %s\n", ied_usr->dev_series); - } else { - apr_snprintf(ied_usr->dev_series, sizeof(ied_usr->dev_series), "%s", ""); // 默认为空字符串 - printf("ied_usr->dev_series (default): %s\n", ied_usr->dev_series); - } - - if (update[i].dev_key != NULL) { - apr_snprintf(ied_usr->dev_key, sizeof(ied_usr->dev_key), "%s", update[i].dev_key); - printf("ied_usr->dev_key: %s\n", ied_usr->dev_key); - } else { - apr_snprintf(ied_usr->dev_key, sizeof(ied_usr->dev_key), "%s", ""); // 默认为空字符串 - printf("ied_usr->dev_key (default): %s\n", ied_usr->dev_key); - } - - ied_usr->dev_idx = new_client_count; // 最新的一个终端排号 - printf("dev_idx: %d\n", ied_usr->dev_idx); - - ied->channel[0].channel_type = CHANNEL_TYPE_IPV4; // channel - ied->channel[0].addr_str[LONGNAME - 1] = 0; // DEV_IP - if (update[i].addr_str != NULL) { - ied->channel[0].addr = ntohl(inet_addr(update[i].addr_str)); // DEV_IP - strncpy(ied->channel[0].addr_str, update[i].addr_str, LONGNAME - 1); // DEV_IP - printf("ied_usr->addr_str: %s\n", ied->channel[0].addr_str); - } else { - ied->channel[0].addr = ntohl(inet_addr("0.0.0.0")); // DEV_IP - strncpy(ied->channel[0].addr_str, update[i].addr_str, LONGNAME - 1); // DEV_IP - printf("ied_usr->addr_str: %s\n", ied->channel[0].addr_str); - } - - if (update[i].port != NULL) { - int port = 102; - if (stringToInt(update[i].port, &port)) { - // 转换成功,portStr全为数字,并且已经转换为int类型的port - ied->channel[0].port = port; // DEV_PortID - printf("ied_usr->port: %d\n", ied->channel[0].port); // DEV_PortID - } else { - ied->channel[0].port = 102; // DEV_PortID - printf("ied_usr->port: %s, 非合法端口. 使用默认端口: %d\n", update[i].port, ied->channel[0].port); // DEV_PortID - } - } - - if (update[i].timestamp != NULL && strlen(update[i].timestamp) > 0) { - // 构造struct tm对象 - struct tm timeinfo = {0}; // 初始化为0 - - // 假设时间字符串格式为 "YYYY-MM-DD HH:MM:SS" - // 使用sscanf从字符串中提取各个时间字段 - if (sscanf(update[i].timestamp, "%4d-%2d-%2d %2d:%2d:%2d", - &timeinfo.tm_year, &timeinfo.tm_mon, &timeinfo.tm_mday, - &timeinfo.tm_hour, &timeinfo.tm_min, &timeinfo.tm_sec) == 6) { - // 将年份从1900开始计算 - timeinfo.tm_year -= 1900; - // 月份从0开始计算,减去1 - timeinfo.tm_mon -= 1; - // 对tm_isdst进行初始化,通常可以设置为-1,由mktime自动判断 - timeinfo.tm_isdst = -1; - - // 使用mktime将struct tm转换为time_t - time_t raw_time = mktime(&timeinfo); - - // 判断mktime是否成功 - if (raw_time != -1) { - ied_usr->time = (long long)raw_time; - printf("ied_usr->time: %lld\n", ied_usr->time); - } else { - printf("Error: mktime failed.\n"); - } - } else { - printf("Error: sscanf failed. Invalid timestamp format.\n"); - } - } - - - chnl_usr = (chnl_usr_t*)apr_pcalloc(g_init_pool, sizeof(chnl_usr_t)); - ied->channel[0].connect = chnl_usr; - chnl_usr->chnl = &(ied->channel[0]); - chnl_usr->chnl_id = 0; - chnl_usr->m_state = CHANNEL_DISCONNECTED; - chnl_usr->m_ClosedMsTime = NEXT_CONNECT_TIME * (-1); - g_pt61850app->chnl_counts++; - - // 将 monitorData 中的数据写入到 LD_info 中 - int count_real_monitor = 0; //遍历监测点台账的计数器 - int j; - for (j = 0; j < 10 && update[i].line[j].monitor_id[0] != '\0'; ++j) { - monitor monitor_data = update[i].line[j]; - - //监测点计数 - count_real_monitor++; - - // 初始化 LD_info - LD_info_t line_info; - memset(&line_info, 0, sizeof(line_info)); - - char logical_device_seq[64]; - - // 填充监测点信息 - strncpy(line_info.mp_id, monitor_data.monitor_id, sizeof(line_info.mp_id) - 1); - strncpy(line_info.name, monitor_data.monitor_name, sizeof(line_info.name) - 1); - strncpy(line_info.voltage_level, monitor_data.voltage_level, sizeof(line_info.voltage_level) - 1); - strncpy(line_info.v_wiring_type, monitor_data.terminal_connect, sizeof(line_info.v_wiring_type) - 1); - strncpy(line_info.monitor_status, monitor_data.status, sizeof(line_info.monitor_status) - 1); - strncpy(line_info.terminal_code, monitor_data.terminal_code, sizeof(line_info.terminal_code) - 1); - - strncpy(logical_device_seq, monitor_data.logical_device_seq, sizeof(logical_device_seq) - 1); - if (isCharPtrEmpty(logical_device_seq)) { - line_info.cpuno = 1; // 默认监测点实例号1 - printf("logical_device_seq: is null, set cpuno: %d\n", line_info.cpuno); - } else { - line_info.cpuno = atoi(logical_device_seq); - printf("logical_device_seq: %d\n", line_info.cpuno); - } - - line_info.line_id = count_real_monitor; // 记录终端排号 - - printf("line_id: %d\n", line_info.line_id); - printf("mp_id: %d\n", line_info.mp_id); - printf("terminal_code: %s\n", line_info.terminal_code); - printf("voltage_level: %d\n", line_info.voltage_level); - printf("v_wiring_type: %d\n", line_info.v_wiring_type); - printf("monitor_status: %d\n", line_info.monitor_status); - printf("name: %s\n", line_info.name); - - - // 填充时间戳 - if (update[i].timestamp[0] != '\0') { - struct tm timeinfo; - char timestamp[64]; - - // 假设update[i].timestamp格式为 "YYYY-MM-DD HH:MM:SS" - // 例如:"2023-01-14 12:34:56" - sscanf(update[i].timestamp, "%4d-%2d-%2d %2d:%2d:%2d", - &timeinfo.tm_year, &timeinfo.tm_mon, &timeinfo.tm_mday, - &timeinfo.tm_hour, &timeinfo.tm_min, &timeinfo.tm_sec); - - // 将年份从1900年开始计算 - timeinfo.tm_year -= 1900; - // 月份从0开始,减1 - timeinfo.tm_mon -= 1; - // 对tm_isdst进行初始化,通常可以设置为-1,由mktime自动判断 - timeinfo.tm_isdst = -1; - - // 使用mktime将struct tm转换为time_t - time_t raw_time = mktime(&timeinfo); - - // 判断mktime是否成功 - if (raw_time != -1) { - line_info.time = (long long)raw_time; - printf("time: %lld\n", line_info.time); - } else { - printf("Error: mktime failed.\n"); - } - } - - line_info.read_flag = 1; //监测点有效 - - // 填充 LD_info - if (ied && ied->usr_ext && line_info.cpuno && ((int)line_info.cpuno < 10)) { - char str[256]; - byte_t cpuno = line_info.cpuno; - printf("cpuno: %d\n", line_info.cpuno); - printf("index cpuno: %d\n", cpuno - 1); - ied_usr = (ied_usr_t*)ied->usr_ext; - ied_usr->LD_info[cpuno - 1] = line_info; // cpuno默认是1 - ied_usr->LD_info[cpuno - 1].ied = ied; - apr_snprintf(str, sizeof(str), "PQMonitorPQM%d", cpuno); - ied_usr->LD_info[cpuno - 1].LD_name = apr_pstrdup(g_init_pool, str); - ied_usr->LD_info[cpuno - 1].ht_fcd = apr_hash_make(g_init_pool); - ied_usr->LD_info[cpuno - 1].ht_full_fcda = apr_hash_make(g_init_pool); - ied_usr->LD_info[cpuno - 1].rptcount = 0; - printf("rptcount: %d\n", ied_usr->LD_info[cpuno - 1].rptcount); - if (cpuno > ied->cpucount) { - ied->cpucount = cpuno; - } - } - // ied_usr->LD_info[j] = line_info; - printf("Monitor Info [ID: %s, Name: %s] saved in LD_info\n", line_info.mp_id, line_info.name); - - } -#endif //3-写入台账内容/////////////////////////////////// @@ -1224,7 +1016,9 @@ void check_ledger_update()//lnk20250113 trigger_update_xml_t trigger_ledger_update_xml; //台账更新文件结构 - printf("begin ledger update...\n"); + printf("check ledger update...\n"); + + last_check_3s_config_time = now; //记录本次运行时间 //判断是否满足执行条件,一个文件就是一个终端,读取文件后删除文件 while (APR_SUCCESS==parse_ledger_update_xml(&trigger_ledger_update_xml)){ //处理台账更新文件,如果有可以更新或者添加台账的