DeliverModule.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //#pragma once
  2. #ifndef DeliverModule_H_
  3. #define DeliverModule_H_
  4. #include <deque>
  5. #include <vector>
  6. #include <string>
  7. #include <mutex>
  8. #include <atomic>
  9. #include <windows.h>
  10. #define FMT_HEADER_ONLY
  11. #include "format.h"
  12. #include "format.cc"
  13. #include "DIOS.Dev.IODevice.hpp"
  14. namespace std {
  15. using fmt::format;
  16. using fmt::format_error;
  17. using fmt::formatter;
  18. }
  19. using std::string;
  20. using std::atomic_bool;
  21. namespace DIOS::Dev::MODLE::SerialGPM { //Serial General Processing Module
  22. //日志等级
  23. enum LOG_V2_LEVEL
  24. {
  25. LOG_V2_FATAL = 0, // Fatal Error.
  26. LOG_V2_ERROR = 1, // Error.
  27. LOG_V2_WARNING = 2, // Warning.
  28. LOG_V2_DEBUG = 3, // Debug.
  29. LOG_V2_INFO = 4 // Information.
  30. };
  31. //通过回调函数调用上层处理函数
  32. typedef void __stdcall SendCommands(const char* pData, unsigned long DataLength, void* ptrObj);
  33. //通过回调函数写日志
  34. typedef void __stdcall WriteLog(const char* pData, LOG_V2_LEVEL level);
  35. //指令类型结构体
  36. class tagCommandType
  37. {
  38. public:
  39. bool bIsFirstSend; //是否第一个发送
  40. int nIsResend; //是否重发:0 不重发,n>0 重发n次(ACK与CP一共的)
  41. bool bIsWait; //是否等待
  42. int nWaitingTime; //等待时间
  43. bool bIsWaitforSelf; //是否等待自己
  44. int nWaitingSelfTime; //等待自己时间
  45. bool bIsWaitforACK; //是否等待ACK
  46. int nWaitingACKTime; //等待ACK时间
  47. bool bIsWaitforCP; //是否等待CP
  48. int nWatingCPTime; //等待CP时间
  49. bool bIsClearSendVector; //是否清空发送队列
  50. bool bIsClearSamePriority; //是否清除同样的优先级
  51. bool bIsReplace; //是否替换CMD头相同的指令
  52. bool bIsPrintLog; //是否打印日志
  53. tagCommandType(bool isPrintLog = false, bool isFirstSend = false, int isResend = 0,
  54. bool isWait = false, int waitingTime = 0,
  55. bool isWaitforSelf = false, int watingSelfCPtime = 0,
  56. bool isWaitforACK = false, int waitingACKTime = 0,
  57. bool isWaitforCP = false, int watingCPtime = 0,
  58. bool isClearSendVector = false, bool isClearSamePriority = false,
  59. bool isReplace = true);
  60. bool operator ==(const tagCommandType& value);
  61. };
  62. //发送指令结构体
  63. struct tagCommandStruct
  64. {
  65. string strCommand; //指令内容
  66. int nCommmandType; //指令类型:tagCommandType list number
  67. tagCommandStruct();
  68. tagCommandStruct(string& cmd, int type);
  69. tagCommandStruct(char* cmd, int lengh, int type);
  70. tagCommandStruct& operator =(const tagCommandStruct& value);
  71. };
  72. //串口处理层
  73. class CDeliverModule
  74. {
  75. public:
  76. CDeliverModule();
  77. ~CDeliverModule(void);
  78. public://发送线程相关的函数
  79. bool InitSendModle(void* ptrObj, SendCommands* pSendData, WriteLog * logFun = NULL);
  80. void EixtSendModle();
  81. //设置日志回调函数
  82. bool SetLogFun(WriteLog* logFun);
  83. //设置优先级: 依次设置指令类型,默认包含一个等级 0,代表直接发送无等待
  84. int SetPriority(bool isPrintLog = false, bool isFirstSend = false, int isResend = 0,
  85. bool isWait = false, int waitingTime = 0,
  86. bool isWaitforSelf = false, int watingSelftime = 0,
  87. bool isWaitforACK = false, int waitingACKTime = 0,
  88. bool isWaitforCP = false, int watingCPtime = 0,
  89. bool isClearSendVector = false, bool isClearSamePriority = false);
  90. int SetPriority_V2(bool isWaitforACK = true, int waitingACKTime = 350,
  91. bool isResend = true, bool isClearSendVector = false, bool isFirstSend = false,
  92. bool isWaitforCP = false, int watingCPtime = 3000,
  93. bool isClearSamePriority = false);
  94. //设置优先级指令是否替换指令头相同的指令
  95. int SetPriority_Replace(int nPriority, bool isReplace);
  96. //设置发送指令
  97. RET_STATUS ProcessCommand(string strCommand, int nType = 0, int nCMDHeadLengh = 0);
  98. RET_STATUS ProcessCommand(char* strCommand, int nLengh, int nType = 0, int nCMDHeadLengh = 0);
  99. //设置ACK,CP指令头
  100. void SetACKCMDHead(char* strCommand, int nLengh);
  101. void SetCPCMDHead(char* strCommand, int nLengh);
  102. void SetSpeSelfHead(map<string, string> cmdHeadmap);
  103. //设置设备处于错误
  104. void SetCurrentStatus(bool isUnitState);
  105. //获取当前错误
  106. bool GetCurrentState();
  107. //获取当前处理指令
  108. string GetCurrentCommand();
  109. //获取当前命令优先级
  110. int GetCurrentCommandPriority();
  111. //收到返回
  112. bool CheckReceive(const char * cmd, int nCMDHeadLengh = 0);
  113. void ReceiveSelf(bool isPrintLog = true);
  114. void ReceiveACK(bool IsACK = true, bool isPrintLog = true);
  115. void ReceiveCP(bool IsCP = true, bool isPrintLog = true);
  116. private:
  117. //开启发送线程
  118. bool StartSendingCommandsThread();
  119. void StopSendingCommandsThread();
  120. void SendCommand(string strcommand, int commandtype, bool isPrintLog = true);
  121. static DWORD SendingCommandsThread(LPVOID pParam);
  122. bool ReProcessCommand(tagCommandStruct strCommand);
  123. void* m_ClientControl; //模块对象
  124. SendCommands* m_pSendData; //发送数据
  125. WriteLog* m_pLogFun; //日志记录接口
  126. HANDLE m_hSendingThreadToggleEvent; //发送事件
  127. HANDLE m_SendingCommandsThread; //发送线程
  128. //int m_nSendingCommandsThreadId; //发送线程ID
  129. atomic_bool m_bSendingCommandsThreadExit; //发送线程退出标志
  130. std::mutex m_iCriticalSection; //指令队列锁
  131. std::deque <tagCommandStruct> m_Commondvector; //指令发送队列
  132. std::vector <tagCommandType> m_CommondType; //指令类型队列
  133. string m_strACKHead; //设置ACK指令头
  134. string m_strCPHead; //设置CP指令头
  135. map<string, string> m_SpeSelfMap; //设置特殊Self指令头对照表(适用于发送的指令头与返回的指令头不同,且区别于ACK、CP这种固定的返回头的情况)
  136. tagCommandStruct m_strcurrentcmd; //当前指令
  137. HANDLE m_hSelfEvent; //Self事件
  138. HANDLE m_hACKEvent; //ACK事件
  139. HANDLE m_hCPEvent; //CP事件
  140. int m_nRsubmit; //当前重试次数
  141. atomic_bool m_bSendEnable; //设备是否可发送
  142. HANDLE m_hUnitStateChangeEvent; //设备状态变更事件
  143. };
  144. }
  145. #endif