CCOS.Dev.Generator.HaoWei.cpp 74 KB

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