#include "pch.h"
#include "LCMESClass.h"
#include <QDateTime>
#include <mmsystem.h>
extern CGlobalFunction m_GFunction; // 全局函数
extern Loger m_Loger; // 全局Log类
extern CGlobalVariant m_GVariant; // 全局变量
extern CDlgDevError m_dlgError; // 错误信息对话框
extern CActionFlow m_GActionFlow; // 主动作流程
extern CAction_Station3Tester m_GActionTester; // 测试工位流程
#pragma comment(lib, "winmm.lib")
LCMESClass* LCMESClass::m_pInstance = nullptr;
LCMESClass* LCMESClass::GetInstance()
{
if (m_pInstance == nullptr) // 错误:m_pInstance 未声明
{
m_pInstance = new LCMESClass();
}
return m_pInstance;
}
LCMESClass::LCMESClass()
{
updataRun = false;
StartWorkerThread();
}
LCMESClass::~LCMESClass()
{
StopWorkerThread();
StopTimer();
}
void LCMESClass::StartWorkerThread()
{
if (m_pWorkerThread == nullptr)
{
m_pWorkerThread = AfxBeginThread(WorkerThreadProc, this);
}
}
void LCMESClass::StopWorkerThread()
{
if (m_pWorkerThread != nullptr)
{
m_bStopThread = true;
m_taskEvent.SetEvent(); // 唤醒线程退出
WaitForSingleObject(m_pWorkerThread->m_hThread, INFINITE);
m_pWorkerThread = nullptr;
}
}
void LCMESClass::PostTask(TaskType type, const CString& param, const CString& param2)
{
if (m_GVariant.m_MesConfig->Enable == 0)
{
return;
}
TaskItem* pItem = new TaskItem();
pItem->type = type;
pItem->param = param;
pItem->param2 = param2;
{
//CriticalSectionLock lock(m_cs); // 使用自定义锁
m_taskList.AddTail(pItem);
}
m_taskEvent.SetEvent(); // 触发事件
}
UINT LCMESClass::WorkerThreadProc(LPVOID pParam)
{
LCMESClass* pThis = (LCMESClass*)pParam;
while (!pThis->m_bStopThread)
{
DWORD dwWait = WaitForSingleObject(pThis->m_taskEvent.m_hObject, INFINITE);
if (dwWait != WAIT_OBJECT_0)
continue;
if (pThis->m_bStopThread)
break;
TaskItem* pItem = nullptr;
{
CriticalSectionLock lock(pThis->m_cs); // 使用自定义锁
if (!pThis->m_taskList.IsEmpty())
{
pItem = pThis->m_taskList.RemoveHead();
}
}
if (pItem)
{
pThis->ProcessTask(pItem);
delete pItem;
}
}
return 0;
}
void LCMESClass::ProcessTask(TaskItem* pItem)
{
switch (pItem->type)
{
case TASK_EVENT_PRODUCT_MOVED:
ExecuteProductMoved(_ttoi(pItem->param), m_GFunction.CString2String(pItem->param2));
break;
case TASK_EVENT_DEVICE_STATE_CHANGED:
ExecuteDeviceStateChanged(_ttoi(pItem->param));
break;
case TASK_EVENT_TEST_END:
ExecuteTestEnd(m_GFunction.CString2String(pItem->param));
break;
case TASK_STATISTICS:
ExecuteStatistics();
case TASK_ALARM_CLEAR:
ExecuteAlarmClear();
break;
}
}
void LCMESClass::ExecuteProductMoved(const int & state, const string& sn)
{
this->ProductMoved(static_cast<ProductPosition>(state), sn);
}
void LCMESClass::ExecuteDeviceStateChanged(int state)
{
DeviceState deviceState = static_cast<DeviceState>(state);
this->DeviceStateChanged(deviceState);
}
void LCMESClass::ExecuteTestEnd(string sn)
{
this->TestEnd(sn);
}
void LCMESClass::ExecuteStatistics()
{
CriticalSectionLock lock(m_cs);
// 发送统计数量总计和OK品数量
this->sendStatistics();
}
void LCMESClass::ExecuteAlarmClear()
{
CriticalSectionLock lock(m_cs);
this->DeviceAlarmCleared();
}
// 定时五分钟发送统计数量总计和OK品数量
void LCMESClass::StartTimer(UINT interval)
{
if (m_GVariant.m_MesConfig->Enable == 0)
{
return;
}
if (m_timerID == 0)
{
m_timerID = timeSetEvent(
interval,
10,
TimerProc,
(DWORD_PTR)this,
TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
}
}
void LCMESClass::StopTimer()
{
// 停止定时器(需保存 timer ID)
if (m_timerID != 0)
{
timeKillEvent(m_timerID);
m_timerID = 0;
}
}
void CALLBACK LCMESClass::TimerProc(UINT uID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
LCMESClass* pThis = (LCMESClass*)dwUser;
if (pThis)
{
pThis->PostTask(TASK_STATISTICS);
}
}
// 产品check
Response LCMESClass::EquipmentTextData_Check(string info)
{
CriticalSectionLock lock(m_cs); // 使用类成员 CCriticalSection m_cs;
// 原始配置
string hostname = m_GVariant.m_MesConfig->Host;
int port = m_GVariant.m_MesConfig->Port;
string api = m_GVariant.m_MesConfig->Check;
Response resp = Send(info, hostname, port, api);
if (SUCCESS == resp.Code)
{
return ParseRcv(resp.Msg);
}
return resp;
}
// 产品过站
Response LCMESClass::EquipmentTextData_Save(string info)
{
// 原始配置
string hostname = m_GVariant.m_MesConfig->Host;
int port = m_GVariant.m_MesConfig->Port;
string api = m_GVariant.m_MesConfig->Check;
Response resp = Send(info, hostname, port, api);
if (SUCCESS == resp.Code)
{
return ParseRcv(resp.Msg);
}
return resp;
}
// 产品生产过程事件、配方比对
Response LCMESClass::deviceInfoCollerct(string info)
{
// 原始配置
string hostname = m_GVariant.m_MesConfig->Host;
int port = m_GVariant.m_MesConfig->Port;
string api = m_GVariant.m_MesConfig->Check;
Response resp = Send(info, hostname, port, api);
if (SUCCESS == resp.Code)
{
return ParseRcv(resp.Msg);
}
return resp;
}
void LCMESClass::Connect()
{
CriticalSectionLock lock(m_cs); // 使用类成员 CCriticalSection m_cs;
if (m_GVariant.m_MesConfig->Enable == 0)
{
m_connectionState = ConnectionState::Disconnected;
return;
}
std::string hostname = m_GVariant.m_MesConfig->Host;
int port = m_GVariant.m_MesConfig->Port;
// 释放旧连接(如果存在)
if (m_pConnection)
{
delete m_pConnection;
m_pConnection = nullptr;
}
// 设置全局超时参数
HINTERNET hSession = (HINTERNET)m_Session; // ✅ 正确获取 HINTERNET 句柄
if (hSession)
{
DWORD connectTimeout = 5000; // 5 秒
DWORD sendTimeout = m_GVariant.m_MesConfig->SendTimeout;
DWORD recvTimeout = m_GVariant.m_MesConfig->RecvTimeout;
InternetSetOption(hSession, INTERNET_OPTION_CONNECT_TIMEOUT, &connectTimeout, sizeof(connectTimeout));
InternetSetOption(hSession, INTERNET_OPTION_SEND_TIMEOUT, &sendTimeout, sizeof(sendTimeout));
InternetSetOption(hSession, INTERNET_OPTION_RECEIVE_TIMEOUT, &recvTimeout, sizeof(recvTimeout));
}
try {
// 创建新连接
m_pConnection = m_Session.GetHttpConnection(
m_GFunction.String2CString(m_GVariant.m_MesConfig->Host),
m_GVariant.m_MesConfig->Port,
_T(""), // username
_T("") // password
);
if (m_pConnection)
{
m_connectionState = ConnectionState::Connected;
CString logMsg;
logMsg.Format(_T("MES连接成功: %s:%d"),
m_GFunction.String2CString(m_GVariant.m_MesConfig->Host), m_GVariant.m_MesConfig->Port);
addInfo(m_GFunction.CString2String(logMsg));
}
else
{
m_connectionState = ConnectionState::Error;
addErrInfo("MES连接失败: 无法创建HTTP连接");
}
}
catch (CInternetException* pEx)
{
TCHAR szError[1024];
pEx->GetErrorMessage(szError, 1024);
m_connectionState = ConnectionState::Error;
addErrInfo("MES连接异常: " + m_GFunction.CString2String(szError));
pEx->Delete();
}
}
Response LCMESClass::Send(const string & info, const string & hostname, const int & port, const string & apiPath)
{
CriticalSectionLock lock(m_cs);
// 检查连接状态
if (m_connectionState != ConnectionState::Connected)
{
Connect(); // 尝试重连
// 重连后仍然失败
if (m_connectionState != ConnectionState::Connected)
{
Response resp;
resp.Code = NETWORK_ERROR;
resp.Msg = "MES connection not established";
return resp;
}
}
Response resp;
if (!m_pConnection)
{
resp.Code = NETWORK_ERROR;
resp.Msg = "Not connected to server. Call Connect() first.";
return resp;
}
try {
auto deleter = [](CHttpFile* p) { if (p) delete p; };
std::unique_ptr<CHttpFile, decltype(deleter)> pFile(
m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,
m_GFunction.String2CString(apiPath)), deleter);
CString strHeaders = _T("Content-Type: application/json");
CString strData = m_GFunction.String2CString(info);
pFile->SendRequest
(
strHeaders,
(DWORD)strHeaders.GetLength(),
(LPVOID)(LPCTSTR)strData,
(DWORD)strData.GetLength()
);
DWORD dwStatusCode;
pFile->QueryInfoStatusCode(dwStatusCode);
CString strResponse;
if (dwStatusCode == HTTP_STATUS_OK)
{
TCHAR szBuffer[1024];
while (pFile->Read(szBuffer, 1023) > 0)
{
strResponse += szBuffer;
}
}
if (dwStatusCode != HTTP_STATUS_OK)
{
resp.Code = HTTP_ERROR;
resp.Msg = std::to_string(dwStatusCode);
return resp;
}
resp.Msg = m_GFunction.CString2String(strResponse);
pFile->Close();
}
catch (CInternetException* pEx)
{
TCHAR szError[1024];
pEx->GetErrorMessage(szError, 1024);
resp.Msg = m_GFunction.CString2String(szError);// 异常信息写入resultMSG
pEx->Delete();
resp.Code = NETWORK_ERROR;
return resp;
}
resp.Code = SUCCESS;
return resp;
}
void LCMESClass::PullMes()
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
if (m_GVariant.m_MesConfig->Enable == 0)
{
return;
}
sSC_PullMes.Init();
updataRun = true;
while (true)
{
int iRtn = FL_PullMes();
if (-1 == iRtn)
{
updataRun = false;
break;
}
Sleep(5);
}
}
// 添加连接状态查询方法(公共接口)
ConnectionState LCMESClass::GetConnectionState() const
{
return m_connectionState;
}
int LCMESClass::Check(string sn)
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
if (m_GVariant.m_MesConfig->Enable == 0)
{
return 1;
}
Check_MesInfo checkInof;
checkInof.EquipmentEncode = m_GVariant.m_MesConfig->m_MesInfo.EquipmentEncode;
checkInof.SpecificationName = m_GVariant.m_MesConfig->m_MesInfo.SpecificationName;
checkInof.UserName = m_GVariant.m_MesConfig->m_MesInfo.UserName;
checkInof.SN = QString::fromLocal8Bit(sn.c_str());
QJsonObject jsonObj;
serializeToJsonFile(checkInof, jsonObj);
QJsonDocument doc(jsonObj);
QString jsonMesInfo = doc.toJson(QJsonDocument::Compact);
jsonMesInfo = jsonMesInfo.replace("/", "//");
jsonMesInfo = jsonMesInfo.replace("\"", "\\\"");
jsonMesInfo = "{\"jsonData\":\"" + jsonMesInfo + "\"}";
jsonMesInfo = jsonMesInfo.replace("\r", "");
string info = jsonMesInfo.toLocal8Bit();
Response res = EquipmentTextData_Check(info);
if (res.Code == 0 || res.Result == "OK")
{
return 1;
}
else
{
addErrInfo(res.Msg);
return -1;
}
}
int LCMESClass::getDeviceInfoCollerctResult(Device_MesInfo& mes)
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
mes.EquipmentEncode = m_GVariant.m_MesConfig->m_MesInfo.EquipmentEncode;
mes.type = 1;
mes.SpecificationName = m_GVariant.m_MesConfig->m_MesInfo.SpecificationName;
mes.UserName = m_GVariant.m_MesConfig->m_MesInfo.UserName;
mes.DocumentMD5 = QString::fromLocal8Bit(m_GActionTester.md5forconfig.c_str());
mes.deviceParams.Total = m_GActionFlow.m_Statistics.m_lTotal;
mes.deviceParams.OKCount = (m_GActionFlow.m_Statistics.m_lBest + m_GActionFlow.m_Statistics.m_lMed + m_GActionFlow.m_Statistics.m_lPass);
QJsonObject jsonObj;
serializeToJsonFile(mes, jsonObj);
QJsonDocument doc(jsonObj);
QString jsonMesInfo = doc.toJson(QJsonDocument::Compact);
jsonMesInfo = jsonMesInfo.replace("/", "//");
jsonMesInfo = jsonMesInfo.replace("\"", "\\\"");
jsonMesInfo = "{\"jsonData\":\"" + jsonMesInfo + "\"}";
jsonMesInfo = jsonMesInfo.replace("\r", "");
string info = jsonMesInfo.toLocal8Bit();
Response res = deviceInfoCollerct(info);
if (res.Code == 0 || res.Result == "OK")
{
return 1;
}
else
{
addErrInfo(res.Msg);
return -1;
}
return 0;
}
int LCMESClass::DeviceStateChanged(DeviceState state)
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
string eventDesc = "";
int deviceStatus = 0;
switch (state)
{
case Waiting:
eventDesc = "待机";
deviceStatus = 1;
break;
case Wroking:
eventDesc = "作业";
deviceStatus = 2;
break;
case Faulted:
eventDesc = "故障";
deviceStatus = 3;
break;
case Stopped:
eventDesc = "关机";
deviceStatus = 4;
break;
default:
eventDesc = "开机";
deviceStatus = 0;
break;
}
Device_MesInfo mes;
mes.eventName = QString::fromLocal8Bit("设备状态");
mes.eventDesc = QString::fromLocal8Bit(eventDesc.c_str());
mes.deviceStatus = deviceStatus;
return getDeviceInfoCollerctResult(mes);
}
// 设备状态解除事件
int LCMESClass::DeviceAlarmCleared()
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
Device_MesInfo mes;
mes.eventName = QString::fromLocal8Bit("报警解除");
mes.eventDesc = QString::fromLocal8Bit("设备报警状态已被人工确认后解除,恢复待机");
mes.deviceStatus = 1; // 待机1
return getDeviceInfoCollerctResult(mes);
}
// 产品移动事件
int LCMESClass::ProductMoved(ProductPosition ProductPosition, string sn)
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
string value = "未知";
switch (ProductPosition)
{
case In:
value = "接料";
break;
case Out:
value = "出料";
break;
default:
value = "未知";
break;
}
Device_MesInfo mes;
mes.eventName = QString::fromLocal8Bit("产品移动");
mes.eventDesc = QString::fromLocal8Bit(value.c_str()); // 接料/出料
mes.deviceStatus = 2; // 作业2
mes.deviceParams.SN = QString::fromLocal8Bit(sn.c_str());
return getDeviceInfoCollerctResult(mes);
}
// 测试完成发送完工报告事件
int LCMESClass::TestEnd(string sn)
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
Device_MesInfo mes;
mes.eventName = QString::fromLocal8Bit("测试完成");
mes.eventDesc = QString::fromLocal8Bit("测试软件完成产品测试");
mes.deviceStatus = 2; // 作业2
mes.deviceParams.SN = QString::fromLocal8Bit(sn.c_str());
return getDeviceInfoCollerctResult(mes);
}
int LCMESClass::sendStatistics()
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
Device_MesInfo mes;
mes.eventName = QString::fromLocal8Bit("生产产品数量");
mes.eventDesc = QString::fromLocal8Bit("统计所有物料和OK物料的数量");
return getDeviceInfoCollerctResult(mes);
}
Response LCMESClass::ParseRcv(string& msg)
{
json j = json::parse(msg);
return Response::from_json(j);
}
int LCMESClass::FL_PullMes()
{
int iRtn = 0;
switch (sSC_PullMes.nStep)
{
case 0:
addInfo(sSC_PullMes, 5, "开始巡检MES目录", true);
return 0;
case 5:
iRtn = checkFolder();
if (-1 == iRtn)
{
addInfo(sSC_PullMes, ST_ERR, "巡检MES目录失败");
}
if (1 == iRtn)
{
sSC_PullMes.nStep = 10;
}
else
{
sSC_PullMes.SetDelay(5, 1000);
}
return 0;
case 10:
if (m_File.checkFileExist(m_destinationFolder, m_currentFileNames))
{
sSC_PullMes.nStep = 15;
//addInfo(sSC_PullMes, 10, "检查所有文件都剪切完成;准备解析文件");
}
else
{
addInfo(sSC_PullMes, ST_ERR, "检查MES文件是否存在失败");
}
return 0;
case 15:
if (m_bufferFileNames.size() == 0)
{
sSC_PullMes.nStep = ST_END;
}
else
{
sSC_PullMes.nStep = 20;
}
return 0;
case 20:
if (m_iItemIndex <= m_bufferFileNames.size() - 1)
{
m_qstrRemoveFile = m_bufferFileNames[m_iItemIndex];
QString tempFile = m_destinationFolder + "\\" + m_qstrRemoveFile;
if (deserializeFromJsonFile(tempFile, m_MesInfo))
{
if (m_MesInfo.Barcode == "" || m_MesInfo.Barcode.length() < 5)
{
m_iItemIndex = 0;//将索引归零
addInfo(sSC_PullMes, 15, "条码长度小于5:条码为" + (string)m_MesInfo.Barcode.toLocal8Bit());
m_bufferFileNames.removeOne(m_bufferFileNames[m_iItemIndex]);
}
else
{
m_iItemIndex++;
m_qqmesInfos.append(m_MesInfo);
if (1 == m_GVariant.m_MesConfig->TestAngleCount)
{
addInfo(sSC_PullMes, 25, "准备上传单角度数据");
}
else
{
if (m_qqmesInfos.size() == 1)
{
m_AllRemoveFileNames.append(m_bufferFileNames[m_iItemIndex]);
m_bufferFileNames.removeOne(m_bufferFileNames[m_iItemIndex]);
m_iItemIndex = 0;
addInfo(sSC_PullMes, 15, "上传双角度,但是只有角度1数据,继续寻找角度2数据文件");
}
else if (m_qqmesInfos.size() == 2)
{
addInfo(sSC_PullMes, 30, "准备上传双角度数据");
}
}
}
}
else
{
addInfo(sSC_PullMes, ST_ERR, "文件" + (string)tempFile.toLocal8Bit() + "json序列化失败");
}
}
else
{
addInfo(sSC_PullMes, ST_END, "缓存文件已上传完毕");
}
return 0;
case 25:
iRtn = updataMes(m_MesInfo, m_GVariant.m_MesConfig->m_MesInfo);
if (1 == iRtn)
{
//清空队列中的
m_bufferFileNames.removeOne(m_qstrRemoveFile);
m_qqmesInfos.clear();
m_iItemIndex = 0;//将索引归零
addInfo(sSC_PullMes, 15, "单角度上传完成,开始缓存新MES数据");
}
else if (-1 == iRtn)
{
sSC_PullMes.nStep = ST_ERR;
}
return 0;
case 30:
if (m_qqmesInfos[0].Barcode == m_qqmesInfos[1].Barcode)
{
m_AllRemoveFileNames.append(m_qstrRemoveFile);
iCount = 0;
addInfo(sSC_PullMes, 35, "比对2次条码一致,准备合并上传");
}
else
{
m_qqmesInfos.removeAt(1);
iCount++;
addInfo(sSC_PullMes, 15, "比对2次条码不一致,继续寻找相同条码文件");
if (iCount >= 2)
{
iCount = 0;
m_AllRemoveFileNames.clear();
m_qqmesInfos.clear();
m_iItemIndex = 0;//将索引归零
}
}
return 0;
case 35:
iRtn = updataMes(m_qqmesInfos, m_GVariant.m_MesConfig->m_MesInfo);
if (-1 == iRtn)
{
sSC_PullMes.nStep = ST_ERR;
}
if (1 == iRtn)
{
m_bufferFileNames.removeOne(m_AllRemoveFileNames[0]);
m_bufferFileNames.removeOne(m_AllRemoveFileNames[1]);
m_qqmesInfos.clear();
m_AllRemoveFileNames.clear();
m_iItemIndex = 0;
addInfo(sSC_PullMes, 15, "双角度合并上传完成");
}
return 0;
case ST_DELAY:
if (GetTickCount() - sSC_PullMes.dwStart >= sSC_PullMes.dwDelay)
{
sSC_PullMes.nStep = sSC_PullMes.nStepNext;
}
return 0;
case ST_ERR:
return -1;
case ST_END:
sSC_PullMes.ulCountEnd = m_GFunction.GetCPUPerformanceCounter();
sSC_PullMes.dCycleTime = (sSC_PullMes.ulCountEnd - sSC_PullMes.ulCountStart) / 1000.0;
sSC_PullMes.AddCTRecordEnd(m_Loger.m_iRecordCycleTime, "Handler流程初始化执行结束", sSC_PullMes.dCycleTime);
if (1 == m_Loger.m_iRecordCycleTime)
m_Loger.RecordLogOperation(sSC_PullMes.strRecord);// 添加到缓存变量
sSC_PullMes.Init();
return 1;
default:
sSC_PullMes.Init();
break;
}
return 0;
}
int LCMESClass::checkFolder()
{
CriticalSectionLock lock(m_cs); // ✅ 加锁
m_currentFileNames.clear();
QString m_sourceFolder = QString::fromLocal8Bit(m_GVariant.m_MesConfig->Path_SourceTestFile.c_str()) + "\\MES" + QDateTime::currentDateTime().addDays(-1).toString("yyyyMMdd");
m_destinationFolder = QString::fromLocal8Bit(m_GVariant.m_MesConfig->Path_DestTestFile.c_str()) + "\\" + QDateTime::currentDateTime().addDays(-1).toString("yyyyMMdd");
bool result1 = m_File.moveAllFiles(m_sourceFolder, m_destinationFolder, m_currentFileNames);
m_sourceFolder = QString::fromLocal8Bit(m_GVariant.m_MesConfig->Path_SourceTestFile.c_str()) + "\\MES" + QDateTime::currentDateTime().toString("yyyyMMdd");
m_destinationFolder = QString::fromLocal8Bit(m_GVariant.m_MesConfig->Path_DestTestFile.c_str()) + "\\" + QDateTime::currentDateTime().toString("yyyyMMdd");
bool result2 = m_File.moveAllFiles(m_sourceFolder, m_destinationFolder, m_currentFileNames);
if (result1 || result2)
{
if (m_currentFileNames.size() > 0)
{
for (auto item : m_currentFileNames)
{
m_bufferFileNames.append(item);
}
return 1;
}
}
else
{
return -1;
}
return 0;
}
int LCMESClass::updataMes(TestData testData, MesInfo mesInfo)
{
mesInfo.SN = testData.Barcode;
mesInfo.LastResult = testData.Result.Aggregate;
mesInfo.TestDate = testData.Basic.Time;
// 重新排序处理
QString tempString = ShortLCMesInfo(testData);
QJsonObject jsonObj2;
serializeToJsonFile(mesInfo, jsonObj2);
QJsonDocument doc(jsonObj2);
QString jsonString = doc.toJson(QJsonDocument::Compact);
QStringList list = jsonString.split(",");
list.insert(5, tempString);
QString jsonMesInfo = list.join(",");;
jsonMesInfo = jsonMesInfo.replace("/", "//");
jsonMesInfo = jsonMesInfo.replace("\"", "\\\"");
jsonMesInfo = "{\"jsonData\":\"" + jsonMesInfo + "\"}";
string info = jsonMesInfo.toLocal8Bit();
Response res = EquipmentTextData_Save(info);
if (res.Code == 0 || res.Result == "OK")
{
return 1;
}
else
{
addInfo(res.Msg);
return -1;
}
return 0;
}
int LCMESClass::updataMes(QQueue<TestData> testData, MesInfo mesInfo)
{
QString tempString = ShortLCMesInfo(testData[0]);
tempString.chop(1);
mesInfo.SN = testData[0].Barcode;
QString firstResult = testData[0].Result.Final;
QString secoundResult = testData[1].Result.Final;
if (firstResult == "FAIL" || secoundResult == "FAIL")
{
mesInfo.LastResult = "FAIL";
}
else
{
mesInfo.LastResult = "PASS";
}
mesInfo.TestDate = testData[0].Basic.Time;
QString tempString2 = ShortLCMesInfo(testData[1]);
tempString2.replace("\"TestData\":{", "");
tempString = tempString + "," + tempString2;
QJsonObject jsonObject3;
serializeToJsonFile(mesInfo, jsonObject3);
QJsonDocument doc(jsonObject3);
QString jsonString2 = doc.toJson(QJsonDocument::Compact);
QStringList list = jsonString2.split(",");
list.insert(5, tempString);
QString jsonMesInfo = list.join(",");
jsonMesInfo = jsonMesInfo.replace("/", "//");
jsonMesInfo = jsonMesInfo.replace("\"", "\\\"");
jsonMesInfo = "{\"jsonData\":\"" + jsonMesInfo + "\"}";
string info = jsonMesInfo.toLocal8Bit();
Response res = EquipmentTextData_Save(info);
if (res.Code == 0 || res.Result == "OK")
{
return 1;
}
else
{
addInfo(res.Msg);
return -1;
}
return 0;
}
QString LCMESClass::ShortLCMesInfo(TestData testData)
{
QString key = "";
QString value = "";
QString tempString = "\"TestData\":{\"";
SplicingEFL(testData, tempString);
SplicingFFL(testData, tempString);
key = "FinalMTFResult";
value = testData.Result.Aggregate;
tempString += (key + "\":\"" + value + "\",\"");
SplicingMTF(testData, tempString);
key = "Angle";
value = testData.Normal.Tilt.Angle;
tempString += (key + "\":\"" + value + "\",\"");
SplicingFocalShfit(testData, tempString);
SplicingPeak(testData, tempString);
SplicingFOV(testData, tempString);
SplicingDOF(testData, tempString);
SplicingMultiFrqMTF(testData, tempString);
key = "SymmetricalGroupAverageCAM2_5TAN";
try
{
value = testData.Normal.SymmetricalFieldMTF.at(1);
}
catch (const std::exception&)
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "AstigmatismCameras1";
try
{
value = testData.Normal.Astigmatism.first();
}
catch (const std::exception&)
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Symmetrical_Group_Ml_(Max-in)_CAM10..13TAN";
if (testData.Normal.SymmetricalGroupMTF.size() > 2)
{
value = testData.Normal.SymmetricalGroupMTF.at(2).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\"}");
return tempString;
}
QString LCMESClass::SplicingEFL(TestData testData, QString & tempString)
{
QString key = "EFL";
QString value = testData.Normal.EFL;
tempString += (key + "\":\"" + value + "\",\"");
return tempString;
}
QString LCMESClass::SplicingFFL(TestData testData, QString & tempString)
{
QString key = "FFL";
QString value = testData.Normal.FFL;
tempString += (key + "\":\"" + value + "\",\"");
return tempString;
}
QString LCMESClass::SplicingMTF(TestData testData, QString & tempString)
{
QString key = "";
QString value = "";
key = "Result";
value = testData.Result.MTF;
tempString += (key + "\":\"" + value + "\",\"");
for (int i = 0; i < testData.Normal.MTF.size(); i++)
{
key = "S" + QString::number(i + 1);
value = testData.Normal.MTF.at(i).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T" + QString::number(i + 1);
value = testData.Normal.MTF.at(i).Tan;
tempString += (key + "\":\"" + value + "\",\"");
}
return tempString;
}
QString LCMESClass::SplicingFocalShfit(TestData testData, QString & tempString)
{
QString key = "FocalShift_S2";
QString value = testData.Normal.FocalShift.Cameras.at(1).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T2";
value = testData.Normal.FocalShift.Cameras.at(1).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S3";
value = testData.Normal.FocalShift.Cameras.at(2).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T3";
value = testData.Normal.FocalShift.Cameras.at(2).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S4";
value = testData.Normal.FocalShift.Cameras.at(3).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T4";
value = testData.Normal.FocalShift.Cameras.at(3).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S5";
value = testData.Normal.FocalShift.Cameras.at(4).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T5";
value = testData.Normal.FocalShift.Cameras.at(4).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S6";
value = testData.Normal.FocalShift.Cameras.at(5).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T6";
value = testData.Normal.FocalShift.Cameras.at(5).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S7";
value = testData.Normal.FocalShift.Cameras.at(6).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T7";
value = testData.Normal.FocalShift.Cameras.at(6).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S8";
value = testData.Normal.FocalShift.Cameras.at(7).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T8";
value = testData.Normal.FocalShift.Cameras.at(7).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S9";
value = testData.Normal.FocalShift.Cameras.at(8).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T9";
value = testData.Normal.FocalShift.Cameras.at(8).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S10";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(9).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T10";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(9).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S11";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(10).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T11";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(10).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S12";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(11).Sag;
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T12";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(11).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_S13";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(12).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FocalShift_T13";
if (testData.Normal.FocalShift.Cameras.size() > 9)
{
value = testData.Normal.FocalShift.Cameras.at(12).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Focal_Shift_Group_CAM_2..5_SAG";
if (testData.Normal.FocalShiftGroup.size() != 0)
{
value = testData.Normal.FocalShiftGroup.at(0).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Focal_Shift_Group_CAM_2..5_TAN";
if (testData.Normal.FocalShiftGroup.size() != 0)
{
value = testData.Normal.FocalShiftGroup.at(0).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Focal_Shift_Group_CAM_6..9_SAG";
if (testData.Normal.FocalShiftGroup.size() > 1)
{
value = testData.Normal.FocalShiftGroup.at(1).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Focal_Shift_Group_CAM_6..9_TAN";
if (testData.Normal.FocalShiftGroup.size() > 1)
{
value = testData.Normal.FocalShiftGroup.at(1).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Focal_Shift_Group_CAM_10..13_SAG";
if (testData.Normal.FocalShiftGroup.size() > 2)
{
value = testData.Normal.FocalShiftGroup.at(2).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "Focal_Shift_Group_CAM_10..13_TAN";
if (testData.Normal.FocalShiftGroup.size() > 2)
{
value = testData.Normal.FocalShiftGroup.at(2).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
return tempString;
}
QString LCMESClass::SplicingPeak(TestData testData, QString & tempString)
{
QString key = "S1PEAK";
QString value = testData.Normal.MTFPeaks.at(0).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T1PEAK";
value = testData.Normal.MTFPeaks.at(0).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S2PEAK";
value = testData.Normal.MTFPeaks.at(1).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T2PEAK";
value = testData.Normal.MTFPeaks.at(1).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S3PEAK";
value = testData.Normal.MTFPeaks.at(2).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T3PEAK";
value = testData.Normal.MTFPeaks.at(2).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S4PEAK";
value = testData.Normal.MTFPeaks.at(3).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T4PEAK";
value = testData.Normal.MTFPeaks.at(3).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S5PEAK";
value = testData.Normal.MTFPeaks.at(4).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T5PEAK";
value = testData.Normal.MTFPeaks.at(4).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S6PEAK";
value = testData.Normal.MTFPeaks.at(5).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T6PEAK";
value = testData.Normal.MTFPeaks.at(5).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S7PEAK";
value = testData.Normal.MTFPeaks.at(6).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T7PEAK";
value = testData.Normal.MTFPeaks.at(6).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S8PEAK";
value = testData.Normal.MTFPeaks.at(7).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T8PEAK";
value = testData.Normal.MTFPeaks.at(7).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S9PEAK";
value = testData.Normal.MTFPeaks.at(8).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T9PEAK";
value = testData.Normal.MTFPeaks.at(8).Tan;
tempString += (key + "\":\"" + value + "\",\"");
key = "S10PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(9).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "T10PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(9).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "S11PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(10).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "T11PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(10).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "S12PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(11).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "T12PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(11).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "S13PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(12).Sag;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "T13PEAK";
if (testData.Normal.MTFPeaks.size() > 9)
{
value = testData.Normal.MTFPeaks.at(12).Tan;
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
return tempString;
}
QString LCMESClass::SplicingFOV(TestData testData, QString & tempString)
{
QString key = "FullFOV2_4";
QString value = "";
if (testData.Normal.FullFOV.size() != 0)
{
value = testData.Normal.FullFOV.at(0);
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FullFOV3_5";
if (testData.Normal.FullFOV.size() > 1)
{
value = testData.Normal.FullFOV.at(1);
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FullFOV6_8";
if (testData.Normal.FullFOV.size() > 2)
{
value = testData.Normal.FullFOV.at(2);
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FullFOV7_9";
if (testData.Normal.FullFOV.size() > 3)
{
value = testData.Normal.FullFOV.at(3);
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FullFOV10_12";
if (testData.Normal.FullFOV.size() > 4)
{
value = testData.Normal.FullFOV.at(4);
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "FullFOV11_13";
if (testData.Normal.FullFOV.size() > 5)
{
value = testData.Normal.FullFOV.at(5);
}
else
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
return tempString;
}
QString LCMESClass::SplicingDOF(TestData testData, QString & tempString)
{
QString key = "DOF";
QString value = testData.Normal.DOF.DOF;
tempString += (key + "\":\"" + value + "\",\"");
key = "A2DOF";
value = testData.Normal.A2DOF.A2DOF;
tempString += (key + "\":\"" + value + "\",\"");
key = "A2_DOFMinus";
try
{
value = testData.Normal.A2DOF.Minus;
}
catch (const std::exception&)
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
key = "A2_DOFPlus";
try
{
value = testData.Normal.A2DOF.Plus;
}
catch (const std::exception&)
{
value = "";
}
tempString += (key + "\":\"" + value + "\",\"");
if (testData.m_bDefocus || testData.Defocus.MTF.size() > 0)
{
// 如果有defocus数据,应该在这里就要拼接好
for (int i = 0; i < testData.Defocus.MTF.size(); i++)
{
key = "Defocus";
QStringList list = testData.Result.Defocus.split(" ");
value = list.at(i);
tempString += (key + "\":\"" + value + "\",\"");
QVector<MTFData> q_verMtfData = testData.Defocus.MTF.at(i);
for (int j = 0; j < q_verMtfData.size(); j++)
{
MTFData data = q_verMtfData.at(j);
key = "S" + QString::number(j + 1);
value = data.Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T" + QString::number(j + 1);
value = data.Tan;
tempString += (key + "\":\"" + value + "\",\"");
}
}
}
return tempString;
}
QString LCMESClass::SplicingMultiFrqMTF(TestData testData, QString & tempString)
{
if (testData.Normal.m_bMultiFrqMTF || testData.Normal.MultiFrqMTF.size() > 0)
{
QString key = "";
QString value = "";
for (int i = 0; i < testData.Normal.MultiFrqMTF.size(); i++)
{
MultiFrqMTFData data = testData.Normal.MultiFrqMTF.at(i);
key = "LstFrq";
value = data.LstFrq;
tempString += (key + "\":\"" + value + "\",\"");
key = "Result";
QStringList list = testData.Result.MultiFrqMTF.split(" ");
value = list.at(i + 1);
tempString += (key + "\":\"" + value + "\",\"");
for (int j = 0; j < data.MTF.size(); j++)
{
key = "S" + QString::number(j + 1);;
value = data.MTF.at(j).Sag;
tempString += (key + "\":\"" + value + "\",\"");
key = "T" + QString::number(j + 1);
value = data.MTF.at(j).Tan;
tempString += (key + "\":\"" + value + "\",\"");
}
}
}
return tempString;
}
QVariantList LCMESClass::jsonArrayToVariantList(const QJsonArray & array)
{
QVariantList list;
for (const QJsonValue& v : array)
{
list.append(v.toVariant());
}
return list;
}
QJsonArray LCMESClass::variantListToJsonArray(const QVariantList & list)
{
QJsonArray array;
for (const QVariant& v : list)
{
array.append(QJsonValue::fromVariant(v));
}
return array;
}
int LCMESClass::addInfo(string& info)
{
if (m_logInfo == info)
{
return 1;
}
m_logInfo = info;
m_Loger.RecordLogMes(m_logInfo);
// 发送消息,传递快照指针
if (NULL != m_hwdMainView)
{
// 使用PostMessage
CString* pMsg = new CString(CA2CT(info.c_str()));
::SendMessage(m_hwdMainView, WM_MESINFO, (WPARAM)pMsg, 0);
}
return 0;
}
int LCMESClass::addInfo(StepCtrl & crtl, int next, string info, bool blag)
{
addInfo(info);
crtl.nStep = next;
if (blag)
{
crtl.ulCountStart = m_GFunction.GetCPUPerformanceCounter();
crtl.InitRecordString();
}
if (next == ST_ERR)
{
addErrInfo(info);
}
return 0;
}
int LCMESClass::addInfo(StepCtrl & crtl, int next, string info, int delay)
{
crtl.SetDelay(next, delay);
addInfo(info);
return 0;
}
int LCMESClass::addErrInfo(string info)
{
info = "MES" + info;
m_GActionFlow.LightOpenRed();
m_dlgError.SetError(info);
m_Loger.RecordLogError(info);
AfxMessageBox(m_GFunction.String2CString(info), MB_OK | MB_ICONSTOP | MB_SYSTEMMODAL);
m_GActionFlow.LightOpenYellow();
return 0;
}
在主线程同步调用会卡死在加锁处