#include "DeliverModule.h" namespace DIOS::Dev::MODLE::SerialGPM { //tagCommandType tagCommandType::tagCommandType(bool isPrintLog, bool isFirstSend, int isResend, bool isWait, int waitingTime, bool isWaitSelf, int watingSelftime, bool isWaitforACK, int waitingACKTime, bool isWaitforCP, int watingCPtime, bool isClearSendVector, bool isClearSamePriority, bool isReplace) { bIsPrintLog = isPrintLog; bIsFirstSend = isFirstSend; nIsResend = isResend; bIsWait = isWait; nWaitingTime = waitingTime; bIsWaitforSelf = isWaitSelf; nWaitingSelfTime = watingSelftime; bIsWaitforACK = isWaitforACK; nWaitingACKTime = waitingACKTime; bIsWaitforCP = isWaitforCP; nWatingCPTime = watingCPtime; bIsClearSendVector = isClearSendVector; bIsClearSamePriority = isClearSamePriority; bIsReplace = isReplace; }; bool tagCommandType::operator ==(const tagCommandType& value) { if (bIsFirstSend == value.bIsFirstSend && nIsResend == value.nIsResend && bIsWait == value.bIsWait && nWaitingTime == value.nWaitingTime && bIsWaitforSelf == value.bIsWaitforSelf && nWaitingSelfTime == value.nWaitingSelfTime && bIsWaitforACK == value.bIsWaitforACK && nWaitingACKTime == value.nWaitingACKTime && bIsWaitforCP == value.bIsWaitforCP && nWatingCPTime == value.nWatingCPTime && bIsClearSendVector == value.bIsClearSendVector && bIsClearSamePriority == value.bIsClearSamePriority && bIsReplace == value.bIsReplace) return true; else return false; } //tagCommandStruct tagCommandStruct::tagCommandStruct() :strCommand(""), nCommmandType(0) {} tagCommandStruct::tagCommandStruct(std::string& cmd, int type) : strCommand(cmd), nCommmandType(type) {} tagCommandStruct::tagCommandStruct(char* cmd, int lengh, int type) :strCommand(cmd, lengh), nCommmandType(type) {} tagCommandStruct& tagCommandStruct::operator =(const tagCommandStruct& value) { strCommand = value.strCommand; nCommmandType = value.nCommmandType; return *this; } //CDeliverModule CDeliverModule::CDeliverModule() { m_ClientControl = NULL; m_pSendData = NULL; m_pLogFun = NULL; m_hSendingThreadToggleEvent = NULL; m_SendingCommandsThread = NULL; //m_nSendingCommandsThreadId = 0; m_bSendingCommandsThreadExit = false; m_Commondvector.clear(); m_CommondType.clear(); m_hSelfEvent = NULL; m_hACKEvent = NULL; m_hCPEvent = NULL; m_nRsubmit = 0; m_bSendEnable = true; m_hUnitStateChangeEvent = NULL; } CDeliverModule::~CDeliverModule(void) { EixtSendModle(); } bool CDeliverModule::InitSendModle(void* ptrObj, SendCommands* pSendData, WriteLog* logFun) { m_ClientControl = ptrObj; m_pSendData = pSendData; m_pLogFun = logFun; //设备初始化的时候要把等待的事件设置为手动激活 m_hSendingThreadToggleEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hUnitStateChangeEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hSelfEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hACKEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hCPEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //并建立发送线程 StartSendingCommandsThread(); tagCommandType proiority0; m_CommondType.push_back(proiority0); return true; } void CDeliverModule::EixtSendModle() { StopSendingCommandsThread(); CloseHandle(m_hSendingThreadToggleEvent); CloseHandle(m_hUnitStateChangeEvent); CloseHandle(m_hSelfEvent); CloseHandle(m_hACKEvent); CloseHandle(m_hCPEvent); m_Commondvector.clear(); m_CommondType.clear(); m_ClientControl = NULL; m_pSendData = NULL; m_pLogFun = NULL; } bool CDeliverModule::SetLogFun(WriteLog* logFun) { if (logFun != NULL) { m_pLogFun = logFun; return true; } return false; } int CDeliverModule::SetPriority(bool isPrintLog, bool isFirstSend, int isResend, bool isWait, int waitingTime, bool isWaitforSelf, int watingSelftime, bool isWaitforACK, int waitingACKTime, bool isWaitforCP, int watingCPtime, bool isClearSendVector, bool isClearSamePriority) { tagCommandType proiority(isPrintLog,isFirstSend, isResend, isWait, waitingTime, isWaitforSelf, watingSelftime, isWaitforACK, waitingACKTime, isWaitforCP, watingCPtime, isClearSendVector, isClearSamePriority); for (int i = 0; i < m_CommondType.size(); i++) { if (m_CommondType[i] == proiority) { if (m_pLogFun) { m_pLogFun("SetPriority:Priority already exist", LOG_V2_WARNING); return i; } } } m_CommondType.push_back(proiority); if (m_pLogFun) { m_pLogFun("SetPriority:add Priority successful", LOG_V2_DEBUG); } return m_CommondType.size() - 1; } int CDeliverModule::SetPriority_V2(bool isWaitforACK, int waitingACKTime, bool isResend, bool isClearSendVector, bool isFirstSend, bool isWaitforCP, int watingCPtime, bool isClearSamePriority) { tagCommandType proiority(true,isFirstSend, isResend, false, 0, false, 0, isWaitforACK, waitingACKTime, isWaitforCP, watingCPtime, isClearSendVector, isClearSamePriority); for (int i = 0; i < m_CommondType.size(); i++) { if (m_CommondType[i] == proiority) { if (m_pLogFun) { m_pLogFun("SetPriority_V2:Priority already exist", LOG_V2_WARNING); return i; } } } m_CommondType.push_back(proiority); m_pLogFun("SetPriority_V2:add Priority successful", LOG_V2_DEBUG); return m_CommondType.size(); } int CDeliverModule::SetPriority_Replace(int nPriority, bool isReplace) { std::string logstr; if (nPriority >= 0 && nPriority < (m_CommondType.size())) { m_CommondType[nPriority].bIsReplace = isReplace; logstr = std::format("SetPriority_Replace:[{:d},{:d}]successful", nPriority, isReplace); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } else { logstr = std::format("SetPriority_Replace:[{:d},{:d}]failed", nPriority, isReplace); m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } return true; } bool CDeliverModule::StartSendingCommandsThread() { //create thread DWORD m_HardwareStatusID; m_SendingCommandsThread = CreateThread(0, 0, SendingCommandsThread, this, 0, &m_HardwareStatusID); if (NULL == m_SendingCommandsThread) { if (m_pLogFun) { m_pLogFun("StartSendingCommandsThread:Create sending commands thread failed!", LOG_V2_ERROR); } return false; } //wait till thread create successful if (WaitForSingleObject(m_hSendingThreadToggleEvent, 5000) == WAIT_OBJECT_0) { if (m_pLogFun) { m_pLogFun("StartSendingCommandsThread:detected the start of sending commands thread", LOG_V2_INFO); } ResetEvent(m_hSendingThreadToggleEvent); //m_nSendingCommandsThreadId = m_HardwareStatusID } else { if (m_pLogFun) { m_pLogFun("StartSendingCommandsThread:start of sending commands thread failed!", LOG_V2_ERROR); } return false; } return true; } void CDeliverModule::StopSendingCommandsThread() { if (NULL != m_SendingCommandsThread) { if (m_pLogFun) { m_pLogFun("StopSendingCommandsThread:sending commands thread exiting...", LOG_V2_INFO); } m_bSendingCommandsThreadExit = true; if (::WaitForSingleObject(m_hSendingThreadToggleEvent, 5000) == WAIT_OBJECT_0) { if (m_pLogFun) { m_pLogFun("StopSendingCommandsThread: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("StopSendingCommandsThread:GetExitCodeThread() error[{:d}]", nError); m_pLogFun(logstr.c_str(), LOG_V2_ERROR); } } TerminateThread(m_SendingCommandsThread, exitCode); } m_SendingCommandsThread = NULL; } } DWORD CDeliverModule::SendingCommandsThread(LPVOID pParam) { CDeliverModule* pCurrentUnit = (CDeliverModule*)pParam; if (pCurrentUnit->m_pLogFun) { pCurrentUnit->m_pLogFun("Enter SendingCommandsThread", LOG_V2_INFO); } else { return 0; } //indicating start thread success SetEvent(pCurrentUnit->m_hSendingThreadToggleEvent); std::string logstr; //perform sending sedecal commands while (!pCurrentUnit->m_bSendingCommandsThreadExit) { //query the command from the vector if (pCurrentUnit->m_Commondvector.size() > 0) { if (!pCurrentUnit->m_bSendEnable) { if (pCurrentUnit->m_pLogFun) { pCurrentUnit->m_pLogFun("SendCommand:In error state,and the command not send", LOG_V2_WARNING); } if (WAIT_OBJECT_0 == WaitForSingleObject(pCurrentUnit->m_hUnitStateChangeEvent, INFINITE)) { if (pCurrentUnit->m_pLogFun) { pCurrentUnit->m_pLogFun("SendCommand:change to standby state,and the command begin send", LOG_V2_WARNING); } } } std::unique_lock uqeSection(pCurrentUnit->m_iCriticalSection); pCurrentUnit->m_strcurrentcmd = pCurrentUnit->m_Commondvector[0]; pCurrentUnit->m_Commondvector.pop_front();//erase the first command uqeSection.unlock(); if (pCurrentUnit->m_strcurrentcmd.nCommmandType >= pCurrentUnit->m_CommondType.size()) { if (pCurrentUnit->m_pLogFun) { logstr = std::format("SendCommand:commandtype[{}] not exist in [{}]", pCurrentUnit->m_strcurrentcmd.nCommmandType, pCurrentUnit->m_CommondType.size()); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } continue; } bool isPrintLog = pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsPrintLog; int eventArrayNum = 0,waitEventTime = 0, waitTime = 0; HANDLE eventArray[3]; if (pCurrentUnit->m_strcurrentcmd.strCommand != "" && pCurrentUnit->m_strcurrentcmd.nCommmandType >= 0)// with commnad { if (pCurrentUnit->m_strcurrentcmd.nCommmandType == 0)//直接发送 { pCurrentUnit->SendCommand(pCurrentUnit->m_strcurrentcmd.strCommand, pCurrentUnit->m_strcurrentcmd.nCommmandType); } else { if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWaitforSelf)//等待自己 { eventArray[eventArrayNum] = pCurrentUnit->m_hSelfEvent; eventArrayNum++; waitEventTime += pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWaitingSelfTime; } if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWaitforACK)//等待ACK { eventArray[eventArrayNum] = pCurrentUnit->m_hACKEvent; eventArrayNum++; waitEventTime += pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWaitingACKTime; } if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWaitforCP)//等待CP { eventArray[eventArrayNum] = pCurrentUnit->m_hCPEvent; eventArrayNum++; waitEventTime += pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWatingCPTime; } if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWait)//等待 { waitTime = pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWaitingTime; } if (pCurrentUnit->m_pLogFun && isPrintLog) { logstr = std::format("SendingCommandsThread:get cmd[{}]type[{}] out from vector,begin wait [{}count,{}time]event [{}]time", pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType, eventArrayNum,waitEventTime, waitTime); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } pCurrentUnit->SendCommand(pCurrentUnit->m_strcurrentcmd.strCommand, pCurrentUnit->m_strcurrentcmd.nCommmandType, isPrintLog); Sleep(waitTime); if (eventArrayNum > 0) { bool cmdOver = false; DWORD dwResult = WaitForMultipleObjects(eventArrayNum, eventArray, TRUE, waitEventTime); switch (dwResult) { case WAIT_OBJECT_0: { if (pCurrentUnit->m_pLogFun && isPrintLog) { logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] is over", pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } pCurrentUnit->m_nRsubmit = 0; cmdOver = true; } break; case WAIT_TIMEOUT: { if (pCurrentUnit->m_pLogFun && isPrintLog) { logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] is timeout", pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } cmdOver = false; } break; case WAIT_FAILED: { if (pCurrentUnit->m_pLogFun && isPrintLog) { logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] is failed", pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } pCurrentUnit->m_nRsubmit = 0; cmdOver = true; } break; } if (!cmdOver) { pCurrentUnit->m_nRsubmit++; if (pCurrentUnit->m_pLogFun && isPrintLog) { logstr = std::format("SendingCommandsThread:current try reSend count[{}],limit[{}]", pCurrentUnit->m_nRsubmit, pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nIsResend); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } if (pCurrentUnit->m_nRsubmit <= pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nIsResend) { pCurrentUnit->ReProcessCommand(pCurrentUnit->m_strcurrentcmd); } else { pCurrentUnit->m_nRsubmit = 0; if (pCurrentUnit->m_pLogFun && isPrintLog) { logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] Exceeded attempts", pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType); pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } } } } } Sleep(10); } else { Sleep(10);//no commnad to send, so sleep 10 ms } } else { Sleep(10);//no commnad to send, so sleep 10 ms } } if (pCurrentUnit->m_pLogFun) { pCurrentUnit->m_pLogFun("End SendingCommandsThread!", LOG_V2_INFO); } //indicating the end of thread; SetEvent(pCurrentUnit->m_hSendingThreadToggleEvent); return 0; } void CDeliverModule::SendCommand(std::string strcommand, int commandtype,bool isPrintLog) { std::string logstr; if (m_pLogFun && isPrintLog) { logstr = std::format("SendCommand:try send cmd[{:s}]type[{:d}]", strcommand.c_str(), commandtype); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } //重置ACK、CP状态 ResetEvent(m_hSelfEvent); ResetEvent(m_hACKEvent); ResetEvent(m_hCPEvent); //调用回调函数将指令发送 if (m_pSendData) { m_pSendData(m_strcurrentcmd.strCommand.c_str(), m_strcurrentcmd.strCommand.length(), m_ClientControl); } else { if (m_pLogFun && isPrintLog) { m_pLogFun("SendCommand:m_pSendData is NULL", LOG_V2_WARNING); } return; } } RET_STATUS CDeliverModule::ProcessCommand(std::string strCommand, int nType, int nCMDHeadLengh) { std::string logstr; if (nType >= m_CommondType.size()) { if (m_pLogFun) { logstr = std::format("ProcessCommand:[{:s},{:d}]is not found", strCommand.c_str(), nType); m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } return RET_STATUS::RET_FAILED; } std::unique_lock uqeSection(m_iCriticalSection); tagCommandStruct tempCMD(strCommand, nType); if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}]", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } if (m_CommondType[nType].bIsReplace) { for (int i = 0; i < m_Commondvector.size(); i++) { if (nCMDHeadLengh > 0) { if (m_Commondvector[i].strCommand.compare(0, nCMDHeadLengh, tempCMD.strCommand, 0, nCMDHeadLengh) == 0) { m_Commondvector[i] = tempCMD; if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}][head[{}]]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType, nCMDHeadLengh); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } uqeSection.unlock(); return RET_STATUS::RET_SUCCEED; } } else { if (m_Commondvector[i].strCommand == tempCMD.strCommand) { m_Commondvector[i] = tempCMD; if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } uqeSection.unlock(); return RET_STATUS::RET_SUCCEED; } } } } else { logstr = std::format("ProcessCommand:[{:s},{:d}] not replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } if (m_CommondType[nType].bIsClearSendVector) { if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}] clear vector", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); m_Commondvector.clear(); } m_Commondvector.push_back(tempCMD); } else if (m_CommondType[nType].bIsClearSamePriority) { for (int j = 0; j < m_Commondvector.size(); j++) { if (m_Commondvector[j].nCommmandType == tempCMD.nCommmandType) { if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("Clear same priority command: {}", m_Commondvector[j].strCommand.c_str()); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } m_Commondvector.erase(m_Commondvector.begin() + j); j--; } } if (m_CommondType[nType].bIsFirstSend) m_Commondvector.push_front(tempCMD); else m_Commondvector.push_back(tempCMD); } else if (m_CommondType[nType].bIsFirstSend) { m_Commondvector.push_front(tempCMD); std::string logstr; if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("FirstSend to {}", tempCMD.strCommand.c_str()); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } } else { m_Commondvector.push_back(tempCMD); } uqeSection.unlock(); return RET_STATUS::RET_SUCCEED; } RET_STATUS CDeliverModule::ProcessCommand(char* strCommand, int nLengh, int nType, int nCMDHeadLengh) { std::string logstr; if (nType >= m_CommondType.size()) { if (m_pLogFun) { logstr = std::format("ProcessCommand:[{:s},{:d}]is not found", strCommand, nType); m_pLogFun(logstr.c_str(), LOG_V2_WARNING); } return RET_STATUS::RET_FAILED; } std::unique_lock uqeSection(m_iCriticalSection); tagCommandStruct tempCMD(strCommand, nLengh, nType); if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}]", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } for (int i = 0; i < m_Commondvector.size(); i++) { if (nCMDHeadLengh > 0) { if (m_Commondvector[i].strCommand.compare(0, nCMDHeadLengh, tempCMD.strCommand, 0, nCMDHeadLengh) == 0) { m_Commondvector[i] = tempCMD; if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}][head[{}]]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType, nCMDHeadLengh); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } uqeSection.unlock(); return RET_STATUS::RET_SUCCEED; } } else { if (m_Commondvector[i].strCommand == tempCMD.strCommand) { m_Commondvector[i] = tempCMD; if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } uqeSection.unlock(); return RET_STATUS::RET_SUCCEED; } } } if (m_CommondType[nType].bIsClearSendVector) { if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("ProcessCommand:[{:s},{:d}] clear vector", tempCMD.strCommand.c_str(), tempCMD.nCommmandType); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); m_Commondvector.clear(); } m_Commondvector.push_back(tempCMD); } else if (m_CommondType[nType].bIsClearSamePriority) { for (int j = 0; j < m_Commondvector.size(); j++) { if (m_Commondvector[j].nCommmandType == tempCMD.nCommmandType) { if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("Clear same priority command: {}", m_Commondvector[j].strCommand.c_str()); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } m_Commondvector.erase(m_Commondvector.begin() + j); j--; } } m_Commondvector.push_back(tempCMD); } else if (m_CommondType[nType].bIsFirstSend) { m_Commondvector.push_front(tempCMD); std::string logstr; if (m_pLogFun && m_CommondType[nType].bIsPrintLog) { logstr = std::format("FirstSend to {}", tempCMD.strCommand.c_str()); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } } else { m_Commondvector.push_back(tempCMD); } uqeSection.unlock(); return RET_STATUS::RET_SUCCEED; } bool CDeliverModule::ReProcessCommand(tagCommandStruct strCommand) { if (!m_bSendEnable) { //不重发的命令,或者设备处于错误状态时不清空发送队列的命令,都不重发 return true; } std::unique_lock uqeSection(m_iCriticalSection); m_Commondvector.push_front(strCommand); uqeSection.unlock(); return true; } void CDeliverModule::SetACKCMDHead(char* strCommand, int nLengh) { std::string logstr; m_strACKHead = std::format("{}", strCommand); logstr = std::format("SetACKCMDHead [{}]", m_strACKHead); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } void CDeliverModule::SetCPCMDHead(char* strCommand, int nLengh) { std::string logstr; m_strCPHead.copy(strCommand, nLengh); m_strCPHead = std::format("{}", strCommand); logstr = std::format("SetCPCMDHead [{}]", m_strCPHead); } void CDeliverModule::SetSpeSelfHead(map cmdHeadmap) { m_SpeSelfMap = cmdHeadmap; } bool CDeliverModule::CheckReceive(const char* cmd, int nCMDHeadLengh) { std::string logstr; bool isPrintLog = m_CommondType[m_strcurrentcmd.nCommmandType].bIsPrintLog; nCMDHeadLengh = min(nCMDHeadLengh, m_strcurrentcmd.strCommand.size()); if (!m_strcurrentcmd.strCommand.empty()) { if (m_CommondType[m_strcurrentcmd.nCommmandType].bIsWaitforSelf) { if (m_pLogFun && isPrintLog) { logstr = std::format("try Check Self"); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } if (m_SpeSelfMap.empty()) { if (m_strcurrentcmd.strCommand.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0) { ReceiveSelf(isPrintLog); return true; } } else { std::string currentCmdHead(m_strcurrentcmd.strCommand,0, nCMDHeadLengh); auto item = m_SpeSelfMap.find(currentCmdHead); if (item != m_SpeSelfMap.end()) { if (item->second.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0) { ReceiveSelf(isPrintLog); return true; } } } } else if (m_CommondType[m_strcurrentcmd.nCommmandType].bIsWaitforACK) { if (m_strACKHead.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0) { ReceiveACK(true, isPrintLog); return true; } } else if (m_CommondType[m_strcurrentcmd.nCommmandType].bIsWaitforCP) { if (m_pLogFun && isPrintLog) { logstr = std::format("try Check CP"); m_pLogFun(logstr.c_str(), LOG_V2_DEBUG); } if (m_strCPHead.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0) { ReceiveCP(true, isPrintLog); return true; } } } return false; } void CDeliverModule::ReceiveSelf(bool isPrintLog) { if (m_pLogFun && isPrintLog) { m_pLogFun("Receive Self", LOG_V2_DEBUG); } SetEvent(m_hSelfEvent); } void CDeliverModule::ReceiveACK(bool IsACK, bool isPrintLog) { if (IsACK) { if (m_pLogFun && isPrintLog) { m_pLogFun("Receive AK", LOG_V2_DEBUG); } SetEvent(m_hACKEvent); } else { if (m_pLogFun && isPrintLog) { m_pLogFun("Receive NAK", LOG_V2_DEBUG); } } } void CDeliverModule::ReceiveCP(bool IsCP, bool isPrintLog) { if (IsCP) { if (m_pLogFun && isPrintLog) { m_pLogFun("Receive CP", LOG_V2_DEBUG); } SetEvent(m_hCPEvent); } else { } } void CDeliverModule::SetCurrentStatus(bool isUnitState) { m_bSendEnable = isUnitState; if (m_bSendEnable) { PulseEvent(m_hUnitStateChangeEvent); } m_nRsubmit = 0; } bool CDeliverModule::GetCurrentState() { return m_bSendEnable; } std::string CDeliverModule::GetCurrentCommand() { return m_strcurrentcmd.strCommand; } int CDeliverModule::GetCurrentCommandPriority() { return m_strcurrentcmd.nCommmandType; } }