CCOS.Dev.Generator.PSG_HR.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #pragma once
  2. #include <thread>
  3. #include <mutex>
  4. #include <condition_variable>
  5. #include <atomic>
  6. #include <memory>
  7. #include <functional>
  8. #include <list>
  9. #include <cassert>
  10. #include <cstring>
  11. #include "SCFWrapper.h"
  12. #include "CCOS.Dev.IODevice.Detail.hpp"
  13. #include "CCOS.Dev.MSGMould.hpp"
  14. #include "CCOS.Dev.Generator.Mould.hpp"
  15. #define _CCOSDEVGENPSGHR_API __attribute__((visibility("default")))
  16. // Linux 兼容的类型定义
  17. using DWORD = unsigned long;
  18. using LPVOID = void*;
  19. using BOOL = int;
  20. #define TRUE 1
  21. #define FALSE 0
  22. // 睡眠函数(毫秒)
  23. inline void Sleep(unsigned int milliseconds) {
  24. std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
  25. }
  26. // 创建线程函数
  27. template<typename Function, typename... Args>
  28. std::thread CreateThread(void*, int, Function&& f, Args&&... args) {
  29. return std::thread(std::forward<Function>(f), std::forward<Args>(args)...);
  30. }
  31. enum PSG_HR_REGULATION_LEVEL { //故障等级
  32. REG_ERRO,
  33. REG_WARN
  34. };
  35. enum PSG_HR_CHARGE_STATE { //电池状态
  36. POWER_LOW,
  37. POWER_NOR
  38. };
  39. #define PSGHR_Com_ReSendLen 32
  40. namespace CCOS::Dev::Detail::Generator
  41. {
  42. using cbFun = std::function <void(char*, int)>;
  43. static const int TIMEOUTVALUE = 100;
  44. //-----------------------------------------------------------------------------
  45. // PSGHRDevice
  46. //-----------------------------------------------------------------------------
  47. class _CCOSDEVGENPSGHR_API PSGHRDevice : public IODeviceDetail,public GeneratorMould
  48. {
  49. using superGen = GeneratorMould;
  50. using super = IODeviceDetail;
  51. public:
  52. PSGHRDevice (std::shared_ptr <IOEventCenter> center, std::shared_ptr<SCFWrapper> SCF, string configfile);
  53. ~PSGHRDevice ();
  54. //发生器支持的通用命令
  55. virtual std::string GetGUID() const override;
  56. virtual RET_STATUS IncKV() override;
  57. virtual RET_STATUS DecKV() override;
  58. virtual RET_STATUS SetKV(float value) override;
  59. virtual RET_STATUS IncMA() override;
  60. virtual RET_STATUS DecMA() override;
  61. virtual RET_STATUS SetMA(float value) override;
  62. virtual RET_STATUS IncMS() override;
  63. virtual RET_STATUS DecMS() override;
  64. virtual RET_STATUS SetMS(float value) override;
  65. virtual RET_STATUS IncMAS() override;
  66. virtual RET_STATUS DecMAS() override;
  67. virtual RET_STATUS SetMAS(float value) override;
  68. virtual RET_STATUS SetTechmode(int value) override;
  69. virtual RET_STATUS SetEXAMMode(std::string value) override;
  70. virtual RET_STATUS SetFocus(int value) override;
  71. virtual RET_STATUS SetAPR(const _tAPRArgs& t) override;
  72. virtual RET_STATUS QueryHE(int& value) override;
  73. virtual RET_STATUS QueryPostKV(float& value) override;
  74. virtual RET_STATUS QueryPostMA(float& value) override;
  75. virtual RET_STATUS QueryPostMS(float& value) override;
  76. virtual RET_STATUS QueryPostMAS(float& value) override;
  77. virtual RET_STATUS SetExpEnable() override;
  78. virtual RET_STATUS SetExpDisable()override;
  79. virtual RET_STATUS PrepareAcquisition()override;
  80. virtual RET_STATUS Reset()override;
  81. virtual RET_STATUS ActiveSyncMode(_tSyncModeArgs value) override;
  82. RET_STATUS RefreshData();
  83. RET_STATUS SetRPS(int rps);//设置连续点片帧率,暂不使用
  84. /********************透视命令****************************/
  85. virtual RET_STATUS SetAPF(const _tAPFArgs& t) override;
  86. virtual RET_STATUS IncFluKV() override;
  87. virtual RET_STATUS DecFluKV() override;
  88. virtual RET_STATUS SetFluKV(float value) override;
  89. virtual RET_STATUS IncFluMA() override;
  90. virtual RET_STATUS DecFluMA() override;
  91. virtual RET_STATUS SetFluMA(float value) override;
  92. virtual RET_STATUS INCPPS() override;
  93. virtual RET_STATUS DECPPS() override;
  94. virtual RET_STATUS SetPPS(float value) override;
  95. virtual RET_STATUS SetABSMode(int nMode) override;
  96. virtual RET_STATUS SetABSCurve(int curveNum) override;
  97. virtual RET_STATUS IncABSCurve() override;
  98. virtual RET_STATUS DecABSCurve() override;
  99. virtual RET_STATUS GetABSCurve();
  100. virtual float GetFluIntTimer() override;
  101. virtual float GetFluAccTimer() override;
  102. virtual RET_STATUS ResetFluTimer(int ntype) override;
  103. virtual RET_STATUS SetFluPre(int value) override;
  104. virtual RET_STATUS SetFluEXP(int value) override;
  105. virtual RET_STATUS SetFluMode(std::string value);
  106. virtual RET_STATUS SetFluDoseLever(int value) override;
  107. virtual RET_STATUS SetPluseWidth(float fplusewidth) override;
  108. virtual RET_STATUS SetAECDensity(int value) override;
  109. virtual RET_STATUS SetAECField(int value) override;
  110. virtual RET_STATUS SetAECFilm(int value) override;
  111. virtual RET_STATUS SetWS(const std::string value) override;
  112. virtual RET_STATUS SetGenSynState(int value) override;
  113. virtual RET_STATUS SetGenState(int value) override;
  114. virtual RET_STATUS SetExpMode(std::string value) override;
  115. virtual RET_STATUS SetFrameRate(float frameRate) override;
  116. virtual RET_STATUS SetFLFMode(std::string value) override;
  117. virtual RET_STATUS SetFLLever(float value);
  118. bool EnableBucky(int nbucky);
  119. bool ECHO(void);
  120. bool SetABSModeNative(int nMode);
  121. RET_STATUS Clear_DAP();
  122. RET_STATUS GetValue_DAP(float& value);
  123. RET_STATUS StartMove();
  124. RET_STATUS EndMove();
  125. //V3新方法
  126. virtual void SubscribeSelf(ccos_mqtt_connection* conn) override;
  127. RET_STATUS SetVibrationGrid(int value);
  128. RET_STATUS GetVibrationGridMS(int &value);
  129. void SetSmartAEC(int value); //智能AEC
  130. bool ReConnect(); //重连
  131. int GridMSMargin(); //返回震动栅MS的余量
  132. bool CalculateAppropriateMA(float& inoutMAS, float& inoutMA, float& inoutMS); //计算震动栅模式下恰当的MA
  133. void ReSendFailedAction(string &cmdNum); //重发失败后返回消息
  134. int GetGenState();
  135. int LoadConfig(string configfile);
  136. public:
  137. ResDataObject m_GenConfig; //driver's config file.
  138. std::shared_ptr<SCFWrapper> m_SCF;
  139. std::shared_ptr<IODeviceDetail> pIODriver;
  140. bool HeartBeatFlag;
  141. //线程变量互斥锁
  142. atomic<int> m_iReSendCMD; //初始命令重发次数
  143. static atomic<int> m_iLoopTime; //循环间隔时间(毫秒)
  144. atomic<int> m_iCompPostMAS; //计算PostMAS条件统计
  145. atomic<int> m_iHeartBeats; //心跳统计
  146. atomic<bool> m_bConnectFlag; //连接标记
  147. bool m_bAECCtlSignal; //在曝光设置更新期间,AEC控制信号必须处于低状态
  148. atomic<bool> m_iVibrationGridState; //震动栅模式(0,1)
  149. int m_iGridMSRadMargin; //震动栅ms手动曝光误差(500-2000ms)
  150. int m_iGridMSAECMargin; //震动栅ms自动曝光误差(500-2000ms)
  151. static atomic<bool> m_bExtraFlag; //使用标记
  152. bool m_isFirstHWPhase = true;
  153. bool m_bGenBusy;
  154. int m_nCtlMode;
  155. bool m_bUseCECmd;
  156. bool m_bIsConfigLoaded;
  157. //串口处理层
  158. int m_nCMDType_WaitTime{ 0 };
  159. int m_nCMDType_HB{ 0 };
  160. int m_nCMDType_WaitACK{ 0 };
  161. static void ProcessClientData(const char* pData, unsigned long DataLength, void* lparam);
  162. RET_STATUS HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh = 3);
  163. RET_STATUS HWSendHBCMD(char* strCommand, int lengh, int headLengh = 3);
  164. RET_STATUS HWSendWaitACKCMD(char* strCommand, int lengh, int headLengh = 3);
  165. private:
  166. //std::unique_ptr<DevDAP::DOSEMould> m_DAP; //暂不使用
  167. std::unique_ptr<nsDetail::MSGUnit> m_MSGUnit; //处理消息上报对象指针
  168. std::thread m_pHardwareStatusThread; //轮询线程句柄
  169. std::thread m_pHardwareRsSendThread; //重发线程句柄
  170. std::shared_ptr<LinuxEvent> m_hGenPostEvent;
  171. atomic<bool> m_bExpEnable; //曝光使能
  172. atomic<bool> m_bInvalidKVMASSetupFlag; //写入到设备的曝光设置无效
  173. atomic<bool> m_bFaultList[18]; //记录 主机请求固件报告故障命令 返回的故障记录
  174. int m_iMaxPower; //最大功率:kV*mA
  175. int MaxHeatContent; //最阳极热容积:kV*mAs
  176. bool m_bMAS_MA_AEC;
  177. enum MyEnum
  178. {
  179. EXPOSURE_SOFTWARE_HARDWARE, //0
  180. EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN, //1
  181. EXPOSURE_SOFTWARE_NOSYNBOX //2
  182. };
  183. private:
  184. RET_STATUS HWSend(const char* strCommand, int lengh, bool reSend = false, int nTimeOut = TIMEOUTVALUE); //指令发送接口
  185. void OnCallBack(); //处理指令回调函数
  186. void Register(); //注册对外提供的属性、方法
  187. bool StartHardwareStatusThread(); //启动轮询线程
  188. static void HardwareStatusThread(PSGHRDevice* pParam); //定时查询状态信息
  189. static void HardwareReSendThread(PSGHRDevice* pParam); //指令重发
  190. void FireNotify(std::string key, int context);//向上层上报消息
  191. void FireNotify(std::string key, float context);
  192. void FireNotify(std::string key, std::string context);
  193. void FireErrorMessage(const bool Act, const int Code, const char* ResInfo = ""); //上报错误消息
  194. void FireWarnMessage(const bool Act, const int Code, const char* ResInfo = ""); //上报告警消息
  195. };
  196. }
  197. //-----------------------------------------------------------------------------
  198. // PSGHRDriver
  199. //-----------------------------------------------------------------------------
  200. namespace CCOS::Dev::Detail::Generator
  201. {
  202. class _CCOSDEVGENPSGHR_API PSGHRDriver : public DriverMould
  203. {
  204. public:
  205. PSGHRDriver ();
  206. virtual ~PSGHRDriver ();
  207. public:
  208. //virtual bool DriverEntry (std::string CfgFileName); //设置配制文件路径,对外接口最先调用
  209. virtual void Prepare () override; //在 DriverEntry 之后执行;选择与物理设备通信方式(串口、TCP)
  210. virtual std::string DriverProbe() override; //在 Prepare 之后执行;读取配置文件模块参数,供上层创建驱动work路径
  211. bool ReConnection();
  212. virtual bool DATA_ACTION Connect () override; //在 DriverProbe 之后执行;根据通信方式与物理设备进行连接
  213. virtual auto CreateDevice(int index)->std::unique_ptr <IODevice> override; //在 Connect 之后执行;创建逻辑设备,供上层创建驱动树节点
  214. virtual bool isConnected() const override; //检查驱动与物理设备连接状态
  215. virtual std::string GetResource() override; //获取配置文件的值
  216. virtual std::string DeviceProbe() override; //读取配置文件模块参数,供上层创建设备work路径
  217. virtual void Disconnect() override; //断开驱动与物理设备连接状态
  218. virtual void Dequeue (const char * Packet, DWORD Length) ; //在super::Connect中轮询,调用 DecodeFrame:查找指令操作对照表,通过对应操作更新数据并调用FireNotify上报更新
  219. virtual void FireNotify (int code, std::string key, std::string content); //向监听者上报事件
  220. static PACKET_RET callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength); //判断从设备读到的数据有没有可用的数据包,有则最终调度到 Dequeue
  221. std::shared_ptr<SCFWrapper> m_scfWrapper;
  222. private:
  223. bool SaveConfigFile(bool bSendNotify);
  224. virtual bool GetDeviceConfig(std::string& Cfg) override;
  225. virtual bool SetDeviceConfig(std::string Cfg) override;
  226. bool GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue);
  227. bool SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue);
  228. std::string m_SCFDllName;
  229. ResDataObject m_DeviceConfig;
  230. ResDataObject m_ConfigAll; //存储当前的配置,用于修改配置时写回文件
  231. ResDataObject m_Configurations; //存储当前配置中“CONFIGURATION”节点的内容
  232. enum class ConnectionState {
  233. Disconnected,
  234. Connecting,
  235. Connected,
  236. Failed
  237. };
  238. enum class ConnectionType {
  239. Serial, // 串口
  240. Ethernet // 网口
  241. };
  242. std::atomic<ConnectionState> m_connectionState{ ConnectionState::Disconnected };
  243. std::chrono::steady_clock::time_point m_lastConnectionAttempt;
  244. std::mutex m_connectionMutex;
  245. const std::chrono::seconds RESET_RETRY_AFTER{ 60 };
  246. // 修改成员变量定义,添加mutable允许const函数修改
  247. mutable int m_connectionRetryCount{ 0 }; // 关键:用mutable修饰
  248. const int MAX_RETRY_COUNT = 3;
  249. const std::chrono::seconds RETRY_INTERVAL{ 5 };
  250. // 串口相关(固定支持的端口,可从配置扩展)
  251. std::vector<std::string> m_serialPorts{ "/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB3", "/dev/ttyUSB4" };
  252. int m_currentSerialPortIndex{ 0 }; // 当前尝试的串口端口索引
  253. ConnectionType m_currentConnType{ ConnectionType::Serial };
  254. //webconfig使用
  255. ResDataObject m_DeviceConfigSend;
  256. string g_strAppPath;
  257. std::unique_ptr <ResDataObject> m_pAttribute;
  258. std::unique_ptr <ResDataObject> m_pDescription;
  259. PSGHRDevice* m_pDevice{ nullptr };
  260. };
  261. }