Files
coco df489d5640 a
2026-07-03 16:05:30 +08:00

1002 lines
32 KiB
C++

// DialTimerTaskMeasu.cpp : implementation file
//
#include "stdafx.h"
#include "geomative.h"
#include "DialTimerTaskMeasu.h"
#include "OperPLC.h"
#include "FileOperTools.h"
#include "DialTimerTask.h"
#include "Lock/AutoLock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDialTimerTaskMeasu dialog
#define MAX_TIMER_REPEAT_SND_CNT 90000
#define MODULE_NAME "CDialTimerTaskMeasu"
#define MAX_TIMER_LIST_SHOW 25
extern CGeoMativeApp theApp;
extern SYSTEMTIME g_sysCurTime;
extern void SplitterString(CStringArray &szArray,const CString& szSource, const CString& szSplitter);
extern int g_iUILanguage;
extern float tcp_ntohf(float f);
extern float tcp_htonf(float f);
extern CString GetMeasuStatusByAlarm(BYTE ucAlarm);
CDialTimerTaskMeasu::CDialTimerTaskMeasu(CWnd* pParent /*=NULL*/)
: CDialog(CDialTimerTaskMeasu::IDD, pParent)
{
//{{AFX_DATA_INIT(CDialTimerTaskMeasu)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_bIsSigMeasuring = false;
m_iRepeatSndCnt = -1;
m_vtMeasureData.clear();
m_bIsRunning = false;
m_pThread = NULL;
m_bIsThreadFinshed = true;
m_bIsOpenTbData = false;
m_ucCmd = 0;
memset(m_ucData, 0, sizeof(m_ucData));
m_iRecvDataLen = 0;
m_bIsSuspend = false;
m_uiDevID = 0;
m_ucCmd = 0;
m_bIsThreadFinshed = true;
}
void CDialTimerTaskMeasu::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDialTimerTaskMeasu)
DDX_Control(pDX, IDC_LIST_MEASU_DATA, m_listMeasuData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDialTimerTaskMeasu, CDialog)
//{{AFX_MSG_MAP(CDialTimerTaskMeasu)
ON_BN_CLICKED(IDC_BUTTON_TIMER_MRASU, OnButtonTimerMeasu)
ON_BN_CLICKED(IDC_BUTTON_MRASU_SUSPEND, OnButtonMrasuSuspend)
ON_BN_CLICKED(IDC_BUTTON_TIMER_CFG, OnButtonTimerCfg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDialTimerTaskMeasu message handlers
void CDialTimerTaskMeasu::OnButtonTimerMeasu()
{
// TODO: Add your control notification handler code here
if (!m_bIsThreadFinshed)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("请不要重复测量"));
else
MessageBoxEx(NULL, _T("Please do not repeat measure."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return;
}
if (NULL == m_pThread)
{
m_bIsRunning = true;
m_pThread = AfxBeginThread(StartMeasuData,(LPVOID)this);
if (NULL == m_pThread)
{
m_bIsRunning = false;
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("创建处理数据线程失败"));
else
MessageBoxEx(NULL, _T("Create process data thread failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return ;
}
GetDlgItem(IDC_BUTTON_TIMER_MRASU)->EnableWindow(FALSE);
}
}
void CDialTimerTaskMeasu::OnButtonMrasuSuspend()
{
// TODO: Add your control notification handler code here
if (!m_bIsSuspend)
{
m_bIsSuspend = true;
SetBtnStatus(EN_TIMER_PROC_MEASU_SUSPEND);
}
else
{
m_bIsSuspend = false;
SetBtnStatus(EN_TIMER_PROC_MEASU_CONTINUE);
}
}
void CDialTimerTaskMeasu::ThreadJoin()
{
if (NULL == m_pThread)
{
return;
}
m_bIsRunning = false;
Sleep(100);
bool bIsShow = false;
while(!m_bIsThreadFinshed)
{
Sleep(100);
}
}
bool CDialTimerTaskMeasu::ProcRcvMeasuData()
{
CString strErr;
if (m_iRecvDataLen < 1)
{
strErr.Empty();
strErr.Format(_T("[%s][%d]接收数据错误,接收字节数不能小于1"),MODULE_NAME, __LINE__);
CFileOperTools::GetInstance()->WriteComLog(strErr);
return EN_MEASU_PROC_DATA_ERR;
}
BYTE ucPacketNum = *((BYTE*)m_ucData + sizeof(m_stQueryTdInfo.ucTaskID));
if (ucPacketNum != m_vtMeasureData.size())
{
strErr.Empty();
strErr.Format(_T("[%s][%d]测量数据包数目错误,本该测量包数=%d,实测包数=%d"), MODULE_NAME, __LINE__,m_vtMeasureData.size(),ucPacketNum);
CFileOperTools::GetInstance()->WriteComLog(strErr);
return EN_MEASU_PROC_DATA_ERR;
}
LPSTMeasuBasicDataRes pMeasuBasicRes = NULL;
int iAttachLen = (1 == m_stQueryTdInfo.iTestType) ? sizeof(STMeasuDataResAttachIP) : 0;
//这里暂时没有对粘包或者分包做处理
if (m_iRecvDataLen != (1 + sizeof(m_stQueryTdInfo.ucTaskID) + ucPacketNum*(sizeof(STMeasuDataResBasic)+iAttachLen)))
{
strErr.Empty();
strErr.Format(_T("[%s][%d]测量数据包长度错误,本该=%d,实际长度=%d"), MODULE_NAME, __LINE__, 1+sizeof(m_stQueryTdInfo.ucTaskID)+ucPacketNum*(sizeof(STMeasuDataResBasic)+iAttachLen), m_iRecvDataLen);
CFileOperTools::GetInstance()->WriteComLog(strErr);
return EN_MEASU_PROC_DATA_ERR;
}
for (int i = 0; i < ucPacketNum; i++)
{
pMeasuBasicRes = (LPSTMeasuBasicDataRes)(m_ucData + 1 + sizeof(m_stQueryTdInfo.ucTaskID) + i*(sizeof(STMeasuDataResBasic)+iAttachLen));
pMeasuBasicRes->iID = ntohl(pMeasuBasicRes->iID);//by quyx 20180902
if (pMeasuBasicRes->iID != m_vtMeasureData[i].dwTsn)
{
strErr.Empty();
strErr.Format(_T("[%s][%d]测量数据乱序,测量TSN(%d),实际接收数据的TSN(%d)"),MODULE_NAME, __LINE__, m_vtMeasureData[i].dwTsn, pMeasuBasicRes->iID);
CFileOperTools::GetInstance()->WriteComLog(strErr);
return EN_MEASU_PROC_DATA_ERR;
}
//处理告警
CString strMeasuStaus = _T("");
if (LANG_ZHCN == g_iUILanguage)
strMeasuStaus.Format(_T("测量状态: %s"), GetMeasuStatusByAlarm(pMeasuBasicRes->ucAlram));
else
strMeasuStaus.Format(_T("Status: %s"), GetMeasuStatusByAlarm(pMeasuBasicRes->ucAlram));
GetDlgItem(IDC_STATIC_ALARM)->SetWindowText(strMeasuStaus);
//如果碰到下面告警,则测量必须要停下来
// if ((pMeasuBasicRes->ucAlram > 0 && pMeasuBasicRes->ucAlram < 3) ||
// (pMeasuBasicRes->ucAlram > 3 && pMeasuBasicRes->ucAlram < 7))
//modifyed by lsq 20171019 这里应张杰的要求,修改临时版本,允许过流保护和短路保护时,继续测量
if (2 == pMeasuBasicRes->ucAlram)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("遇到严重告警,终止测量"));
else
MessageBoxEx(NULL, _T("Stop measuring if severe warning is encountered"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return EN_MEASU_PROC_WARINNG;
}
//网络序转本机序 by quyx 20180902
pMeasuBasicRes->fI = tcp_ntohf(pMeasuBasicRes->fI);
pMeasuBasicRes->fK = tcp_ntohf(pMeasuBasicRes->fK);
pMeasuBasicRes->fR0 = tcp_ntohf(pMeasuBasicRes->fR0);
pMeasuBasicRes->fR0_LC = tcp_ntohf(pMeasuBasicRes->fR0_LC);
pMeasuBasicRes->fSP = tcp_ntohf(pMeasuBasicRes->fSP);
pMeasuBasicRes->fV = tcp_ntohf(pMeasuBasicRes->fV);
if (m_TaskdDBOper.UpdateTdBasicData(m_stQueryTdInfo.iSptType, m_stQueryTdInfo.iTestType,m_stQueryTdInfo.iTdChannelID,
m_vtMeasureData[i].dwTsn, pMeasuBasicRes, m_ucData + 1 + sizeof(m_stQueryTdInfo.ucTaskID) + (i + 1)*sizeof(STMeasuDataResBasic)+i*iAttachLen))
{
// m_iListDataID++;
bool bIsNeedInsert = true;
int iItemIndex;
// if (m_iSelMeasuIndex>=0 && m_iSelMeasuIndex < m_dialListMeasuTask.m_listData.GetItemCount() )
// {
// iItemIndex = m_iSelMeasuIndex++;
// bIsNeedInsert = false;
// }
// else
// {
// m_iSelMeasuIndex = -1;
if (m_listMeasuData.GetItemCount() >= MAX_TIMER_LIST_SHOW)
m_listMeasuData.DeleteAllItems();
iItemIndex = m_listMeasuData.GetItemCount();
// }
CString strTxt = _T("");
// if (bIsNeedInsert)
// {
strTxt.Empty();
strTxt.Format(_T("%d"), m_vtMeasureData[i].dwTsn);
m_listMeasuData.InsertItem(iItemIndex,strTxt);
// }
int iColIndex = 1;
strTxt.Empty();
if (0 == m_stQueryTdInfo.iSptType)
strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fA);
else
strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fA);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
if (0 == m_stQueryTdInfo.iSptType)
strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fB);
else
strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fB);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
if (0 == m_stQueryTdInfo.iSptType)
strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fM);
else
strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fM);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
if (0 == m_stQueryTdInfo.iSptType)
strTxt.Format(_T("%.2f"), m_vtMeasureData[i].fN);
else
strTxt.Format(_T("%d"), (int)m_vtMeasureData[i].fN);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
strTxt.Format(_T("%d"), pMeasuBasicRes->ucStack);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
strTxt.Format(_T("%.2f"), pMeasuBasicRes->fK);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
strTxt.Format(_T("%.2f"), pMeasuBasicRes->fV);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
strTxt.Format(_T("%.2f"), pMeasuBasicRes->fI);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
strTxt.Format(_T("%.2f"), pMeasuBasicRes->fR0);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
strTxt.Empty();
strTxt.Format(_T("%.2f"), pMeasuBasicRes->fSP);
m_listMeasuData.SetItemText(iItemIndex, iColIndex++, strTxt);
m_listMeasuData.SetItemData(iItemIndex, m_vtMeasureData[i].dwTsn);
}
else
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("插入测量数据信息失败"));
else
MessageBoxEx(NULL, _T("Failed to insert measurement data information"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return EN_MEASU_PROC_DB_ERR;
}
}
return EN_MEASU_PROC_SUCCESS;
}
void CDialTimerTaskMeasu::NotifyResFunc(LPARAM lOperType, LPARAM lOperRes, LPVOID lpWnd)
{
if (NULL == lpWnd)
{
return;
}
CDialTimerTaskMeasu* pDialMeasu = (CDialTimerTaskMeasu*)lpWnd;
CString strTips = _T("");
//如果是发送失败,或者是下发任务失败的话,则直接进行提示
//按照和舒嘉讨论的结果,为了屏蔽网络的原因,现在模式改成如果没有收到相应的响应报文,就一直进行数据重发操作
//所以这段中断流程要屏蔽
/*
if (EN_RECV_SUCCESS != lOperRes && (EN_NETWORK_SEND == lOperType || EN_CTRL_MEASURE_DATA != pDialMeasu->m_ucCmd))
{
pDialMeasu->StopMeasuDataInfo(false);
strTips.Empty();
strTips.Format(_T("[%s][%d]NotifyResFunc error. OperType = %d, OperRes = %d, cmd = %d"),
MODULE_NAME, __LINE__,lOperType, lOperRes, pDialMeasu->m_ucCmd );
CFileOperTools::GetInstance()->WriteComLog(strTips);
strTips.Empty();
strTips.Format(_T("测量失败,出错代码为(%d,%d,%d)"), lOperType, lOperRes,pDialMeasu->m_ucCmd);
AfxMessageBox(strTips);
return;
}
*/
CString strLog = _T("");
if(EN_CTRL_MEASURE_DATA == pDialMeasu->m_ucCmd)
{
//如果是超时或者是数据错误的话,则运行进行重测
if (lOperRes == EN_RECV_TIMEOUT || lOperRes == EN_RECV_DATA_ERROR||lOperRes == EN_RECV_DATA_DISORDER ||
EN_CLOUND_SND_FAILED == lOperRes)
{
if (-1 == pDialMeasu->m_iRepeatSndCnt)
{
strLog.Empty();
strLog.Format(_T("[%s][%d]Is being trigger repeat measure, tripper reason = %d"), MODULE_NAME, __LINE__, lOperRes);
CFileOperTools::GetInstance()->WriteComLog(strLog);
pDialMeasu->m_iRepeatSndCnt = MAX_TIMER_REPEAT_SND_CNT+1;
}
else
{
strLog.Empty();
strLog.Format(_T("[%s][%d]measure data failed, OperType = %d,OperRes = %d,NeedRepeatCnt = %d"),
MODULE_NAME, __LINE__,lOperType,lOperRes, pDialMeasu->m_iRepeatSndCnt);
CFileOperTools::GetInstance()->WriteComLog(strLog);
}
}
else if (lOperRes == EN_RECV_SUCCESS)
{
//如果测量成功,则给下面50毫秒的时间间隔
int iRes = pDialMeasu->ProcRcvMeasuData();
if (iRes == EN_MEASU_PROC_DB_ERR || iRes == EN_MEASU_PROC_WARINNG)
{
pDialMeasu->StopMeasuDataInfo(false);
return;
}
else if (iRes == EN_MEASU_PROC_SUCCESS)
pDialMeasu->m_iRepeatSndCnt = -1;
else
if (pDialMeasu->m_iRepeatSndCnt == -1)
pDialMeasu->m_iRepeatSndCnt = MAX_TIMER_REPEAT_SND_CNT+1;
}
else
{
pDialMeasu->StopMeasuDataInfo(false);
if (LANG_ZHCN == g_iUILanguage)
{
strTips.Format(_T("Measurement data failed, error code is (%d,%d)"), lOperType, lOperRes);
AfxMessageBox(strTips);
}
else
{
strTips.Format(_T("测量数据失败,出错代码为(%d,%d)"), lOperType, lOperRes);
MessageBoxEx(NULL, strTips, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
}
return;
}
}
//如果是点名或者设置设备参数的话,能执行到这部表示已经执行成功了,此时则进行下一步的操作
else if (EN_CTRL_ROLLCALL_CABLE == pDialMeasu->m_ucCmd || EN_CTRL_SET_TASK_PARAM == pDialMeasu->m_ucCmd ||
EN_PLC_POWER_ON == pDialMeasu->m_ucCmd)
{
//只有在成功的时候才会进行下一步的操作
if (EN_RECV_SUCCESS == lOperRes)
{
//如果是对PLC进行开关机的话,则需要判断是否开机正确
if (EN_TIMER_PROC_PLC_POWERON == pDialMeasu->m_ucProcStep)
{
int iPlcRes = COperPLC::GetInstance()->ParsePlcOperResInfo((char*)(pDialMeasu->m_ucData),pDialMeasu->m_iRecvDataLen);
if (EN_PLC_RES_SUCCESS == iPlcRes)
pDialMeasu->m_ucProcStep++;
else
{
strLog.Empty();
strLog.Format(_T("[%s][%d]excute plc cmd failed, res = %d, recv_data_len = %d"), MODULE_NAME, __LINE__, iPlcRes, pDialMeasu->m_iRecvDataLen);
CFileOperTools::GetInstance()->WriteComLog(strLog);
}
}
else
pDialMeasu->m_ucProcStep++;
}
else
{
strLog.Empty();
strLog.Format(_T("[%s][%d]excute ctrl cmd failed, proc_step = %d, res = %d"), MODULE_NAME, __LINE__, pDialMeasu->m_ucProcStep,lOperRes);
CFileOperTools::GetInstance()->WriteComLog(strLog);
}
}
else
{
strLog.Empty();
strLog.Format(_T("[%s][%d]Unknow ctrl command(%d)"), MODULE_NAME, __LINE__, pDialMeasu->m_ucCmd);
CFileOperTools::GetInstance()->WriteComLog(strLog);
}
pDialMeasu->m_bIsSigMeasuring = false;
}
int CDialTimerTaskMeasu::GetNeedMeasuData(char* pData,int &iLen, int iMaxLen)
{
iLen = 0;
m_vtMeasureData.clear();
if (m_TaskdDBOper.QueryNextTdBasicData(m_vtMeasureData) < 0)
{
return -1;
}
int iNum = m_vtMeasureData.size();
if (iNum > 0)
{
if (iMaxLen < iNum * sizeof(STSigSndDataInfo))
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("测量数据的长度过大"));
else
MessageBoxEx(NULL, _T("The length of measure data is overlength"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return -1;
}
memcpy(pData, m_stQueryTdInfo.ucTaskID, sizeof(m_stQueryTdInfo.ucTaskID));
pData[sizeof(m_stQueryTdInfo.ucTaskID)] = m_vtMeasureData.size();
//pData[0]= m_vtMeasureData.size();
for (int i = 0; i < m_vtMeasureData.size(); i++)
{
STSigSndDataInfo stSigData;//转网络序
memcpy(&stSigData, &m_vtMeasureData[i], sizeof(STSigSndDataInfo));
stSigData.dwTsn = htonl(stSigData.dwTsn);
stSigData.fA = tcp_htonf(stSigData.fA);
stSigData.fB = tcp_htonf(stSigData.fB);
stSigData.fM = tcp_htonf(stSigData.fM);
stSigData.fN = tcp_htonf(stSigData.fN);
stSigData.fK = tcp_htonf(stSigData.fK);
memcpy(pData + 1 + sizeof(m_stQueryTdInfo.ucTaskID) + i*sizeof(STSigSndDataInfo), &stSigData, sizeof(STSigSndDataInfo));
}
iLen = 1 + sizeof(m_stQueryTdInfo.ucTaskID)+iNum*sizeof(STSigSndDataInfo);
}
return 1;
}
void CDialTimerTaskMeasu::StopMeasuDataInfo(bool bIsNormalExit)
{
int iID = 0;
if (m_bIsOpenTbData)
{
m_TaskdDBOper.CloseTdData();
m_bIsOpenTbData = false;
}
m_bIsSigMeasuring = false;
m_iRepeatSndCnt = -1;
if (bIsNormalExit)
m_ucProcStep = EN_TIMER_PROC_MEASU_FIN;
else
m_ucProcStep = EN_TIMER_PROC_MEASU_TRMI;
}
void CDialTimerTaskMeasu::ClearRecvBuffer()
{
memset(m_ucData, 0, sizeof(m_ucData));
m_iRecvDataLen = 0;
}
bool CDialTimerTaskMeasu::OperPLC(BYTE ucCmd, CString strPlcID, bool bIsShowErrSyn)
{
char chMsg[300]={0};
int iLen = 0;
COperPLC::GetInstance()->GetPlcCmdInfo(ucCmd, chMsg, iLen, 300);
if (iLen < 1)
{
return false;
}
DWORD dwDevID = atoi(strPlcID);
CString strErr = _T("");
CAutoLock autoLock(theApp.m_NetWorkOper.GetNetCriticalSection());
if (!theApp.m_NetWorkOper.SendCtrlMsgDirect(1,dwDevID, EN_DEV_PLC, chMsg, iLen))
{
if (bIsShowErrSyn)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("同步失败"));
else
MessageBoxEx(NULL, _T("Synchronization failure"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
}
else
{
strErr.Empty();
strErr.Format(_T("[CDialTimerTaskMeasu][%d]send oper ctrl cmd(%d) failed."),__LINE__, ucCmd);
CFileOperTools::GetInstance()->WriteComLog(strErr);
}
return false;
}
Sleep(300);
if (EN_RECV_SUCCESS != theApp.m_NetWorkOper.RecvMsgDirect(chMsg, &iLen, 300, 90))
{
if (bIsShowErrSyn)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("接收数据失败"));
else
MessageBoxEx(NULL, _T("Data received failed"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
}
else
{
strErr.Empty();
strErr.Format(_T("[CDialTimerTaskMeasu][%d]recv oper ctrl cmd(%d) response failed."),__LINE__, ucCmd);
CFileOperTools::GetInstance()->WriteComLog(strErr);
}
return false;
}
if (iLen != sizeof(STRemPlcDataInfo))
{
if (bIsShowErrSyn)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("长度校验失败"));
else
MessageBoxEx(NULL, _T("Length check failed"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
}
else
{
strErr.Empty();
strErr.Format(_T("[CDialTimerTaskMeasu][%d]recv oper ctrl cmd(%d) error,check format failed."),__LINE__, ucCmd);
CFileOperTools::GetInstance()->WriteComLog(strErr);
}
return false;
}
STRemPlcDataInfo* pPlcInfo = (STRemPlcDataInfo*)chMsg;
if ( 1== pPlcInfo->ucResult )
return true;
else
return false;
}
bool CDialTimerTaskMeasu::GetRollCallData(char* pData, int& iLen, int iMaxLen)
{
memset(pData, 0, iMaxLen);
iLen = 0;
return true;
}
extern float tcp_htonf(float f);
bool CDialTimerTaskMeasu::GetCfgTaskData(DWORD dwTdID,char* pData, int& iLen, int iMaxLen)
{
CString strLog = _T("");
iLen = 0;
if (m_TaskdDBOper.QueryTdBasicInfo(dwTdID, &m_stQueryTdInfo))
{
STMeasuTaskParam stTaskParam;
memset(&stTaskParam, 0, sizeof(STMeasuTaskParam));
stTaskParam.ucTxPeriod = m_stQueryTdInfo.iTestPeriod;
stTaskParam.ucTestType = m_stQueryTdInfo.iTestType;
stTaskParam.ucSptType = m_stQueryTdInfo.iSptType;
stTaskParam.ucCableLayout = m_stQueryTdInfo.iCableLayout;
stTaskParam.ucStacking = m_stQueryTdInfo.ucStack;
stTaskParam.iSAInterval = htonl(m_stQueryTdInfo.iSAInterval);
stTaskParam.ucArrayType = m_stQueryTdInfo.iAR;
stTaskParam.fElecSpace = tcp_htonf(m_stQueryTdInfo.fXElecDistance);
stTaskParam.ucOrgFlg = m_stQueryTdInfo.ucOrgFlg;
memcpy(stTaskParam.ucTaskID, m_stQueryTdInfo.ucTaskID, sizeof(stTaskParam.ucTaskID));
iLen = sizeof(STMeasuTaskParam);
memcpy(pData, &stTaskParam, iLen);
}
else
{
strLog.Format(_T("[%s][%d]Query task information failed. taskid = %d"), dwTdID);
CFileOperTools::GetInstance()->WriteComLog(strLog);
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("查询任务信息失败"));
else
MessageBoxEx(NULL, _T("Query task information failed"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return false;
}
return true;
}
void CDialTimerTaskMeasu::SetBtnStatus(BYTE ucStatus)
{
switch (ucStatus)
{
case EN_TIMER_PROC_ROLLCALL:
case EN_TIMER_PROC_CFG_TASK:
case EN_TIMER_PROC_MEASU_DATA:
GetDlgItem(IDC_BUTTON_TIMER_MRASU)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->SetWindowText("Suspend");
break;
case EN_TIMER_PROC_MEASU_SUSPEND:
GetDlgItem(IDC_BUTTON_TIMER_MRASU)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->SetWindowText("Continue");
break;
default:
GetDlgItem(IDC_BUTTON_TIMER_MRASU)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->SetWindowText("Suspend");
break;
}
}
bool CDialTimerTaskMeasu::StartTimerTask(const STTimerTask* pTimerTask)
{
//首先开机
char chSndBuf[500] = {0};
int iSndLen = 0, iTimeout = 0, iMaxRcvLen = 0;
// BYTE ucCmd;
m_ucProcStep = EN_TIMER_PROC_PLC_POWERON;
// m_ucProcStep =EN_TIMER_PROC_ROLLCALL;
m_iRepeatSndCnt = -1;
STRespDataInfo stRspData;
STSendDataInfo stSendData;
CString strLog;
if (m_bIsOpenTbData)
{
m_TaskdDBOper.CloseTdData();
m_bIsOpenTbData = false;
}
GetDlgItem(IDC_STATIC_TASK_NAME)->SetWindowText(pTimerTask->strTaskName);
strLog.Empty();
strLog.Format(_T("[%s][%d]Is ready to excute timer task(%s)"),MODULE_NAME, __LINE__, pTimerTask->strTaskName);
CFileOperTools::GetInstance()->WriteComLog(strLog);
// m_bIsSigMeasuring =
//filled current test date and time to task by quyx 20180419
_CommandPtr pCmdUpd = NULL;
CString szTDate = _T("");
CString szTTime = _T("");
CString szSql = _T("");
DWORD dwID = DWORD(pTimerTask->uiTaskID);
szTDate.Empty();
szTDate.Format(_T("%04d-%02d-%02d"),g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay);
szTTime.Empty();
szTTime.Format(_T("%02d:%02d:%02d"),g_sysCurTime.wHour, g_sysCurTime.wMinute, g_sysCurTime.wSecond);
pCmdUpd.CreateInstance(_uuidof(Command));
pCmdUpd->ActiveConnection = theApp.m_pConnection;
try
{
if ((szTDate.GetLength() != (int)VAL_ZERO) && (szTTime.GetLength() != (int)VAL_ZERO))
{
szSql.Empty();
szSql.Format(_T("update td set Tdate = '%s',Ttime = '%s' where ID = %d"), szTDate, szTTime,dwID);
theApp.m_pConnection->BeginTrans();
pCmdUpd->CommandText = szSql.AllocSysString();
pCmdUpd->Execute(NULL, NULL, adCmdText);
theApp.m_pConnection->CommitTrans();
}
}
catch (_com_error e)
{
theApp.m_pConnection->RollbackTrans();
AfxMessageBox(e.Description());
return -1;
}
//end filled current test date and time to task by quyx 20180419
while(m_bIsRunning)
{
if (m_bIsSigMeasuring || m_bIsSuspend)
{
Sleep(30);
continue;
}
if (EN_TIMER_PROC_PLC_POWERON == m_ucProcStep)
{
m_ucCmd = EN_PLC_POWER_ON;
COperPLC::GetInstance()->GetPlcCmdInfo(m_ucCmd, chSndBuf, iSndLen, 500);
if (iSndLen < 1)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("PLC开机失败"));
else
MessageBoxEx(NULL, _T("PLC power on failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return false;
}
iMaxRcvLen = 200;
iTimeout = 3000;
}
else if (EN_TIMER_PROC_ROLLCALL == m_ucProcStep)
{
m_ucCmd = EN_CTRL_ROLLCALL_CABLE;
GetRollCallData(chSndBuf, iSndLen, 500);
iMaxRcvLen = 200;
iTimeout = 40000;
}
else if (EN_TIMER_PROC_CFG_TASK == m_ucProcStep)
{
m_ucCmd = EN_CTRL_SET_TASK_PARAM;
GetCfgTaskData(pTimerTask->uiTaskID,chSndBuf, iSndLen, 500);
iMaxRcvLen = MAX_RECV_MEASURE;
iTimeout = 12000;
}
else if (EN_TIMER_PROC_MEASU_DATA == m_ucProcStep)
{
if (!m_bIsOpenTbData)
{
if (!m_TaskdDBOper.OpenTdData(pTimerTask->uiTaskID,m_stQueryTdInfo.iSptType,1, theApp.m_ucMaxChannel))
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("打开数据表失败"));
else
MessageBoxEx(NULL, _T("Failed to open data table"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return false;
}
m_bIsOpenTbData = true;
}
m_ucCmd = EN_CTRL_MEASURE_DATA;
iMaxRcvLen = MAX_RECV_MEASURE;
//test为了屏蔽网络中断的影响,这里将单个点的最大超时时间改为1分钟
//那么最多9万重发机会的话,
//激点数据后面再修改
iTimeout = 60000;
if (m_iRepeatSndCnt <= 0)
{
//如果没有打开数据表的话,则进行打开数据表
memset(chSndBuf, 0, sizeof(chSndBuf));
iSndLen = 0;
m_iRepeatSndCnt = -1;//初始状态
int iRes = GetNeedMeasuData(chSndBuf, iSndLen,500);
if (0 == iSndLen)
{
if (iRes > 0)
{
StopMeasuDataInfo();
SetBtnStatus(m_ucProcStep);
return true;
}
else
{
StopMeasuDataInfo(false);
SetBtnStatus(m_ucProcStep);
return false;
}
}
}
else
{
strLog.Empty();
strLog.Format(_T("[%s][%d]Is being repeat measure, reverse repeat cnt = %d"), MODULE_NAME, __LINE__, --m_iRepeatSndCnt);
CFileOperTools::GetInstance()->WriteComLog(strLog);
}
}
else
{
strLog.Empty();
strLog.Format(_T("[%s][%d]Unknow timer proc step(%d)"), MODULE_NAME, __LINE__,m_ucProcStep);
CFileOperTools::GetInstance()->WriteComLog(strLog);
SetBtnStatus(m_ucProcStep);
return (m_ucProcStep == EN_TIMER_PROC_MEASU_FIN) ? true : false;
}
stSendData.pData = chSndBuf;
stSendData.wDataLen = iSndLen;
if (EN_TIMER_PROC_PLC_POWERON == m_ucProcStep)
{
stSendData.ucDstType = EN_DEV_PLC;
stSendData.uiDevID = atoi(pTimerTask->strPlcID);
}
else
{
stSendData.ucDstType = EN_DEV_GD10;
stSendData.uiDevID = m_uiDevID;
}
ClearRecvBuffer();
stRspData.pData = (char*)m_ucData;
stRspData.pRecvLen = &m_iRecvDataLen;
stRspData.iMaxRcvLen = iMaxRcvLen;
stRspData.iTimeout = iTimeout;
if (false == theApp.m_NetWorkOper.TransferOper(m_ucCmd, &stSendData, true, &stRspData, NotifyResFunc, this))
{
StopMeasuDataInfo(false);
Sleep(30);
continue;
}
m_bIsSigMeasuring = true;
}
}
int CDialTimerTaskMeasu::CompareTdTimer(const SYSTEMTIME &curTime, CString strTdTimer)
{
CString str1 = _T("");
strTdTimer.TrimLeft();
strTdTimer.TrimRight();
str1.Format(_T("%04d-%02d-%02d %02d:%02d"),curTime.wYear, curTime.wMonth, curTime.wDay,curTime.wHour,curTime.wMinute);
int nYear=0, nMonth = 0, nDay = 0, nHour = 0, nMinute = 0, nSecond = 0;
if (-1 == strTdTimer.Find(_T("-")))
_stscanf(strTdTimer, _T("%d/%d/%d %d:%d:%d"), &nYear, &nMonth, &nDay, &nHour, &nMinute, &nSecond);
else
_stscanf(strTdTimer, _T("%d-%d-%d %d:%d:%d"), &nYear, &nMonth, &nDay, &nHour, &nMinute, &nSecond);
CString strTemp = strTdTimer;
strTdTimer.Empty();
strTdTimer.Format(_T("%04d-%02d-%02d %02d:%02d"), nYear, nMonth, nDay, nHour,nMinute);
int iRes = strcmp(str1, strTdTimer);
if (iRes > 0)
{
CString strLog = _T("");
strLog.Format(_T("[%s][%d]timer task timeout. Sys_time = %s, Task_time = %s,Task_org=%s"),MODULE_NAME, __LINE__,str1, strTdTimer,strTemp);
CFileOperTools::GetInstance()->WriteComLog(strLog);
}
return iRes;
}
UINT CDialTimerTaskMeasu::StartMeasuData(LPVOID pInfo)
{
CDialTimerTaskMeasu* pMeasuData = (CDialTimerTaskMeasu*)pInfo;
char chSndBuf[500] = {0};
std::vector<STTimerTask> vtTimerTd;
pMeasuData->m_TaskdDBOper.QueryTimerTaskValiad(vtTimerTd, g_sysCurTime);
if (vtTimerTd.size() < 1)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("没有找到有效的定时任务"));
else
MessageBoxEx(NULL, _T("Can not found valid timer task"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
return 0;
}
pMeasuData->m_bIsThreadFinshed = false;
int iSendLen = 0;
// STTimerTask timerTd = vtTimerTd[0];
CString strErr = _T("");
while(pMeasuData->m_bIsRunning)
{
if (vtTimerTd.size() < 1)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("执行完成"));
else
MessageBoxEx(NULL, _T("Excute complete"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
break;
}
int iRes = pMeasuData->CompareTdTimer(g_sysCurTime, vtTimerTd[0].strRunTime);
if (iRes < 0)
{
Sleep(1000);
continue;
}
else if (iRes > 0)
{
pMeasuData->m_TaskdDBOper.DeleteTimerTask(vtTimerTd[0].uiTaskID);
vtTimerTd.erase(vtTimerTd.begin());
continue;
}
else
{
bool bRes = pMeasuData->StartTimerTask(&(vtTimerTd[0]));
/////////////////////////////////////////////////////////////////////////
// char chSndBuf[500] = {0};
// int iSndLen = 0;
if (false == pMeasuData->OperPLC(EN_PLC_POWER_OFF, vtTimerTd[0].strPlcID,false))
{
CString strErr = _T("");
strErr.Format(_T("[CDialTimerTaskMeasu][%d]oper timer task power off cmd failed."),__LINE__);
CFileOperTools::GetInstance()->WriteComLog(strErr);
}
// COperPLC::GetInstance()->GetPlcCmdInfo(EN_PLC_POWER_OFF, chSndBuf, iSendLen, 500);
//////////////////////////////////////////////////////////////////////////
pMeasuData->m_TaskdDBOper.DeleteTimerTask(vtTimerTd[0].uiTaskID);
vtTimerTd.erase(vtTimerTd.begin());
if (!bRes)
{
if (LANG_ZHCN == g_iUILanguage)
AfxMessageBox(_T("执行定时任务错误"));
else
MessageBoxEx(NULL, _T("Excute timer task error"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
break;
}
}
}
pMeasuData->m_bIsThreadFinshed = true;
return 1;
}
BOOL CDialTimerTaskMeasu::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CString strColTitle;
int iColIndex = 0;
m_listMeasuData.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP
| LVS_EX_ONECLICKACTIVATE | LVS_EX_GRIDLINES);
m_listMeasuData.InsertColumn(iColIndex++, _T("ID"),LVCFMT_LEFT,50);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_DB_SCRIPT_C1);
m_listMeasuData.InsertColumn(iColIndex++, _T("A"), LVCFMT_CENTER, 50);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_DB_SCRIPT_C2);
m_listMeasuData.InsertColumn(iColIndex++, _T("B"), LVCFMT_CENTER, 50);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_DB_SCRIPT_P1);
m_listMeasuData.InsertColumn(iColIndex++, _T("M"), LVCFMT_CENTER, 50);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_DB_SCRIPT_P2);
m_listMeasuData.InsertColumn(iColIndex++, _T("N"), LVCFMT_CENTER, 50);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_DB_SCRIPT_N);
m_listMeasuData.InsertColumn(iColIndex++, _T("Stack"), LVCFMT_CENTER, 50);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_DB_SCRIPT_K);
m_listMeasuData.InsertColumn(iColIndex++, _T("K"), LVCFMT_CENTER, 90);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_MEASU_TD_V);
m_listMeasuData.InsertColumn(iColIndex++, _T("V(mV)"), LVCFMT_CENTER, 95);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_MEASU_TD_I);
m_listMeasuData.InsertColumn(iColIndex++, _T("I(mA)"), LVCFMT_CENTER, 95);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_MEASU_TD_R0);
m_listMeasuData.InsertColumn(iColIndex++, _T("R0(Ohm*m)"), LVCFMT_CENTER, 95);
// strColTitle.Empty();
// strColTitle.LoadString(IDS_MEASU_TD_SP);
m_listMeasuData.InsertColumn(iColIndex++, _T("SP(mV)"), LVCFMT_CENTER, 95);
GetDlgItem(IDC_BUTTON_MRASU_SUSPEND)->ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDialTimerTaskMeasu::OnCancel()
{
// TODO: Add extra cleanup here
if (NULL != m_pThread)
{
int nRet = 0;
if (LANG_ZHCN == g_iUILanguage)
nRet = AfxMessageBox(_T("是否确定关闭?"), MB_YESNO | MB_ICONQUESTION);
else
nRet = MessageBoxEx(NULL, _T("Are you sure to close?"), STRING_MESSAGEBOXEX_TITLE, MB_YESNO, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US));
if (IDYES != nRet)
return;
}
ThreadJoin();
CDialog::OnCancel();
}
void CDialTimerTaskMeasu::OnButtonTimerCfg()
{
// TODO: Add your control notification handler code here
CDialTimerTask dialTimerTd;
dialTimerTd.m_dataOper.m_uiDevID = m_uiDevID;
dialTimerTd.DoModal();
}