#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include "CCOS.Dev.IODevice.Detail.hpp" #include "CCOS.Dev.MSGMould.hpp" #include "CCOS.Dev.Generator.Mould.hpp" #include "DAP.BasicMoulds.hpp" // #include "DeliverModule.h" // 已废弃,Linux下不再使用 #include "LogicClient.h" #include "SCFWrapper.h" #define _CCOSDEVGENDelta_API __attribute__((visibility("default"))) // Linux 兼容的数据类型定义 using DWORD = unsigned long; using LPVOID = void*; using BOOL = int; using HANDLE = void*; using BYTE = unsigned char; using UINT8 = uint8_t; using UINT16 = uint16_t; using UINT32 = uint32_t; using FLOAT = float; #define TRUE 1 #define FALSE 0 #define MAX_PATH 260 #define WAIT_OBJECT_0 0 // 睡眠函数(以毫秒为单位) inline void Sleep(unsigned int milliseconds) { std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); } enum DELTA_ABNORMAL_LEVEL { //异常等级 REG_ERRO, REG_WARN }; enum TransToType { tochar = 0, toint, tohex }; struct AECINFORM { int Chamber; int Field; int Density; int FilmScreenSpeed; }; static const int TIMEOUTVALUE = 100; static int m_nCountTimes = 0; //----------------------------------------------------------------------------- // DeltaDevice //----------------------------------------------------------------------------- namespace CCOS::Dev::Detail::Generator { namespace nDev = CCOS::Dev; namespace DevDAP = CCOS::Dev::Detail::DAP; class _CCOSDEVGENDelta_API DeltaDevice : public IODeviceDetail,public GeneratorMould { using super = IODeviceDetail; using superGen = GeneratorMould; public: DeltaDevice(std::shared_ptr center, std::shared_ptr SCF,string configfile); ~DeltaDevice (); bool DecodeFrame(const char* strPackage, int nLength); void ProcessCommand(const char* strPackage, int Index, int End); 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 SetFocus(int value) override; 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 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 SetGenSynState(int value) override; virtual RET_STATUS SetGenState(int value) override; virtual RET_STATUS SetExpEnable() override; virtual RET_STATUS SetExpDisable()override; virtual RET_STATUS Reset()override; virtual RET_STATUS SetExpMode(std::string value) override; virtual RET_STATUS SetFrameRate(FLOAT frameRate) override; virtual RET_STATUS SetFLFMode(std::string value) override; virtual RET_STATUS SetEXAMMode(std::string value) override; virtual RET_STATUS ActiveSyncMode(_tSyncModeArgs value) override; static DeltaDevice* g_GenDevice; //串口处理层(已废弃) // 注释:DeliverModule已废弃,Linux下不再使用 // static nsSerialGPM::CDeliverModule m_tDelivermodule; // int m_nCMDType_ACK{ 0 }; // int m_nCMDType_WaitTime{ 0 }; // int m_nCMDType_WaitACK{ 0 }; // int m_nCMDType_WaitSELF{ 0 }; // static void ProcessClientData(const char* pData, unsigned long DataLength, void* lparam); // static void WriteLog(const char* pData, nsSerialGPM::LOG_V2_LEVEL level); //RET_STATUS HWSendACKCMD(string strCommand, int headLengh = 1); //RET_STATUS HWSendWaittimeCMD(string strCommand, int headLengh = 1); // RET_STATUS HWSendWaitACKCMD(string strCommand, int headLengh = 1); bool LoadConfig(string configfile); int GetGenState(); private: void Register(); RET_STATUS RefreshData(); RET_STATUS HWSend(const char * strCommand, int lengh, int nTimeOut = TIMEOUTVALUE); 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 = ""); //上报告警消息 bool GetConfData(); _tSyncModeArgs m_cfgCurrentWSSYN; //当前选中的工作位和对应的同步模式 _tAPRArgs m_tAPRdata; //保存服务传入的参数 ResDataObject m_GenConfig; //driver's config file std::unique_ptr m_DAP; //DAP值 std::unique_ptr m_MSGUnit; //错误消息处理对象 string m_ErrorCode; //错误码,防止重复发送 bool m_ExpPreFlagStatus{ false }; //发生器返回的是否允许prepare bool m_XRayOnFlagStatus{ false }; //发生器返回的X-RAY ON bool m_SysLimitFlagStatus{ false }; //发生器返回的SYSTEM LIMITS bool m_InterlockFlagStatus{ false }; //发生器返回的INTERLOCK bool m_TubeFlagStatus{ false }; //发生器返回的TUBE bool m_KVHoldFlagStatus{ false }; //发生器返回的KV HOLD bool m_ServiceFlagStatus{ false }; //发生器返回的SERVICE bool m_FluLevelFlagStatus{ false }; //发生器返回的FLUORO LEVEL bool m_ExamProFlagStatus{ false }; //发生器返回的EXAM PROCESS COMPLETED bool m_ExamSwitchFlagStatus{ false }; //发生器返回的EXPOSURE SWITCH PRESSED bool m_XRDisableFlagStatus{ false }; //发生器返回的XR DISABLE bool m_ABSFlagStatus{ false }; //发生器返回的FLUORO LOCK bool m_AECFlagStatus{ false }; //发生器返回的RAD LOCK //lsy bool ChangeGenParam(int kv, float fma, float fms, float fmas); bool SetRADKV_MA_MS(int kv, float ma, float ms); bool SetRADKV_MAS(int kv, float mas); bool SetAECKV_MA_MS(int kv, float ma, float ms); int FormatCommand(char* buf, int len); //int GetFacValue(vector Index, int& value); int GetFacFloatValue(vector Index, float& value); string m_strCommand; string uint8ArrayToHexStr(const UINT8* array, size_t length); string m_strKV; vector m_vecKV; string m_strMAS; vector m_vecMAS; string m_strMA; vector m_vecMA; string m_strMS; vector m_vecMS; int m_nTubeMaSFMaxValue{ 160 }; int m_nTubeMaBFMinValue{ 180 }; bool m_nTableUsePanelAEC{ false }; bool m_nWallUsePanelAEC{ false }; bool m_nFreeUsePanelAEC{ false }; bool m_nTableUsePanelDDR{ false }; bool m_nWallUsePanelDDR{ false }; bool m_nFreeUsePanelDDR{ false }; bool m_bSendXwindowToDector{ false }; bool SetAECInformation(int chamber, int field, int density, int film); bool SetRadMode(int radmode, int desmode); bool SetDAECEnable(int enable);//打开关闭DAEC功能 int m_nChamberIndex{ 1 }; int WSGen2UI(int InputWS); int m_nAecFiled{2}; int m_nAecDensity{0}; int m_nAecFilm{ 0 }; bool SetDualEnergyCommand(const _tAPRArgs& t); bool m_bIsDualExp{ false }; int m_nExposureNumber{ 0 }; bool SetAPRForDual(int nWS, int nFO, int nET, int nAECFieldSel, int nAECFilmSel, float fAECDensity, float fKV, float fMA, float fMS, float fMAS); bool GetLastExpParamenters(void); std::unordered_map m_ErrorMessages; std::unordered_map m_WarnMessages; void RetrieveFirewareVersion(); string AnalysisCommand(int number, const char* chdigital, int type); LogicClient* m_pDetectorClient; int m_nDEFrameRate{ 1 }; int m_nCurErrLevel{ 2 }; bool m_bXronMsg{ false }; std::string GetFileVersion(std::string strFilePathName); std::string GetDynamicLibraryPath(); void SetNoDrMode(); void SetSyncOutMode(); void SeleckDetectorNumber(int detectorID); //查询线程 static atomic m_iLoopTime; //循环查询时间(毫秒) static atomic m_bExtraFlag; //使能标记 bool StartHardwareStatusThread(); static void HardwareStatusThread(DeltaDevice* pParam); std::thread m_pHardwareStatusThread; std::shared_ptr m_SCF; void RetrieveHU(); void CheckGeneratorStatus(); bool m_nProcessXrayon{ false }; void RetrieveMaxRating(); int m_nMaxKW{ 0 }; //AEC参数 void UpdataAECInformation(AECINFORM aec);//通过发生器返回的参数更新AEC参数 AECINFORM m_AECinformation; void RetrieveGeneratroDerating(); string m_strConfigPath{ "" }; //断线重连 bool m_bReconnect{ false }; bool m_bSetAPR{ false }; vector> parse_packets(const vector& data); bool m_bLoadConfigFile{ false }; // 数据缓冲区(线程安全) char m_buffer[1024]; int m_bufferLength{ 0 }; std::queue m_partialCommandQueue; int m_currentIndex{ 0 }; std::mutex m_bufferMutex; // 保护缓冲区的互斥锁 }; } //----------------------------------------------------------------------------- // DeltaDriver //----------------------------------------------------------------------------- namespace CCOS::Dev::Detail::Generator { class _CCOSDEVGENDelta_API DeltaDriver : public DriverMould { using super = DriverMould; public: DeltaDriver (); virtual ~DeltaDriver (); public: //virtual bool DriverEntry (std::string CfgFileName); //配置文件路径,本接口最先调用 virtual void Prepare() override; //在 DriverEntry 之后执行;选择与发生器设备通讯方式(串口、TCP) virtual std::string DriverProbe() override; //在 Prepare 之后执行;获取配置文件模型名,供上层创建设备work路径 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); ResDataObject m_DeviceConfig; ResDataObject m_ConfigAll; //存储当前配置项,如果修改配置时写入文件 ResDataObject m_Configurations; //存储当前配置项中CONFIGURATION节点内容 bool m_bDemoMode; bool m_bDemoConnected; // 在 DEMO 模式下, 是否调用过 Connect //webconfig使用 ResDataObject m_DeviceConfigSend; string g_strAppPath; std::unique_ptr m_pAttribute; std::unique_ptr m_pDescription; DeltaDevice* m_pDevice{ nullptr }; 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 }; std::string m_SCFDllName; }; }