#include "DataFileProcessor.h"

DataFileProcessor::DataFileProcessor(QObject* parent /*= nullptr*/)
{
#ifdef _DEBUG
	m_qstrFilePath = "E:/WorkSpace/TowerOptoSifAndSpectral/Data";
#else
	m_qstrFilePath = "/home/data/Data";
	m_qstrWavelengthInfoFileName = "/home/data/Data/WavelengthInfo.txt";
#endif // DEBUG
	
}

DataFileProcessor::~DataFileProcessor()
{
}


int DataFileProcessor::WriteWavelengthInfo(float *pfWaveLength, int iLength)
{
	QFileInfo qfFileInfo(m_qstrWavelengthInfoFileName);
	if (qfFileInfo.isFile())
	{
		return 0;
	}
	else
	{
		bool bRes = true;
		QFile qfData(m_qstrWavelengthInfoFileName);
		bRes = qfData.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
		if (!bRes)
		{
			qDebug() << "WriteEnvironmentInfo open Failed.";
			return 1;
		}

		QString qstrTemp = "wavelength = ";
		qfData.write(qstrTemp.toLatin1());
		qfData.write("{ ");
		for (int i=0;i< iLength;i++)
		{
			qstrTemp =QString::number(pfWaveLength[i], 'f', 2);
			if (i== (iLength-1))
			{
				qfData.write(qstrTemp.toLatin1());
			}
			else
			{
				qfData.write(qstrTemp.toLatin1());
				qfData.write(", ");
			}
		}
		qfData.write("}");
		qfData.close();

	}
	return 0;
}

int DataFileProcessor::WriteData(DataFrame dfDataFrame)
{
	QFile qfData(m_qstrFullFileName);
	bool bRes = qfData.open(QFile::WriteOnly| QIODevice::Append);
	if (!bRes)
	{
		qDebug() << "WriteData open Failed.";
		return 0;
	}
	qfData.write((char*)&dfDataFrame, sizeof(DataFrame));
	return 0;
}

void DataFileProcessor::GenerateFilePath()
{
    m_qdtTime = QDateTime::currentDateTime();
	QString qstrAddYMD = m_qdtTime.toString("/yyyy_MM_dd");
	QString qstrAddHMS = m_qdtTime.toString("hh_mm_ss");


	m_qstrFullFileName = m_qstrFilePath + qstrAddYMD;
	QString qstrTemp= m_qstrFullFileName;
	
	m_qstrFullFileName= m_qstrFullFileName+"/"+ qstrAddHMS+".dat";

	QDir qdirPathTemp(m_qstrFilePath);
	if (!qdirPathTemp.exists())
	{
		bool bRes = qdirPathTemp.mkdir(m_qstrFilePath);
		if (!bRes)
		{
			qDebug() << "DataFileProcessor mkdir Failed.";
		}
	}

	QDir qdirPathTempA(qstrTemp);
	if (!qdirPathTempA.exists())
	{
		bool bRes = qdirPathTempA.mkdir(qstrTemp);
		if (!bRes)
		{
			qDebug() << "DataFileProcessor mkdir Failed.";
		}
	}
}


