123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- #include "stdafx.h"
- #include "DeliverModule.h"
- #include <process.h>
- CDeliverModule::CDeliverModule(WriteLog* logFun)
- {
- m_SdcCommondvector.clear();
- m_CommondType.clear();
- m_hSendingThreadToggleEvent=NULL;
- m_bSendingCommandsThreadExit = false;
- m_SendingCommandsThread=NULL;
- m_hACKEvent=NULL;
- m_nAKRsubmit=0;
- m_bisNAK=false;
- m_hCPEvent=NULL;
- m_nCPRsubmit=0;
- m_bisNCP=false;
- m_ClientControl = NULL;
- m_pSendData=NULL;
- m_pLogFun = logFun;
- m_bIsGenErr=false;
- m_nAKResendTotalNum=3;
- m_nCPResendTotalNum=1;
- }
- CDeliverModule::~CDeliverModule(void)
- {
- }
- /////////////////////
- //设备初始化的时候要把等待AK的事件设置为手动激活
- //m_hACKEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
- //并建立发送线程
- //StartSendingCommandsThread(this);
- ///////////////////
- /////////////////////
- //退出的时候也要调用函数结束掉发送线程
- //StopSendingCommandsThread();
- ////////////////////
- void CDeliverModule::SetPriority(int nCommandType,bool IsWaitforACK,int WaitingTime,bool IsResend,bool IsClearSendVector,bool IsFirstSend,bool IsWaitforCP,int WatingCPtime,bool isClearSamePriority)
- {
- tagCommandTye proiority;
- proiority.nCommandType=nCommandType;
- proiority.IsWaitforACK=IsWaitforACK;
- proiority.WaitingTime=WaitingTime;
- proiority.IsResend=IsResend;
- proiority.IsClearSendVector=IsClearSendVector;
- proiority.IsFirstSend=IsFirstSend;
- proiority.IsWaitforCP=IsWaitforCP;
- proiority.WatingCPtime=WatingCPtime;
- proiority.isClearSamePriority=isClearSamePriority;
- m_CommondType.push_back(proiority);
- }
- void CDeliverModule::SetGenErrStatus(bool isGenErr)
- {
- m_bIsGenErr=isGenErr;
- }
- bool CDeliverModule::SetLogFun(WriteLog* logFun)
- {
- if(logFun != NULL)
- {
- m_pLogFun = logFun;
- return true;
- }
- return false;
- }
- bool CDeliverModule::InitSendModle(SendCommands* pSendData,void* lparam)
- {
- m_ClientControl = lparam;
- m_pSendData = pSendData;
- //设备初始化的时候要把等待AK的事件设置为手动激活
- m_hACKEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
- m_hCPEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
- //并建立发送线程
- StartSendingCommandsThread(this);
- return true;
- }
- void CDeliverModule::EixtSendModle()
- {
- StopSendingCommandsThread();
- m_ClientControl = NULL;
- m_pSendData=NULL;
- m_SdcCommondvector.clear();
- m_CommondType.clear();
- }
- DWORD CDeliverModule::SendingCommandsThread(LPVOID pParam)
- {
- CDeliverModule* pCurrentGen = (CDeliverModule*) pParam;
- if (pCurrentGen == NULL)
- {
- if (pCurrentGen->m_pLogFun)
- {
- pCurrentGen->m_pLogFun("The pCurrentGen is NULL", LOG_V2_ERROR);
- }
- return 0;
- }
- DWORD event = 0;
- BOOL bExit = false;
- //indicating start thread success;
- SetEvent(pCurrentGen->m_hSendingThreadToggleEvent);
- //perform sending sedecal commands
- while (!pCurrentGen->m_bSendingCommandsThreadExit)
- {
- //query the command from the vector
- tagCommandStruct tempCommand;
- tempCommand.strCommand = "";
- tempCommand.nCommmandType = 0;
- if (pCurrentGen->m_SdcCommondvector.size() > 0)
- {
- std::unique_lock<std::mutex> uqeSection(pCurrentGen->m_iCriticalSection);
- tempCommand.nCommmandType = pCurrentGen->m_SdcCommondvector[0].nCommmandType;
- tempCommand.strCommand = pCurrentGen->m_SdcCommondvector[0].strCommand;
- pCurrentGen->m_SdcCommondvector.erase(pCurrentGen->m_SdcCommondvector.begin());//erase the first command
- uqeSection.unlock();
- if (tempCommand.strCommand != "")// with commnad
- {
- pCurrentGen->SendCommand(tempCommand.strCommand,tempCommand.nCommmandType);
- Sleep(50);
- }
- else
- {
- Sleep(10);//no commnad to send, so sleep 10 ms
- }
- // send command
- }
- else
- {
- Sleep(10);
- }
- }
- //indicating the end of thread;
- SetEvent(pCurrentGen->m_hSendingThreadToggleEvent);
- return 0;
- }
- //start timer to do offset calibration periodically;
- bool CDeliverModule::StartSendingCommandsThread(CDeliverModule* pCurrentGen)
- {
- //create thread;
- DWORD m_HardwareStatusID;
- m_SendingCommandsThread = CreateThread(0, 0, SendingCommandsThread, this, 0, &m_HardwareStatusID);
- if (NULL == m_SendingCommandsThread)
- {
- if (m_pLogFun)
- {
- m_pLogFun("Create sending commands thread failed!", LOG_V2_ERROR);
- }
- return false;
- }
- //wait till thread create successful;
- if (WaitForSingleObject(pCurrentGen->m_hSendingThreadToggleEvent,5000) == WAIT_OBJECT_0)
- {
- if (m_pLogFun)
- {
- m_pLogFun("detected the start of sending commands thread!", LOG_V2_INFO);
- }
- ResetEvent(pCurrentGen->m_hSendingThreadToggleEvent);
- }
- m_nSendingCommandsThreadId = m_HardwareStatusID;
- return true;
- }
- //stop sending commands thread periodically;
- void CDeliverModule::StopSendingCommandsThread()
- {
- if (m_pLogFun)
- {
- m_pLogFun("sending commands thread exiting...", LOG_V2_WARNING);
- }
- if (NULL != m_SendingCommandsThread)
- {
- m_bSendingCommandsThreadExit = true;
- if (::WaitForSingleObject(m_hSendingThreadToggleEvent,5000)==WAIT_OBJECT_0)
- {
- if (m_pLogFun)
- {
- m_pLogFun("detected the exit of sending commands thread!", LOG_V2_INFO);
- }
- ResetEvent(m_hSendingThreadToggleEvent);
- }
- else
- {
- DWORD exitCode = 0;
- //Retrieves the termination status of the specified thread;
- if (!GetExitCodeThread(m_SendingCommandsThread, &exitCode))
- {
- DWORD nError = ::GetLastError();
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("GetExitCodeThread() error, error No is {:d}", nError);
- m_pLogFun(logstr.c_str(), LOG_V2_ERROR);
- }
- }
- TerminateThread(m_SendingCommandsThread,exitCode);
- }
- m_SendingCommandsThread = NULL;
- }
- }
- /*
- strCommand which will be send to gen side
- nType commnad type 0, no ACK , 1, with ACK, 2, apr command, 3, focus?(to be confirmed)
- */
- bool CDeliverModule::ProcessCommand(std::string strCommand,int nType)
- {
- std::unique_lock<std::mutex> uqeSection(m_iCriticalSection);
- m_SDCCommand.strCommand = strCommand;
- m_SDCCommand.nCommmandType = nType;
- /*std::string logstr;
- logstr = "strcommand is " + m_SDCCommand.strCommand;
- m_pGen->logfile->WriteLog(logstr,LOG_INFORMATION,LOG_DEBUG,true);
- logstr.Format("CommmandType is %d",nType);
- m_pGen->logfile->WriteLog(logstr,LOG_INFORMATION,LOG_DEBUG,true);*/
- int i=0;
- for (i=0;i<m_CommondType.size();i++)
- {
- if (m_SDCCommand.nCommmandType==m_CommondType[i].nCommandType)
- {
- m_SDCCommand.nCommmandType=i;//将设置的优先级对应到优先级列表中的位置,方便获取该优先级的属性
- /*std::string logstr;
- logstr.Format("CommmandType in list is %d",i);
- m_pGen->logfile->WriteLog(logstr,LOG_INFORMATION,LOG_DEBUG,true);*/
-
- break;
- }
- }
- if (i==m_CommondType.size())
- {
- uqeSection.unlock();
- return false;
- }
- std::string logstr;
- for(int j=0;j<m_SdcCommondvector.size();j++)
- {
- if(m_SdcCommondvector[j].strCommand==m_SDCCommand.strCommand)
- {
- m_SdcCommondvector.erase(m_SdcCommondvector.begin()+j);
- j--;
- }
- }
- if (m_CommondType[i].IsClearSendVector)
- {
- for(int j=0;j<m_SdcCommondvector.size();j++)
- {
- if(!m_CommondType[m_SdcCommondvector[j].nCommmandType].IsClearSendVector)
- {
- if (m_pLogFun)
- {
- logstr = std::format("Clear command to {}", j);
- m_pLogFun(logstr.c_str(), LOG_V2_INFO);
- }
- m_SdcCommondvector.erase(m_SdcCommondvector.begin()+j);
- j--;
-
- }
- }
- m_SdcCommondvector.push_back(m_SDCCommand);
- if (m_pLogFun)
- {
- logstr = std::format("Clear vector to {}", m_SdcCommondvector.size());
- m_pLogFun(logstr.c_str(), LOG_V2_INFO);
- }
- }
- else if (m_CommondType[i].IsFirstSend)
- {
- m_SdcCommondvector.push_front(m_SDCCommand);
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("FirstSend to {}", 0);
- m_pLogFun(logstr.c_str(), LOG_V2_INFO);
- }
- }
- else if(m_CommondType[i].isClearSamePriority)
- {
- for(int j=0;j<m_SdcCommondvector.size();j++)
- {
-
- if(m_SdcCommondvector[j].nCommmandType==m_SDCCommand.nCommmandType)
- {
- if (m_pLogFun)
- {
- logstr = std::format("Clear same priority command: {}", m_SdcCommondvector[j].strCommand.c_str());
- m_pLogFun(logstr.c_str(), LOG_V2_INFO);
- }
- m_SdcCommondvector.erase(m_SdcCommondvector.begin()+j);
- j--;
- }
- }
- m_SdcCommondvector.push_back(m_SDCCommand);
- }
- else
- {
- m_SdcCommondvector.push_back(m_SDCCommand);
- }
- uqeSection.unlock();
- return true;
- }
- bool CDeliverModule::ReProcessCommand(tagCommandStruct strCommand)
- {
- m_SDCCommand.nCommmandType = strCommand.nCommmandType;
- m_SDCCommand.strCommand = strCommand.strCommand;
- if (!m_CommondType[strCommand.nCommmandType].IsResend||(m_bIsGenErr&&!m_CommondType[strCommand.nCommmandType].IsClearSendVector))
- {
- //不重发的命令,或者设备处于错误状态时不清空发送队列的命令,都不重发
- return true;
- }
- std::unique_lock<std::mutex> uqeSection(m_iCriticalSection);
- m_SdcCommondvector.push_front(m_SDCCommand);
- uqeSection.unlock();
- return true;
- }
- void CDeliverModule::SendCommand(std::string strcommand, int commandtype)
- {
- std::string strlog = "";
- if (m_bIsGenErr)
- {
- if(!m_CommondType[commandtype].IsClearSendVector)
- {
- if (m_pLogFun)
- {
- m_pLogFun("In error state,and the command is not nessesery", LOG_V2_WARNING);
- }
- return;
- }
- }
- if (m_strcurrentcmd.strCommand!=strcommand)
- {
- m_nAKRsubmit=0;
- m_nCPRsubmit=0;
- }
- m_strcurrentcmd.strCommand = strcommand;//保存当前发送的命令
- m_strcurrentcmd.nCommmandType=commandtype;
- ResetEvent(m_hACKEvent);
- ResetEvent(m_hCPEvent);
- m_bisNAK=false;//重置NAK状态
- m_bisNCP=false;
- bool isSend=false;
- //if(m_strcurrentcmd.strCommand.Mid(0,2)!="ST"&&m_strcurrentcmd.strCommand.Mid(0,2)!="HU")
- //{
- // std::string strMessage;
- // strMessage.Format("%s 是否发送成功!",GetCurrentCommand());
- // int result=AfxMessageBox(strMessage,MB_YESNO);
- // if (result==6)
- // {
- // isSend=true;
- // }
- // else if (result==7)
- // {
- // isSend=false;
- // }
- //}
- ////////////////////////
- //调用回调函数将指令发送
- if(m_pSendData)
- {
- //if(isSend)
- m_pSendData(m_strcurrentcmd.strCommand.c_str(),m_strcurrentcmd.strCommand.length(),m_ClientControl);
- }
- else
- return;
- ///////////////////////////
- //int AKTimeOut=0;
- //if (!isSend)
- //{
- // int result=AfxMessageBox("AK是否超时,选‘是’AK超时,选‘否’CP超时",MB_YESNO);
- // if (result==6)
- // {
- // AKTimeOut=1;
- // }
- // else if (result==7)
- // {
- // AKTimeOut=-1;
- // }
- //}
- if (m_CommondType[commandtype].IsWaitforACK)
- {
- //如果AK超时或者错,则在函数中重发,直接返回,不等CP
- //if(AKTimeOut==1)
- {
- if(!WaitforACK(m_CommondType[commandtype].WaitingTime))
- return;
- }
- }
- if (m_CommondType[commandtype].IsWaitforCP)
- {
- WaitforCP(m_CommondType[commandtype].WatingCPtime);
- }
- }
- bool CDeliverModule::ReceiveACK (bool IsACK)
- {
- if (IsACK)
- {
- m_bisNAK=false;
- if (m_pLogFun)
- {
- m_pLogFun("Receive AK!", LOG_V2_INFO);
- }
- }
- else
- {
- m_bisNAK=true;
- if (m_pLogFun)
- {
- m_pLogFun("Receive NAK!", LOG_V2_INFO);
- }
- }
- SetEvent(m_hACKEvent);
- return m_bisNAK;
- }
- bool CDeliverModule::WaitforACK(DWORD nWaitingTime)
- {
- if (WaitForSingleObject(m_hACKEvent,nWaitingTime) == WAIT_OBJECT_0)
- {
- if (m_bisNAK)
- {
- if (m_nAKRsubmit<m_nAKResendTotalNum)
- {
- m_nAKRsubmit++;
- ReProcessCommand(m_strcurrentcmd);
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("wait NACK {}!", m_nAKRsubmit);
- m_pLogFun(logstr.c_str(), LOG_V2_INFO);
- }
- }
- else
- {
- //重发三次都是NAK,所以判断机架断开连接
- //AfxMessageBox("收到NAK,重发失败",MB_OK);
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("Send failed {}!", 0);
- m_pLogFun(logstr.c_str(), LOG_V2_ERROR);
- }
- m_nAKRsubmit=0;
- }
- return false;
- }
- else
- {
- m_nAKRsubmit=0;
- ResetEvent(m_hACKEvent);
- return true;
- }
- }
- else
- {
- if (m_nAKRsubmit<m_nAKResendTotalNum)
- {
- m_nAKRsubmit++;
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("wait ACK timeout {}!", m_nAKRsubmit);
- m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
- }
- ReProcessCommand(m_strcurrentcmd);
- }
- else
- {
- //重发三次都是没有回复,所以判断机架断开连接
- //AfxMessageBox("AK超时,重发失败",MB_OK);
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("Send failed {}!", 0);
- m_pLogFun(logstr.c_str(),LOG_V2_ERROR);
- }
- m_nAKRsubmit=0;
- }
- return false;
- }
- }
- bool CDeliverModule::WaitforCP(DWORD nWaitingTime)
- {
- if (WaitForSingleObject(m_hCPEvent,nWaitingTime) == WAIT_OBJECT_0)
- {
- if (m_bisNCP)
- {
- if (m_nCPRsubmit<m_nCPResendTotalNum)
- {
- m_nCPRsubmit++;
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("Wait NCP{}", m_nCPRsubmit);
- m_pLogFun(logstr.c_str(), LOG_V2_INFO);
- }
- ReProcessCommand(m_strcurrentcmd);
- }
- else
- {
- //重发三次都是NAK,所以判断机架断开连接
- //AfxMessageBox("发送指令出错,重发失败",MB_OK);
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("Send failed {}!", 0);
- m_pLogFun(logstr.c_str(), LOG_V2_ERROR);
- }
- m_nCPRsubmit=0;
- }
- return false;
- }
- else
- {
- m_nCPRsubmit=0;
- ResetEvent(m_hCPEvent);
- return true;
- }
- }
- else
- {
- if (m_nCPRsubmit<1)
- {
- m_nCPRsubmit++;
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("Wait CP timeout {}!", m_nCPRsubmit);
- m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
- }
- ReProcessCommand(m_strcurrentcmd);
- }
- else
- {
- std::string logstr;
- if (m_pLogFun)
- {
- logstr = std::format("Send failed {}!", 0);
- m_pLogFun(logstr.c_str(), LOG_V2_ERROR);
- }
- m_nCPRsubmit=0;
- }
- return false;
- }
- }
- bool CDeliverModule::ReceiveCP (bool IsCP)
- {
- if (IsCP)
- {
- m_bisNCP=false;
- if (m_pLogFun)
- {
- m_pLogFun("Receive CP!", LOG_V2_INFO);
- }
- }
- else
- {
- m_bisNCP=true;
- if (m_pLogFun)
- {
- m_pLogFun("Receive NCP!", LOG_V2_INFO);
- }
- }
- SetEvent(m_hCPEvent);
- return m_bisNCP;
- }
- std::string CDeliverModule::GetCurrentCommand()
- {
- std::string command;
- command = m_strcurrentcmd.strCommand;
- return command;
- }
- void CDeliverModule::SetAKResendNum(int ResendNum)
- {
- m_nAKResendTotalNum=ResendNum;
- }
- void CDeliverModule::SetCPResendNum(int ResendNum)
- {
- m_nCPResendTotalNum=ResendNum;
- }
- int CDeliverModule::GetCurrentCommandPriority()
- {
- int Priority=m_CommondType[m_strcurrentcmd.nCommmandType].nCommandType;
- return Priority;
- }
- bool CDeliverModule::GetCurrentErrorState()
- {
- return m_bIsGenErr;
- }
|