CCOS.Dev.Generator.PSG_HD.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 _CCOSDEVGENPSGHD_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_HD_REGULATION_LEVEL { //故障等级
  32. REG_ERRO,
  33. REG_WARN
  34. };
  35. enum PSG_HD_CHARGE_STATE { //电池状态
  36. POWER_LOW,
  37. POWER_NOR
  38. };
  39. #define PSGHD_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. // PSGHDDevice
  46. //-----------------------------------------------------------------------------
  47. class _CCOSDEVGENPSGHD_API PSGHDDevice : public IODeviceDetail,public GeneratorMould
  48. {
  49. using super = IODeviceDetail;
  50. using superGen = GeneratorMould;
  51. public:
  52. PSGHDDevice (std::shared_ptr <IOEventCenter> center, std::shared_ptr<SCFWrapper> SCF, string configfile);
  53. ~PSGHDDevice ();
  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 Reset()override;
  80. virtual RET_STATUS ActiveSyncMode(_tSyncModeArgs value) override;
  81. virtual RET_STATUS SetCollimatorLight(unsigned short value) override;
  82. virtual RET_STATUS SetDeviceSleepState(const int value) override;
  83. RET_STATUS RefreshData();
  84. RET_STATUS SetRPS(int rps);//设置连续点片帧率,暂不使用
  85. virtual RET_STATUS SetAECDensity(int value) override;
  86. virtual RET_STATUS SetAECField(int value) override;
  87. virtual RET_STATUS SetAECFilm(int value) override;
  88. virtual RET_STATUS SetWS(const std::string value) override;
  89. virtual RET_STATUS SetGenSynState(int value) override;
  90. virtual RET_STATUS SetGenState(int value) override;
  91. virtual RET_STATUS SetExpMode(std::string value) override;
  92. virtual RET_STATUS SetFrameRate(float frameRate) override;
  93. bool EnableBucky(int nbucky);
  94. bool ECHO(void);
  95. bool SetABSModeNative(int nMode);
  96. RET_STATUS Clear_DAP();
  97. RET_STATUS GetValue_DAP(float& value);
  98. RET_STATUS StartMove();
  99. RET_STATUS EndMove();
  100. //V3新方法
  101. virtual void SubscribeSelf(ccos_mqtt_connection* conn) override;
  102. RET_STATUS SetVibrationGrid(int value);
  103. RET_STATUS GetVibrationGridMS(int &value);
  104. void SetSmartAEC(int value); //智能AEC
  105. bool ReConnect(); //重连
  106. int GridMSMargin(); //返回震动栅MS的余量
  107. bool CalculateAppropriateMA(float& inoutMAS, float& inoutMA, float& inoutMS); //计算震动栅模式下恰当的MA
  108. void ReSendFailedAction(string &cmdNum); //重发失败后返回消息
  109. int GetGenState();
  110. int LoadConfig(string configfile);
  111. public:
  112. ResDataObject m_GenConfig; //driver's config file.
  113. std::shared_ptr<SCFWrapper> m_SCF;
  114. std::shared_ptr<IODeviceDetail> pIODriver;
  115. bool HeartBeatFlag;
  116. //线程变量互斥锁
  117. atomic<int> m_iReSendCMD; //初始命令重发次数
  118. static atomic<int> m_iLoopTime; //循环间隔时间(毫秒)
  119. atomic<int> m_iCompPostMAS; //计算PostMAS条件统计
  120. atomic<int> m_iHeartBeats; //心跳统计
  121. atomic<bool> m_bConnectFlag; //连接标记
  122. bool m_bAECCtlSignal; //在曝光设置更新期间,AEC控制信号必须处于低状态
  123. atomic<bool> m_iVibrationGridState; //震动栅模式(0,1)
  124. int m_iGridMSRadMargin; //震动栅ms手动曝光误差(500-2000ms)
  125. int m_iGridMSAECMargin; //震动栅ms自动曝光误差(500-2000ms)
  126. static atomic<bool> m_bExtraFlag; //使用标记
  127. bool m_bSleepState{ false };
  128. bool m_bGenBusy;
  129. int m_nCtlMode;
  130. bool m_bUseCECmd;
  131. bool m_bIsConfigLoaded;
  132. //串口处理层
  133. int m_nCMDType_WaitTime{ 0 };
  134. int m_nCMDType_HB{ 0 };
  135. int m_nCMDType_WaitACK{ 0 };
  136. static void ProcessClientData(const char* pData, unsigned long DataLength, void* lparam);
  137. RET_STATUS HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh = 3);
  138. RET_STATUS HWSendHBCMD(char* strCommand, int lengh, int headLengh = 3);
  139. RET_STATUS HWSendWaitACKCMD(char* strCommand, int lengh, int headLengh = 3);
  140. private:
  141. //std::unique_ptr<DevDAP::DOSEMould> m_DAP; //暂不使用
  142. std::unique_ptr<nsDetail::MSGUnit> m_MSGUnit; //处理消息上报对象指针
  143. std::thread m_pHardwareStatusThread; //轮询线程句柄
  144. std::thread m_pHardwareRsSendThread; //重发线程句柄
  145. std::shared_ptr<LinuxEvent> m_hGenPostEvent;
  146. atomic<bool> m_bExpEnable; //曝光使能
  147. atomic<bool> m_bInvalidKVMASSetupFlag; //写入到设备的曝光设置无效
  148. atomic<bool> m_bFaultList[18]; //记录 主机请求固件报告故障命令 返回的故障记录
  149. int m_iMaxPower; //最大功率:kV*mA
  150. int MaxHeatContent; //最阳极热容积:kV*mAs
  151. bool m_bMAS_MA_AEC;
  152. enum MyEnum
  153. {
  154. EXPOSURE_SOFTWARE_HARDWARE, //0
  155. EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN, //1
  156. EXPOSURE_SOFTWARE_NOSYNBOX //2
  157. };
  158. private:
  159. RET_STATUS HWSend(const char* strCommand, int lengh, bool reSend = false, int nTimeOut = TIMEOUTVALUE); //指令发送接口
  160. void OnCallBack(); //处理指令回调函数
  161. void Register(); //注册对外提供的属性、方法
  162. bool StartHardwareStatusThread(); //启动轮询线程
  163. static void HardwareStatusThread(PSGHDDevice* pParam); //定时查询状态信息
  164. static void HardwareReSendThread(PSGHDDevice* pParam); //指令重发
  165. void FireNotify(std::string key, int context);//向上层上报消息
  166. void FireNotify(std::string key, float context);
  167. void FireNotify(std::string key, std::string context);
  168. void FireErrorMessage(const bool Act, const int Code, const char* ResInfo = ""); //上报错误消息
  169. void FireWarnMessage(const bool Act, const int Code, const char* ResInfo = ""); //上报告警消息
  170. };
  171. }
  172. //-----------------------------------------------------------------------------
  173. // PSGHDDriver
  174. //-----------------------------------------------------------------------------
  175. namespace CCOS::Dev::Detail::Generator
  176. {
  177. class _CCOSDEVGENPSGHD_API PSGHDDriver : public DriverMould
  178. {
  179. public:
  180. PSGHDDriver ();
  181. virtual ~PSGHDDriver ();
  182. public:
  183. //virtual bool DriverEntry (std::string CfgFileName); //设置配制文件路径,对外接口最先调用
  184. virtual void Prepare () override; //在 DriverEntry 之后执行;选择与物理设备通信方式(串口、TCP)
  185. virtual std::string DriverProbe() override; //在 Prepare 之后执行;读取配置文件模块参数,供上层创建驱动work路径
  186. bool ReConnection();
  187. virtual bool DATA_ACTION Connect () override; //在 DriverProbe 之后执行;根据通信方式与物理设备进行连接
  188. virtual auto CreateDevice(int index)->std::unique_ptr <IODevice> override; //在 Connect 之后执行;创建逻辑设备,供上层创建驱动树节点
  189. virtual bool isConnected() const override; //检查驱动与物理设备连接状态
  190. virtual std::string GetResource() override; //获取配置文件的值
  191. virtual std::string DeviceProbe() override; //读取配置文件模块参数,供上层创建设备work路径
  192. virtual void Disconnect() override; //断开驱动与物理设备连接状态
  193. virtual void Dequeue (const char * Packet, DWORD Length) ; //在super::Connect中轮询,调用 DecodeFrame:查找指令操作对照表,通过对应操作更新数据并调用FireNotify上报更新
  194. virtual void FireNotify (int code, std::string key, std::string content); //向监听者上报事件
  195. static PACKET_RET callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength); //判断从设备读到的数据有没有可用的数据包,有则最终调度到 Dequeue
  196. std::shared_ptr<SCFWrapper> m_scfWrapper;
  197. private:
  198. bool SaveConfigFile(bool bSendNotify);
  199. virtual bool GetDeviceConfig(std::string& Cfg) override;
  200. virtual bool SetDeviceConfig(std::string Cfg) override;
  201. bool GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue);
  202. bool SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue);
  203. std::string m_SCFDllName;
  204. ResDataObject m_DeviceConfig;
  205. ResDataObject m_ConfigAll; //存储当前的配置,用于修改配置时写回文件
  206. ResDataObject m_Configurations; //存储当前配置中“CONFIGURATION”节点的内容
  207. enum class ConnectionState {
  208. Disconnected,
  209. Connecting,
  210. Connected,
  211. Failed
  212. };
  213. std::atomic<ConnectionState> m_connectionState{ ConnectionState::Disconnected };
  214. std::chrono::steady_clock::time_point m_lastConnectionAttempt;
  215. std::mutex m_connectionMutex;
  216. int m_connectionRetryCount{ 0 };
  217. const int MAX_RETRY_COUNT = 3;
  218. const std::chrono::seconds RETRY_INTERVAL{ 5 };
  219. //webconfig使用
  220. ResDataObject m_DeviceConfigSend;
  221. string g_strAppPath;
  222. std::unique_ptr <ResDataObject> m_pAttribute;
  223. std::unique_ptr <ResDataObject> m_pDescription;
  224. PSGHDDevice* m_pDevice{ nullptr };
  225. };
  226. }