#include"kutil.h" #include #include #include #include #include "libxl.h" #include #include #include #include #include #include #include #include #include #include #include #include "libxl.h" #include #include #include #include #include #include #include #include using namespace libxl; using namespace std::chrono_literals; using namespace std; #define debug true #define ITK_err 919012 std::string generateJsonString2(char *folwId, vector id, vector version, vector fileName) { std::string jsonStr = "{\n"; jsonStr += " \"flowId\":\"" + std::string(folwId) + "\",\n"; jsonStr += " \"file\":\n"; jsonStr += " [\n"; for (int i = 0; i < id.size();i++) { jsonStr += " {\n"; if (i == id.size()-1) { jsonStr += " \"id\":\"" + std::string(id[i]) + "\",\n"; jsonStr += " \"version\":\"" + std::string(version[i]) + "\",\n"; jsonStr += " \"url\":\"" + std::string(fileName[i]) + "\"\n"; jsonStr += " }\n"; break; } jsonStr += " \"id\":\"" + std::string(id[i]) + "\",\n"; jsonStr += " \"version\":\"" + std::string(version[i]) + "\",\n"; jsonStr += " \"type\":\"" + std::string(fileName[i]) + "\"\n"; jsonStr += " },\n"; } jsonStr += " ]\n"; jsonStr += " }\n"; return jsonStr; } // 转义json字符串 void escapeJson2(std::string& str) { int len = str.length(); int newLen = len; for (int i = 0; i < len; i++) { if (str[i] == '"' || str[i] == '\\') { newLen++; } } std::string escapedStr; escapedStr.reserve(newLen); for (int i = 0; i < len; i++) { if (str[i] == '"' || str[i] == '\\') { escapedStr.push_back('\\'); } escapedStr.push_back(str[i]); } str = escapedStr; } bool copyFileToFolder(const std::string& filePath, const std::string& folderPath) { // 打开源文件 std::ifstream sourceFile(filePath, std::ios::binary); if (!sourceFile) { std::cout << "文件不存在或无法打开" << std::endl; return false; } // 获取文件名 size_t lastSlashIndex = filePath.find_last_of("/\\"); std::string fileName = filePath.substr(lastSlashIndex + 1); // 构造目标路径 std::string destinationPath = folderPath + "/" + fileName; // 打开目标文件 std::ofstream destinationFile(destinationPath, std::ios::binary); if (!destinationFile) { std::cout << "无法创建目标文件" << std::endl; return false; } // 复制文件 destinationFile << sourceFile.rdbuf(); // 关闭文件 sourceFile.close(); destinationFile.close(); std::cout << "文件已成功复制到目标文件夹" << std::endl; return true; } int LD_SendDJJDD_To_MES(EPM_action_message_t msg) { printf("开始执行"); int att_cnt = 0; int doc_num = 0; tag_t rootTask_tag = NULLTAG; tag_t task = NULLTAG; tag_t* doc_tags = NULLTAG; string error; char* arg = NULL, * argflag = NULL, * argvalue = NULL; int arg_cnt = TC_number_of_arguments(msg.arguments); char* types = NULL; string erro2 = ""; string erro = ""; int send1 = 0; int send2 = 0; task = msg.task; ITKCALL(EPM_ask_root_task(task, &rootTask_tag)); if (arg_cnt > 0) { for (int i = 0; i < arg_cnt; i++) { arg = TC_next_argument(msg.arguments); ITKCALL(ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue)); if (strcmp(argflag, "type") == 0) { if (argvalue != NULL) { printf("获取的值%s:\n", argvalue); types = argvalue; } } } printf("获取UID"); char* flowId; ITK__convert_tag_to_uid(rootTask_tag, &flowId); printf("%s\n", flowId); //获取流程下目标关系下的对象 printf("8888888888888888888888888888888888888888888\n"); printf("获取流程下目标关系下的对象\n"); ITKCALL(EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &doc_num, &doc_tags)); printf("开始循环获取的目标对象%d\n", doc_num); vectorids; vector revisions; vector fileNames; //获取首选项的值 printf("获取首选项的值\n"); char* url = NULL, *sendfilepath = NULL,*fileURL = NULL; PREF_ask_char_value("LD_PLM2MES_DJJDD_Config", 0, &url); PREF_ask_char_value("LD_PLM2MES_DJJDD_Config", 1, &sendfilepath); PREF_ask_char_value("LD_PLM2MES_DJJDD_Config", 2, &fileURL); printf("url:%s\n", url); printf("sendfilepath:%s\n", sendfilepath); printf("fileURL:%s\n", fileURL); for (int i = 0; i < doc_num; i++) { char* objtype = NULL; printf("获取类型\n"); ITKCALL(AOM_ask_value_string(doc_tags[i], "object_type", &objtype)); vector ans; Split(types, ";", ans); for (int n = 0; n < ans.size(); n++) { string gettype = ans[n]; printf("与流程参数type对比%s=%s\n",gettype.c_str(), objtype); if (strcmp(gettype.c_str(), objtype) == 0) { //获取id printf("获取id\n"); char* id = NULL; ITKCALL(AOM_ask_value_string(doc_tags[i], "item_id", &id)); ids.push_back(id); //获取revision printf("获取revision\n"); char* version = NULL; ITKCALL(AOM_ask_value_string(doc_tags[i], "item_revision_id", &version)); revisions.push_back(version); //获取数据集文件 printf("获取数据集文件\n"); int das_num = 0; tag_t* das_tags; ITKCALL(AOM_ask_value_tags(doc_tags[i], "IMAN_specification", &das_num, &das_tags)); string files; for (int m = 0; m < das_num;m++) { char* fileName = NULL; ITKCALL(AOM_ask_value_string(das_tags[m], "object_name", &fileName)); time_t nowtime; time(&nowtime); //获取1970年1月1日0点0分0秒到现在经过的秒数 tm p; localtime_s(&p, &nowtime); //将秒数转换为本地时间,年从1900算起,需要+1900,月为0-11,所以要+1 char fileDate[128] = ""; char buffer[80]; sprintf_s(buffer, "%04d%d%02d%02d%02d%02d", 1900 + p.tm_year, p.tm_mon + 1, p.tm_mday,p.tm_hour,p.tm_min,p.tm_sec); printf("获取的时间:%s\n",buffer); char sendname[SS_MAXPATHLEN] = ""; strcat(sendname, buffer); strcat(sendname, "-"); strcat(sendname, fileName); printf("合并后的字符串: % s\n", sendname); printf("开始复制文件到file空间\n"); tag_t ref_object = NULLTAG; printf("1"); AE_reference_type_t reference_type; printf("2"); char ref_name[WSO_name_size_c + 1] = "excel"; ITKCALL(AE_ask_dataset_named_ref2(das_tags[m], ref_name, &reference_type, &ref_object)); printf("3"); if (reference_type == AE_PART_OF) { printf("7"); char temp_file[SS_MAXPATHLEN] = ""; strcpy(temp_file, sendfilepath); strcat(temp_file, "\\"); strcat(temp_file, sendname); printf("temp_file%s", temp_file); ITKCALL(IMF_export_file(ref_object, temp_file)); } char temp_fileurl[SS_MAXPATHLEN] = ""; strcpy(temp_fileurl, fileURL); strcat(temp_fileurl, sendname); files = files + temp_fileurl + ","; } if (!files.empty()) { files = files.substr(0, files.length() - 1); fileNames.push_back(files); } else { fileNames.push_back(""); } } } } printf("获取要发送的json字符串\n"); printf("flowId:%s\n", flowId); //获取要发送的json字符串 std::string jsonString = generateJsonString2(flowId, ids, revisions,fileNames); printf("jsonString:%s\n", jsonString.c_str()); std::regex regex("\\s+"); std::string result = std::regex_replace(jsonString, regex, ""); // 转义处理后的json字符串 printf("转义处理后的json字符串\n"); escapeJson2(result); printf("jsonString:%s\n", result.c_str()); //调用jar包向接口发送数据 printf("开始执行cmd方法"); char cmd[1024] = ""; strcpy(cmd, "java -jar D:\\Siemens\\Teamcenter13\\bin\\sapjco3.jar"); strcat(cmd, " "); strcat(cmd, url); strcat(cmd, " "); strcat(cmd, result.c_str()); strcat(cmd, " "); strcat(cmd, "3"); printf("\n%s\n", cmd); system(cmd); //读取本地文件获取执行结果 //读取本地的result文件(SAP) string fileid = flowId; string MESfile = "D:\\TCTOMES\\" + fileid + "result.txt"; std::ifstream file(MESfile); if (!file.is_open()) { std::cerr << "Failed to open file!" << std::endl; char* error = "MES服务未响应"; EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", error); return 1; } else { std::stringstream buffer1; buffer1 << file.rdbuf(); std::string resultStr = buffer1.str(); printf("返回的值%s:\n", resultStr.c_str()); if (strcmp(resultStr.c_str(), "成功") == 0) { return 0; } else { EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", resultStr.c_str()); return 1; } } return 0; } }