DeliverModule.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. #include "DeliverModule.h"
  2. namespace DIOS::Dev::MODLE::SerialGPM {
  3. //tagCommandType
  4. tagCommandType::tagCommandType(bool isPrintLog, bool isFirstSend, int isResend,
  5. bool isWait, int waitingTime,
  6. bool isWaitSelf, int watingSelftime,
  7. bool isWaitforACK, int waitingACKTime,
  8. bool isWaitforCP, int watingCPtime,
  9. bool isClearSendVector, bool isClearSamePriority,
  10. bool isReplace)
  11. {
  12. bIsPrintLog = isPrintLog;
  13. bIsFirstSend = isFirstSend;
  14. nIsResend = isResend;
  15. bIsWait = isWait;
  16. nWaitingTime = waitingTime;
  17. bIsWaitforSelf = isWaitSelf;
  18. nWaitingSelfTime = watingSelftime;
  19. bIsWaitforACK = isWaitforACK;
  20. nWaitingACKTime = waitingACKTime;
  21. bIsWaitforCP = isWaitforCP;
  22. nWatingCPTime = watingCPtime;
  23. bIsClearSendVector = isClearSendVector;
  24. bIsClearSamePriority = isClearSamePriority;
  25. bIsReplace = isReplace;
  26. };
  27. bool tagCommandType::operator ==(const tagCommandType& value)
  28. {
  29. if (bIsFirstSend == value.bIsFirstSend &&
  30. nIsResend == value.nIsResend &&
  31. bIsWait == value.bIsWait &&
  32. nWaitingTime == value.nWaitingTime &&
  33. bIsWaitforSelf == value.bIsWaitforSelf &&
  34. nWaitingSelfTime == value.nWaitingSelfTime &&
  35. bIsWaitforACK == value.bIsWaitforACK &&
  36. nWaitingACKTime == value.nWaitingACKTime &&
  37. bIsWaitforCP == value.bIsWaitforCP &&
  38. nWatingCPTime == value.nWatingCPTime &&
  39. bIsClearSendVector == value.bIsClearSendVector &&
  40. bIsClearSamePriority == value.bIsClearSamePriority &&
  41. bIsReplace == value.bIsReplace)
  42. return true;
  43. else
  44. return false;
  45. }
  46. //tagCommandStruct
  47. tagCommandStruct::tagCommandStruct() :strCommand(""), nCommmandType(0) {}
  48. tagCommandStruct::tagCommandStruct(std::string& cmd, int type) : strCommand(cmd), nCommmandType(type) {}
  49. tagCommandStruct::tagCommandStruct(char* cmd, int lengh, int type) :strCommand(cmd, lengh), nCommmandType(type) {}
  50. tagCommandStruct& tagCommandStruct::operator =(const tagCommandStruct& value)
  51. {
  52. strCommand = value.strCommand;
  53. nCommmandType = value.nCommmandType;
  54. return *this;
  55. }
  56. //CDeliverModule
  57. CDeliverModule::CDeliverModule()
  58. {
  59. m_ClientControl = NULL;
  60. m_pSendData = NULL;
  61. m_pLogFun = NULL;
  62. m_hSendingThreadToggleEvent = NULL;
  63. m_SendingCommandsThread = NULL;
  64. //m_nSendingCommandsThreadId = 0;
  65. m_bSendingCommandsThreadExit = false;
  66. m_Commondvector.clear();
  67. m_CommondType.clear();
  68. m_hSelfEvent = NULL;
  69. m_hACKEvent = NULL;
  70. m_hCPEvent = NULL;
  71. m_nRsubmit = 0;
  72. m_bSendEnable = true;
  73. m_hUnitStateChangeEvent = NULL;
  74. }
  75. CDeliverModule::~CDeliverModule(void)
  76. {
  77. EixtSendModle();
  78. }
  79. bool CDeliverModule::InitSendModle(void* ptrObj, SendCommands* pSendData, WriteLog* logFun)
  80. {
  81. m_ClientControl = ptrObj;
  82. m_pSendData = pSendData;
  83. m_pLogFun = logFun;
  84. //设备初始化的时候要把等待的事件设置为手动激活
  85. m_hSendingThreadToggleEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  86. m_hUnitStateChangeEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  87. m_hSelfEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  88. m_hACKEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  89. m_hCPEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  90. //并建立发送线程
  91. StartSendingCommandsThread();
  92. tagCommandType proiority0;
  93. m_CommondType.push_back(proiority0);
  94. return true;
  95. }
  96. void CDeliverModule::EixtSendModle()
  97. {
  98. StopSendingCommandsThread();
  99. CloseHandle(m_hSendingThreadToggleEvent);
  100. CloseHandle(m_hUnitStateChangeEvent);
  101. CloseHandle(m_hSelfEvent);
  102. CloseHandle(m_hACKEvent);
  103. CloseHandle(m_hCPEvent);
  104. m_Commondvector.clear();
  105. m_CommondType.clear();
  106. m_ClientControl = NULL;
  107. m_pSendData = NULL;
  108. m_pLogFun = NULL;
  109. }
  110. bool CDeliverModule::SetLogFun(WriteLog* logFun)
  111. {
  112. if (logFun != NULL)
  113. {
  114. m_pLogFun = logFun;
  115. return true;
  116. }
  117. return false;
  118. }
  119. int CDeliverModule::SetPriority(bool isPrintLog, bool isFirstSend, int isResend,
  120. bool isWait, int waitingTime,
  121. bool isWaitforSelf, int watingSelftime,
  122. bool isWaitforACK, int waitingACKTime,
  123. bool isWaitforCP, int watingCPtime,
  124. bool isClearSendVector, bool isClearSamePriority)
  125. {
  126. tagCommandType proiority(isPrintLog,isFirstSend, isResend,
  127. isWait, waitingTime,
  128. isWaitforSelf, watingSelftime,
  129. isWaitforACK, waitingACKTime,
  130. isWaitforCP, watingCPtime,
  131. isClearSendVector, isClearSamePriority);
  132. for (int i = 0; i < m_CommondType.size(); i++)
  133. {
  134. if (m_CommondType[i] == proiority)
  135. {
  136. if (m_pLogFun)
  137. {
  138. m_pLogFun("SetPriority:Priority already exist", LOG_V2_WARNING);
  139. return i;
  140. }
  141. }
  142. }
  143. m_CommondType.push_back(proiority);
  144. if (m_pLogFun)
  145. {
  146. m_pLogFun("SetPriority:add Priority successful", LOG_V2_DEBUG);
  147. }
  148. return m_CommondType.size() - 1;
  149. }
  150. int CDeliverModule::SetPriority_V2(bool isWaitforACK, int waitingACKTime,
  151. bool isResend, bool isClearSendVector, bool isFirstSend,
  152. bool isWaitforCP, int watingCPtime,
  153. bool isClearSamePriority)
  154. {
  155. tagCommandType proiority(true,isFirstSend, isResend,
  156. false, 0,
  157. false, 0,
  158. isWaitforACK, waitingACKTime,
  159. isWaitforCP, watingCPtime,
  160. isClearSendVector, isClearSamePriority);
  161. for (int i = 0; i < m_CommondType.size(); i++)
  162. {
  163. if (m_CommondType[i] == proiority)
  164. {
  165. if (m_pLogFun)
  166. {
  167. m_pLogFun("SetPriority_V2:Priority already exist", LOG_V2_WARNING);
  168. return i;
  169. }
  170. }
  171. }
  172. m_CommondType.push_back(proiority);
  173. m_pLogFun("SetPriority_V2:add Priority successful", LOG_V2_DEBUG);
  174. return m_CommondType.size();
  175. }
  176. int CDeliverModule::SetPriority_Replace(int nPriority, bool isReplace)
  177. {
  178. std::string logstr;
  179. if (nPriority >= 0 && nPriority < (m_CommondType.size()))
  180. {
  181. m_CommondType[nPriority].bIsReplace = isReplace;
  182. logstr = std::format("SetPriority_Replace:[{:d},{:d}]successful", nPriority, isReplace);
  183. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  184. }
  185. else
  186. {
  187. logstr = std::format("SetPriority_Replace:[{:d},{:d}]failed", nPriority, isReplace);
  188. m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  189. }
  190. return true;
  191. }
  192. bool CDeliverModule::StartSendingCommandsThread()
  193. {
  194. //create thread
  195. DWORD m_HardwareStatusID;
  196. m_SendingCommandsThread = CreateThread(0, 0, SendingCommandsThread, this, 0, &m_HardwareStatusID);
  197. if (NULL == m_SendingCommandsThread)
  198. {
  199. if (m_pLogFun)
  200. {
  201. m_pLogFun("StartSendingCommandsThread:Create sending commands thread failed!", LOG_V2_ERROR);
  202. }
  203. return false;
  204. }
  205. //wait till thread create successful
  206. if (WaitForSingleObject(m_hSendingThreadToggleEvent, 5000) == WAIT_OBJECT_0)
  207. {
  208. if (m_pLogFun)
  209. {
  210. m_pLogFun("StartSendingCommandsThread:detected the start of sending commands thread", LOG_V2_INFO);
  211. }
  212. ResetEvent(m_hSendingThreadToggleEvent);
  213. //m_nSendingCommandsThreadId = m_HardwareStatusID
  214. }
  215. else
  216. {
  217. if (m_pLogFun)
  218. {
  219. m_pLogFun("StartSendingCommandsThread:start of sending commands thread failed!", LOG_V2_ERROR);
  220. }
  221. return false;
  222. }
  223. return true;
  224. }
  225. void CDeliverModule::StopSendingCommandsThread()
  226. {
  227. if (NULL != m_SendingCommandsThread)
  228. {
  229. if (m_pLogFun)
  230. {
  231. m_pLogFun("StopSendingCommandsThread:sending commands thread exiting...", LOG_V2_INFO);
  232. }
  233. m_bSendingCommandsThreadExit = true;
  234. if (::WaitForSingleObject(m_hSendingThreadToggleEvent, 5000) == WAIT_OBJECT_0)
  235. {
  236. if (m_pLogFun)
  237. {
  238. m_pLogFun("StopSendingCommandsThread:detected the exit of sending commands thread", LOG_V2_INFO);
  239. }
  240. ResetEvent(m_hSendingThreadToggleEvent);
  241. }
  242. else
  243. {
  244. DWORD exitCode = 0;
  245. //Retrieves the termination status of the specified thread
  246. if (!GetExitCodeThread(m_SendingCommandsThread, &exitCode))
  247. {
  248. DWORD nError = ::GetLastError();
  249. std::string logstr;
  250. if (m_pLogFun)
  251. {
  252. logstr = std::format("StopSendingCommandsThread:GetExitCodeThread() error[{:d}]", nError);
  253. m_pLogFun(logstr.c_str(), LOG_V2_ERROR);
  254. }
  255. }
  256. TerminateThread(m_SendingCommandsThread, exitCode);
  257. }
  258. m_SendingCommandsThread = NULL;
  259. }
  260. }
  261. DWORD CDeliverModule::SendingCommandsThread(LPVOID pParam)
  262. {
  263. CDeliverModule* pCurrentUnit = (CDeliverModule*)pParam;
  264. if (pCurrentUnit->m_pLogFun)
  265. {
  266. pCurrentUnit->m_pLogFun("Enter SendingCommandsThread", LOG_V2_INFO);
  267. }
  268. else
  269. {
  270. return 0;
  271. }
  272. //indicating start thread success
  273. SetEvent(pCurrentUnit->m_hSendingThreadToggleEvent);
  274. std::string logstr;
  275. //perform sending sedecal commands
  276. while (!pCurrentUnit->m_bSendingCommandsThreadExit)
  277. {
  278. //query the command from the vector
  279. if (pCurrentUnit->m_Commondvector.size() > 0)
  280. {
  281. if (!pCurrentUnit->m_bSendEnable)
  282. {
  283. if (pCurrentUnit->m_pLogFun)
  284. {
  285. pCurrentUnit->m_pLogFun("SendCommand:In error state,and the command not send", LOG_V2_WARNING);
  286. }
  287. if (WAIT_OBJECT_0 == WaitForSingleObject(pCurrentUnit->m_hUnitStateChangeEvent, INFINITE))
  288. {
  289. if (pCurrentUnit->m_pLogFun)
  290. {
  291. pCurrentUnit->m_pLogFun("SendCommand:change to standby state,and the command begin send", LOG_V2_WARNING);
  292. }
  293. }
  294. }
  295. std::unique_lock<std::mutex> uqeSection(pCurrentUnit->m_iCriticalSection);
  296. pCurrentUnit->m_strcurrentcmd = pCurrentUnit->m_Commondvector[0];
  297. pCurrentUnit->m_Commondvector.pop_front();//erase the first command
  298. uqeSection.unlock();
  299. if (pCurrentUnit->m_strcurrentcmd.nCommmandType >= pCurrentUnit->m_CommondType.size())
  300. {
  301. if (pCurrentUnit->m_pLogFun)
  302. {
  303. logstr = std::format("SendCommand:commandtype[{}] not exist in [{}]",
  304. pCurrentUnit->m_strcurrentcmd.nCommmandType, pCurrentUnit->m_CommondType.size());
  305. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  306. }
  307. continue;
  308. }
  309. bool isPrintLog = pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsPrintLog;
  310. int eventArrayNum = 0,waitEventTime = 0, waitTime = 0;
  311. HANDLE eventArray[3];
  312. if (pCurrentUnit->m_strcurrentcmd.strCommand != "" && pCurrentUnit->m_strcurrentcmd.nCommmandType >= 0)// with commnad
  313. {
  314. if (pCurrentUnit->m_strcurrentcmd.nCommmandType == 0)//直接发送
  315. {
  316. pCurrentUnit->SendCommand(pCurrentUnit->m_strcurrentcmd.strCommand, pCurrentUnit->m_strcurrentcmd.nCommmandType);
  317. }
  318. else
  319. {
  320. if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWaitforSelf)//等待自己
  321. {
  322. eventArray[eventArrayNum] = pCurrentUnit->m_hSelfEvent;
  323. eventArrayNum++;
  324. waitEventTime += pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWaitingSelfTime;
  325. }
  326. if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWaitforACK)//等待ACK
  327. {
  328. eventArray[eventArrayNum] = pCurrentUnit->m_hACKEvent;
  329. eventArrayNum++;
  330. waitEventTime += pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWaitingACKTime;
  331. }
  332. if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWaitforCP)//等待CP
  333. {
  334. eventArray[eventArrayNum] = pCurrentUnit->m_hCPEvent;
  335. eventArrayNum++;
  336. waitEventTime += pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWatingCPTime;
  337. }
  338. if (pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].bIsWait)//等待
  339. {
  340. waitTime = pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nWaitingTime;
  341. }
  342. if (pCurrentUnit->m_pLogFun && isPrintLog)
  343. {
  344. logstr = std::format("SendingCommandsThread:get cmd[{}]type[{}] out from vector,begin wait [{}count,{}time]event [{}]time",
  345. pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType, eventArrayNum,waitEventTime, waitTime);
  346. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  347. }
  348. pCurrentUnit->SendCommand(pCurrentUnit->m_strcurrentcmd.strCommand, pCurrentUnit->m_strcurrentcmd.nCommmandType, isPrintLog);
  349. Sleep(waitTime);
  350. if (eventArrayNum > 0)
  351. {
  352. bool cmdOver = false;
  353. DWORD dwResult = WaitForMultipleObjects(eventArrayNum, eventArray, TRUE, waitEventTime);
  354. switch (dwResult)
  355. {
  356. case WAIT_OBJECT_0:
  357. {
  358. if (pCurrentUnit->m_pLogFun && isPrintLog)
  359. {
  360. logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] is over",
  361. pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType);
  362. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  363. }
  364. pCurrentUnit->m_nRsubmit = 0;
  365. cmdOver = true;
  366. }
  367. break;
  368. case WAIT_TIMEOUT:
  369. {
  370. if (pCurrentUnit->m_pLogFun && isPrintLog)
  371. {
  372. logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] is timeout",
  373. pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType);
  374. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  375. }
  376. cmdOver = false;
  377. }
  378. break;
  379. case WAIT_FAILED:
  380. {
  381. if (pCurrentUnit->m_pLogFun && isPrintLog)
  382. {
  383. logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] is failed",
  384. pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType);
  385. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  386. }
  387. pCurrentUnit->m_nRsubmit = 0;
  388. cmdOver = true;
  389. }
  390. break;
  391. }
  392. if (!cmdOver)
  393. {
  394. pCurrentUnit->m_nRsubmit++;
  395. if (pCurrentUnit->m_pLogFun && isPrintLog)
  396. {
  397. logstr = std::format("SendingCommandsThread:current try reSend count[{}],limit[{}]",
  398. pCurrentUnit->m_nRsubmit, pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nIsResend);
  399. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  400. }
  401. if (pCurrentUnit->m_nRsubmit <= pCurrentUnit->m_CommondType[pCurrentUnit->m_strcurrentcmd.nCommmandType].nIsResend)
  402. {
  403. pCurrentUnit->ReProcessCommand(pCurrentUnit->m_strcurrentcmd);
  404. }
  405. else
  406. {
  407. pCurrentUnit->m_nRsubmit = 0;
  408. if (pCurrentUnit->m_pLogFun && isPrintLog)
  409. {
  410. logstr = std::format("SendingCommandsThread:cmd[{}]type[{}] Exceeded attempts",
  411. pCurrentUnit->m_strcurrentcmd.strCommand.c_str(), pCurrentUnit->m_strcurrentcmd.nCommmandType);
  412. pCurrentUnit->m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  413. }
  414. }
  415. }
  416. }
  417. }
  418. Sleep(10);
  419. }
  420. else
  421. {
  422. Sleep(10);//no commnad to send, so sleep 10 ms
  423. }
  424. }
  425. else
  426. {
  427. Sleep(10);//no commnad to send, so sleep 10 ms
  428. }
  429. }
  430. if (pCurrentUnit->m_pLogFun)
  431. {
  432. pCurrentUnit->m_pLogFun("End SendingCommandsThread!", LOG_V2_INFO);
  433. }
  434. //indicating the end of thread;
  435. SetEvent(pCurrentUnit->m_hSendingThreadToggleEvent);
  436. return 0;
  437. }
  438. void CDeliverModule::SendCommand(std::string strcommand, int commandtype,bool isPrintLog)
  439. {
  440. std::string logstr;
  441. if (m_pLogFun && isPrintLog)
  442. {
  443. logstr = std::format("SendCommand:try send cmd[{:s}]type[{:d}]", strcommand.c_str(), commandtype);
  444. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  445. }
  446. //重置ACK、CP状态
  447. ResetEvent(m_hSelfEvent);
  448. ResetEvent(m_hACKEvent);
  449. ResetEvent(m_hCPEvent);
  450. //调用回调函数将指令发送
  451. if (m_pSendData)
  452. {
  453. m_pSendData(m_strcurrentcmd.strCommand.c_str(), m_strcurrentcmd.strCommand.length(), m_ClientControl);
  454. }
  455. else
  456. {
  457. if (m_pLogFun && isPrintLog)
  458. {
  459. m_pLogFun("SendCommand:m_pSendData is NULL", LOG_V2_WARNING);
  460. }
  461. return;
  462. }
  463. }
  464. RET_STATUS CDeliverModule::ProcessCommand(std::string strCommand, int nType, int nCMDHeadLengh)
  465. {
  466. std::string logstr;
  467. if (nType >= m_CommondType.size())
  468. {
  469. if (m_pLogFun)
  470. {
  471. logstr = std::format("ProcessCommand:[{:s},{:d}]is not found", strCommand.c_str(), nType);
  472. m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  473. }
  474. return RET_STATUS::RET_FAILED;
  475. }
  476. std::unique_lock<std::mutex> uqeSection(m_iCriticalSection);
  477. tagCommandStruct tempCMD(strCommand, nType);
  478. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  479. {
  480. logstr = std::format("ProcessCommand:[{:s},{:d}]", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  481. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  482. }
  483. if (m_CommondType[nType].bIsReplace)
  484. {
  485. for (int i = 0; i < m_Commondvector.size(); i++)
  486. {
  487. if (nCMDHeadLengh > 0)
  488. {
  489. if (m_Commondvector[i].strCommand.compare(0, nCMDHeadLengh, tempCMD.strCommand, 0, nCMDHeadLengh) == 0)
  490. {
  491. m_Commondvector[i] = tempCMD;
  492. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  493. {
  494. logstr = std::format("ProcessCommand:[{:s},{:d}][head[{}]]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType, nCMDHeadLengh);
  495. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  496. }
  497. uqeSection.unlock();
  498. return RET_STATUS::RET_SUCCEED;
  499. }
  500. }
  501. else
  502. {
  503. if (m_Commondvector[i].strCommand == tempCMD.strCommand)
  504. {
  505. m_Commondvector[i] = tempCMD;
  506. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  507. {
  508. logstr = std::format("ProcessCommand:[{:s},{:d}]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  509. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  510. }
  511. uqeSection.unlock();
  512. return RET_STATUS::RET_SUCCEED;
  513. }
  514. }
  515. }
  516. }
  517. else
  518. {
  519. logstr = std::format("ProcessCommand:[{:s},{:d}] not replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  520. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  521. }
  522. if (m_CommondType[nType].bIsClearSendVector)
  523. {
  524. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  525. {
  526. logstr = std::format("ProcessCommand:[{:s},{:d}] clear vector", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  527. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  528. m_Commondvector.clear();
  529. }
  530. m_Commondvector.push_back(tempCMD);
  531. }
  532. else if (m_CommondType[nType].bIsClearSamePriority)
  533. {
  534. for (int j = 0; j < m_Commondvector.size(); j++)
  535. {
  536. if (m_Commondvector[j].nCommmandType == tempCMD.nCommmandType)
  537. {
  538. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  539. {
  540. logstr = std::format("Clear same priority command: {}", m_Commondvector[j].strCommand.c_str());
  541. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  542. }
  543. m_Commondvector.erase(m_Commondvector.begin() + j);
  544. j--;
  545. }
  546. }
  547. if (m_CommondType[nType].bIsFirstSend)
  548. m_Commondvector.push_front(tempCMD);
  549. else
  550. m_Commondvector.push_back(tempCMD);
  551. }
  552. else if (m_CommondType[nType].bIsFirstSend)
  553. {
  554. m_Commondvector.push_front(tempCMD);
  555. std::string logstr;
  556. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  557. {
  558. logstr = std::format("FirstSend to {}", tempCMD.strCommand.c_str());
  559. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  560. }
  561. }
  562. else
  563. {
  564. m_Commondvector.push_back(tempCMD);
  565. }
  566. uqeSection.unlock();
  567. return RET_STATUS::RET_SUCCEED;
  568. }
  569. RET_STATUS CDeliverModule::ProcessCommand(char* strCommand, int nLengh, int nType, int nCMDHeadLengh)
  570. {
  571. std::string logstr;
  572. if (nType >= m_CommondType.size())
  573. {
  574. if (m_pLogFun)
  575. {
  576. logstr = std::format("ProcessCommand:[{:s},{:d}]is not found", strCommand, nType);
  577. m_pLogFun(logstr.c_str(), LOG_V2_WARNING);
  578. }
  579. return RET_STATUS::RET_FAILED;
  580. }
  581. std::unique_lock<std::mutex> uqeSection(m_iCriticalSection);
  582. tagCommandStruct tempCMD(strCommand, nLengh, nType);
  583. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  584. {
  585. logstr = std::format("ProcessCommand:[{:s},{:d}]", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  586. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  587. }
  588. for (int i = 0; i < m_Commondvector.size(); i++)
  589. {
  590. if (nCMDHeadLengh > 0)
  591. {
  592. if (m_Commondvector[i].strCommand.compare(0, nCMDHeadLengh, tempCMD.strCommand, 0, nCMDHeadLengh) == 0)
  593. {
  594. m_Commondvector[i] = tempCMD;
  595. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  596. {
  597. logstr = std::format("ProcessCommand:[{:s},{:d}][head[{}]]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType, nCMDHeadLengh);
  598. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  599. }
  600. uqeSection.unlock();
  601. return RET_STATUS::RET_SUCCEED;
  602. }
  603. }
  604. else
  605. {
  606. if (m_Commondvector[i].strCommand == tempCMD.strCommand)
  607. {
  608. m_Commondvector[i] = tempCMD;
  609. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  610. {
  611. logstr = std::format("ProcessCommand:[{:s},{:d}]is already in vector,so replace", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  612. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  613. }
  614. uqeSection.unlock();
  615. return RET_STATUS::RET_SUCCEED;
  616. }
  617. }
  618. }
  619. if (m_CommondType[nType].bIsClearSendVector)
  620. {
  621. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  622. {
  623. logstr = std::format("ProcessCommand:[{:s},{:d}] clear vector", tempCMD.strCommand.c_str(), tempCMD.nCommmandType);
  624. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  625. m_Commondvector.clear();
  626. }
  627. m_Commondvector.push_back(tempCMD);
  628. }
  629. else if (m_CommondType[nType].bIsClearSamePriority)
  630. {
  631. for (int j = 0; j < m_Commondvector.size(); j++)
  632. {
  633. if (m_Commondvector[j].nCommmandType == tempCMD.nCommmandType)
  634. {
  635. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  636. {
  637. logstr = std::format("Clear same priority command: {}", m_Commondvector[j].strCommand.c_str());
  638. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  639. }
  640. m_Commondvector.erase(m_Commondvector.begin() + j);
  641. j--;
  642. }
  643. }
  644. m_Commondvector.push_back(tempCMD);
  645. }
  646. else if (m_CommondType[nType].bIsFirstSend)
  647. {
  648. m_Commondvector.push_front(tempCMD);
  649. std::string logstr;
  650. if (m_pLogFun && m_CommondType[nType].bIsPrintLog)
  651. {
  652. logstr = std::format("FirstSend to {}", tempCMD.strCommand.c_str());
  653. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  654. }
  655. }
  656. else
  657. {
  658. m_Commondvector.push_back(tempCMD);
  659. }
  660. uqeSection.unlock();
  661. return RET_STATUS::RET_SUCCEED;
  662. }
  663. bool CDeliverModule::ReProcessCommand(tagCommandStruct strCommand)
  664. {
  665. if (!m_bSendEnable)
  666. {
  667. //不重发的命令,或者设备处于错误状态时不清空发送队列的命令,都不重发
  668. return true;
  669. }
  670. std::unique_lock<std::mutex> uqeSection(m_iCriticalSection);
  671. m_Commondvector.push_front(strCommand);
  672. uqeSection.unlock();
  673. return true;
  674. }
  675. void CDeliverModule::SetACKCMDHead(char* strCommand, int nLengh)
  676. {
  677. std::string logstr;
  678. m_strACKHead = std::format("{}", strCommand);
  679. logstr = std::format("SetACKCMDHead [{}]", m_strACKHead);
  680. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  681. }
  682. void CDeliverModule::SetCPCMDHead(char* strCommand, int nLengh)
  683. {
  684. std::string logstr;
  685. m_strCPHead.copy(strCommand, nLengh);
  686. m_strCPHead = std::format("{}", strCommand);
  687. logstr = std::format("SetCPCMDHead [{}]", m_strCPHead);
  688. }
  689. void CDeliverModule::SetSpeSelfHead(map<string, string> cmdHeadmap)
  690. {
  691. m_SpeSelfMap = cmdHeadmap;
  692. }
  693. bool CDeliverModule::CheckReceive(const char* cmd, int nCMDHeadLengh)
  694. {
  695. std::string logstr;
  696. bool isPrintLog = m_CommondType[m_strcurrentcmd.nCommmandType].bIsPrintLog;
  697. nCMDHeadLengh = min(nCMDHeadLengh, m_strcurrentcmd.strCommand.size());
  698. if (!m_strcurrentcmd.strCommand.empty())
  699. {
  700. if (m_CommondType[m_strcurrentcmd.nCommmandType].bIsWaitforSelf)
  701. {
  702. if (m_pLogFun && isPrintLog)
  703. {
  704. logstr = std::format("try Check Self");
  705. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  706. }
  707. if (m_SpeSelfMap.empty())
  708. {
  709. if (m_strcurrentcmd.strCommand.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0)
  710. {
  711. ReceiveSelf(isPrintLog);
  712. return true;
  713. }
  714. }
  715. else
  716. {
  717. std::string currentCmdHead(m_strcurrentcmd.strCommand,0, nCMDHeadLengh);
  718. auto item = m_SpeSelfMap.find(currentCmdHead);
  719. if (item != m_SpeSelfMap.end())
  720. {
  721. if (item->second.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0)
  722. {
  723. ReceiveSelf(isPrintLog);
  724. return true;
  725. }
  726. }
  727. }
  728. }
  729. else if (m_CommondType[m_strcurrentcmd.nCommmandType].bIsWaitforACK)
  730. {
  731. if (m_strACKHead.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0)
  732. {
  733. ReceiveACK(true, isPrintLog);
  734. return true;
  735. }
  736. }
  737. else if (m_CommondType[m_strcurrentcmd.nCommmandType].bIsWaitforCP)
  738. {
  739. if (m_pLogFun && isPrintLog)
  740. {
  741. logstr = std::format("try Check CP");
  742. m_pLogFun(logstr.c_str(), LOG_V2_DEBUG);
  743. }
  744. if (m_strCPHead.compare(0, nCMDHeadLengh, cmd, 0, nCMDHeadLengh) == 0)
  745. {
  746. ReceiveCP(true, isPrintLog);
  747. return true;
  748. }
  749. }
  750. }
  751. return false;
  752. }
  753. void CDeliverModule::ReceiveSelf(bool isPrintLog)
  754. {
  755. if (m_pLogFun && isPrintLog)
  756. {
  757. m_pLogFun("Receive Self", LOG_V2_DEBUG);
  758. }
  759. SetEvent(m_hSelfEvent);
  760. }
  761. void CDeliverModule::ReceiveACK(bool IsACK, bool isPrintLog)
  762. {
  763. if (IsACK)
  764. {
  765. if (m_pLogFun && isPrintLog)
  766. {
  767. m_pLogFun("Receive AK", LOG_V2_DEBUG);
  768. }
  769. SetEvent(m_hACKEvent);
  770. }
  771. else
  772. {
  773. if (m_pLogFun && isPrintLog)
  774. {
  775. m_pLogFun("Receive NAK", LOG_V2_DEBUG);
  776. }
  777. }
  778. }
  779. void CDeliverModule::ReceiveCP(bool IsCP, bool isPrintLog)
  780. {
  781. if (IsCP)
  782. {
  783. if (m_pLogFun && isPrintLog)
  784. {
  785. m_pLogFun("Receive CP", LOG_V2_DEBUG);
  786. }
  787. SetEvent(m_hCPEvent);
  788. }
  789. else
  790. {
  791. }
  792. }
  793. void CDeliverModule::SetCurrentStatus(bool isUnitState)
  794. {
  795. m_bSendEnable = isUnitState;
  796. if (m_bSendEnable)
  797. {
  798. PulseEvent(m_hUnitStateChangeEvent);
  799. }
  800. m_nRsubmit = 0;
  801. }
  802. bool CDeliverModule::GetCurrentState()
  803. {
  804. return m_bSendEnable;
  805. }
  806. std::string CDeliverModule::GetCurrentCommand()
  807. {
  808. return m_strcurrentcmd.strCommand;
  809. }
  810. int CDeliverModule::GetCurrentCommandPriority()
  811. {
  812. return m_strcurrentcmd.nCommmandType;
  813. }
  814. }