#include "MainDataUploader.h"

CMainDataUploader::CMainDataUploader(QObject* parent /*= nullptr*/)
{
	m_pRadConverter = new RadConverter;
	m_pTransfer = new ZZ_HttpTransfer;
	//connect(this, &CMainDataUploader::SignalInit_Self, m_pTransfer, &ZZ_HttpTransfer::SlotInit_Self);
}

CMainDataUploader::~CMainDataUploader()
{
	if (m_pRadConverter)
	{
		delete m_pRadConverter;
	}
	if (m_pTransfer)
	{
		delete m_pTransfer;
	}
}

void CMainDataUploader::SetErr(ErrInfo struErrInfo)
{
	m_pTransfer->SetErrInfo(struErrInfo);
	//m_struErrInfo = struErrInfo;
}

void CMainDataUploader::SetData(vector<vector<DataFrame>> vecData)
{
	QDateTime qdtTime = QDateTime::currentDateTime();
	QString qstrAddYMD = qdtTime.toString("yyyy_MM_dd");
	QString qstrAddHMS = qdtTime.toString("_hh_mm_ss");

	m_qstrGrabDate.clear();
	m_qstrGrabDate = qstrAddYMD + qstrAddHMS;

	m_vecData = vecData;
}

void CMainDataUploader::SetRTGP(RunTimeGrabberParams struGrabberRTParams)
{
	m_struGrabberRTParams = struGrabberRTParams;
}

void CMainDataUploader::SetContext(EContext struEC, MEContext struMEC)
{
	m_struEC = struEC;
	m_struMEC = struMEC;
}

void CMainDataUploader::Calibration()
{
	int iScanPoints = m_struGrabberRTParams.apsParams.iTotalPosition - 1;
	int iDevices = m_struGrabberRTParams.fscParams.ucDeviceNumber;

	m_vecCalcedData.clear();
	m_vecCalcedData.resize(iDevices);
	for (int i=0;i< iDevices;i++)
	{
		m_vecCalcedData[i].resize(iScanPoints);
	}

	for (int j = 0; j < iDevices; j++)
	{
		for (int i = 0; i < iScanPoints; i++)
		{
			m_vecCalcedData[j][i].usExposureTimeInMS = m_vecData[j][i].usExposureTimeInMS;
			m_vecCalcedData[j][i].iPixels = m_vecCalData[j][i].iPixels;
			m_vecCalcedData[j][i].fTemperature = m_vecData[j][i].fTemperature;
			m_vecCalcedData[j][i].qstrGrabDate = m_qstrGrabDate;
			for (int k=0;k< m_struGrabberRTParams.fscParams.usPixels[j];k++)
			{
				m_vecCalcedData[j][i].fData[k] = (float)(m_vecData[j][i].lData[k]* m_vecCalData[j][i].dCal_Gain[k]* m_vecCalData[j][i].uiExposureTimeInMS/m_vecData[j][i].usExposureTimeInMS);
			}
		}
	}
}

int CMainDataUploader::UploadInfo()
{
	m_pTransfer->SendInfo();
	return 0;
}

int CMainDataUploader::UploadErr()
{
	m_pTransfer->SendErr();
	return 0;
}

int CMainDataUploader::UploadData()
{
	m_pTransfer->SetCalData(m_vecCalcedData);
	m_pTransfer->SendData();
	return 0;
}

int CMainDataUploader::Initialize()
{
	m_pRadConverter->LoadCalibrationFrames(m_struGrabberRTParams,m_vecCalData);
	m_pTransfer->SetContext(m_struEC, m_struMEC);
	m_pTransfer->SetDeviceInfo(m_struGrabberRTParams);
	//m_pTransfer->Init_Self();
	return 0;
}

// int CMainDataUploader::Init_Self()
// {
// 	emit SignalInit_Self();
// 	return 0;
// }

// int CMainDataUploader::SlotPushOneDataFrame(vector<vector<DataFrame>> vecData)
// {
// 	return 0;
// }

int CMainDataUploader::SlotPushOneDataFrame()
{
	Calibration();
	UploadData();
	m_vecData.clear();
	return 0;
}

int CMainDataUploader::SlotUpdateErrorInfo()
{
	return 0;
}

// int CMainDataUploader::SlotInit_Self()
// {
// 
// }
