#include "stdafx.h" #include "OTSOxfordImpl.h" #include "ControllerHelper.h" #include "COTSUtilityDllFunExport.h" #include "OxfordImplConst.h" #ifdef _DEBUG #define new DEBUG_NEW #endif #include namespace OTSController { OxfordImpl::OxfordImpl(void) : m_bInit(false) { if (ManagedGlobals::oxfordController == nullptr) { ManagedGlobals::oxfordController = gcnew OxfordControllerWrapper(); } } OxfordImpl::~OxfordImpl(void) { CloseClient(); } void OxfordImpl::CloseClient(void) { ManagedGlobals::oxfordController->CloseClient(); } bool OxfordImpl::Connect() { if (!m_bInit) { OxfordWrapperErrorCode r; if (!ManagedGlobals::oxfordController->Init()) { r=OxfordWrapperErrorCode::CONTROLLER_INIT_FAILED; return false; } if (!ManagedGlobals::oxfordController->IsConnected()) { r=OxfordWrapperErrorCode::CONTROLLER_NOT_CONNECTED; return false; } r=OxfordWrapperErrorCode::SUCCEED; m_bInit = true; } return m_bInit; } bool OxfordImpl::DisConnect() { if (m_bInit) { CloseClient(); m_bInit = false; } return true; } bool OxfordImpl::IsConnected() { if (m_bInit) { if (!ManagedGlobals::oxfordController->IsConnected()) { return false; } return true; } return false; } bool OxfordImpl::GetPositionXY(double& a_dPosX, double& a_dPosY) { if (m_bInit) { if (!ManagedGlobals::oxfordController->GetPositionXY(a_dPosX, a_dPosY)) { LogErrorTrace(__FILE__, __LINE__, _T("GetPositionXY command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetPositionXY(double a_dPosX, double a_dPosY) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetPositionXY(a_dPosX, a_dPosY)) { LogErrorTrace(__FILE__, __LINE__, _T("SetPositionXY command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetWorkingDistance(double& a_dWorkingDistance) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->GetWorkingDistance(a_dWorkingDistance)) { LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingDistance command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetWorkingDistance(double a_dWorkingDistance) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetWorkingDistance(a_dWorkingDistance)) { LogErrorTrace(__FILE__, __LINE__, _T("SetWorkingDistance command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetMagnification(double& a_dMagnification) { if (m_bInit) { if (!ManagedGlobals::oxfordController->GetMagnification(a_dMagnification)) { LogErrorTrace(__FILE__, __LINE__, _T("GetMagnification command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetMagnification(double a_dMagnification) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetMagnification(a_dMagnification)) { LogErrorTrace(__FILE__, __LINE__, _T("SetMagnification command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetHighVoltage(double& a_dHighVoltage) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->GetHighVoltage(a_dHighVoltage)) { LogErrorTrace(__FILE__, __LINE__, _T("GetHighVoltage command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetHighVoltage(double a_dHighVoltage) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetHighVoltage(a_dHighVoltage)) { LogErrorTrace(__FILE__, __LINE__, _T("SetHighVoltage command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetBeamOn(bool& a_bBeamOn) { if (m_bInit) { if (!ManagedGlobals::oxfordController->GetBeamOn(a_bBeamOn)) { LogErrorTrace(__FILE__, __LINE__, _T("GetBeamOn command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetBeamOn(bool a_bBeamOn) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->SetBeamOn(a_bBeamOn)) { LogErrorTrace(__FILE__, __LINE__, _T("SetBeamOn command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetBeamBlank(bool& a_bBeamBlank) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->GetBeamBlank(a_bBeamBlank)) { LogErrorTrace(__FILE__, __LINE__, _T("GetBeamBlank command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetBeamBlank(bool a_bBeamBlank) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetBeamBlank(a_bBeamBlank)) { LogErrorTrace(__FILE__, __LINE__, _T("SetBeamBlank command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetExternal(bool& a_bExternal) { if (m_bInit) { if (!ManagedGlobals::oxfordController->GetExternalScan(a_bExternal)) { LogErrorTrace(__FILE__, __LINE__, _T("GetExternal command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetExternal(bool a_bExternal) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->SetExternalScan(a_bExternal)) { LogErrorTrace(__FILE__, __LINE__, _T("SetExternal command failed.")); return false; } CString s = __T(""); if (a_bExternal) { s = __T("On"); } else { s = __T("Off"); } LogWarn(__FILE__, __LINE__, _T("SetExternal: ") +s); return true; } return false; } bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize) { if (m_bInit) { if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nAcTime, a_pnCounts, a_nBufferSize)) { LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed.")); return false; } return true; } return false; } bool OxfordImpl::CollectXrayDataAtPos(const double a_dPosX, const double a_dPosY, const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize) { if (m_bInit) { if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize)) { LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayDataAtPos command failed.")); return false; } return true; } return false; } bool OxfordImpl::StopCollectXrayData() { if (m_bInit) { if (!ManagedGlobals::oxfordController->StopXrayCollecting()) { LogErrorTrace(__FILE__, __LINE__, _T("stopxraycollecting command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->SetBeamPosition(a_dPosX, a_dPosY)) { LogErrorTrace(__FILE__, __LINE__, _T("SetBeamPosition command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetScanSpeed(a_nMilliseconds)) { LogErrorTrace(__FILE__, __LINE__, _T("SetScanSpeed command failed.")); return false; } return true; } return false; } bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->GetImageSize(a_nWidth, a_nHeight)) { LogErrorTrace(__FILE__, __LINE__, _T("GetImageSize command failed.")); return false; } return true; } return false; } bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight) { if (m_bInit) { if (!ManagedGlobals::oxfordController->SetImageSize(a_nWidth, a_nHeight)) { LogErrorTrace(__FILE__, __LINE__, _T("SetImageSize command failed.")); return false; } return true; } return false; } bool OxfordImpl::CollectImage(BYTE* a_pImageBits) { if (m_bInit) { if (!ManagedGlobals::oxfordController->CollectImage(a_pImageBits)) { LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed.")); return false; } return true; } return false; } bool OxfordImpl::QuantifySpectrum(unsigned char* cResult) { if ( m_bInit) { if (!ManagedGlobals::oxfordController->QuantifySpectrum(cResult)) { LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed.")); return false; } return true; } return false; } BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS) { // do nothing if points list is empty if (a_listXrayPois.empty()) { // points list is empty LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty.")); return TRUE; } // create array of BrukerSegment long nCollectCount = (long)a_listXrayPois.size(); OxfordXrayData* segmentArray(new OxfordXrayData[nCollectCount]); for (int i = 0; i < nCollectCount; ++i) { CPoint poi = a_listXrayPois[i]->GetPosition(); segmentArray[i].m_nPosX = poi.x; segmentArray[i].m_nPosY = poi.y; } if (!ManagedGlobals::oxfordController->CollectXrayPoints(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS)) { LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayList command failed.")); return (int)OxfordWrapperErrorCode::COMMAND_FAILED; } // get the specs for a_vXPoints for (int i = 0; i < nCollectCount; ++i) { // set spectrum data for the x-ray point a_listXrayPois[i]->SetXrayData((DWORD*)(segmentArray[i].m_pXrayData)); if (m_bDoQuantification) { // quantify the spectrum CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)segmentArray[i].m_strElementResult)); a_listXrayPois[i]->SetElementQuantifyData(vElement); } } for (int i = 0; i < nCollectCount; ++i) { // check spectrum DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { // captured an empty spectrum CPoint poi = a_listXrayPois[i]->GetPosition(); // try to redo x-ray collection at the position static DWORD nChannelData[GENERALXRAYCHANNELS]; memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS); if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS)) { // error CString s; s.Format(_T("Call CollectXRayPoint failed: index = %d(x:%d, y:%d))"), i, poi.x, poi.y); LogErrorTrace(__FILE__,__LINE__,s); return false; } // set spectrum with new spectrum a_listXrayPois[i]->SetXrayData(nChannelData); nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { CString s1; s1.Format(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."), nTatolXrayCount, i, poi.x, poi.y); LogTrace(__FILE__, __LINE__, s1); } else { CString s2; s2.Format(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"), nTatolXrayCount, i, poi.x, poi.y); LogTrace(__FILE__, __LINE__, s2); } } } delete segmentArray; // ok return TRUE return TRUE; // error, return false return FALSE; } BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector& a_vFeatures, DWORD a_nXRayAQTime) { // do nothing if points list is empty if (a_listXrayPois.empty()) { // points list is empty LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty.")); return TRUE; } if (a_listXrayPois.size() != a_vFeatures.size()) { return FALSE; } // create array of BrukerSegment long nCollectCount = (long)a_listXrayPois.size(); //convert the brukerfeature to oxfordfeature OxfordXrayData* features(new OxfordXrayData[nCollectCount]); int nTotalNum = 0; for (size_t i = 0; i < a_listXrayPois.size(); i++) { long nSegmentCount = (long)a_vFeatures[i].SegmentCount; features[i].m_nChordNum = nSegmentCount; long nPixelCount = 0; if (nSegmentCount > 0) { BrukerSegment* segs = a_vFeatures[i].pSegment; for (int j = 0; j < nSegmentCount; j++) { features[i].m_ChordList[j].m_nX = segs[j].XStart; features[i].m_ChordList[j].m_nY = segs[j].Y; features[i].m_ChordList[j].m_nLength = segs[j].XCount; nPixelCount += segs[j].XCount; } features[i].m_nPixelNum = nPixelCount; } else { // will generate according to the x-ray position // this shouldn't happen } nTotalNum += nPixelCount; } int a_nACTimeMS = a_nXRayAQTime / nTotalNum; if (!ManagedGlobals::oxfordController->CollectXrayArea(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS)) { LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayArea failed")); return false; } // get the specs for a_vXPoints for (int i = 0; i < nCollectCount; ++i) { // set spectrum data for the x-ray point a_listXrayPois[i]->SetXrayData((DWORD*)(features[i].m_pXrayData)); // set spectrum data for the x-ray point if (m_bDoQuantification) { // quantify the spectrum CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)features[i].m_strElementResult)); a_listXrayPois[i]->SetElementQuantifyData(vElement); } } for (int i = 0; i < nCollectCount; ++i) { // check spectrum DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { // captured an empty spectrum CPoint poi = a_listXrayPois[i]->GetPosition(); // try to redo x-ray collection at the position static DWORD nChannelData[GENERALXRAYCHANNELS]; memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS); if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS)) { LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed.")); return false; } // set spectrum with new spectrum a_listXrayPois[i]->SetXrayData(nChannelData); nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."), nTatolXrayCount, i, poi.x, poi.y);*/ } else { /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"), nTatolXrayCount, i, poi.x, poi.y);*/ } } } delete features; return TRUE; } }