| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372 |
- // CCOS.Dev.GEN.CPI.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include <assert.h>
- #include <functional>
- #include <unordered_map>
- #include <fstream>
- #include <set>
- #include <unistd.h>
- #include <linux/limits.h>
- #include "LogicDevice.h"
- #include "CCOS.Dev.Generator.CPI.h"
- #include "Helper.JSON.hpp"
- #include "LogLocalHelper.h"
- #include "Log4CPP.h"
- using namespace std::placeholders;
- using namespace CCOS::Dev::Detail::Generator;
- namespace nsGEN = CCOS::Dev::Detail::Generator;
- static const int msTimeOut_Lock = 500;
- // Linux 环境下的 SCF 库文件名
- static const auto COM_SCFDllName = "libSerialSCF.so";
- static const auto TCP_SCFDllName = "libTcpipSCF.so";
- // 定义 Sleep 宏(如果头文件中没有定义)
- #ifndef Sleep
- #define Sleep(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms))
- #endif
- //-----------------------------------------------------------------------------
- // CPIDevice
- //-----------------------------------------------------------------------------
- float g_MA_List[] = { 8, 10, 12.5, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640 };
- float g_MAS_List[] = { 1, 1.25, 1.6, 2, 2.5, 3.2, 4, 5, 6.4, 8, 10, 12.5, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000 };
- float g_MS_List[] = { 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6400, 8000, 10000 };
- int g_AECFIELD_List[] = { 1, 10, 100, 11, 101, 111 };
- std::vector <float> gf_MA_List = { 8, 10, 12.5, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640 };
- string GetProcessDirectory()
- {
- char result[PATH_MAX] = { 0 };
- ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
- if (count == -1)
- {
- return "";
- }
- string fullpath = string(result, count);
- string::size_type lastSlash = fullpath.find_last_of('/');
- if (lastSlash == string::npos)
- {
- return "";
- }
- return fullpath.substr(0, lastSlash);
- }
- nsGEN::CPIDevice::CPIDevice(std::shared_ptr <IOEventCenter> center, std::shared_ptr<SCFWrapper> SCF, string configfile)
- : super(center)
- , superGen()
- , m_SCF(SCF)
- {
- assert(EventCenter);
-
- FINFO("version:3.0.0.5");
- m_DoseUnit.m_KV.reset(new KVMould(0.0, 40.0, 120.0, 1.0));
- m_DoseUnit.m_MA.reset(new MAMould(0.0, 1.0, 1000.0, 0.1));
- m_DoseUnit.m_MS.reset(new MSMould(0.0, 1.0, 10000.0, 0.01));
- m_DoseUnit.m_MAS.reset(new MASMould(0.0, 0.5, 1000.0, 0.01));
- m_DoseUnit.m_Techmode.reset(new TECHMODEMould(0, 0, 2, 1));
- m_DoseUnit.m_WS.reset(new WORKSTATIONMould(1, 0, 5, 1));
- m_DoseUnit.m_Focus.reset(new FOCUSMould(1, 0, 1, 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, -4, 4, 1));
- m_DoseUnit.m_HE.reset(new TUBEHEATMould(0, 0, 100, 1));
- m_DoseUnit.m_PostKV.reset(new POSTKVMould(0.0, 40.0, 120.0, 1.0));
- m_DoseUnit.m_PostMA.reset(new POSTMAMould(0.0, 1.0, 1000.0, 0.1));
- m_DoseUnit.m_PostMS.reset(new POSTMSMould(0.0, 1.0, 10000.0, 0.01));
- m_DoseUnit.m_PostMAS.reset(new POSTMASMould(0.0, 0.5, 1000.0, 0.01));
- m_DoseUnit.m_GenSynState.reset(new GENSYNSTATEMould(0, 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_MSGUnit.reset(new nsDetail::MSGUnit(center, nsGEN::GeneratorUnitType));
- m_DAP.reset(new DevDAP::DOSEMould(0.0, 0.0, 1000.0, 0.01));
- m_hGenPostEvent = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
- m_bExtraFlag = true; // 启用硬件状态查询线程
- m_bWithCollimator = false;
- m_nDeviceType = 0;
-
- m_fMsLimit = 500;
- m_strConfigPath = configfile;
- ResDataObject temp;
- temp.loadFile(m_strConfigPath.c_str());
- m_GenConfig = temp["CONFIGURATION"];
- TransJsonText(m_GenConfig);
- m_nDeviceType = (int)(m_GenConfig["GenType"]);//默认设置为2. 带有coll的。
- if (m_nDeviceType == 2)
- {
- m_bWithCollimator = true;
- }
- m_nAECMode = (int)m_GenConfig["AECMODE"];
- m_fMsLimit = (float)m_GenConfig["LIMITMS"];//default 500
- m_nCtlMode = (int)m_GenConfig["GenCtrlMode"];//default 2
- char strStart1[6] = { 0x01,0x35,0x00,0x01,0x01,0x38 };
- char strStart2[7] = { 0x01,0x01,0x00,0x02,0x00,0x64,0x73 };
- char strStart0a[7] = { 0x01,0x06,0x00,0x02,0x01,0x00,0x0a };
- char strStart1b[7] = { 0x01,0x06,0x00,0x02,0x01,0x01,0x0b };
- char strStart2c[7] = { 0x01,0x06,0x00,0x02,0x01,0x02,0x0c };
- char strStart3d[7] = { 0x01,0x06,0x00,0x02,0x01,0x03,0x0d };
- char strStart4e[7] = { 0x01,0x06,0x00,0x02,0x01,0x04,0x0e };
- char strStart5f[7] = { 0x01,0x06,0x00,0x02,0x01,0x05,0x0f };
- char strStart00c[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x00,0x0c };
- char strStart01d[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x01,0x0d };
- char strStart02e[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x02,0x0e };
- char strStart03f[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x03,0x0f };
- char strStart040[8] = { 0x01,0x07,0x00,0x03,0x01,0x00,0x04,0x10 };
- char strEnd1[8] = { 0x43,0x41,0x30,0x03,0xb7,0x50,0x52,0x30 };
- char strEnd2[8] = { 0x03,0xd5,0x01,0x35,0x00,0x01,0x00,0x37 };
- if (m_nCtlMode == 2)
- {
- //SendCommands(strStart1, 6);
- HWSend(strStart1);
- //::Sleep(50);
- HWSend(strStart2);
- //::Sleep(50);
- HWSend(strStart0a);
- //::Sleep(50);
- HWSend(strStart1b);
- //::Sleep(50);
- HWSend(strStart2c);
- //::Sleep(50);
- HWSend(strStart3d);
- //::Sleep(50);
- HWSend(strStart4e);
- //::Sleep(50);
- HWSend(strStart5f);
- //::Sleep(50);
- HWSend(strStart0a);
- //::Sleep(50);
- HWSend(strStart00c);
- //::Sleep(50);
- HWSend(strStart01d);
- //::Sleep(50);
- HWSend(strStart02e);
- //::Sleep(50);
- HWSend(strStart03f);
- //::Sleep(50);
- HWSend(strStart040);
- //::Sleep(50);
- HWSend(strStart00c);
- //::Sleep(50);
- HWSend(strStart01d);
- //::Sleep(50);
- HWSend(strStart02e);
- //::Sleep(50);
- HWSend(strStart03f);
- //::Sleep(50);
- HWSend(strStart040);
- SetExpEnable();
- }
- m_bDAPEnable = false;
- m_bSaveMSMA = true;
- //SynMode default is what?
- OnCallBack();
- Register();
- Reset();
- Sleep(500);
- HWSend("DS");
- HWSend("ET?");
- HWSend("ST");
- RefreshData();
- HWSend("ATU?");//直到收到ATU1为止。
- m_bCheckATUStatus = true;
- StartHardwareStatusThread();
- }
- nsGEN::CPIDevice::~CPIDevice()
- {
- FINFO("========================================");
- FINFO("CPIDevice Destructor Starting...");
- FINFO("========================================");
- FINFO("Stopping hardware status thread...");
- m_bExtraFlag = false;
- if (m_pHardwareStatusThread.joinable()) {
- FINFO("Waiting for hardware status thread to join...");
- m_pHardwareStatusThread.join();
- FINFO("Hardware status thread joined successfully");
- } else {
- FINFO("Hardware status thread not joinable, skipping join");
- }
- FINFO("========================================");
- FINFO("CPIDevice Destructor Completed");
- FINFO("========================================");
- }
- std::string nsGEN::CPIDevice::GetGUID() const
- {
- FINFO("\n===============GetGUID : {$} ===================\n", GeneratorUnitType);
- return GeneratorUnitType;
- }
- void nsGEN::CPIDevice::Register()
- {
- auto Disp = m_Dispatch.Lock().As();
- superGen::Register(Disp);
- superGen::RegisterRAD(Disp);
- superGen::RegisterAEC(Disp);
- superGen::RegisterExpEnable(Disp);
- Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
- auto fun_Clear_DAP = [this](auto a, auto&)
- {
- return Clear_DAP();
- };
- Disp->Action.Push("Clear_DAP", fun_Clear_DAP);
- auto fun_GetValue_DAP = [this](auto a, auto& b)
- {
- float value = 0;
- RET_STATUS ret = GetValue_DAP(value);
- b = ToJSON(value);
- return ret;
- };
- Disp->Action.Push("GetValue_DAP", fun_GetValue_DAP);
- auto fun_StartMove = [this](auto a,auto& b)
- {
- return StartMove();
- };
- Disp->Action.Push("StartMove", fun_StartMove);
- auto fun_EndMove = [this](auto a, auto& b)
- {
- return EndMove();
- };
- Disp->Action.Push("EndMove", fun_EndMove);
- }
- RET_STATUS nsGEN::CPIDevice::IncKV()
- {
- if (!m_DoseUnit.m_KV->CanInc()) return RET_STATUS::RET_SUCCEED;
- return HWSend("KV+");
- }
- RET_STATUS nsGEN::CPIDevice::DecKV()
- {
- if (!m_DoseUnit.m_KV->CanDec()) return RET_STATUS::RET_SUCCEED;
- return HWSend("KV-");
- }
- RET_STATUS nsGEN::CPIDevice::SetKV(float value)
- {
- if (!m_DoseUnit.m_KV->Verify(value)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "KV%03d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::IncMA()
- {
- if (!m_DoseUnit.m_MA->CanInc()) return RET_STATUS::RET_SUCCEED;
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode == 1)
- {
- FINFO("\n In AEC mode, do not allowed INCMA");
- return RET_STATUS::RET_FAILED;
- }
-
- return HWSend("MA+");
- }
- RET_STATUS nsGEN::CPIDevice::DecMA()
- {
- if (!m_DoseUnit.m_MA->CanDec()) return RET_STATUS::RET_SUCCEED;
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode == 1)
- {
- FINFO("\n In AEC mode, do not allowed DECMA");
- return RET_STATUS::RET_FAILED;
- }
-
- return HWSend("MA-");
- }
- RET_STATUS nsGEN::CPIDevice::SetMA(float value)
- {
- if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
-
-
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode == 1)
- {
- //logfile->WriteLog("In AEC mode, do not allowed SetMA", LOG_INFORMATION, LOG_DEBUG, true);
- FINFO("\n In AEC mode, do not allowed SetMA");
- return RET_STATUS::RET_FAILED;
- }
-
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "MA%05d", (int)(value * 10));
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::IncMS()
- {
- if (!m_DoseUnit.m_MS->CanInc()) return RET_STATUS::RET_SUCCEED;
- /*
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- PRINTA_INFO("\n Techmode is MAS, can't inc MS");
- return RET_STATUS::RET_FAILED;
- }
- */
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 2)
- {
- //logfile->WriteLog("In AEC mode, do not allowed SetMA", LOG_INFORMATION, LOG_DEBUG, true);
- FINFO("\n In AEC mode, do not allowed INCMS");
- return RET_STATUS::RET_FAILED;
- }
- return HWSend("MS+");
- }
- RET_STATUS nsGEN::CPIDevice::DecMS()
- {
- if (!m_DoseUnit.m_MS->CanDec()) return RET_STATUS::RET_SUCCEED;
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 2)
- {
- FINFO("\n Techmode is MAS, can't dec MS");
- return RET_STATUS::RET_FAILED;
- }
- return HWSend("MS-");
- }
- RET_STATUS nsGEN::CPIDevice::SetMS(float value)
- {
- if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 2)
- {
- FINFO("\n In AEC mode, do not allowed SetMS");
- return RET_STATUS::RET_FAILED;
- }
- char temp[50] { 0 };
- snprintf(temp, sizeof(temp), "MS%05d", (int)(value * 10));
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::IncMAS()
- {
- if (!m_DoseUnit.m_MAS->CanInc()) return RET_STATUS::RET_SUCCEED;
-
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 1)
- {
- FINFO("\n Techmode is not MAS, can't inc MAS");
- return RET_STATUS::RET_FAILED;
- }
- return HWSend("MX+");
- }
- RET_STATUS nsGEN::CPIDevice::DecMAS()
- {
- if (!m_DoseUnit.m_MAS->CanDec()) return RET_STATUS::RET_SUCCEED;
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 1)
- {
- FINFO("\n Techmode is not MAS, can't dec MAS");
- return RET_STATUS::RET_FAILED;
- }
- return HWSend("MX-");
- }
- RET_STATUS nsGEN::CPIDevice::SetMAS(float value)
- {
- if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC && m_nAECMode != 1)
- {
- FINFO("\n Techmode is not MAS, can't set MAS");
- return RET_STATUS::RET_FAILED;
- }
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "MX%05d", (int)(value * 10));
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::SetTechmode(int value)
- {
- if (!m_DoseUnit.m_Techmode->Verify(value)) return RET_STATUS::RET_SUCCEED;
-
- //char temp[50] = { 0 };
- //sprintf_s(temp, "ET%01d", (int)value);
- //return HWSend(temp);
- //CPI设置ET mode有点特殊
- float fms, fma;
- m_nPreET = m_DoseUnit.m_Techmode->Get();
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "ET%01d", (int)value);
- HWSend(temp);
- if (!m_bSaveMSMA)
- return RET_STATUS::RET_FAILED;
-
- if (value == AttrKey::TECHMODE_V2TYPE::ET_TIME)
- {
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)//NOTE: use m_DoseUnit.m_Techmode->Get() not accurate. m_nPreET more better.!!!!
- {
- // mas - time
- if ((m_fPreMA > 0.1))
- {
- Sleep(50);
- SetMA(m_fPreMA);
- }
- Sleep(50);
- SetMS(m_PrefMS);
- }
- else if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC)
- {
- // aec - time
- if (m_bSaveMSMA)
- {
- fms = m_PrefMS;
- fma = m_fPreMA;
- Sleep(50);
- SetMS(fms);
- Sleep(50);
- SetMA(fma);
- }
- }
- }
- else if (value == AttrKey::TECHMODE_V2TYPE::ET_MAS) //mas
- {
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
- {
- // time - mas save
- if (m_bSaveMSMA)
- {
- m_PrefMS = m_DoseUnit.m_MS->Get();
- m_fPreMA = m_DoseUnit.m_MA->Get();
- }
- }
- else if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_AEC)
- {
- // aec - mas
- if (m_bSaveMSMA)
- {
- fms = m_PrefMS;
- fma = m_fPreMA;
- }
- SetMAS(m_PrefMS * m_fPreMA / 1000); //mas mode : set dose to ma * ms
- }
- }
- else if (value == AttrKey::TECHMODE_V2TYPE::ET_AEC) //aec
- {
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
- {
- // time - aec save
- if (m_bSaveMSMA)
- {
- m_PrefMS = m_DoseUnit.m_MS->Get();
- m_fPreMA = m_DoseUnit.m_MA->Get();
-
- }
- }
- else if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- // mas - aec
- if ((m_fPreMA > 0.1))
- {
- Sleep(50);
- if (m_nAECMode != 1)
- SetMA(m_fPreMA);
- }
- }
- Sleep(50);
- if (m_nAECMode == 2)
- SetMS(m_fMsLimit);
- //ysj++:不知道为什么要这么干,不过此处需要一个设置MS的语句
- //if (m_nWS == GEN_WS_TOMO)
- //{
- // if (m_nAECMode == 2)
- // SetMS(m_PrefMS);
- //}
- //else
- //{
- // if (m_nAECMode == 2)
- // SetMS(m_fMsLimit);
- //}
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetFocus(int value)
- {
- if (!m_DoseUnit.m_Focus->Verify(value)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FO%01d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::SetAECDensity(int value)
- {
- FINFO("Enter SetAECDensity {$}", 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;
- }
- }
- char temp[50] = { 0 };
- if (nAECDensity >= 0)
- {
- snprintf(temp, sizeof(temp), "FN+%01d", (int)nAECDensity);
- }
- else
- {
- snprintf(temp, sizeof(temp), "FN-%01d", (int)nAECDensity);
- }
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::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;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FI%03d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::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;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FS%03d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::SetWS(const string value)
- {
- 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"];
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "WS%01d", tempws);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::SetAPR(const _tAPRArgs& t)
- {
- m_t = t;
- FINFO("*********************Enter SetAPR**********************");
- FINFO("APR:t.fKV={$}, t.fMA={$}, t.fMAS={$}, t.fMS={$}, t.nAECDensity={$}, t.nAECField={$}, t.nAECFilm={$}, t.nFocus={$}, t.nTechmode={$}", t.fKV, t.fMA, t.fMAS, t.fMS, t.nAECDensity, t.nAECField, t.nAECFilm, t.nFocus, t.nTechmode);
- /// //////////////////////////////////////////////begin
- //SetWS(); //ysj++:缺少传递ws的apr结构体
- //GetTechMode(); //ysj++:理论上,init时,RR指令已经获取ET了。
- HWSend("ET?");
- if (m_DoseUnit.m_Focus->Get() == 0)
- {
- SetFocus(t.nFocus);
- }
- /*
- * //not add this logic: because struct _tAPRArgs ,not include WS item.
-
- if ((nWS != GEN_WS_TABLE) && (nWS != GEN_WS_WALL) && (nET == GEN_ET_AEC))
- {
- if (m_nET != GEN_ET_AEC)
- {
- nET = m_nET;
- }
- else
- {
- nET = GEN_ET_MATIME;
- }
- }
- */
- m_PrefMS = t.fMS;
- m_fPreMA = t.fMA;
- m_bSaveMSMA = false;
-
- if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_AEC)
- {
- // aec
- SetTechmode(t.nTechmode);
- SetAECField(t.nAECField);
- SetAECDensity(t.nAECDensity);
-
- if (t.nAECFilm == 0)
- {
- SetAECFilm(10);
- }
- else
- {
- SetAECFilm(t.nAECFilm);
- }
- if (m_nAECMode == 2)
- {
- if (t.fKV > m_DoseUnit.m_KV->Get())
- {
- SetMA(t.fMA);
- SetKV(t.fKV);
- }
- else
- {
- if (t.fMA < m_DoseUnit.m_MA->Get())
- {
- SetMA(t.fMA);
- SetKV(t.fKV);
- }
- else
- {
- SetKV(t.fKV);
- SetMA(t.fMA);
- }
- }
- Sleep(50);
- SetMS(m_fMsLimit);//int V2 mode.m_fMsLimit is set by UI.but in CCOS2.0 it set by config file.
- }
- else if (m_nAECMode == 1)
- {
- if (t.fKV > m_DoseUnit.m_KV->Get())
- {
- SetMA(t.fMA);
- Sleep(50);
- SetKV(t.fKV);
- }
- else
- {
- if (t.fMA < m_DoseUnit.m_MA->Get())
- {
- SetMA(t.fMA);
- Sleep(50);
- SetKV(t.fKV);
- }
- else
- {
- SetKV(t.fKV);
- Sleep(50);
- SetMA(t.fMA);
- }
- }
- Sleep(50);
- SetMAS(t.fMAS);
- }
- else
- {
- if (t.fMA < m_DoseUnit.m_MA->Get())
- {
- SetMA(t.fMA);
- Sleep(50);
- SetKV(t.fKV);
- }
- else
- {
- SetKV(t.fKV);
- Sleep(50);
- SetMA(t.fMA);
- }
- }
- }
- else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- // mas
- SetTechmode(t.nTechmode);
- SetKV(t.fKV);
- 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);
- Sleep(50);
- if (t.fKV > m_DoseUnit.m_KV->Get())
- {
- SetMA(t.fMA);
- Sleep(50);
- SetKV(t.fKV);
- }
- else
- {
- if (t.fMA < m_DoseUnit.m_MA->Get())
- {
- SetMA(t.fMA);
- Sleep(50);
- SetKV(t.fKV);
- }
- else
- {
- SetKV(t.fKV);
- Sleep(50);
- SetMA(t.fMA);
- }
- }
- Sleep(80);
- SetMS(t.fMS);
- }
- if (m_DoseUnit.m_Focus->Get() == 1)
- {
- SetFocus(t.nFocus);
- }
- m_bSaveMSMA = true;
- ///////////////////////end
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::QueryDAP()
- {
- if (m_bDAPEnable)
- {
- return HWSend("DA");
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::QueryHE(int& value)
- {
- value = m_DoseUnit.m_HE->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::QueryPostKV(float& value)
- {
- value = m_DoseUnit.m_PostKV->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::QueryPostMA(float& value)
- {
- value = m_DoseUnit.m_PostMA->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::QueryPostMS(float& value)
- {
- value = m_DoseUnit.m_PostMS->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::QueryPostMAS(float& value)
- {
- value = m_DoseUnit.m_PostMAS->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::Clear_DAP()
- {
- if (m_bDAPEnable)
- {
- return HWSend("DZ");
- }
- else
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::GetValue_DAP(float& value)
- {
- value = m_DAP->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::StartMove()
- {
- FINFO("Enter startMove");
- FINFO("end startmove");
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::EndMove()
- {
- FINFO("Enter endmove");
- FINFO("end EndMove");
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetCollimatorSize(int height, int width)
- {
- if (!m_bWithCollimator)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "ACH%05d", (int)(height * 100));
- HWSend(temp);
- snprintf(temp, sizeof(temp), "ACW%05d", (int)(width * 100));
- HWSend(temp);
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetFilter(int filterType)
- {
- if (!m_bWithCollimator)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- if (filterType > 3)
- {
- filterType -= 3;
- }
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FIL%02d", (int)(filterType));
- return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::SetGenSynState(int value)
- {
- if (AttrKey::GENERATOR_RAD_XRAYON == value) //��ͬ��,�յ�XR1ʱ����ϵͳ���ΪXRAYON״̬�������ô˴���Ȼ�������̻ظ�XR1����������Ȼ����������ߡ�
- {
- FINFO("SetGenSynState be call.this is soft syn mode.");
- HWSend("XR1");
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON);
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetGenState(int value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetExpMode(std::string value)
- {
- FINFO("Enter SetExpMode...{$}",value);
- m_DoseUnit.m_ExpMode->Update(value);
-
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetFLFMode(std::string value)
- {
- return RET_STATUS::RET_SUCCEED;
- //char temp[50]{ 0 };
- //sprintf_s(temp, "RTS%s",value.c_str());
- //return HWSend(temp);
- }
- RET_STATUS nsGEN::CPIDevice::SetEXAMMode(std::string value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetFrameRate(float frameRate)
- {
- m_DoseUnit.m_FrameRate->Update(frameRate);
-
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetRPS(int rps)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::RefreshData()
- {
- if (m_nDeviceType == 1)
- {
- HWSend("RRX");
- }
- else
- {
- //HWSend("RR");
- HWSend("KV?");
- HWSend("MA?");
- HWSend("MS?");
- HWSend("MX?");
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetExpEnable()
- {
- m_bExpEnable = true;
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetExpDisable()
- {
- m_bExpEnable = false;
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::Reset()
- {
- return HWSend("RE");
- }
- RET_STATUS nsGEN::CPIDevice::SetCollimatorDev(OemCollimator* dev)
- {
- if (dev)
- m_pCollDev.reset(dev);
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetMechDev(OemMechanical* dev)
- {
- //if (dev)
- //m_pMechDev.reset(dev);
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::SetDapDev(OemDap* dev)
- {
- if (dev)
- m_pDapDev.reset(dev);
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::CPIDevice::HWSend(const char* strCommand, int nTimeOut)
- {
- if (!m_SCF) {
- FINFO("Failed - Serial communication interface not initialized");
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
- {
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- FINFO("Generator status updated to {$}", static_cast<int>(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN));
- }
- return RET_STATUS::RET_FAILED;
- }
- if (!m_SCF->IsConnected())
- {
- FERROR("Failed - Device not connected");
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
- {
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- FINFO("Generator status updated to {$}", static_cast<int>(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN));
- }
- return RET_STATUS::RET_FAILED;
- }
- // 计算命令长度
- int cmdLen = strlen(strCommand);
- // 检查缓冲区大小,数据包 = 命令 + ETX(1字节) + CheckSum(1字节)
- const int maxCmdLen = 256; // 增大缓冲区以支持更长的命令
- if (cmdLen > maxCmdLen - 2)
- {
- FERROR("Command too long: {$} bytes, max allowed: {$} bytes\n", cmdLen, maxCmdLen - 2);
- return RET_STATUS::RET_FAILED;
- }
- char strSendCommand[maxCmdLen] = { 0 };
- // 计算校验和
- int tmpSum = 0;
- for (int i = 0; i < cmdLen; i++)
- {
- tmpSum += (unsigned char)strCommand[i];
- }
- char checkSum = char(tmpSum + 3);
- // 构建数据包:命令 + ETX + CheckSum
- memcpy(strSendCommand, strCommand, cmdLen);
- strSendCommand[cmdLen] = 0x03;
- strSendCommand[cmdLen + 1] = checkSum;
- int totalLen = cmdLen + 2; // 实际发送的总长度
- // 打印完整数据包的十六进制和ASCII(含ETX和CheckSum)便于调试
- FINFO("==OUT== CMD={$} LEN={$}", strCommand, totalLen);
- // 使用 SCFWrapper 发送数据
- unsigned int retLength = 0;
- if (m_SCF->Lock(1000) == WAIT_OBJECT_0)
- {
- int result = m_SCF->SendPacket(strSendCommand, (unsigned int)totalLen, (unsigned int)nTimeOut, retLength);
- m_SCF->Unlock();
- if (result != SCF_SUCCEED)
- {
- FERROR("SendPacket failed, result={$}", result);
- return RET_STATUS::RET_FAILED;
- }
- }
- else
- {
- FERROR("Failed to lock SCF");
- return RET_STATUS::RET_FAILED;
- }
- Sleep(nTimeOut);
- return RET_STATUS::RET_SUCCEED;
- }
- //-----------------------------------------------------------------------------
- // ProcessCmd
- //-----------------------------------------------------------------------------
- void nsGEN::CPIDevice::FireNotify(std::string key, std::string content)
- {
- EventCenter->OnNotify(1, key, content);
- }
- struct tFrameMapping
- {
- static const int MaxLen = 5; // ǰ���ܳ����� 5 ���ַ� !
- using cbFun = std::function <void(const char*, int)>;
- char strHead[MaxLen];
- int NbOfCharOfHead;
- cbFun fun;
- tFrameMapping(const char* str, int len, cbFun f)
- {
- assert(len < MaxLen); //len���ֻ����4
- //strHead[0] = 0x02; //STX
- //for (int i = 0; i < len; i++) //��strHead��ֵ
- // strHead[i + 1] = str[i];
- //NbOfCharOfHead = len + 1;
- //fun = f;
- //assert(len < MaxLen);
- for (int i = 0; i < len; i++)
- strHead[i] = str[i];
- NbOfCharOfHead = len;
- fun = f;
- }
- };
- static std::list <tFrameMapping> arFrame;
- static bool DecodeFrame(const char* strFrame, int length);
- //KV075 MA05000 MS00800 MX00400
- // ==IN==:KV075 MA05000 MS00800 MX00400
- void nsGEN::CPIDevice::OnCallBack()
- {
- auto HWNotProcess = [](const char* value, int length) -> void
- {
- FINFO("\n This commands didn't need to process!\n");
- };
- auto HWKV = [this](const char* value, int length) -> void
- {
- assert(value);
- if (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;
- //if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
- //{
- // //if (m_DoseUnit.m_MAS->Update(0))
- // // FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
- //}
- //else
- //{
- // if (m_DoseUnit.m_MAS->Update(fmas))
- // FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
- //}
- if (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;
- if (m_DoseUnit.m_MA->Update(fma))
- FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet());
- };
- auto HWMS = [this](const char* value, int length)
- {
- assert(value);
- if (m_DoseUnit.m_MS->Update(atof(value) / 10.0))
- FireNotify(AttrKey::MS, m_DoseUnit.m_MS->JSGet());
- };
- auto HWFocus = [this](const char* value, int length)
- {
- assert(value);
- if (m_DoseUnit.m_Focus->Update(atoi(value)))
- FireNotify(AttrKey::FOCUS, m_DoseUnit.m_Focus->JSGet());
- };
- auto HWTechmode = [this](const char* value, int length)
- {
- assert(value);
- if (m_DoseUnit.m_Techmode->Update(atoi(value)))
- FireNotify(AttrKey::TECHMODE, m_DoseUnit.m_Techmode->JSGet());
- };
- auto HWAECField = [this](const char* value, int length)
- {
- assert(value);
- int nvalue = atoi(value);
- if (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);
- //ysj++:理论上 此处得到WS的值,需要转为对应的数字,转换后的数字,能给UI一致,从而得出正确的ws并显示出来。
- 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)
- {
- m_bInExpState = true;
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_READY);
- m_hGenPostEvent->ResetEvent();
- HWSend("PR2");
- }
- else if (nValue == 1)
- {
- m_bInExpState = true;
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_PREPARE);
- HWSend("PR1");
- }
- else if (nValue == 0)
- {
- m_bInExpState = false;;
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF);
- HWSend("PR0");
- }
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- };
- auto HWXR = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 1)
- {
- if ((this->m_nCtlMode == 2) && m_bExpEnable)
- {
- if (EXPOSURE_SOFTWARE_NOSYNBOX == (int)m_GenConfig["SynMode"] || EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN == (int)m_nCtlMode["SynMode"])
- {
- FINFO("recv XR1 ,in nosynbox or in derect mode ,do nothing,and wait invoke be call,then send XR1,and send XRAYON msg.");
- //m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON);// ysj++ 我们认为此处不应该发这个消息,准确的时机 应该是invoke被调用的时候,
- }
- if (EXPOSURE_SOFTWARE_NOSYNBOX != (int)m_GenConfig["SynMode"])
- {
- FINFO("recv XR1 ,in hardware or in derect mode ,so response XR1.");
- HWSend("XR1");
- }
- }
- else
- {
- if (2 != this->m_nCtlMode)
- {
- if (EXPOSURE_SOFTWARE_NOSYNBOX == (int)m_GenConfig["SynMode"] || EXPOSURE_NOSYNBOX_DIRCETCONNECT_DETECTOR_GEN == (int)m_nCtlMode["SynMode"])
- {
- //::SendMessage(this->m_hWnd, MSG_SYN_STATUS, SYN_GEN_EIPON, 1); //------------------------------------------------ò��û����ͬ������Ϣ
- FINFO("recv XR1 : ctrlmode !=2, in No SYNbox or in directly mode, Send Msg SYN_GEN_EIPON");
- }
- }
- }
- }
- else if (nValue == 0)
- {
-
- if (m_nCtlMode == 2)
- {
- HWSend("XR0");
- }
- HWSend("DA");
- HWSend("AP?");
- HWSend("AT?");
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF);
- }
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- };
- auto HWAPDOSE = [this](const char* value, int length)//post mas
- {
- assert(value);
- if(m_DoseUnit.m_PostMAS->Update(atof(value) / 10.0))
- FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet());
- //m_hGenPostEvent->SetEvent();
- };
- auto HWKW = [this](const char* value, int length)
- {
- assert(value);
- FINFO("Recv KW={$}", atoi(value));
- };
- auto HWATDOSE = [this](const char* value, int length)
- {
- assert(value);
- if(m_DoseUnit.m_PostMS->Update(atof(value)/10.0))
- FireNotify(m_DoseUnit.m_PostMS->GetKey(), m_DoseUnit.m_PostMS->JSGet());
- //m_hGenPostEvent->SetEvent();
- };
- auto HWDAP = [this](const char* value, int length)
- {
- assert(value);
- FINFO("Recv DAP ={$}", atof(value) / 100.0);
- //ysj++:理论上此处应该调用DAP设备。
- if (m_pDapDev)
- {
- m_pDapDev->UpdateMammo_DAP(atof(value) / 100.0);
- }
- };
- auto HWERROR = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue != 0)
- {
- char ErrorCode[10]{ "" };
- snprintf(ErrorCode, sizeof(ErrorCode), "CPI_ERR_%d", nValue);
- int level = 1;
- m_MSGUnit->AddErrorMessage(ErrorCode, level, "");
- //去配置文件中找error对应的信息
- }
- else
- {
- int level = 1;
- char ErrorCode[10]{ "" };
- m_MSGUnit->DelErrorMessage(ErrorCode, level, "");
- }
- };
- auto HWWARN = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue != 0)
- {
- char WarnCode[20]{ "" };
- snprintf(WarnCode, sizeof(WarnCode), "CPI_WARN_%d", nValue);
- int level = 1;
- m_MSGUnit->AddWarnMessage(WarnCode, level, "");
- }
- else
- {
- int level = 1;
- char WarnCode[10]{ "" };
- m_MSGUnit->AddWarnMessage(WarnCode, level, "");
- }
- };
- auto HWEHE = [this](const char* value, int length)
- {
- assert(value);
- if(m_DoseUnit.m_HE->Update(atoi(value)))
- FireNotify(m_DoseUnit.m_HE->GetKey(), m_DoseUnit.m_HE->JSGet());
- };
- auto HWFLK = [this](const char* value, int length)
- {
- assert(value);
- if (m_DoseUnit.m_KV->Update(atof(value)))
- FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet());
- };
- auto HWFLM = [this](const char* value, int length)
- {
- assert(value);
- if (m_DoseUnit.m_MA->Update(atof(value)/10.0))
- FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet());
- };
- auto HWFLS = [this](const char* value, int length)
- {
- assert(value);
- if (m_DoseUnit.m_FrameRate->Update(atof(value)/10.0))
- FireNotify(AttrKey::FRAMERATE, m_DoseUnit.m_FrameRate->JSGet());
- };
- auto HWFLX = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 1)
- {
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYON);
- }
- else if (nValue == 0)
- {
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYOFF);
- }
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- };
- auto HWFLP = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 1)
- {
- //PRINTA_INFO("\n recv FLP1\n");
- }
- else if (nValue == 4)
- {
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_READY);
- }
- else if (nValue == 0)
- {
- m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_OFF);
- }
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- };
- auto HWACH = [this](const char* value, int length)
- {
- assert(value);
- if (m_pCollDev)
- {
- m_pCollDev->UpdateCollimatorXSize(atoi(value) / 100.0);
- }
-
- };
- auto HWACW = [this](const char* value, int length)
- {
- assert(value);
- if (m_pCollDev)
- {
- m_pCollDev->UpdateCollimatorYSize(atoi(value) / 100.0);
- }
- };
- auto HWFIL = [this](const char* value, int length)
- {
- assert(value);
- if (m_pCollDev)
- {
- m_pCollDev->SetCollimatorFilter(atoi(value));
- }
- };
- auto HWATU = [this](const char* value, int length)
- {
- assert(value);
- if (1 == atoi(value))
- {
- m_bCheckATUStatus = false;//not need continue check status
- }
- else if (0 == atoi(value))
- {
- m_bCheckATUStatus = true;
- }
- };
- auto HWDS = [this](const char* value, int length)
- {
- assert(value);
- m_bDAPEnable = (bool)atoi(value);
- };
- arFrame.clear();
- 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("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("AP", 2, HWAPDOSE));
- arFrame.push_back(tFrameMapping("AT", 2, HWATDOSE));
- arFrame.push_back(tFrameMapping("EL", 2, HWERROR));
- arFrame.push_back(tFrameMapping("ER", 2, HWWARN));
- arFrame.push_back(tFrameMapping("HE", 2, HWEHE));
- 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("ACH", 3, HWACH));
- arFrame.push_back(tFrameMapping("ACW", 3, HWACW));
- arFrame.push_back(tFrameMapping("FIL", 3, HWFIL));
- arFrame.push_back(tFrameMapping("ATU", 3, HWATU));
- }
- bool nsGEN::CPIDevice::StartHardwareStatusThread()
- {
- FINFO("enter Start HardwareStatus Thread ");
- try {
- if (!m_pHardwareStatusThread.joinable()) {
- m_pHardwareStatusThread = std::thread(HardwareStatusThread, this);
- FINFO("Hardware status thread started successfully");
- return true;
- } else {
- FINFO("Hardware status thread already running");
- return true;
- }
- }
- catch (const std::exception& e) {
- FERROR("Start HardwareStatus Thread Failed: {$}", e.what());
- return false;
- }
- }
- void nsGEN::CPIDevice::HardwareStatusThread(CPIDevice* pParam)
- {
- if (pParam == NULL)
- {
- FERROR("HardwareStatusThread: Invalid parameter");
- return;
- }
- FINFO("HardwareStatusThread start");
- int cnt = 0;
- while (pParam->m_bExtraFlag)
- {
- Sleep(2000);
- if (!pParam->m_bExtraFlag) {
- break;
- }
- pParam->HWSend("HE?");
- if (pParam->m_bCheckATUStatus)
- {
- cnt++;
- if (cnt == 10)
- {
- pParam->HWSend("ATU?");
- cnt = 0;
- }
- }
- else
- {
- cnt = 0;
- }
- }
- FINFO("HardwareStatusThread stop");
- }
- //-----------------------------------------------------------------------------
- // CPIDriver
- //-----------------------------------------------------------------------------
- nsGEN::CPIDriver::CPIDriver()
- {
- m_bDemoConnected = false;
- m_pDriGenDev = nullptr;
- m_pDriCollDev = nullptr;
- m_pDriDapDev = nullptr;
- m_pAttribute.reset(new ResDataObject());
- m_pDescription.reset(new ResDataObject());
- }
- nsGEN::CPIDriver::~CPIDriver()
- {
- }
- auto nsGEN::CPIDriver::CreateDevice(int index) -> std::unique_ptr<IODevice>
- {
- if (index == 0)
- {
- m_pDriGenDev = new CPIDevice(EventCenter, m_scfWrapper, m_ConfigFileName);
- auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriGenDev));
- return dev;
- }
- else if (index == 1)
- {
- FINFO("Enter CreateDevice COLL");
- m_pDriCollDev = new OemCollimator(std::shared_ptr<IOEventCenter>(new IOEventCenter()));
- auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriCollDev));
- m_pDriCollDev->SetCtrlDev(m_pDriGenDev);
- m_pDriGenDev->SetCollimatorDev(m_pDriCollDev);
- FINFO("Leave CreateDevice COLL");
- return dev;
- }
- // else if (index == 2) //------------------------------因为机械部分没有可获取的信息,所以此处屏蔽机械设备的创建
- // {
- // FINFO("Enter CreateDevice Mech");
- // m_pDriMechDev = new OemMechanical(std::shared_ptr<IOEventCenter>(new IOEventCenter()));
- // auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriMechDev));
- // m_pDriMechDev->SetCtrlDev(m_pDriGenDev);
- // m_pDriGenDev->SetMechDev(m_pDriMechDev);
- // FINFO("Leave CreateDevice Mech");
- // return dev;
- // }
- else if (index == 2) //----------------------------------可能DAP设备也是如此
- {
- FINFO("Enter CreateDevice DAP");
- m_pDriDapDev = new OemDap(std::shared_ptr<IOEventCenter>(new IOEventCenter()));
- auto dev = std::unique_ptr<IODevice>(new IODevice(m_pDriDapDev));
- m_pDriDapDev->SetCtrlDev(m_pDriGenDev);
- m_pDriGenDev->SetDapDev(m_pDriDapDev);
- FINFO("Leave CreateDevice DAP");
- return dev;
- }
- // 处理所有未匹配的index,确保函数有返回值
- return nullptr;
- }
- void nsGEN::CPIDriver::FireNotify(int code, std::string key, std::string content)
- {
- EventCenter->OnNotify(code, key, content);
- }
- void nsGEN::CPIDriver::Prepare()
- {
- FINFO("========================================");
- FINFO("CPIDriver::Prepare() Starting...");
- FINFO("========================================");
- // 初始化日志系统
- std::string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
- std::string LogHost = "DevCPI";
- std::string moduleName = "DevCPI";
- FINFO("Initializing log module...");
- FINFO("Log config path: {$}", strLogPath.c_str());
- FINFO("Log host: {$}", LogHost.c_str());
- FINFO("Module name: {$}", moduleName.c_str());
- bool ret = initLogModule(
- LogHost, // 主机名(用于日志路径中的{host}占位符)
- moduleName, // 唯一模块名
- strLogPath, // 配置文件路径
- true // 是否输出到控制台(可选)
- );
- if (!ret) {
- std::cerr << "Log init failed!" << std::endl;
- FERROR("Failed to initialize log module");
- return;
- }
- FINFO("Log module initialized successfully");
- CPI_SetLocalModuleName(moduleName);
- FINFO("Getting connection DLL from config: {$}", m_ConfigFileName.c_str());
- m_SCFDllName = GetConnectDLL(m_ConfigFileName);
- FINFO("SCF DLL name: {$}", m_SCFDllName.c_str());
- FINFO("Calling parent Prepare()...");
- super::Prepare();
- FINFO("========================================");
- FINFO("CPIDriver::Prepare() Completed");
- FINFO("========================================");
- }
- bool DATA_ACTION nsGEN::CPIDriver::Connect()
- {
- std::lock_guard<std::mutex> lock(m_connectionMutex);
- const auto currentState = m_connectionState.load();
- auto now = std::chrono::steady_clock::now();
- // 1. 处理可重试的失败状态
- if (currentState == ConnectionState::Failed) {
- if ((now - m_lastConnectionAttempt) >= RETRY_INTERVAL && m_connectionRetryCount < MAX_RETRY_COUNT) {
- m_connectionState = ConnectionState::Disconnected;
- }
- else {
- return false; // 不满足重试条件,直接返回
- }
- }
- // 2. 检查无效状态(正在连接/已连接但实际有效)
- if (currentState == ConnectionState::Connecting) {
- FINFO("Already connecting (type: {$})",
- m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
- return true;
- }
- if (currentState == ConnectionState::Connected && m_scfWrapper && m_scfWrapper->IsConnected()) {
- FINFO("Already connected (type: {$})",
- m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
- return true;
- }
- // 3. 检查重试间隔
- if (m_connectionRetryCount > 0 && (now - m_lastConnectionAttempt) < RETRY_INTERVAL) {
- FINFO("Retry in {$}s (type: {$})",
- std::chrono::duration_cast<std::chrono::seconds>(RETRY_INTERVAL - (now - m_lastConnectionAttempt)).count(),
- m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
- return false;
- }
- ResDataObject connParam = GetConnectParam(m_ConfigFileName);
- std::string connPortStr = "";
- std::string connTypeStr = (std::string)connParam["type"]; // 从配置读取type字段
- m_currentConnType = (connTypeStr == "COM") ? ConnectionType::Serial : ConnectionType::Ethernet;
- if (m_currentConnType == ConnectionType::Serial)
- {
- connPortStr = (std::string)connParam["port"];// 从配置读取port字段
- // 查找配置端口在现有端口列表中的位置
- auto it = std::find(m_serialPorts.begin(), m_serialPorts.end(), connPortStr);
- if (it == m_serialPorts.end()) {
- // 配置的端口不在列表中,添加到首位
- FINFO("Configured serial port {$} not found, adding to front of port list", connPortStr);
- m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
- }
- else if (it != m_serialPorts.begin()) {
- // 配置的端口存在但不在首位,移动到首位
- FINFO("Moving configured serial port {$} to front of port list", connPortStr);
- m_serialPorts.erase(it);
- m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
- }
- }
- // 4. 执行连接流程
- m_connectionState = ConnectionState::Connecting;
- m_lastConnectionAttempt = now;
- std::string connInfo;
- try {
- if (m_currentConnType == ConnectionType::Serial) {
- // 串口连接:使用当前索引的端口
- std::string currentPort = m_serialPorts[m_currentSerialPortIndex];
- connParam.update("port", currentPort.c_str()); // 将当前尝试的端口写入参数
- connInfo = "Serial (port: " + currentPort + ")";
- }
- else {
- // 网口连接:直接使用配置参数
- connInfo = "Ethernet (ip: " + std::string(connParam["ip"]) + ")";
- }
- FINFO("Enter Connect ({$}), config: {$}", connInfo, connParam.encode());
- if (!m_scfWrapper->Initialize(m_SCFDllName)) {
- FINFO("Init failed: {$}", m_scfWrapper->GetLastError());
- m_connectionState = ConnectionState::Failed;
- return false;
- }
- m_scfWrapper->SetDataReceivedCallback([this](const char* data, uint32_t length) {
- this->Dequeue(data, length);
- });
- auto erCode = m_scfWrapper->Connect(connParam, &CPIDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
- if (erCode != SCF_SUCCEED || !m_scfWrapper->StartAutoReceive()) {
- FINFO("Connect failed (code: {$}) for {$}", erCode, connInfo);
- m_scfWrapper->Disconnect();
- m_connectionState = ConnectionState::Failed;
- // 串口连接:未遍历完所有端口时,优先切换端口重试(不计入总重试次数)
- if (m_currentConnType == ConnectionType::Serial) {
- int nextIndex = (m_currentSerialPortIndex + 1) % m_serialPorts.size();
- // 判断是否已遍历所有端口(当前索引是最后一个时,nextIndex会回到0)
- bool allPortsTried = (nextIndex == 0);
- if (!allPortsTried) {
- // 未遍历完所有端口:切换到下一端口,不增加重试计数
- m_currentSerialPortIndex = nextIndex;
- m_connectionRetryCount = 0;
- FINFO("Trying next serial port: {$}", m_serialPorts[nextIndex]);
- return false; // 触发外部线程立即尝试下一端口
- }
- else {
- // 已遍历所有端口:重置到第一个端口,增加重试计数(进入间隔等待)
- m_currentSerialPortIndex = 0;
- m_connectionRetryCount++;
- FINFO("All serial ports tried, retry count: {$}/{$}", m_connectionRetryCount, MAX_RETRY_COUNT);
- return false;
- }
- }
- // 所有端口失败(串口)或网口失败,才增加总重试计数
- m_connectionRetryCount++;
- return false;
- }
- // 连接成功:重置状态
- m_connectionState = ConnectionState::Connected;
- m_connectionRetryCount = 0;
- m_currentSerialPortIndex = 0; // 重置串口端口索引
- FINFO("Connected successfully ({$})", connInfo);
- return true;
- }
- catch (const std::exception& e) {
- FINFO("Exception for {$}: {$}", connInfo, e.what());
- m_connectionState = ConnectionState::Failed;
- m_connectionRetryCount++;
- return false;
- }
- }
- void nsGEN::CPIDriver::Disconnect()
- {
- FINFO("========================================");
- FINFO("CPIDriver::Disconnect() Entry");
- if (m_scfWrapper) {
- FINFO("Stopping auto receive...");
- m_scfWrapper->StopAutoReceive();
- FINFO("Auto receive stopped");
- FINFO("Disconnecting SCF wrapper...");
- m_scfWrapper->Disconnect();
- FINFO("SCF wrapper disconnected");
- } else {
- FWARN("SCF wrapper is null, nothing to disconnect");
- }
- FINFO("CPIDriver::Disconnect() Completed");
- FINFO("========================================");
- }
- bool nsGEN::CPIDriver::isConnected() const
- {
- const auto state = m_connectionState.load();
- // 1. 连接中/实际已连接:返回true(无需重连)
- if (state == ConnectionState::Connecting || (m_scfWrapper && m_scfWrapper->IsConnected())) {
- //FINFO(state == ConnectionState::Connecting ? "Connecting in progress" : "Already connected");
- return true;
- }
- // 2. 失败状态处理:判断是否允许重试
- if (state == ConnectionState::Failed) {
- auto now = std::chrono::steady_clock::now();
- const auto timeSinceLast = now - m_lastConnectionAttempt;
- // 2.1 达到最大重试次数,但超过重置间隔:重置计数,允许重新重试
- if (m_connectionRetryCount >= MAX_RETRY_COUNT && timeSinceLast >= RESET_RETRY_AFTER) {
- FINFO("Max retries reached, resetting count after {$}s",
- std::chrono::duration_cast<std::chrono::seconds>(timeSinceLast).count());
- m_connectionRetryCount = 0; // 重置计数(因mutable修饰,const函数可修改)
- }
- // 2.2 不适合重连(时间未到 或 次数仍超限):返回true阻止重连
- if (timeSinceLast < RETRY_INTERVAL || m_connectionRetryCount >= MAX_RETRY_COUNT) {
- FINFO(timeSinceLast < RETRY_INTERVAL ?
- "Retry later ({$}s)" : "Max retries, waiting {$}s to reset",
- std::chrono::duration_cast<std::chrono::seconds>(
- timeSinceLast < RETRY_INTERVAL ? RETRY_INTERVAL - timeSinceLast : RESET_RETRY_AFTER - timeSinceLast
- ).count()
- );
- return true;
- }
- }
- // 3. 其他情况(适合重连):返回false触发Connect()
- return false;
- }
- std::string nsGEN::CPIDriver::DriverProbe()
- {
- 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", "CPI");
- HardwareInfo.add("ProductID", "HF");
- HardwareInfo.add("SerialID", "Drv");
- }
- string ret = HardwareInfo.encode();
- return ret;
- }
- std::string nsGEN::CPIDriver::DeviceProbe()
- {
- 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", "CPI");
- HardwareInfo.add("ProductID", "HF");
- HardwareInfo.add("SerialID", "Drv");
- }
- string ret = HardwareInfo.encode();
- return ret;
- }
- bool nsGEN::CPIDriver::GetDeviceConfig(std::string& Cfg)
- {
- Cfg = m_DeviceConfigSend.encode();
- //printf("GetDeviceConfig over");
- printf("GetDeviceConfig over , %s", Cfg.c_str());
- return true;
- }
- bool nsGEN::CPIDriver::SetDeviceConfig(std::string Cfg)
- {
- FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
- ResDataObject DeviceConfig;
- DeviceConfig.decode(Cfg.c_str());
- ResDataObject DescriptionTempEx;
- DescriptionTempEx = DeviceConfig["DeviceConfig"]["Attribute"];
- FDEBUG("Attribute:{$}", DescriptionTempEx.encode());
- bool bSaveFile = false;
- string strAccess = "";
- for (int i = 0; i < DescriptionTempEx.size(); i++)
- {
- string strKey = DescriptionTempEx.GetKey(i);
- FINFO("{$}", strKey.c_str());
- try
- {
- if (m_pAttribute->GetFirstOf(strKey.c_str()) >= 0)
- {
- strAccess = (string)(*m_pDescription)[strKey.c_str()]["Access"];
- if ("RW" == strAccess)
- {
- (*m_pAttribute)[strKey.c_str()] = DescriptionTempEx[i];
- int nConfigInfoCount = (int)m_Configurations["ConfigToolInfo"].GetKeyCount("AttributeInfo");
- FINFO("nConfigInfoCount {$}", nConfigInfoCount);
- string strTemp = "";
- 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;
- }
- }
- if (SetDeviceConfigValue(m_Configurations, strTemp.c_str(), 1, DescriptionTempEx[i]))
- {
- FDEBUG("SetDeviceConfigValue over");
- bSaveFile = true;
- }
- }
- else
- {
- FINFO("{$} is not a RW configuration item", strKey.c_str());
- }
- }
- else
- {
- FINFO("without this attribute {$}", strKey.c_str());
- }
- }
- catch (ResDataObjectExption& e)
- {
- FERROR("SetDriverConfig crashed: {$}", e.what());
- return false;
- }
- }
- if (bSaveFile)
- {
- SaveConfigFile(true);
- }
- return true;
- }
- bool nsGEN::CPIDriver::SaveConfigFile(bool bSendNotify)
- {
- m_ConfigAll["CONFIGURATION"] = m_Configurations;
- bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
- FINFO("SaveConfigFile over {$}", bRt);
- return true;
- }
- bool nsGEN::CPIDriver::GetDeviceConfigValue(ResDataObject config, const char* pInnerKey, int nPathID, string& strValue)
- {
- strValue = "";
- string strTemp = pInnerKey;
- if (1 == nPathID)
- {
- 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::CPIDriver::SetDeviceConfigValue(ResDataObject& config, const char* pInnerKey, int nPathID, const char* szValue)
- {
- string strTemp = pInnerKey;
- FDEBUG("Begin to change {$} item value to {$}", 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;
- //if ((strTemp.compare("WSTable") == 0) ||
- // (strTemp.compare("WSWall") == 0) ||
- // (strTemp.compare("WSFree") == 0) ||
- // (strTemp.compare("WSTomo") == 0) ||
- // (strTemp.compare("WSConventional") == 0)
- // )
- //{
- // int sum = (*szValue) + 1;
- // (*resTemp)[strTemp.c_str()] = (char*)(&sum);
- //}
- //else
- // (*resTemp)[strTemp.c_str()] = szValue;
- }
- else
- {
- *resTemp = szValue;
- }
- }
- catch (ResDataObjectExption& e)
- {
- FERROR("SetDriverConfigvalue crashed: {$}", e.what());
- return false;
- }
- }
- return true;
- }
- std::string nsGEN::CPIDriver::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 = "";
- int nTemp = -1;
- char sstream[10] = { 0 };
- string strValue = "";
- string strType = "";
- /***
- ***/
- try
- {
- 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
- strTemp = (string)m_Configurations["ConfigToolInfo"][nInfoIndex]["InnerKey"];
- nTemp = (int)m_Configurations["ConfigToolInfo"][nInfoIndex]["PathID"];
- GetDeviceConfigValue(r_config, strTemp.c_str(), nTemp, strValue);
- //printf("********************************innerkey=%s --strValue = %s\n", strTemp.c_str(), strValue.c_str());
- 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
- {
- (*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)
- {
- 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)
- {
- FERROR("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;
- FERROR("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());
- FDEBUG("get resource over {$}", DescriptionTempEx.encode());
- printf("************* get resource over %s \n", DescriptionTempEx.encode());
- return res;
- }
- void nsGEN::CPIDriver::Dequeue(const char* Packet, DWORD Length)
- {
- DecodeFrame(Packet, Length);
- }
- PACKET_RET nsGEN::CPIDriver::callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength)
- {
- if (nLength < 1)
- {
- printf("nLength < 1, nLength==%d \n", nLength);
- FINFO("nLength < 1, nLength=={$} \n", nLength);
- return PACKET_USELESS;
- }
- for (uint32_t 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);
- //for test
- //for (int j = 0; j < nLength; j++)
- //{
- // printf("%2X,", RecData[j]);
- //}
- //printf("\n");
- 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;
- printf("==IN==:%s\n", strtemp);
- FINFO("==IN==:{$}\n", strtemp);
- return PACKET_ISPACKET;
- }
- }
- return PACKET_NOPACKET;
- }
- //-----------------------------------------------------------------------------
- // DecodeFrame
- //-----------------------------------------------------------------------------
- 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);
- if (found == arFrame.end())
- {
- return false;
- }
- 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;
- }
- //-----------------------------------------------------------------------------
- // GetIODriver & CreateIODriver
- //-----------------------------------------------------------------------------
- static nsGEN::CPIDriver gIODriver;
- extern "C" CCOS::Dev::IODriver * GetIODriver()
- {
- return &gIODriver;
- }
- extern "C" CCOS::Dev::IODriver * CreateIODriver()
- {
- return new nsGEN::CPIDriver();
- }
|