// CCOS.Dev.GEN.PSGRF.cpp : 定义 DLL 应用程序的导出函数。 #include "stdafx.h" #include #include #include "LogicDevice.h" using namespace std::placeholders; #include #include #include #include "Helper.JSON.hpp" #include "CCOS.Dev.Generator.PSG_RF.h" using namespace CCOS::Dev::Detail::Generator; namespace nsGEN = CCOS::Dev::Detail::Generator; static nsGEN::PSGRFDriver* pIODriver = nullptr; //关闭无关警告 #pragma warning (disable:4244) // warning C4244: “初始化”: 从“double”转换到“float”,可能丢失数据 #pragma warning (disable:4305) // warning C4305: “参数”: 从“double”到“float”截断 #pragma warning (disable:4267) // warning C4267 : “初始化”: 从“size_t”转换到“int”,可能丢失数据 #pragma warning (disable:4805) // warning C4805: “!=”: 在操作中将类型“bool”与类型“int”混合不安全 #define PSGRF_LARGE_POWER 5 #define PSGRF_SMALL_POWER 1.1 #define PSGRF_MAX_HEAT 225 #define PSGRF_MIN_MA 0.1 #define PSGRF_MAX_MA 1000.0 #define PSGRF_MIN_MS 0.1 #define PSGRF_MAX_MS 10000.0 //设置相关常量 #define PSGRF_LoopDefHBTime 1000 #define PSGRF_LoopExpHBTime 500 static const int msTimeOut_Lock = 500; //通讯接口锁定时间 #define PSGRF_Com_NormalLen 150 #define PSGRF_ETX 0x03 #define PSGRF_RESOK "$" /* 解释: = ASCII 字母数字命令 = ASCII 数字数据 = ASCII 数字数据 = 所有命令、数据和 ETX 字节的二进制 1 字节累加和 */ //设置对应通信接口库 #ifdef _WIN64 #ifdef _DEBUG static const auto COM_SCFDllName = "Ccos.Dev.SerialSCFX64D.dll"; #else static const auto COM_SCFDllName = "Ccos.Dev.SerialSCFX64.dll"; #endif #endif #ifdef _WIN64 #ifdef _DEBUG static const auto TCP_SCFDllName = "Ccos.Dev.TcpipSCFX64D.dll"; #else static const auto TCP_SCFDllName = "Ccos.Dev.TcpipSCFX64.dll"; #endif #endif Log4CPP::Logger* mLog::gLogger = nullptr; std::atomic lastValidResponse; constexpr auto TIMEOUT = std::chrono::seconds(5); // 超时阈值 //nsGEN::tFrameMapItem::tFrameMapItem() //{ // m_fFun = NULL; //} //nsGEN::tFrameMapItem::tFrameMapItem(cbFun f) //{ // m_fFun = f; //} //nsGEN::tFrameMapItem& nsGEN::tFrameMapItem::operator =(const tFrameMapItem& value) //{ // m_fFun = value.m_fFun; // return *this; //} struct tFrameMapping { static const int MaxLen = 5; // 前缀不能超超过 5 个字符 ! using cbFun = std::function ; char strHead[MaxLen]; int NbOfCharOfHead; cbFun fun; tFrameMapping(char* str, int len, cbFun f) { assert(len < MaxLen); //len最大只能是4 for (int i = 0; i < len; i++) strHead[i] = str[i]; NbOfCharOfHead = len; fun = f; } }; //响应操作对照表 static std::list arFrame; //查找响应操作对照表执行对应操作 static bool DecodeFrame(const char* strFrame, int length) { auto pr = [strFrame, length](const tFrameMapping& Item) { for (int i = 0; i < Item.NbOfCharOfHead; i++) { if (strFrame[i] != Item.strHead[i]) { return false; } } return true; }; auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);//此处pr,是上面定义的 lambda表达式,用来在list中找到对于的包头。 if (found == arFrame.end()) { return false; } lastValidResponse.store(std::chrono::steady_clock::now()); const auto& Item = *found; auto pc = strFrame; char data[100] = { 0 }; memcpy(data, strFrame + Item.NbOfCharOfHead, length - Item.NbOfCharOfHead); Item.fun(data, length - Item.NbOfCharOfHead); return true; } //----------------------------------------------------------------------------- // PSGRFDevice //----------------------------------------------------------------------------- atomic nsGEN::PSGRFDevice::m_iLoopTime = PSGRF_LoopDefHBTime; atomic nsGEN::PSGRFDevice::m_bExtraFlag = false; nsSerialGPM::CDeliverModule nsGEN::PSGRFDevice::m_tDelivermodule; static atomicHeartBeatFlag = false; nsGEN::PSGRFDevice::PSGRFDevice(std::shared_ptr center, nsSCF::SCF SCF, string configfile) : super(center, SCF) { assert(EventCenter); m_bExtraFlag = true; m_pTrueParameterThread = NULL; m_pHardwareStatusThread = NULL; m_pHardwareRsSendThread = NULL; m_bExpEnable = false; m_iCompPostMAS = 0; m_bAECCtlSignal = false; m_iHeartBeats = 0; m_bConnectFlag = true; m_bInvalidKVMASSetupFlag = false; m_bGenBusy = false; m_bReturnFlag = false; m_iLoopTime.store(PSGRF_LoopDefHBTime); m_DoseUnit.m_FLAccTimeUnit = "m"; for (int i = 0; i < 18; i++) { m_bFaultList[i] = false; }; m_bCMD68Flag = true; m_bMAS_MA_AEC = false; m_iMaxPower = PSGRF_LARGE_POWER; //KW MaxHeatContent = PSGRF_MAX_HEAT; //KJ string version; if(GetVersion(version, hMyModule)) mLog::FINFO("\n===============log begin : version:{$} ===================\n", version.c_str()); else mLog::FINFO("\n===============log begin : version:0.0.0.0 ===================\n"); //设置发生器属性集合各个值的范围及精度 m_DoseUnit.m_KV.reset(new KVMould(39.0, 39.0, 151.0, 1.0)); m_DoseUnit.m_MA.reset(new MAMould(0.0, PSGRF_MIN_MA, PSGRF_MAX_MA, 0.1)); m_DoseUnit.m_MS.reset(new MSMould(0.0, PSGRF_MIN_MS, PSGRF_MAX_MS, 0.01)); m_DoseUnit.m_MAS.reset(new MASMould(0.0, 0.1, 1000.0, 0.01)); m_DoseUnit.m_Techmode.reset(new TECHMODEMould(AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_2P, AttrKey::TECHMODE_NOAEC_3P, AttrKey::TECHMODE_AEC_MAS_MA, 1)); m_DoseUnit.m_WS.reset(new WORKSTATIONMould(1, 0, 5, 1)); m_DoseUnit.m_Focus.reset(new FOCUSMould(AttrKey::FOCUS_TYPE::FOCUS_SMALL, AttrKey::FOCUS_SMALL, AttrKey::FOCUS_LARGE, 1)); m_DoseUnit.m_AECField.reset(new AECFIELDMould(0, 0, 111, 1)); m_DoseUnit.m_AECFilm.reset(new AECFILMMould(0, 0, 2, 1)); m_DoseUnit.m_AECDensity.reset(new AECDENSITYMould(0, -3, 3, 1)); m_DoseUnit.m_GenHE.reset(new GENHEATMould(10, 0, 100, 1)); m_DoseUnit.m_HE.reset(new TUBEHEATMould(0, 0, 100, 1)); m_DoseUnit.m_GenSynState.reset(new GENSYNSTATEMould(AttrKey::GENERATOR_RAD_OFF, AttrKey::GENERATOR_SYNC_ERR, AttrKey::GENERATOR_SYNC_MAX, 1)); m_DoseUnit.m_GenState.reset(new GENSTATEMould(0, AttrKey::GENERATOR_STATUS_SHUTDOWN, AttrKey::GENERATOR_STATUS_MAX, 1)); m_DoseUnit.m_GenTotalExpNumber.reset(new TOTALEXPNUMMould(0, 0, 9999, 1)); m_DoseUnit.m_GenTotalAcqTimes.reset(new TOTALACQTIMESMould(0, 0, 9999, 1)); m_DoseUnit.m_GenTubeCoolWaitTimes.reset(new TUBECOOLTIMEMould(0, 0, 9999, 1)); m_DoseUnit.m_GenTubeOverLoadNumber.reset(new TUBEOVERLOADNUMMould(0, 0, 9999, 1)); m_DoseUnit.m_GenCurrentExpNumber.reset(new CUREXPNUMMould(0, 0, 9999, 1)); m_DoseUnit.m_ExpMode.reset(new EXPMODEMould(AttrKey::EXPMODE_TYPE::Single)); m_DoseUnit.m_FrameRate.reset(new FRAMERATEMould(0, 0, 16, 1)); m_DoseUnit.m_FLMode.reset(new FLUModeMould(AttrKey::GENERATOR_FLUMode::GENERATOR_FLMODE_NOTFLU)); m_DoseUnit.m_BatteryChargeState.reset(new BATTERYCHARGSTATEMould(0, 0, 1, 1)); m_DoseUnit.m_TubeTargetMaterial.reset(new TUBETARGETMATERIALMould(AttrKey::TUBETARGETMATERIAL_TYPE::MO)); m_DoseUnit.m_TubeAngle.reset(new TUBEANGLEMould(0, -45, 45, 1)); m_DoseUnit.m_FLIntTime.reset(new FLUIntTimeMould(0.0, 0.0, 100.0, 0.1)); m_DoseUnit.m_FLAccTime.reset(new FLAccTimeMould(0.0, 0.0, 999.0, 0.1)); m_DoseUnit.m_FLKV.reset(new FLUKVMould(0, 40, 125, 1)); m_DoseUnit.m_FLMS.reset(new FLUMSMould(10.0, 10.0, 999999.0, 0.01)); m_DoseUnit.m_FLMA.reset(new FLUMAMould(0.1, 0.1, 99.0, 0.1)); m_DoseUnit.m_ABSStatus.reset(new FLUABSStatusMould(0, 0, 2, 1)); m_DoseUnit.m_PPS.reset(new PPSMould(0.5,0.5, 30, 0.1)); m_DoseUnit.m_DoseLevel.reset(new FLUDoseLevelMould(1, 0, 2, 1)); m_DoseUnit.m_FLMode.reset(new FLUModeMould(0, 0, 4, 1)); m_DoseUnit.m_Curve.reset(new FLUCurveMould(0, 0, 3, 1)); //Actual exposure parameters 值 m_DoseUnit.m_PostKV.reset(new POSTKVMould(0.0, 40.0, 120.0, 1.0)); m_DoseUnit.m_PostMA.reset(new POSTMAMould(0.0, 0.1, 1000.0, 0.1)); m_DoseUnit.m_PostMS.reset(new POSTMSMould(0.0, 1.0, 9999999.0, 0.01)); m_DoseUnit.m_PostMAS.reset(new POSTMASMould(0.0, 0.5, 1000.0, 0.01)); //发生器告警及错误消息 m_MSGUnit.reset(new nsDetail::MSGUnit(center, nsGEN::GeneratorUnitType)); std::string configPath; configPath = GetDynamicLibraryPath() + "\\ErrorWarnInfo.json"; m_DeviceErrorHandler.reset(new DeviceErrorHandler(center, nsGEN::GeneratorUnitType, configPath)); m_hGenPostEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_tDelivermodule.InitSendModle(this, &ProcessClientData, WriteLog); m_nCMDType_WaitTime = m_tDelivermodule.SetPriority(true, false, false, true, 100); m_nCMDType_HB = m_tDelivermodule.SetPriority(false, true, false, true, 100); m_nCMDType_WaitACK = m_tDelivermodule.SetPriority(true, false, 3, false, 0,true,1000); mLog::FINFO("m_nCMDType_WaitTime[{$}]m_nCMDType_HB[{$}] m_nCMDType_WaitACK[{$}]", m_nCMDType_WaitTime, m_nCMDType_HB, m_nCMDType_WaitACK); lastValidResponse = std::chrono::steady_clock::now(); m_bDAPEnable = false; m_bSaveMSMA = true; m_bIsConfigLoaded = false; m_bUseCECmd = false; //配置响应操作对照表 供发生器回传的数据触发相应的操作 OnCallBack(); //将发生器可以对外提供的指令注册集进行补充 Register(); //重置发生器 HWSend("RE", 2); Sleep(500); //以下进行默认设置,需要注意默认值是否正确 //RefreshData(); //刷新初始数值 if (1 == m_bUseCECmd) { HWSend("CE0", 3); } HWSend("RS", 2); HWSend("ET?", 3); //查询2、3点模式 HWSend("ST?", 3); HWSend("WS1", 3); //启动硬件状态轮询进程 StartHardwareStatusThread(); } nsGEN::PSGRFDevice::~PSGRFDevice() { m_bExtraFlag = false; mLog::FINFO("\n===============log end ===================\n"); CloseHandle(m_hGenPostEvent); if (m_pHardwareRsSendThread != NULL) { WaitForSingleObject(m_pHardwareRsSendThread, INFINITE); CloseHandle(m_pHardwareRsSendThread); m_pHardwareRsSendThread = NULL; } if (m_pTrueParameterThread != NULL) { WaitForSingleObject(m_pTrueParameterThread, INFINITE); CloseHandle(m_pTrueParameterThread); m_pTrueParameterThread = NULL; } if (m_pHardwareStatusThread != NULL) { WaitForSingleObject(m_pHardwareStatusThread, INFINITE); CloseHandle(m_pHardwareStatusThread); m_pHardwareStatusThread = NULL; } arFrame.clear(); } std::string nsGEN::PSGRFDevice::GetGUID() const { mLog::FINFO("===============GetGUID : {$} ===================\n", GeneratorUnitType); return GeneratorUnitType; } void nsGEN::PSGRFDevice::Register() { auto Disp = &Dispatch; superGen::Register(Disp); superGen::RegisterRAD(Disp); superGen::RegisterAEC(Disp); superGen::RegisterExpEnable(Disp); superGen::RegisterGeneratortoSyncStatus(Disp); superGen::RegisterFluoro(Disp); Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; }); Disp->Get.Push(AttrKey::DENHEAT, [this](std::string& out) { out = m_DoseUnit.m_GenHE->JSGet(); return RET_STATUS::RET_SUCCEED; }); auto fun_Clear_DAP = [this](auto in, auto& out) { return Clear_DAP(); }; Disp->Action.Push("Clear_DAP", fun_Clear_DAP); auto fun_GetValue_DAP = [this](auto in, auto& out) { float value = 0; RET_STATUS ret = GetValue_DAP(value); out = ToJSON(value); return ret; }; Disp->Action.Push("GetValue_DAP", fun_GetValue_DAP); auto fun_StartMove = [this](auto in, auto& out) { return StartMove(); }; Disp->Action.Push("StartMove", fun_StartMove); auto fun_EndMove = [this](auto in, auto& out) { return EndMove(); }; Disp->Action.Push("EndMove", fun_EndMove); auto fun_SetVibrationGrid = [this](auto in, auto& out) { auto value = JSONTo (in); RET_STATUS ret = SetVibrationGrid(value); return ret; }; Disp->Action.Push("SetVibrationGrid", fun_SetVibrationGrid); auto fun_GetVibrationGridMS = [this](auto in, auto& out) { int value = 0; RET_STATUS ret = GetVibrationGridMS(value); out = ToJSON(value); return ret; }; Disp->Action.Push("GetVibrationGridMS", fun_GetVibrationGridMS); } RET_STATUS nsGEN::PSGRFDevice::IncKV() { mLog::FINFO("KV value before calling IncKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str()); if (!m_DoseUnit.m_KV->CanInc()) return RET_STATUS::RET_SUCCEED; return HWSend("KV+", 3); } RET_STATUS nsGEN::PSGRFDevice::DecKV() { mLog::FINFO("KV value before calling DecKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str()); if (!m_DoseUnit.m_KV->CanDec()) return RET_STATUS::RET_SUCCEED; return HWSend("KV-", 3); } RET_STATUS nsGEN::PSGRFDevice::SetKV(float value) { mLog::FINFO("KV value before calling SetKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str()); if (!m_DoseUnit.m_KV->Verify(value)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "KV%03d", (int)value); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::IncMA() { mLog::FINFO("MA value before calling IncMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str()); if (!m_DoseUnit.m_MA->CanInc()) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) { mLog::FINFO("\n Techmode is MAS, can't inc MA"); return RET_STATUS::RET_FAILED; } return HWSend("MA+",3); } RET_STATUS nsGEN::PSGRFDevice::DecMA() { mLog::FINFO("MA value before calling DecMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str()); if (!m_DoseUnit.m_MA->CanDec()) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) { mLog::FINFO("\n Techmode is MAS, can't dec MA"); return RET_STATUS::RET_FAILED; } return HWSend("MA-", 3); } RET_STATUS nsGEN::PSGRFDevice::SetMA(float value) { mLog::FINFO("MA value before calling SetMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str()); if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) { mLog::FINFO("\n Techmode is MAS, can't set MA"); return RET_STATUS::RET_FAILED; } char temp[50] = { 0 }; sprintf_s(temp, "MA%05d", (int)(value * 10)); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::IncMS() { mLog::FINFO("MS value before calling IncMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str()); if (!m_DoseUnit.m_MS->CanInc()) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_2P || m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_AEC_2P) { mLog::FINFO("Techmode is 2Point, Cannot inc MS \n"); return RET_STATUS::RET_FAILED; } return HWSend("MS+",3); } RET_STATUS nsGEN::PSGRFDevice::DecMS() { mLog::FINFO("MS value before calling DecMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str()); if (!m_DoseUnit.m_MS->CanDec()) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_2P || m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_AEC_2P) { mLog::FINFO("Techmode is 2Point, Cannot dec MS \n"); return RET_STATUS::RET_FAILED; } return HWSend("MS-", 3);; } RET_STATUS nsGEN::PSGRFDevice::SetMS(float value) { mLog::FINFO("MS value before calling SetMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str()); if (!m_DoseUnit.m_MS->Verify(value)) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_2P || m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_AEC_2P) { mLog::FINFO("Techmode is 2Point, Cannot set MS \n"); return RET_STATUS::RET_FAILED; } char temp[50] = { 0 }; sprintf_s(temp, "MS%07d", (int)(value * 100)); return HWSend(temp,strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::IncMAS() { mLog::FINFO("MAS value before calling IncMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str()); if (!m_DoseUnit.m_MAS->CanInc()) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_3P || m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_AEC_3P) { mLog::FINFO("Techmode is 3Point, Cannot inc MAS \n"); return RET_STATUS::RET_FAILED; } return HWSend("MX+" , 3); } RET_STATUS nsGEN::PSGRFDevice::DecMAS() { mLog::FINFO("MAS value before calling DecMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str()); if (!m_DoseUnit.m_MAS->CanDec()) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_3P || m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_AEC_3P) { mLog::FINFO("Techmode is 3Point, Cannot dec MAS \n"); return RET_STATUS::RET_FAILED; } return HWSend("MX-", 3); } RET_STATUS nsGEN::PSGRFDevice::SetMAS(float value) { mLog::FINFO("MAS value before calling SetMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str()); if (!m_DoseUnit.m_MAS->Verify(value)) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_3P || m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_TYPE::TECHMODE_AEC_3P) { mLog::FINFO("Techmode is 3Point, Cannot set MAS \n"); return RET_STATUS::RET_FAILED; } char temp[50] = { 0 }; sprintf_s(temp, "MX%06d", (int)(value * 100)); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetTechmode(int value) { mLog::FINFO("Techmode value before calling SetTechmode: {$}\n", m_DoseUnit.m_Techmode->JSGet().c_str()); if (!m_DoseUnit.m_Techmode->Verify(value)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "ET%1d", (int)value); HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetEXAMMode(std::string value) { //上层给我设置exam mode(manual semiauto automatic)对应(NoAEC2Point AEC2Point AEC2Point) mLog::FINFO("Enter setexammode func value = {$}\n", value); if (value == AttrKey::EXAMMODE_TYPE::MANUAL) { SetTechmode(AttrKey::TECHMODE_TYPE::TECHMODE_NOAEC_2P); } else if (value == AttrKey::EXAMMODE_TYPE::SEMIAUTO) { SetTechmode(AttrKey::TECHMODE_TYPE::TECHMODE_AEC_2P); } else if (value == AttrKey::EXAMMODE_TYPE::AUTOMATIC) { SetTechmode(AttrKey::TECHMODE_TYPE::TECHMODE_AEC_2P); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetAPR(const _tAPRArgs& t) { m_bGenBusy = true; mLog::FINFO("APR:KV={$},MA={$},MS={$},MAS={$},Focus={$},Techmode={$},WS={$},AECDensity={$},AECField={$},AECFilm={$}", t.fKV, t.fMA, t.fMS, t.fMAS, t.nFocus, t.nTechmode, t.nWS, t.nAECDensity, t.nAECField, t.nAECFilm); if (t.nFocus < 0) { mLog::FINFO("SetAPR: the focus value is amall than 0, set focus to small focus\n"); SetFocus(0); } int nTempAECFilm = 1; switch (t.nAECFilm) { case 0: nTempAECFilm = 1; break; case 1: nTempAECFilm = 10; break; case 2: nTempAECFilm = 100; break; default: break; } SetKV(t.fKV); if (m_bLargeFocusEnable) { SetFocus(1); } else { SetFocus(t.nFocus); } if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_AEC) { // aec SetTechmode(t.nTechmode); SetAECField(t.nAECField); SetAECDensity(t.nAECDensity); SetAECFilm(nTempAECFilm); SetMA(t.fMA); SetMS(t.fMS); } else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_MAS) { // mas SetTechmode(t.nTechmode); const float EPSINON = 0.000001; if ((t.fMAS >= -EPSINON) && (t.fMAS <= EPSINON)) { SetMAS(t.fMA * t.fMS / 1000); } else { SetMAS(t.fMAS); } } else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_TIME) { // time SetTechmode(t.nTechmode); SetMA(t.fMA); SetMS(t.fMS); } m_bGenBusy = false; ///////////////////////end RefreshData(); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::RefreshData() { if (!m_bGenBusy) { HWSend("RR", 2); HWSend("RF", 2); HWSend("RS", 2); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetFocus(int value) { mLog::FINFO("Focus value before calling SetFocus: {$}\n", m_DoseUnit.m_Focus->JSGet().c_str()); if (!m_DoseUnit.m_Focus->Verify(value)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "FO%01d", (int)value); return HWSend(temp,strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::Reset() { mLog::Debug("clear all errors \n"); HWSend("RE",2);//仅重置错误状态 m_DeviceErrorHandler->ClearAllErrors(); m_DeviceErrorHandler->ClearAllWarnings(); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::ActiveSyncMode(_tSyncModeArgs value) { mLog::FINFO("value.strSyncMode: {$}, value.strSyncValue: {$}, value.strWS: {$} \n", value.strSyncMode, value.strSyncValue, value.strWS); int nSyncModeValue = atoi(value.strSyncValue.c_str()); char temp[50] = { 0 }; sprintf_s(temp, "WS%01d", nSyncModeValue); return HWSend(temp,strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::QueryHE(int& value) { if (!m_bGenBusy) return HWSend("HE?", 3); return RET_STATUS::RET_SUCCEED; } void nsGEN::PSGRFDevice::SubscribeSelf(ccos_mqtt_connection* conn) { //订阅GEN所有Action //SubscribeTopic(conn, "CCOS/DEVICE/Generator/Action/#"); Moduld层默认订阅了这个Action,如果这边也订阅的话就会执行两遍Action,可能会出问题 } RET_STATUS nsGEN::PSGRFDevice::SetVibrationGrid(int value)//发生器暂无此设置 { mLog::FINFO("Enter StartVibrationGrid:[{$}]", value); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::GetVibrationGridMS(int& value) //发生器暂无此设置 { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetAECDensity(int value) { if (!m_DoseUnit.m_AECDensity->Verify(value)) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC) return RET_STATUS::RET_FAILED; int nAECDensity = m_DoseUnit.m_AECDensity->Get(); if (value < m_DoseUnit.m_AECDensity->Get()) { if (value < m_DoseUnit.m_AECDensity->Get() - 1) { nAECDensity = (int)value; } else { nAECDensity = nAECDensity - 1; } } else if (value > m_DoseUnit.m_AECDensity->Get()) { if (value > m_DoseUnit.m_AECDensity->Get() + 1) { nAECDensity = (int)value; } else { nAECDensity = nAECDensity + 1; } } m_DoseUnit.m_AECDensity->Update(value); char temp[50] = { 0 }; if (nAECDensity >= 0) { sprintf_s(temp, "FN+%01d", (int)nAECDensity); } else { sprintf_s(temp, "FN-%01d", (int)nAECDensity); } return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetAECField(int value) { if (!m_DoseUnit.m_AECField->Verify(value)) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) return RET_STATUS::RET_FAILED; m_DoseUnit.m_AECField->Update(value); char temp[50] = { 0 }; sprintf_s(temp, "FI%03d", (int)value); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetAECFilm(int value) { if (!m_DoseUnit.m_AECFilm->Verify(value)) return RET_STATUS::RET_SUCCEED; if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS) return RET_STATUS::RET_FAILED; m_DoseUnit.m_AECFilm->Update(value); char temp[50] = { 0 }; sprintf_s(temp, "FS%03d", (int)value); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetWS(const string value) { mLog::FINFO("Enter SetWS {$}", value); int tempws = 0; if (value == "Table") tempws = (int)m_GenConfig["WSTable"]; else if (value == "Wall") tempws = (int)m_GenConfig["WSWall"]; else if (value == "Direct") tempws = (int)m_GenConfig["WSConventional"]; else if (value == "Free") tempws = (int)m_GenConfig["WSFree"]; else if (value == "Tomo") tempws = (int)m_GenConfig["WSTomo"]; m_DoseUnit.m_WS->Update(tempws); char temp[50] = { 0 }; sprintf_s(temp, "WS%01d", tempws); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::QueryPostKV(float& value) { m_DoseUnit.m_PostKV->Update(m_DoseUnit.m_KV->Get()); value = m_DoseUnit.m_PostKV->Get(); return HWSend("VP?",3); } RET_STATUS nsGEN::PSGRFDevice::QueryPostMA(float& value) { return HWSend("PA?", 3); } RET_STATUS nsGEN::PSGRFDevice::QueryPostMS(float& value) { return HWSend("AT?",3); } RET_STATUS nsGEN::PSGRFDevice::QueryPostMAS(float& value) { return HWSend("AP?", 3); } RET_STATUS nsGEN::PSGRFDevice::Clear_DAP() { if (m_bDAPEnable) { return HWSend("DZ",2); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::GetValue_DAP(float& value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::StartMove() //发生器无此设置 { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::EndMove() //发生器无此设置 { return RET_STATUS::RET_SUCCEED; } int nsGEN::PSGRFDevice::GetGenState() { if (m_DoseUnit.m_GenState != NULL) { return m_DoseUnit.m_GenState->Get(); } else { return 0; } } int nsGEN::PSGRFDevice::LoadConfig(string configfile) { mLog::FINFO("=====================LoadConfig========================="); // 检查文件是否存在 std::ifstream file(configfile); if (!file) { // 文件不存在,直接返回空的Connection对象 mLog::FINFO("Config file does not exist: {$}", configfile.c_str()); return -1; } if (m_bIsConfigLoaded) { mLog::FINFO("Configuration already loaded."); return 0; } ResDataObject temp; temp.loadFile(configfile.c_str()); m_GenConfig = temp["CONFIGURATION"]; TransJsonText(m_GenConfig); if (m_GenConfig.GetKeyCount("loopEnable") > 0) { m_bExtraFlag = (int)m_GenConfig["loopEnable"]; } if (m_GenConfig.GetKeyCount("ReturnTime") > 0) { m_nReturnTime = (int)m_GenConfig["ReturnTime"]; } else { m_nReturnTime = 0; } if (m_GenConfig.GetKeyCount("LargeFocusEnable") > 0) { m_bLargeFocusEnable = (int)m_GenConfig["LargeFocusEnable"]; } else { m_bLargeFocusEnable = 0; } if (m_GenConfig.GetKeyCount(ConfKey::CcosTubeInfo) > 0) { string tempValue = m_GenConfig[ConfKey::CcosTubeInfo]; m_DoseUnit.m_TubeInfo.reset(new TUBEINFOMould(tempValue)); FireNotify(AttrKey::TUBEINFO, m_DoseUnit.m_TubeInfo->JSGet()); } if (m_GenConfig.GetKeyCount(ConfKey::CcosFocusSmall) > 0) { float tempValue = (float)m_GenConfig[ConfKey::CcosFocusSmall]; m_DoseUnit.m_FocusSmall = tempValue; } if (m_GenConfig.GetKeyCount(ConfKey::CcosFocusLarge) > 0) { float tempValue = (float)m_GenConfig[ConfKey::CcosFocusLarge]; m_DoseUnit.m_FocusLarge = tempValue; } if (m_GenConfig.GetKeyCount("GenCtrlMode") > 0) { m_nCtlMode = (float)m_GenConfig["GenCtrlMode"];//default 2 float tempValue = (float)m_GenConfig[ConfKey::CcosFocusLarge]; } if (m_GenConfig.GetKeyCount("USECECMD") > 0) { m_bUseCECmd = (bool)m_GenConfig["USECECMD"]; } // Update PF limits UpdateLimits("PFMALimitMax", m_fPFLimitMaxMA, 40.0f); UpdateLimits("PFKVLimitMax", m_fPFLimitMaxKV, 80.0f); UpdateLimits("PFMALimitMin", m_fPFLimitMinMA, 1.0f); UpdateLimits("PFKVLimitMin", m_fPFLimitMinKV, 39.0f); // Update HLF limits UpdateLimits("HLFMALimitMax", m_fHLFLimitMaxMA, 99.0f); UpdateLimits("HLFKVLimitMax", m_fHLFLimitMaxKV, 151.0f); UpdateLimits("HLFMALimitMin", m_fHLFLimitMinMA, 40.0f); UpdateLimits("HLFKVLimitMin", m_fHLFLimitMinKV, 80.0f); m_bIsConfigLoaded = true; return 0; } RET_STATUS CCOS::Dev::Detail::Generator::PSGRFDevice::SimulateError(std::string Error) { mLog::FINFO("Enter SimulateError...{$} \n", Error.c_str()); std::string type = m_DeviceErrorHandler->ParseAndReport(Error); if (type == "error") { mLog::FINFO("type == error"); if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetGenSynState(int value) { mLog::FINFO("Enter SetGenSynState...{$} \n", value); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetGenState(int value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetExpMode(std::string value) { mLog::FINFO("Enter SetExpMode...{$} \n",value.c_str()); m_DoseUnit.m_ExpMode->Update(value); //add for dcm iRF hard mLog::FINFO("SetExpMode:add for dcm iRF hard"); if (!m_DeviceErrorHandler->HasActiveErrors()) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); else { m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR); FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); } FireNotify(m_DoseUnit.m_TubeTargetMaterial->GetKey(), m_DoseUnit.m_TubeTargetMaterial->JSGet()); FireNotify(m_DoseUnit.m_TubeAngle->GetKey(), m_DoseUnit.m_TubeAngle->JSGet()); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetFLFMode(std::string value) { FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); mLog::FINFO("Enter SetFLFMode...,FLFMode:{$} \n", value.c_str()); if (value == "CF") { m_DoseUnit.m_FLKV->UpdateLimitMax(151); m_DoseUnit.m_FLKV->UpdateLimitMin(39); m_DoseUnit.m_FLMode->Update(1); HWSend("FLF1", 4); //SetPPS(15); SetPluseWidth(15); } else if (value == "PF") { /*m_DoseUnit.m_FLKV->UpdateLimitMax(m_fPFLimitMaxKV); m_DoseUnit.m_FLKV->UpdateLimitMin(m_fPFLimitMinKV); m_DoseUnit.m_FLMA->UpdateLimitMax(m_fPFLimitMaxMA); m_DoseUnit.m_FLMA->UpdateLimitMin(m_fPFLimitMinMA);*/ /*if (m_DoseUnit.m_FLKV->Get() > m_fPFLimitMaxKV) { SetFluKV(m_fPFLimitMaxKV + 1); } if (m_DoseUnit.m_FLMA->Get() > m_fPFLimitMaxMA) { SetFluMA(m_fPFLimitMinMA); }*/ m_DoseUnit.m_FLMode->Update(2); HWSend("FLF2", 4); } else if (value == "HLF") { /*m_DoseUnit.m_FLKV->UpdateLimitMax(m_fHLFLimitMaxKV); m_DoseUnit.m_FLKV->UpdateLimitMin(m_fHLFLimitMinKV); m_DoseUnit.m_FLMA->UpdateLimitMax(m_fHLFLimitMaxMA); m_DoseUnit.m_FLMA->UpdateLimitMin(m_fHLFLimitMinMA);*/ /*if (m_DoseUnit.m_FLKV->Get() < m_fHLFLimitMinKV) { SetFluKV(m_fHLFLimitMinKV); } if (m_DoseUnit.m_FLMA->Get() < m_fHLFLimitMinMA) { SetFluMA(m_fHLFLimitMinMA); }*/ m_DoseUnit.m_FLMode->Update(2); HWSend("FLF2", 4); } else { mLog::FINFO("other FluMode : {$}", value.c_str()); return RET_STATUS::RET_SUCCEED; } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetFLLever(FLOAT value) { mLog::FINFO("Enter SetFLLever...{$} \n", value); m_DoseUnit.m_DoseLevel->Update(value); char temp[50]{ 0 }; sprintf_s(temp, "FLD%f", value); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetFrameRate(FLOAT frameRate) { mLog::FINFO("SetFrameRate in\n"); /*m_DoseUnit.m_FrameRate->Update(frameRate); char temp[50]{ 0 }; sprintf_s(temp, "FLS%03d", int(frameRate * 10)); return HWSend(temp, strlen(temp));*/ return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetRPS(int rps) //发生器无此动态设置 { return RET_STATUS::RET_SUCCEED; } std::string nsGEN::PSGRFDevice::GetDynamicLibraryPath() { HMODULE hModule = GetModuleHandle("CCOS.Dev.Generator.PSG_RF64.dll"); char path[MAX_PATH]; GetModuleFileName(hModule, path, MAX_PATH); std::string fullPath(path); size_t pos = fullPath.find_last_of("\\/"); mLog::FINFO("PSG dll fullPath {$}", fullPath); return fullPath.substr(0, pos); } RET_STATUS nsGEN::PSGRFDevice::SetAPF(const _tAPFArgs& t) { m_bGenBusy = true; mLog::FINFO("APF:FLKV={$},FLMA={$},PPS={$},WS={$},FLuType={$},ABSMode={$},DoseLever={$},m_Focus={$}", t.nFLKV, t.fFLMA, t.nPPS, t.nWS, t.nFluMode, t.nABSMode, t.nDoseLever, m_DoseUnit.m_Focus->Get()); if (m_bLargeFocusEnable && m_DoseUnit.m_Focus->Get() != 1) { SetFocus(1); } //SetPPS(t.nPPS); SetFKM(t.nFLKV, t.fFLMA); m_bGenBusy = false; //RefreshData(); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::IncFluKV() { mLog::FINFO("FluKV value before calling IncFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str()); if (!m_DoseUnit.m_FLKV->CanInc()) return RET_STATUS::RET_SUCCEED; return HWSend("FLK+", 4); } RET_STATUS nsGEN::PSGRFDevice::DecFluKV() { mLog::FINFO("FluKV value before calling DecFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str()); if (!m_DoseUnit.m_FLKV->CanDec()) return RET_STATUS::RET_SUCCEED; return HWSend("FLK-", 4); } RET_STATUS nsGEN::PSGRFDevice::SetFluKV(float value) { mLog::FINFO("FluKV value before calling SetFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str()); if (!m_DoseUnit.m_FLKV->Verify(value)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "FLK%03d", (int)value); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::IncFluMA() { mLog::FINFO("FluMA value before calling IncFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str()); if (!m_DoseUnit.m_FLMA->CanInc()) return RET_STATUS::RET_SUCCEED; return HWSend("FLM+", 4); } RET_STATUS nsGEN::PSGRFDevice::DecFluMA() { mLog::FINFO("FluMA value before calling DecFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str()); if (!m_DoseUnit.m_FLMA->CanDec()) return RET_STATUS::RET_SUCCEED; return HWSend("FLM-", 4); } RET_STATUS nsGEN::PSGRFDevice::SetFluMA(float value) { mLog::FINFO("FluMA value before calling SetFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str()); if (!m_DoseUnit.m_FLMA->Verify(value)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "FLM%03d", (int)(value * 10)); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::INCPPS() { if (!m_DoseUnit.m_PPS->CanInc()) return RET_STATUS::RET_SUCCEED; return HWSend("FLS+", 4); } RET_STATUS nsGEN::PSGRFDevice::DECPPS() { if (!m_DoseUnit.m_PPS->CanDec()) return RET_STATUS::RET_SUCCEED; return HWSend("FLS-", 4); } RET_STATUS nsGEN::PSGRFDevice::SetPPS(float value) { if (!m_DoseUnit.m_PPS->Verify(value)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "FLS%03d", (int)(value * 10)); //return HWSend(temp, strlen(temp)); return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetABSMode(int nMode) { if (!m_DoseUnit.m_ABSStatus->Verify(nMode)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; mLog::FINFO("SetABSMode[{$}] \n", nMode); sprintf_s(temp, "FLA%d", (int)nMode); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetABSCurve(int curveNum) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::IncABSCurve() { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::DecABSCurve() { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::GetABSCurve() { return HWSend("FLA?", 4); } float nsGEN::PSGRFDevice::GetFluIntTimer() { return HWSend("FLI?", 4); } float nsGEN::PSGRFDevice::GetFluAccTimer() { return HWSend("FLT?", 4); } RET_STATUS nsGEN::PSGRFDevice::ResetFluTimer(int value) { char temp[50] = { 0 }; sprintf_s(temp, "FLR%d", (int)value); mLog::FINFO("ReSetFluAccTimer:[{$}] \n", value); HWSend(temp, strlen(temp)); return HWSend("FLT?",4); } RET_STATUS nsGEN::PSGRFDevice::SetFluPre(int value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetFluEXP(int value) { return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetFluMode(std::string value) { mLog::FINFO("Enter SetFLFMode...{$} \n", value.c_str()); if (value == "CF") { m_DoseUnit.m_FLMode->Update(1); return HWSend("FLF1", 4); } else if (value == "PF") { m_DoseUnit.m_FLMode->Update(2); return HWSend("FLF2", 4); } else { mLog::FINFO("other FluMode : {$}",value.c_str()); return RET_STATUS::RET_SUCCEED; } } RET_STATUS nsGEN::PSGRFDevice::SetFluDoseLever(int value) { mLog::FINFO("Enter SetFluDoseLever...{$} \n", value); // 提取当前剂量级别 int currentDoseLevel = m_DoseUnit.m_DoseLevel->Get(); // 只有在剂量级别发生变化时才继续处理 if (currentDoseLevel == value) { return RET_STATUS::RET_SUCCEED; } float fma; // 计算新的FLMA值 if (value == 1) { fma = 2 * m_DoseUnit.m_FLMA->Get(); } else if (value == 0) { fma = 0.5 * m_DoseUnit.m_FLMA->Get(); } else { mLog::FINFO("Invalid value:{$} \n", value); return RET_STATUS::RET_FAILED; // 非法值处理 } // 更新剂量级别 m_DoseUnit.m_DoseLevel->Update(value); // 验证FLMA值 if (!m_DoseUnit.m_FLMA->Verify(fma)) { return RET_STATUS::RET_SUCCEED; } // 发送命令到硬件 char temp[50] = { 0 }; sprintf_s(temp, "FLM%03d", (int)(fma * 10)); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetPluseWidth(float fplusewidth) { //if (!m_DoseUnit.m_PPS->Verify(fplusewidth)) return RET_STATUS::RET_SUCCEED; char temp[50] = { 0 }; sprintf_s(temp, "FLW%05d", (int)round(fplusewidth * 100)); return HWSend(temp, strlen(temp)); } RET_STATUS nsGEN::PSGRFDevice::SetExpEnable() { if (1 == m_bUseCECmd) { HWSend("CE1", 3); } HWSend("ST?", 3); mLog::FINFO("SetExpEnable in\n"); m_bExpEnable = true; if (!m_DeviceErrorHandler->HasActiveErrors()) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); else { m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR); FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); } return RET_STATUS::RET_SUCCEED; } RET_STATUS nsGEN::PSGRFDevice::SetExpDisable() { mLog::FINFO("SetExpDisable in\n"); if (1 == m_bUseCECmd) { HWSend("CE0", 3); } FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); m_bExpEnable = false; return RET_STATUS::RET_SUCCEED; } void nsGEN::PSGRFDevice::SetSmartAEC(int value) //发生器暂无此设置 { } //----------------------------------------------------------------------------- // ProcessCmd //----------------------------------------------------------------------------- void nsGEN::PSGRFDevice::ProcessClientData(const char* pData, unsigned long nDataLength, void* lparam) { PSGRFDevice* pCurGen = (PSGRFDevice*)lparam; pCurGen->HWSend(pData, nDataLength); } void nsGEN::PSGRFDevice::WriteLog(const char* pData, nsSerialGPM::LOG_V2_LEVEL level) { switch (level) { case nsSerialGPM::LOG_V2_FATAL: case nsSerialGPM::LOG_V2_ERROR: mLog::Error(pData); break; case nsSerialGPM::LOG_V2_WARNING: mLog::Warn(pData); break; case nsSerialGPM::LOG_V2_DEBUG: mLog::Debug(pData); break; case nsSerialGPM::LOG_V2_INFO: mLog::FINFO(pData); break; default: break; } } RET_STATUS nsGEN::PSGRFDevice::HWSendWaittimeCMD(char* strCommand, int lengh, int headLengh) { return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitTime, headLengh); } RET_STATUS nsGEN::PSGRFDevice::HWSendHBCMD(char* strCommand, int lengh, int headLengh) { return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_HB, headLengh); } RET_STATUS nsGEN::PSGRFDevice::HWSendWaitACKCMD(char* strCommand, int lengh, int headLengh) { return m_tDelivermodule.ProcessCommand(strCommand, lengh, m_nCMDType_WaitACK, headLengh); } RET_STATUS nsGEN::PSGRFDevice::HWSend(const char* strCommand, int length, bool reSend, int nTimeOut) { std::lock_guard lock(sendMutex); // 加锁,确保线程安全 static auto lastSendTime = std::chrono::steady_clock::now(); // 上次发送时间 auto currentTime = std::chrono::steady_clock::now(); // 当前时间 auto elapsed = std::chrono::duration_cast(currentTime - lastSendTime).count(); const int minInterval = 50; // 最小发送间隔(毫秒) if (elapsed < minInterval) // 如果距离上次发送少于50毫秒 { std::this_thread::sleep_for(std::chrono::milliseconds(minInterval - elapsed)); // 等待直到满足最小间隔 } if (!m_bConnectFlag) { mLog::Error("==OUT==: not Connect, [{$}] send failed \n", strCommand); return RET_STATUS::RET_FAILED; } if (!m_SCF) return RET_STATUS::RET_FAILED; char strSendCommand[100] = { 0 }; int len = strlen(strCommand); int tmpSum = 0; for (int i = 0; i < len; i++) { tmpSum += static_cast(strCommand[i]); } char checkSum = static_cast((tmpSum + 3) % 256); // Calculate checksum memcpy(strSendCommand, strCommand, len); strSendCommand[len] = 0x03; // ETX control character strSendCommand[len + 1] = checkSum; // Append checksum mLog::FINFO("==OUT==: [{$}] \n", strSendCommand); int retLength; m_SCF.Lock(msTimeOut_Lock) .SendPacket(strSendCommand, len + 2, nTimeOut, retLength); // Send with the correct length lastSendTime = std::chrono::steady_clock::now(); // 更新上次发送时间 return RET_STATUS::RET_SUCCEED; } void nsGEN::PSGRFDevice::FireNotify(string key, int context) { char szInfo[64] = { 0 }; sprintf_s(szInfo, "%d", context); std::string str = szInfo; EventCenter->OnNotify(1, key, str); } void nsGEN::PSGRFDevice::FireNotify(std::string key, float context) { char szInfo[16] = { 0 }; sprintf_s(szInfo,15, "%.2f", context); std::string str = szInfo; mLog::FINFO("FireNotify(float):[{$}][{$}]", szInfo, str.c_str()); EventCenter->OnNotify(1, key, str); } void nsGEN::PSGRFDevice::FireNotify(std::string key, std::string context) { EventCenter->OnNotify(1, key, context); } void nsGEN::PSGRFDevice::FireErrorMessage(const bool Act, const int Code, const char* ResInfo) { string ErrorCode("PSGRF_ERR_"); ErrorCode += std::to_string(Code); int level = PSG_RF_REGULATION_LEVEL::REG_ERRO; if (Act) { mLog::Error("add {$}:{$}", ErrorCode.c_str(), ResInfo); m_MSGUnit->AddErrorMessage(ErrorCode.c_str(), level, ResInfo); } else { mLog::Error("del {$}:{$}", ErrorCode.c_str(), ResInfo); m_MSGUnit->DelErrorMessage(ErrorCode.c_str(), level, ResInfo); } } void nsGEN::PSGRFDevice::FireWarnMessage(const bool Act, const int Code, const char* ResInfo) { string ErrorCode("PSGRF_WAR_"); ErrorCode += std::to_string(Code); int level = PSG_RF_REGULATION_LEVEL::REG_WARN; if (Act) { mLog::Error("add {$}:{$}", ErrorCode.c_str(), ResInfo); m_MSGUnit->AddWarnMessage(ErrorCode.c_str(), level, ResInfo); } else { mLog::Error("del {$}:{$}", ErrorCode.c_str(), ResInfo); m_MSGUnit->DelWarnMessage(ErrorCode.c_str(), level, ResInfo); } } void nsGEN::PSGRFDevice::OnCallBack() { //无 操作 auto HWNotProcess = [](const char* value, int length) -> void { mLog::FINFO("This commands[{$}] didn't need to process", value); }; auto HWKV = [this](const char* value, int length) -> void { assert(value); if (length > 20) //if length more than 20, it must be a long string like :070 MA00320 MS00063 MX00036 { mLog::FINFO("value:{$}", value); //loop the long string to find kv ma ms mas int tmpkv; float tmpma, tmpms, tmpmx; string strTemp = ""; char tmpbuf[7] = { 0 }; //kv tmpbuf[0] = '0'; tmpbuf[1] = '0'; tmpbuf[2] = '0'; tmpbuf[3] = '0'; tmpbuf[4] = value[0]; tmpbuf[5] = value[1]; tmpbuf[6] = value[2]; tmpkv = atoi(tmpbuf); m_DoseUnit.m_KV->Update(tmpkv); FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet()); //ma tmpbuf[0] = '0'; tmpbuf[1] = '0'; tmpbuf[2] = value[6]; tmpbuf[3] = value[7]; tmpbuf[4] = value[8]; tmpbuf[5] = value[9]; tmpbuf[6] = value[10]; tmpma = atof(tmpbuf) / 10.0; m_DoseUnit.m_MA->Update(tmpma); FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet()); //ms tmpbuf[0] = value[14]; tmpbuf[1] = value[15]; tmpbuf[2] = value[16]; tmpbuf[3] = value[17]; tmpbuf[4] = value[18]; tmpbuf[5] = value[19]; tmpbuf[6] = value[20]; tmpms = atof(tmpbuf) / 100.0; m_DoseUnit.m_MS->Update(tmpms); FireNotify(AttrKey::MS, m_DoseUnit.m_MS->JSGet()); //mx tmpbuf[0] = '0'; tmpbuf[1] = value[24]; tmpbuf[2] = value[25]; tmpbuf[3] = value[26]; tmpbuf[4] = value[27]; tmpbuf[5] = value[28]; tmpbuf[6] = value[29]; tmpmx = atof(tmpbuf) / 100.0; m_DoseUnit.m_MAS->Update(tmpmx); FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet()); mLog::FINFO("tmpkv={$} tmpma={$}, tmpms={$}, tmpmx={$};", tmpkv, tmpma, tmpms, tmpmx); } else { m_DoseUnit.m_KV->Update(atof(value)); FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet()); } }; auto HWMAS = [this](const char* value, int length) { assert(value); float fmas = atof(value) / 10.0; m_DoseUnit.m_MAS->Update(fmas); FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet()); }; auto HWMA = [this](const char* value, int length) { assert(value); float fma = atof(value) / 10.0; m_DoseUnit.m_MA->Update(fma); FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet()); }; auto HWMS = [this](const char* value, int length) { assert(value); float fms = atof(value) / 100.0; m_DoseUnit.m_MS->Update(fms); FireNotify(AttrKey::MS, m_DoseUnit.m_MS->JSGet()); }; auto HWVP = [this](const char* value, int length) { assert(value); m_DoseUnit.m_PostKV->Update(atof(value)); FireNotify(AttrKey::POSTKV, m_DoseUnit.m_PostKV->JSGet()); mLog::FINFO("Actual exposure parameters KV(POSTKV-VP):{$}", m_DoseUnit.m_PostKV->JSGet().c_str()); }; auto HWPA = [this](const char* value, int length) { assert(value); float fma = atof(value) / 10.0; m_DoseUnit.m_PostMA->Update(fma); FireNotify(AttrKey::POSTMA, m_DoseUnit.m_PostMA->JSGet()); mLog::FINFO("Actual exposure parameters MA(POSTMAPA):{$}", m_DoseUnit.m_PostMA->JSGet().c_str()); }; auto HWMC = [this](const char* value, int length) { assert(value); float fma = atof(value) / 10.0; m_DoseUnit.m_PostMA->Update(fma); FireNotify(AttrKey::POSTMA, m_DoseUnit.m_PostMA->JSGet()); mLog::FINFO("Actual exposure parameters MA(POSTMA-MC):{$}", m_DoseUnit.m_PostMA->JSGet().c_str()); }; auto HWAP = [this](const char* value, int length) { assert(value); float fmas = atof(value) / 100.0; m_DoseUnit.m_PostMAS->Update(fmas); FireNotify(AttrKey::POSTMAS, m_DoseUnit.m_PostMAS->JSGet()); mLog::FINFO("Actual exposure parameters MAS:{$}", m_DoseUnit.m_PostMAS->JSGet().c_str()); }; auto HWFocus = [this](const char* value, int length) { assert(value); int nfous = atoi(value); m_DoseUnit.m_Focus->Update(nfous); FireNotify(AttrKey::FOCUS, m_DoseUnit.m_Focus->JSGet()); mLog::FINFO("Current focus:{$}, FO={$}", atoi(m_DoseUnit.m_Focus->JSGet().c_str()) ? " large focus" : " small focus", m_DoseUnit.m_Focus->JSGet().c_str()); }; auto HWTechmode = [this](const char* value, int length) { assert(value); int ntechmode = atoi(value); m_DoseUnit.m_Techmode->Update(ntechmode); FireNotify(AttrKey::TECHMODE, m_DoseUnit.m_Techmode->JSGet()); switch (ntechmode) { case 0: mLog::FINFO("ET={$}", "mA/ms", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 1: mLog::FINFO("ET={$}", "mAs", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 2: mLog::FINFO("ET={$}", "AEC / mA", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 3: mLog::FINFO("ET={$}", "mAs / ms", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 4: mLog::FINFO("ET={$}", "AEC", m_DoseUnit.m_Techmode->JSGet().c_str()); break; } }; auto HWAECField = [this](const char* value, int length) { /*assert(value); int nvalue = atoi(value); if (m_DoseUnit.m_AECField->Get() != nvalue && m_DoseUnit.m_AECField->Update(nvalue)) FireNotify(AttrKey::AECFIELD, m_DoseUnit.m_AECField->JSGet());*/ }; auto HWAECFilm = [this](const char* value, int length) { /*assert(value); if (m_DoseUnit.m_AECFilm->Update(atoi(value))) FireNotify(AttrKey::AECFILM, m_DoseUnit.m_AECFilm->JSGet());*/ }; auto HWAECDensity = [this](const char* value, int length) { /*assert(value); if (m_DoseUnit.m_AECDensity->Update(atoi(value))) FireNotify(AttrKey::AECDENSITY, m_DoseUnit.m_AECDensity->JSGet());*/ }; auto HWWS = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (m_DoseUnit.m_WS->Update(nValue)) { FireNotify(m_DoseUnit.m_WS->GetKey(), m_DoseUnit.m_WS->JSGet()); } }; auto HWPR = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (nValue == 2) { mLog::FINFO("high-pressure generator enters the exposure preparation stage."); } else if (nValue == 1) { m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_PREPARE); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_PREPARE"); if (m_iLoopTime != PSGRF_LoopExpHBTime) { mLog::Debug("quicken loopTime[{$}]->[{$}]", m_iLoopTime.load(), PSGRF_LoopExpHBTime); m_iLoopTime = PSGRF_LoopExpHBTime; } } else if (nValue == 0) { m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF); Sleep(m_nReturnTime); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("m_nReturnTimee={$};", m_nReturnTime); mLog::FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_OFF"); if (m_iLoopTime == PSGRF_LoopExpHBTime) { if ((int)m_GenConfig["loopTime"] >= 100) { m_iLoopTime = (int)m_GenConfig["loopTime"]; } else m_iLoopTime = PSGRF_LoopDefHBTime; mLog::Debug("reduction loopTime[{$}]->[{$}]", PSGRF_LoopExpHBTime, m_iLoopTime.load()); } m_bGenBusy = false; RefreshData(); } }; auto HWXR = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (nValue == 2) { m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_XRAYON"); } else if (nValue == 1) { m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_READY); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); ResetEvent(m_hGenPostEvent); mLog::FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_READY"); } else if (nValue == 0) { m_bGenBusy = false; m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYOFF); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_XRAYOFF"); } }; auto HWAPDOSE = [this](const char* value, int length)//post mas { assert(value); m_DoseUnit.m_PostMAS->Update(atof(value) / 100.0); FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet()); mLog::FINFO("Actual exposure parameters MAS:{$}", m_DoseUnit.m_PostMAS->JSGet().c_str()); }; auto HWATDOSE = [this](const char* value, int length) { assert(value); double numericalValue = atof(value); mLog::FINFO("Actual exposure parameters MS(numericalValue):{$}", numericalValue); m_DoseUnit.m_PostMS->Update(numericalValue / 100.0); std::ostringstream stream; stream << std::fixed << std::setprecision(6) << m_DoseUnit.m_PostMS->JSGet(); // Assuming JSGet() returns double. FireNotify(m_DoseUnit.m_PostMS->GetKey(), stream.str()); mLog::FINFO("Actual exposure parameters MS(stream):{$}", stream.str()); }; auto HWDAP = [this](const char* value, int length) { assert(value); mLog::FINFO("Recv DAP ={$}", atof(value) / 100.0); }; auto HWEHE = [this](const char* value, int length) { m_iHeartBeats = 0; assert(value); int nhe = atoi(value); mLog::FINFO("HE:{$}%", nhe); if (m_DoseUnit.m_HE->Update(nhe)) FireNotify(m_DoseUnit.m_HE->GetKey(), m_DoseUnit.m_HE->JSGet()); }; auto HWHH = [this](const char* value, int length) { m_iHeartBeats = 0; assert(value); int nhe = atoi(value); if (m_DoseUnit.m_GenHE->Update(nhe)) FireNotify(m_DoseUnit.m_GenHE->GetKey(), m_DoseUnit.m_GenHE->JSGet()); }; //04 FLM00010 FLI000 FLT000 FLF1 FLA0 FLS060 FLD0 FLO0 FLC1 FLW00400 //040 FLM00010 FLI000 FLT000 FLF1 FLA0 FLS060 FLD0 FLO0 FLC1 FLW00400 auto HWFLK = [this](const char* value, int length) { assert(value); if (length > 20) // Check if length is more than 20 { int tmpflk, tmpflf, tmpfla, tmpfld, tmpflo; float tmpflm, tmpfli, tmpflt, tmpfls, tmpflw; char tmpbuf[5]{ 0, 0, 0, 0, 0 }; //flk tmpbuf[0] = value[0]; tmpbuf[1] = value[1]; tmpbuf[2] = value[2]; tmpflk = atoi(tmpbuf); m_DoseUnit.m_FLKV->Update(tmpflk); FireNotify(AttrKey::FLUKV, m_DoseUnit.m_FLKV->JSGet()); // flm tmpbuf[0] = value[7]; tmpbuf[1] = value[8]; tmpbuf[2] = value[9]; tmpbuf[3] = value[10]; tmpbuf[4] = value[11]; tmpflm = atof(tmpbuf) / 10.0; m_DoseUnit.m_FLMA->Update(tmpflm); FireNotify(AttrKey::FLUMA, m_DoseUnit.m_FLMA->JSGet()); // fli tmpbuf[0] = value[16]; tmpbuf[1] = value[17]; tmpbuf[2] = value[18]; tmpbuf[3] = 0; tmpbuf[4] = 0; tmpfli = atof(tmpbuf) / 10.0; m_DoseUnit.m_FLIntTime->Update(tmpfli); FireNotify(AttrKey::FLUIntTime, m_DoseUnit.m_FLIntTime->JSGet()); // flt tmpbuf[0] = value[23]; tmpbuf[1] = value[24]; tmpbuf[2] = value[25]; tmpbuf[3] = 0; tmpbuf[4] = 0; tmpflt = atof(tmpbuf) / 10.0; if (m_DoseUnit.m_FLAccTime->Update(tmpflt)) FireNotify(AttrKey::FLUAccTime, m_DoseUnit.m_FLAccTime->JSGet()); // flf tmpbuf[0] = value[30]; tmpbuf[1] = 0; tmpbuf[2] = 0; tmpflf = atoi(tmpbuf); m_DoseUnit.m_FLMode->Update(tmpflf); FireNotify(AttrKey::FLUMode, m_DoseUnit.m_FLMode->JSGet()); // fla tmpbuf[0] = value[35]; tmpbuf[1] = 0; tmpbuf[2] = 0; tmpfla = atoi(tmpbuf); if (m_DoseUnit.m_ABSStatus->Update(tmpfla)) FireNotify(AttrKey::FLUABSStatus, m_DoseUnit.m_ABSStatus->JSGet()); // fls tmpbuf[0] = value[40]; tmpbuf[1] = value[41]; tmpbuf[2] = value[42]; tmpfls = atof(tmpbuf) / 10.0; if (m_DoseUnit.m_PPS->Update(tmpfls)) FireNotify(AttrKey::FLUPPS, m_DoseUnit.m_PPS->JSGet()); // fld /*tmpbuf[0] = value[47]; tmpbuf[1] = 0; tmpbuf[2] = 0; tmpfld = atoi(tmpbuf); if (m_DoseUnit.m_DoseLevel->Update(tmpfld)) FireNotify(AttrKey::FLUDoseLevel, m_DoseUnit.m_DoseLevel->JSGet());*/ // flo tmpbuf[0] = value[52]; tmpbuf[1] = 0; tmpbuf[2] = 0; tmpflo = atoi(tmpbuf); if (m_DoseUnit.m_Curve->Update(tmpflo)) FireNotify(AttrKey::FLUCurve, m_DoseUnit.m_Curve->JSGet()); // flw tmpbuf[0] = value[62]; tmpbuf[1] = value[63]; tmpbuf[2] = value[64]; tmpbuf[3] = value[65]; tmpbuf[4] = value[66]; tmpflw = atof(tmpbuf) / 100.0; if (m_DoseUnit.m_FLMS->Update(tmpflw)) FireNotify(AttrKey::FLUMS, m_DoseUnit.m_FLMS->JSGet()); mLog::FINFO("tmpflk={$}, tmpflf={$}, tmpfla={$}, tmpfld={$}, tmpflo={$}, tmpflm={$}, tmpfli={$}, tmpflt={$}, tmpfls={$}, tmpflw={$};", tmpflk, tmpflf, tmpfla, tmpfld, tmpflo, tmpflm, tmpfli, tmpflt, tmpfls, tmpflw); } else { int tmpflkv = atoi(value); if (m_DoseUnit.m_FLKV->Update(tmpflkv)) { FireNotify(AttrKey::FLUKV, m_DoseUnit.m_FLKV->JSGet()); } } }; auto HWFLM = [this](const char* value, int length) { assert(value); float tmpflm = atof(value) / 10.0; if (m_DoseUnit.m_FLMA->Update(tmpflm)) FireNotify(AttrKey::FLUMA, m_DoseUnit.m_FLMA->JSGet()); }; auto HWFKM = [this](const char* value, int length) { assert(value); int tmpflk; float tmpflm; char tmpbuf[5]{ 0, 0, 0, 0, 0 }; //flk tmpbuf[0] = value[1]; tmpbuf[1] = value[2]; tmpbuf[2] = value[3]; tmpflk = atoi(tmpbuf); mLog::FINFO("HWFKM-tmpflk:{$};", tmpflk); if (m_DoseUnit.m_FLKV->Update(tmpflk)) FireNotify(AttrKey::FLUKV, m_DoseUnit.m_FLKV->JSGet()); // flm tmpbuf[0] = value[5]; tmpbuf[1] = value[6]; tmpbuf[2] = value[7]; tmpbuf[3] = value[8]; tmpbuf[4] = value[9]; tmpflm = atof(tmpbuf) / 10.0; mLog::FINFO("HWFKM-tmpflm:{$};", tmpflm); if (m_DoseUnit.m_FLMA->Update(tmpflm)) FireNotify(AttrKey::FLUMA, m_DoseUnit.m_FLMA->JSGet()); }; auto HWABS = [this](const char* value, int length) { assert(value); int tmpflk; float tmpflm; char tmpbuf[5]{ 0, 0, 0, 0, 0 }; //flk tmpbuf[0] = value[0]; tmpbuf[1] = value[1]; tmpbuf[2] = value[2]; tmpflk = atoi(tmpbuf); mLog::FINFO("HWABS-tmpflk:{$};", tmpflk); m_DoseUnit.m_FLKV->Update(tmpflk); FireNotify(AttrKey::FLUKV, m_DoseUnit.m_FLKV->JSGet()); // flm tmpbuf[0] = value[3]; tmpbuf[1] = value[4]; tmpbuf[2] = value[5]; tmpflm = atof(tmpbuf) / 10.0; mLog::FINFO("HWABS-tmpflm:{$};", tmpflm); m_DoseUnit.m_FLMA->Update(tmpflm); FireNotify(AttrKey::FLUMA, m_DoseUnit.m_FLMA->JSGet()); }; auto HWFLW = [this](const char* value, int length) { assert(value); float tmpflms = atof(value) / 100.0; if (m_DoseUnit.m_FLMS->Update(tmpflms)) FireNotify(AttrKey::FLUMS, m_DoseUnit.m_FLMS->JSGet()); }; auto HWFLI = [this](const char* value, int length) { assert(value); float tmpfli = atof(value) / 10.0; if (m_DoseUnit.m_FLIntTime->Update(tmpfli)) FireNotify(AttrKey::FLUIntTime, m_DoseUnit.m_FLIntTime->JSGet()); }; auto HWFLT = [this](const char* value, int length) { assert(value); float tmpflt = atof(value) / 10.0; if (m_DoseUnit.m_FLAccTime->Update(tmpflt)) { std::string strFlAccTime = m_DoseUnit.m_FLAccTime->JSGet(); mLog::FINFO("FLUORO ACCUMULATIVE TIMER:{$};", strFlAccTime.c_str()); FireNotify(AttrKey::FLUAccTime, strFlAccTime); } }; auto HWFLS = [this](const char* value, int length) { assert(value); float tmppps = atof(value) / 10.0; if (m_DoseUnit.m_PPS->Update(tmppps)) FireNotify(AttrKey::FLUPPS, m_DoseUnit.m_PPS->JSGet()); }; auto HWFLF = [this](const char* value, int length) { assert(value); int tmpflf = atoi(value); mLog::FINFO("HWFLF={$};", tmpflf); if (m_DoseUnit.m_FLMode->Update(tmpflf)) FireNotify(AttrKey::FLUMode, m_DoseUnit.m_FLMode->JSGet()); }; auto HWFLX = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (!m_bReturnFlag) { if (nValue == 2) { m_bReturnFlag = true; m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYON); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status {$};", "GENERATOR_FLU_XRAYON"); } else if (nValue == 1) { mLog::FINFO("Recv FLX1, do nothing"); } else if (nValue == 0) { int nFlFMode = atoi(m_DoseUnit.m_FLMode->JSGet().c_str()); if (nFlFMode == 1) { m_bGenBusy = false; m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYOFF); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status {$};", "GENERATOR_FLU_XRAYOFF"); } } } }; auto HWFLP = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (nValue == 2) { m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_READY); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status {$};", "GENERATOR_FLU_READY"); } else if (nValue == 1) { if (m_iLoopTime != PSGRF_LoopExpHBTime) { mLog::Debug("quicken loopTime[{$}]->[{$}]", m_iLoopTime.load(), PSGRF_LoopExpHBTime); m_iLoopTime = PSGRF_LoopExpHBTime; } mLog::FINFO("Recv FLP1, do nothing\n"); } else if (nValue == 0) { m_bReturnFlag = false; int nFlFMode = atoi(m_DoseUnit.m_FLMode->JSGet().c_str()); if (nFlFMode == 2) { m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYOFF); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); } m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_OFF); FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet()); mLog::FINFO("Generator exposure process status {$};", "GENERATOR_FLU_OFF"); mLog::FINFO("m_DoseUnit.m_FLMode={$}- number ={$};", m_DoseUnit.m_FLMode->JSGet().c_str(), nFlFMode); if (m_iLoopTime == PSGRF_LoopExpHBTime) { if ((int)m_GenConfig["loopTime"] >= 100) { m_iLoopTime = (int)m_GenConfig["loopTime"]; } else m_iLoopTime = PSGRF_LoopDefHBTime; mLog::Debug("reduction loopTime[{$}]->[{$}]", PSGRF_LoopExpHBTime, m_iLoopTime.load()); } m_bGenBusy = false; RefreshData(); } }; auto HWDS = [this](const char* value, int length) { assert(value); m_bDAPEnable = (bool)atoi(value); }; auto HWFLA = [this](const char* value, int length) { assert(value); int tmpfla = atoi(value); if (m_DoseUnit.m_ABSStatus->Update(tmpfla)) FireNotify(AttrKey::FLUABSStatus, m_DoseUnit.m_ABSStatus->JSGet()); }; auto HWFLD = [this](const char* value, int length) { assert(value); }; auto HWFLC = [this](const char* value, int length) { assert(value); }; auto HWFLO = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (m_DoseUnit.m_Curve->Update(nValue)) FireNotify(AttrKey::FLUCurve, m_DoseUnit.m_Curve->JSGet()); }; auto HWER = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (nValue != 0) { std::ostringstream oss; oss << "PSGRF_ER" << nValue; std::string ErrorCode = oss.str(); std::string type = m_DeviceErrorHandler->ParseAndReport(ErrorCode); if (type == "error") { if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); } } }; auto HWEL = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (nValue != 0) { std::ostringstream oss; oss << "PSGRF_EL" << nValue; std::string ErrorCode = oss.str(); std::string type = m_DeviceErrorHandler->ParseAndReport(ErrorCode); if (type == "error") { if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); } } }; auto HWMSG = [this](const char* value, int length) { assert(value); int nValue = atoi(value); if (nValue != 0) { std::ostringstream oss; oss << "PSGRF_MSG" << nValue; std::string ErrorCode = oss.str(); std::string type = m_DeviceErrorHandler->ParseAndReport(ErrorCode); if (type == "error") { if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); } } }; //==IN==:TU0 WS1 FO0 ET0 FI010 FS001 FN0 HE000 auto HWTU = [this](const char* value, int length) -> void { assert(value); mLog::FINFO("recv TU={$},len={$}", value, length); char tmpbuf[3] = { 0,0,0 }; int tmpWS, tmpFO, tmpET, tmpField, tmpFilm, tmpDensity, tmpHE; //ws tmpbuf[0] = value[4]; tmpWS = atoi(tmpbuf); m_DoseUnit.m_WS->Update(tmpWS); FireNotify(m_DoseUnit.m_WS->GetKey(), m_DoseUnit.m_WS->JSGet()); //FO tmpbuf[0] = value[8]; tmpFO = atoi(tmpbuf); m_DoseUnit.m_Focus->Update(tmpFO); FireNotify(AttrKey::FOCUS, m_DoseUnit.m_Focus->JSGet()); mLog::FINFO("Current focus:{$}, FO={$}", atoi(m_DoseUnit.m_Focus->JSGet().c_str()) ? "large focus" : "small focus", m_DoseUnit.m_Focus->JSGet().c_str()); //ET tmpbuf[0] = value[12]; tmpET = atoi(tmpbuf); m_DoseUnit.m_Techmode->Update(tmpET); FireNotify(AttrKey::TECHMODE, m_DoseUnit.m_Techmode->JSGet()); switch (tmpET) { case 0: mLog::FINFO("ET={$}", "mA/ms", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 1: mLog::FINFO("ET={$}", "mAs", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 2: mLog::FINFO("ET={$}", "AEC / mA", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 3: mLog::FINFO("ET={$}", "mAs / ms", m_DoseUnit.m_Techmode->JSGet().c_str()); break; case 4: mLog::FINFO("ET={$}", "AEC", m_DoseUnit.m_Techmode->JSGet().c_str()); break; } //FIELD tmpbuf[0] = value[16]; tmpbuf[1] = value[17]; tmpbuf[2] = value[18]; tmpField = atoi(tmpbuf); if (m_DoseUnit.m_AECField->Update(tmpField)) FireNotify(AttrKey::AECFIELD, m_DoseUnit.m_AECField->JSGet()); //Film tmpbuf[0] = value[22]; tmpbuf[1] = value[23]; tmpbuf[2] = value[24]; tmpFilm = atoi(tmpbuf); if (m_DoseUnit.m_AECFilm->Update(tmpFilm)) FireNotify(AttrKey::AECFILM, m_DoseUnit.m_AECFilm->JSGet()); //Density tmpbuf[0] = value[29]; tmpbuf[1] = 0; tmpbuf[2] = 0; tmpDensity = atoi(tmpbuf); if (m_DoseUnit.m_AECDensity->Update(tmpDensity)) FireNotify(AttrKey::AECDENSITY, m_DoseUnit.m_AECDensity->JSGet()); //HE tmpbuf[0] = value[33]; tmpbuf[1] = value[34]; tmpbuf[2] = value[35]; tmpHE = atoi(tmpbuf); m_DoseUnit.m_HE->Update(tmpHE); FireNotify(m_DoseUnit.m_HE->GetKey(), m_DoseUnit.m_HE->JSGet()); mLog::FINFO("parse tmpWS={$}, tmpFO={$}, tmpET={$}, tmpField={$}, tmpFilm={$}, tmpDensity={$}, tmpHE={$}", tmpWS, tmpFO, tmpET, tmpField, tmpFilm, tmpDensity, tmpHE); }; auto HWST = [this](const char* value, int length) { assert(value); int genStatus = atoi(value); mLog::FINFO("genStatus={$}", genStatus); switch (genStatus) { case 1: //初始化 mLog::Debug("Get Gen Status_1:GENSTATE {$} -> (Initialization Phase)", m_DoseUnit.m_GenState->JSGet()); if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_INIT)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); break; case 2: mLog::Debug("Get Gen Status_2:GENSTATE {$} -> (Standby Phase)", m_DoseUnit.m_GenState->JSGet()); m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_STANDBY); break; case 3: // Rad Preparation Phase mLog::Debug("Get Gen Status_3:GENSTATE {$} -> (Rad Preparation Phase)", m_DoseUnit.m_GenState->JSGet()); if (m_iLoopTime != PSGRF_LoopExpHBTime) //加快查询时间 { mLog::Debug("Get Gen Status_3:quicken loopTime[{$}]->[{$}]", m_iLoopTime.load(), PSGRF_LoopExpHBTime); m_iLoopTime = PSGRF_LoopExpHBTime; } break; case 4: mLog::Debug("Get Gen Status_4:GENSTATE {$} -> (Rad Ready Phase)", m_DoseUnit.m_GenState->JSGet()); break; case 5: mLog::Debug("Get Gen Status_5:GENSTATE {$} -> (Rad Exposure Phase)", m_DoseUnit.m_GenState->JSGet()); if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); break; case 6: mLog::Debug("Get Gen Status_6:GENSTATE {$} -> (Hangover Phase)", m_DoseUnit.m_GenState->JSGet()); break; case 7: if (m_iLoopTime == PSGRF_LoopExpHBTime) { if ((int)m_GenConfig["loopTime"] >= 100) { m_iLoopTime = (int)m_GenConfig["loopTime"]; } else m_iLoopTime = PSGRF_LoopDefHBTime; mLog::Debug("reduction loopTime[{$}]->[{$}]", PSGRF_LoopExpHBTime, m_iLoopTime.load()); } mLog::Debug("Get Gen Status_7:GENSTATE {$} -> (Error Phase)", m_DoseUnit.m_GenState->JSGet()); if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); break; case 8: mLog::Debug("Get Gen Status_8:GENSTATE {$} -> (Calibration Phase)", m_DoseUnit.m_GenState->JSGet()); break; case 9: mLog::Debug("Get Gen Status_9:GENSTATE {$} -> (Fluoro Continuous Phase)", m_DoseUnit.m_GenSynState->JSGet()); if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); break; case 10: mLog::Debug("Get Gen Status_10:GENSTATE {$} -> (Fluoro Pulse Phase)", m_DoseUnit.m_GenSynState->JSGet()); if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP)) FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet()); break; case 11: mLog::Debug("Get Gen Status_11:GENSTATE {$} -> (Fluoro Boost Phase)", m_DoseUnit.m_GenSynState->JSGet()); if (m_iLoopTime != PSGRF_LoopExpHBTime) //加快查询时间 { mLog::Debug("Get Gen Status_3:quicken loopTime[{$}]->[{$}]", m_iLoopTime.load(), PSGRF_LoopExpHBTime); m_iLoopTime = PSGRF_LoopExpHBTime; } break; default: mLog::Debug("Get Gen Status:[{$}] unknown", genStatus); break; } }; auto HWSR = [this](const char* value, int length) { assert(value); int stopReason = atoi(value); mLog::FINFO("stopReason={$}", stopReason); switch (stopReason) { case 0: mLog::Debug("Exposure stop reason:AEC feedback voltage is too low"); break; case 5: mLog::Debug("Exposure stop reason: Stop exposure manually"); break; case 6: mLog::Debug("Exposure stop reason: door lock open"); break; case 7: mLog::Debug("Exposure stop reason:Bulb tube anode ignition"); break; case 8: mLog::Debug("Exposure stop reason:Bulb cathode ignition"); break; case 9: mLog::Debug("Exposure stop reason:The pipe is lit"); break; case 10: mLog::Debug("Exposure stop reason:KV is too low or too high"); break; case 11: mLog::Debug("Exposure stop reason:Ma too low"); break; case 12: mLog::Debug("Exposure stop reason:Set ms reached"); break; case 13: mLog::Debug("Exposure stop reason:Set mAs reached"); break; case 14: mLog::Debug("Exposure stop reason:AEC dose reached"); break; default: mLog::Debug("Exposure stop reason:default"); break; } }; arFrame.clear(); arFrame.push_back(tFrameMapping("EL", 2, HWEL)); arFrame.push_back(tFrameMapping("ER", 2, HWER)); arFrame.push_back(tFrameMapping("MSG", 3, HWMSG)); arFrame.push_back(tFrameMapping("TU", 2, HWTU)); arFrame.push_back(tFrameMapping("EC", 2, HWNotProcess)); arFrame.push_back(tFrameMapping("PW", 2, HWNotProcess)); arFrame.push_back(tFrameMapping("KV", 2, HWKV)); arFrame.push_back(tFrameMapping("MX", 2, HWMAS)); arFrame.push_back(tFrameMapping("MA", 2, HWMA)); arFrame.push_back(tFrameMapping("MS", 2, HWMS)); arFrame.push_back(tFrameMapping("VP", 2, HWVP)); arFrame.push_back(tFrameMapping("PA", 2, HWPA)); arFrame.push_back(tFrameMapping("MC", 2, HWMC)); arFrame.push_back(tFrameMapping("AP", 2, HWAPDOSE)); arFrame.push_back(tFrameMapping("ET", 2, HWTechmode)); arFrame.push_back(tFrameMapping("FO", 2, HWFocus)); arFrame.push_back(tFrameMapping("FI", 2, HWAECField)); arFrame.push_back(tFrameMapping("FS", 2, HWAECFilm)); arFrame.push_back(tFrameMapping("FN", 2, HWAECDensity)); arFrame.push_back(tFrameMapping("WS", 2, HWWS)); arFrame.push_back(tFrameMapping("PR", 2, HWPR)); arFrame.push_back(tFrameMapping("XR", 2, HWXR)); arFrame.push_back(tFrameMapping("AT", 2, HWATDOSE)); arFrame.push_back(tFrameMapping("FLK", 3, HWFLK)); arFrame.push_back(tFrameMapping("FLM", 3, HWFLM)); arFrame.push_back(tFrameMapping("FLW", 3, HWFLW)); arFrame.push_back(tFrameMapping("FLI", 3, HWFLI)); arFrame.push_back(tFrameMapping("FLT", 3, HWFLT)); arFrame.push_back(tFrameMapping("FLS", 3, HWFLS)); arFrame.push_back(tFrameMapping("FLF", 3, HWFLF)); arFrame.push_back(tFrameMapping("FLP", 3, HWFLP)); arFrame.push_back(tFrameMapping("FLX", 3, HWFLX)); arFrame.push_back(tFrameMapping("FLA", 3, HWFLA)); arFrame.push_back(tFrameMapping("FLD", 3, HWFLD)); arFrame.push_back(tFrameMapping("FLC", 3, HWFLC)); arFrame.push_back(tFrameMapping("FLO", 3, HWFLO)); arFrame.push_back(tFrameMapping("FKM", 3, HWFKM)); arFrame.push_back(tFrameMapping("ABS", 3, HWABS)); arFrame.push_back(tFrameMapping("HE", 2, HWEHE)); arFrame.push_back(tFrameMapping("HH", 2, HWHH)); arFrame.push_back(tFrameMapping("DA", 2, HWDAP)); arFrame.push_back(tFrameMapping("DV", 2, HWDAP)); arFrame.push_back(tFrameMapping("DS", 2, HWDS)); arFrame.push_back(tFrameMapping("ST", 2, HWST)); arFrame.push_back(tFrameMapping("SR", 2, HWSR)); arFrame.push_back(tFrameMapping("P", 1, HWPR)); } bool nsGEN::PSGRFDevice::ReConnect() { mLog::FINFO("Enter PSG_reConnect"); m_SCF.Disconnect(); if (!pIODriver) { mLog::FINFO("PSG_reConnect:Driver null"); } else if (pIODriver->ReConnection(m_SCF)) { FireErrorMessage(false, 1, "lost Connect"); m_bConnectFlag = true; mLog::FINFO("PSG_reConnect success"); return true; } else { mLog::FINFO("PSG_reConnect failed"); } return false; } int nsGEN::PSGRFDevice::GridMSMargin()//发生器暂无此设置 { return 0; } bool nsGEN::PSGRFDevice::CalculateAppropriateMA(float& inoutMAS, float& inoutMA, float& inoutMS) { mLog::FINFO("Enter CalculateAppropriateMA:MAS[{$}],MA[{$}],MS[{$}]", inoutMAS, inoutMA, inoutMS); if (m_DoseUnit.m_Focus->Get() == AttrKey::FOCUS_TYPE::FOCUS_LARGE) { m_iMaxPower = PSGRF_LARGE_POWER; } else { m_iMaxPower = PSGRF_SMALL_POWER; } int currKV = 0 , tempMA = 0, tempMS = 0; currKV = m_DoseUnit.m_KV->Get(); tempMA = m_iMaxPower * 1000 / currKV; mLog::Debug("power[{$}]*1000 / KV[{$}] = MAX_MA[{$}]", m_iMaxPower, currKV, tempMA); if (tempMA > PSGRF_MAX_MA) { tempMA = PSGRF_MAX_MA; mLog::Debug(" MAX_MA too big,be close to range_right[{$}]", tempMA); } else if (tempMA < PSGRF_MIN_MA) { mLog::Warn(" MAX_MA too small,compute failed"); return false; } for (int i = tempMA;i >= PSGRF_MIN_MA; i--) { tempMS = inoutMAS * 1000.0 / i; mLog::Debug("MAS[{$}]*1000 / temp_MA[{$}] = temp_MS[{$}]", inoutMAS, i, tempMS); } mLog::Debug("can not use MAS[{$}]compute Appropriate MA MS", inoutMAS); return false; } void nsGEN::PSGRFDevice::ReSendFailedAction(string& cmdNum)//发生器暂无此设置 { } void nsGEN::PSGRFDevice::UpdateLimits(const std::string& key, float& currentValue, float defaultValue) { if (m_GenConfig.GetKeyCount(key.c_str()) > 0) { currentValue = static_cast(m_GenConfig[key.c_str()]); } else { currentValue = defaultValue; } } void nsGEN::PSGRFDevice::UpdateLimitsInt(const std::string& key, int& currentValue, int defaultValue) { if (m_GenConfig.GetKeyCount(key.c_str()) > 0) { currentValue = static_cast(m_GenConfig[key.c_str()]); } else { currentValue = defaultValue; } } bool nsGEN::PSGRFDevice::EnableBucky(int nbucky) { char temp[50]{ 0 }; sprintf_s(temp, "BU%1d", nbucky); return HWSend(temp, strlen(temp)); } bool nsGEN::PSGRFDevice::ECHO(void) { return HWSend("EC", 2); } bool nsGEN::PSGRFDevice::SetABSModeNative(int nMode) { char temp[50]{ 0 }; sprintf_s(temp, "FLA%1d", nMode); return HWSend(temp, strlen(temp)); } bool nsGEN::PSGRFDevice::SetFKM(float fKV, float fMA) { // 使用 std::stringstream 进行字符串格式化 std::ostringstream oss; /*oss << "FKM " << std::setw(3) << std::setfill('0') << static_cast(fKV) << " " << std::setw(4) << std::setfill('0') << static_cast(fMA * 10);*/ oss << "ABS" << std::setw(3) << std::setfill('0') << static_cast(fKV) << std::setw(3) << std::setfill('0') << static_cast(fMA * 10); // 获取格式化后的字符串 std::string command = oss.str(); // 假设 HWSend 函数期望一个以 null 结尾的字符串,使用 c_str() 获取指针 return HWSend(command.c_str(), command.length()); } bool nsGEN::PSGRFDevice::StartHardwareStatusThread() { mLog::FINFO("enter Start HardwareStatus Thread "); if (m_pHardwareStatusThread == NULL) { DWORD m_HardwareStatusID; m_pHardwareStatusThread = CreateThread(0, 0, HardwareStatusThread, this, 0, &m_HardwareStatusID); if (m_pHardwareStatusThread == NULL) { mLog::Error("Start HardwareStatus Thread Failed"); return false; } } return true; } bool nsGEN::PSGRFDevice::StartTrueParameterThread() { mLog::FINFO("enter Start TrueParameterThread Thread "); if (m_pTrueParameterThread == NULL) { DWORD m_pTrueParameterID; m_pTrueParameterThread = CreateThread(0, 0, TrueParameterThread, this, 0, &m_pTrueParameterID); if (m_pTrueParameterThread == NULL) { mLog::Error("Start TrueParameterThread Thread Failed"); return false; } } return true; } DWORD nsGEN::PSGRFDevice::HardwareStatusThread(LPVOID pParam) { PSGRFDevice* pCurGen = (PSGRFDevice*)pParam; if (pCurGen == NULL) { return false; } HeartBeatFlag = true; mLog::FINFO("loopTime = {$}", pCurGen->m_iLoopTime.load()); int messageIndex = 0; while (m_bExtraFlag) { auto now = std::chrono::steady_clock::now(); auto last = lastValidResponse.load(); if (now - last > TIMEOUT && pCurGen->m_DoseUnit.m_GenState->Get() > 0) { pCurGen->m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN); // 超时未响应则重置状态 } if (messageIndex % 5 == 0) { pCurGen->HWSend("HE?", 3); Sleep(100); pCurGen->HWSend("FLT?", 4); Sleep(100); pCurGen->HWSend("ST?", 3); } Sleep(pCurGen->m_iLoopTime); messageIndex++; } return true; } DWORD nsGEN::PSGRFDevice::TrueParameterThread(LPVOID pParam) { PSGRFDevice* pCurGen = (PSGRFDevice*)pParam; if (pCurGen == NULL) { return false; }; return true; } //----------------------------------------------------------------------------- // PSGRFDriver //----------------------------------------------------------------------------- nsGEN::PSGRFDriver::PSGRFDriver() { m_pAttribute.reset(new ResDataObject()); m_pDescription.reset(new ResDataObject()); } nsGEN::PSGRFDriver::~PSGRFDriver() { mLog::Close(); mLog::gLogger = nullptr; } void nsGEN::PSGRFDriver::Prepare() { string strLogPath = GetProcessDirectory() + R"(\OEMDrivers\Generator\Conf\Log4CPP.Config.GEN.xml)"; Log4CPP::GlobalContext::Map::Set(ECOM::Utility::Hash("LogFileName"), "GEN.PSG_RF"); auto rc = Log4CPP::LogManager::LoadConfigFile(strLogPath.c_str()); mLog::gLogger = Log4CPP::LogManager::GetLogger("GEN.PSG_RF"); m_SCFDllName = GetConnectDLL(m_ConfigFileName); super::Prepare(); mLog::FINFO("OK."); } std::string nsGEN::PSGRFDriver::DriverProbe() { mLog::FINFO("DriverProbe in \n"); ResDataObject r_config, HardwareInfo; if (r_config.loadFile(m_ConfigFileName.c_str())) { HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]); HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]); HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]); HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]); HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]); } else { HardwareInfo.add("MajorID", "Generator"); HardwareInfo.add("MinorID", "Dr"); HardwareInfo.add("VendorID", "PSGRF"); HardwareInfo.add("ProductID", "HF"); HardwareInfo.add("SerialID", "Drv"); } string ret = HardwareInfo.encode(); return ret; } bool nsGEN::PSGRFDriver::ReConnection(nsSCF::SCF& DevSCF) { super::Disconnect(); mLog::FINFO("ReConnection:SCF Disconnect"); ResDataObject Connection = GetConnectParam(m_ConfigFileName); mLog::FINFO("ReConnection:{$} \n", Connection.encode()); auto erCode = m_SCF.Connect(Connection.encode(), &nsGEN::PSGRFDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000); if (erCode == SCF_ERR::SCF_SUCCEED) { Sleep(1000); auto rc = super::Connect(); if (!rc) { mLog::FINFO("ReConnection:super Connect failed"); } else { DevSCF = m_SCF; return true; } } else { mLog::FINFO("ReConnection failed"); } return false; } bool nsGEN::PSGRFDriver::Connect() { mLog::FINFO("Enter {$},configlef={$}\n", __FUNCTION__, m_ConfigFileName.c_str()); super::Disconnect(); m_SCF.Disconnect(); ResDataObject Connection = GetConnectParam(m_ConfigFileName); // 检查文件是否存在 std::ifstream file(m_ConfigFileName); if (!file) { // 文件不存在,直接返回空的Connection对象 mLog::FINFO("file does not exist"); } mLog::FINFO("connections:{$} \n", Connection.encode()); auto erCode = m_SCF.Connect(Connection.encode(), &nsGEN::PSGRFDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000); if (erCode != SCF_ERR::SCF_SUCCEED) { mLog::FINFO("SCF connection failed: erCode != SCF_ERR::SCF_SUCCEED\n"); return false; //return erCode; } auto rc = super::Connect(); if (!rc) { mLog::FINFO("super::Connect() failed\n"); return false; //return 0; } mLog::FINFO("connections SUCCEED \n"); return true; //return SCF_ERR::SCF_SUCCEED; } auto nsGEN::PSGRFDriver::CreateDevice(int index) -> std::unique_ptr { mLog::FINFO("CreateDevice in\n"); /*if (!m_SCF.isConnected()) { mLog::Error("CreateDevice:m_SCF is not Connected \n"); return nullptr; }*/ m_pDevice = new PSGRFDevice(EventCenter, m_SCF, m_ConfigFileName); auto dev = std::unique_ptr (new IODevice(m_pDevice)); mLog::FINFO("CreateDevice out\n"); return dev; } void nsGEN::PSGRFDriver::FireNotify(int code, std::string key, std::string content) { EventCenter->OnNotify(code, key, content); } bool nsGEN::PSGRFDriver::isConnected() const { if (!super::isConnected()) { mLog::FINFO("No valid connection!!!\n"); return false; } if (m_pDevice == nullptr) { mLog::FINFO("m_pDevice == nullptr\n"); return false; } if (m_pDevice->LoadConfig(m_ConfigFileName) == -1) { return false; //return 0; } int genState = m_pDevice->GetGenState(); mLog::FINFO("m_pDevice->GetGenState() == {$}\n", genState); return genState > 0; // 只有返回值大于 0 时才返回 true } std::string nsGEN::PSGRFDriver::GetResource() { mLog::Debug("GetResource"); ResDataObject r_config, temp; if (!temp.loadFile(m_ConfigFileName.c_str())) { return ""; } m_ConfigAll = temp; r_config = temp["CONFIGURATION"]; m_Configurations = r_config; ResDataObject DescriptionTemp; ResDataObject DescriptionSend; ResDataObject m_DescriptionSend; ResDataObject ListTemp; string strTemp = ""; //用于读取字符串配置信息 string strIndex = ""; //用于读取配置信息中的List项 int nTemp = -1; //用于读取整型配置信息 char sstream[10] = { 0 }; //用于转换值 string strValue = ""; //用于存储配置的值 string strType = ""; //用于存储配置的类型 int/float/string... /*** * 1. 通过循环,将所有配置项写到pDeviceConfig * 2. 记录配置项的内部key以及配置类型,类型对应了不同配置文件路径,用于读写真实值 ***/ try { //便利ConfigToolInfo 中 所有的AttributeInfo 属性段 int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo"); m_pAttribute->clear(); m_pDescription->clear(); for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++) { DescriptionTemp.clear(); DescriptionSend.clear(); ListTemp.clear(); //AttributeType strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Type"]; DescriptionTemp.add(ConfKey::CcosType, strTemp.c_str());//CcosGeneratorAttribute DescriptionSend.add(ConfKey::CcosType, strTemp.c_str());//CcosGeneratorAttribute strType = strTemp; //记录配置项的类型 //AttributeKey //1. 根据AttributeType,内部key和配置路径,拿到当前的真实值 strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"]; nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"]; GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue); //得到strValue的值 //printf("********************************innerkey=%s --strValue = %s\n", strTemp.c_str(), strValue.c_str()); //2. 赋值 strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; if ("int" == strType) { (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str())); } else if ("float" == strType) { (*m_pAttribute).add(strTemp.c_str(), atoi(strValue.c_str())); } else //其它先按string类型处理 { (*m_pAttribute).add(strTemp.c_str(), strValue.c_str()); } //printf("********************************outkey =%s --strValue = %s\n", strTemp.c_str(), strValue.c_str()); //AttributeAccess strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Access"]; DescriptionTemp.add(ConfKey::CcosAccess, strTemp.c_str()); DescriptionSend.add(ConfKey::CcosAccess, strTemp.c_str()); /* //AttributeRangeMin strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMin"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::CcosRangeMin, strTemp.c_str()); } //AttributeRangeMax strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["RangeMax"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::CcosRangeMax, strTemp.c_str()); } */ //AttributeList nTemp = m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListNum"]; if (nTemp > 0) //ListNum不大于0时说明不需要list配置 { for (int nListIndex = 0; nListIndex < nTemp; nListIndex++) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["ListInfo"][nListIndex]; auto temKey = std::to_string(nListIndex); ListTemp.add(temKey.c_str(), strTemp.c_str()); } DescriptionTemp.add(ConfKey::CcosList, ListTemp); DescriptionSend.add(ConfKey::CcosList, ListTemp.encode()); } //AttributeRequired strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["Required"]; DescriptionTemp.add(ConfKey::CcosRequired, strTemp.c_str()); DescriptionSend.add(ConfKey::CcosRequired, strTemp.c_str()); //AttributeDefaultValue strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeDescripition"]["DefaultValue"]; if (strTemp != "") //不需要的配置项为空 { DescriptionTemp.add(ConfKey::CcosDefaultValue, strTemp.c_str()); DescriptionSend.add(ConfKey::CcosDefaultValue, strTemp.c_str()); } strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; (*m_pDescription).add(strTemp.c_str(), DescriptionTemp); m_DescriptionSend.add(strTemp.c_str(), DescriptionSend.encode()); } } catch (ResDataObjectExption& e) { mLog::Error("Get config error: {$}", e.what()); return ""; } ResDataObject resDeviceResource; resDeviceResource.add(ConfKey::CcosGeneratorAttribute, (*m_pAttribute)); resDeviceResource.add(ConfKey::CcosGeneratorDescription, (*m_pDescription)); ResDataObject DescriptionTempEx; DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource); m_DeviceConfig.clear(); m_DeviceConfig = DescriptionTempEx; //mLog::Debug("local ************* get resource over {$}", DescriptionTempEx.encode()); //printf("local ************* get resource over %s \n", DescriptionTempEx.encode()); resDeviceResource.clear(); resDeviceResource.add(ConfKey::CcosGeneratorAttribute, (*m_pAttribute)); resDeviceResource.add(ConfKey::CcosGeneratorDescription, m_DescriptionSend); DescriptionTempEx.clear(); DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource); m_DeviceConfigSend.clear(); m_DeviceConfigSend = DescriptionTempEx; string res = m_DeviceConfigSend.encode(); //printf("%s", res.c_str()); //mLog::Debug("get resource over {$}", DescriptionTempEx.encode()); //("************* get resource over %s \n", DescriptionTempEx.encode()); return res; } std::string nsGEN::PSGRFDriver::DeviceProbe() { mLog::FINFO("std::string nsGEN::PSGRFDriver::DeviceProbe() in\n"); ResDataObject r_config, HardwareInfo; if (r_config.loadFile(m_ConfigFileName.c_str())) { HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]); HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]); HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]); HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]); HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]); } else { HardwareInfo.add("MajorID", "Generator"); HardwareInfo.add("MinorID", "Dr"); HardwareInfo.add("VendorID", "PSGRF"); HardwareInfo.add("ProductID", "HF"); HardwareInfo.add("SerialID", "Dev"); } string ret = HardwareInfo.encode(); mLog::FINFO("std::string nsGEN::PSGRFDriver::DeviceProbe() out\n"); return ret; } void nsGEN::PSGRFDriver::Disconnect() { super::Disconnect(); m_SCF.Disconnect(); mLog::Close(); mLog::gLogger = nullptr; } void nsGEN::PSGRFDriver::Dequeue(const char* Packet, DWORD Length) { DecodeFrame(Packet, Length); } PACKET_RET nsGEN::PSGRFDriver::callbackPackageProcess(const char* RecData, DWORD nLength, DWORD& PacketLength) { if (nLength < 1) { mLog::FINFO("nLength < 1, nLength=={$} \n", nLength); return PACKET_USELESS; } for (DWORD i = 0; i < nLength - 1; i++) { //1 首先寻找包头 if (RecData[i] == 0x02) { if (i != 0) { PacketLength = i; //i之前的数据,全部扔掉 char strtemp[100] = { 0 }; memcpy(strtemp, RecData, i); strtemp[PacketLength + 1] = 0; printf("==IN error data ==:%s,PacketLength=%d,nLength=%d\n", strtemp, PacketLength, nLength); return PACKET_USELESS; } } if (RecData[i] == 0x03) { PacketLength = i + 2; //+2 because index + ETX + sum. char strtemp[100] = { 0 }; memcpy(strtemp, RecData, i); //RKC005(not include 03 + sum); //only 复制 0x03之前的数据,这样解析时,后面的03 ,sum 都不要了。 strtemp[PacketLength + 1] = 0; mLog::FINFO("==IN==:{$}\n", strtemp); return PACKET_ISPACKET; } } return PACKET_NOPACKET; } bool nsGEN::PSGRFDriver::GetDeviceConfig(std::string& Cfg) { Cfg = m_DeviceConfigSend.encode(); printf("GetDeviceConfig over , %s", Cfg.c_str()); return true; } bool nsGEN::PSGRFDriver::SetDeviceConfig(std::string Cfg) { mLog::FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str()); printf("\n--Func-- SetDeviceConfig %s\n", Cfg.c_str()); ResDataObject DeviceConfig; DeviceConfig.decode(Cfg.c_str()); ResDataObject DescriptionTempEx; DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"]; mLog::Debug("Attribute:{$}", DescriptionTempEx.encode()); bool bSaveFile = false; //true:重新保存配置文件 string strAccess = ""; for (int i = 0; i < DescriptionTempEx.size(); i++) { string strKey = DescriptionTempEx.GetKey(i); mLog::FINFO("{$}", strKey.c_str()); printf("%s\n", strKey.c_str()); try { if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0) { strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"]; if ("RW" == strAccess) { //修改对应配置,在其他单元的配置项要同时调用其修改函数修改真实值 //1. 修改内存中的值,用于给上层发消息 (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i]; //2. 拿到Innerkey int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo"); mLog::FINFO("nConfigInfoCount {$}", nConfigInfoCount); string strTemp = ""; //存储AttributeKey for (int nInfoIndex = 0; nInfoIndex < nConfigInfoCount; nInfoIndex++) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["AttributeKey"]; if (strTemp == strKey) { strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"]; break; } } //3. 修改配置文件中的值 if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i])) { mLog::Debug("SetDeviceConfigValue over"); bSaveFile = true; } } else { mLog::FINFO("{$} is not a RW configuration item", strKey.c_str()); } } else { mLog::FINFO("without this attribute {$}", strKey.c_str()); } } catch (ResDataObjectExption& e) { printf("\nSetDriverConfig crashed: %s\n", e.what()); mLog::Error("SetDriverConfig crashed: {$}", e.what()); return false; } } if (bSaveFile) { //3. 重新保存配置文件 SaveConfigFile(true); } return true; } bool nsGEN::PSGRFDriver::SaveConfigFile(bool bSendNotify) { m_ConfigAll["CONFIGURATION"] = m_Configurations; bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str()); mLog::FINFO("SaveConfigFile over {$}", bRt); return true; } bool nsGEN::PSGRFDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue) { strValue = ""; string strTemp = pInnerKey; if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取 { int pos = 0; ResDataObject resTemp = config; while ((pos = strTemp.find_first_of(',')) != string::npos) { string Key = strTemp.substr(0, pos); string TempValue = resTemp[Key.c_str()].encode(); // printf("-TempValue=== %s", TempValue.c_str()); resTemp.clear(); resTemp.decode(TempValue.c_str()); strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1); //printf("-************--%s", strTemp.c_str()); } if (strTemp != "") { strValue = (string)resTemp[strTemp.c_str()]; } else { strValue = (string)resTemp; } } //printf("------------%s", strValue.c_str()); return true; } bool nsGEN::PSGRFDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue) { string strTemp = pInnerKey; mLog::Debug("Begin to change {$} item value to {$}", pInnerKey, szValue); printf("\nbbbbbbbbbbbbbbBegin to change {%s} item value to {%s}\n", pInnerKey, szValue); if (1 == nPathID) //从DriverConfig路径下每个DPC自己的配置文件读取 { try { int pos = 0; ResDataObject* resTemp = &config; while ((pos = strTemp.find_first_of(',')) != string::npos) { string Key = strTemp.substr(0, pos); resTemp = &(*resTemp)[Key.c_str()]; strTemp = strTemp.substr(pos + 1, strTemp.length() - pos - 1); } if (strTemp != "") { (*resTemp)[strTemp.c_str()] = szValue; } else { *resTemp = szValue; } } catch (ResDataObjectExption& e) { mLog::Error("SetDriverConfigvalue crashed: {$}", e.what()); return false; } } return true; } //----------------------------------------------------------------------------- // GetIODriver & CreateIODriver //----------------------------------------------------------------------------- static nsGEN::PSGRFDriver gIODriver; extern "C" CCOS::Dev::IODriver * __cdecl GetIODriver() // 返回静态对象的引用, 调用者不能删除 ! { return &gIODriver; } extern "C" CCOS::Dev::IODriver * __cdecl CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 ! { pIODriver = new nsGEN::PSGRFDriver(); return pIODriver; }