#include "DataFileProcessor.h"

DataFileProcessor::DataFileProcessor()
{
#ifdef _DEBUG
	m_qstrFilePath = "E:/WorkSpace/TowerOptoSifAndSpectral/Data";
#else
	m_qstrFilePath = /home/data/Data;
#endif // DEBUG
	
}

DataFileProcessor::~DataFileProcessor()
{
}

void DataFileProcessor::SetEnvironmentContex(EContext struEC)
{
	m_struEC = struEC;
}

void DataFileProcessor::SetManmadeEnviromentalContext(MEContext struMEC)
{
	m_struMEC = struMEC;
}

bool DataFileProcessor::WriteDataFile()
{
	GenerateFilePath();
	WriteInfo();
	bool res = true;
	return res;
}

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

	m_struEC.qstrUTCDateTime = m_qdtTime.toUTC().toString("yyyy_MM_dd hh:mm:ss");

	m_qstrFullFileName = m_qstrFilePath + qstrAddYMD;
	m_qstrFilePath= m_qstrFullFileName;
	if (m_struEC.qstrLocation=="")
	{
		m_struEC.qstrLocation = "Unknown";
	}
	m_qstrFullFileName= m_qstrFullFileName+"/"+m_struEC.qstrLocation + qstrAddHMS+".txt";

	QDir qdirPath(m_qstrFilePath);
	if (!qdirPath.exists())
	{
		qdirPath.mkdir(m_qstrFilePath);
	}
}

bool DataFileProcessor::WriteInfo()
{
	bool bRes = true;
	QFile qfData(m_qstrFullFileName);
	bRes = qfData.open(QFile::WriteOnly|QFile::Text|QFile::Truncate);
	if (!bRes)
	{
		return bRes;
	}
	//EC
	qfData.write("EnvironmentalContext,");
	qfData.write("CaseHumidity,");
	qfData.write(m_struEC.qstrCaseHumidity.toLatin1());
	qfData.write(",");
	qfData.write("CaseTemperature,");
	qfData.write(m_struEC.qstrCaseTemperature.toLatin1());
	qfData.write(",");
	qfData.write("GPS_Altitude,");
	qfData.write(m_struEC.qstrGPS_Altitude.toLatin1());
	qfData.write(",");
	qfData.write("GPS_Latitude,");
	qfData.write(m_struEC.qstrGPS_Latitude.toLatin1());
	qfData.write(",");
	qfData.write("GPS_Longtitude,");
	qfData.write(m_struEC.qstrGPS_Longtitude.toLatin1());
	qfData.write(",");
	qfData.write("GPS_North,");
	qfData.write(m_struEC.qstrGPS_North.toLatin1());
	qfData.write(",");
	qfData.write("Location,");
	qfData.write(m_struEC.qstrLocation.toLatin1());
	qfData.write(",");
	qfData.write("UTCDateTime,");
	qfData.write(m_struEC.qstrUTCDateTime.toLatin1());
	qfData.write(",");

	//qfData.write("\n");

	//MEC
	//qfData.write("ManmadeEnvironmentalContext\n");
	qfData.write("DownlaodAddress,");
	qfData.write(m_struMEC.qstrDownlaodAddress.toLatin1());
	qfData.write(",");
	qfData.write("DownloadUserID,");
	qfData.write(m_struMEC.qstrDownloadUserID.toLatin1());
	qfData.write(",");
	qfData.write("HTTPServer,");
	qfData.write(m_struMEC.qstrHTTPServer.toLatin1());
	qfData.write(",");
	qfData.write("InstallationTime,");
	qfData.write(m_struMEC.qstrInstallationTime.toLatin1());
	qfData.write(",");
	qfData.write("NameOfMaintenanceStaff,");
	qfData.write(m_struMEC.qstrNameOfMaintenanceStaff.toLatin1());
	qfData.write(",");
	qfData.write("PhoneNumberOfMaintenanceStaff,");
	qfData.write(m_struMEC.qstrPhoneNumberOfMaintenanceStaff.toLatin1());
	qfData.write(",");

	qfData.close();
	return bRes;
}

bool DataFileProcessor::WriteData()
{
	bool res = true;
	return res;
}
