123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- #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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<string, string> 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;
- }
- }
|