CCOS.Dev.Generator.HaoWei.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. // CCOS.Dev.GEN.HaoWei.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include <assert.h>
  4. #include <functional>
  5. #include <unordered_map>
  6. #include <fstream>
  7. #include <filesystem>
  8. #include <set>
  9. #include "LogicDevice.h"
  10. #include "Helper.JSON.hpp"
  11. #include "LogLocalHelper.h"
  12. #include "Log4CPP.h"
  13. #include "CCOS.Dev.Generator.HaoWei.h"
  14. using namespace std::placeholders;
  15. using namespace CCOS::Dev::Detail::Generator;
  16. namespace nsGEN = CCOS::Dev::Detail::Generator;
  17. static const int msTimeOut_Lock = 500;
  18. static const auto COM_SCFDllName = "libSerialSCF.so";
  19. static const auto TCP_SCFDllName = "libTcpipSCF.so";
  20. #define Sleep(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms))
  21. //-----------------------------------------------------------------------------
  22. // HaoWeiDevice
  23. //-----------------------------------------------------------------------------
  24. // 储存的点值
  25. std::vector<int> R10_MA = { 100, 125, 160, 200, 250, 320, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6300, 8000, 10000 };
  26. std::vector<int> R10_MS = { 10, 12, 16, 20, 25, 32, 40, 50, 63, 80, 100, 125, 160, 200, 250, 320, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000, 25000, 32000, 40000, 50000, 63000, 80000, 100000 };
  27. std::vector<int> R10_MAS = { 10, 12, 16, 20, 25, 32, 40, 50, 63, 80, 100, 125, 160, 200, 250, 320, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000, 25000, 32000, 40000, 50000, 63000, 80000, 100000 };
  28. std::vector<int> R20_MA = { 100, 110, 125, 140, 160, 180, 200, 220, 250, 280, 320, 360, 400, 450, 500, 560, 630, 710, 800, 900, 1000, 1100, 1250, 1400, 1600, 1800, 2000, 2200, 2500, 2800, 3200, 3600, 4000, 4500, 5000, 5600, 6300, 7100, 8000, 9000, 10000 };
  29. std::vector<int> R20_MS = { 10, 11, 12, 14, 16, 18, 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 63, 71, 80, 90, 100, 110, 125, 140, 160, 180, 200, 220, 250, 280, 320, 360, 400, 450, 500, 560, 630, 710, 800, 900, 1000, 1100, 1250, 1400, 1600, 1800, 2000, 2200, 2500, 2800, 3200, 3600, 4000, 4500, 5000, 5600, 6300, 7100, 8000, 9000, 10000, 11000, 12500, 14000, 16000, 18000, 20000, 22000, 25000, 28000, 32000, 36000, 40000, 45000, 50000, 56000, 63000, 71000, 80000, 90000, 100000 };
  30. std::vector<int> R20_MAS = { 10, 11, 12, 14, 16, 18, 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 63, 71, 80, 90, 100, 110, 125, 140, 160, 180, 200, 220, 250, 280, 320, 360, 400, 450, 500, 560, 630, 710, 800, 900, 1000, 1100, 1250, 1400, 1600, 1800, 2000, 2200, 2500, 2800, 3200, 3600, 4000, 4500, 5000, 5600, 6300, 7100, 8000, 9000, 10000, 11000, 12500, 14000, 16000, 18000, 20000, 22000, 25000, 28000, 32000, 36000, 40000, 45000, 50000, 56000, 63000, 71000, 80000, 90000, 100000 };
  31. atomic<int> nsGEN::HaoWeiDevice::m_iLoopTime = HaoWei_LoopDefTime;
  32. std::atomic<std::chrono::steady_clock::time_point> lastValidResponse;
  33. constexpr auto TIMEOUT = std::chrono::seconds(12); // 超时阈值
  34. nsGEN::HaoWeiDevice::HaoWeiDevice(std::shared_ptr <IOEventCenter> center, std::shared_ptr<SCFWrapper> SCF, string configfile)
  35. : super(center)
  36. , superGen()
  37. , m_SCF(SCF)
  38. {
  39. assert(EventCenter);
  40. m_bExtraFlag = true;
  41. m_bMasR20 = 0;
  42. m_bUseEAcmd = 0;
  43. m_bResetActive = false;
  44. m_bIsConfigLoaded = false;
  45. m_bHasInitializedDevice = false;
  46. lastValidResponse = std::chrono::steady_clock::now();
  47. m_DoseUnit.m_KV.reset(new KVMould(0.0, 40.0, 125.0, 1.0));
  48. m_DoseUnit.m_MA.reset(new MAMould(0.0, 10.0, 1000.0, 0.1));
  49. m_DoseUnit.m_MS.reset(new MSMould(0.0, 1.0, 6300.0, 0.01));
  50. m_DoseUnit.m_MAS.reset(new MASMould(0.0, 0.1, 1000.0, 0.01));
  51. m_DoseUnit.m_Techmode.reset(new TECHMODEMould(0, 0, 2, 1));
  52. m_DoseUnit.m_WS.reset(new WORKSTATIONMould(1, 0, 5, 1));
  53. m_DoseUnit.m_Focus.reset(new FOCUSMould(1, 0, 1, 1));
  54. m_DoseUnit.m_AECField.reset(new AECFIELDMould(0, 0, 111, 1));
  55. m_DoseUnit.m_AECFilm.reset(new AECFILMMould(0, 0, 2, 1));
  56. m_DoseUnit.m_AECDensity.reset(new AECDENSITYMould(0, -4, 4, 1));
  57. m_DoseUnit.m_HE.reset(new TUBEHEATMould(0, 0, 100, 1));
  58. m_DoseUnit.m_PostKV.reset(new POSTKVMould(0.0, 40.0, 120.0, 1.0));
  59. m_DoseUnit.m_PostMA.reset(new POSTMAMould(0.0, 10.0, 1000.0, 0.1));
  60. m_DoseUnit.m_PostMS.reset(new POSTMSMould(0.0, 1.0, 10000.0, 0.01));
  61. m_DoseUnit.m_PostMAS.reset(new POSTMASMould(0.0, 0.5, 1000.0, 0.01));
  62. m_DoseUnit.m_GenSynState.reset(new GENSYNSTATEMould(0, AttrKey::GENERATOR_SYNC_ERR, AttrKey::GENERATOR_SYNC_MAX, 1));
  63. m_DoseUnit.m_GenState.reset(new GENSTATEMould(0, AttrKey::GENERATOR_STATUS_SHUTDOWN, AttrKey::GENERATOR_STATUS_MAX, 1));
  64. m_DoseUnit.m_GenTotalExpNumber.reset(new TOTALEXPNUMMould(0, 0, 9999, 1));
  65. m_DoseUnit.m_GenTotalAcqTimes.reset(new TOTALACQTIMESMould(0, 0, 9999, 1));
  66. m_DoseUnit.m_GenTubeCoolWaitTimes.reset(new TUBECOOLTIMEMould(0, 0, 9999, 1));
  67. m_DoseUnit.m_GenTubeOverLoadNumber.reset(new TUBEOVERLOADNUMMould(0, 0, 9999, 1));
  68. m_DoseUnit.m_GenCurrentExpNumber.reset(new CUREXPNUMMould(0, 0, 9999, 1));
  69. m_DoseUnit.m_ExpMode.reset(new EXPMODEMould(AttrKey::EXPMODE_TYPE::Single));
  70. m_DoseUnit.m_FrameRate.reset(new FRAMERATEMould(0, 0, 16, 1));
  71. m_MSGUnit.reset(new nsDetail::MSGUnit(center, nsGEN::GeneratorUnitType));
  72. m_DAP.reset(new DevDAP::DOSEMould(0.0, 0.0, 1000.0, 0.01));
  73. m_DoseUnit.m_FLMode.reset(new FLUModeMould(AttrKey::GENERATOR_FLUMode::GENERATOR_FLMODE_NOTFLU));
  74. m_DoseUnit.m_FLIntTime.reset(new FLUIntTimeMould(0.0, 0.0, 100.0, 0.1));
  75. m_DoseUnit.m_FLAccTime.reset(new FLAccTimeMould(0.0, 0.0, 999.0, 0.1));
  76. m_DoseUnit.m_FLKV.reset(new FLUKVMould(0, 40, 125, 1));
  77. m_DoseUnit.m_FLMS.reset(new FLUMSMould(10.0, 10.0, 999999.0, 0.01));
  78. m_DoseUnit.m_FLMA.reset(new FLUMAMould(0.5, 0.5, 99.0, 0.1));
  79. m_DoseUnit.m_ABSStatus.reset(new FLUABSStatusMould(0, 0, 2, 1));
  80. m_DoseUnit.m_PPS.reset(new PPSMould(0.5, 0.5, 30, 0.1));
  81. m_DoseUnit.m_DoseLevel.reset(new FLUDoseLevelMould(0, 0, 2, 1));
  82. m_DoseUnit.m_Curve.reset(new FLUCurveMould(0, 0, 3, 1));
  83. string strErrConfig = GetProcessDirectory() + R"(\OEMDrivers\Generator\HaoWei\ErrorWarnInfo.json)";
  84. m_DeviceErrorHandler.reset(new DeviceErrorHandler(center, nsGEN::GeneratorUnitType, strErrConfig));
  85. OnCallBack();
  86. Register();
  87. HWSend("ST?");
  88. StartHardwareStatusThread();
  89. }
  90. nsGEN::HaoWeiDevice::~HaoWeiDevice()
  91. {
  92. m_bExtraFlag = false;
  93. if (m_pHardwareStatusThread.joinable()) {
  94. m_pHardwareStatusThread.join();
  95. }
  96. FINFO("\n===============log end ===================\n");
  97. }
  98. std::string nsGEN::HaoWeiDevice::GetGUID() const
  99. {
  100. FINFO("\n===============GetGUID : {$} ===================\n", GeneratorUnitType);
  101. return GeneratorUnitType;
  102. }
  103. void nsGEN::HaoWeiDevice::Register()
  104. {
  105. auto Disp = m_Dispatch.Lock().As();
  106. superGen::Register(Disp);
  107. superGen::RegisterRAD(Disp);
  108. superGen::RegisterAEC(Disp);
  109. superGen::RegisterExpEnable(Disp);
  110. superGen::RegisterGeneratortoSyncStatus(Disp);
  111. superGen::RegisterFluoro(Disp);
  112. Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
  113. auto fun_Clear_DAP = [this](auto a, auto&)
  114. {
  115. return Clear_DAP();
  116. };
  117. Disp->Action.Push("Clear_DAP", fun_Clear_DAP);
  118. auto fun_GetValue_DAP = [this](auto a, auto& b)
  119. {
  120. float value = 0;
  121. RET_STATUS ret = GetValue_DAP(value);
  122. b = ToJSON(value);
  123. return ret;
  124. };
  125. Disp->Action.Push("GetValue_DAP", fun_GetValue_DAP);
  126. auto fun_StartMove = [this](auto a,auto& b)
  127. {
  128. return StartMove();
  129. };
  130. Disp->Action.Push("StartMove", fun_StartMove);
  131. auto fun_EndMove = [this](auto a, auto& b)
  132. {
  133. return EndMove();
  134. };
  135. Disp->Action.Push("EndMove", fun_EndMove);
  136. }
  137. RET_STATUS nsGEN::HaoWeiDevice::IncKV()
  138. {
  139. FINFO("KV value before calling IncKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str());
  140. /*if (!m_DoseUnit.m_KV->CanInc())
  141. {
  142. m_DeviceErrorHandler->ParseAndReport("ECOM_KVMAX");
  143. return RET_STATUS::RET_SUCCEED;
  144. }*/
  145. return HWSend("KV+");
  146. }
  147. RET_STATUS nsGEN::HaoWeiDevice::DecKV()
  148. {
  149. FINFO("KV value before calling DecKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str());
  150. /*if (!m_DoseUnit.m_KV->CanDec())
  151. {
  152. m_DeviceErrorHandler->ParseAndReport("ECOM_KVMIN");
  153. return RET_STATUS::RET_SUCCEED;
  154. }*/
  155. return HWSend("KV-");
  156. }
  157. RET_STATUS nsGEN::HaoWeiDevice::SetKV(float value)
  158. {
  159. FINFO("KV value before calling SetKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str());
  160. //if (!m_DoseUnit.m_KV->Verify(value)) return RET_STATUS::RET_SUCCEED;
  161. char temp[50] = { 0 };
  162. snprintf(temp, sizeof(temp), "KV%03d", (int)value);
  163. return HWSend(temp);
  164. }
  165. RET_STATUS nsGEN::HaoWeiDevice::IncMA()
  166. {
  167. FINFO("MA value before calling IncMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str());
  168. //if (!m_DoseUnit.m_MA->CanInc()) return RET_STATUS::RET_SUCCEED;
  169. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  170. {
  171. FINFO("\n Techmode is MAS, can't inc MA");
  172. return RET_STATUS::RET_FAILED;
  173. }
  174. return HWSend("MA+");
  175. }
  176. RET_STATUS nsGEN::HaoWeiDevice::DecMA()
  177. {
  178. FINFO("MA value before calling DecMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str());
  179. //if (!m_DoseUnit.m_MA->CanDec()) return RET_STATUS::RET_SUCCEED;
  180. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  181. {
  182. FINFO("\n Techmode is MAS, can't dec MA");
  183. return RET_STATUS::RET_FAILED;
  184. }
  185. return HWSend("MA-");
  186. }
  187. RET_STATUS nsGEN::HaoWeiDevice::SetMA(float value)
  188. {
  189. FINFO("MA value before calling SetMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str());
  190. if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
  191. int index = 0;
  192. if (m_bMasR20)
  193. {
  194. index = FindClosestIndex(R20_MA, static_cast<int>(value));
  195. }
  196. else
  197. {
  198. index = FindClosestIndex(R10_MA, static_cast<int>(value));
  199. }
  200. char temp[50] = { 0 };
  201. snprintf(temp, sizeof(temp), "MA%02d", index + 1); // index+1 to match the original requirement
  202. return HWSend(temp);
  203. }
  204. RET_STATUS nsGEN::HaoWeiDevice::IncMS()
  205. {
  206. FINFO("MS value before calling IncMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str());
  207. /*if (!m_DoseUnit.m_MS->CanInc())
  208. {
  209. int Level = 1;
  210. m_MSGUnit->AddWarnMessage("HaoWei_WARN", Level, "Generator MS Limit");
  211. return RET_STATUS::RET_SUCCEED;
  212. }*/
  213. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  214. {
  215. FINFO("\n Techmode is MAS, can't inc MS");
  216. return RET_STATUS::RET_FAILED;
  217. }
  218. return HWSend("MS+");
  219. }
  220. RET_STATUS nsGEN::HaoWeiDevice::DecMS()
  221. {
  222. FINFO("MS value before calling DecMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str());
  223. //if (!m_DoseUnit.m_MS->CanDec()) return RET_STATUS::RET_SUCCEED;
  224. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  225. {
  226. FINFO("\n Techmode is MAS, can't dec MS");
  227. return RET_STATUS::RET_FAILED;
  228. }
  229. return HWSend("MS-");
  230. }
  231. RET_STATUS nsGEN::HaoWeiDevice::SetMS(float value)
  232. {
  233. FINFO("MS value before calling SetMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str());
  234. //if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
  235. int index = 0;
  236. if (m_bMasR20)
  237. {
  238. index = FindClosestIndex(R20_MS, static_cast<int>(value));
  239. }
  240. else
  241. {
  242. index = FindClosestIndex(R10_MS, static_cast<int>(value));
  243. }
  244. char temp[50] = { 0 };
  245. snprintf(temp, sizeof(temp), "MS%02d", index + 1); // index+1 to match the original requirement
  246. return HWSend(temp);
  247. }
  248. RET_STATUS nsGEN::HaoWeiDevice::IncMAS()
  249. {
  250. FINFO("MAS value before calling IncMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str());
  251. //if (!m_DoseUnit.m_MAS->CanInc()) return RET_STATUS::RET_SUCCEED;
  252. if (!m_DoseUnit.m_MS->CanInc())
  253. {
  254. int Level = 1;
  255. m_MSGUnit->AddWarnMessage("HaoWei_WARN", Level, "Generator MS Limit");
  256. return RET_STATUS::RET_SUCCEED;
  257. }
  258. if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_MAS)
  259. {
  260. FINFO("\n Techmode is not MAS, can't inc MAS");
  261. return RET_STATUS::RET_FAILED;
  262. }
  263. return HWSend("MX+");
  264. }
  265. RET_STATUS nsGEN::HaoWeiDevice::DecMAS()
  266. {
  267. FINFO("MAS value before calling DecMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str());
  268. //if (!m_DoseUnit.m_MAS->CanDec()) return RET_STATUS::RET_SUCCEED;
  269. if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_MAS)
  270. {
  271. FINFO("\n Techmode is not MAS, can't dec MAS");
  272. return RET_STATUS::RET_FAILED;
  273. }
  274. return HWSend("MX-");
  275. }
  276. RET_STATUS nsGEN::HaoWeiDevice::SetMAS(float value)
  277. {
  278. FINFO("MAS value before calling SetMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str());
  279. //if (!m_DoseUnit.m_MAS->Verify(value)) return RET_STATUS::RET_SUCCEED;
  280. int index = 0;
  281. if (m_bMasR20)
  282. {
  283. index = FindClosestIndex(R20_MAS, static_cast<int>(value));
  284. }
  285. else
  286. {
  287. index = FindClosestIndex(R10_MAS, static_cast<int>(value));
  288. }
  289. char temp[50] = { 0 };
  290. snprintf(temp, sizeof(temp), "MX%02d", index + 1); // index+1 to match the original requirement
  291. return HWSend(temp);
  292. }
  293. RET_STATUS nsGEN::HaoWeiDevice::SetTechmode(int value)
  294. {
  295. FINFO("Techmode value before calling SetTechmode: {$}\n", m_DoseUnit.m_Techmode->JSGet().c_str());
  296. char temp[50] = { 0 };
  297. snprintf(temp, sizeof(temp), "TE%01d", (int)value);
  298. return HWSend(temp);
  299. }
  300. RET_STATUS nsGEN::HaoWeiDevice::SetFocus(int value)
  301. {
  302. FINFO("Focus value before calling SetFocus: {$}\n", m_DoseUnit.m_Focus->JSGet().c_str());
  303. char temp[50] = { 0 };
  304. snprintf(temp, sizeof(temp), "FO%01d", (int)value);
  305. return HWSend(temp);
  306. }
  307. RET_STATUS nsGEN::HaoWeiDevice::SetAECDensity(int value)
  308. {
  309. if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_AEC)
  310. return RET_STATUS::RET_FAILED;
  311. char temp[50] = { 0 };
  312. snprintf(temp, sizeof(temp), "FN%01d", (int)(value + 8));
  313. return HWSend(temp);
  314. }
  315. RET_STATUS nsGEN::HaoWeiDevice::SetAECField(int value)
  316. {
  317. if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_AEC)
  318. return RET_STATUS::RET_FAILED;
  319. char temp[50] = { 0 };
  320. snprintf(temp, sizeof(temp), "FI%03d", (int)value);
  321. return HWSend(temp);
  322. }
  323. RET_STATUS nsGEN::HaoWeiDevice::SetAECFilm(int value)
  324. {
  325. if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_AEC)
  326. return RET_STATUS::RET_FAILED;
  327. char temp[50] = { 0 };
  328. snprintf(temp, sizeof(temp), "FS%03d", (int)value);
  329. return HWSend(temp);
  330. }
  331. RET_STATUS nsGEN::HaoWeiDevice::SetWS(const string value)
  332. {
  333. int tempws = 0;
  334. if (value == "Table") tempws = (int)m_GenConfig["WSTable"];
  335. else if (value == "Wall") tempws = (int)m_GenConfig["WSWall"];
  336. else if (value == "Direct") tempws = (int)m_GenConfig["WSConventional"];
  337. else if (value == "Free") tempws = (int)m_GenConfig["WSFree"];
  338. else if (value == "Tomo") tempws = (int)m_GenConfig["WSTomo"];
  339. char temp[50] = { 0 };
  340. snprintf(temp, sizeof(temp), "WS%01d", tempws);
  341. return HWSend(temp);
  342. }
  343. string nsGEN::HaoWeiDevice::WSUI2Gen(int nUIWS)
  344. {
  345. string strWS = "";
  346. try
  347. {
  348. if (nUIWS == AttrKey::GENWS_TYPE::TABLE) //lying: cross mode
  349. {
  350. strWS = m_GenConfig["WSTable"].encode();
  351. }
  352. else if (nUIWS == AttrKey::GENWS_TYPE::WALL) //standing mode
  353. {
  354. strWS = m_GenConfig["WSWall"].encode();
  355. }
  356. else if (nUIWS == AttrKey::GENWS_TYPE::FREE_TABLE) //standing mode
  357. {
  358. strWS = m_GenConfig["WSFree"].encode();
  359. }
  360. else if (nUIWS == AttrKey::GENWS_TYPE::TOMO) //standing mode
  361. {
  362. strWS = m_GenConfig["WSTOMO"].encode();
  363. }
  364. else if (nUIWS == AttrKey::GENWS_TYPE::CONVENTIONAL) //standing mode
  365. {
  366. strWS = m_GenConfig["WSConventional"].encode();
  367. }
  368. }
  369. catch (ResDataObjectExption& exp)
  370. {
  371. FERROR("Get configuration failed, {$}\n", exp.what());
  372. }
  373. FINFO("Set WS: {$},Generator workstaion: {$}\n", nUIWS, strWS);
  374. if (strWS == "")
  375. {
  376. strWS = "Table";
  377. }
  378. return strWS;
  379. }
  380. RET_STATUS nsGEN::HaoWeiDevice::SetAPR(const _tAPRArgs& t)
  381. {
  382. m_t = t;
  383. FINFO("*********************Enter SetAPR*********************");
  384. FINFO("t.ws={$},t.fKV={$},t.fMA={$},t.fMAS={$},t.nAECDensity={$},t.nAECField={$},t.nAECFilm={$},t.nFocus={$},t.nTechmode={$}", t.nWS,t.fKV, t.fMA, t.fMAS, t.nAECDensity, t.nAECField, t.nAECFilm, t.nFocus, t.nTechmode);
  385. //2
  386. SetKV(t.fKV);
  387. //3
  388. SetFocus(t.nFocus);
  389. //4
  390. if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_AEC)//aec
  391. {
  392. SetTechmode(t.nTechmode);
  393. SetAECField(t.nAECField);
  394. SetAECDensity(t.nAECDensity);
  395. SetMA(t.fMA);
  396. SetMS(t.fMS);
  397. }
  398. else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_MAS)//2p
  399. {
  400. SetTechmode(t.nTechmode);
  401. SetMAS(t.fMAS);
  402. }
  403. else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_TIME)//3p
  404. {
  405. SetTechmode(t.nTechmode);
  406. SetMA(t.fMA);
  407. SetMS(t.fMS);
  408. }
  409. HWSend("RS?");
  410. FINFO("*********************Leave SetAPR*********************");
  411. return RET_STATUS::RET_SUCCEED;
  412. }
  413. RET_STATUS nsGEN::HaoWeiDevice::QueryHE(int& value)
  414. {
  415. return RET_STATUS::RET_SUCCEED;
  416. }
  417. RET_STATUS nsGEN::HaoWeiDevice::QueryPostKV(float& value)
  418. {
  419. return RET_STATUS::RET_SUCCEED;
  420. }
  421. RET_STATUS nsGEN::HaoWeiDevice::QueryPostMA(float& value)
  422. {
  423. return RET_STATUS::RET_SUCCEED;
  424. }
  425. RET_STATUS nsGEN::HaoWeiDevice::QueryPostMS(float& value)
  426. {
  427. return RET_STATUS::RET_SUCCEED;
  428. }
  429. RET_STATUS nsGEN::HaoWeiDevice::QueryPostMAS(float& value)
  430. {
  431. value = m_DoseUnit.m_PostMAS->Get();
  432. return RET_STATUS::RET_SUCCEED;
  433. }
  434. RET_STATUS nsGEN::HaoWeiDevice::Clear_DAP()
  435. {
  436. return RET_STATUS::RET_SUCCEED;
  437. }
  438. RET_STATUS nsGEN::HaoWeiDevice::GetValue_DAP(float& value)
  439. {
  440. return RET_STATUS::RET_SUCCEED;
  441. }
  442. RET_STATUS nsGEN::HaoWeiDevice::InitDevice()
  443. {
  444. HWSend("AC0");
  445. HWSend("SF0");
  446. HWSend("RS?");
  447. HWSend("FO?");
  448. GetSoftwareVersion();
  449. HWSend("GC?");
  450. SetPriorityCoefficient(m_bMasR20);
  451. return RET_STATUS::RET_SUCCEED;
  452. }
  453. RET_STATUS nsGEN::HaoWeiDevice::StartMove()
  454. {
  455. FINFO("Enter startMove");
  456. FINFO("end startmove");
  457. return RET_STATUS::RET_SUCCEED;
  458. }
  459. RET_STATUS nsGEN::HaoWeiDevice::EndMove()
  460. {
  461. FINFO("Enter endmove");
  462. FINFO("end EndMove");
  463. return RET_STATUS::RET_SUCCEED;
  464. }
  465. int CCOS::Dev::Detail::Generator::HaoWeiDevice::GetGenState()
  466. {
  467. const int currentState = m_DoseUnit.m_GenState->Get();
  468. if (currentState == 0)
  469. {
  470. m_DeviceErrorHandler->ParseAndReport("ECOM_CommLost");
  471. }
  472. return currentState;
  473. }
  474. int CCOS::Dev::Detail::Generator::HaoWeiDevice::LoadConfig(string configfile)
  475. {
  476. FINFO("=====================LoadConfig=========================");
  477. // 检查文件是否存在
  478. std::ifstream file(configfile);
  479. if (!file) {
  480. // 文件不存在,直接返回空的Connection对象
  481. FINFO("Config file does not exist: {$}", configfile.c_str());
  482. return -1;
  483. }
  484. if (m_bIsConfigLoaded)
  485. {
  486. FINFO("Configuration already loaded.");
  487. return 0;
  488. }
  489. m_strConfigPath = configfile;
  490. ResDataObject temp;
  491. temp.loadFile(m_strConfigPath.c_str());
  492. m_GenConfig = temp["CONFIGURATION"];
  493. TransJsonText(m_GenConfig);
  494. if (m_GenConfig.GetKeyCount("R20Enable") > 0)
  495. {
  496. m_bMasR20 = (bool)m_GenConfig["R20Enable"];
  497. }
  498. HWSend("ST?");
  499. m_bIsConfigLoaded = true;
  500. return 0;
  501. }
  502. int nsGEN::HaoWeiDevice::SetPriorityCoefficient(int nCoefficient)
  503. {
  504. char temp[50] = { 0 };
  505. snprintf(temp, sizeof(temp), "GC%01d", nCoefficient);
  506. return HWSend(temp);
  507. }
  508. int CCOS::Dev::Detail::Generator::HaoWeiDevice::GetSoftwareVersion()
  509. {
  510. FINFO("Enter GetSoftwareVersion...\n");
  511. return HWSend("GR?");
  512. }
  513. int CCOS::Dev::Detail::Generator::HaoWeiDevice::SetPulseSyncMode(int nMode)
  514. {
  515. char temp[50] = { 0 };
  516. snprintf(temp, sizeof(temp), "FMM%01d", nMode);
  517. return HWSend(temp);
  518. }
  519. RET_STATUS nsGEN::HaoWeiDevice::SimulateError(std::string Error)
  520. {
  521. FINFO("Enter SimulateError...{$} \n", Error.c_str());
  522. std::string type = m_DeviceErrorHandler->ParseAndReport(Error);
  523. if (type == "error")
  524. {
  525. FINFO("type == error");
  526. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR))
  527. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  528. }
  529. return RET_STATUS::RET_SUCCEED;
  530. }
  531. int nsGEN::HaoWeiDevice::FindClosestIndex(const std::vector<int>& values, int target)
  532. {
  533. auto it = std::lower_bound(values.begin(), values.end(), target);
  534. if (it == values.end()) {
  535. return values.size() - 1;
  536. }
  537. int index = std::distance(values.begin(), it);
  538. if (index > 0 && std::abs(values[index - 1] - target) <= std::abs(values[index] - target)) {
  539. return index - 1;
  540. }
  541. return index;
  542. }
  543. RET_STATUS nsGEN::HaoWeiDevice::SetGenSynState(int value)
  544. {
  545. FINFO("Enter SetGenSynState:[{$}]", value);
  546. switch (value)
  547. {
  548. case AttrKey::GENERATOR_RAD_OFF:
  549. {
  550. }break;
  551. case AttrKey::GENERATOR_RAD_PREPARE:
  552. {
  553. }break;
  554. case AttrKey::GENERATOR_RAD_READY:
  555. {
  556. }break;
  557. case AttrKey::GENERATOR_RAD_XRAYON:
  558. {
  559. }break;
  560. case AttrKey::GENERATOR_RAD_XRAYOFF:
  561. {
  562. }break;
  563. case AttrKey::GENERATOR_FLU_OFF:
  564. {
  565. }break;
  566. case AttrKey::GENERATOR_FLU_READY:
  567. {
  568. }break;
  569. case AttrKey::GENERATOR_FLU_XRAYON:
  570. {
  571. int fluMode = m_DoseUnit.m_FLMode->Get();
  572. FINFO("SetGenSynState: current FluMode[{$}]", fluMode);
  573. switch (fluMode)
  574. {
  575. case AttrKey::GENERATOR_FLMODE_NOTFLU:
  576. break;
  577. case AttrKey::GENERATOR_FLMODE_CF:
  578. case AttrKey::GENERATOR_FLMODE_HCF:
  579. {
  580. }break;
  581. case AttrKey::GENERATOR_FLMODE_PF:
  582. case AttrKey::GENERATOR_FLMODE_HPF:
  583. {
  584. }break;
  585. break;
  586. case AttrKey::GENERATOR_FLMODE_MAX:
  587. break;
  588. default:
  589. break;
  590. }
  591. }break;
  592. case AttrKey::GENERATOR_FLU_XRAYOFF:
  593. {
  594. }break;
  595. default:
  596. break;
  597. }
  598. return RET_STATUS::RET_SUCCEED;
  599. }
  600. RET_STATUS nsGEN::HaoWeiDevice::SetGenState(int value)
  601. {
  602. return RET_STATUS::RET_SUCCEED;
  603. }
  604. RET_STATUS nsGEN::HaoWeiDevice::SetExpMode(std::string value)
  605. {
  606. FINFO("Enter SetExpMode...{$}",value);
  607. m_DoseUnit.m_ExpMode->Update(value);
  608. if (!m_DeviceErrorHandler->HasActiveErrors())
  609. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  610. else
  611. {
  612. m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR);
  613. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  614. }
  615. return RET_STATUS::RET_SUCCEED;
  616. }
  617. RET_STATUS nsGEN::HaoWeiDevice::SetFLFMode(std::string value)
  618. {
  619. FINFO("Enter SetFLFMode...,FLFMode:{$} \n", value.c_str());
  620. if (value == "CF")
  621. {
  622. m_DoseUnit.m_FLMode->Update(1);
  623. HWSend("FMF0");
  624. }
  625. else if (value == "PF")
  626. {
  627. m_DoseUnit.m_FLMode->Update(2);
  628. HWSend("FMF1");
  629. }
  630. else if (value == "HLF")
  631. {
  632. m_DoseUnit.m_FLMode->Update(2);
  633. HWSend("FMF1");
  634. }
  635. else
  636. {
  637. FINFO("other FluMode : {$}", value.c_str());
  638. }
  639. return RET_STATUS::RET_SUCCEED;
  640. }
  641. RET_STATUS nsGEN::HaoWeiDevice::SetAPF(const _tAPFArgs& t)
  642. {
  643. FINFO("APF:FLKV={$},FLMA={$},PPS={$},WS={$},FLuType={$},ABSMode={$},DoseLever={$}", t.nFLKV, t.fFLMA, t.nPPS, t.nWS, t.nFluMode, t.nABSMode, t.nDoseLever);
  644. SetFluKV(t.nFLKV);
  645. SetFluMA(t.fFLMA);
  646. return RET_STATUS::RET_SUCCEED;
  647. }
  648. RET_STATUS nsGEN::HaoWeiDevice::IncFluKV()
  649. {
  650. FINFO("FluKV value before calling IncFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str());
  651. //if (!m_DoseUnit.m_FLKV->CanInc()) return RET_STATUS::RET_SUCCEED;
  652. return HWSend("FKV+");
  653. }
  654. RET_STATUS nsGEN::HaoWeiDevice::DecFluKV()
  655. {
  656. FINFO("FluKV value before calling DecFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str());
  657. // (!m_DoseUnit.m_FLKV->CanDec()) return RET_STATUS::RET_SUCCEED;
  658. return HWSend("FKV-", 4);
  659. }
  660. RET_STATUS nsGEN::HaoWeiDevice::SetFluKV(float value)
  661. {
  662. FINFO("FluKV value before calling SetFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str());
  663. //if (!m_DoseUnit.m_FLKV->Verify(value)) return RET_STATUS::RET_SUCCEED;
  664. char temp[50] = { 0 };
  665. snprintf(temp, sizeof(temp), "FKV%03d", (int)value);
  666. return HWSend(temp, strlen(temp));
  667. }
  668. RET_STATUS nsGEN::HaoWeiDevice::IncFluMA()
  669. {
  670. FINFO("FluMA value before calling IncFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str());
  671. //if (!m_DoseUnit.m_FLMA->CanInc()) return RET_STATUS::RET_SUCCEED;
  672. return HWSend("FMA+");
  673. }
  674. RET_STATUS nsGEN::HaoWeiDevice::DecFluMA()
  675. {
  676. FINFO("FluMA value before calling DecFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str());
  677. //if (!m_DoseUnit.m_FLMA->CanDec()) return RET_STATUS::RET_SUCCEED;
  678. return HWSend("FMA-");
  679. }
  680. RET_STATUS nsGEN::HaoWeiDevice::SetFluMA(float value)
  681. {
  682. FINFO("FluMA value before calling SetFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str());
  683. if (!m_DoseUnit.m_FLMA->Verify(value)) return RET_STATUS::RET_SUCCEED;
  684. char temp[50] = { 0 };
  685. snprintf(temp, sizeof(temp), "FMA%04d", (int)(value * 10));
  686. return HWSend(temp, strlen(temp));
  687. }
  688. RET_STATUS nsGEN::HaoWeiDevice::INCPPS()
  689. {
  690. return RET_STATUS::RET_SUCCEED;
  691. }
  692. RET_STATUS nsGEN::HaoWeiDevice::DECPPS()
  693. {
  694. return RET_STATUS::RET_SUCCEED;
  695. }
  696. RET_STATUS nsGEN::HaoWeiDevice::SetPPS(float value)
  697. {
  698. //if (!m_DoseUnit.m_PPS->Verify(value)) return RET_STATUS::RET_SUCCEED;
  699. char temp[50] = { 0 };
  700. snprintf(temp, sizeof(temp), "FMN%02f", value);
  701. return HWSend(temp, strlen(temp));
  702. }
  703. RET_STATUS nsGEN::HaoWeiDevice::SetABSMode(int nMode)
  704. {
  705. if (!m_DoseUnit.m_ABSStatus->Verify(nMode)) return RET_STATUS::RET_SUCCEED;
  706. char temp[50] = { 0 };
  707. FINFO("SetABSMode[{$}] \n", nMode);
  708. snprintf(temp, sizeof(temp), "IBS%1d", (int)nMode);
  709. return HWSend(temp, strlen(temp));
  710. }
  711. RET_STATUS nsGEN::HaoWeiDevice::SetABSCurve(int curveNum)
  712. {
  713. return RET_STATUS::RET_SUCCEED;
  714. }
  715. RET_STATUS nsGEN::HaoWeiDevice::IncABSCurve()
  716. {
  717. return RET_STATUS::RET_SUCCEED;
  718. }
  719. RET_STATUS nsGEN::HaoWeiDevice::DecABSCurve()
  720. {
  721. return RET_STATUS::RET_SUCCEED;
  722. }
  723. RET_STATUS nsGEN::HaoWeiDevice::GetABSCurve()
  724. {
  725. return RET_STATUS::RET_SUCCEED;
  726. }
  727. float nsGEN::HaoWeiDevice::GetFluIntTimer()
  728. {
  729. return RET_STATUS::RET_SUCCEED;
  730. }
  731. float nsGEN::HaoWeiDevice::GetFluAccTimer()
  732. {
  733. return RET_STATUS::RET_SUCCEED;
  734. }
  735. RET_STATUS nsGEN::HaoWeiDevice::ResetFluTimer(int ntype)
  736. {
  737. FINFO("ReSetFluAccTimer:[{$}] \n", ntype);
  738. return HWSend("FTC");
  739. }
  740. RET_STATUS nsGEN::HaoWeiDevice::SetFluPre(int value)
  741. {
  742. return RET_STATUS::RET_SUCCEED;
  743. }
  744. RET_STATUS nsGEN::HaoWeiDevice::SetFluEXP(int value)
  745. {
  746. return RET_STATUS::RET_SUCCEED;
  747. }
  748. RET_STATUS nsGEN::HaoWeiDevice::SetFluMode(std::string value)
  749. {
  750. FINFO("Enter SetFLFMode...{$} \n", value.c_str());
  751. if (value == "CF")
  752. {
  753. m_DoseUnit.m_FLMode->Update(1);
  754. return HWSend("FLF1", 4);
  755. }
  756. else if (value == "PF")
  757. {
  758. m_DoseUnit.m_FLMode->Update(2);
  759. return HWSend("FLF2", 4);
  760. }
  761. else
  762. {
  763. FINFO("other FluMode : {$}", value.c_str());
  764. return RET_STATUS::RET_SUCCEED;
  765. }
  766. }
  767. RET_STATUS nsGEN::HaoWeiDevice::SetFluDoseLever(int value)
  768. {
  769. return RET_STATUS::RET_SUCCEED;
  770. }
  771. void CCOS::Dev::Detail::Generator::HaoWeiDevice::UpdateLimits(const std::string& key, float& currentValue, float defaultValue)
  772. {
  773. if (m_GenConfig.GetKeyCount(key.c_str()) > 0)
  774. {
  775. currentValue = static_cast<float>(m_GenConfig[key.c_str()]);
  776. }
  777. else
  778. {
  779. currentValue = defaultValue;
  780. }
  781. }
  782. void CCOS::Dev::Detail::Generator::HaoWeiDevice::UpdateLimitsInt(const std::string& key, int& currentValue, int defaultValue)
  783. {
  784. if (m_GenConfig.GetKeyCount(key.c_str()) > 0)
  785. {
  786. currentValue = static_cast<int>(m_GenConfig[key.c_str()]);
  787. }
  788. else
  789. {
  790. currentValue = defaultValue;
  791. }
  792. }
  793. RET_STATUS nsGEN::HaoWeiDevice::SetEXAMMode(std::string value)
  794. {
  795. //EXAMMODE_TYPE::MANUAL
  796. return RET_STATUS::RET_SUCCEED;
  797. }
  798. RET_STATUS nsGEN::HaoWeiDevice::ActiveSyncMode(_tSyncModeArgs value)
  799. {
  800. FINFO("value.strSyncMode: {$}, value.strSyncValue: {$}, value.strWS: {$} \n", value.strSyncMode, value.strSyncValue, value.strWS);
  801. int nSyncModeValue = atoi(value.strSyncValue.c_str());
  802. char temp[50] = { 0 };
  803. snprintf(temp, sizeof(temp), "WS%01d", nSyncModeValue);
  804. return HWSend(temp);
  805. }
  806. RET_STATUS nsGEN::HaoWeiDevice::SetFrameRate(float frameRate)
  807. {
  808. m_DoseUnit.m_FrameRate->Update(frameRate); //this variable should be set when in oncallback.
  809. char temp[50]{ 0 };
  810. snprintf(temp, sizeof(temp), "FLS%03d", int(frameRate*10));
  811. return HWSend(temp);
  812. }
  813. RET_STATUS nsGEN::HaoWeiDevice::SetRPS(int rps)
  814. {
  815. return RET_STATUS::RET_SUCCEED;
  816. //char temp[50]{ 0 };
  817. //snprintf(temp, sizeof(temp), "RPS%03d", rps * 10);
  818. //return HWSend(temp);
  819. }
  820. RET_STATUS nsGEN::HaoWeiDevice::RefreshData()
  821. {
  822. HWSend("RS?");
  823. return RET_STATUS::RET_SUCCEED;
  824. }
  825. RET_STATUS nsGEN::HaoWeiDevice::SetExpEnable()
  826. {
  827. FINFO("SetExpEnable in ...\n");
  828. if (!m_DeviceErrorHandler->HasActiveErrors())
  829. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  830. else
  831. {
  832. m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR);
  833. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  834. }
  835. return RET_STATUS::RET_SUCCEED;
  836. //return HWSend("EXB1");
  837. }
  838. RET_STATUS nsGEN::HaoWeiDevice::SetExpDisable()
  839. {
  840. FINFO("SetExpDisable in...\n");
  841. if (!m_DeviceErrorHandler->HasActiveErrors())
  842. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  843. else
  844. {
  845. m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR);
  846. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  847. }
  848. FINFO("SetExpDisable... {$}\n", m_DoseUnit.m_GenState->JSGet().c_str());
  849. return RET_STATUS::RET_SUCCEED;
  850. }
  851. RET_STATUS nsGEN::HaoWeiDevice::Reset()
  852. {
  853. m_bResetActive = true;
  854. FINFO("RESET in...\n");
  855. return HWSend("CLR0");
  856. m_DeviceErrorHandler->ClearAllErrors();
  857. m_DeviceErrorHandler->ClearAllWarnings();
  858. int level = 0;
  859. }
  860. RET_STATUS nsGEN::HaoWeiDevice::HWSend(const char* strCommand, int lengh, bool reSend, int nTimeOut)
  861. {
  862. if (!m_SCF) {
  863. FINFO("Failed - Serial communication interface not initialized");
  864. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
  865. {
  866. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  867. FINFO("Generator status updated to {$}", static_cast<int>(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN));
  868. }
  869. return RET_STATUS::RET_FAILED;
  870. }
  871. if (!m_SCF->IsConnected())
  872. {
  873. FERROR("Failed - Device not connected");
  874. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
  875. {
  876. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  877. FINFO("Generator status updated to {$}", static_cast<int>(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN));
  878. }
  879. return RET_STATUS::RET_FAILED;
  880. }
  881. // 使用传入的lengh参数,如果为0则用strlen计算
  882. int cmdLen = (lengh > 0) ? lengh : strlen(strCommand);
  883. // 检查缓冲区大小,数据包 = 命令 + ETX(1字节) + CheckSum(1字节)
  884. const int maxCmdLen = 256; // 增大缓冲区以支持更长的命令
  885. if (cmdLen > maxCmdLen - 2)
  886. {
  887. FERROR("Command too long: {$} bytes, max allowed: {$} bytes\n", cmdLen, maxCmdLen - 2);
  888. return RET_STATUS::RET_FAILED;
  889. }
  890. char strSendCommand[maxCmdLen] = { 0 };
  891. // 计算校验和
  892. int tmpSum = 0;
  893. for (int i = 0; i < cmdLen; i++)
  894. {
  895. tmpSum += (unsigned char)strCommand[i];
  896. }
  897. char checkSum = char(tmpSum + 3);
  898. // 构建数据包:命令 + ETX + CheckSum
  899. memcpy(strSendCommand, strCommand, cmdLen);
  900. strSendCommand[cmdLen] = 0x03;
  901. strSendCommand[cmdLen + 1] = checkSum;
  902. strSendCommand[cmdLen + 2] = 0x00;
  903. int totalLen = cmdLen + 3; // 实际发送的总长度
  904. // 打印完整数据包的十六进制和ASCII(含ETX和CheckSum)便于调试
  905. std::string hexStr;
  906. std::string asciiStr;
  907. hexStr.reserve(totalLen * 3);
  908. asciiStr.reserve(cmdLen + 10);
  909. int printLen = std::min(totalLen, 32); // 最多打印前32字节
  910. for (int i = 0; i < printLen; i++) {
  911. char buf[4];
  912. snprintf(buf, sizeof(buf), "%02X ", (unsigned char)strSendCommand[i]);
  913. hexStr += buf;
  914. }
  915. if (totalLen > 32) hexStr += "...";
  916. // 提取可打印的ASCII字符串(命令部分,不包括ETX和CheckSum)
  917. for (int i = 0; i < cmdLen; i++) {
  918. char ch = strCommand[i];
  919. if (ch >= 32 && ch <= 126) { // 可打印字符
  920. asciiStr += ch;
  921. }
  922. else {
  923. asciiStr += '.'; // 不可打印字符用'.'表示
  924. }
  925. }
  926. asciiStr += " + ETX + CKS"; // 标注后面的控制字符
  927. FINFO("==OUT== Packet[{$}]: HEX=[{$}] ASCII=[{$}]\n", totalLen, hexStr.c_str(), asciiStr.c_str());
  928. // 发送数据包,支持重发机制
  929. int maxRetry = reSend ? 2 : 1; // 如果reSend为true,最多重试2次
  930. unsigned int retLength = 0;
  931. for (int retry = 0; retry < maxRetry; retry++)
  932. {
  933. if (retry > 0)
  934. {
  935. FWARN("Retry sending packet, attempt {$}/{$}\n", retry + 1, maxRetry);
  936. Sleep(100); // 重试前短暂延时
  937. }
  938. if (m_SCF->Lock(1000) == WAIT_OBJECT_0)
  939. {
  940. // 使用实际长度totalLen,而不是strlen
  941. int result = m_SCF->SendPacket(strSendCommand, totalLen, nTimeOut, retLength);
  942. m_SCF->Unlock();
  943. if (result == SCF_SUCCEED)
  944. {
  945. if (retry > 0)
  946. {
  947. FINFO("Send succeeded after {$} retries\n", retry);
  948. }
  949. return RET_STATUS::RET_SUCCEED;
  950. }
  951. else
  952. {
  953. FERROR("SendPacket failed, result={$}, retry={$}/{$}\n", result, retry + 1, maxRetry);
  954. }
  955. }
  956. else
  957. {
  958. FERROR("Lock failed, retry={$}/{$}\n", retry + 1, maxRetry);
  959. }
  960. }
  961. FERROR("Send failed after {$} attempts\n", maxRetry);
  962. return RET_STATUS::RET_FAILED;
  963. }
  964. //-----------------------------------------------------------------------------
  965. // ProcessCmd
  966. //-----------------------------------------------------------------------------
  967. void nsGEN::HaoWeiDevice::FireNotify(std::string key, std::string content)
  968. {
  969. EventCenter->OnNotify(1, key, content);
  970. }
  971. struct tFrameMapping
  972. {
  973. static const int MaxLen = 5; // 前缀不能超超过 5 个字符 !
  974. using cbFun = std::function <void(const char*, int)>;
  975. char strHead[MaxLen];
  976. int NbOfCharOfHead;
  977. cbFun fun;
  978. tFrameMapping(const char* str, int len, cbFun f)
  979. {
  980. assert(len < MaxLen); //len最大只能是4
  981. //strHead[0] = 0x02; //STX ----------->note : no package header
  982. for (int i = 0; i < len; i++) //给strHead赋值
  983. strHead[i] = str[i];
  984. NbOfCharOfHead = len;
  985. fun = f;
  986. }
  987. };
  988. static std::list <tFrameMapping> arFrame;
  989. static bool DecodeFrame(const char* strFrame, int length);
  990. void nsGEN::HaoWeiDevice::OnCallBack()
  991. {
  992. auto HWNotProcess = [](const char* value, int length) -> void
  993. {
  994. printf("\n This commands didn't need to process!\n");
  995. FINFO("\n This commands didn't need to process!\n");
  996. };
  997. //==IN==:KV070 MA00320 MS00063 MX00036 sometimes : this long str appear. so must deal with it
  998. auto HWKV = [this](const char* value, int length) -> void
  999. {
  1000. assert(value);
  1001. FINFO("hwkv={$},len={$}",value, length);
  1002. int tmpkv = atoi(value);
  1003. m_DoseUnit.m_KV->Update(tmpkv);
  1004. FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet());
  1005. };
  1006. //==IN==:TU0 WS1 FO0 ET0 FI010 FS001 FN 0 HE000
  1007. auto HWTU = [this](const char* value, int length) -> void
  1008. {
  1009. assert(value);
  1010. FINFO("recv TU={$},len={$}", value,length);
  1011. };
  1012. auto HWEC = [this](const char* value, int length) -> void
  1013. {
  1014. assert(value);
  1015. FINFO("recv EC{$}",value);
  1016. };
  1017. auto HWST = [this](const char* value, int length) -> void
  1018. {
  1019. assert(value);
  1020. int genStatus = atoi(value);
  1021. // 获取状态码的首位数字,用来判断阶段
  1022. int statusPrefix = genStatus / 10;
  1023. // 根据阶段处理不同的状态
  1024. switch (statusPrefix)
  1025. {
  1026. case 10:
  1027. // 初始化阶段 (101, 102, 103)
  1028. FINFO("Get Gen Status:GENSTATE {$} -> (Initialization Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1029. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_INIT))
  1030. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  1031. break;
  1032. case 20:
  1033. // 待机阶段 (200)
  1034. if (!m_bHasInitializedDevice)
  1035. {
  1036. InitDevice();
  1037. m_bHasInitializedDevice = true;
  1038. }
  1039. FINFO("Get Gen Status:GENSTATE {$} -> (Standby Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1040. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_STANDBY))
  1041. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  1042. break;
  1043. case 30:
  1044. // 拍片准备阶段 (300)
  1045. FINFO("Get Gen Status:GENSTATE {$} -> (Film Prep Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1046. break;
  1047. case 40:
  1048. // 透视准备阶段 (400)
  1049. FINFO("Get Gen Status:GENSTATE {$} -> (Fluoroscopy Prep Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1050. break;
  1051. case 50:
  1052. // 曝光阶段 (500)
  1053. FINFO("Get Gen Status:GENSTATE {$} -> (Exposure Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1054. break;
  1055. case 60:
  1056. // 透视阶段 (600)
  1057. FINFO("Get Gen Status:GENSTATE {$} -> (Fluoroscopy Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1058. break;
  1059. case 70:
  1060. // 点片曝光阶段 (700)
  1061. FINFO("Get Gen Status:GENSTATE {$} -> (Spot Exposure Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1062. break;
  1063. case 80:
  1064. // 故障阶段 (800)
  1065. FINFO("Get Gen Status:GENSTATE {$} -> (Fault Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1066. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR))
  1067. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  1068. break;
  1069. case 90:
  1070. // 灯丝自动校准阶段 (900)
  1071. FINFO("Get Gen Status:GENSTATE {$} -> (Filament Auto Calibration Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1072. break;
  1073. case 91:
  1074. // 透视校准阶段 (910)
  1075. FINFO("Get Gen Status:GENSTATE {$} -> (Fluoroscopy Calibration Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1076. break;
  1077. case 92:
  1078. // 配置阶段 (920)
  1079. FINFO("Get Gen Status:GENSTATE {$} -> (Configuration Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1080. break;
  1081. case 93:
  1082. // 复位阶段 (930)
  1083. FINFO("Get Gen Status:GENSTATE {$} -> (Reset Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1084. break;
  1085. case 94:
  1086. // 升级阶段 (940)
  1087. FINFO("Get Gen Status:GENSTATE {$} -> (Upgrade Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
  1088. break;
  1089. default:
  1090. FINFO("Get Gen Status: [{$}] unknown", genStatus);
  1091. break;
  1092. }
  1093. };
  1094. auto HWMAS = [this](const char* value, int length)
  1095. {
  1096. assert(value);
  1097. int index = atoi(value);
  1098. float fmas = (m_bMasR20) ? R20_MAS[index] : R10_MAS[index];
  1099. fmas = fmas / 100.0;
  1100. FINFO("Current MAS:{$}", fmas);
  1101. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
  1102. {
  1103. m_DoseUnit.m_MAS->Update(fmas);
  1104. FireNotify(AttrKey::MAS, to_string(0));
  1105. }
  1106. else
  1107. {
  1108. m_DoseUnit.m_MAS->Update(fmas);
  1109. FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
  1110. }
  1111. };
  1112. auto HWMA = [this](const char* value, int length)
  1113. {
  1114. assert(value);
  1115. int index = atoi(value);
  1116. float fma = (m_bMasR20) ? R20_MA[index] : R10_MA[index];
  1117. fma = fma / 10.0;
  1118. FINFO("Current MA:{$}", fma);
  1119. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  1120. {
  1121. m_DoseUnit.m_MA->Update(fma);
  1122. FireNotify(AttrKey::MA, to_string(0));
  1123. }
  1124. else
  1125. {
  1126. m_DoseUnit.m_MA->Update(fma);
  1127. FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet());
  1128. }
  1129. };
  1130. auto HWMS = [this](const char* value, int length)
  1131. {
  1132. assert(value);
  1133. int index = atoi(value);
  1134. float fms = (m_bMasR20) ? R20_MS[index] : R10_MS[index];
  1135. fms = fms / 10.0;
  1136. FINFO("Current MS:{$}", fms);
  1137. if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
  1138. {
  1139. m_DoseUnit.m_MS->Update(fms);
  1140. FireNotify(AttrKey::MS, to_string(0));
  1141. }
  1142. else
  1143. {
  1144. m_DoseUnit.m_MS->Update(fms);
  1145. FireNotify(AttrKey::MS, m_DoseUnit.m_MS->JSGet());
  1146. }
  1147. };
  1148. auto HWFocus = [this](const char* value, int length)
  1149. {
  1150. assert(value);
  1151. int nfous = atoi(value);
  1152. m_DoseUnit.m_Focus->Update(nfous);
  1153. {
  1154. FireNotify(AttrKey::FOCUS, m_DoseUnit.m_Focus->JSGet());
  1155. FINFO("Current focus:{$}, FO={$}", atoi(m_DoseUnit.m_Focus->JSGet().c_str()) ? "large focus" : "small focus", m_DoseUnit.m_Focus->JSGet().c_str());
  1156. }
  1157. };
  1158. auto HWTechmode = [this](const char* value, int length)
  1159. {
  1160. assert(value);
  1161. int ntechmode = atoi(value);
  1162. m_DoseUnit.m_Techmode->Update(ntechmode);
  1163. FireNotify(AttrKey::TECHMODE, m_DoseUnit.m_Techmode->JSGet());
  1164. switch (ntechmode)
  1165. {
  1166. case 0:
  1167. FINFO("ET={$}", "mA/ms", m_DoseUnit.m_Techmode->JSGet().c_str());
  1168. break;
  1169. case 1:
  1170. FINFO("ET={$}", "mAs", m_DoseUnit.m_Techmode->JSGet().c_str());
  1171. break;
  1172. case 2:
  1173. FINFO("ET={$}", "AEC", m_DoseUnit.m_Techmode->JSGet().c_str());
  1174. break;
  1175. }
  1176. };
  1177. auto HWAECField = [this](const char* value, int length)
  1178. {
  1179. assert(value);
  1180. int nvalue = atoi(value);
  1181. if (m_DoseUnit.m_AECField->Update(nvalue))
  1182. FireNotify(AttrKey::AECFIELD, m_DoseUnit.m_AECField->JSGet());
  1183. };
  1184. auto HWAECFilm = [this](const char* value, int length)
  1185. {
  1186. assert(value);
  1187. int nvalue = atoi(value);
  1188. if (m_DoseUnit.m_AECFilm->Update(nvalue))
  1189. FireNotify(AttrKey::AECFILM, m_DoseUnit.m_AECFilm->JSGet());
  1190. };
  1191. auto HWAECDensity = [this](const char* value, int length)
  1192. {
  1193. assert(value);
  1194. int nvalue = atoi(value);
  1195. if (m_DoseUnit.m_AECDensity->Update(nvalue))
  1196. FireNotify(AttrKey::AECDENSITY, m_DoseUnit.m_AECDensity->JSGet());
  1197. };
  1198. auto HWWS = [this](const char* value, int length)
  1199. {
  1200. assert(value);
  1201. int nValue = atoi(value);
  1202. if (m_DoseUnit.m_WS->Update(nValue))
  1203. {
  1204. FireNotify(m_DoseUnit.m_WS->GetKey(), m_DoseUnit.m_WS->JSGet());
  1205. }
  1206. };
  1207. auto HWPR = [this](const char* value, int length)
  1208. {
  1209. assert(value);
  1210. int nValue = atoi(value);
  1211. if (nValue == 2)
  1212. {
  1213. FINFO("The high voltage generator enters the exposure preparation stage。");
  1214. HWSend("PR2");
  1215. }
  1216. else if (nValue == 1)
  1217. {
  1218. HWSend("PR1");
  1219. m_iLoopTime = HaoWei_LoopExpTime;
  1220. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_PREPARE))
  1221. {
  1222. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1223. FINFO("Generator exposure process status:GENERATOR_RAD_PREPARE");
  1224. }
  1225. }
  1226. else if (nValue == 0)
  1227. {
  1228. HWSend("PR0");
  1229. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF));
  1230. {
  1231. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1232. FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_OFF");
  1233. FINFO("HWPR m_DoseUnit.m_GenSynState->JSGet()={$}", m_DoseUnit.m_GenSynState->JSGet().c_str());
  1234. RefreshData();
  1235. }
  1236. if (m_iLoopTime == HaoWei_LoopExpTime)
  1237. {
  1238. if ((int)m_GenConfig["loopTime"] >= 100)
  1239. {
  1240. m_iLoopTime = (int)m_GenConfig["loopTime"];
  1241. }
  1242. else
  1243. m_iLoopTime = HaoWei_LoopDefTime;
  1244. FINFO("reduction loopTime[{$}]->[{$}]", HaoWei_LoopExpTime, m_iLoopTime.load());
  1245. }
  1246. }
  1247. };
  1248. auto HWXR = [this](const char* value, int length)
  1249. {
  1250. assert(value);
  1251. int nValue = atoi(value);
  1252. if (nValue == 1)
  1253. {
  1254. HWSend("XR1");
  1255. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_READY))
  1256. {
  1257. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1258. FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_READY");
  1259. }
  1260. }
  1261. else if (nValue == 2)
  1262. {
  1263. HWSend("XR2");
  1264. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP))
  1265. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  1266. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON))
  1267. {
  1268. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1269. FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_XRAYON");
  1270. }
  1271. }
  1272. else if (nValue == 0)
  1273. {
  1274. HWSend("XR0");
  1275. if(m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYOFF));
  1276. {
  1277. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1278. FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_XRAYOFF");
  1279. FINFO("HWXR m_DoseUnit.m_GenSynState->JSGet()={$}", m_DoseUnit.m_GenSynState->JSGet().c_str());
  1280. HWSend("AT?");
  1281. }
  1282. }
  1283. };
  1284. auto HWVPDOSE = [this](const char* value, int length)//mas
  1285. {
  1286. assert(value);
  1287. m_DoseUnit.m_PostKV->Update(atof(value));
  1288. FireNotify(AttrKey::POSTKV, m_DoseUnit.m_PostKV->JSGet());
  1289. FINFO("Actual exposure parameters KV:{$}", m_DoseUnit.m_PostKV->JSGet().c_str());
  1290. };
  1291. auto HWAPDOSE = [this](const char* value, int length)//mas
  1292. {
  1293. assert(value);
  1294. m_DoseUnit.m_PostMAS->Update(atof(value) / 10.0);
  1295. FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet());
  1296. FireNotify(AttrKey::POSTKV, m_DoseUnit.m_KV->JSGet());
  1297. FINFO("Actual exposure parameters MAS:{$}", m_DoseUnit.m_PostMAS->JSGet().c_str());
  1298. };
  1299. auto HWATDOSE = [this](const char* value, int length)
  1300. {
  1301. assert(value);
  1302. m_DoseUnit.m_PostMS->Update(atof(value) / 10.0);
  1303. FireNotify(m_DoseUnit.m_PostMS->GetKey(), m_DoseUnit.m_PostMS->JSGet());
  1304. m_DoseUnit.m_PostMA->Update(m_DoseUnit.m_MA->Get());
  1305. FireNotify(m_DoseUnit.m_PostMA->GetKey(), m_DoseUnit.m_PostMA->JSGet());
  1306. m_DoseUnit.m_PostKV->Update(m_DoseUnit.m_KV->Get());
  1307. FireNotify(AttrKey::POSTKV, m_DoseUnit.m_PostKV->JSGet());
  1308. m_DoseUnit.m_PostMAS->Update(m_DoseUnit.m_MAS->Get());
  1309. FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet());
  1310. FINFO("Actual exposure parameters MS:{$}", m_DoseUnit.m_PostMS->JSGet().c_str());
  1311. };
  1312. auto HWDAPST = [this](const char* value, int length)
  1313. {
  1314. assert(value);
  1315. int dapStatus = atoi(value);
  1316. if (dapStatus == 1)
  1317. {
  1318. m_bDAPEnable = true;
  1319. m_bAKEnable = false;
  1320. }
  1321. else if (dapStatus == 2)
  1322. {
  1323. m_bDAPEnable = false;
  1324. m_bAKEnable = true;
  1325. }
  1326. else if (dapStatus == 3)
  1327. {
  1328. m_bDAPEnable = true;
  1329. m_bAKEnable = true;
  1330. }
  1331. else
  1332. {
  1333. m_bDAPEnable = false;
  1334. m_bAKEnable = false;
  1335. }
  1336. };
  1337. auto HWDT = [this](const char* value, int length)
  1338. {
  1339. assert(value);
  1340. int dapStatus = atoi(value);
  1341. if (dapStatus == 0)
  1342. {
  1343. m_bDAPEnable = false;
  1344. FINFO("DAP test failed");
  1345. }
  1346. else if (dapStatus == 1)
  1347. {
  1348. m_bDAPEnable = true;
  1349. FINFO("DAP test passed");
  1350. }
  1351. else if (dapStatus == 2)
  1352. {
  1353. m_bDAPEnable = true;
  1354. FINFO("DAP test in progress.");
  1355. }
  1356. };
  1357. auto HWDAP = [this](const char* value, int length)
  1358. {
  1359. assert(value);
  1360. //m_DoseUnit.->Update(atof(value));
  1361. //FireNotify(m_DoseUnit.m_PostMS->GetKey(), m_DoseUnit.m_PostMS->JSGet());
  1362. //SetEvent(m_hGenPostEvent);
  1363. m_DAP->Update(atof(value)); //should push to subsystem.......
  1364. };
  1365. auto HWGR = [this](const char* value, int length)
  1366. {
  1367. assert(value);
  1368. // 提取机器型号、功率版本和功能类型
  1369. std::string version(value, 3); // 前3个字符为机器型号
  1370. char functionalityChar = value[3]; // 第4个字符表示功能类型
  1371. // 功能类型的映射
  1372. std::string functionality;
  1373. if (functionalityChar == '0') {
  1374. functionality = "PR (pet photography)";
  1375. }
  1376. else if (functionalityChar == '1') {
  1377. functionality = "PF (Pet fluoroscopy)";
  1378. }
  1379. else if (functionalityChar == '2') {
  1380. functionality = "MR (Human medical photography)";
  1381. }
  1382. else if (functionalityChar == '3') {
  1383. functionality = "MF (Human medical fluoroscopy)";
  1384. }
  1385. else if (functionalityChar == '4') {
  1386. functionality = "DR (Single tooth)";
  1387. }
  1388. else if (functionalityChar == '5') {
  1389. functionality = "DF (CBCT)";
  1390. }
  1391. else if (functionalityChar == '6') {
  1392. functionality = "IX (industrial circle)";
  1393. }
  1394. else if (functionalityChar == '7') {
  1395. functionality = "0X (other)";
  1396. }
  1397. else {
  1398. functionality = "Unknown Functionality:"+ functionalityChar;
  1399. }
  1400. // 日志打印输出
  1401. FINFO("Version: {$}, Functionality: {$}", version, functionality.c_str());
  1402. };
  1403. auto HWGV = [this](const char* value, int length)
  1404. {
  1405. assert(value);
  1406. // 打印原始值
  1407. FINFO("value: {$}", value);
  1408. int versionNumber = atoi(value);
  1409. // 拆解 versionNumber 为 4个部分(假设数字按版本号分割:10010 -> V1.0.0.10)
  1410. int major = versionNumber / 10000; // 取高位,得到主版本号
  1411. int minor = (versionNumber / 1000) % 10; // 取次高位,得到次版本号
  1412. int patch = (versionNumber / 100) % 10; // 取中位,得到修补版本号
  1413. int build = versionNumber % 100; // 取低位,得到构建版本号
  1414. // 格式化并打印详细版本信息
  1415. FINFO("Software Version: V{$}.{$}.{$}.{$}", major, minor, patch, build);
  1416. };
  1417. auto HWGU = [this](const char* value, int length)
  1418. {
  1419. assert(value);
  1420. // 打印原始值
  1421. FINFO("value: {$}", value);
  1422. std::string versionCode(value, 2);
  1423. // 定义版本类型
  1424. std::string versionType;
  1425. if (versionCode == "00") {
  1426. versionType = "General Version";
  1427. }
  1428. else if (versionCode == "01") {
  1429. versionType = "Custom Version";
  1430. }
  1431. else {
  1432. versionType = "Unknown Version"; // 处理其他情况,可以根据需求扩展
  1433. }
  1434. // 输出详细的版本信息
  1435. FINFO("Software Version: {$}, Type: {$}", value, versionType.c_str());
  1436. };
  1437. auto HWTR = [this](const char* value, int length)
  1438. {
  1439. assert(value);
  1440. if (value)
  1441. {
  1442. FINFO("Start automatic exposure once, after the end of an automatic reset");
  1443. }
  1444. else
  1445. {
  1446. FINFO("Stop automatic exposure");
  1447. }
  1448. };
  1449. auto HWGC = [this](const char* value, int length)
  1450. {
  1451. assert(value);
  1452. if (value)
  1453. {
  1454. FINFO("R20");
  1455. }
  1456. else
  1457. {
  1458. FINFO("R10");
  1459. }
  1460. };
  1461. auto HWER = [this](const char* value, int length)
  1462. {
  1463. assert(value);
  1464. std::string errorCode = "HaoWei_ER" + std::string(value);
  1465. if (m_DeviceErrorHandler->ParseAndReport(errorCode) == "error")
  1466. {
  1467. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR))
  1468. {
  1469. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  1470. }
  1471. }
  1472. };
  1473. auto HWWAR = [this](const char* value, int length)
  1474. {
  1475. assert(value);
  1476. int nWran = atoi(value);
  1477. if (nWran)
  1478. {
  1479. m_DeviceErrorHandler->ParseAndReport("ECOM_FluoroTimerLimitError");
  1480. }
  1481. };
  1482. auto HWEHE = [this](const char* value, int length)
  1483. {
  1484. assert(value);
  1485. int nhe = atoi(value);
  1486. FINFO("HE{$}%", nhe);
  1487. if (m_DoseUnit.m_HE->Update(nhe))
  1488. FireNotify(m_DoseUnit.m_HE->GetKey(), m_DoseUnit.m_HE->JSGet());
  1489. };
  1490. //075 FLM100 FLI000 FLT006 FLF2 FLA0 FLS060 FLZ0 FLD0
  1491. auto HWFKV = [this](const char* value, int length)
  1492. {
  1493. assert(value);
  1494. m_DoseUnit.m_FLKV->Update(atof(value));
  1495. FireNotify(AttrKey::FLUKV, m_DoseUnit.m_FLKV->JSGet());
  1496. };
  1497. auto HWFMA = [this](const char* value, int length)
  1498. {
  1499. assert(value);
  1500. float tmpflm = atof(value) / 10.0;
  1501. if (m_DoseUnit.m_FLMA->Update(tmpflm))
  1502. FireNotify(AttrKey::FLUMA, m_DoseUnit.m_FLMA->JSGet());
  1503. };
  1504. auto HWFLS = [this](const char* value, int length)
  1505. {
  1506. assert(value);
  1507. FINFO("HWFLS={$}", value);
  1508. float tmppps = atof(value) / 10.0;
  1509. if (m_DoseUnit.m_PPS->Update(tmppps))
  1510. FireNotify(AttrKey::FLUPPS, m_DoseUnit.m_PPS->JSGet());
  1511. };
  1512. auto HWFLX = [this](const char* value, int length)
  1513. {
  1514. assert(value);
  1515. int nValue = atoi(value);
  1516. if (nValue == 0)
  1517. {
  1518. HWSend("FLX0");
  1519. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYOFF))
  1520. {
  1521. FINFO("Generator exposure process status{$};", "GENERATOR_FLU_XRAYOFF");
  1522. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1523. }
  1524. }
  1525. else
  1526. {
  1527. HWSend("FLX1");
  1528. if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP))
  1529. FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
  1530. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYON))
  1531. {
  1532. FINFO("Generator exposure process status{$};", "GENERATOR_FLU_XRAYON");
  1533. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1534. }
  1535. }
  1536. };
  1537. auto HWFLP = [this](const char* value, int length)
  1538. {
  1539. assert(value);
  1540. int nValue = atoi(value);
  1541. if (nValue == 0)
  1542. {
  1543. if (m_iLoopTime == HaoWei_LoopExpTime)
  1544. {
  1545. if ((int)m_GenConfig["loopTime"] >= 100)
  1546. {
  1547. m_iLoopTime = (int)m_GenConfig["loopTime"];
  1548. }
  1549. else
  1550. m_iLoopTime = HaoWei_LoopDefTime;
  1551. FINFO("reduction loopTime[{$}]->[{$}]", HaoWei_LoopExpTime, m_iLoopTime.load());
  1552. }
  1553. HWSend("FLP0");
  1554. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_OFF))
  1555. {
  1556. FINFO("脚闸抬起,发生器曝光流程状态{$};", "GENERATOR_FLU_OFF");
  1557. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1558. }
  1559. RefreshData();
  1560. }
  1561. else
  1562. {
  1563. m_iLoopTime = HaoWei_LoopExpTime;
  1564. HWSend("FLP1");
  1565. if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_READY))
  1566. {
  1567. FINFO("Generator exposure process status{$};", "GENERATOR_FLU_READY");
  1568. FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
  1569. }
  1570. }
  1571. };
  1572. auto HWFLF = [this](const char* value, int length)
  1573. {
  1574. assert(value);
  1575. FINFO("HWFLF={$};", value);
  1576. int tmpflf = atoi(value);
  1577. if (m_DoseUnit.m_FLMode->Update(tmpflf))
  1578. FireNotify(AttrKey::FLUMode, m_DoseUnit.m_FLMode->JSGet());
  1579. };
  1580. auto HWFLA = [this](const char* value, int length)
  1581. {
  1582. assert(value);
  1583. int tmpfla = atoi(value);
  1584. if (m_DoseUnit.m_ABSStatus->Update(tmpfla))
  1585. FireNotify(AttrKey::FLUABSStatus, m_DoseUnit.m_ABSStatus->JSGet());
  1586. };
  1587. auto HWFLD = [this](const char* value, int length)
  1588. {
  1589. assert(value);
  1590. };
  1591. auto HWFLC = [this](const char* value, int length)
  1592. {
  1593. assert(value);
  1594. };
  1595. auto HWFLO = [this](const char* value, int length)
  1596. {
  1597. assert(value);
  1598. int nValue = atoi(value);
  1599. if (m_DoseUnit.m_Curve->Update(nValue))
  1600. FireNotify(AttrKey::FLUCurve, m_DoseUnit.m_Curve->JSGet());
  1601. };
  1602. auto HWFLW = [this](const char* value, int length)
  1603. {
  1604. assert(value);;
  1605. float tmpflms = atof(value) / 100.0;
  1606. if (m_DoseUnit.m_FLMS->Update(tmpflms))
  1607. FireNotify(AttrKey::FLUMS, m_DoseUnit.m_FLMS->JSGet());
  1608. };
  1609. auto HWFLI = [this](const char* value, int length)
  1610. {
  1611. assert(value);
  1612. float tmpfli = atof(value) / 10.0;
  1613. if (m_DoseUnit.m_FLIntTime->Update(tmpfli))
  1614. FireNotify(AttrKey::FLUIntTime, m_DoseUnit.m_FLIntTime->JSGet());
  1615. };
  1616. auto HWFLT = [this](const char* value, int length)
  1617. {
  1618. assert(value);
  1619. float tmpflt = atof(value);
  1620. if (m_DoseUnit.m_FLAccTime->Update(tmpflt))
  1621. FireNotify(AttrKey::FLUAccTime, m_DoseUnit.m_FLAccTime->JSGet());
  1622. };
  1623. // 有部分前缀是包含关系, 长的包含短的, 例如 KVS 包含了 KV.
  1624. // 因此长的在前面, 短的在后面
  1625. // !!! Device 是个短寿命对象, 而 arFrame 是静态变量 !!!
  1626. // !!! 因此, 在添加到 arFrame 之前, 务必先清零 !!!
  1627. arFrame.clear();
  1628. arFrame.push_back(tFrameMapping("KV", 2, HWKV));
  1629. arFrame.push_back(tFrameMapping("TU", 2, HWTU));
  1630. arFrame.push_back(tFrameMapping("MX", 2, HWMAS));
  1631. arFrame.push_back(tFrameMapping("MA", 2, HWMA));
  1632. arFrame.push_back(tFrameMapping("MS", 2, HWMS));
  1633. arFrame.push_back(tFrameMapping("TE", 2, HWTechmode));
  1634. arFrame.push_back(tFrameMapping("FO", 2, HWFocus));
  1635. arFrame.push_back(tFrameMapping("FI", 2, HWAECField));
  1636. arFrame.push_back(tFrameMapping("FS", 2, HWAECFilm));
  1637. arFrame.push_back(tFrameMapping("FN", 2, HWAECDensity));
  1638. arFrame.push_back(tFrameMapping("WS", 2, HWWS));
  1639. arFrame.push_back(tFrameMapping("PR", 2, HWPR));
  1640. arFrame.push_back(tFrameMapping("XR", 2, HWXR));
  1641. arFrame.push_back(tFrameMapping("VP", 2, HWVPDOSE));
  1642. arFrame.push_back(tFrameMapping("AP", 2, HWAPDOSE));
  1643. arFrame.push_back(tFrameMapping("AT", 2, HWATDOSE));
  1644. arFrame.push_back(tFrameMapping("ER", 2, HWER));
  1645. arFrame.push_back(tFrameMapping("HE", 2, HWEHE));
  1646. arFrame.push_back(tFrameMapping("ST", 2, HWST));
  1647. arFrame.push_back(tFrameMapping("EC", 2, HWEC));
  1648. arFrame.push_back(tFrameMapping("GR", 2, HWGR));
  1649. arFrame.push_back(tFrameMapping("GV", 2, HWGV));
  1650. arFrame.push_back(tFrameMapping("GU", 2, HWGU));
  1651. arFrame.push_back(tFrameMapping("TR", 2, HWTR));
  1652. arFrame.push_back(tFrameMapping("DS", 2, HWDAPST)); //dap status
  1653. arFrame.push_back(tFrameMapping("DA", 2, HWDAP)); //dap value
  1654. arFrame.push_back(tFrameMapping("DT", 2, HWDT)); //test dap
  1655. arFrame.push_back(tFrameMapping("WAR", 3, HWWAR));
  1656. arFrame.push_back(tFrameMapping("FKV", 3, HWFKV));
  1657. arFrame.push_back(tFrameMapping("FMA", 3, HWFMA));
  1658. arFrame.push_back(tFrameMapping("FLS", 3, HWFLS));
  1659. arFrame.push_back(tFrameMapping("FLF", 3, HWFLF));
  1660. arFrame.push_back(tFrameMapping("FLP", 3, HWFLP));
  1661. arFrame.push_back(tFrameMapping("FLX", 3, HWFLX));
  1662. arFrame.push_back(tFrameMapping("FLW", 3, HWFLW));
  1663. arFrame.push_back(tFrameMapping("FLI", 3, HWFLI));
  1664. arFrame.push_back(tFrameMapping("FTS", 3, HWFLT));
  1665. arFrame.push_back(tFrameMapping("FLA", 3, HWFLA));
  1666. arFrame.push_back(tFrameMapping("FLD", 3, HWFLD));
  1667. arFrame.push_back(tFrameMapping("FLC", 3, HWFLD));
  1668. arFrame.push_back(tFrameMapping("FLO", 3, HWFLO));
  1669. }
  1670. bool nsGEN::HaoWeiDevice::StartHardwareStatusThread()
  1671. {
  1672. FINFO("========================================");
  1673. FINFO("Starting Hardware Status Thread...");
  1674. if (!m_pHardwareStatusThread.joinable())
  1675. {
  1676. FINFO("Creating new hardware status thread");
  1677. m_pHardwareStatusThread = std::thread(HardwareStatusThread, this);
  1678. FINFO("Hardware status thread created successfully");
  1679. FINFO("========================================");
  1680. return true;
  1681. }
  1682. FWARN("Hardware status thread already running");
  1683. FINFO("========================================");
  1684. return false;
  1685. }
  1686. void nsGEN::HaoWeiDevice::HardwareStatusThread(HaoWeiDevice* pParam)
  1687. {
  1688. FINFO("========================================");
  1689. FINFO("HardwareStatusThread Entry");
  1690. FINFO("========================================");
  1691. HaoWeiDevice* pCurGen = pParam;
  1692. if (pCurGen == NULL)
  1693. {
  1694. FERROR("HardwareStatusThread: pParam is NULL, exiting thread");
  1695. return;
  1696. }
  1697. int currtTime = pCurGen->m_iLoopTime;
  1698. int messageIndex = 0;
  1699. int loopCount = 0;
  1700. FINFO("Hardware status monitoring started");
  1701. FINFO("Initial loop time: {$} ms", currtTime);
  1702. while (true)
  1703. {
  1704. auto now = std::chrono::steady_clock::now();
  1705. auto last = lastValidResponse.load();
  1706. if (now - last > TIMEOUT && pCurGen->m_DoseUnit.m_GenState->Get() > 0) {
  1707. FINFO("The timeout did not respond");
  1708. pCurGen->m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN); // 超时未响应则重置状态
  1709. }
  1710. currtTime = pCurGen->m_iLoopTime;
  1711. Sleep(currtTime);
  1712. //获取消息
  1713. //pCurGen->HWSend("ST?");
  1714. }
  1715. FINFO("HardwareStatusThread stop");
  1716. }
  1717. //-----------------------------------------------------------------------------
  1718. // HaoWeiDriver
  1719. //-----------------------------------------------------------------------------
  1720. nsGEN::HaoWeiDriver::HaoWeiDriver()
  1721. {
  1722. }
  1723. nsGEN::HaoWeiDriver::~HaoWeiDriver()
  1724. {
  1725. }
  1726. auto nsGEN::HaoWeiDriver::CreateDevice(int index) -> std::unique_ptr <IODevice>
  1727. {
  1728. FINFO("CreateDevice in\n");
  1729. m_pDevice = new HaoWeiDevice(EventCenter, m_scfWrapper, m_ConfigFileName);
  1730. auto dev = std::unique_ptr <IODevice>(new IODevice(m_pDevice));
  1731. FINFO("CreateDevice out\n");
  1732. return dev;
  1733. }
  1734. void nsGEN::HaoWeiDriver::FireNotify(int code, std::string key, std::string content)
  1735. {
  1736. EventCenter->OnNotify(code, key, content);
  1737. }
  1738. void nsGEN::HaoWeiDriver::Prepare()
  1739. {
  1740. FINFO("========================================");
  1741. FINFO("Prepare() Starting...");
  1742. FINFO("========================================");
  1743. // 初始化日志系统
  1744. std::string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
  1745. std::string LogHost = "DevHaoWei";
  1746. std::string moduleName = "DevHaoWei";
  1747. FINFO("Initializing log module...");
  1748. FINFO("Log config path: {$}", strLogPath.c_str());
  1749. FINFO("Log host: {$}", LogHost.c_str());
  1750. FINFO("Module name: {$}", moduleName.c_str());
  1751. bool ret = initLogModule(
  1752. LogHost, // 主机名(用于日志路径中的{host}占位符)
  1753. moduleName, // 唯一模块名
  1754. strLogPath, // 配置文件路径
  1755. true // 是否输出到控制台(可选)
  1756. );
  1757. if (!ret) {
  1758. std::cerr << "Log init failed!" << std::endl;
  1759. FERROR("Failed to initialize log module");
  1760. return;
  1761. }
  1762. FINFO("Log module initialized successfully");
  1763. HaoWei_SetLocalModuleName(moduleName);
  1764. FINFO("Getting connection DLL from config: {$}", m_ConfigFileName.c_str());
  1765. m_SCFDllName = GetConnectDLL(m_ConfigFileName);
  1766. FINFO("SCF DLL name: {$}", m_SCFDllName.c_str());
  1767. FINFO("Calling parent Prepare()...");
  1768. super::Prepare();
  1769. FINFO("========================================");
  1770. FINFO("REMEDYSTDriver::Prepare() Completed");
  1771. FINFO("========================================");
  1772. }
  1773. bool DATA_ACTION nsGEN::HaoWeiDriver::Connect()
  1774. {
  1775. std::lock_guard<std::mutex> lock(m_connectionMutex);
  1776. const auto currentState = m_connectionState.load();
  1777. auto now = std::chrono::steady_clock::now();
  1778. // 1. 处理可重试的失败状态
  1779. if (currentState == ConnectionState::Failed) {
  1780. if ((now - m_lastConnectionAttempt) >= RETRY_INTERVAL && m_connectionRetryCount < MAX_RETRY_COUNT) {
  1781. m_connectionState = ConnectionState::Disconnected;
  1782. }
  1783. else {
  1784. return false; // 不满足重试条件,直接返回
  1785. }
  1786. }
  1787. // 2. 检查无效状态(正在连接/已连接但实际有效)
  1788. if (currentState == ConnectionState::Connecting) {
  1789. FINFO("Already connecting (type: {$})",
  1790. m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
  1791. return true;
  1792. }
  1793. if (currentState == ConnectionState::Connected && m_scfWrapper && m_scfWrapper->IsConnected()) {
  1794. FINFO("Already connected (type: {$})",
  1795. m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
  1796. return true;
  1797. }
  1798. // 3. 检查重试间隔
  1799. if (m_connectionRetryCount > 0 && (now - m_lastConnectionAttempt) < RETRY_INTERVAL) {
  1800. FINFO("Retry in {$}s (type: {$})",
  1801. std::chrono::duration_cast<std::chrono::seconds>(RETRY_INTERVAL - (now - m_lastConnectionAttempt)).count(),
  1802. m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
  1803. return false;
  1804. }
  1805. ResDataObject connParam = GetConnectParam(m_ConfigFileName);
  1806. std::string connPortStr = "";
  1807. std::string connTypeStr = (std::string)connParam["type"]; // 从配置读取type字段
  1808. m_currentConnType = (connTypeStr == "COM") ? ConnectionType::Serial : ConnectionType::Ethernet;
  1809. if (m_currentConnType == ConnectionType::Serial)
  1810. {
  1811. connPortStr = (std::string)connParam["port"];// 从配置读取port字段
  1812. // 查找配置端口在现有端口列表中的位置
  1813. auto it = std::find(m_serialPorts.begin(), m_serialPorts.end(), connPortStr);
  1814. if (it == m_serialPorts.end()) {
  1815. // 配置的端口不在列表中,添加到首位
  1816. FINFO("Configured serial port {$} not found, adding to front of port list", connPortStr);
  1817. m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
  1818. }
  1819. else if (it != m_serialPorts.begin()) {
  1820. // 配置的端口存在但不在首位,移动到首位
  1821. FINFO("Moving configured serial port {$} to front of port list", connPortStr);
  1822. m_serialPorts.erase(it);
  1823. m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
  1824. }
  1825. }
  1826. // 4. 执行连接流程
  1827. m_connectionState = ConnectionState::Connecting;
  1828. m_lastConnectionAttempt = now;
  1829. std::string connInfo;
  1830. try {
  1831. if (m_currentConnType == ConnectionType::Serial) {
  1832. // 串口连接:使用当前索引的端口
  1833. std::string currentPort = m_serialPorts[m_currentSerialPortIndex];
  1834. connParam.update("port", currentPort.c_str()); // 将当前尝试的端口写入参数
  1835. connInfo = "Serial (port: " + currentPort + ")";
  1836. }
  1837. else {
  1838. // 网口连接:直接使用配置参数
  1839. connInfo = "Ethernet (ip: " + std::string(connParam["ip"]) + ")";
  1840. }
  1841. FINFO("Enter Connect ({$}), config: {$}", connInfo, connParam.encode());
  1842. if (!m_scfWrapper->Initialize(m_SCFDllName)) {
  1843. FINFO("Init failed: {$}", m_scfWrapper->GetLastError());
  1844. m_connectionState = ConnectionState::Failed;
  1845. return false;
  1846. }
  1847. m_scfWrapper->SetDataReceivedCallback([this](const char* data, uint32_t length) {
  1848. this->Dequeue(data, length);
  1849. });
  1850. auto erCode = m_scfWrapper->Connect(connParam, &HaoWeiDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
  1851. if (erCode != SCF_SUCCEED || !m_scfWrapper->StartAutoReceive()) {
  1852. FINFO("Connect failed (code: {$}) for {$}", erCode, connInfo);
  1853. m_scfWrapper->Disconnect();
  1854. m_connectionState = ConnectionState::Failed;
  1855. // 串口连接:未遍历完所有端口时,优先切换端口重试(不计入总重试次数)
  1856. if (m_currentConnType == ConnectionType::Serial) {
  1857. int nextIndex = (m_currentSerialPortIndex + 1) % m_serialPorts.size();
  1858. // 判断是否已遍历所有端口(当前索引是最后一个时,nextIndex会回到0)
  1859. bool allPortsTried = (nextIndex == 0);
  1860. if (!allPortsTried) {
  1861. // 未遍历完所有端口:切换到下一端口,不增加重试计数
  1862. m_currentSerialPortIndex = nextIndex;
  1863. m_connectionRetryCount = 0;
  1864. FINFO("Trying next serial port: {$}", m_serialPorts[nextIndex]);
  1865. return false; // 触发外部线程立即尝试下一端口
  1866. }
  1867. else {
  1868. // 已遍历所有端口:重置到第一个端口,增加重试计数(进入间隔等待)
  1869. m_currentSerialPortIndex = 0;
  1870. m_connectionRetryCount++;
  1871. FINFO("All serial ports tried, retry count: {$}/{$}", m_connectionRetryCount, MAX_RETRY_COUNT);
  1872. return false;
  1873. }
  1874. }
  1875. // 所有端口失败(串口)或网口失败,才增加总重试计数
  1876. m_connectionRetryCount++;
  1877. return false;
  1878. }
  1879. // 连接成功:重置状态
  1880. m_connectionState = ConnectionState::Connected;
  1881. m_connectionRetryCount = 0;
  1882. m_currentSerialPortIndex = 0; // 重置串口端口索引
  1883. FINFO("Connected successfully ({$})", connInfo);
  1884. return true;
  1885. }
  1886. catch (const std::exception& e) {
  1887. FINFO("Exception for {$}: {$}", connInfo, e.what());
  1888. m_connectionState = ConnectionState::Failed;
  1889. m_connectionRetryCount++;
  1890. return false;
  1891. }
  1892. }
  1893. void nsGEN::HaoWeiDriver::Disconnect()
  1894. {
  1895. FINFO("========================================");
  1896. FINFO("REMEDYSTDriver::Disconnect() Entry");
  1897. if (m_scfWrapper) {
  1898. FINFO("Stopping auto receive...");
  1899. m_scfWrapper->StopAutoReceive();
  1900. FINFO("Auto receive stopped");
  1901. FINFO("Disconnecting SCF wrapper...");
  1902. m_scfWrapper->Disconnect();
  1903. FINFO("SCF wrapper disconnected");
  1904. m_connectionState = ConnectionState::Disconnected;
  1905. FINFO("Connection state set to Disconnected");
  1906. }
  1907. else {
  1908. FWARN("SCF wrapper is null, nothing to disconnect");
  1909. }
  1910. FINFO("REMEDYSTDriver::Disconnect() Completed");
  1911. FINFO("========================================");
  1912. }
  1913. bool nsGEN::HaoWeiDriver::isConnected() const
  1914. {
  1915. const auto state = m_connectionState.load();
  1916. // 1. 连接中/实际已连接:返回true(无需重连)
  1917. if (state == ConnectionState::Connecting || (m_scfWrapper && m_scfWrapper->IsConnected())) {
  1918. //FINFO(state == ConnectionState::Connecting ? "Connecting in progress" : "Already connected");
  1919. return true;
  1920. }
  1921. // 2. 失败状态处理:判断是否允许重试
  1922. if (state == ConnectionState::Failed) {
  1923. auto now = std::chrono::steady_clock::now();
  1924. const auto timeSinceLast = now - m_lastConnectionAttempt;
  1925. // 2.1 达到最大重试次数,但超过重置间隔:重置计数,允许重新重试
  1926. if (m_connectionRetryCount >= MAX_RETRY_COUNT && timeSinceLast >= RESET_RETRY_AFTER) {
  1927. FINFO("Max retries reached, resetting count after {$}s",
  1928. std::chrono::duration_cast<std::chrono::seconds>(timeSinceLast).count());
  1929. m_connectionRetryCount = 0; // 重置计数(因mutable修饰,const函数可修改)
  1930. }
  1931. // 2.2 不适合重连(时间未到 或 次数仍超限):返回true阻止重连
  1932. if (timeSinceLast < RETRY_INTERVAL || m_connectionRetryCount >= MAX_RETRY_COUNT) {
  1933. FINFO(timeSinceLast < RETRY_INTERVAL ?
  1934. "Retry later ({$}s)" : "Max retries, waiting {$}s to reset",
  1935. std::chrono::duration_cast<std::chrono::seconds>(
  1936. timeSinceLast < RETRY_INTERVAL ? RETRY_INTERVAL - timeSinceLast : RESET_RETRY_AFTER - timeSinceLast
  1937. ).count()
  1938. );
  1939. return true;
  1940. }
  1941. }
  1942. // 3. 其他情况(适合重连):返回false触发Connect()
  1943. return false;
  1944. }
  1945. std::string nsGEN::HaoWeiDriver::DriverProbe()
  1946. {
  1947. FINFO("DriverProbe in \n");
  1948. ResDataObject r_config, HardwareInfo;
  1949. if (r_config.loadFile(m_ConfigFileName.c_str()))
  1950. {
  1951. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  1952. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  1953. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  1954. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  1955. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  1956. }
  1957. else
  1958. {
  1959. HardwareInfo.add("MajorID", "Generator");
  1960. HardwareInfo.add("MinorID", "Dr");
  1961. HardwareInfo.add("VendorID", "HaoWei");
  1962. HardwareInfo.add("ProductID", "HF");
  1963. HardwareInfo.add("SerialID", "Drv");
  1964. }
  1965. string ret = HardwareInfo.encode();
  1966. FINFO("DriverProbe out \n");
  1967. return ret;
  1968. }
  1969. std::string nsGEN::HaoWeiDriver::GetResource()
  1970. {
  1971. ResDataObject temp;
  1972. if (!temp.loadFile(m_ConfigFileName.c_str()))
  1973. return std::string();
  1974. auto r_config = temp["CONFIGURATION"];
  1975. for (auto& Item : m_ConfigInfo)
  1976. {
  1977. string key = Item.GetKey();
  1978. if (key == ConfKey::CcosGeneratorType)
  1979. {
  1980. Item.SetCurrentValue(((string)r_config["VendorID"]).c_str());
  1981. }
  1982. else if (key == ConfKey::CcosGeneratorModel)
  1983. {
  1984. Item.SetCurrentValue(((string)r_config["ProductID"]).c_str());
  1985. }
  1986. else if (key == ConfKey::CcosWSTable || key == ConfKey::CcosWSWall || key == ConfKey::CcosWSFree
  1987. || key == ConfKey::CcosWSTomo || key == ConfKey::CcosWSConventional)
  1988. {
  1989. Item.SetCurrentValue(((string)r_config[key.c_str()]).c_str());
  1990. }
  1991. else if (key == ConfKey::CcosSynTable || key == ConfKey::CcosSynWall || key == ConfKey::CcosSynFree
  1992. || key == ConfKey::CcosSynTomo || key == ConfKey::CcosSynConventional)
  1993. {
  1994. Item.SetCurrentValue(((string)r_config[key.c_str()]).c_str());
  1995. }
  1996. else if (key == ConfKey::CcosSCFType)
  1997. {
  1998. Item.SetCurrentValue(((string)r_config["connections"][0]["type"]).c_str());
  1999. }
  2000. else if (key == ConfKey::CcosSCFPort || key == ConfKey::CcosSCFBaudrate || key == ConfKey::CcosSCFBytesize
  2001. || key == ConfKey::CcosSCFParity || key == ConfKey::CcosSCFStopbits || key == ConfKey::CcosSCFIP)
  2002. {
  2003. if (r_config["connections"][0].GetFirstOf(key.c_str()) >= 0)
  2004. {
  2005. Item.SetCurrentValue(((string)r_config["connections"][0][key.c_str()]).c_str());
  2006. }
  2007. }
  2008. }
  2009. ResDataObject resAttr, resDescription;
  2010. for (auto Item : m_ConfigInfo)
  2011. {
  2012. resAttr.add(Item.GetKey(), Item.GetCurrentValue());
  2013. resDescription.add(Item.GetKey(), Item.GetDescription());
  2014. }
  2015. ResDataObject resDeviceResource;
  2016. resDeviceResource.add(ConfKey::CcosGeneratorAttribute, resAttr);
  2017. resDeviceResource.add(ConfKey::CcosGeneratorDescription, resDescription);
  2018. string res = resDeviceResource.encode();
  2019. //printf("resDeviceResource :%s \n", resDeviceResource.encode());
  2020. FINFO("resDeviceResource :{$} \n", resDeviceResource.encode());
  2021. ResDataObject DescriptionTempEx;
  2022. DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource);
  2023. m_DeviceConfig.clear();
  2024. m_DeviceConfig = DescriptionTempEx;
  2025. return res;
  2026. }
  2027. bool nsGEN::HaoWeiDriver::GetDeviceConfig(std::string& Cfg)
  2028. {
  2029. //Cfg = m_DeviceConfigSend.encode();
  2030. Cfg = m_DeviceConfig.encode();
  2031. //printf("GetDeviceConfig over");
  2032. printf("GetDeviceConfig over , %s", Cfg.c_str());
  2033. return true;
  2034. }
  2035. bool nsGEN::HaoWeiDriver::SetDeviceConfig(std::string Cfg)
  2036. {
  2037. FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
  2038. return true;
  2039. }
  2040. bool nsGEN::HaoWeiDriver::SaveConfigFile(bool bSendNotify)
  2041. {
  2042. m_ConfigAll["CONFIGURATION"] = m_Configurations;
  2043. bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
  2044. FINFO("SaveConfigFile over {$}", bRt);
  2045. return true;
  2046. }
  2047. std::string nsGEN::HaoWeiDriver::DeviceProbe()
  2048. {
  2049. FINFO("std::string nsGEN::PSGRFDriver::DeviceProbe() in\n");
  2050. ResDataObject r_config, HardwareInfo;
  2051. if (r_config.loadFile(m_ConfigFileName.c_str()))
  2052. {
  2053. HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
  2054. HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
  2055. HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
  2056. HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
  2057. HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
  2058. }
  2059. else
  2060. {
  2061. HardwareInfo.add("MajorID", "Generator");
  2062. HardwareInfo.add("MinorID", "Dr");
  2063. HardwareInfo.add("VendorID", "HaoWei");
  2064. HardwareInfo.add("ProductID", "HF");
  2065. HardwareInfo.add("SerialID", "1234");
  2066. }
  2067. string ret = HardwareInfo.encode();
  2068. FINFO("std::string nsGEN::PSGRFDriver::DeviceProbe() out\n");
  2069. return ret;
  2070. }
  2071. void nsGEN::HaoWeiDriver::Dequeue(const char* Packet, DWORD Length)
  2072. {
  2073. DecodeFrame(Packet, Length);
  2074. }
  2075. /*
  2076. ==IN==:KV070 MA00320 MS00063 MX00036
  2077. ==IN==:TU0 WS1 FO0 ET0 FI010 FS001 FN 0 HE000
  2078. how to split the str like up.
  2079. //command+03+sum
  2080. */
  2081. PACKET_RET nsGEN::HaoWeiDriver::callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength)
  2082. {
  2083. FINFO("==IN==11:[{:02X$}]\n", RecData);
  2084. if (nLength < 1) // 最小有效包为0x03 + checksum + 0x00(3字节)
  2085. {
  2086. return PACKET_USELESS;
  2087. }
  2088. for (DWORD i = 0; i < nLength; ++i) // 遍历所有字节
  2089. {
  2090. if (RecData[i] == 0x00)
  2091. {
  2092. if (i < 2) { // 至少需要0x03、校验和和结束符
  2093. return PACKET_USELESS;
  2094. }
  2095. size_t markerPos = i - 2;
  2096. if (RecData[markerPos] != 0x03) { // 检查0x03标识符
  2097. // 不是有效的起始符,继续查找下一个结束符
  2098. continue;
  2099. }
  2100. //BYTE calcSum = 0;
  2101. //for (size_t j = 0; j <= markerPos; ++j) {
  2102. // calcSum += RecData[j];
  2103. //}
  2104. //BYTE recvSum = RecData[i - 1];
  2105. //if (calcSum != recvSum) {
  2106. // // 校验失败,继续查找后续可能的包
  2107. // continue;
  2108. //}
  2109. PacketLength = static_cast<DWORD>(i - 2); //应该+2.才会把最后的checmsum也包含进来。FO1+03+sum.
  2110. char strtemp[100] = { 0 };
  2111. memcpy(strtemp, RecData, i);
  2112. strtemp[i + 1] = 0;
  2113. FINFO("==IN==:{:02x$}\n", strtemp);
  2114. return PACKET_ISPACKET;
  2115. }
  2116. }
  2117. return PACKET_NOPACKET;
  2118. }
  2119. //-----------------------------------------------------------------------------
  2120. // DecodeFrame
  2121. //-----------------------------------------------------------------------------
  2122. static bool DecodeFrame(const char* strFrame, int length)
  2123. {
  2124. FINFO("==IN==:{:02x$}\n", strFrame);
  2125. auto pr = [strFrame, length](const tFrameMapping& Item)
  2126. {
  2127. for (int i = 0; i < Item.NbOfCharOfHead; i++)
  2128. {
  2129. if (strFrame[i] != Item.strHead[i])
  2130. {
  2131. return false;
  2132. }
  2133. }
  2134. return true;
  2135. };
  2136. auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
  2137. if (found == arFrame.end())
  2138. {
  2139. return false;
  2140. }
  2141. lastValidResponse.store(std::chrono::steady_clock::now());
  2142. const auto& Item = *found;
  2143. auto pc = strFrame;
  2144. pc += Item.NbOfCharOfHead;
  2145. Item.fun(pc, length - Item.NbOfCharOfHead);
  2146. return true;
  2147. }
  2148. //-----------------------------------------------------------------------------
  2149. // GetIODriver & CreateIODriver
  2150. //-----------------------------------------------------------------------------
  2151. static nsGEN::HaoWeiDriver gIODriver;
  2152. extern "C" CCOS::Dev::IODriver * GetIODriver() // 返回静态对象的引用, 调用者不能删除 !
  2153. {
  2154. return &gIODriver;
  2155. }
  2156. extern "C" CCOS::Dev::IODriver * CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 !
  2157. {
  2158. return new nsGEN::HaoWeiDriver();
  2159. }