12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819 |
- #include "Detector_TiRayDR.h"
- #include "CCOS.Dev.FPD.TiRayDR.h"
- #include "MyPingip.h"
- #include <dlfcn.h>
- #include <pthread.h>
- using EventListenerType = void(*)(TiRayEvent, void*);
- Detector_TiRayDR* g_pDetector = nullptr;
- //extern Log4CPP::Logger* mLog::gLogger;
- /****************************************/
- int (*GetSdkVersion_Ptr)(void);
- TiRayError(*Scan_Ptr)(ResultCallback fn, const char* interfaceIp, int scanDuration);
- TiRayError(*SetIp_Ptr)(const char* detectorSN, const char* upperIp, const char* lowerIp, const char* interfaceIp);
- TiRayError(*Startup_Ptr)(TiRayModel model, EventCallback fn, const StartupOption * option);
- void (*Stop_Ptr)();
- TiRayError(*Execute_Ptr)(int detectorId, int commandId, TiRayVariant argv[], int argc);
- TiRayError(*ApplyPreset_Ptr)(int detectorId, TiRayVariant argv[], int argc, ResultCallback fn);
- TiRayError(*GenerateTemplate_Ptr)(TemplateType type, TiRayVariant images[], int count, void* templateBuffer, int bufferSize);
- static int g_load_error = 0;
- static void LoadOneFunc(void* hInstLib, void** pFunction, const char* funcName)
- {
- *pFunction = dlsym(hInstLib, funcName);
- const char* dlsym_error = dlerror();
- if (dlsym_error != NULL) {
- printf("Failed to load %s. Error = %s", funcName, dlsym_error);
- g_load_error = 1;
- }
- }
- void OnEvent(TiRayEvent eventType, TiRayVariant argv[], int argc) {
- if (argc > 0) {
- auto arg = new std::vector<TiRayVariant>(argc);
- for (int i = 0; i < argc; i++) {
- if (argv[i].Type == TiRayVariant::TiRayBuffer) {
- arg->at(i).Type = TiRayVariant::TiRayBuffer;
- arg->at(i).DataLen = argv[i].DataLen;
- arg->at(i).DataValue = new char[argv[i].DataLen];
- memcpy(arg->at(i).DataValue, argv[i].DataValue, argv[i].DataLen);
- }
- else {
- memcpy(&arg->at(i), &argv[i], sizeof(TiRayVariant));
- }
- }
- }
- }
- #define LOAD_ONE_FUNC(handle, funcName) LoadOneFunc(handle, (void**)&(funcName##_Ptr), #funcName)
- /****************************************/
- Detector_TiRayDR::Detector_TiRayDR()
- :m_nPanelCount{},
- m_nCurrentPanelID{},
- m_nImageWidth{},
- m_nImageHeight{},
- m_nWidthOffset{},
- m_nHeightOffset{},
- m_nRawImgWidth{},
- m_nRawImgHeight{},
- m_nCalibrationRounds{},
- m_nCalibCurrentCalibrationRound{},
- m_nCalibCurrentExposureIndex{},
- m_nExposureNumCurrentRound{},
- m_hTiRayDRModule{},
- m_hReconnectThread{},
- m_hFPDScanThread{},
- m_hRadAcquisitionThread{},
- m_hStatusMonitorThread{},
- m_pRawImgBuffer{},
- m_pImgBuffer{},
- m_pZSKKCalib{},
- m_strDetectorType{},
- m_strSerialNum{},
- m_nImageNum{},
- m_nDetectorID{},
- m_nNotifyStatusTimePeriod{},
- m_pStPanelStatus{},
- m_nReconnectTimePeriod(5000),
- m_nAppStatus(APP_STATUS::APP_STATUS_IDLE),
- m_eCaliType(CCOS_CALIBRATION_TYPE_NONE),
- m_nSyncMode(SYNC_SOFTWARE),
- m_nCalibrationMode(CCOS_CALIBRATION_MODE_ZSKK),
- m_bSaveRaw(true),
- m_bConnected(false),
- m_bAEDReady(false),
- m_bAEDWorkFlag(false),
- m_bExitRadAcqStatus(false),
- m_bMonitorFlag(false)
- {
- m_pDPC2PanelID = new map<FPDDeviceTiRay*, int>();
- m_pPanelID2DPC = new map<int, FPDDeviceTiRay*>();
- m_hExitRadAcqStatus = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
- m_hInitEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
- m_hExitEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
- m_hReConnectEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
- m_hRadEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
- m_hArrayEvent.push_back(m_hInitEvent);
- m_hArrayEvent.push_back(m_hExitEvent);
- m_hArrayEvent.push_back(m_hReConnectEvent);
- m_hArrayEvent.push_back(m_hRadEvent);
- m_hToggleEvent = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, FALSE);
- }
- Detector_TiRayDR::~Detector_TiRayDR()
- {
- CloseStatusMonitor();
- CloseDetectorScan();
- if (m_hReconnectThread != 0) {
- if (m_bReconnectThreadRunning) {
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- ts.tv_sec += 2; // 设置2秒超时
- // 等待线程结束
- pthread_timedjoin_np(m_hReconnectThread, nullptr, &ts);
- }
- // 如果线程仍在运行,强制取消
- if (m_bReconnectThreadRunning) {
- pthread_cancel(m_hReconnectThread);
- }
- m_hReconnectThread = 0;
- }
- if (m_pRawImgBuffer)
- {
- delete[] m_pRawImgBuffer;
- m_pRawImgBuffer = nullptr;
- }
- if (m_pImgBuffer)
- {
- delete[] m_pImgBuffer;
- m_pImgBuffer = nullptr;
- }
- if (m_pZSKKCalib)
- {
- delete m_pZSKKCalib;
- m_pZSKKCalib = nullptr;
- }
-
- }
- bool Detector_TiRayDR::ScanDetector(string& strDetectorInfo)
- {
- ResDataObject result;
- std::vector<scan_result> scan_results;
- // 扫描探测器并收集所有结果
- scan([&scan_results](scan_result&& res) {
- scan_results.emplace_back(std::move(res));
- });
- if (scan_results.empty()) {
- std::cerr << "[Detector_TiRayDR::ScanDetector] No detectors were found!" << std::endl;
- return false;
- }
- std::cout << "[Detector_TiRayDR::ScanDetector] Scanned " << scan_results.size() << " detector(s):" << std::endl;
- for (size_t i = 0; i < scan_results.size(); ++i) {
- const auto& res = scan_results[i];
- ResDataObject detectorData;
- detectorData.add("SerialNumber", res.sn.c_str());
- detectorData.add("Model", res.model.c_str());
- detectorData.add("TupperIP", res.upper_ip.c_str());
- detectorData.add("DetectorIP", res.detector_ip.c_str());
- std::string key = "DetectorData_" + std::to_string(i);
- result.add(key.c_str(), detectorData);
- // 输出当前探测器信息
- std::cout << "[Detector_TiRayDR::ScanDetector] Detector " << (i + 1) << ":"
- << " Serial number:" << res.sn
- << ", Model:" << res.model
- << ", tupper IP:" << res.upper_ip
- << ", detector IP:" << res.detector_ip << std::endl;
- }
- try {
- strDetectorInfo = result.encode();
- }
- catch (const std::exception& e) {
- std::cerr << "[Detector_TiRayDR::ScanDetector] Encode failed: " << e.what() << std::endl;
- return false;
- }
- return true;
- }
- bool Detector_TiRayDR::DriverEntry(FPDDeviceTiRay* pDrvDPC, ResDataObject& Configuration)
- {
- //mLog::FINFO("--TiRayDR Func-- DriverEntry Start");
- map<FPDDeviceTiRay*, int>::iterator DPCsIter = m_pDPC2PanelID->find(pDrvDPC);
- if (DPCsIter != m_pDPC2PanelID->end())
- {
- //mLog::FERROR("This DPC already exist");
- return false;
- }
- CPanelStatus* p = new CPanelStatus();
- m_pStPanelStatus[m_nPanelCount] = p;
- m_pDPC2PanelID->insert(pair<FPDDeviceTiRay*, int>(pDrvDPC, m_nPanelCount));
- m_pPanelID2DPC->insert(pair<int, FPDDeviceTiRay*>(m_nPanelCount, pDrvDPC));
- m_nPanelCount++;
- m_ModeConfig = Configuration; //记录配置 --目前只有一个平板,多板时应该分别存储
- //mLog::FINFO("Config: {$}", m_ModeConfig.encode());
- try
- {
- m_nCalibrationMode = (CCOS_CALIBRATION_MODE)(int)m_ModeConfig["CalibMode"];
- }
- catch (ResDataObjectExption& e)
- {
- //mLog::FERROR("Read configuration failed, Error code: {$}", e.what());
- }
- //mLog::FINFO("TiRayDR DriverEntry Over");
- return true;
- }
- bool Detector_TiRayDR::Connect(FPDDeviceTiRay* pDrvDPC, const char* szWorkPath)
- {
- //mLog::FINFO("--TiRayDR Func-- Connect Start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FINFO("Not current DPC, return true");
- return true;
- }
- if (!m_pZSKKCalib)
- {
- m_pZSKKCalib = new CZSKKCalibrationCtrl();
- }
- m_strWorkPath = szWorkPath;
- if (m_hFPDScanThread == 0)
- {
- pthread_t threadId;
- int result = pthread_create(&threadId, NULL, onFPDScanThread, this);
- if (result == 0) {
- m_hFPDScanThread = threadId; // 存储线程ID
- }
- else {
- //mLog::FERROR("Thread creation failed: %d", result);
- return false;
- }
- }
- if (m_hInitEvent) {
- m_hInitEvent->SetEvent();
- }
- //mLog::FINFO("TiRayDR Connect Over");
- return true;
- }
- bool Detector_TiRayDR::Disconnect()
- {
- //mLog::FINFO("--TiRayDR Func-- Disconnect Begin");
- m_hExitEvent->SetEvent(); //关闭Scan线程
- bool result = m_hToggleEvent->Wait(65000);
- if (result)
- {
- //mLog::FINFO("Leave scan thread over");
- }
- else
- {
- //mLog::FERROR("Till time detectorData");
- }
- //mLog::FINFO("Call API Stop");
- Stop();
- //mLog::FINFO("TiRayDR Disconnect Over");
- return true;
- }
- void Detector_TiRayDR::EnterExamMode(int nExamMode)
- {
- switch (nExamMode)
- {
- case APP_STATUS_WORK_BEGIN:
- //mLog::FINFO("Enter into Exam Windows");
- m_nAppStatus = APP_STATUS_WORK_BEGIN;
- break;
- case APP_STATUS_WORK_END:
- //mLog::FINFO("Quit Exam Windows");
- m_nAppStatus = APP_STATUS_WORK_END;
- break;
- case APP_STATUS_DETSHARE_BEGIN:
- //mLog::FINFO("Enter into Detector Share Windows");
- m_nAppStatus = APP_STATUS_DETSHARE_BEGIN;
- break;
- case APP_STATUS_DETSHAR_END:
- //mLog::FINFO("Quit Detector Share Windows");
- m_nAppStatus = APP_STATUS_DETSHAR_END;
- break;
- case APP_STATUS_CAL_BEGIN:
- //mLog::FINFO("Enter into Calibration Windows");
- m_nAppStatus = APP_STATUS_CAL_BEGIN;
- break;
- case APP_STATUS_CAL_END:
- //mLog::FINFO("Quit Calibration Windows");
- m_nAppStatus = APP_STATUS_CAL_END;
- break;
- case APP_STATUS_WORK_IN_SENSITIVITY:
- //mLog::FINFO("Enter into sensitivity test interface");
- m_nAppStatus = APP_STATUS_WORK_IN_SENSITIVITY;
- break;
- default:
- break;
- }
- }
- /***
- ** 根据采集模式申请图像buffer
- ***/
- bool Detector_TiRayDR::SetAcqMode(int nMode)
- {
- //mLog::FINFO("--TiRayDR Func-- SetAcqMode Start");
- //mLog::FINFO("Detector_TiRayDR::SetAcqMode mode:{$}", nMode);
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("Detector not connected, return");
- return false;
- }
- int nRes;
- m_bExitRadAcqStatus = false;
- try
- {
- //设置采集模式,根据不同的场景设置不同的采集模式
- m_nImageWidth = (int)m_ModeConfig["ModeTable"][0]["ImageWidth"];
- m_nImageHeight = (int)m_ModeConfig["ModeTable"][0]["ImageHeight"];
- m_nWidthOffset = (int)m_ModeConfig["ModeTable"][0]["WidthOffset"];
- m_nHeightOffset = (int)m_ModeConfig["ModeTable"][0]["HeightOffset"];
- //mLog::FINFO("After crop image width: {$}, height: {$}, WidthOffset: {$}, HeightOffset: {$}", m_nImageWidth, m_nImageHeight, m_nWidthOffset, m_nHeightOffset);
- m_bSaveRaw = (int)m_ModeConfig["ModeTable"][0]["IsSaveRaw"];
- //mLog::FINFO("m_nSaveRaw:{$}", m_bSaveRaw);
- if (nullptr != m_pImgBuffer)
- {
- delete[] m_pImgBuffer;
- m_pImgBuffer = nullptr;
- }
- m_pImgBuffer = new WORD[(size_t)m_nImageWidth * (size_t)m_nImageHeight];
- }
- catch (ResDataObjectExption& e)
- {
- //mLog::FERROR("Get config FERROR: {$}", e.what());
- return false;
- }
- //TiRayVariant Param[2]{};
- //Param[0].Type = TiRayVariant::TiRayInt;
- //Param[0].IntValue = TiRayAttribute::Attr_BinningMode;
- //Param[1].Type = TiRayVariant::TiRayInt;
- //Param[1].IntValue = BinningMode::Binning_None;
- //nRes = Execute_Ptr(m_nDetectorID, Cmd_WriteAttribute, Param, 2);
- //if ((TiRayError)nRes != TiRayError::Err_Success)
- //{
- // //mLog::Error("Set BinningMode Failed, Reason:{$}", nRes);
- // return false;
- //}
- //else
- //{
- // //mLog::FINFO("Set BinningMode Success");
- //}
- //TiRayVariant ParamAED[2]{};
- //ParamAED[0].Type = TiRayVariant::TiRayInt;
- //ParamAED[0].IntValue = TiRayAttribute::Attr_AEDSensitivity;
- //ParamAED[1].Type = TiRayVariant::TiRayInt;
- //ParamAED[1].IntValue = 200;
- //nRes = Execute_Ptr(m_nDetectorID, Cmd_WriteAttribute, ParamAED, 2);
- //if ((TiRayError)nRes != TiRayError::Err_Success)
- //{
- // //mLog::Error("Set BinningMode Failed, Reason:{$}", nRes);
- // return false;
- //}
- //else
- //{
- // //mLog::FINFO("Set BinningMode Success");
- //}
- StatusFeedback(EVT_STATUS_PANEL, PANEL_SLEEP);
- //mLog::FINFO("TiRayDR SetAcqMode Over");
-
- return true;
- }
- bool Detector_TiRayDR::SetSyncMode(int nSyncMode)
- {
- //mLog::FINFO("--TiRayDR Func-- SetSyncMode Start");
- //mLog::FINFO("SetSyncMode: {$}", nSyncMode);
- std::cout << "--TiRayDR Func-- SetSyncMode Start" << std::endl;
- std::cout << "SetSyncMode: " << nSyncMode << std::endl;
- int nRes;
- TiRayVariant Param[2]{};
- Param[0].Type = TiRayVariant::TiRayInt;
- Param[0].IntValue = TiRayAttribute::Attr_WorkMode;
- Param[1].Type = TiRayVariant::TiRayInt;
- if (nSyncMode == 1)
- {
- Param[1].IntValue = WorkMode::WorkMode_Idle;
- m_nSyncMode = SYNC_MODE::SYNC_SOFTWARE;
- std::cout << "Setting SyncMode to Software Sync" << std::endl;
- nRes = Execute_Ptr(m_nDetectorID, Cmd_WriteAttribute, Param, 2);
- if ((TiRayError)nRes != TiRayError::Err_Success)
- {
- std::cout << "Error: Failed to set SyncMode to Software Sync. Reason: " << nRes << std::endl;
- //mLog::Error("Set SyncMode SoftSync, Reason:{$}", nRes);
- return false;
- }
- else
- {
- std::cout << "Success: Software Sync mode set successfully" << std::endl;
- //mLog::FINFO("Set SoftSync Success");
- }
- }
- else if (nSyncMode == 2)
- {
- m_nSyncMode = SYNC_MODE::SYNC_HARDWARE;
- Param[1].IntValue = WorkMode::WorkMode_SyncIn;
- std::cout << "Setting SyncMode to Hardware Sync" << std::endl;
- nRes = Execute_Ptr(m_nDetectorID, Cmd_WriteAttribute, Param, 2);
- if ((TiRayError)nRes != TiRayError::Err_Success)
- {
- std::cout << "Error: Failed to set SyncMode to Hardware Sync. Reason: " << nRes << std::endl;
- //mLog::Error("Set SyncMode HardSync, Reason:{$}", nRes);
- return false;
- }
- else
- {
- std::cout << "Success: Hardware Sync mode set successfully" << std::endl;
- //mLog::FINFO("Set HardSync Success");
- }
- }
- else if (nSyncMode == 3)
- {
- Param[1].IntValue = WorkMode_FreeSync;
- m_nSyncMode = SYNC_MODE::SYNC_AED;
- std::cout << "Setting SyncMode to AED Sync" << std::endl;
- nRes = Execute_Ptr(m_nDetectorID, Cmd_WriteAttribute, Param, 2);
- if ((TiRayError)nRes != TiRayError::Err_Success)
- {
- std::cout << "Error: Failed to set SyncMode to AED Sync. Reason: " << nRes << std::endl;
- //mLog::Error("Set SyncMode AED, Reason:{$}", nRes);
- return false;
- }
- else
- {
- std::cout << "Success: AED Sync mode set successfully" << std::endl;
- //mLog::FINFO("Set AED Success");
- }
- }
- m_pStPanelStatus[m_nCurrentPanelID]->eSyncMode = (SYNC_MODE)m_nSyncMode;
- //mLog::FINFO("TiRayDR SetSyncMode Over");
- std::cout << "TiRayDR SetSyncMode Over" << std::endl;
- return true;
- }
- bool Detector_TiRayDR::PrepareAcquisition(FPDDeviceTiRay* pDrvDPC)
- {
- //mLog::FINFO("--TiRayDR Func-- PrepareAcquisition Start");
- //mLog::FINFO("PrepareAcquisition start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FERROR("Not current DPC, return");
- return false;
- }
- //未初始化、未连接 不执行
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("Detector not connected, return");
- return false;
- }
- if (m_bAEDReady)
- {
- g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_STANDBY);
- }
- g_pDetector->StatusFeedback(EVT_STATUS_PANEL, PANEL_STANDBY);
- //m_hRadEvent->SetEvent();
- //mLog::FINFO("TiRayDR PrepareAcquisition Over");
- return true;
- }
- bool Detector_TiRayDR::StartAcquisition(FPDDeviceTiRay* pDrvDPC)
- {
- //mLog::FINFO("--TiRayDR Func-- StartAcquisition Start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FERROR("Not current DPC, return");
- return false;
- }
- //未初始化、未连接 不执行
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("Detector not connected, return");
- return false;
- }
- if (m_nSyncMode == SYNC_MODE::SYNC_SOFTWARE)
- {
- //SetSyncMode(1);
- auto err = Execute_Ptr(m_nDetectorID, Cmd_Photo, nullptr, 0);
- if (err != Err_Success) {
- cout << "[Detector_TiRayDR::StartAcquisition] Failed to Execute_Ptr Cmd_Photo. Error code: " << err << endl;
- return false;
- }
- }
- StatusFeedback(EVT_STATUS_PANEL, PANEL_START_ACQ);
- m_bAEDWorkFlag = true;
- StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_ON);
- //mLog::FINFO("TiRayDR StartAcquisition Over");
- return true;
- }
- bool Detector_TiRayDR::StopAcquisition(FPDDeviceTiRay* pDrvDPC)
- {
- //mLog::FINFO("--TiRayDR Func-- StopAcquisition Start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FERROR("Not current DPC, return");
- return false;
- }
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("Detector not connected, return");
- return false;
- }
- //mLog::FINFO("## Stop Acquisition ##");
- StatusFeedback(EVT_STATUS_PANEL, PANEL_STANDBY);
- SetTiRayDPCStatus(eDetStatus::DetStatus_Standby); //停止采集
- //mLog::FINFO("TiRayDR StopAcquisition Over");
- return true;
- }
- bool Detector_TiRayDR::ActiveCalibration(FPDDeviceTiRay* pDrvDPC, CCOS_CALIBRATION_TYPE eType)
- {
- //mLog::FINFO("--TiRayDR Func-- ActiveCalibration Start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FERROR("Not current DPC, return");
- return false;
- }
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("bConnectState is false, Detector not connected, return");
- return false;
- }
- StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_START);
- m_nAppStatus = APP_STATUS_CAL_BEGIN; //激活校正,置为校正界面
- m_eCaliType = eType;
- if (CCOS_CALIBRATION_TYPE_DARK == m_eCaliType)
- {
- //mLog::FINFO("Active Dark Calibration");
- }
- if (CCOS_CALIBRATION_TYPE_XRAY == m_eCaliType)
- {
- //mLog::FINFO("Active Xray Calibration");
- if (m_nCalibrationMode) //厂商校正ActiveCalibration
- {
- }
- //else //ZSKK校正
- //{
- // if (!m_pZSKKCalib)
- // {
- // //mLog::FERROR("ZSKK Calibration object is undefined");
- // }
- // else
- // {
- // //加载ZSKK的校正文件
- // m_pZSKKCalib->m_strRawImgPath = m_strWorkPath + "/rawdata/";
- // m_pZSKKCalib->m_strRefFilePath = m_strWorkPath + "/references/";
- // m_pZSKKCalib->m_nFullImgWidth = m_nImageWidth;
- // m_pZSKKCalib->m_nFullImgHeight = m_nImageHeight;
- // m_pZSKKCalib->m_nReferenceNum = m_nCalibrationRounds;
- // m_pZSKKCalib->m_nSaturationValue = 50000;
- // //LoadZSKKGainMap 参数为false,意思是开始增益校正
- // m_pZSKKCalib->LoadZSKKGainMap(false, m_strDetectorType);
- // m_pZSKKCalib->LoadZSKKPixelMap(false, m_strDetectorType);
- // //mLog::FINFO("Load ecom gain and pixel map, references file path: {$}", m_pZSKKCalib->m_strRefFilePath);
- // }
- //}
- }
- //mLog::FINFO("TiRayDR ActiveCalibration Over");
- return true;
- }
- /***
- * 接受曝光图像
- ***/
- bool Detector_TiRayDR::AcceptCalibration()
- {
- //mLog::FINFO("--TiRayDR Func-- AcceptCalibration Start");
- if (m_nCalibrationMode)//厂商校正AcceptCalibration
- {
- //不做处理
- }
- else //ZSKK校正
- {
- //WORD* pImageBuffer = nullptr;
- ////这里要注意使用的image buffer是哪个,裁剪和不裁剪是不一样的
- //if (m_nWidthOffset != 0 || m_nHeightOffset != 0)
- //{
- // pImageBuffer = m_pImgBuffer;
- //}
- //else
- //{
- // pImageBuffer = m_pImgBuffer;//之后再做区分,先测试用
- //}
- //if (m_nCalibCurrentExposureIndex == 1)
- //{
- // m_pZSKKCalib->AddImageToPixMap(pImageBuffer);
- // m_pZSKKCalib->AverageZSKKGainMap(pImageBuffer, m_nCalibCurrentCalibrationRound - 1, true);
- //}
- //else
- //{
- // m_pZSKKCalib->AverageZSKKGainMap(pImageBuffer, m_nCalibCurrentCalibrationRound - 1, false); //曝光第几轮
- //}
- }
- //mLog::FINFO("TiRayDR AcceptCalibration Over");
- return true;
- }
- /***
- * 拒绝曝光图像
- ***/
- bool Detector_TiRayDR::RejectCalibration()
- {
- const std::string funcTag = "[Detector_TiRayDR::RejectCalibration] ";
- // 检查当前是否在进行增益校正
- if (m_eCaliType != CCOS_CALIBRATION_TYPE_XRAY)
- {
- cout << funcTag << "No active gain calibration in progress" << endl;
- return false;
- }
- bool rejected = false;
- if (m_bUseGainV2)
- {
- if (!m_currentDoseImages.empty())
- {
- m_currentDoseImages.pop_back();
- cout << funcTag << "Rejected last image in dose group " << m_currentDoseIndex
- << ". Remaining in group: " << m_currentDoseImages.size() << endl;
- rejected = true;
- }
- else
- {
- cout << funcTag << "No images in current dose group to reject" << endl;
- }
- }
- else
- {
- if (!m_gainCalibImages.empty())
- {
- m_gainCalibImages.pop_back(); // 移除最后添加的图像
- cout << funcTag << "Rejected last gain image. Remaining: " << m_gainCalibImages.size() << endl;
- rejected = true;
- }
- else
- {
- cout << funcTag << "No gain images to reject" << endl;
- }
- }
- return rejected;
- }
- /***
- * 设置校正轮数
- ***/
- bool Detector_TiRayDR::SetCalibRounds(int nCalibRounds)
- {
- //mLog::FINFO("--TiRayDR Func-- SetCalibRounds Start");
- m_nCalibrationRounds = nCalibRounds;
- //mLog::FINFO("Set reference number: {$}", m_nCalibrationRounds);
- //mLog::FINFO("TiRayDR SetCalibRounds Over");
- return true;
- }
- bool Detector_TiRayDR::GetCalibrationStep(int nCalibCurrentCalibrationRound, int nCalibrationRounds, int nCalibCurrentExposureIndex, int nExposureNumCurrentRound)
- {
- //mLog::FINFO("--TiRayDR Func-- GetCalibrationStep Start");
- m_nCalibCurrentCalibrationRound = nCalibCurrentCalibrationRound;
- m_nCalibrationRounds = nCalibrationRounds;
- m_nCalibCurrentExposureIndex = nCalibCurrentExposureIndex;
- m_nExposureNumCurrentRound = nExposureNumCurrentRound;
- //mLog::FINFO("Calibration Step===Round: {$}/{$}, ExposureNum: {$}/{$}", nCalibCurrentCalibrationRound, nCalibrationRounds,
- //nCalibCurrentExposureIndex, nExposureNumCurrentRound);
- //mLog::FINFO("TiRayDR GetCalibrationStep Over");
- return true;
- }
- bool Detector_TiRayDR::PrepareCalibration(FPDDeviceTiRay* pDrvDPC)
- {
- const std::string funcTag = "[Detector_TiRayDR::PrepareCalibration] ";
- std::cout << funcTag << "--TiRayDR Function-- PrepareCalibration started" << std::endl;
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- std::cout << funcTag << "Error: Not current DPC device, returning false" << std::endl;
- return false;
- }
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- std::cout << funcTag << "Error: Detector not connected (bConnectState is false), returning false" << std::endl;
- return false;
- }
- auto writeAttrWithLog = [this](const std::string& attrName, int value, int err) {
- if (err == Err_Success)
- {
- std::cout << "[Detector_TiRayDR::PrepareCalibration] Success: Wrote attribute " << attrName << " with value " << value << std::endl;
- }
- else
- {
- std::cout << "[Detector_TiRayDR::PrepareCalibration] Error: Failed to write attribute " << attrName << ", error code: " << err << std::endl;
- }
- return err;
- };
- if (CCOS_CALIBRATION_TYPE_DARK == m_eCaliType)
- {
- int ret = DarkAcquisition();
- std::cout << funcTag << "Info: DarkAcquisition returned " << ret << std::endl;
- if (!ret)
- {
- std::cout << funcTag << "Info: Sending calibration start status (EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_BEGIN)" << std::endl;
- StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_BEGIN);
- }
- }
- else if (CCOS_CALIBRATION_TYPE_XRAY == m_eCaliType)
- {
- std::cout << funcTag << "Info: Preparing X-ray calibration (CCOS_CALIBRATION_TYPE_XRAY)" << std::endl;
- int err = write_attribute(Attr_WorkMode, WorkMode_FreeSync);
- writeAttrWithLog("Attr_WorkMode", WorkMode_FreeSync, err);
- err = write_attribute(Attr_PhotoInterval, 3000);
- writeAttrWithLog("Attr_PhotoInterval", 3000, err);
- err = write_attribute(Attr_CalibrationMode, CalibrationMode_None);
- writeAttrWithLog("Attr_CalibrationMode", CalibrationMode_None, err);
- }
- std::cout << funcTag << "Info: TiRayDR PrepareCalibration completed" << std::endl;
- return true;
- }
- //软同步调用接口,其它同步模式没有动作
- bool Detector_TiRayDR::StartCalibration(FPDDeviceTiRay* pDrvDPC)
- {
- //mLog::FINFO("--TiRayDR Func-- StartCalibration Start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FERROR("Not current DPC, return");
- return false;
- }
- //未初始化、未连接 不执行
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("bConnectState is false, Detector not connected, return");
- return false;
- }
- if (CCOS_CALIBRATION_TYPE_DARK == m_eCaliType)
- {
- //mLog::FINFO("StartCalibration DARK");
- StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_END_OK);
- m_pStPanelStatus[m_nCurrentPanelID]->eFPDStatus = eDetStatus::DetStatus_Offset;
- }
- else if (CCOS_CALIBRATION_TYPE_XRAY == m_eCaliType)
- {
- //mLog::FINFO("StartCalibration XRAY");
- bool ret = StartAcquisition(pDrvDPC);
- if (!ret)
- {
- //mLog::FERROR("StartCalibration Over");
- return false;
- }
- m_pStPanelStatus[m_nCurrentPanelID]->eFPDStatus = eDetStatus::DetStatus_XrayCalibration;
- }
- //mLog::FINFO("TiRayDR StartCalibration Over");
- return true;
- }
- /***
- ** 说明:终止校正
- ***/
- RET_STATUS Detector_TiRayDR::AbortCalibration(FPDDeviceTiRay* pDrvDPC)
- {
- const std::string funcTag = "[Detector_TiRayDR::AbortCalibration] ";
- //mLog::FINFO("--TiRayDR Func-- AbortCalibration Start");
- if (m_eCaliType == CCOS_CALIBRATION_TYPE_XRAY)
- {
- // 清除增益校正相关数据
- m_gainCalibImages.clear();
- m_currentDoseImages.clear();
- m_gainV2MeanImages.clear();
- m_currentDoseIndex = 0;
- cout << funcTag << "Aborted " << (!m_bUseGainV2 ? "Gain" : "GainV2")
- << " calibration. All related data cleared" << endl;
- }
- auto err = write_attribute(Attr_CalibrationMode, CalibrationMode_Defect);
- if (err == Err_Success)
- {
- std::cout << funcTag << "Success: Wrote attribute Attr_CalibrationMode with value CalibrationMode_Defect" << std::endl;
- }
- else
- {
- std::cout << funcTag << "Error: Failed to write attribute Attr_CalibrationMode, error code: " << err << std::endl;
- }
- // 恢复初始状态
- m_eCaliType = CCOS_CALIBRATION_TYPE_NONE;
- m_nAppStatus = APP_STATUS_CAL_END;
- //mLog::FINFO("TiRayDR AbortCalibration Over");
- return RET_STATUS::RET_SUCCEED;
- }
- bool Detector_TiRayDR::StopCalibration(FPDDeviceTiRay* pDrvDPC)
- {
- //mLog::FINFO("--TiRayDR Func-- StopCalibration Start");
- if ((*m_pDPC2PanelID)[pDrvDPC] != m_nCurrentPanelID)
- {
- //mLog::FERROR("Not current DPC, return");
- return false;
- }
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("bConnectState is false, Detector not connected, return");
- return false;
- }
- m_nAppStatus = APP_STATUS_CAL_END;
- //mLog::FINFO("TiRayDR StopCalibration Over");
- return true;
- }
- /***
- ** 说明:结束校正
- ** DPC处理完校正报告后调用,此处上传map、报告等文件
- ***/
- bool Detector_TiRayDR::CompleteCalibration(FPDDeviceTiRay* pDrvDPC)
- {
- //mLog::FINFO("--TiRayDR Func-- CompleteCalibration Start");
- //mLog::FINFO("Calib Type {$}", (int)m_eCaliType);
- const std::string funcTag = "[Detector_TiRayDR::CompleteCalibration] ";
- if (m_eCaliType == CCOS_CALIBRATION_TYPE_DARK)
- {
- //mLog::FINFO("DARK Calib over");
- }
- else if (m_eCaliType == CCOS_CALIBRATION_TYPE_XRAY)
- {
- //mLog::FINFO("XRAY Calib over");
- m_nAppStatus = APP_STATUS_CAL_END;
- }
- m_eCaliType = CCOS_CALIBRATION_TYPE_NONE;
- auto err = write_attribute(Attr_CalibrationMode, CalibrationMode_Defect);
- if (err == Err_Success)
- {
- std::cout << funcTag << "Success: Wrote attribute Attr_CalibrationMode with value CalibrationMode_Defect" << std::endl;
- }
- else
- {
- std::cout << funcTag << "Error: Failed to write attribute Attr_CalibrationMode, error code: " << err << std::endl;
- }
- //mLog::FINFO("TiRayDR CompleteCalibration Over");
- return true;
- }
- bool Detector_TiRayDR::SaveCalibrationFile()
- {
- //mLog::FINFO("--TiRayDR Func-- SaveCalibrationFile Start");
- if (m_nCalibrationMode)//厂商校正
- {
- //不做处理
- }
- else
- {
- //mLog::FINFO("Save ZSKK Calibration File");
- m_pZSKKCalib->StoreZSKKGainMap(m_strDetectorType);
- m_pZSKKCalib->StoreZSKKPixMap(m_strDetectorType);
- }
- //更新配置文件中校正日期和时间
- GlobalTime stCurrentTime = { 0 };
- GetLocalTime(&stCurrentTime);
- //mLog::FINFO("Current time: {$04d}/{$02d}/{$02d} {$02d}:{$02d}:{$02d}:{$03d}",
- //stCurrentTime.wYear, stCurrentTime.wMonth, stCurrentTime.wDay,
- //stCurrentTime.wHour, stCurrentTime.wMinute, stCurrentTime.wSecond, stCurrentTime.wMilliseconds);
- //mLog::FINFO("TiRayDR SaveCalibrationFile Over");
- return true;
- }
- CCOS_CALIBRATION_TYPE Detector_TiRayDR::GetCalibType()
- {
- //mLog::FINFO("--TiRayDR Func-- GetCalibType Start");
- //mLog::FINFO("Get Calib Type {$}", (int)m_eCaliType);
- //mLog::FINFO("TiRayDR GetCalibType Over");
- return m_eCaliType;
- }
- ModelResolveResult Detector_TiRayDR::ResolveModelType(const std::string& detectorType)
- {
- ModelResolveResult result;
- result.isValid = true;
- if (detectorType == "TiRay_GQ1613") {
- result.model = Model_GQ1613;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_GQ1613)..." << endl;
- }
- else if (detectorType == "TiRay_DY1613") {
- result.model = Model_DY1613;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY1613)..." << endl;
- }
- else if (detectorType == "TiRay_LT1719") {
- result.model = Model_LT1719;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_LT1719)..." << endl;
- }
- else if (detectorType == "TiRay_DY4343") {
- result.model = Model_DY4343;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY4343)..." << endl;
- }
- else if (detectorType == "TiRay_DY2530W") {
- result.model = Model_DY2530W;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY2530W)..." << endl;
- }
- else if (detectorType == "TiRay_DY2121") {
- result.model = Model_DY2121;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY2121)..." << endl;
- }
- else if (detectorType == "TiRay_DY4343D") {
- result.model = Model_DY4343D;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY4343D)..." << endl;
- }
- else if (detectorType == "TiRay_GZ0404") {
- result.model = Model_GZ0404;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_GZ0404)..." << endl;
- }
- else if (detectorType == "TiRay_DY3543W") {
- result.model = Model_DY3543W;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY3543W)..." << endl;
- }
- else if (detectorType == "TiRay_DY4343W") {
- result.model = Model_DY4343W;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY4343W)..." << endl;
- }
- else if (detectorType == "TiRay_DY3543") {
- result.model = Model_DY3543;
- cout << "[Detector_TiRayDR::OpenDetector] Try to start the detector(Model_DY3543)..." << endl;
- }
- else {
- cout << "[Detector_TiRayDR::OpenDetector] Unsupported detector type: " << detectorType << endl;
- // 使用第一个枚举值作为默认,通过isValid标志判断有效性
- result.model = Model_GQ1613;
- result.isValid = false;
- }
- return result;
- }
- bool Detector_TiRayDR::LoadDll(string strWorkPath)
- {
- //mLog::FINFO("--TiRayDR Func-- LoadDll Start");
- string strSDKPath = "";
- try
- {
- strSDKPath = (string)m_ModeConfig["SDKPath"];
- }
- catch (ResDataObjectExption& e)
- {
- //mLog::FERROR("Read configuration failed! reason: {$}", e.what());
- return false;
- }
- string workpath = strWorkPath + strSDKPath;
- string drvpath = workpath + "/TiRayLib.so";
- //mLog::FINFO("SDK path:{$}", drvpath);
- m_hTiRayDRModule = dlopen(drvpath.c_str(), RTLD_LAZY);
- if (m_hTiRayDRModule == nullptr)
- {
- //mLog::FERROR("Load {$} failed! FERROR: {$}", drvpath.c_str(), dlerror());
- return false;
- }
- LOAD_ONE_FUNC(m_hTiRayDRModule, GetSdkVersion);
- LOAD_ONE_FUNC(m_hTiRayDRModule, Scan);
- LOAD_ONE_FUNC(m_hTiRayDRModule, SetIp);
- LOAD_ONE_FUNC(m_hTiRayDRModule, Startup);
- LOAD_ONE_FUNC(m_hTiRayDRModule, Stop);
- LOAD_ONE_FUNC(m_hTiRayDRModule, Execute);
- LOAD_ONE_FUNC(m_hTiRayDRModule, ApplyPreset);
- LOAD_ONE_FUNC(m_hTiRayDRModule, GenerateTemplate);
-
- //mLog::FINFO("TiRayDR LoadDll Over");
- return true;
- }
- bool Detector_TiRayDR::ReleaseDll()
- {
- //mLog::FINFO("--TiRayDR Func-- ReleaseDll Start");
- if (m_hTiRayDRModule != nullptr)
- {
- int result = dlclose(m_hTiRayDRModule);
- if (result != 0) {
- const char* error = dlerror();
- if (error) {
- //mLog::FERROR("Failed to unload library: %s", error);
- }
- else {
- //mLog::FERROR("Failed to unload library (unknown error)");
- }
- return false;
- }
- m_hTiRayDRModule = nullptr;
- }
- //mLog::FINFO("TiRayDR ReleaseDll Over");
- return true;
- }
- /***
- ** 连接探测器
- ***/
- bool Detector_TiRayDR::OpenDetector()
- {
- cout << "[Detector_TiRayDR::OpenDetector] Start executing the detector connection..." << endl;
- //mLog::FINFO("--TiRayDR Func-- OpenDetector Start");
- // 检查连接状态
- bool wired = CheckConnect(m_strWiredIP);
- bool wireless = CheckConnect(m_strWirelessIP);
- cout << "[Detector_TiRayDR::OpenDetector] Connection status - Wired:" << (wired ? "Connected" : "Not connected")
- << ", wireless:" << (wireless ? "Connected" : "Not Connected") << endl;
- cout << "[Detector_TiRayDR::OpenDetector] Start scanning the detector..." << endl;
- std::vector<scan_result> scan_results;
- scan([&scan_results](scan_result&& result) {
- scan_results.emplace_back(std::move(result));
- });
- if (scan_results.empty()) {
- cout << "[Detector_TiRayDR::OpenDetector] No detectors were found!" << endl;
- return false;
- }
- cout << "[Detector_TiRayDR::OpenDetector] Scanned to" << scan_results.size() << " detector:" << endl;
- for (auto& res : scan_results) {
- cout << "[Detector_TiRayDR::OpenDetector] Serial number:" << res.sn
- << ", Model:" << res.model
- << ", tupper IP:" << res.upper_ip
- << ", detector IP:" << res.detector_ip << endl;
- }
- m_strDetectorType = (string)m_ModeConfig["DeviceName"];
- // 初始化启动参数
- string ip = "0.0.0.0";
- StartupOption option{};
- memcpy(option.Ip, ip.c_str(), std::min(ip.size(), sizeof(option.Ip) - 1));
- option.EnableLog = true;
- // 调用解析函数处理设备类型
- ModelResolveResult resolveResult = ResolveModelType(m_strDetectorType);
- if (!resolveResult.isValid) {
- return false;
- }
- // 启动探测器
- auto err = Startup_Ptr(resolveResult.model, on_event_callback, &option);
- if (err != Err_Success) {
- cout << "[Detector_TiRayDR::OpenDetector] Failed to start the detector" << endl;
- return false;
- }
- auto LoadCalibrationFile = [this](int calibrationParam)
- {
- TiRayError err;
- switch (calibrationParam) {
- case 1:
- err = write_attribute(Attr_CalibrationMode, CalibrationMode_None);
- break;
- case 2:
- err = write_attribute(Attr_CalibrationMode, CalibrationMode_Gain);
- break;
- case 3:
- err = write_attribute(Attr_CalibrationMode, CalibrationMode_Offset);
- break;
- case 4:
- err = write_attribute(Attr_CalibrationMode, CalibrationMode_Defect);
- break;
- default:
- cout << "[Detector_TiRayDR::OpenDetector] Invalid calibration parameter: " << calibrationParam << endl;
- return false;
- }
- if (err != Err_Success) {
- cout << "[Detector_TiRayDR::OpenDetector] Failed to write Attr_CalibrationMode, error code: " << err << endl;
- }
- else {
- cout << "[Detector_TiRayDR::OpenDetector] Succeeded in writing Attr_CalibrationMode" << endl;
- }
- return true;
- };
- m_strWiredIP = (std::string)m_ModeConfig["DetectorWiredIP"];
- m_strWirelessIP = (std::string)m_ModeConfig["DetectorWirelessIP"];
- m_strLocalIP = (std::string)m_ModeConfig["LocalIP"];
- m_strSerialNum = (std::string)m_ModeConfig["SerialNumber"];
- cout << "[Detector_TiRayDR::OpenDetector] Configuration parameters loaded - Wired IP:" << m_strWiredIP
- << ", Wireless IP:" << m_strWirelessIP << ", Local IP:" << m_strLocalIP << endl;
- //mLog::FINFO("WiredIP: {$}, LocalIP: {$}, WirelessIP: {$}, SerialNumber:{$}", m_strWiredIP, m_strLocalIP, m_strWirelessIP, m_strSerialNum);
-
- cout << "[Detector_TiRayDR::OpenDetector] Start setting the IP address..." << endl;
- err = SetIp_Ptr(m_strSerialNum.c_str(), m_strLocalIP.c_str(), m_strWiredIP.c_str(), nullptr);
- if (err != TiRayError::Err_Success)
- {
- cout << "[Detector_TiRayDR::OpenDetector] The IP setting failed, error code:" << err << " (will continue to execute)" << endl;
- }
- else {
- cout << "[Detector_TiRayDR::OpenDetector] IP setting was successful" << endl;
- }
- while (!m_bConnected)
- {
- usleep(10000);
- }
- m_pStPanelStatus[m_nCurrentPanelID]->bConnectState = true;
- ////mLog::FINFO("m_pStPanelStatus[m_nCurrentPanelID]->bConnectState = true");
- //StatusFeedback(EVT_STATUS_PANEL, PANEL_CONNECT);
- ////mLog::FINFO("Connect detector({$}) success", m_nDetectorID);
- if (m_strDetectorType == "TiRay_DY4343" || m_strDetectorType == "TiRay_DY4343D")
- {
- m_bUseGainV2 = true;
- }
- m_nImageWidth = (int)m_ModeConfig["ModeTable"][0]["ImageWidth"];
- m_nImageHeight = (int)m_ModeConfig["ModeTable"][0]["ImageHeight"];
- m_nRawImgWidth = (int)m_ModeConfig["ModeTable"][0]["RawImgWidth"];
- m_nRawImgHeight = (int)m_ModeConfig["ModeTable"][0]["RawImgHeight"];
- //mLog::FINFO("After crop image width: {$}, height: {$}, WidthOffset: {$}, HeightOffset: {$}", m_nImageWidth, m_nImageHeight, m_nWidthOffset, m_nHeightOffset);
- m_bSaveRaw = (int)m_ModeConfig["ModeTable"][0]["IsSaveRaw"];
- if (m_pRawImgBuffer == nullptr)
- {
- m_pRawImgBuffer = new WORD[(size_t)m_nRawImgHeight * (size_t)m_nRawImgWidth];
- }
- if (true)
- {
- m_pImgBuffer = new WORD[(size_t)m_nImageWidth * (size_t)m_nImageWidth];
- }
- // 加载校准文件
- cout << "[Detector_TiRayDR::OpenDetector] Start loading the calibration file..." << endl;
- //if (m_nCalibrationMode) //TiRayCalibration
- SetSyncMode(3);
- err = write_attribute(Attr_PhotoInterval, 3000);
- if (err != Err_Success)
- cout << "[Detector_TiRayDR::OpenDetector] Failed to write Attr_PhotoInterval. Error code: " << err << endl;
- err = write_attribute(Attr_PhotoNumber, 1);
- if (err != Err_Success)
- cout << "[Detector_TiRayDR::OpenDetector] Failed to write Attr_PhotoNumber. Error code: " << err << endl;
- LoadCalibrationFile(4);
- printf("read detector attributes......\n");
- /*auto [err1, work_mode] = read_attribute<int>(Attr_WorkMode);
- if (err1 != Err_Success)
- std::cout << "read Attr_WorkMode failed with error = " << err1 << std::endl;
- else
- std::cout << "read Attr_WorkMode " << work_mode << std::endl;
- auto [err2, photo_interval] = read_attribute<int>(Attr_PhotoInterval);
- if (err2 != Err_Success)
- std::cout << "read Attr_PhotoInterval failed with error = " << err2 << std::endl;
- else
- std::cout << "read Attr_PhotoInterval " << photo_interval << std::endl;
- auto [err3, photo_number] = read_attribute<int>(Attr_PhotoNumber);
- if (err3 != Err_Success)
- std::cout << "read Attr_PhotoNumber failed with error = " << err3 << std::endl;
- else
- std::cout << "read Attr_PhotoNumber " << photo_number << std::endl;
- auto [err4, calibration_mode] = read_attribute<int>(Attr_CalibrationMode);
- if (err4 != Err_Success)
- std::cout << "read Attr_CalibrationMode failed with error = " << err4 << std::endl;
- else
- std::cout << "read Attr_CalibrationMode " << calibration_mode << std::endl;*/
- //else
- //{
- // LoadCalibrationFile(3);
- // //mLog::Info("Load ZSKK Reference file");
- // m_pZSKKCalib->m_strRawImgPath = m_strWorkPath + "/rawdata/";
- // m_pZSKKCalib->m_strRefFilePath = m_strWorkPath + "/references/";
- // //读取配置文件中RAD模式的高和宽
- // m_pZSKKCalib->m_nFullImgWidth = m_nImageWidth;
- // m_pZSKKCalib->m_nFullImgHeight = m_nImageHeight;
- // m_pZSKKCalib->m_nSaturationValue = 50000;
- // if (!m_pZSKKCalib->LoadZSKKGainMap(true, m_strDetectorType))
- // {
- // //mLog::Warn("Load ZSKK Gain calibration failed");
- // }
- // if (!m_pZSKKCalib->LoadZSKKPixelMap(true, m_strDetectorType))
- // {
- // //mLog::Warn("Load ZSKK Defect calibration failed");
- // }
- //}
-
- SetTiRayDPCStatus(eDetStatus::DetStatus_Standby);
- //mLog::FINFO("OpenDetector Over");
- return true;
- }
- bool Detector_TiRayDR::CheckConnect(string strIP)
- {
- CMyPingip obPingIp;
- if (!obPingIp.PingFunction(strIP.c_str()))
- {
- //mLog::FINFO("ping {$} Failed", strIP);
- return false;
- }
- return true;
- }
- bool Detector_TiRayDR::OpenStatusMonitor()
- {
- //mLog::FINFO("---Open Status Monitor Thread---");
-
- if (m_hStatusMonitorThread == 0) // 检查线程是否已创建
- {
- int result = pthread_create(&m_hStatusMonitorThread, NULL, TiRayStatusMonitorThread, this);
- if (result != 0) {
- //mLog::FERROR("Failed to create status monitor thread: %d", result);
- return false;
- }
- }
- return true;
- }
- void* Detector_TiRayDR::TiRayStatusMonitorThread(PVOID pvoid)
- {
- Detector_TiRayDR* pCurrentPanelOpr = static_cast<Detector_TiRayDR*>(pvoid);
- if (pCurrentPanelOpr == nullptr)
- {
- //mLog::FERROR("TiRay Status Monitor parameter FERROR");
- return nullptr;
- }
- //mLog::FINFO("Begin StatusMonitor");
- DWORD dwStatusCheckTime = 5000;
- while (!pCurrentPanelOpr->m_bMonitorFlag)
- {
- pCurrentPanelOpr->StatusMonitor();
- usleep(pCurrentPanelOpr->m_nNotifyStatusTimePeriod*1000);
- }
- return 0;
- }
- bool Detector_TiRayDR::StatusMonitor()
- {
- if (!m_pStPanelStatus[m_nCurrentPanelID]->bConnectState)
- {
- //mLog::FERROR("Detector not connected, return");
- ErrorFeedback(EVT_ERR_COMMUNICATE, "true");
- return false;
- }
- //StatusType: 1:Temperature 2:Wifi 3:Battery
- //auto ReadStatus = [this](int StatusType)
- //{
- // TiRayVariant TempParam[1]{};
- // TempParam[0].Type = TiRayVariant::TiRayInt;
- //};
- int nTemperature = 0;
- int nWifiQuality = 0;
- int nBatteryLevel = 0;
- TiRayVariant TemperatureParam[1]{};
- TemperatureParam[0].Type = TiRayVariant::TiRayInt;
- TemperatureParam[0].IntValue = 0;
- Execute_Ptr(m_nDetectorID, Cmd_ReadAttribute, TemperatureParam, 1);
- nTemperature = TemperatureParam[0].IntValue;
- TiRayVariant WifiParam[1]{};
- WifiParam[0].Type = TiRayVariant::TiRayInt;
- WifiParam[0].IntValue = nWifiQuality;
- Execute_Ptr(m_nDetectorID, Cmd_ReadAttribute, WifiParam, 1);
- TiRayVariant BatteryParam[1]{};
- BatteryParam[0].Type = TiRayVariant::TiRayInt;
- BatteryParam[0].IntValue = nBatteryLevel;
- Execute_Ptr(m_nDetectorID, Cmd_ReadAttribute, BatteryParam, 1);
- StatusFeedback(EVT_STATUS_TEMPERATURE, 0, "", m_nCurrentPanelID, nTemperature);
- StatusFeedback(EVT_STATUS_BATTERY_VALUE, nBatteryLevel, "", m_nCurrentPanelID);
- StatusFeedback(EVT_STATUS_WIFI, nWifiQuality, "", m_nCurrentPanelID);
- return true;
- }
- bool Detector_TiRayDR::CloseStatusMonitor()
- {
- m_bMonitorFlag = true;
- m_hStatusMonitorThread = 0;
- //mLog::FINFO("---Close Status Monitor Thread---");
- return true;
- }
- bool Detector_TiRayDR::CloseDetectorScan()
- {
- m_hExitEvent->SetEvent();
- return false;
- }
- bool Detector_TiRayDR::LoadCalibrationFiles(int nCalibrationMode)
- {
- //mLog::FINFO("--TiRayDR Func-- LoadCalibrationFiles");
- int nRes;
- TiRayVariant Param[2]{};
- Param[0].Type = TiRayVariant::TiRayInt;
- Param[0].IntValue = TiRayAttribute::Attr_CalibrationMode;
- Param[1].Type = TiRayVariant::TiRayInt;
- if (nCalibrationMode == 1)
- {
- Param[1].IntValue = CalibrationMode::CalibrationMode_None;
- }
- else if (nCalibrationMode == 2)
- {
- Param[1].IntValue = CalibrationMode::CalibrationMode_Gain;
- }
- else if (nCalibrationMode == 3)
- {
- Param[1].IntValue = CalibrationMode::CalibrationMode_Offset;
- }
- else if (nCalibrationMode == 4)
- {
- Param[1].IntValue = CalibrationMode::CalibrationMode_Defect;
- //mLog::FINFO("All Calibration Mode");
- }
- nRes = Execute_Ptr(m_nDetectorID, Cmd_WriteAttribute, Param, 2);
- if ((TiRayError)nRes != TiRayError::Err_Success)
- {
- //mLog::Error("Use CalibrationMode Failed, Reason:{$}", nRes);
- return false;
- }
- else
- {
- //mLog::FINFO("Use CalibrationMode Success");
- }
- //mLog::FINFO("LoadCalibrationFiles Over");
- return true;
- }
- void* Detector_TiRayDR::onFPDScanThread(PVOID pvoid)
- {
- Detector_TiRayDR* pOpr = (Detector_TiRayDR*)pvoid;
- //mLog::FINFO("Enter scan thread");
- bool bExit = false;
- DWORD dwTimeOut = INFINITE;
- while (!bExit)
- {
- DWORD dwRet = LinuxEvent::WaitForMultipleEvents(pOpr->m_hArrayEvent,dwTimeOut);
- if (WAIT_OBJECT_0 == dwRet) //m_hInitEvent
- {
- pOpr->OnProcessInitFPD();
- }
- else if (WAIT_OBJECT_0 + 1 == dwRet) //m_hExitEvent
- {
- bExit = true;
- }
- else if (WAIT_OBJECT_0 + 2 == dwRet) //m_hReConnectEvent
- {
- pOpr->OnReconnectFPD();
- }
- else if (WAIT_OBJECT_0 + 3 == dwRet) //m_hRadEvent
- {
- //mLog::FINFO("[Get Rad Event]");
- pOpr->OpenRadAcquisition();
- }
- else if (WAIT_TIMEOUT == dwRet)
- {
- }
- }
- pOpr->m_hToggleEvent->SetEvent();
- //mLog::FINFO("Leave scan thread");
- return 0;
- }
- void Detector_TiRayDR::OnProcessInitFPD()
- {
- //mLog::FINFO("--TiRayDR Func-- OnProcessInitFPD");
- StatusFeedback(EVT_STATUS_INIT, PANEL_EVENT_START);
- if (!LoadDll(m_strWorkPath))
- {
- ErrorFeedback(EVT_ERR_INIT_FAILED, "true");
- StatusFeedback(EVT_STATUS_INIT, PANEL_EVENT_END_ERROR); //初始化失败
- return;
- }
- register_event_listener(OnEvent);
- //mLog::FINFO("Register Event Listener");
- if (!OpenDetector())
- {
- //mLog::FINFO("Open detector failed, Connect failed");
- ErrorFeedback(EVT_ERR_COMMUNICATE, "true");
- StatusFeedback(EVT_STATUS_INIT, PANEL_EVENT_END); //初始化时连接失败
- }
- else
- {
- m_pStPanelStatus[m_nCurrentPanelID]->bInitOver = true;
- StatusFeedback(EVT_STATUS_INIT, PANEL_EVENT_END_OK);
- }
- //mLog::FINFO("OnProcessInitFPD Over");
- }
- void Detector_TiRayDR::OnReconnectFPD()
- {
- //mLog::FINFO("OnReconnectFPD start");
- if (m_hReconnectThread == 0)
- {
- m_bReconnectThreadRunning = true;
- int result = pthread_create(&m_hReconnectThread, NULL, onReconnectThread, this);
- if (result != 0) {
- //mLog::FERROR("Failed to create reconnect thread: %d", result);
- m_hReconnectThread = 0;
- m_bReconnectThreadRunning = false;
- }
- }
- //mLog::FINFO("OnReconnectFPD end");
- }
- /***
- ** 获取一帧暗场图
- ***/
- int Detector_TiRayDR::DarkAcquisition()
- {
- const std::string funcTag = "[Detector_TiRayDR::DarkAcquisition] ";
- TiRayError err;
- // 设置校准模式为None
- err = write_attribute(Attr_CalibrationMode, CalibrationMode_None);
- if (err != Err_Success) {
- cout << funcTag << "Failed to write Attr_CalibrationMode. Error code: " << err << endl;
- return -1;
- }
- // 设置拍摄间隔
- err = write_attribute(Attr_PhotoInterval, 1000);
- if (err != Err_Success) {
- cout << funcTag << "Failed to write Attr_PhotoInterval. Error code: " << err << endl;
- return -1;
- }
- // 设置工作模式为空闲
- err = write_attribute(Attr_WorkMode, WorkMode_Idle);
- if (err != Err_Success) {
- cout << funcTag << "Failed to write Attr_WorkMode. Error code: " << err << endl;
- return -1;
- }
- // 执行拍摄命令
- err = Execute_Ptr(m_nDetectorID, Cmd_Photo, nullptr, 0);
- if (err != Err_Success) {
- cout << funcTag << "Failed to Execute_Ptr Cmd_Photo. Error code: " << err << endl;
- return -1;
- }
- return 0; // 成功执行返回0
- }
- /***
- ** 裁剪图像
- ** pOutImg: 裁剪后图像; pInImg: 裁剪前图像; nInWidth: 裁剪前图像宽度
- ***/
- bool Detector_TiRayDR::GetEffectiveImage(WORD* pOutImg, WORD* pInImg, int nInWidth)
- {
- if (pOutImg == NULL || pInImg == NULL || nInWidth < 0)
- {
- //mLog::FERROR("Illegal parameter, can not get effective image");
- return false;
- }
- try
- {
- for (int i = 0; i < m_nImageHeight; i++)
- {
- memcpy(pOutImg + i * m_nImageWidth, pInImg + (i + m_nHeightOffset) * nInWidth + m_nWidthOffset, m_nImageWidth * sizeof(WORD));
- }
- }
- catch (...)
- {
- //mLog::FERROR("Get effective image crashed");
- return false;
- }
- return true;
- }
- Detector_TiRayDR::eDetStatus Detector_TiRayDR::GetTiRayDPCStatus(int nDetectorIndex)
- {
- if (-1 == nDetectorIndex)
- {
- nDetectorIndex = m_nCurrentPanelID;
- }
- string strStatus = "Unknown";
- switch (m_pStPanelStatus[nDetectorIndex]->eFPDStatus)
- {
- case eDetStatus::DetStatus_NotIni:
- strStatus = "NotIni";
- break;
- case eDetStatus::DetStatus_NotConn:
- strStatus = "NotConn";
- break;
- case eDetStatus::DetStatus_Sleep:
- strStatus = "Sleep";
- break;
- case eDetStatus::DetStatus_Standby:
- strStatus = "Standby";
- break;
- case eDetStatus::DetStatus_Work:
- strStatus = "Work";
- break;
- case eDetStatus::DetStatus_Acquire:
- strStatus = "Acquire";
- break;
- case eDetStatus::DetStatus_Offset:
- strStatus = "Offset";
- break;
- case eDetStatus::DetStatus_XrayCalibration:
- strStatus = "XrayCalibration";
- break;
- default:
- break;
- }
- //mLog::FINFO("Driver status: {$}", strStatus.c_str());
- return m_pStPanelStatus[nDetectorIndex]->eFPDStatus;
- }
- bool Detector_TiRayDR::SetTiRayDPCStatus(eDetStatus status, int nDetectorIndex)
- {
- if (-1 == nDetectorIndex)
- {
- nDetectorIndex = m_nCurrentPanelID;
- }
- string strStatus = "Unknown";
- bool bSetStatus = true;
- switch (status)
- {
- case eDetStatus::DetStatus_NotIni:
- strStatus = "NotIni";
- break;
- case eDetStatus::DetStatus_NotConn:
- strStatus = "NotConn";
- break;
- case eDetStatus::DetStatus_Sleep:
- strStatus = "Sleep";
- break;
- case eDetStatus::DetStatus_Standby:
- strStatus = "Standby";
- break;
- case eDetStatus::DetStatus_Work:
- strStatus = "Work";
- break;
- case eDetStatus::DetStatus_Acquire:
- strStatus = "Acquire";
- break;
- case eDetStatus::DetStatus_Offset:
- strStatus = "Offset";
- break;
- case eDetStatus::DetStatus_XrayCalibration:
- strStatus = "XrayCalibration";
- break;
- default:
- bSetStatus = false;
- break;
- }
- if (bSetStatus)
- {
- m_pStPanelStatus[nDetectorIndex]->eFPDStatus = status;
- //mLog::FINFO("Set driver status: {$}", strStatus.c_str());
- }
- else
- {
- //mLog::FERROR("{$} {$} is a illegal status", strStatus, (int)status);
- }
- return bSetStatus;
- }
- bool Detector_TiRayDR::IsConnected(string strIP)
- {
- //mLog::FINFO("Check ping {$}", strIP);
- CMyPingip obPingIp;
- if (!obPingIp.PingFunction(strIP.c_str()))
- {
- //mLog::FINFO("ping {$} Failed", strIP);
- return false;
- }
- return true;
- }
- bool Detector_TiRayDR::CheckConnection()
- {
- return false;
- }
- bool Detector_TiRayDR::ReConnectFPD()
- {
- CloseStatusMonitor();
- return true;
- }
- bool Detector_TiRayDR::OpenRadAcquisition()
- {
- //mLog::FINFO("---Begin Rad Acquisition Thread---");
- m_bExitRadAcqStatus = true;
- if (m_hRadAcquisitionThread == 0)
- {
- int result = pthread_create(&m_hRadAcquisitionThread, NULL, RadAcquisitionThread, this);
- if (result != 0) {
- //mLog::FERROR("Failed to create status monitor thread: %d", result);
- return false;
- }
- }
- return true;
- }
- void* Detector_TiRayDR::RadAcquisitionThread(void* pParam)
- {
- Detector_TiRayDR* pCurrentPanelOpr = reinterpret_cast<Detector_TiRayDR*>(pParam);
- if (pCurrentPanelOpr == nullptr)
- {
- //mLog::FERROR("Query Acq Status Thread parameter FERROR");
- }
- pCurrentPanelOpr->PerformRadAcquisition();
- pCurrentPanelOpr->CloseRadAcquisition();
- return nullptr;
- }
- bool Detector_TiRayDR::PerformRadAcquisition()
- {
- //mLog::FINFO("## PerformRadAcquisition ##");
- //mLog::FINFO("Start Acquisition");
- //mLog::FINFO("DetectorID: {$}", m_nDetectorID);
- //int nRes = Execute_Ptr(m_nDetectorID, Cmd_Photo, nullptr, 0);
- //if ((TiRayError)nRes != TiRayError::Err_Success)
- //{
- // //mLog::FERROR("Start Acquisition Failed");
- //}
- while (true)
- {
- usleep(100000);
- if (!m_bExitRadAcqStatus)
- {
- break;
- }
- }
- m_hExitRadAcqStatus->SetEvent();
- //mLog::FINFO("PerformRadAcquisition Over");
-
- return true;
- }
- bool Detector_TiRayDR::CloseRadAcquisition()
- {
- m_bExitRadAcqStatus = false;
- bool dwResult = false;
- if (m_hExitRadAcqStatus) {
- dwResult = m_hExitRadAcqStatus->Wait(1000);
- }
- if (dwResult)
- {
- //mLog::FINFO("[Get ExitRadAcqStatus Event]");
- }
- else
- {
- if (m_hRadAcquisitionThread != 0) {
- pthread_cancel(m_hRadAcquisitionThread);
- // 等待线程实际退出
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- ts.tv_sec += 1; // 等待1秒
- pthread_timedjoin_np(m_hRadAcquisitionThread, nullptr, &ts);
- //mLog::FWARN("Kill QueryAcqStatus Thread");
- }
- //mLog::FWARN("Kill QueryAcqStatus Thread");
- }
- m_hRadAcquisitionThread = 0;
- //mLog::FINFO("---Exit Rad Acq Status Thread---");
- return true;
- }
- /***
- ** 检测接口是否有错误,true:有错;false:没错
- ***/
- bool Detector_TiRayDR::TestError(TiRayError nErrorCode)
- {
- if (nErrorCode == TiRayError::Err_Success)
- {
- return true;
- }
- else
- {
- switch (nErrorCode)
- {
- case TiRayError::Err_SystemFailure:
- {
- //mLog::FERROR("System Internal Error");
- break;
- }
- case TiRayError::Err_WrongModel:
- {
- //mLog::FERROR("The specified model is incorrect");
- break;
- }
- case TiRayError::Err_DetectorNonExists:
- {
- //mLog::FERROR("The specified detector with the given ID dose not exist");
- break;
- }
- case TiRayError::Err_NetworkFailure:
- {
- //mLog::FERROR("The network communication has failed");
- break;
- }
- case TiRayError::Err_InvalidParam:
- {
- //mLog::FERROR("The provided parameters are incorrect");
- break;
- }
- case TiRayError::Err_UploadInProgress:
- {
- //mLog::FERROR("The upload is in progress");
- break;
- }
- default:
- //mLog::FERROR("Unkonwn Error");
- break;
- }
- return false;
- }
- }
- void Detector_TiRayDR::register_event_listener(const EventListener& fn)
- {
- const auto iter = std::find_if(m_listeners.begin(), m_listeners.end(), [&fn](auto& listener) {
- return listener.template target<EventListenerType>() == fn.template target<EventListenerType>();
- });
- if (iter == m_listeners.end())
- m_listeners.push_back(fn);
- }
- void Detector_TiRayDR::unregister_event_listener(const EventListener& fn)
- {
- const auto iter = std::find_if(m_listeners.begin(), m_listeners.end(), [&fn](auto& listener) {
- return listener.template target<EventListenerType>() == fn.template target<EventListenerType>();
- });
- if (iter == m_listeners.end())
- m_listeners.erase(iter);
- }
- bool Detector_TiRayDR::cropImage(unsigned short* srcData, int srcWidth, int srcHeight,
- unsigned short* destData, int destWidth, int destHeight)
- {
- // 验证指针有效性
- if (srcData == nullptr || destData == nullptr) {
- cout << "[Detector_TiRayDR::cropImage] Error: Source or destination pointer is null" << endl;
- return false;
- }
- // 验证尺寸合法性
- if (destWidth <= 0 || destHeight <= 0 ||
- srcWidth <= 0 || srcHeight <= 0 ||
- destWidth > srcWidth || destHeight > srcHeight)
- {
- cout << "[Detector_TiRayDR::cropImage] Invalid crop size: src("
- << srcWidth << "x" << srcHeight << ") dest("
- << destWidth << "x" << destHeight << ")" << endl;
- return false;
- }
- // 计算裁剪起始位置
- int startX = (srcWidth - destWidth) / 2;
- int startY = (srcHeight - destHeight) / 2;
- // 边界检查
- if (startX < 0) startX = 0;
- if (startY < 0) startY = 0;
- // 确保裁剪区域不会超出源图像边界
- if (startX + destWidth > srcWidth) {
- destWidth = srcWidth - startX;
- cout << "[Detector_TiRayDR::cropImage] Adjusted destWidth to " << destWidth
- << " to avoid out-of-bounds access" << endl;
- }
- if (startY + destHeight > srcHeight) {
- destHeight = srcHeight - startY;
- cout << "[Detector_TiRayDR::cropImage] Adjusted destHeight to " << destHeight
- << " to avoid out-of-bounds access" << endl;
- }
- // 逐行复制裁剪区域
- for (int y = 0; y < destHeight; ++y)
- {
- // 计算源图像行起始地址
- const unsigned short* srcRow = srcData + (startY + y) * srcWidth + startX;
- // 计算目标图像行起始地址
- unsigned short* destRow = destData + y * destWidth;
- // 复制一行像素
- memcpy(destRow, srcRow, destWidth * sizeof(unsigned short));
- }
- cout << "[Detector_TiRayDR::cropImage] Successfully cropped image from "
- << srcWidth << "x" << srcHeight << " to " << destWidth << "x" << destHeight << endl;
- return true;
- }
- std::string Detector_TiRayDR::saveProcessedImage(Detector_TiRayDR& detector, unsigned short* data, size_t size, const std::string& baseTimestamp)
- {
- const size_t MAX_PROC_FILE_COUNT = 10;
- const std::string TMPFS_DIR = "/mnt/tmpfs/";
- const std::string ORIGINAL_DIR = detector.m_strWorkPath + "/RawData/";
- std::string procDir;
- bool useTmpfs = false;
- // 确定存储目录
- if (std::filesystem::exists(TMPFS_DIR) && std::filesystem::is_directory(TMPFS_DIR))
- {
- procDir = TMPFS_DIR;
- useTmpfs = true;
- std::cout << "[Detector_TiRayDR::saveProcessedImage] Using tmpfs directory: " << procDir << std::endl;
- }
- else
- {
- procDir = ORIGINAL_DIR;
- std::cout << "[Detector_TiRayDR::saveProcessedImage] /mnt/tmpfs not found, using original directory: " << procDir << std::endl;
- }
- const std::string FILE_PREFIX = "Proc_";
- std::string filePath;
- try
- {
- std::filesystem::create_directories(procDir);
- filePath = procDir + FILE_PREFIX + baseTimestamp + ".raw";
- std::ofstream file(filePath, std::ios::binary);
- if (file.is_open())
- {
- file.write(reinterpret_cast<const char*>(detector.m_pImgBuffer), size);
- std::cout << "[Detector_TiRayDR::saveProcessedImage] Saved image: " << filePath << std::endl;
- }
- else
- {
- std::cout << "[Detector_TiRayDR::saveProcessedImage] Failed to open file: " << filePath << std::endl;
- return "";
- }
- cleanOldFiles(procDir, FILE_PREFIX, MAX_PROC_FILE_COUNT, useTmpfs);
- }
- catch (const std::filesystem::filesystem_error& e)
- {
- std::cout << "[Detector_TiRayDR::saveProcessedImage] File system error: " << e.what() << std::endl;
- return "";
- }
- catch (const std::exception& e)
- {
- std::cout << "[Detector_TiRayDR::saveProcessedImage] Unexpected error: " << e.what() << std::endl;
- return "";
- }
- return filePath;
- }
- void Detector_TiRayDR::saveRawImage(Detector_TiRayDR& detector, unsigned short* data, size_t size, const std::string& baseTimestamp)
- {
- const std::string RAW_DIR = detector.m_strWorkPath + "/OriginalData/";
- const std::string FILE_PREFIX = "orig_";
- const size_t MAX_RAW_FILE_COUNT = 20;
- std::string filePath;
- try
- {
- std::filesystem::create_directories(RAW_DIR);
- filePath = RAW_DIR + FILE_PREFIX + baseTimestamp + ".raw";
- std::ofstream file(filePath, std::ios::binary);
- if (file.is_open())
- {
- // 直接写入unsigned short缓冲区数据
- file.write(reinterpret_cast<const char*>(data), size);
- cout << "[Detector_TiRayDR::saveRawImage] Saved raw image: " << filePath << endl;
- }
- else
- {
- cout << "[Detector_TiRayDR::saveRawImage] Failed to open file: " << filePath << endl;
- return;
- }
- // 清理旧的原始图
- cleanOldFiles(RAW_DIR, FILE_PREFIX, MAX_RAW_FILE_COUNT, false);
- }
- catch (const std::filesystem::filesystem_error& e)
- {
- cout << "[Detector_TiRayDR::saveRawImage] File system error: " << e.what() << endl;
- }
- }
- std::filesystem::space_info getFileSystemInfo(const std::string& dirPath) {
- try {
- return std::filesystem::space(dirPath);
- }
- catch (const std::filesystem::filesystem_error& e) {
- std::cout << "[getFileSystemInfo] Failed to get filesystem info for "
- << dirPath << ": " << e.what() << std::endl;
- return {};
- }
- }
- void Detector_TiRayDR::cleanOldFiles(const std::string& dirPath, const std::string& filePrefix,
- size_t maxCount, bool checkSize)
- {
- const std::string funcTag = "[Detector_TiRayDR::cleanOldFiles] ";
- try
- {
- // 检查目录是否存在
- if (!std::filesystem::exists(dirPath) || !std::filesystem::is_directory(dirPath))
- {
- std::cout << funcTag << "Directory not found: " << dirPath << std::endl;
- return;
- }
- std::vector<std::filesystem::directory_entry> targetFiles;
- for (const auto& entry : std::filesystem::directory_iterator(dirPath))
- {
- // 只处理常规文件
- if (!entry.is_regular_file())
- continue;
- // 检查文件扩展名是否为.raw
- const std::string extension = entry.path().extension().string();
- if (extension != ".raw")
- continue;
- // 检查文件名是否以指定前缀开头
- const std::string filename = entry.path().filename().string();
- if (filename.length() < filePrefix.length())
- continue;
- if (filename.substr(0, filePrefix.length()) == filePrefix)
- {
- targetFiles.push_back(entry);
- }
- }
- // 按文件最后修改时间排序(最旧的在前)
- std::sort(targetFiles.begin(), targetFiles.end(),
- [](const std::filesystem::directory_entry& a, const std::filesystem::directory_entry& b)
- {
- return std::filesystem::last_write_time(a) < std::filesystem::last_write_time(b);
- });
- // 1. 先按数量限制清理
- size_t filesToDelete = 0;
- if (targetFiles.size() > maxCount)
- {
- filesToDelete = targetFiles.size() - maxCount;
- // 删除超出数量限制的最旧文件
- for (size_t i = 0; i < filesToDelete; ++i)
- {
- const auto& fileToDelete = targetFiles[i];
- if (std::filesystem::remove(fileToDelete.path()))
- {
- std::cout << funcTag << "Deleted old file (count limit): " << fileToDelete.path() << std::endl;
- }
- else
- {
- std::cout << funcTag << "Failed to delete file: " << fileToDelete.path() << std::endl;
- }
- }
- // 更新目标文件列表(移除已删除的文件)
- targetFiles.erase(targetFiles.begin(), targetFiles.begin() + filesToDelete);
- filesToDelete = 0; // 重置计数器用于大小检查
- }
- // 2. 如果需要,按大小限制清理(仅对tmpfs目录)
- if (checkSize && !targetFiles.empty())
- {
- // 获取文件系统信息
- auto fsInfo = getFileSystemInfo(dirPath);
- if (fsInfo.capacity == 0) // 获取信息失败
- {
- std::cout << funcTag << "Cannot check filesystem size, skipping size check" << std::endl;
- return;
- }
- // 计算最大允许使用空间(预留10%作为缓冲)
- const uintmax_t reservedSpace = fsInfo.capacity / 10; // 10%预留空间
- const uintmax_t maxAllowedSize = fsInfo.capacity - reservedSpace;
- // 计算当前文件总大小
- auto calculateTotalSize = [](const std::vector<std::filesystem::directory_entry>& files) {
- uintmax_t total = 0;
- for (const auto& file : files) {
- total += file.file_size();
- }
- return total;
- };
- uintmax_t currentTotalSize = calculateTotalSize(targetFiles);
- std::cout << funcTag << "Current files total size: " << currentTotalSize
- << ", Max allowed size: " << maxAllowedSize << std::endl;
- // 如果当前总大小超过限制,继续删除最旧的文件
- while (currentTotalSize > maxAllowedSize && !targetFiles.empty())
- {
- const auto& fileToDelete = targetFiles[0];
- uintmax_t fileSize = fileToDelete.file_size();
- if (std::filesystem::remove(fileToDelete.path()))
- {
- std::cout << funcTag << "Deleted old file (size limit): " << fileToDelete.path()
- << " (" << fileSize << " bytes)" << std::endl;
- // 更新总量和文件列表
- currentTotalSize -= fileSize;
- targetFiles.erase(targetFiles.begin());
- filesToDelete++;
- }
- else
- {
- std::cout << funcTag << "Failed to delete file: " << fileToDelete.path() << std::endl;
- break; // 删除失败时停止处理
- }
- }
- }
- std::cout << funcTag << "Cleanup completed. Deleted " << filesToDelete
- << " files. Remaining: " << targetFiles.size() << std::endl;
- }
- catch (const std::filesystem::filesystem_error& e)
- {
- std::cout << funcTag << "File system error: " << e.what() << std::endl;
- }
- catch (const std::exception& e)
- {
- std::cout << funcTag << "Unexpected error: " << e.what() << std::endl;
- }
- }
- void Detector_TiRayDR::handleHardwareSyncImage(Detector_TiRayDR& detector, TiRayVariant argv[])
- {
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_ON);
- const size_t rawPixelCount = argv[3].DataLen / sizeof(unsigned short);
- detector.m_pRawImgBuffer = reinterpret_cast<unsigned short*>(argv[3].DataValue);
- const size_t rawSize = rawPixelCount * sizeof(unsigned short);
- bool cropSuccess = cropImage(
- detector.m_pRawImgBuffer,
- detector.m_nRawImgWidth, detector.m_nRawImgHeight, // 原始图像宽高
- detector.m_pImgBuffer,
- detector.m_nImageWidth, detector.m_nImageHeight // 目标宽高
- );
- if (!cropSuccess)
- {
- cout << "[Detector_TiRayDR::handleAedSyncImage] Failed to crop image" << endl;
- return;
- }
- const size_t processedSize = detector.m_nImageWidth * detector.m_nImageHeight * sizeof(unsigned short);
- // 生成关联的基础文件名
- const std::string baseTimestamp = std::to_string(
- std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::system_clock::now().time_since_epoch()
- ).count()
- );
- std::string processedImagePath = saveProcessedImage(
- detector,
- detector.m_pImgBuffer,
- processedSize,
- baseTimestamp
- );
- if (detector.m_bSaveRaw)
- {
- saveRawImage(
- detector,
- detector.m_pRawImgBuffer,
- rawSize,
- baseTimestamp
- );
- }
- // 应用校准
- // applyCalibration(detector);
- detector.InfoFeedback(EVT_DATA_RAW_IMAGE, -1, 0, 0, processedImagePath.c_str());
- //detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_OFF);
- detector.m_bExitRadAcqStatus = false;
- }
- void Detector_TiRayDR::handleSoftwareSyncImage(Detector_TiRayDR& detector, TiRayVariant argv[])
- {
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_ON);
- const size_t rawPixelCount = argv[3].DataLen / sizeof(unsigned short);
- detector.m_pRawImgBuffer = reinterpret_cast<unsigned short*>(argv[3].DataValue);
- const size_t rawSize = rawPixelCount * sizeof(unsigned short);
- bool cropSuccess = cropImage(
- detector.m_pRawImgBuffer,
- detector.m_nRawImgWidth, detector.m_nRawImgHeight, // 原始图像宽高
- detector.m_pImgBuffer,
- detector.m_nImageWidth, detector.m_nImageHeight // 目标宽高
- );
- if (!cropSuccess)
- {
- cout << "[Detector_TiRayDR::handleSoftwareSyncImage] Failed to crop image" << endl;
- return;
- }
- const size_t processedSize = detector.m_nImageWidth * detector.m_nImageHeight * sizeof(unsigned short);
- // 生成关联的基础文件名
- const std::string baseTimestamp = std::to_string(
- std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::system_clock::now().time_since_epoch()
- ).count()
- );
- std::string processedImagePath = saveProcessedImage(
- detector,
- detector.m_pImgBuffer,
- processedSize,
- baseTimestamp
- );
- if (detector.m_bSaveRaw)
- {
- saveRawImage(
- detector,
- detector.m_pRawImgBuffer,
- rawSize,
- baseTimestamp
- );
- }
- detector.InfoFeedback(EVT_DATA_RAW_IMAGE, -1, 0, 0, processedImagePath.c_str());
- // 应用校准
- // applyCalibration(detector);
- //detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_OFF);
- detector.m_bExitRadAcqStatus = false;
- }
- void Detector_TiRayDR::handleAedSyncImage(Detector_TiRayDR& detector, TiRayVariant argv[])
- {
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_START_ACQ);
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XRAY_ON);
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_ON);
- const size_t rawPixelCount = argv[3].DataLen / sizeof(unsigned short);
- detector.m_pRawImgBuffer = reinterpret_cast<unsigned short*>(argv[3].DataValue);
- const size_t rawSize = rawPixelCount * sizeof(unsigned short);
- bool cropSuccess = cropImage(
- detector.m_pRawImgBuffer,
- detector.m_nRawImgWidth, detector.m_nRawImgHeight, // 原始图像宽高
- detector.m_pImgBuffer,
- detector.m_nImageWidth, detector.m_nImageHeight // 目标宽高
- );
- if (!cropSuccess)
- {
- cout << "[Detector_TiRayDR::handleAedSyncImage] Failed to crop image" << endl;
- return;
- }
- const size_t processedSize = detector.m_nImageWidth * detector.m_nImageHeight * sizeof(unsigned short);
- // 生成关联的基础文件名
- const std::string baseTimestamp = std::to_string(
- std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::system_clock::now().time_since_epoch()
- ).count()
- );
- std::string processedImagePath = saveProcessedImage(
- detector,
- detector.m_pImgBuffer,
- processedSize,
- baseTimestamp
- );
- if (detector.m_bSaveRaw)
- {
- saveRawImage(
- detector,
- detector.m_pRawImgBuffer,
- rawSize,
- baseTimestamp
- );
- }
- // applyCalibration(detector);
- detector.InfoFeedback(EVT_DATA_RAW_IMAGE, -1, 0, 0, processedImagePath.c_str());
- detector.m_bAEDWorkFlag = false;
- //usleep(500000);
- //detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_XWINDOW_OFF);
- detector.m_bExitRadAcqStatus = false;
- }
- void Detector_TiRayDR::handleDarkCalibration(Detector_TiRayDR& detector, TiRayVariant argv[])
- {
- const std::string funcTag = "[Detector_TiRayDR::handleDarkCalibration] ";
- static int callCount = 0;
- static std::vector<TiRayVariant> datas;
- // 处理当前传入的图像数据
- auto size = argv[3].DataLen;
- auto data = argv[3].DataValue;
- image_info image;
- image.width = argv[0].IntValue;
- image.height = argv[1].IntValue;
- image.data.resize(size);
- memcpy(image.data.data(), data, size);
- // 将当前图像数据存入集合
- TiRayVariant variant;
- variant.Type = TiRayVariant::TiRayBuffer;
- variant.DataLen = static_cast<int>(image.data.size());
- variant.DataValue = image.data.data();
- datas.push_back(variant);
- // 调用次数递增(当前已处理的图像数量)
- callCount++;
- // 若未收集到6张图,调用拍摄命令准备下一张
- if (callCount < 6) {
- auto err = Execute_Ptr(detector.m_nDetectorID, Cmd_Photo, nullptr, 0);
- if (err != Err_Success) {
- cout << funcTag << "Failed to execute Cmd_Photo for next image. Error code: " << err << endl;
- // 重置状态,避免异常累积
- callCount = 0;
- datas.clear();
- return;
- }
- cout << funcTag << "Prepared for " << callCount + 1 << "th image" << endl;
- }
- // 收集到6张图,执行模板生成和上传
- else {
- image_info template_image;
- template_image.data.resize(datas[0].DataLen);
- template_image.width = image.width;
- template_image.height = image.height;
- // 生成模板并检查结果
- auto err = GenerateTemplate_Ptr(Offset, datas.data(), static_cast<int>(datas.size()),
- template_image.data.data(), static_cast<int>(template_image.data.size()));
- if (err != Err_Success) {
- cout << funcTag << "Failed to generate offset template. Error code: " << err << endl;
- }
- else {
- cout << funcTag << "Offset template generated successfully with 6 images" << endl;
- // 上传模板并检查结果
- err = detector.upload(detector.offset, template_image.data.data(), static_cast<int>(template_image.data.size()));
- if (err != Err_Success) {
- cout << funcTag << "Failed to upload offset template. Error code: " << err << endl;
- }
- else {
- cout << funcTag << "Offset template uploaded successfully" << endl;
- }
- }
- // 重置静态变量,为下一次校准做准备
- callCount = 0;
- datas.clear();
- }
- }
- void Detector_TiRayDR::handleGainCalibration(Detector_TiRayDR& detector, TiRayVariant argv[])
- {
- const std::string funcTag = "[Detector_TiRayDR::handleGainCalibration] ";
- int doseCount = 5; // 剂量数量
- int imagesPerDose = 6; // 每个剂量的图像数量
- auto size = argv[3].DataLen;
- auto data = argv[3].DataValue;
- // 创建图像信息并存储
- image_info image;
- image.width = argv[0].IntValue;
- image.height = argv[1].IntValue;
- image.data.resize(size);
- memcpy(image.data.data(), data, size);
- // 根据模式存储图像
- if (!detector.m_bUseGainV2)
- {
- // Gain模式:直接存储所有30张图像
- detector.m_gainCalibImages.push_back(image);
- cout << funcTag << "Gain mode - Collected " << detector.m_gainCalibImages.size()
- << "/" << doseCount * imagesPerDose << " images" << endl;
- // 收集完30张图像后生成并上传模板
- if (detector.m_gainCalibImages.size() == doseCount * imagesPerDose)
- {
- cout << funcTag << "All 30 gain calibration images collected, generating template..." << endl;
- // 准备数据并生成Gain模板
- std::vector<TiRayVariant> datas;
- for (auto& img : detector.m_gainCalibImages)
- {
- TiRayVariant variant;
- variant.Type = TiRayVariant::TiRayBuffer;
- variant.DataLen = static_cast<int>(img.data.size());
- variant.DataValue = img.data.data();
- datas.push_back(variant);
- }
- image_info template_image;
- template_image.width = detector.m_gainCalibImages[0].width;
- template_image.height = detector.m_gainCalibImages[0].height;
- template_image.data.resize(datas[0].DataLen);
- auto err = GenerateTemplate_Ptr(Gain, datas.data(), static_cast<int>(datas.size()),
- template_image.data.data(), static_cast<int>(template_image.data.size()));
- if (err != Err_Success)
- {
- cout << funcTag << "Failed to generate Gain template. Error code: " << err << endl;
- }
- else
- {
- cout << funcTag << "Gain template generated successfully" << endl;
- // 上传模板
- err = detector.upload(detector.gain, template_image.data.data(),
- static_cast<int>(template_image.data.size()));
- if (err != Err_Success)
- {
- cout << funcTag << "Failed to upload Gain template. Error code: " << err << endl;
- }
- else
- {
- cout << funcTag << "Gain template uploaded successfully" << endl;
- }
- }
- // 清空存储的图像,准备下次校正
- detector.m_gainCalibImages.clear();
- }
- }
- else if (detector.m_bUseGainV2)
- {
- // Gainv2模式:按剂量组存储图像
- detector.m_currentDoseImages.push_back(image);
- cout << funcTag << "Gainv2 mode - Collected " << detector.m_currentDoseImages.size()
- << "/" << imagesPerDose << " images for dose " << detector.m_currentDoseIndex + 1 << endl;
- // 收集完当前剂量的6张图像后生成均值图
- if (detector.m_currentDoseImages.size() == imagesPerDose)
- {
- cout << funcTag << "All " << imagesPerDose << " images for dose " << detector.m_currentDoseIndex + 1
- << " collected, generating mean image..." << endl;
- // 生成当前剂量的均值图
- std::vector<TiRayVariant> datas;
- for (auto& img : detector.m_currentDoseImages)
- {
- TiRayVariant variant;
- variant.Type = TiRayVariant::TiRayBuffer;
- variant.DataLen = static_cast<int>(img.data.size());
- variant.DataValue = img.data.data();
- datas.push_back(variant);
- }
- image_info mean_image;
- mean_image.width = detector.m_currentDoseImages[0].width;
- mean_image.height = detector.m_currentDoseImages[0].height;
- mean_image.data.resize(datas[0].DataLen);
- auto err = GenerateTemplate_Ptr(Mean, datas.data(), static_cast<int>(datas.size()),
- mean_image.data.data(), static_cast<int>(mean_image.data.size()));
- if (err != Err_Success)
- {
- cout << funcTag << "Failed to generate mean image for dose " << detector.m_currentDoseIndex + 1
- << ". Error code: " << err << endl;
- // 清空当前剂量数据,重新收集
- detector.m_currentDoseImages.clear();
- return;
- }
- // 保存均值图并重置当前剂量图像集合
- detector.m_gainV2MeanImages.push_back(mean_image);
- detector.m_currentDoseImages.clear();
- detector.m_currentDoseIndex++;
- cout << funcTag << "Mean image for dose " << detector.m_currentDoseIndex
- << " generated successfully. Total mean images: " << detector.m_gainV2MeanImages.size() << endl;
- // 收集完5个剂量的均值图后生成最终模板
- if (detector.m_gainV2MeanImages.size() == doseCount)
- {
- cout << funcTag << "All " << doseCount << " mean images collected, generating final GainV2 template..." << endl;
- // 生成最终的GainV2模板
- std::vector<TiRayVariant> meanDatas;
- for (auto& meanImg : detector.m_gainV2MeanImages)
- {
- TiRayVariant variant;
- variant.Type = TiRayVariant::TiRayBuffer;
- variant.DataLen = static_cast<int>(meanImg.data.size());
- variant.DataValue = meanImg.data.data();
- meanDatas.push_back(variant);
- }
- image_info final_template;
- final_template.width = detector.m_gainV2MeanImages[0].width;
- final_template.height = detector.m_gainV2MeanImages[0].height;
- final_template.data.resize(meanDatas[0].DataLen);
- auto err = GenerateTemplate_Ptr(GainV2, meanDatas.data(), static_cast<int>(meanDatas.size()),
- final_template.data.data(), static_cast<int>(final_template.data.size()));
- if (err != Err_Success)
- {
- cout << funcTag << "Failed to generate GainV2 template. Error code: " << err << endl;
- }
- else
- {
- cout << funcTag << "GainV2 template generated successfully" << endl;
- // 上传模板
- err = detector.upload(detector.gain, final_template.data.data(),
- static_cast<int>(final_template.data.size()));
- if (err != Err_Success)
- {
- cout << funcTag << "Failed to upload GainV2 template. Error code: " << err << endl;
- }
- else
- {
- cout << funcTag << "GainV2 template uploaded successfully" << endl;
- }
- }
- // 重置所有存储和计数器,准备下次校正
- detector.m_gainV2MeanImages.clear();
- detector.m_currentDoseIndex = 0;
- }
- }
- }
- }
- void Detector_TiRayDR::handleImageReceived(Detector_TiRayDR& detector, TiRayVariant argv[])
- {
- // 处理暗校准模式
- if (CCOS_CALIBRATION_TYPE_DARK == detector.m_eCaliType)
- {
- handleDarkCalibration(detector, argv);
- return;
- }
- else if (CCOS_CALIBRATION_TYPE_XRAY == detector.m_eCaliType)
- {
- handleGainCalibration(detector, argv);
- return;
- }
- // 根据同步模式处理图像
- switch (detector.m_nSyncMode)
- {
- case SYNC_MODE::SYNC_AED:
- handleAedSyncImage(detector, argv);
- break;
- case SYNC_MODE::SYNC_SOFTWARE:
- handleSoftwareSyncImage(detector, argv);
- break;
- case SYNC_MODE::SYNC_HARDWARE:
- handleHardwareSyncImage(detector, argv);
- break;
- }
- }
- void Detector_TiRayDR::on_event_callback(int detectorId, TiRayEvent eventType, TiRayVariant argv[], int argc, void* reservedArg, int reservedArgLen)
- {
- auto& detector = *g_pDetector;
- const std::string funcTag = "[Detector_TiRayDR::on_event_callback] ";
- switch (eventType)
- {
- case TiRayEvent::Evt_DetectorConnect:
- {
- detector.m_nDetectorID = detectorId;
- detector.m_bConnected = true;
- detector.m_pStPanelStatus[detector.m_nCurrentPanelID]->bConnectState = true;
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_CONNECT);
- cout << funcTag << "Evt_DetectorConnect!!!" << endl;
- //mLog::FINFO("Evt_DetectorConnect!!");
- break;
- }
- case TiRayEvent::Evt_DetectorDisconnect:
- {
- detector.m_nDetectorID = INT_MAX;
- detector.m_bConnected = false;
- detector.m_pStPanelStatus[detector.m_nCurrentPanelID]->bConnectState = false;
- detector.StatusFeedback(EVT_STATUS_PANEL, PANEL_CLOSE);
- detector.m_hReConnectEvent->SetEvent();
- cout << funcTag << "Evt_DetectorDisconnect!!!" << endl;
- //mLog::FINFO("Evt_DetectorDisconnect!!");
- break;
- }
- case TiRayEvent::Evt_WriteAttribute:
- {
- //mLog::FINFO("WriteAttribute Success");
- break;
- }
- case TiRayEvent::Evt_ReadAttribute:
- {
- //mLog::FINFO("ReadAttribute Success");
- //const TiRayAttribute attrib = (TiRayAttribute)argv[0].IntValue;
- //if (attrib == Attr_BatteryStatus) {
- // auto battery_level = argv[1].IntValue;
- // auto is_recharging = argv[2].IntValue;
- //}
- break;
- }
- case TiRayEvent::Evt_ImageReceived:
- {
- cout << funcTag << "Evt_ImageReceived!!!" << endl;
- //mLog::FINFO("Evt_ImageReceived!!");
- handleImageReceived(detector, argv);
- break;
- }
- case TiRayEvent::Evt_ExposureStatus:
- {
- //mLog::FINFO("argv {$}, argc {$}", argv[0].IntValue, argc);
- if (argv[0].IntValue == 0)
- {
- detector.m_bAEDReady = true;
- }
- if (argv[0].IntValue == 1)
- {
- detector.m_bAEDReady = false;
- }
- break;
- }
- case TiRayEvent::Evt_UploadProgress:
- cout << funcTag << "Upload progress: " << argv[0].IntValue << "%" << endl;
- break;
- case TiRayEvent::Evt_UpdateFinish:
- cout << funcTag << "Update completed successfully" << endl;
- detector.StatusFeedback(EVT_STATUS_CALIBRATIOIN, PANEL_EVENT_END_OK);
- break;
- case TiRayEvent::Evt_UploadTimeout:
- // 输出上传超时警告
- cout << funcTag << "Warning: Upload timed detectorData" << endl;
- break;
- default:
- //mLog::FERROR("not support current event ID:{$}", (int)eventType);
- break;
- }
- }
- void Detector_TiRayDR::ConfFeedback(int nEventID, int nDetectorID, const char* pszMsg, int nParam1, float fParam2, int nPtrParamLen, void* pParam)
- {
- if (-1 == nDetectorID)
- {
- nDetectorID = m_nCurrentPanelID;
- }
- ((FPDDeviceTiRay*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
- nEventID, EVT_LEVEL_CONFIGURATION, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
- }
- void Detector_TiRayDR::InfoFeedback(int nEventID, int nDetectorID, int nParam1, float fParam2, const char* pszMsg, int nPtrParamLen, void* pParam)
- {
- if (-1 == nDetectorID)
- {
- nDetectorID = m_nCurrentPanelID;
- }
- ((FPDDeviceTiRay*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
- nEventID, EVT_LEVEL_INFORMATOION, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
- }
- void Detector_TiRayDR::StatusFeedback(int nEventID, int nParam1, const char* pszMsg, int nDetectorID, float fParam2, int nPtrParamLen, void* pParam)
- {
- if (-1 == nDetectorID)
- {
- nDetectorID = m_nCurrentPanelID;
- }
- ((FPDDeviceTiRay*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
- nEventID, EVT_LEVEL_STATUS, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
- }
- void Detector_TiRayDR::DataFeedback(int nEventID, void* pParam, int nParam1, float fParam2, const char* pszMsg, int nPtrParamLen, int nDetectorID)
- {
- if (-1 == nDetectorID)
- {
- nDetectorID = m_nCurrentPanelID;
- }
- ((FPDDeviceTiRay*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
- nEventID, EVT_LEVEL_DATA, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
- }
- void Detector_TiRayDR::WarnFeedback(int nEventID, const char* pszMsg, int nParam1, float fParam2, int nPtrParamLen, void* pParam, int nDetectorID)
- {
- if (-1 == nDetectorID)
- {
- nDetectorID = m_nCurrentPanelID;
- }
- ((FPDDeviceTiRay*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
- nEventID, EVT_LEVEL_WARNING, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
- }
- void Detector_TiRayDR::ErrorFeedback(int nEventID, const char* pszMsg, int nDetectorID, int nParam1, float fParam2, int nPtrParamLen, void* pParam)
- {
- if (-1 == nDetectorID)
- {
- nDetectorID = m_nCurrentPanelID;
- }
- ((FPDDeviceTiRay*)(*m_pPanelID2DPC)[nDetectorID])->OnFPDCallback(nDetectorID,
- nEventID, EVT_LEVEL_ERROR, pszMsg, nParam1, fParam2, nPtrParamLen, pParam);
- }
- bool Detector_TiRayDR::UpdateCalibMode(CCOS_CALIBRATION_MODE eCalibMode)
- {
- //mLog::FINFO("--TiRayDR Func-- UpdateCalibMode");
- //mLog::FINFO("CalibMode:{$}", (int)eCalibMode);
- m_nCalibrationMode = eCalibMode;
- //mLog::FINFO("UpdateCalibMode Over");
- return true;
- }
- void Detector_TiRayDR::SetNotifyStatusTimePeriod(int nTime)
- {
- //mLog::FINFO("--Func TiRay-- SetNotifyStatusTimePeriod Start");
- //mLog::FINFO("NotifyStatusTimePeriod:{$}", nTime);
- m_nNotifyStatusTimePeriod = nTime;
- //mLog::FINFO("SetNotifyStatusTimePeriod Over");
- }
- void Detector_TiRayDR::SetReconnectTimePeriod(int nTime)
- {
- //mLog::FINFO("--Func TiRay-- SetReconnectTimePeriod Start");
- //mLog::FINFO("ReconnectTimePeriod:{$}", nTime);
- m_nReconnectTimePeriod = nTime;
- //mLog::FINFO("SetReconnectTimePeriod Over");
- }
- int Detector_TiRayDR::TiRay_GetSdkVersion()
- {
- return GetSdkVersion_Ptr();
- }
- TiRayError Detector_TiRayDR::TiRay_Execute(int detectorId, int commandId, TiRayVariant argv[], int argc)
- {
- return Execute_Ptr(detectorId, commandId, argv, argc);
- }
- TiRayError Detector_TiRayDR::TiRay_ApplyPreset(int detectorId, TiRayVariant argv[], int argc, ResultCallback fn)
- {
- return ApplyPreset_Ptr(detectorId, argv, argc, fn);
- }
- TiRayError Detector_TiRayDR::TiRay_GenerateTemplate(TemplateType type, TiRayVariant images[], int count, void* templateBuffer, int bufferSize)
- {
- return GenerateTemplate_Ptr(type, images, count, templateBuffer, bufferSize);
- }
- TiRayError Detector_TiRayDR::TiRay_Scan(ResultCallback fn, const char* interfaceIp, int scanDuration)
- {
- return Scan_Ptr(fn, interfaceIp, scanDuration);
- }
- TiRayError Detector_TiRayDR::TiRay_SetIp(const char* detectorSN, const char* upperIp, const char* lowerIp, const char* interfaceIp)
- {
- return SetIp_Ptr(detectorSN, upperIp, lowerIp, interfaceIp);
- }
- TiRayError Detector_TiRayDR::TiRay_Startup(TiRayModel model, EventCallback fn, const StartupOption* option)
- {
- return Startup_Ptr(model, fn, option);
- }
- void Detector_TiRayDR::TiRay_Stop()
- {
- Stop_Ptr();
- }
- /***
- ** 说明:重连探测器线程
- ***/
- void* Detector_TiRayDR::onReconnectThread(PVOID pvoid)
- {
- //mLog::FINFO("Reconnect detector thread start");
- Detector_TiRayDR* pThis = static_cast<Detector_TiRayDR*>(pvoid);
- while (!pThis->m_bConnected)
- {
- pThis->OpenDetector();
- usleep(pThis->m_nReconnectTimePeriod*1000);
- }
- pThis->m_bReconnectThreadRunning = false;
- //mLog::FINFO("Leave reconnect detector thread");
- return 0;
- }
- TiRayError Detector_TiRayDR::upload(upload_type type, char* data, int size)
- {
- TiRayVariant param[1]{};
- param[0].Type = TiRayVariant::TiRayBuffer;
- param[0].DataValue = data;
- param[0].DataLen = size;
- if (type == offset) {
- return Execute_Ptr(m_nDetectorID, Cmd_UploadOffsetTemplate, param, 1);
- }
- if (type == gain) {
- return Execute_Ptr(m_nDetectorID, Cmd_UploadGainTemplate, param, 1);
- }
- if (type == firmware) {
- return Execute_Ptr(m_nDetectorID, Cmd_UpdateFirmware, param, 1);
- }
- return Err_InvalidParam;
- }
|