1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500 |
- // CCOS.Dev.GEN.HaoWei.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include <assert.h>
- #include <functional>
- #include <unordered_map>
- #include <fstream>
- #include <filesystem>
- #include <set>
- #include "LogicDevice.h"
- #include "Helper.JSON.hpp"
- #include "LogLocalHelper.h"
- #include "Log4CPP.h"
- #include "CCOS.Dev.Generator.HaoWei.h"
- using namespace std::placeholders;
- using namespace CCOS::Dev::Detail::Generator;
- namespace nsGEN = CCOS::Dev::Detail::Generator;
- static const int msTimeOut_Lock = 500;
- static const auto COM_SCFDllName = "libSerialSCF.so";
- static const auto TCP_SCFDllName = "libTcpipSCF.so";
- #define Sleep(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms))
- //-----------------------------------------------------------------------------
- // HaoWeiDevice
- //-----------------------------------------------------------------------------
- // 储存的点值
- 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 };
- 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 };
- 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 };
- 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 };
- 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 };
- 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 };
- atomic<int> nsGEN::HaoWeiDevice::m_iLoopTime = HaoWei_LoopDefTime;
- atomic<bool> nsGEN::HaoWeiDevice::m_bExtraFlag = false; // 定义静态成员变量
- std::atomic<std::chrono::steady_clock::time_point> lastValidResponse;
- constexpr auto TIMEOUT = std::chrono::seconds(12); // 超时阈值
- nsGEN::HaoWeiDevice::HaoWeiDevice(std::shared_ptr <IOEventCenter> center, std::shared_ptr<SCFWrapper> SCF, string configfile)
- : super(center)
- , superGen()
- , m_SCF(SCF)
- {
- assert(EventCenter);
- m_bExtraFlag = true;
- m_bMasR20 = 0;
- m_bUseEAcmd = 0;
- m_bResetActive = false;
- m_bIsConfigLoaded = false;
- m_bHasInitializedDevice = false;
- lastValidResponse = std::chrono::steady_clock::now();
- m_DoseUnit.m_KV.reset(new KVMould(0.0, 40.0, 125.0, 1.0));
- m_DoseUnit.m_MA.reset(new MAMould(0.0, 10.0, 1000.0, 0.1));
- m_DoseUnit.m_MS.reset(new MSMould(0.0, 1.0, 6300.0, 0.01));
- m_DoseUnit.m_MAS.reset(new MASMould(0.0, 0.1, 1000.0, 0.01));
- m_DoseUnit.m_Techmode.reset(new TECHMODEMould(0, 0, 2, 1));
- m_DoseUnit.m_WS.reset(new WORKSTATIONMould(1, 0, 5, 1));
- m_DoseUnit.m_Focus.reset(new FOCUSMould(1, 0, 1, 1));
- m_DoseUnit.m_AECField.reset(new AECFIELDMould(0, 0, 111, 1));
- m_DoseUnit.m_AECFilm.reset(new AECFILMMould(0, 0, 2, 1));
- m_DoseUnit.m_AECDensity.reset(new AECDENSITYMould(0, -4, 4, 1));
- m_DoseUnit.m_HE.reset(new TUBEHEATMould(0, 0, 100, 1));
- m_DoseUnit.m_PostKV.reset(new POSTKVMould(0.0, 40.0, 120.0, 1.0));
- m_DoseUnit.m_PostMA.reset(new POSTMAMould(0.0, 10.0, 1000.0, 0.1));
- m_DoseUnit.m_PostMS.reset(new POSTMSMould(0.0, 1.0, 10000.0, 0.01));
- m_DoseUnit.m_PostMAS.reset(new POSTMASMould(0.0, 0.5, 1000.0, 0.01));
- m_DoseUnit.m_GenSynState.reset(new GENSYNSTATEMould(0, AttrKey::GENERATOR_SYNC_ERR, AttrKey::GENERATOR_SYNC_MAX, 1));
- m_DoseUnit.m_GenState.reset(new GENSTATEMould(0, AttrKey::GENERATOR_STATUS_SHUTDOWN, AttrKey::GENERATOR_STATUS_MAX, 1));
- m_DoseUnit.m_GenTotalExpNumber.reset(new TOTALEXPNUMMould(0, 0, 9999, 1));
- m_DoseUnit.m_GenTotalAcqTimes.reset(new TOTALACQTIMESMould(0, 0, 9999, 1));
- m_DoseUnit.m_GenTubeCoolWaitTimes.reset(new TUBECOOLTIMEMould(0, 0, 9999, 1));
- m_DoseUnit.m_GenTubeOverLoadNumber.reset(new TUBEOVERLOADNUMMould(0, 0, 9999, 1));
- m_DoseUnit.m_GenCurrentExpNumber.reset(new CUREXPNUMMould(0, 0, 9999, 1));
- m_DoseUnit.m_ExpMode.reset(new EXPMODEMould(AttrKey::EXPMODE_TYPE::Single));
- m_DoseUnit.m_FrameRate.reset(new FRAMERATEMould(0, 0, 16, 1));
- m_MSGUnit.reset(new nsDetail::MSGUnit(center, nsGEN::GeneratorUnitType));
- m_DAP.reset(new DevDAP::DOSEMould(0.0, 0.0, 1000.0, 0.01));
- m_DoseUnit.m_FLMode.reset(new FLUModeMould(AttrKey::GENERATOR_FLUMode::GENERATOR_FLMODE_NOTFLU));
- m_DoseUnit.m_FLIntTime.reset(new FLUIntTimeMould(0.0, 0.0, 100.0, 0.1));
- m_DoseUnit.m_FLAccTime.reset(new FLAccTimeMould(0.0, 0.0, 999.0, 0.1));
- m_DoseUnit.m_FLKV.reset(new FLUKVMould(0, 40, 125, 1));
- m_DoseUnit.m_FLMS.reset(new FLUMSMould(10.0, 10.0, 999999.0, 0.01));
- m_DoseUnit.m_FLMA.reset(new FLUMAMould(0.5, 0.5, 99.0, 0.1));
- m_DoseUnit.m_ABSStatus.reset(new FLUABSStatusMould(0, 0, 2, 1));
- m_DoseUnit.m_PPS.reset(new PPSMould(0.5, 0.5, 30, 0.1));
- m_DoseUnit.m_DoseLevel.reset(new FLUDoseLevelMould(0, 0, 2, 1));
- m_DoseUnit.m_Curve.reset(new FLUCurveMould(0, 0, 3, 1));
- /*string strErrConfig = GetProcessDirectory() + R"(\OEMDrivers\Generator\HaoWei\ErrorWarnInfo.json)";
- m_DeviceErrorHandler.reset(new DeviceErrorHandler(center, nsGEN::GeneratorUnitType, strErrConfig));*/
- OnCallBack();
- Register();
- HWSend("ST?");
- StartHardwareStatusThread();
- }
- nsGEN::HaoWeiDevice::~HaoWeiDevice()
- {
- m_bExtraFlag = false;
- if (m_pHardwareStatusThread.joinable()) {
- m_pHardwareStatusThread.join();
- }
- FINFO("\n===============log end ===================\n");
- }
- std::string nsGEN::HaoWeiDevice::GetGUID() const
- {
- FINFO("\n===============GetGUID : {$} ===================\n", GeneratorUnitType);
- return GeneratorUnitType;
- }
- void nsGEN::HaoWeiDevice::Register()
- {
- auto Disp = m_Dispatch.Lock().As();
- superGen::Register(Disp);
- superGen::RegisterRAD(Disp);
- superGen::RegisterAEC(Disp);
- superGen::RegisterExpEnable(Disp);
- superGen::RegisterGeneratortoSyncStatus(Disp);
- superGen::RegisterFluoro(Disp);
- Disp->Get.Push(m_MSGUnit->GetKey().c_str(), [this](std::string& out) { out = m_MSGUnit->JSGet(); return RET_STATUS::RET_SUCCEED; });
- auto fun_Clear_DAP = [this](auto a, auto&)
- {
- return Clear_DAP();
- };
- Disp->Action.Push("Clear_DAP", fun_Clear_DAP);
- auto fun_GetValue_DAP = [this](auto a, auto& b)
- {
- float value = 0;
- RET_STATUS ret = GetValue_DAP(value);
- b = ToJSON(value);
- return ret;
- };
- Disp->Action.Push("GetValue_DAP", fun_GetValue_DAP);
- auto fun_StartMove = [this](auto a,auto& b)
- {
- return StartMove();
- };
- Disp->Action.Push("StartMove", fun_StartMove);
- auto fun_EndMove = [this](auto a, auto& b)
- {
- return EndMove();
- };
- Disp->Action.Push("EndMove", fun_EndMove);
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncKV()
- {
- FINFO("KV value before calling IncKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str());
- /*if (!m_DoseUnit.m_KV->CanInc())
- {
- m_DeviceErrorHandler->ParseAndReport("ECOM_KVMAX");
- return RET_STATUS::RET_SUCCEED;
- }*/
- return HWSend("KV+");
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecKV()
- {
- FINFO("KV value before calling DecKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str());
- /*if (!m_DoseUnit.m_KV->CanDec())
- {
- m_DeviceErrorHandler->ParseAndReport("ECOM_KVMIN");
- return RET_STATUS::RET_SUCCEED;
- }*/
- return HWSend("KV-");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetKV(float value)
- {
- FINFO("KV value before calling SetKV: {$}\n", m_DoseUnit.m_KV->JSGet().c_str());
- //if (!m_DoseUnit.m_KV->Verify(value)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "KV%03d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncMA()
- {
- FINFO("MA value before calling IncMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str());
- //if (!m_DoseUnit.m_MA->CanInc()) return RET_STATUS::RET_SUCCEED;
- if (!IsMAMSOperationAllowed("inc MA"))
- return RET_STATUS::RET_FAILED;
- return HWSend("MA+");
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecMA()
- {
- FINFO("MA value before calling DecMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str());
- //if (!m_DoseUnit.m_MA->CanDec()) return RET_STATUS::RET_SUCCEED;
- if (!IsMAMSOperationAllowed("dec MA"))
- return RET_STATUS::RET_FAILED;
- return HWSend("MA-");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetMA(float value)
- {
- FINFO("MA value before calling SetMA: {$}\n", m_DoseUnit.m_MA->JSGet().c_str());
- if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
- int index = 0;
- if (m_bMasR20)
- {
- index = FindClosestIndex(R20_MA, static_cast<int>(value));
- }
- else
- {
- index = FindClosestIndex(R10_MA, static_cast<int>(value));
- }
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "MA%02d", index + 1); // index+1 to match the original requirement
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncMS()
- {
- FINFO("MS value before calling IncMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str());
- /*if (!m_DoseUnit.m_MS->CanInc())
- {
- int Level = 1;
- m_MSGUnit->AddWarnMessage("HaoWei_WARN", Level, "Generator MS Limit");
- return RET_STATUS::RET_SUCCEED;
- }*/
- if (!IsMAMSOperationAllowed("inc MS"))
- return RET_STATUS::RET_FAILED;
- return HWSend("MS+");
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecMS()
- {
- FINFO("MS value before calling DecMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str());
- //if (!m_DoseUnit.m_MS->CanDec()) return RET_STATUS::RET_SUCCEED;
- if (!IsMAMSOperationAllowed("dec MS"))
- return RET_STATUS::RET_FAILED;
- return HWSend("MS-");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetMS(float value)
- {
- FINFO("MS value before calling SetMS: {$}\n", m_DoseUnit.m_MS->JSGet().c_str());
- //if (!m_DoseUnit.m_MA->Verify(value)) return RET_STATUS::RET_SUCCEED;
- int index = 0;
- if (m_bMasR20)
- {
- index = FindClosestIndex(R20_MS, static_cast<int>(value));
- }
- else
- {
- index = FindClosestIndex(R10_MS, static_cast<int>(value));
- }
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "MS%02d", index + 1); // index+1 to match the original requirement
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncMAS()
- {
- FINFO("MAS value before calling IncMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str());
- //if (!m_DoseUnit.m_MAS->CanInc()) return RET_STATUS::RET_SUCCEED;
- if (!m_DoseUnit.m_MS->CanInc())
- {
- int Level = 1;
- m_MSGUnit->AddWarnMessage("HaoWei_WARN", Level, "Generator MS Limit");
- return RET_STATUS::RET_SUCCEED;
- }
- if (!IsMASOperationAllowed("inc MAS"))
- return RET_STATUS::RET_FAILED;
- return HWSend("MX+");
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecMAS()
- {
- FINFO("MAS value before calling DecMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str());
- //if (!m_DoseUnit.m_MAS->CanDec()) return RET_STATUS::RET_SUCCEED;
- if (!IsMASOperationAllowed("dec MAS"))
- return RET_STATUS::RET_FAILED;
- return HWSend("MX-");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetMAS(float value)
- {
- FINFO("MAS value before calling SetMAS: {$}\n", m_DoseUnit.m_MAS->JSGet().c_str());
- //if (!m_DoseUnit.m_MAS->Verify(value)) return RET_STATUS::RET_SUCCEED;
- int index = 0;
- if (m_bMasR20)
- {
- index = FindClosestIndex(R20_MAS, static_cast<int>(value));
- }
- else
- {
- index = FindClosestIndex(R10_MAS, static_cast<int>(value));
- }
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "MX%02d", index + 1); // index+1 to match the original requirement
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetTechmode(int value)
- {
- FINFO("Techmode value before calling SetTechmode: {$}\n", m_DoseUnit.m_Techmode->JSGet().c_str());
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "TE%01d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFocus(int value)
- {
- FINFO("Focus value before calling SetFocus: {$}\n", m_DoseUnit.m_Focus->JSGet().c_str());
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FO%01d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetAECDensity(int value)
- {
- if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_AEC)
- return RET_STATUS::RET_FAILED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FN%01d", (int)(value + 8));
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetAECField(int value)
- {
- if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_AEC)
- return RET_STATUS::RET_FAILED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FI%03d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetAECFilm(int value)
- {
- if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_AEC)
- return RET_STATUS::RET_FAILED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FS%03d", (int)value);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetWS(const string value)
- {
- int tempws = 0;
- if (value == "Table") tempws = (int)m_GenConfig["WSTable"];
- else if (value == "Wall") tempws = (int)m_GenConfig["WSWall"];
- else if (value == "Direct") tempws = (int)m_GenConfig["WSConventional"];
- else if (value == "Free") tempws = (int)m_GenConfig["WSFree"];
- else if (value == "Tomo") tempws = (int)m_GenConfig["WSTomo"];
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "WS%01d", tempws);
- return HWSend(temp);
- }
- string nsGEN::HaoWeiDevice::WSUI2Gen(int nUIWS)
- {
- string strWS = "";
- try
- {
- if (nUIWS == AttrKey::GENWS_TYPE::TABLE) //lying: cross mode
- {
- strWS = m_GenConfig["WSTable"].encode();
- }
- else if (nUIWS == AttrKey::GENWS_TYPE::WALL) //standing mode
- {
- strWS = m_GenConfig["WSWall"].encode();
- }
- else if (nUIWS == AttrKey::GENWS_TYPE::FREE_TABLE) //standing mode
- {
- strWS = m_GenConfig["WSFree"].encode();
- }
- else if (nUIWS == AttrKey::GENWS_TYPE::TOMO) //standing mode
- {
- strWS = m_GenConfig["WSTOMO"].encode();
- }
- else if (nUIWS == AttrKey::GENWS_TYPE::CONVENTIONAL) //standing mode
- {
- strWS = m_GenConfig["WSConventional"].encode();
- }
- }
- catch (ResDataObjectExption& exp)
- {
- FERROR("Get configuration failed, {$}\n", exp.what());
- }
- FINFO("Set WS: {$},Generator workstaion: {$}\n", nUIWS, strWS);
- if (strWS == "")
- {
- strWS = "Table";
- }
- return strWS;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetAPR(const _tAPRArgs& t)
- {
- m_t = t;
- FINFO("*********************Enter SetAPR*********************");
- 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);
- //2
- SetKV(t.fKV);
- //3
- SetFocus(t.nFocus);
- //4
- if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_AEC)//aec
- {
- SetTechmode(t.nTechmode);
- SetAECField(t.nAECField);
- SetAECDensity(t.nAECDensity);
- SetMA(t.fMA);
- SetMS(t.fMS);
- }
- else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_MAS)//2p
- {
- SetTechmode(t.nTechmode);
- SetMAS(t.fMAS);
- }
- else if (t.nTechmode == AttrKey::TECHMODE_V2TYPE::ET_TIME)//3p
- {
- SetTechmode(t.nTechmode);
- SetMA(t.fMA);
- SetMS(t.fMS);
- }
- HWSend("RS?");
- FINFO("*********************Leave SetAPR*********************");
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::QueryHE(int& value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::QueryPostKV(float& value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::QueryPostMA(float& value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::QueryPostMS(float& value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::QueryPostMAS(float& value)
- {
- value = m_DoseUnit.m_PostMAS->Get();
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::Clear_DAP()
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::GetValue_DAP(float& value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::InitDevice()
- {
- HWSend("AC0");
- HWSend("SF0");
- HWSend("RS?");
- HWSend("FO?");
- GetSoftwareVersion();
- HWSend("GC?");
- SetPriorityCoefficient(m_bMasR20);
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::StartMove()
- {
- FINFO("Enter startMove");
- FINFO("end startmove");
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::EndMove()
- {
- FINFO("Enter endmove");
- FINFO("end EndMove");
- return RET_STATUS::RET_SUCCEED;
- }
- int CCOS::Dev::Detail::Generator::HaoWeiDevice::GetGenState()
- {
- const int currentState = m_DoseUnit.m_GenState->Get();
- if (currentState == 0)
- {
- m_DeviceErrorHandler->ParseAndReport("ECOM_CommLost");
- }
- return currentState;
- }
- int CCOS::Dev::Detail::Generator::HaoWeiDevice::LoadConfig(string configfile)
- {
- FINFO("=====================LoadConfig=========================");
- // 检查文件是否存在
- std::ifstream file(configfile);
- if (!file) {
- // 文件不存在,直接返回空的Connection对象
- FINFO("Config file does not exist: {$}", configfile.c_str());
- return -1;
- }
- if (m_bIsConfigLoaded)
- {
- FINFO("Configuration already loaded.");
- return 0;
- }
- m_strConfigPath = configfile;
- ResDataObject temp;
- temp.loadFile(m_strConfigPath.c_str());
- m_GenConfig = temp["CONFIGURATION"];
- TransJsonText(m_GenConfig);
- if (m_GenConfig.GetKeyCount("R20Enable") > 0)
- {
- m_bMasR20 = (bool)m_GenConfig["R20Enable"];
- }
- HWSend("ST?");
- m_bIsConfigLoaded = true;
- return 0;
- }
- int nsGEN::HaoWeiDevice::SetPriorityCoefficient(int nCoefficient)
- {
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "GC%01d", nCoefficient);
- return HWSend(temp);
- }
- int CCOS::Dev::Detail::Generator::HaoWeiDevice::GetSoftwareVersion()
- {
- FINFO("Enter GetSoftwareVersion...\n");
- return HWSend("GR?");
- }
- int CCOS::Dev::Detail::Generator::HaoWeiDevice::SetPulseSyncMode(int nMode)
- {
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FMM%01d", nMode);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SimulateError(std::string Error)
- {
- FINFO("Enter SimulateError...{$} \n", Error.c_str());
- std::string type = m_DeviceErrorHandler->ParseAndReport(Error);
- if (type == "error")
- {
- FINFO("type == error");
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR))
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- }
- return RET_STATUS::RET_SUCCEED;
- }
- int nsGEN::HaoWeiDevice::FindClosestIndex(const std::vector<int>& values, int target)
- {
- auto it = std::lower_bound(values.begin(), values.end(), target);
- if (it == values.end()) {
- return values.size() - 1;
- }
- int index = std::distance(values.begin(), it);
- if (index > 0 && std::abs(values[index - 1] - target) <= std::abs(values[index] - target)) {
- return index - 1;
- }
- return index;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetGenSynState(int value)
- {
- FINFO("Enter SetGenSynState:[{$}]", value);
- switch (value)
- {
- case AttrKey::GENERATOR_RAD_OFF:
- {
-
- }break;
- case AttrKey::GENERATOR_RAD_PREPARE:
- {
-
- }break;
- case AttrKey::GENERATOR_RAD_READY:
- {
- }break;
- case AttrKey::GENERATOR_RAD_XRAYON:
- {
-
- }break;
- case AttrKey::GENERATOR_RAD_XRAYOFF:
- {
-
- }break;
- case AttrKey::GENERATOR_FLU_OFF:
- {
-
- }break;
- case AttrKey::GENERATOR_FLU_READY:
- {
-
- }break;
- case AttrKey::GENERATOR_FLU_XRAYON:
- {
- int fluMode = m_DoseUnit.m_FLMode->Get();
- FINFO("SetGenSynState: current FluMode[{$}]", fluMode);
- switch (fluMode)
- {
- case AttrKey::GENERATOR_FLMODE_NOTFLU:
- break;
- case AttrKey::GENERATOR_FLMODE_CF:
- case AttrKey::GENERATOR_FLMODE_HCF:
- {
-
- }break;
- case AttrKey::GENERATOR_FLMODE_PF:
- case AttrKey::GENERATOR_FLMODE_HPF:
- {
-
- }break;
- break;
- case AttrKey::GENERATOR_FLMODE_MAX:
- break;
- default:
- break;
- }
- }break;
- case AttrKey::GENERATOR_FLU_XRAYOFF:
- {
-
- }break;
- default:
- break;
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetGenState(int value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetExpMode(std::string value)
- {
- FINFO("Enter SetExpMode...{$}",value);
- m_DoseUnit.m_ExpMode->Update(value);
- if (!m_DeviceErrorHandler->HasActiveErrors())
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- else
- {
- m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR);
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFLFMode(std::string value)
- {
- FINFO("Enter SetFLFMode...,FLFMode:{$} \n", value.c_str());
- if (value == "CF")
- {
- m_DoseUnit.m_FLMode->Update(1);
- HWSend("FMF0");
- }
- else if (value == "PF")
- {
- m_DoseUnit.m_FLMode->Update(2);
- HWSend("FMF1");
- }
- else if (value == "HLF")
- {
- m_DoseUnit.m_FLMode->Update(2);
- HWSend("FMF1");
- }
- else
- {
- FINFO("other FluMode : {$}", value.c_str());
- }
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetAPF(const _tAPFArgs& t)
- {
- FINFO("APF:FLKV={$},FLMA={$},PPS={$},WS={$},FLuType={$},ABSMode={$},DoseLever={$}", t.nFLKV, t.fFLMA, t.nPPS, t.nWS, t.nFluMode, t.nABSMode, t.nDoseLever);
- SetFluKV(t.nFLKV);
- SetFluMA(t.fFLMA);
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncFluKV()
- {
- FINFO("FluKV value before calling IncFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str());
- //if (!m_DoseUnit.m_FLKV->CanInc()) return RET_STATUS::RET_SUCCEED;
- return HWSend("FKV+");
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecFluKV()
- {
- FINFO("FluKV value before calling DecFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str());
- // (!m_DoseUnit.m_FLKV->CanDec()) return RET_STATUS::RET_SUCCEED;
- return HWSend("FKV-", 4);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFluKV(float value)
- {
- FINFO("FluKV value before calling SetFluKV: {$}\n", m_DoseUnit.m_FLKV->JSGet().c_str());
- //if (!m_DoseUnit.m_FLKV->Verify(value)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FKV%03d", (int)value);
- return HWSend(temp, strlen(temp));
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncFluMA()
- {
- FINFO("FluMA value before calling IncFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str());
- //if (!m_DoseUnit.m_FLMA->CanInc()) return RET_STATUS::RET_SUCCEED;
- return HWSend("FMA+");
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecFluMA()
- {
- FINFO("FluMA value before calling DecFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str());
- //if (!m_DoseUnit.m_FLMA->CanDec()) return RET_STATUS::RET_SUCCEED;
- return HWSend("FMA-");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFluMA(float value)
- {
- FINFO("FluMA value before calling SetFluMA: {$}\n", m_DoseUnit.m_FLMA->JSGet().c_str());
- if (!m_DoseUnit.m_FLMA->Verify(value)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FMA%04d", (int)(value * 10));
- return HWSend(temp, strlen(temp));
- }
- RET_STATUS nsGEN::HaoWeiDevice::INCPPS()
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::DECPPS()
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetPPS(float value)
- {
- //if (!m_DoseUnit.m_PPS->Verify(value)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "FMN%02f", value);
- return HWSend(temp, strlen(temp));
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetABSMode(int nMode)
- {
- if (!m_DoseUnit.m_ABSStatus->Verify(nMode)) return RET_STATUS::RET_SUCCEED;
- char temp[50] = { 0 };
- FINFO("SetABSMode[{$}] \n", nMode);
- snprintf(temp, sizeof(temp), "IBS%1d", (int)nMode);
- return HWSend(temp, strlen(temp));
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetABSCurve(int curveNum)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::IncABSCurve()
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::DecABSCurve()
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::GetABSCurve()
- {
- return RET_STATUS::RET_SUCCEED;
- }
- float nsGEN::HaoWeiDevice::GetFluIntTimer()
- {
- return m_DoseUnit.m_FLIntTime->Get();
- }
- float nsGEN::HaoWeiDevice::GetFluAccTimer()
- {
- return m_DoseUnit.m_FLAccTime->Get();
- }
- RET_STATUS nsGEN::HaoWeiDevice::ResetFluTimer(int ntype)
- {
- FINFO("ReSetFluAccTimer:[{$}] \n", ntype);
- return HWSend("FTC");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFluPre(int value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFluEXP(int value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFluMode(std::string value)
- {
- FINFO("Enter SetFLFMode...{$} \n", value.c_str());
- if (value == "CF")
- {
- m_DoseUnit.m_FLMode->Update(1);
- return HWSend("FLF1", 4);
- }
- else if (value == "PF")
- {
- m_DoseUnit.m_FLMode->Update(2);
- return HWSend("FLF2", 4);
- }
- else
- {
- FINFO("other FluMode : {$}", value.c_str());
- return RET_STATUS::RET_SUCCEED;
- }
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFluDoseLever(int value)
- {
- return RET_STATUS::RET_SUCCEED;
- }
- void CCOS::Dev::Detail::Generator::HaoWeiDevice::UpdateLimits(const std::string& key, float& currentValue, float defaultValue)
- {
- if (m_GenConfig.GetKeyCount(key.c_str()) > 0)
- {
- currentValue = static_cast<float>(m_GenConfig[key.c_str()]);
- }
- else
- {
- currentValue = defaultValue;
- }
- }
- void CCOS::Dev::Detail::Generator::HaoWeiDevice::UpdateLimitsInt(const std::string& key, int& currentValue, int defaultValue)
- {
- if (m_GenConfig.GetKeyCount(key.c_str()) > 0)
- {
- currentValue = static_cast<int>(m_GenConfig[key.c_str()]);
- }
- else
- {
- currentValue = defaultValue;
- }
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetEXAMMode(std::string value)
- {
- //EXAMMODE_TYPE::MANUAL
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::ActiveSyncMode(_tSyncModeArgs value)
- {
- FINFO("value.strSyncMode: {$}, value.strSyncValue: {$}, value.strWS: {$} \n", value.strSyncMode, value.strSyncValue, value.strWS);
- int nSyncModeValue = atoi(value.strSyncValue.c_str());
- char temp[50] = { 0 };
- snprintf(temp, sizeof(temp), "WS%01d", nSyncModeValue);
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetFrameRate(float frameRate)
- {
- m_DoseUnit.m_FrameRate->Update(frameRate); //this variable should be set when in oncallback.
-
- char temp[50]{ 0 };
- snprintf(temp, sizeof(temp), "FLS%03d", int(frameRate*10));
- return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetRPS(int rps)
- {
- return RET_STATUS::RET_SUCCEED;
- //char temp[50]{ 0 };
- //snprintf(temp, sizeof(temp), "RPS%03d", rps * 10);
- //return HWSend(temp);
- }
- RET_STATUS nsGEN::HaoWeiDevice::RefreshData()
- {
- HWSend("RS?");
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetExpEnable()
- {
- FINFO("SetExpEnable in ...\n");
- if (!m_DeviceErrorHandler->HasActiveErrors())
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- else
- {
- m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR);
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- }
- return RET_STATUS::RET_SUCCEED;
- //return HWSend("EXB1");
- }
- RET_STATUS nsGEN::HaoWeiDevice::SetExpDisable()
- {
- FINFO("SetExpDisable in...\n");
- if (!m_DeviceErrorHandler->HasActiveErrors())
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- else
- {
- m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR);
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- }
- FINFO("SetExpDisable... {$}\n", m_DoseUnit.m_GenState->JSGet().c_str());
- return RET_STATUS::RET_SUCCEED;
- }
- RET_STATUS nsGEN::HaoWeiDevice::Reset()
- {
- m_bResetActive = true;
- FINFO("RESET in...\n");
- // 清除错误和警告
- m_DeviceErrorHandler->ClearAllErrors();
- m_DeviceErrorHandler->ClearAllWarnings();
- // 发送复位命令
- return HWSend("CLR0");
- }
- // 辅助函数:处理连接错误并更新发生器状态
- RET_STATUS nsGEN::HaoWeiDevice::HandleConnectionError(const char* errorMsg)
- {
- FERROR(errorMsg);
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN))
- {
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- FINFO("Generator status updated to SHUTDOWN");
- }
- return RET_STATUS::RET_FAILED;
- }
- // 辅助函数:检查MA/MS操作是否允许(MAS模式下不允许)
- bool nsGEN::HaoWeiDevice::IsMAMSOperationAllowed(const char* operationName)
- {
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- FWARN("Techmode is MAS, cannot {$}", operationName);
- return false;
- }
- return true;
- }
- // 辅助函数:检查MAS操作是否允许(非MAS模式下不允许)
- bool nsGEN::HaoWeiDevice::IsMASOperationAllowed(const char* operationName)
- {
- if (m_DoseUnit.m_Techmode->Get() != AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- FWARN("Techmode is not MAS, cannot {$}", operationName);
- return false;
- }
- return true;
- }
- RET_STATUS nsGEN::HaoWeiDevice::HWSend(const char* strCommand, int lengh, bool reSend, int nTimeOut)
- {
- if (!m_SCF) {
- return HandleConnectionError("Failed - Serial communication interface not initialized");
- }
- if (!m_SCF->IsConnected())
- {
- return HandleConnectionError("Failed - Device not connected");
- }
- // 使用传入的lengh参数,如果为0则用strlen计算
- int cmdLen = (lengh > 0) ? lengh : strlen(strCommand);
- // 检查缓冲区大小,数据包 = 命令 + ETX(1字节) + CheckSum(1字节)
- const int maxCmdLen = 256; // 增大缓冲区以支持更长的命令
- if (cmdLen > maxCmdLen - 2)
- {
- FERROR("Command too long: {$} bytes, max allowed: {$} bytes\n", cmdLen, maxCmdLen - 2);
- return RET_STATUS::RET_FAILED;
- }
- char strSendCommand[maxCmdLen] = { 0 };
- // 计算校验和
- int tmpSum = 0;
- for (int i = 0; i < cmdLen; i++)
- {
- tmpSum += (unsigned char)strCommand[i];
- }
- char checkSum = char(tmpSum + 3);
- // 构建数据包:命令 + ETX + CheckSum
- memcpy(strSendCommand, strCommand, cmdLen);
- strSendCommand[cmdLen] = 0x03;
- strSendCommand[cmdLen + 1] = checkSum;
- strSendCommand[cmdLen + 2] = 0x00;
- int totalLen = cmdLen + 3; // 实际发送的总长度
- // 打印完整数据包的十六进制和ASCII(含ETX和CheckSum)便于调试
- std::string hexStr;
- std::string asciiStr;
- hexStr.reserve(totalLen * 3);
- asciiStr.reserve(cmdLen + 10);
- int printLen = std::min(totalLen, 32); // 最多打印前32字节
- for (int i = 0; i < printLen; i++) {
- char buf[4];
- snprintf(buf, sizeof(buf), "%02X ", (unsigned char)strSendCommand[i]);
- hexStr += buf;
- }
- if (totalLen > 32) hexStr += "...";
- // 提取可打印的ASCII字符串(命令部分,不包括ETX和CheckSum)
- for (int i = 0; i < cmdLen; i++) {
- char ch = strCommand[i];
- if (ch >= 32 && ch <= 126) { // 可打印字符
- asciiStr += ch;
- }
- else {
- asciiStr += '.'; // 不可打印字符用'.'表示
- }
- }
- asciiStr += " + ETX + CKS"; // 标注后面的控制字符
- FINFO("==OUT== Packet[{$}]: HEX=[{$}] ASCII=[{$}]\n", totalLen, hexStr.c_str(), asciiStr.c_str());
- // 发送数据包,支持重发机制
- int maxRetry = reSend ? 2 : 1; // 如果reSend为true,最多重试2次
- unsigned int retLength = 0;
- for (int retry = 0; retry < maxRetry; retry++)
- {
- if (retry > 0)
- {
- FWARN("Retry sending packet, attempt {$}/{$}\n", retry + 1, maxRetry);
- Sleep(100); // 重试前短暂延时
- }
- if (m_SCF->Lock(1000) == WAIT_OBJECT_0)
- {
- // 使用实际长度totalLen,而不是strlen
- int result = m_SCF->SendPacket(strSendCommand, totalLen, nTimeOut, retLength);
- m_SCF->Unlock();
- if (result == SCF_SUCCEED)
- {
- if (retry > 0)
- {
- FINFO("Send succeeded after {$} retries\n", retry);
- }
- return RET_STATUS::RET_SUCCEED;
- }
- else
- {
- FERROR("SendPacket failed, result={$}, retry={$}/{$}\n", result, retry + 1, maxRetry);
- }
- }
- else
- {
- FERROR("Lock failed, retry={$}/{$}\n", retry + 1, maxRetry);
- }
- }
- FERROR("Send failed after {$} attempts\n", maxRetry);
- return RET_STATUS::RET_FAILED;
- }
- //-----------------------------------------------------------------------------
- // ProcessCmd
- //-----------------------------------------------------------------------------
- void nsGEN::HaoWeiDevice::FireNotify(std::string key, std::string content)
- {
- EventCenter->OnNotify(1, key, content);
- }
- struct tFrameMapping
- {
- static const int MaxLen = 5; // 前缀不能超超过 5 个字符 !
- using cbFun = std::function <void(const char*, int)>;
- char strHead[MaxLen];
- int NbOfCharOfHead;
- cbFun fun;
- tFrameMapping(const char* str, int len, cbFun f)
- {
- assert(len < MaxLen); //len最大只能是4
- //strHead[0] = 0x02; //STX ----------->note : no package header
- for (int i = 0; i < len; i++) //给strHead赋值
- strHead[i] = str[i];
- NbOfCharOfHead = len;
- fun = f;
- }
- };
- static std::list <tFrameMapping> arFrame;
- static bool DecodeFrame(const char* strFrame, int length);
- void nsGEN::HaoWeiDevice::OnCallBack()
- {
- auto HWNotProcess = [](const char* value, int length) -> void
- {
- printf("\n This commands didn't need to process!\n");
- FINFO("\n This commands didn't need to process!\n");
- };
- //==IN==:KV070 MA00320 MS00063 MX00036 sometimes : this long str appear. so must deal with it
- auto HWKV = [this](const char* value, int length) -> void
- {
- assert(value);
- FINFO("hwkv={$},len={$}",value, length);
- int tmpkv = atoi(value);
- m_DoseUnit.m_KV->Update(tmpkv);
- FireNotify(AttrKey::KV, m_DoseUnit.m_KV->JSGet());
- };
- //==IN==:TU0 WS1 FO0 ET0 FI010 FS001 FN 0 HE000
- auto HWTU = [this](const char* value, int length) -> void
- {
- assert(value);
- FINFO("recv TU={$},len={$}", value,length);
- };
- auto HWEC = [this](const char* value, int length) -> void
- {
- assert(value);
- FINFO("recv EC{$}",value);
- };
- auto HWST = [this](const char* value, int length) -> void
- {
- assert(value);
- int genStatus = atoi(value);
- // 获取状态码的首位数字,用来判断阶段
- int statusPrefix = genStatus / 10;
- // 根据阶段处理不同的状态
- switch (statusPrefix)
- {
- case 10:
- // 初始化阶段 (101, 102, 103)
- FINFO("Get Gen Status:GENSTATE {$} -> (Initialization Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_INIT))
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- break;
- case 20:
- // 待机阶段 (200)
- if (!m_bHasInitializedDevice)
- {
- InitDevice();
- m_bHasInitializedDevice = true;
- }
- FINFO("Get Gen Status:GENSTATE {$} -> (Standby Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_STANDBY))
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- break;
- case 30:
- // 拍片准备阶段 (300)
- FINFO("Get Gen Status:GENSTATE {$} -> (Film Prep Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 40:
- // 透视准备阶段 (400)
- FINFO("Get Gen Status:GENSTATE {$} -> (Fluoroscopy Prep Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 50:
- // 曝光阶段 (500)
- FINFO("Get Gen Status:GENSTATE {$} -> (Exposure Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 60:
- // 透视阶段 (600)
- FINFO("Get Gen Status:GENSTATE {$} -> (Fluoroscopy Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 70:
- // 点片曝光阶段 (700)
- FINFO("Get Gen Status:GENSTATE {$} -> (Spot Exposure Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 80:
- // 故障阶段 (800)
- FINFO("Get Gen Status:GENSTATE {$} -> (Fault Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR))
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- break;
- case 90:
- // 灯丝自动校准阶段 (900)
- FINFO("Get Gen Status:GENSTATE {$} -> (Filament Auto Calibration Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 91:
- // 透视校准阶段 (910)
- FINFO("Get Gen Status:GENSTATE {$} -> (Fluoroscopy Calibration Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 92:
- // 配置阶段 (920)
- FINFO("Get Gen Status:GENSTATE {$} -> (Configuration Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 93:
- // 复位阶段 (930)
- FINFO("Get Gen Status:GENSTATE {$} -> (Reset Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- case 94:
- // 升级阶段 (940)
- FINFO("Get Gen Status:GENSTATE {$} -> (Upgrade Phase)", m_DoseUnit.m_GenState->JSGet().c_str());
- break;
- default:
- FINFO("Get Gen Status: [{$}] unknown", genStatus);
- break;
- }
- };
- auto HWMAS = [this](const char* value, int length)
- {
- assert(value);
- int index = atoi(value);
- float fmas = (m_bMasR20) ? R20_MAS[index] : R10_MAS[index];
- fmas = fmas / 100.0;
- FINFO("Current MAS:{$}", fmas);
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_TIME)
- {
- m_DoseUnit.m_MAS->Update(fmas);
- FireNotify(AttrKey::MAS, to_string(0));
- }
- else
- {
- m_DoseUnit.m_MAS->Update(fmas);
- FireNotify(AttrKey::MAS, m_DoseUnit.m_MAS->JSGet());
- }
- };
- auto HWMA = [this](const char* value, int length)
- {
- assert(value);
- int index = atoi(value);
- float fma = (m_bMasR20) ? R20_MA[index] : R10_MA[index];
- fma = fma / 10.0;
- FINFO("Current MA:{$}", fma);
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- m_DoseUnit.m_MA->Update(fma);
- FireNotify(AttrKey::MA, to_string(0));
- }
- else
- {
- m_DoseUnit.m_MA->Update(fma);
- FireNotify(AttrKey::MA, m_DoseUnit.m_MA->JSGet());
- }
- };
- auto HWMS = [this](const char* value, int length)
- {
- assert(value);
- int index = atoi(value);
- float fms = (m_bMasR20) ? R20_MS[index] : R10_MS[index];
- fms = fms / 10.0;
- FINFO("Current MS:{$}", fms);
- if (m_DoseUnit.m_Techmode->Get() == AttrKey::TECHMODE_V2TYPE::ET_MAS)
- {
- m_DoseUnit.m_MS->Update(fms);
- FireNotify(AttrKey::MS, to_string(0));
- }
- else
- {
- m_DoseUnit.m_MS->Update(fms);
- FireNotify(AttrKey::MS, m_DoseUnit.m_MS->JSGet());
- }
- };
- auto HWFocus = [this](const char* value, int length)
- {
- assert(value);
- int nfous = atoi(value);
- m_DoseUnit.m_Focus->Update(nfous);
- {
- FireNotify(AttrKey::FOCUS, m_DoseUnit.m_Focus->JSGet());
- FINFO("Current focus:{$}, FO={$}", atoi(m_DoseUnit.m_Focus->JSGet().c_str()) ? "large focus" : "small focus", m_DoseUnit.m_Focus->JSGet().c_str());
- }
- };
- auto HWTechmode = [this](const char* value, int length)
- {
- assert(value);
- int ntechmode = atoi(value);
- m_DoseUnit.m_Techmode->Update(ntechmode);
- FireNotify(AttrKey::TECHMODE, m_DoseUnit.m_Techmode->JSGet());
- switch (ntechmode)
- {
- case 0:
- FINFO("ET={$}", "mA/ms", m_DoseUnit.m_Techmode->JSGet().c_str());
- break;
- case 1:
- FINFO("ET={$}", "mAs", m_DoseUnit.m_Techmode->JSGet().c_str());
- break;
- case 2:
- FINFO("ET={$}", "AEC", m_DoseUnit.m_Techmode->JSGet().c_str());
- break;
- }
- };
- auto HWAECField = [this](const char* value, int length)
- {
- assert(value);
- int nvalue = atoi(value);
- if (m_DoseUnit.m_AECField->Update(nvalue))
- FireNotify(AttrKey::AECFIELD, m_DoseUnit.m_AECField->JSGet());
- };
- auto HWAECFilm = [this](const char* value, int length)
- {
- assert(value);
- int nvalue = atoi(value);
- if (m_DoseUnit.m_AECFilm->Update(nvalue))
- FireNotify(AttrKey::AECFILM, m_DoseUnit.m_AECFilm->JSGet());
- };
- auto HWAECDensity = [this](const char* value, int length)
- {
- assert(value);
- int nvalue = atoi(value);
- if (m_DoseUnit.m_AECDensity->Update(nvalue))
- FireNotify(AttrKey::AECDENSITY, m_DoseUnit.m_AECDensity->JSGet());
- };
- auto HWWS = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (m_DoseUnit.m_WS->Update(nValue))
- {
- FireNotify(m_DoseUnit.m_WS->GetKey(), m_DoseUnit.m_WS->JSGet());
- }
- };
- auto HWPR = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 2)
- {
- FINFO("The high voltage generator enters the exposure preparation stage。");
- HWSend("PR2");
- }
- else if (nValue == 1)
- {
- HWSend("PR1");
- m_iLoopTime = HaoWei_LoopExpTime;
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_PREPARE))
- {
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- FINFO("Generator exposure process status:GENERATOR_RAD_PREPARE");
- }
- }
- else if (nValue == 0)
- {
- HWSend("PR0");
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_OFF));
- {
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_OFF");
- FINFO("HWPR m_DoseUnit.m_GenSynState->JSGet()={$}", m_DoseUnit.m_GenSynState->JSGet().c_str());
- RefreshData();
- }
- if (m_iLoopTime == HaoWei_LoopExpTime)
- {
- if ((int)m_GenConfig["loopTime"] >= 100)
- {
- m_iLoopTime = (int)m_GenConfig["loopTime"];
- }
- else
- m_iLoopTime = HaoWei_LoopDefTime;
- FINFO("reduction loopTime[{$}]->[{$}]", HaoWei_LoopExpTime, m_iLoopTime.load());
- }
- }
- };
- auto HWXR = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 1)
- {
- HWSend("XR1");
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_READY))
- {
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_READY");
- }
- }
- else if (nValue == 2)
- {
- HWSend("XR2");
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP))
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYON))
- {
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_XRAYON");
- }
- }
- else if (nValue == 0)
- {
- HWSend("XR0");
- if(m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_RAD_XRAYOFF));
- {
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- FINFO("Generator exposure process status:{$};", "GENERATOR_RAD_XRAYOFF");
- FINFO("HWXR m_DoseUnit.m_GenSynState->JSGet()={$}", m_DoseUnit.m_GenSynState->JSGet().c_str());
- HWSend("AT?");
- }
- }
- };
- auto HWVPDOSE = [this](const char* value, int length)//mas
- {
- assert(value);
- m_DoseUnit.m_PostKV->Update(atof(value));
- FireNotify(AttrKey::POSTKV, m_DoseUnit.m_PostKV->JSGet());
- FINFO("Actual exposure parameters KV:{$}", m_DoseUnit.m_PostKV->JSGet().c_str());
- };
- auto HWAPDOSE = [this](const char* value, int length)//mas
- {
- assert(value);
- m_DoseUnit.m_PostMAS->Update(atof(value) / 10.0);
- FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet());
- FireNotify(AttrKey::POSTKV, m_DoseUnit.m_KV->JSGet());
- FINFO("Actual exposure parameters MAS:{$}", m_DoseUnit.m_PostMAS->JSGet().c_str());
- };
- auto HWATDOSE = [this](const char* value, int length)
- {
- assert(value);
- m_DoseUnit.m_PostMS->Update(atof(value) / 10.0);
- FireNotify(m_DoseUnit.m_PostMS->GetKey(), m_DoseUnit.m_PostMS->JSGet());
- m_DoseUnit.m_PostMA->Update(m_DoseUnit.m_MA->Get());
- FireNotify(m_DoseUnit.m_PostMA->GetKey(), m_DoseUnit.m_PostMA->JSGet());
- m_DoseUnit.m_PostKV->Update(m_DoseUnit.m_KV->Get());
- FireNotify(AttrKey::POSTKV, m_DoseUnit.m_PostKV->JSGet());
- m_DoseUnit.m_PostMAS->Update(m_DoseUnit.m_MAS->Get());
- FireNotify(m_DoseUnit.m_PostMAS->GetKey(), m_DoseUnit.m_PostMAS->JSGet());
- FINFO("Actual exposure parameters MS:{$}", m_DoseUnit.m_PostMS->JSGet().c_str());
- };
- auto HWDAPST = [this](const char* value, int length)
- {
- assert(value);
- int dapStatus = atoi(value);
- if (dapStatus == 1)
- {
- m_bDAPEnable = true;
- m_bAKEnable = false;
- }
- else if (dapStatus == 2)
- {
- m_bDAPEnable = false;
- m_bAKEnable = true;
- }
- else if (dapStatus == 3)
- {
- m_bDAPEnable = true;
- m_bAKEnable = true;
- }
- else
- {
- m_bDAPEnable = false;
- m_bAKEnable = false;
- }
- };
- auto HWDT = [this](const char* value, int length)
- {
- assert(value);
- int dapStatus = atoi(value);
- if (dapStatus == 0)
- {
- m_bDAPEnable = false;
- FINFO("DAP test failed");
- }
- else if (dapStatus == 1)
- {
- m_bDAPEnable = true;
- FINFO("DAP test passed");
- }
- else if (dapStatus == 2)
- {
- m_bDAPEnable = true;
- FINFO("DAP test in progress.");
- }
- };
- auto HWDAP = [this](const char* value, int length)
- {
- assert(value);
- //m_DoseUnit.->Update(atof(value));
- //FireNotify(m_DoseUnit.m_PostMS->GetKey(), m_DoseUnit.m_PostMS->JSGet());
- //SetEvent(m_hGenPostEvent);
- m_DAP->Update(atof(value)); //should push to subsystem.......
- };
- auto HWGR = [this](const char* value, int length)
- {
- assert(value);
- // 提取机器型号、功率版本和功能类型
- std::string version(value, 3); // 前3个字符为机器型号
- char functionalityChar = value[3]; // 第4个字符表示功能类型
- // 功能类型的映射
- std::string functionality;
- if (functionalityChar == '0') {
- functionality = "PR (pet photography)";
- }
- else if (functionalityChar == '1') {
- functionality = "PF (Pet fluoroscopy)";
- }
- else if (functionalityChar == '2') {
- functionality = "MR (Human medical photography)";
- }
- else if (functionalityChar == '3') {
- functionality = "MF (Human medical fluoroscopy)";
- }
- else if (functionalityChar == '4') {
- functionality = "DR (Single tooth)";
- }
- else if (functionalityChar == '5') {
- functionality = "DF (CBCT)";
- }
- else if (functionalityChar == '6') {
- functionality = "IX (industrial circle)";
- }
- else if (functionalityChar == '7') {
- functionality = "0X (other)";
- }
- else {
- functionality = "Unknown Functionality:"+ functionalityChar;
- }
- // 日志打印输出
- FINFO("Version: {$}, Functionality: {$}", version, functionality.c_str());
- };
- auto HWGV = [this](const char* value, int length)
- {
- assert(value);
- // 打印原始值
- FINFO("value: {$}", value);
- int versionNumber = atoi(value);
- // 拆解 versionNumber 为 4个部分(假设数字按版本号分割:10010 -> V1.0.0.10)
- int major = versionNumber / 10000; // 取高位,得到主版本号
- int minor = (versionNumber / 1000) % 10; // 取次高位,得到次版本号
- int patch = (versionNumber / 100) % 10; // 取中位,得到修补版本号
- int build = versionNumber % 100; // 取低位,得到构建版本号
- // 格式化并打印详细版本信息
- FINFO("Software Version: V{$}.{$}.{$}.{$}", major, minor, patch, build);
- };
- auto HWGU = [this](const char* value, int length)
- {
- assert(value);
- // 打印原始值
- FINFO("value: {$}", value);
- std::string versionCode(value, 2);
- // 定义版本类型
- std::string versionType;
- if (versionCode == "00") {
- versionType = "General Version";
- }
- else if (versionCode == "01") {
- versionType = "Custom Version";
- }
- else {
- versionType = "Unknown Version"; // 处理其他情况,可以根据需求扩展
- }
- // 输出详细的版本信息
- FINFO("Software Version: {$}, Type: {$}", value, versionType.c_str());
- };
- auto HWTR = [this](const char* value, int length)
- {
- assert(value);
- if (value)
- {
- FINFO("Start automatic exposure once, after the end of an automatic reset");
- }
- else
- {
- FINFO("Stop automatic exposure");
- }
- };
- auto HWGC = [this](const char* value, int length)
- {
- assert(value);
- if (value)
- {
- FINFO("R20");
- }
- else
- {
- FINFO("R10");
- }
- };
- auto HWER = [this](const char* value, int length)
- {
- assert(value);
- std::string errorCode = "HaoWei_ER" + std::string(value);
- if (m_DeviceErrorHandler->ParseAndReport(errorCode) == "error")
- {
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_ERROR))
- {
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- }
- }
- };
- auto HWWAR = [this](const char* value, int length)
- {
- assert(value);
- int nWran = atoi(value);
- if (nWran)
- {
- m_DeviceErrorHandler->ParseAndReport("ECOM_FluoroTimerLimitError");
- }
- };
- auto HWEHE = [this](const char* value, int length)
- {
- assert(value);
- int nhe = atoi(value);
- FINFO("HE{$}%", nhe);
- if (m_DoseUnit.m_HE->Update(nhe))
- FireNotify(m_DoseUnit.m_HE->GetKey(), m_DoseUnit.m_HE->JSGet());
- };
- //075 FLM100 FLI000 FLT006 FLF2 FLA0 FLS060 FLZ0 FLD0
- auto HWFKV = [this](const char* value, int length)
- {
- assert(value);
- m_DoseUnit.m_FLKV->Update(atof(value));
- FireNotify(AttrKey::FLUKV, m_DoseUnit.m_FLKV->JSGet());
- };
- auto HWFMA = [this](const char* value, int length)
- {
- assert(value);
- float tmpflm = atof(value) / 10.0;
- if (m_DoseUnit.m_FLMA->Update(tmpflm))
- FireNotify(AttrKey::FLUMA, m_DoseUnit.m_FLMA->JSGet());
- };
- auto HWFLS = [this](const char* value, int length)
- {
- assert(value);
- FINFO("HWFLS={$}", value);
- float tmppps = atof(value) / 10.0;
- if (m_DoseUnit.m_PPS->Update(tmppps))
- FireNotify(AttrKey::FLUPPS, m_DoseUnit.m_PPS->JSGet());
- };
- auto HWFLX = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 0)
- {
- HWSend("FLX0");
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYOFF))
- {
- FINFO("Generator exposure process status{$};", "GENERATOR_FLU_XRAYOFF");
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- }
- }
- else
- {
- HWSend("FLX1");
- if (m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_EXP))
- FireNotify(AttrKey::GENSTATE, m_DoseUnit.m_GenState->JSGet());
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_XRAYON))
- {
- FINFO("Generator exposure process status{$};", "GENERATOR_FLU_XRAYON");
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- }
- }
- };
- auto HWFLP = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (nValue == 0)
- {
- if (m_iLoopTime == HaoWei_LoopExpTime)
- {
- if ((int)m_GenConfig["loopTime"] >= 100)
- {
- m_iLoopTime = (int)m_GenConfig["loopTime"];
- }
- else
- m_iLoopTime = HaoWei_LoopDefTime;
- FINFO("reduction loopTime[{$}]->[{$}]", HaoWei_LoopExpTime, m_iLoopTime.load());
- }
- HWSend("FLP0");
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_OFF))
- {
- FINFO("脚闸抬起,发生器曝光流程状态{$};", "GENERATOR_FLU_OFF");
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- }
- RefreshData();
- }
- else
- {
- m_iLoopTime = HaoWei_LoopExpTime;
- HWSend("FLP1");
- if (m_DoseUnit.m_GenSynState->Update(AttrKey::GENERATOR_FLU_READY))
- {
- FINFO("Generator exposure process status{$};", "GENERATOR_FLU_READY");
- FireNotify(m_DoseUnit.m_GenSynState->GetKey(), m_DoseUnit.m_GenSynState->JSGet());
- }
- }
- };
- auto HWFLF = [this](const char* value, int length)
- {
- assert(value);
- FINFO("HWFLF={$};", value);
- int tmpflf = atoi(value);
- if (m_DoseUnit.m_FLMode->Update(tmpflf))
- FireNotify(AttrKey::FLUMode, m_DoseUnit.m_FLMode->JSGet());
- };
- auto HWFLA = [this](const char* value, int length)
- {
- assert(value);
- int tmpfla = atoi(value);
- if (m_DoseUnit.m_ABSStatus->Update(tmpfla))
- FireNotify(AttrKey::FLUABSStatus, m_DoseUnit.m_ABSStatus->JSGet());
- };
- auto HWFLD = [this](const char* value, int length)
- {
- assert(value);
- };
- auto HWFLC = [this](const char* value, int length)
- {
- assert(value);
- };
- auto HWFLO = [this](const char* value, int length)
- {
- assert(value);
- int nValue = atoi(value);
- if (m_DoseUnit.m_Curve->Update(nValue))
- FireNotify(AttrKey::FLUCurve, m_DoseUnit.m_Curve->JSGet());
- };
- auto HWFLW = [this](const char* value, int length)
- {
- assert(value);;
- float tmpflms = atof(value) / 100.0;
- if (m_DoseUnit.m_FLMS->Update(tmpflms))
- FireNotify(AttrKey::FLUMS, m_DoseUnit.m_FLMS->JSGet());
- };
- auto HWFLI = [this](const char* value, int length)
- {
- assert(value);
- float tmpfli = atof(value) / 10.0;
- if (m_DoseUnit.m_FLIntTime->Update(tmpfli))
- FireNotify(AttrKey::FLUIntTime, m_DoseUnit.m_FLIntTime->JSGet());
- };
- auto HWFLT = [this](const char* value, int length)
- {
- assert(value);
- float tmpflt = atof(value);
- if (m_DoseUnit.m_FLAccTime->Update(tmpflt))
- FireNotify(AttrKey::FLUAccTime, m_DoseUnit.m_FLAccTime->JSGet());
- };
- // 有部分前缀是包含关系, 长的包含短的, 例如 KVS 包含了 KV.
- // 因此长的在前面, 短的在后面
- // !!! Device 是个短寿命对象, 而 arFrame 是静态变量 !!!
- // !!! 因此, 在添加到 arFrame 之前, 务必先清零 !!!
- arFrame.clear();
-
- arFrame.push_back(tFrameMapping("KV", 2, HWKV));
- arFrame.push_back(tFrameMapping("TU", 2, HWTU));
- arFrame.push_back(tFrameMapping("MX", 2, HWMAS));
- arFrame.push_back(tFrameMapping("MA", 2, HWMA));
- arFrame.push_back(tFrameMapping("MS", 2, HWMS));
- arFrame.push_back(tFrameMapping("TE", 2, HWTechmode));
- arFrame.push_back(tFrameMapping("FO", 2, HWFocus));
- arFrame.push_back(tFrameMapping("FI", 2, HWAECField));
- arFrame.push_back(tFrameMapping("FS", 2, HWAECFilm));
- arFrame.push_back(tFrameMapping("FN", 2, HWAECDensity));
- arFrame.push_back(tFrameMapping("WS", 2, HWWS));
- arFrame.push_back(tFrameMapping("PR", 2, HWPR));
- arFrame.push_back(tFrameMapping("XR", 2, HWXR));
- arFrame.push_back(tFrameMapping("VP", 2, HWVPDOSE));
- arFrame.push_back(tFrameMapping("AP", 2, HWAPDOSE));
- arFrame.push_back(tFrameMapping("AT", 2, HWATDOSE));
- arFrame.push_back(tFrameMapping("ER", 2, HWER));
- arFrame.push_back(tFrameMapping("HE", 2, HWEHE));
- arFrame.push_back(tFrameMapping("ST", 2, HWST));
- arFrame.push_back(tFrameMapping("EC", 2, HWEC));
- arFrame.push_back(tFrameMapping("GR", 2, HWGR));
- arFrame.push_back(tFrameMapping("GV", 2, HWGV));
- arFrame.push_back(tFrameMapping("GU", 2, HWGU));
- arFrame.push_back(tFrameMapping("TR", 2, HWTR));
- arFrame.push_back(tFrameMapping("DS", 2, HWDAPST)); //dap status
- arFrame.push_back(tFrameMapping("DA", 2, HWDAP)); //dap value
- arFrame.push_back(tFrameMapping("DT", 2, HWDT)); //test dap
- arFrame.push_back(tFrameMapping("WAR", 3, HWWAR));
- arFrame.push_back(tFrameMapping("FKV", 3, HWFKV));
- arFrame.push_back(tFrameMapping("FMA", 3, HWFMA));
- arFrame.push_back(tFrameMapping("FLS", 3, HWFLS));
- arFrame.push_back(tFrameMapping("FLF", 3, HWFLF));
- arFrame.push_back(tFrameMapping("FLP", 3, HWFLP));
- arFrame.push_back(tFrameMapping("FLX", 3, HWFLX));
- arFrame.push_back(tFrameMapping("FLW", 3, HWFLW));
- arFrame.push_back(tFrameMapping("FLI", 3, HWFLI));
- arFrame.push_back(tFrameMapping("FTS", 3, HWFLT));
- arFrame.push_back(tFrameMapping("FLA", 3, HWFLA));
- arFrame.push_back(tFrameMapping("FLD", 3, HWFLD));
- arFrame.push_back(tFrameMapping("FLC", 3, HWFLD));
- arFrame.push_back(tFrameMapping("FLO", 3, HWFLO));
- }
- bool nsGEN::HaoWeiDevice::StartHardwareStatusThread()
- {
- FINFO("========================================");
- FINFO("Starting Hardware Status Thread...");
- if (!m_pHardwareStatusThread.joinable())
- {
- FINFO("Creating new hardware status thread");
- m_pHardwareStatusThread = std::thread(HardwareStatusThread, this);
- FINFO("Hardware status thread created successfully");
- FINFO("========================================");
- return true;
- }
- FWARN("Hardware status thread already running");
- FINFO("========================================");
- return false;
- }
- void nsGEN::HaoWeiDevice::HardwareStatusThread(HaoWeiDevice* pParam)
- {
- FINFO("========================================");
- FINFO("HardwareStatusThread Entry");
- FINFO("========================================");
- HaoWeiDevice* pCurGen = pParam;
- if (pCurGen == nullptr)
- {
- FERROR("HardwareStatusThread: pParam is nullptr, exiting thread");
- return;
- }
- int currtTime = pCurGen->m_iLoopTime;
- int messageIndex = 0;
- int loopCount = 0;
- FINFO("Hardware status monitoring started");
- FINFO("Initial loop time: {$} ms", currtTime);
- while (pCurGen->m_bExtraFlag) // 添加退出条件
- {
- auto now = std::chrono::steady_clock::now();
- auto last = lastValidResponse.load();
- auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - last);
- // 检查通讯超时
- if (elapsed > TIMEOUT) {
- int currentState = pCurGen->m_DoseUnit.m_GenState->Get();
- if (currentState > 0) {
- FWARN("Communication timeout: {$}s, shutting down generator", elapsed.count());
- pCurGen->m_DoseUnit.m_GenState->Update(nsGEN::AttrKey::GENERATOR_STATUS_SHUTDOWN); // 超时未响应则重置状态
- }
- }
- currtTime = pCurGen->m_iLoopTime;
- std::this_thread::sleep_for(std::chrono::milliseconds(currtTime));
- //获取消息
- //pCurGen->HWSend("ST?");
- }
- FINFO("HardwareStatusThread stop");
- }
- //-----------------------------------------------------------------------------
- // HaoWeiDriver
- //-----------------------------------------------------------------------------
- nsGEN::HaoWeiDriver::HaoWeiDriver()
- : m_scfWrapper(std::make_shared<SCFWrapper>())
- {
- }
- nsGEN::HaoWeiDriver::~HaoWeiDriver()
- {
- }
- auto nsGEN::HaoWeiDriver::CreateDevice(int index) -> std::unique_ptr <IODevice>
- {
- FINFO("CreateDevice in\n");
-
- m_pDevice = new HaoWeiDevice(EventCenter, m_scfWrapper, m_ConfigFileName);
- auto dev = std::unique_ptr <IODevice>(new IODevice(m_pDevice));
- FINFO("CreateDevice out\n");
- return dev;
- }
- void nsGEN::HaoWeiDriver::FireNotify(int code, std::string key, std::string content)
- {
- EventCenter->OnNotify(code, key, content);
- }
- void nsGEN::HaoWeiDriver::Prepare()
- {
- FINFO("========================================");
- FINFO("Prepare() Starting...");
- FINFO("========================================");
- // 初始化日志系统
- std::string strLogPath = GetProcessDirectory() + R"(/Conf/log_config.xml)";
- std::string LogHost = "DevHaoWei";
- std::string moduleName = "DevHaoWei";
- FINFO("Initializing log module...");
- FINFO("Log config path: {$}", strLogPath.c_str());
- FINFO("Log host: {$}", LogHost.c_str());
- FINFO("Module name: {$}", moduleName.c_str());
- bool ret = initLogModule(
- LogHost, // 主机名(用于日志路径中的{host}占位符)
- moduleName, // 唯一模块名
- strLogPath, // 配置文件路径
- true // 是否输出到控制台(可选)
- );
- if (!ret) {
- std::cerr << "Log init failed!" << std::endl;
- FERROR("Failed to initialize log module");
- return;
- }
- FINFO("Log module initialized successfully");
- HaoWei_SetLocalModuleName(moduleName);
- FINFO("Getting connection DLL from config: {$}", m_ConfigFileName.c_str());
- m_SCFDllName = GetConnectDLL(m_ConfigFileName);
- FINFO("SCF DLL name: {$}", m_SCFDllName.c_str());
- FINFO("Calling parent Prepare()...");
- super::Prepare();
- FINFO("========================================");
- FINFO("REMEDYSTDriver::Prepare() Completed");
- FINFO("========================================");
- }
- bool DATA_ACTION nsGEN::HaoWeiDriver::Connect()
- {
- std::lock_guard<std::mutex> lock(m_connectionMutex);
- const auto currentState = m_connectionState.load();
- auto now = std::chrono::steady_clock::now();
- // 1. 处理可重试的失败状态
- if (currentState == ConnectionState::Failed) {
- if ((now - m_lastConnectionAttempt) >= RETRY_INTERVAL && m_connectionRetryCount < MAX_RETRY_COUNT) {
- m_connectionState = ConnectionState::Disconnected;
- }
- else {
- return false; // 不满足重试条件,直接返回
- }
- }
- // 2. 检查无效状态(正在连接/已连接但实际有效)
- if (currentState == ConnectionState::Connecting) {
- FINFO("Already connecting (type: {$})",
- m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
- return true;
- }
- if (currentState == ConnectionState::Connected && m_scfWrapper && m_scfWrapper->IsConnected()) {
- FINFO("Already connected (type: {$})",
- m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
- return true;
- }
- // 3. 检查重试间隔
- if (m_connectionRetryCount > 0 && (now - m_lastConnectionAttempt) < RETRY_INTERVAL) {
- FINFO("Retry in {$}s (type: {$})",
- std::chrono::duration_cast<std::chrono::seconds>(RETRY_INTERVAL - (now - m_lastConnectionAttempt)).count(),
- m_currentConnType == ConnectionType::Serial ? "Serial" : "Ethernet");
- return false;
- }
- ResDataObject connParam = GetConnectParam(m_ConfigFileName);
- std::string connPortStr = "";
- std::string connTypeStr = (std::string)connParam["type"]; // 从配置读取type字段
- m_currentConnType = (connTypeStr == "COM") ? ConnectionType::Serial : ConnectionType::Ethernet;
- if (m_currentConnType == ConnectionType::Serial)
- {
- connPortStr = (std::string)connParam["port"];// 从配置读取port字段
- // 查找配置端口在现有端口列表中的位置
- auto it = std::find(m_serialPorts.begin(), m_serialPorts.end(), connPortStr);
- if (it == m_serialPorts.end()) {
- // 配置的端口不在列表中,添加到首位
- FINFO("Configured serial port {$} not found, adding to front of port list", connPortStr);
- m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
- }
- else if (it != m_serialPorts.begin()) {
- // 配置的端口存在但不在首位,移动到首位
- FINFO("Moving configured serial port {$} to front of port list", connPortStr);
- m_serialPorts.erase(it);
- m_serialPorts.insert(m_serialPorts.begin(), connPortStr);
- }
- }
- // 4. 执行连接流程
- m_connectionState = ConnectionState::Connecting;
- m_lastConnectionAttempt = now;
- std::string connInfo;
- try {
- if (m_currentConnType == ConnectionType::Serial) {
- // 串口连接:使用当前索引的端口
- std::string currentPort = m_serialPorts[m_currentSerialPortIndex];
- connParam.update("port", currentPort.c_str()); // 将当前尝试的端口写入参数
- connInfo = "Serial (port: " + currentPort + ")";
- }
- else {
- // 网口连接:直接使用配置参数
- connInfo = "Ethernet (ip: " + std::string(connParam["ip"]) + ")";
- }
- FINFO("Enter Connect ({$}), config: {$}", connInfo, connParam.encode());
- if (!m_scfWrapper->Initialize(m_SCFDllName)) {
- FINFO("Init failed: {$}", m_scfWrapper->GetLastError());
- m_connectionState = ConnectionState::Failed;
- return false;
- }
- m_scfWrapper->SetDataReceivedCallback([this](const char* data, uint32_t length) {
- this->Dequeue(data, length);
- });
- auto erCode = m_scfWrapper->Connect(connParam, &HaoWeiDriver::callbackPackageProcess, SCF_PACKET_TRANSFER, 3000);
- if (erCode != SCF_SUCCEED || !m_scfWrapper->StartAutoReceive()) {
- FINFO("Connect failed (code: {$}) for {$}", erCode, connInfo);
- m_scfWrapper->Disconnect();
- m_connectionState = ConnectionState::Failed;
- // 串口连接:未遍历完所有端口时,优先切换端口重试(不计入总重试次数)
- if (m_currentConnType == ConnectionType::Serial) {
- int nextIndex = (m_currentSerialPortIndex + 1) % m_serialPorts.size();
- // 判断是否已遍历所有端口(当前索引是最后一个时,nextIndex会回到0)
- bool allPortsTried = (nextIndex == 0);
- if (!allPortsTried) {
- // 未遍历完所有端口:切换到下一端口,不增加重试计数
- m_currentSerialPortIndex = nextIndex;
- m_connectionRetryCount = 0;
- FINFO("Trying next serial port: {$}", m_serialPorts[nextIndex]);
- return false; // 触发外部线程立即尝试下一端口
- }
- else {
- // 已遍历所有端口:重置到第一个端口,增加重试计数(进入间隔等待)
- m_currentSerialPortIndex = 0;
- m_connectionRetryCount++;
- FINFO("All serial ports tried, retry count: {$}/{$}", m_connectionRetryCount, MAX_RETRY_COUNT);
- return false;
- }
- }
- // 所有端口失败(串口)或网口失败,才增加总重试计数
- m_connectionRetryCount++;
- return false;
- }
- // 连接成功:重置状态
- m_connectionState = ConnectionState::Connected;
- m_connectionRetryCount = 0;
- m_currentSerialPortIndex = 0; // 重置串口端口索引
- FINFO("Connected successfully ({$})", connInfo);
- return true;
- }
- catch (const std::exception& e) {
- FINFO("Exception for {$}: {$}", connInfo, e.what());
- m_connectionState = ConnectionState::Failed;
- m_connectionRetryCount++;
- return false;
- }
- }
- void nsGEN::HaoWeiDriver::Disconnect()
- {
- FINFO("========================================");
- FINFO("REMEDYSTDriver::Disconnect() Entry");
- if (m_scfWrapper) {
- FINFO("Stopping auto receive...");
- m_scfWrapper->StopAutoReceive();
- FINFO("Auto receive stopped");
- FINFO("Disconnecting SCF wrapper...");
- m_scfWrapper->Disconnect();
- FINFO("SCF wrapper disconnected");
- m_connectionState = ConnectionState::Disconnected;
- FINFO("Connection state set to Disconnected");
- }
- else {
- FWARN("SCF wrapper is null, nothing to disconnect");
- }
- FINFO("REMEDYSTDriver::Disconnect() Completed");
- FINFO("========================================");
- }
- bool nsGEN::HaoWeiDriver::isConnected() const
- {
- const auto state = m_connectionState.load();
- // 1. 连接中/实际已连接:返回true(无需重连)
- if (state == ConnectionState::Connecting || (m_scfWrapper && m_scfWrapper->IsConnected())) {
- //FINFO(state == ConnectionState::Connecting ? "Connecting in progress" : "Already connected");
- return true;
- }
- // 2. 失败状态处理:判断是否允许重试
- if (state == ConnectionState::Failed) {
- auto now = std::chrono::steady_clock::now();
- const auto timeSinceLast = now - m_lastConnectionAttempt;
- // 2.1 达到最大重试次数,但超过重置间隔:重置计数,允许重新重试
- if (m_connectionRetryCount >= MAX_RETRY_COUNT && timeSinceLast >= RESET_RETRY_AFTER) {
- FINFO("Max retries reached, resetting count after {$}s",
- std::chrono::duration_cast<std::chrono::seconds>(timeSinceLast).count());
- m_connectionRetryCount = 0; // 重置计数(因mutable修饰,const函数可修改)
- }
- // 2.2 不适合重连(时间未到 或 次数仍超限):返回true阻止重连
- if (timeSinceLast < RETRY_INTERVAL || m_connectionRetryCount >= MAX_RETRY_COUNT) {
- FINFO(timeSinceLast < RETRY_INTERVAL ?
- "Retry later ({$}s)" : "Max retries, waiting {$}s to reset",
- std::chrono::duration_cast<std::chrono::seconds>(
- timeSinceLast < RETRY_INTERVAL ? RETRY_INTERVAL - timeSinceLast : RESET_RETRY_AFTER - timeSinceLast
- ).count()
- );
- return true;
- }
- }
- // 3. 其他情况(适合重连):返回false触发Connect()
- return false;
- }
- std::string nsGEN::HaoWeiDriver::DriverProbe()
- {
- FINFO("DriverProbe in \n");
- ResDataObject r_config, HardwareInfo;
- if (r_config.loadFile(m_ConfigFileName.c_str()))
- {
- HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
- HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
- HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
- HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
- HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
- }
- else
- {
- HardwareInfo.add("MajorID", "Generator");
- HardwareInfo.add("MinorID", "Dr");
- HardwareInfo.add("VendorID", "HaoWei");
- HardwareInfo.add("ProductID", "HF");
- HardwareInfo.add("SerialID", "Drv");
- }
- string ret = HardwareInfo.encode();
- FINFO("DriverProbe out \n");
- return ret;
- }
- std::string nsGEN::HaoWeiDriver::GetResource()
- {
- ResDataObject temp;
- if (!temp.loadFile(m_ConfigFileName.c_str()))
- return std::string();
- auto r_config = temp["CONFIGURATION"];
- for (auto& Item : m_ConfigInfo)
- {
- string key = Item.GetKey();
- if (key == ConfKey::CcosGeneratorType)
- {
- Item.SetCurrentValue(((string)r_config["VendorID"]).c_str());
- }
- else if (key == ConfKey::CcosGeneratorModel)
- {
- Item.SetCurrentValue(((string)r_config["ProductID"]).c_str());
- }
- else if (key == ConfKey::CcosWSTable || key == ConfKey::CcosWSWall || key == ConfKey::CcosWSFree
- || key == ConfKey::CcosWSTomo || key == ConfKey::CcosWSConventional)
- {
- Item.SetCurrentValue(((string)r_config[key.c_str()]).c_str());
- }
- else if (key == ConfKey::CcosSynTable || key == ConfKey::CcosSynWall || key == ConfKey::CcosSynFree
- || key == ConfKey::CcosSynTomo || key == ConfKey::CcosSynConventional)
- {
- Item.SetCurrentValue(((string)r_config[key.c_str()]).c_str());
- }
- else if (key == ConfKey::CcosSCFType)
- {
- Item.SetCurrentValue(((string)r_config["connections"][0]["type"]).c_str());
- }
- else if (key == ConfKey::CcosSCFPort || key == ConfKey::CcosSCFBaudrate || key == ConfKey::CcosSCFBytesize
- || key == ConfKey::CcosSCFParity || key == ConfKey::CcosSCFStopbits || key == ConfKey::CcosSCFIP)
- {
- if (r_config["connections"][0].GetFirstOf(key.c_str()) >= 0)
- {
- Item.SetCurrentValue(((string)r_config["connections"][0][key.c_str()]).c_str());
- }
- }
- }
- ResDataObject resAttr, resDescription;
- for (auto Item : m_ConfigInfo)
- {
- resAttr.add(Item.GetKey(), Item.GetCurrentValue());
- resDescription.add(Item.GetKey(), Item.GetDescription());
- }
- ResDataObject resDeviceResource;
- resDeviceResource.add(ConfKey::CcosGeneratorAttribute, resAttr);
- resDeviceResource.add(ConfKey::CcosGeneratorDescription, resDescription);
- string res = resDeviceResource.encode();
- //printf("resDeviceResource :%s \n", resDeviceResource.encode());
- FINFO("resDeviceResource :{$} \n", resDeviceResource.encode());
- ResDataObject DescriptionTempEx;
- DescriptionTempEx.add(ConfKey::CcosGeneratorConfig, resDeviceResource);
- m_DeviceConfig.clear();
- m_DeviceConfig = DescriptionTempEx;
- return res;
- }
- bool nsGEN::HaoWeiDriver::GetDeviceConfig(std::string& Cfg)
- {
- //Cfg = m_DeviceConfigSend.encode();
- Cfg = m_DeviceConfig.encode();
- //printf("GetDeviceConfig over");
- printf("GetDeviceConfig over , %s", Cfg.c_str());
- return true;
- }
- bool nsGEN::HaoWeiDriver::SetDeviceConfig(std::string Cfg)
- {
- FINFO("--Func-- SetDeviceConfig {$}\n", Cfg.c_str());
- return true;
- }
- bool nsGEN::HaoWeiDriver::SaveConfigFile(bool bSendNotify)
- {
- m_ConfigAll["CONFIGURATION"] = m_Configurations;
- bool bRt = m_ConfigAll.SaveFile(m_ConfigFileName.c_str());
- FINFO("SaveConfigFile over {$}", bRt);
- return true;
- }
- std::string nsGEN::HaoWeiDriver::DeviceProbe()
- {
- FINFO("std::string nsGEN::PSGRFDriver::DeviceProbe() in\n");
- ResDataObject r_config, HardwareInfo;
- if (r_config.loadFile(m_ConfigFileName.c_str()))
- {
- HardwareInfo.add("MajorID", r_config["CONFIGURATION"]["MajorID"]);
- HardwareInfo.add("MinorID", r_config["CONFIGURATION"]["MinorID"]);
- HardwareInfo.add("VendorID", r_config["CONFIGURATION"]["VendorID"]);
- HardwareInfo.add("ProductID", r_config["CONFIGURATION"]["ProductID"]);
- HardwareInfo.add("SerialID", r_config["CONFIGURATION"]["SerialID"]);
- }
- else
- {
- HardwareInfo.add("MajorID", "Generator");
- HardwareInfo.add("MinorID", "Dr");
- HardwareInfo.add("VendorID", "HaoWei");
- HardwareInfo.add("ProductID", "HF");
- HardwareInfo.add("SerialID", "1234");
- }
- string ret = HardwareInfo.encode();
- FINFO("std::string nsGEN::PSGRFDriver::DeviceProbe() out\n");
- return ret;
- }
- void nsGEN::HaoWeiDriver::Dequeue(const char* Packet, DWORD Length)
- {
- DecodeFrame(Packet, Length);
- }
- /*
- ==IN==:KV070 MA00320 MS00063 MX00036
- ==IN==:TU0 WS1 FO0 ET0 FI010 FS001 FN 0 HE000
- how to split the str like up.
- //command+03+sum
- */
- PACKET_RET nsGEN::HaoWeiDriver::callbackPackageProcess(const char* RecData, uint32_t nLength, uint32_t& PacketLength)
- {
- FINFO("==IN==11:[{:02X$}]\n", RecData);
- if (nLength < 1) // 最小有效包为0x03 + checksum + 0x00(3字节)
- {
- return PACKET_USELESS;
- }
- for (DWORD i = 0; i < nLength; ++i) // 遍历所有字节
- {
- if (RecData[i] == 0x00)
- {
- if (i < 2) { // 至少需要0x03、校验和和结束符
- return PACKET_USELESS;
- }
- size_t markerPos = i - 2;
- if (RecData[markerPos] != 0x03) { // 检查0x03标识符
- // 不是有效的起始符,继续查找下一个结束符
- continue;
- }
- //BYTE calcSum = 0;
- //for (size_t j = 0; j <= markerPos; ++j) {
- // calcSum += RecData[j];
- //}
- //BYTE recvSum = RecData[i - 1];
- //if (calcSum != recvSum) {
- // // 校验失败,继续查找后续可能的包
- // continue;
- //}
- PacketLength = static_cast<DWORD>(i - 2); //应该+2.才会把最后的checmsum也包含进来。FO1+03+sum.
- char strtemp[100] = { 0 };
- memcpy(strtemp, RecData, i);
- strtemp[i + 1] = 0;
- FINFO("==IN==:{:02x$}\n", strtemp);
- return PACKET_ISPACKET;
- }
- }
- return PACKET_NOPACKET;
- }
- //-----------------------------------------------------------------------------
- // DecodeFrame
- //-----------------------------------------------------------------------------
- static bool DecodeFrame(const char* strFrame, int length)
- {
- FINFO("==IN==:{:02x$}\n", strFrame);
- auto pr = [strFrame, length](const tFrameMapping& Item)
- {
- for (int i = 0; i < Item.NbOfCharOfHead; i++)
- {
- if (strFrame[i] != Item.strHead[i])
- {
- return false;
- }
- }
- return true;
- };
- auto found = std::find_if(arFrame.begin(), arFrame.end(), pr);
- if (found == arFrame.end())
- {
- return false;
- }
- lastValidResponse.store(std::chrono::steady_clock::now());
- const auto& Item = *found;
- auto pc = strFrame;
- pc += Item.NbOfCharOfHead;
- Item.fun(pc, length - Item.NbOfCharOfHead);
- return true;
- }
- //-----------------------------------------------------------------------------
- // GetIODriver & CreateIODriver
- //-----------------------------------------------------------------------------
- static nsGEN::HaoWeiDriver gIODriver;
- extern "C" CCOS::Dev::IODriver * GetIODriver() // 返回静态对象的引用, 调用者不能删除 !
- {
- return &gIODriver;
- }
- extern "C" CCOS::Dev::IODriver * CreateIODriver() // 返回新对象, 调用者必须自行删除此对象 !
- {
- return new nsGEN::HaoWeiDriver();
- }
|