You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

535 lines
14 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"
#include <sys/types.h>
#include <time.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <iostream>
#include <windows.h>
#include <string>
bool timeoutFlag = false;
std::mutex timeoutMutex;
int executeCommand(const char* cmd, std::string& strResult, int timeout)
{
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = nullptr;
HANDLE hReadPipe, hWritePipe;
if (!CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0))
{
std::cout << "Failed to create pipe" << std::endl;
return -1;
}
STARTUPINFOA si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
if (!CreateProcessA(NULL, const_cast<char*>(cmd), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
std::cout << "Failed to create process" << std::endl;
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
return -1;
}
CloseHandle(hWritePipe);
DWORD dwExitCode = 0;
bool isTimeout = false;
if (WaitForSingleObject(pi.hProcess, timeout * 1000) != WAIT_OBJECT_0)
{
isTimeout = true;
TerminateProcess(pi.hProcess, 0);
}
else
{
GetExitCodeProcess(pi.hProcess, &dwExitCode);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (!isTimeout)
{
CHAR buffer[8000];
DWORD bytesRead;
while (ReadFile(hReadPipe, buffer, sizeof(buffer) - 1, &bytesRead, NULL))
{
if (bytesRead == 0)
break;
buffer[bytesRead] = '\0';
strResult += buffer;
}
}
CloseHandle(hReadPipe);
if (isTimeout)
{
std::cout << "Command execution timed out" << std::endl;
strResult += "³¬Ê±";
return -1;
}
else
{
return dwExitCode;
}
}
int ML_SendSapItem(EPM_action_message_t msg)
{
printf("=========================¶ÔÏóÏ·¢µ½SAP Start===================\n");
auto startTime = std::chrono::high_resolution_clock::now();
//POM_AM__set_application_bypass(true);
int ifail = ITK_ok;
int attachments_num = 0;
tag_t rootTask = NULLTAG, *attachments = NULLTAG;
//»ñÈ¡ÈÎÎñ¶ÔÏó
EPM_ask_root_task(msg.task, &rootTask);
//»ñÈ¡ÈÎÎñÄ¿±ê¶ÔÏó
EPM_ask_attachments(rootTask, EPM_target_attachment, &attachments_num, &attachments);
//»ñÈ¡²ÎÊý
char *argflag = NULL, *argvalue = NULL, *arg = NULL;
char messageUser[1024] = "", messageResult[1024] = "", timeout[16] = "";
int arg_cnt = TC_number_of_arguments(msg.arguments);
printf("²ÎÊý¸öÊýΪ£º%d\n", arg_cnt);
if (arg_cnt > 0)
{
for (int i = 0; i<arg_cnt; i++)
{
//»ñÈ¡ÏÂÒ»¸ö²ÎÊý£¨´Ó0¿ªÊ¼£©
arg = TC_next_argument(msg.arguments);
//»ñÈ¡²ÎÊýµÄÃû³ÆºÍÖµ
ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
if (stricmp(argflag, "ownerId") == 0)
{
if (argvalue != NULL)
{
strcpy(messageUser, argvalue);
}
}
else if (stricmp(argflag, "messageResult") == 0)
{
if (argvalue != NULL)
{
strcpy(messageResult, argvalue);
}
}
else if (stricmp(argflag, "timeout") == 0)
{
if (argvalue != NULL)
{
strcpy(timeout, argvalue);
}
}
}
}
vector<char *> messageUserValues;
if (strstr(messageUser, ",") != NULL)
{
int vectorValueCount = 0;
char ** vectorValueChar = new char *[64];
split(messageUser, ",", vectorValueChar, &vectorValueCount);
for (int i = 0; i < vectorValueCount; i++)
{
messageUserValues.push_back(vectorValueChar[i]);
}
}
else
{
messageUserValues.push_back(messageUser);
}
int pref_cnt = 0;
char ** pref_vals = NULL;
//»ñÈ¡Ê×Ñ¡ÏîµÄÖµ
PREF_ask_char_values("ML_ItemSendSapProperty", &pref_cnt, &pref_vals);
map<string, string> typePropertyType;
for (int j = 0; j < pref_cnt; j++)
{
if (strstr(pref_vals[j], "-") != NULL)
{
//°´ÕÕ-½øÐвð·Ö¡£²ð·ÖÌõ¼þ
int valueCount = 0;
char ** valueChar = new char *[64];
//·Ö¸î×Ö·û´®
split(pref_vals[j], "-", valueChar, &valueCount);
typePropertyType[valueChar[0]] = valueChar[1];// .insert(std::pair<char*, char*>(valueChar[0], valueChar[1]));
}
}
bool isStart = true;
char *tc_root_file = getenv("TEMP"); //C:\Siemens\Teamcenter11
char *tc_root = getenv("TC_ROOT");
char parameters[100000] = "";//дÈëµ½ÎļþµÄÖµ
strcat(parameters, "getItemPropertys}}");
strcat(parameters, "[");
for (int i = 0; i < attachments_num; i++)
{
char *itemType = NULL;
AOM_ask_value_string(attachments[i], "object_type", &itemType);
printf("itemType£º%s\n", itemType);
//¹ýÂ˵ô·Ç°æ±¾µÄ¶ÔÏó
if ((strcmp(itemType, "ML8_RefrigeratorRevision") == 0) || (strcmp(itemType, "ML8_WashingRevision") == 0)
|| (strcmp(itemType, "ML8_ColdRevision") == 0) || (strcmp(itemType, "ML8_OthersRevision") == 0) || (strcmp(itemType, "ML8_PartRevision") == 0)
|| (strcmp(itemType, "ML8_RawMaterialRevision") == 0) || (strcmp(itemType, "ML8_UnProductRevision") == 0) || (strcmp(itemType, "ML8_ProductOthRevision") == 0))
{
string propertyNames = typePropertyType[itemType];
if (propertyNames.c_str() != NULL)
{
if (isStart)
{
strcat(parameters, "{");
isStart = false;
}
else
{
strcat(parameters, ",{");
}
const char * constName = propertyNames.c_str();
char* name = new char[2048];//×ã¹»³¤
strcpy(name, constName);
if (strstr(name, ",") != NULL)
{
//°´ÕÕ,½øÐвð·Ö¡£²ð·ÖÌõ¼þ
int valueCount = 0;
char ** valueChar = new char *[64];
//·Ö¸î×Ö·û´®
split(name, ",", valueChar, &valueCount);
for (int k = 0; k < valueCount; k++)
{
if (strstr(valueChar[k], "=") != NULL)
{
//°´ÕÕ-½øÐвð·Ö¡£²ð·ÖÌõ¼þ
int valueNum = 0;
char ** values = new char *[64];
//·Ö¸î×Ö·û´®
split(valueChar[k], "=", values, &valueNum);
strcat(parameters, "\"");
strcat(parameters, values[1] == NULL || strlen(values[1]) == 0 ? "" : values[1]);
strcat(parameters, "\"");
strcat(parameters, ":");
char * item_rev_value = NULL;
TCGetPropertyValue(attachments[i], "Revision", values[0], &item_rev_value);
if (strcmp(values[1], "matltype") == 0)
{
//ÎïÁÏÀàÐÍÌØÊâ´¦Àí£¬ÅжÏÊÇ·ñÊÇÁã¼þºÍÔ­¸¨²ÄÁÏ
if ((strcmp(itemType, "ML8_PartRevision") == 0) || (strcmp(itemType, "ML8_RawMaterialRevision") == 0) || (strcmp(itemType, "ML8_UnProductRevision") == 0))
{
char * processingMode = NULL;
//»ñÈ¡¼Ó¹¤·½Ê½
TCGetPropertyValue(attachments[i], "Revision", "rev.ml8_ProcessingMode", &processingMode);
printf("¼Ó¹¤·½Ê½%s================================\n", processingMode);
if (strcmp(processingMode, "×ÔÖÆ") == 0)
{
item_rev_value = "HALB";
}
else
{
item_rev_value = "ROH";
}
DOFREE(processingMode);
}
}
if (strcmp(values[1], "uom") == 0)
{
printf("µ¥Î»%s================================\n", item_rev_value);
if (item_rev_value == NULL || strcmp(item_rev_value, "") == 0)
{
item_rev_value = "ea";
}
else
{
if (strcmp(item_rev_value, "Eac") == 0 || strcmp(item_rev_value, "Each.") == 0)
{
item_rev_value = "ea";
}
}
}
if (strstr(item_rev_value, "\"") != NULL)
{
printf("¸ü¸Äǰ:%s\n", item_rev_value);
string resultString = replace_all_distinct(item_rev_value, "\"", "&\"");
char resultChar[256] = "";
strcpy(resultChar, resultString.c_str());
*(resultChar + strlen(resultString.c_str())) = '\0';
printf("¸ü¸Äºó:%s\n", resultChar);
item_rev_value = resultChar;
printf("¸ü¸Äºó:%s\n", item_rev_value);
}
strcat(parameters, "\"");
strcat(parameters, item_rev_value);
strcat(parameters, "\"");
if (k < valueCount - 1)
{
strcat(parameters, ",");
}
DOFREE(item_rev_value);
values = NULL;
}
printf("parameters£º%s\n", parameters);
}
valueChar = NULL;
}
else
{
//°´ÕÕ-½øÐвð·Ö¡£²ð·ÖÌõ¼þ
int valueCount = 0;
char ** valueChar = new char *[64];
//·Ö¸î×Ö·û´®
split(name, "=", valueChar, &valueCount);
strcat(parameters, "\"");
strcat(parameters, valueChar[1] == NULL || strlen(valueChar[1]) == 0 ? "" : valueChar[1]);
strcat(parameters, "\"");
strcat(parameters, ":");
char * item_rev_value = NULL;
TCGetPropertyValue(attachments[i], "Revision", valueChar[0], &item_rev_value);
if (strcmp(valueChar[1], "matltype") == 0)
{
//ÎïÁÏÀàÐÍÌØÊâ´¦Àí£¬ÅжÏÊÇ·ñÊÇÁã¼þºÍÔ­¸¨²ÄÁÏ
if ((strcmp(itemType, "ML8_PartRevision") == 0) || (strcmp(itemType, "ML8_RawMaterialRevision") == 0) || (strcmp(itemType, "ML8_UnProductRevision") == 0))
{
char * processingMode = NULL;
//»ñÈ¡¼Ó¹¤·½Ê½
TCGetPropertyValue(attachments[i], "Revision", "rev.ml8_ProcessingMode", &processingMode);
printf("¼Ó¹¤·½Ê½%s================================\n", processingMode);
if (strcmp(processingMode, "×ÔÖÆ") == 0)
{
item_rev_value = "HALB";
}
else
{
item_rev_value = "ROH";
}
DOFREE(processingMode);
}
}
if (strcmp(valueChar[1], "uom") == 0)
{
if (item_rev_value == NULL || strcmp(item_rev_value, "") == 0)
{
item_rev_value = "ea";
}
else
{
if (strcmp(item_rev_value, "Eac") == 0 || strcmp(item_rev_value, "Each.") == 0)
{
item_rev_value = "ea";
}
}
}
if (strstr(item_rev_value, "\"") != NULL)
{
printf("¸ü¸Äǰ:%s\n", item_rev_value);
string resultString = replace_all_distinct(item_rev_value, "\"", "&\"");
char resultChar[256] = "";
strcpy(resultChar, resultString.c_str());
*(resultChar + strlen(resultString.c_str())) = '\0';
printf("¸ü¸Äºó:%s\n", resultChar);
item_rev_value = resultChar;
printf("¸ü¸Äºó:%s\n", item_rev_value);
}
strcat(parameters, "\"");
strcat(parameters, item_rev_value);
strcat(parameters, "\"");
DOFREE(item_rev_value);
valueChar = NULL;
}
strcat(parameters, "}");
DOFREE(constName);
name = NULL;
}
}
DOFREE(itemType);
}
strcat(parameters, "]");
//»ñÈ¡µ±Ç°Ê±¼ä
time_t now = time(0);
tm *p = localtime(&now);
char date[128] = "";
sprintf_s(date, "%04d%02d%02d%02d%02d%02d", 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
//°ÑÊý¾ÝÓÃдÈëÎļþ
char data_file[SS_MAXPATHLEN] = "";
strcat(data_file, tc_root_file);
strcat(data_file, "\\");
strcat(data_file, date);
strcat(data_file, ".txt");
ofstream file;
file.open(data_file);
file << parameters << endl; // ʹÓÃÓëcoutͬÑùµÄ·½Ê½½øÐÐдÈë
file.close();
//cmdÖ¸Áî
char cmd[256] = "";
strcpy(cmd, "java -jar \"");
//strcat(cmd, jar_file);
strcat(cmd, tc_root);
strcat(cmd, "\\portal\\plugins\\");
strcat(cmd, "ML_SendSap.jar");
strcat(cmd, "\" ");
//´«²Î
cout << data_file << endl;
strcat(cmd, data_file);
if (strcmp(timeout, "") != 0)
{
strcat(cmd, " ");
strcat(cmd, timeout);
}
else
{
strcat(cmd, " 600");
}
//ÓÃÀ´´«µÝ±¾Á÷³ÌµÄÁ÷³ÌÃû³Æ(@·Ö¸î)
//strcat(cmd,"@");
//strcat(cmd,handler_name);
string strResult;
int timeout1 = 600; // ÉèÖó¬Ê±Ê±¼äΪ600Ãë
int exitCode = executeCommand(cmd, strResult, timeout1);
std::cout << "Command output: " << strResult.c_str() << std::endl;
cout << strResult << endl;
unsigned int iSize = strResult.size();
if (iSize > 0 && strResult[iSize - 1] == '\n' && strlen(parameters) > 0||strcmp(strResult.c_str(),"³¬Ê±") == 0)
{
if (strstr(strResult.c_str(), "Connect to SAP gateway failed") != NULL)
{
strResult = "Á¬½ÓSAPʧ°Ü,ÇëÁªÏµSAP¹ÜÀíÔ±´¦Àí£¡";
}
printf("Ï·¢Ê§°Ü\n");
cout << strResult << endl;
//·¢ËÍÏûÏ¢µ½Æóҵ΢ÐÅ
for (auto messageUserValue : messageUserValues) {
itemSendToWeixin(messageUserValue, rootTask, strResult);
}
if (strcmp(messageResult, "1") == 0)
{
ifail = 1;
}
}
else
{
POM_AM__set_application_bypass(TRUE);
//»ñÈ¡Ê״η¢ËÍʱ¼ä
for (int i = 0; i < attachments_num; i++)
{
char *itemType = NULL;
AOM_ask_value_string(attachments[i], "object_type", &itemType);
//¹ýÂ˵ô·Ç°æ±¾µÄ¶ÔÏó
if ((strcmp(itemType, "ML8_RefrigeratorRevision") == 0) || (strcmp(itemType, "ML8_WashingRevision") == 0)
|| (strcmp(itemType, "ML8_ColdRevision") == 0) || (strcmp(itemType, "ML8_OthersRevision") == 0) || (strcmp(itemType, "ML8_PartRevision") == 0)
|| (strcmp(itemType, "ML8_RawMaterialRevision") == 0) || (strcmp(itemType, "ML8_UnProductRevision") == 0) || (strcmp(itemType, "ML8_ProductOthRevision") == 0))
{
char * sendTime = NULL;
AOM_ask_value_string(attachments[i], "ml8_PropertyFirstTime", &sendTime);
//»ñÈ¡µ±Ç°Ê±¼ä
time_t now = time(0);
tm *p = localtime(&now);
char dateNow[128] = "";
sprintf_s(dateNow, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
AOM_lock(attachments[i]);
if (sendTime == NULL || strcmp(sendTime, "") == 0)
{
AOM_set_value_string(attachments[i], "ml8_PropertyFirstTime", dateNow);
}
else
{
AOM_set_value_string(attachments[i], "ml8_PropertyNewTime", dateNow);
}
AOM_save(attachments[i]);
AOM_unlock(attachments[i]);
AOM_refresh(attachments[i], false);
DOFREE(sendTime);
}
DOFREE(itemType);
}
POM_AM__set_application_bypass(FALSE);
}
messageUserValues.clear();
vector<char *>().swap(messageUserValues);
MEM_free(attachments);
tc_root_file = NULL;
pref_vals = NULL;
if (ifail == 1)
{
EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, strResult.c_str());
}
if (strcmp(strResult.c_str(), "³¬Ê±") == 0)
{
ifail = 1;
EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "δ½ÓÊÕµ½ERP·µ»ØÐÅÏ¢£¬ÇëÁªÏµ¹ÜÀíÔ±´¦Àí£¡");
}
auto stopTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
//std::cout << "ML_SendSapItemÓÃʱ:" << duration.count() / 1000 << std::endl;
string usetime = "ML_SendSapItemÓÃʱ:";
usetime.append(std::to_string(duration.count() / 1000));
WriteLog(true, usetime.c_str());
printf("=========================¶ÔÏóÏ·¢µ½SAP End===================\n");
return ifail;
}