#pragma once #include #include #include #include #include #include #include #include #include #include "SCFWrapper.h" #include "CCOS.Dev.IODevice.Detail.hpp" #include "CCOS.Dev.MSGMould.hpp" #include "CCOS.Dev.Generator.Mould.hpp" #define _CCOSDEVGENPSGHD_API __attribute__((visibility("default"))) // Linux 兼容的类型定义 using DWORD = unsigned long; using LPVOID = void*; using BOOL = int; #define TRUE 1 #define FALSE 0 // 睡眠函数(毫秒) inline void Sleep(unsigned int milliseconds) { std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); } // 创建线程函数 template std::thread CreateThread(void*, int, Function&& f, Args&&... args) { return std::thread(std::forward(f), std::forward(args)...); } enum PSG_HD_REGULATION_LEVEL { //故障等级 REG_ERRO, REG_WARN }; enum PSG_HD_CHARGE_STATE { //电池状态 POWER_LOW, POWER_NOR }; #define PSGHD_Com_ReSendLen 32 namespace CCOS::Dev::Detail::Generator { using cbFun = std::function ; static const int TIMEOUTVALUE = 100; //----------------------------------------------------------------------------- // PSGHDDevice //----------------------------------------------------------------------------- class _CCOSDEVGENPSGHD_API PSGHDDevice : public IODeviceDetail,public GeneratorMould { using super = IODeviceDetail; using superGen = GeneratorMould; public: PSGHDDevice (std::shared_ptr center, std::shared_ptr SCF, string configfile); ~PSGHDDevice (); //发生器支持的通用命令 virtual std::string GetGUID() const override; virtual RET_STATUS IncKV() override; virtual RET_STATUS DecKV() override; virtual RET_STATUS SetKV(float value) override; virtual RET_STATUS IncMA() override; virtual RET_STATUS DecMA() override; virtual RET_STATUS SetMA(float value) override; virtual RET_STATUS IncMS() override; virtual RET_STATUS DecMS() override; virtual RET_STATUS SetMS(float value) override; virtual RET_STATUS IncMAS() override; virtual RET_STATUS DecMAS() override; virtual RET_STATUS SetMAS(float value) override; virtual RET_STATUS SetTechmode(int value) override; virtual RET_STATUS SetEXAMMode(std::string value) override; virtual RET_STATUS SetFocus(int value) override; virtual RET_STATUS SetAPR(const _tAPRArgs& t) override; virtual RET_STATUS QueryHE(int& value) override; virtual RET_STATUS QueryPostKV(float& value) override; virtual RET_STATUS QueryPostMA(float& value) override; virtual RET_STATUS QueryPostMS(float& value) override; virtual RET_STATUS QueryPostMAS(float& value) override; virtual RET_STATUS SetExpEnable() override; virtual RET_STATUS SetExpDisable()override; virtual RET_STATUS PrepareAcquisition()override; virtual RET_STATUS Reset()override; virtual RET_STATUS ActiveSyncMode(_tSyncModeArgs value) override; virtual RET_STATUS SetCollimatorLight(unsigned short value) override; virtual RET_STATUS SetDeviceSleepState(const int value) override; RET_STATUS RefreshData(); RET_STATUS SetRPS(int rps);//设置连续点片帧率,暂不使用 virtual RET_STATUS SetAECDensity(int value) override; virtual RET_STATUS SetAECField(int value) override; virtual RET_STATUS SetAECFilm(int value) override; virtual RET_STATUS SetWS(const std::string value) override; virtual RET_STATUS SetGenSynState(int value) override; virtual RET_STATUS SetGenState(int value) override; virtual RET_STATUS SetExpMode(std::string value) override; virtual RET_STATUS SetFrameRate(float frameRate) override; bool EnableBucky(int nbucky); bool ECHO(void); bool SetABSModeNative(int nMode); RET_STATUS Clear_DAP(); RET_STATUS GetValue_DAP(float& value); RET_STATUS StartMove(); RET_STATUS EndMove(); //V3新方法 virtual void SubscribeSelf(ccos_mqtt_connection* conn) override; RET_STATUS SetVibrationGrid(int value); RET_STATUS GetVibrationGridMS(int &value); void SetSmartAEC(int value); //智能AEC bool ReConnect(); //重连 int GridMSMargin(); //返回震动栅MS的余量 bool CalculateAppropriateMA(float& inoutMAS, float& inoutMA, float& inoutMS); //计算震动栅模式下恰当的MA void ReSendFailedAction(string &cmdNum); //重发失败后返回消息 int GetGenState(); int LoadConfig(string configfile); public: ResDataObject m_GenConfig; //driver's config file. std::shared_ptr m_SCF; std::shared_ptr pIODriver; //线程变量互斥锁 atomic m_iReSendCMD; //初始命令重发次数 static atomic m_iLoopTime; //循环间隔时间(毫秒) atomic m_iCompPostMAS; //计算PostMAS条件统计 atomic m_iHeartBeats; //心跳统计 atomic m_bConnectFlag; //连接标记 bool m_bAECCtlSignal; //在曝光设置更新期间,AEC控制信号必须处于低状态 atomic m_iVibrationGridState; //震动栅模式(0,1) int m_iGridMSRadMargin; //震动栅ms手动曝光误差(500-2000ms) int m_iGridMSAECMargin; //震动栅ms自动曝光误差(500-2000ms) static atomic m_bExtraFlag; //使用标记 atomic m_bSleepState{ false }; bool m_bGenBusy; int m_nCtlMode; bool m_bUseCECmd; bool m_bIsConfigLoaded; //串口处理层 int m_nCMDType_WaitTime{ 0 }; int m_nCMDType_HB{ 0 }; int m_nCMDType_WaitACK{ 0 }; static void ProcessClientData(const char* pData, unsigned long DataLength, void* lparam); RET_STATUS HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh = 3); RET_STATUS HWSendHBCMD(char* strCommand, int lengh, int headLengh = 3); RET_STATUS HWSendWaitACKCMD(char* strCommand, int lengh, int headLengh = 3); private: //std::unique_ptr m_DAP; //暂不使用 std::unique_ptr m_MSGUnit; //处理消息上报对象指针 std::thread m_pHardwareStatusThread; //轮询线程句柄 std::thread m_pHardwareRsSendThread; //重发线程句柄 std::shared_ptr m_hGenPostEvent; bool m_isFirstHWPhase = true; atomic m_bExpEnable; //曝光使能 atomic m_bInvalidKVMASSetupFlag; //写入到设备的曝光设置无效 atomic m_bFaultList[18]; //记录 主机请求固件报告故障命令 返回的故障记录 int m_iMaxPower; //最大功率:kV*mA int MaxHeatContent; //最阳极热容积:kV*mAs bool m_bMAS_MA_AEC; enum MyEnum { EXPOSURE_SOFTWARE_HARDWARE, //0 EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN, //1 EXPOSURE_SOFTWARE_NOSYNBOX //2 }; private: RET_STATUS HWSend(const char* strCommand, int lengh, bool reSend = false, int nTimeOut = TIMEOUTVALUE); //指令发送接口 void OnCallBack(); //处理指令回调函数 void Register(); //注册对外提供的属性、方法 bool StartHardwareStatusThread(); //启动轮询线程 static void HardwareStatusThread(PSGHDDevice* pParam); //定时查询状态信息 static void HardwareReSendThread(PSGHDDevice* pParam); //指令重发 void FireNotify(std::string key, int context);//向上层上报消息 void FireNotify(std::string key, float context); void FireNotify(std::string key, std::string context); void FireErrorMessage(const bool Act, const int Code, const char* ResInfo = ""); //上报错误消息 void FireWarnMessage(const bool Act, const int Code, const char* ResInfo = ""); //上报告警消息 }; } //----------------------------------------------------------------------------- // PSGHDDriver //----------------------------------------------------------------------------- namespace CCOS::Dev::Detail::Generator { class _CCOSDEVGENPSGHD_API PSGHDDriver : public DriverMould { public: PSGHDDriver (); virtual ~PSGHDDriver (); public: //virtual bool DriverEntry (std::string CfgFileName); //设置配制文件路径,对外接口最先调用 virtual void Prepare () override; //在 DriverEntry 之后执行;选择与物理设备通信方式(串口、TCP) virtual std::string DriverProbe() override; //在 Prepare 之后执行;读取配置文件模块参数,供上层创建驱动work路径 bool ReConnection(); virtual bool DATA_ACTION Connect () override; //在 DriverProbe 之后执行;根据通信方式与物理设备进行连接 virtual auto CreateDevice(int index)->std::unique_ptr override; //在 Connect 之后执行;创建逻辑设备,供上层创建驱动树节点 virtual bool isConnected() const override; //检查驱动与物理设备连接状态 virtual std::string GetResource() override; //获取配置文件的值 virtual std::string DeviceProbe() override; //读取配置文件模块参数,供上层创建设备work路径 virtual void Disconnect() override; //断开驱动与物理设备连接状态 virtual void Dequeue (const char * Packet, DWORD Length) ; //在super::Connect中轮询,调用 DecodeFrame:查找指令操作对照表,通过对应操作更新数据并调用FireNotify上报更新 virtual void FireNotify (int code, std::string key, std::string content); //向监听者上报事件 static PACKET_RET callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength); //判断从设备读到的数据有没有可用的数据包,有则最终调度到 Dequeue std::shared_ptr m_scfWrapper; private: bool SaveConfigFile(bool bSendNotify); virtual bool GetDeviceConfig(std::string& Cfg) override; virtual bool SetDeviceConfig(std::string Cfg) override; bool GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue); bool SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue); std::string m_SCFDllName; ResDataObject m_DeviceConfig; ResDataObject m_ConfigAll; //存储当前的配置,用于修改配置时写回文件 ResDataObject m_Configurations; //存储当前配置中“CONFIGURATION”节点的内容 enum class ConnectionState { Disconnected, Connecting, Connected, Failed }; enum class ConnectionType { Serial, // 串口 Ethernet // 网口 }; std::atomic m_connectionState{ ConnectionState::Disconnected }; std::chrono::steady_clock::time_point m_lastConnectionAttempt; std::mutex m_connectionMutex; const std::chrono::seconds RESET_RETRY_AFTER{ 60 }; // 修改成员变量定义,添加mutable允许const函数修改 mutable int m_connectionRetryCount{ 0 }; // 关键:用mutable修饰 const int MAX_RETRY_COUNT = 3; const std::chrono::seconds RETRY_INTERVAL{ 5 }; // 串口相关(固定支持的端口,可从配置扩展) std::vector m_serialPorts{ "/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", "/dev/ttyUSB4" }; int m_currentSerialPortIndex{ 0 }; // 当前尝试的串口端口索引 ConnectionType m_currentConnType{ ConnectionType::Serial }; //webconfig使用 ResDataObject m_DeviceConfigSend; string g_strAppPath; std::unique_ptr m_pAttribute; std::unique_ptr m_pDescription; PSGHDDevice* m_pDevice{ nullptr }; }; }