#include "stdafx.h"
#include "ECGFile.h"
#include "DataSaveThread.h"
#include "vECG.h"



CECGFile::CECGFile()
{
	int i = 0;
	m_pstECGFileHead = new struct ECG_FILE_HEAD;
	for (i = 0; i < 12; i++)
	{
		m_pstECGFileDataInfo[i] = new struct ECG_FILE_DATA_INFO;
		m_stInputECGData.pECGData[i] = new INT16[ECG_FILE_DATA_LENGTH];
	}
	m_stInputECGData.i32Length = ECG_FILE_DATA_LENGTH;
	m_pFileBuf = new UINT8[ECG_FILE_LENGTH_LIMIT];
}


CECGFile::~CECGFile()
{
	int i = 0;
	delete m_pstECGFileHead;
	for (i = 0; i < 12; i++)
	{
		delete m_pstECGFileDataInfo[i];
		delete m_stInputECGData.pECGData[i];
	}
	delete m_pFileBuf;

}


int CECGFile::make_file(CPatient *pat, struct GET_ECG_DATA *st)
{
	CString strTime;

	
	wcscpy_s(m_pstECGFileHead->cID, 32, pat->cID);
	wcscpy_s(m_pstECGFileHead->cName, 32, pat->cName);
	wcscpy_s(m_pstECGFileHead->cAge, 8, pat->cAge);
	wcscpy_s(m_pstECGFileHead->cSex, 8, pat->cSex);
	wcscpy_s(m_pstECGFileHead->cWeight, 8, pat->cWeight);
	wcscpy_s(m_pstECGFileHead->cHeight, 8, pat->cHeight);
	wcscpy_s(m_pstECGFileHead->cPressSys, 8, pat->cPressSys);
	wcscpy_s(m_pstECGFileHead->cPressDia, 8, pat->cPressDia);
	//wcscpy_s(m_pstECGFileHead->cTime, strTime);
	m_pstECGFileHead->i32HR = theApp.m_i32HR;
	m_pstECGFileHead->i32ST1 = theApp.m_i32ST1;
	m_pstECGFileHead->i32ST2 = theApp.m_i32ST2;

	m_pstECGFileHead->i32ECGType = 3;

	int i = 0;
	int t = (sizeof(struct ECG_FILE_HEAD) + 12*sizeof(struct ECG_FILE_DATA_INFO)+31)/32,j;
	t *= 32;
	j = t;
	for (i = 0; i < 12; i++)
	{
		m_pstECGFileDataInfo[i]->i32ECGLead=i;
		m_pstECGFileDataInfo[i]->i32ECGSampleRate=250;
		m_pstECGFileDataInfo[i]->i32ECGSampleTime=10;
		m_pstECGFileDataInfo[i]->i32DataLength = ECG_FILE_DATA_LENGTH;
		m_pstECGFileDataInfo[i]->i32Offset = j;
		j = j + ECG_FILE_DATA_LENGTH*2;
		memcpy(m_pFileBuf + sizeof(struct ECG_FILE_HEAD) + i*sizeof(struct ECG_FILE_DATA_INFO), m_pstECGFileDataInfo[i], sizeof(struct ECG_FILE_DATA_INFO));
	}
	j = t;
	
	for (t = 0; t < 12;t++)
	{
		memcpy(m_pFileBuf + j, st->pECGData[t], ECG_FILE_DATA_LENGTH * 2);
		j += ECG_FILE_DATA_LENGTH * 2;
	}
	

	m_pstECGFileHead->i32FileLength = j;

	
	
	return 0;
}
int CECGFile::output_file()
{
	CFile m_FileECG;
	CString strFileName, strTime;

	COleDateTime tm = COleDateTime::GetCurrentTime();
	strTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());

	wcscpy_s(m_pstECGFileHead->cTime, 32, strTime);
	memcpy(m_pFileBuf, m_pstECGFileHead, sizeof(struct ECG_FILE_HEAD));
	strFileName.Format(_T("%s\\%s_%04d%02d%02d%02d%02d%02d.ecg"), theApp.m_strDir, m_pstECGFileHead->cName, tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	TRY
	{
		if (m_FileECG.Open(strFileName, CFile::modeCreate | CFile::modeWrite, 0))
		{
			m_FileECG.Write(m_pFileBuf, m_pstECGFileHead->i32FileLength);
			m_FileECG.Flush();
		}
		m_FileECG.Close();
	}
	CATCH(CFileException, e)
	{
		m_FileECG.Abort();
	}
	END_CATCH
	return 0;
}


int  CECGFile::rec_make_file(struct GET_ECG_DATA *st)
{
	
	wcscpy_s(m_pstECGFileHead->cID, 32, st->cID);
	wcscpy_s(m_pstECGFileHead->cName, 32, st->cName);
	wcscpy_s(m_pstECGFileHead->cAge, 8, st->cAge);
	wcscpy_s(m_pstECGFileHead->cSex, 8, st->cSex);
	wcscpy_s(m_pstECGFileHead->cWeight, 8, st->cWeight);
	wcscpy_s(m_pstECGFileHead->cHeight, 8, st->cHeight);
	wcscpy_s(m_pstECGFileHead->cPressSys, 8, st->cPressSys);
	wcscpy_s(m_pstECGFileHead->cPressDia, 8, st->cPressDia);
	wcscpy_s(m_pstECGFileHead->cTime, 32, st->cTime);
	m_pstECGFileHead->i32HR = st->i32HR;
	m_pstECGFileHead->i32ST1 = st->i32ST1;
	m_pstECGFileHead->i32ST2 = st->i32ST2;

	m_pstECGFileHead->i32ECGType = 3;

	int i = 0;
	int t = (sizeof(struct ECG_FILE_HEAD) + 12 * sizeof(struct ECG_FILE_DATA_INFO) + 31) / 32, j;
	t *= 32;
	j = t;
	for (i = 0; i < 12; i++)
	{
		m_pstECGFileDataInfo[i]->i32ECGLead = i;
		m_pstECGFileDataInfo[i]->i32ECGSampleRate = 250;
		m_pstECGFileDataInfo[i]->i32ECGSampleTime = 10;
		m_pstECGFileDataInfo[i]->i32DataLength = ECG_FILE_DATA_LENGTH;
		m_pstECGFileDataInfo[i]->i32Offset = j;
		j = j + ECG_FILE_DATA_LENGTH * 2;
		memcpy(m_pFileBuf + sizeof(struct ECG_FILE_HEAD) + i*sizeof(struct ECG_FILE_DATA_INFO), m_pstECGFileDataInfo[i], sizeof(struct ECG_FILE_DATA_INFO));
	}
	j = t;

	for (t = 0; t < 12; t++)
	{
		memcpy(m_pFileBuf + j, st->pECGData[t], ECG_FILE_DATA_LENGTH * 2);
		j += ECG_FILE_DATA_LENGTH * 2;
	}


	m_pstECGFileHead->i32FileLength = j;

	memcpy(m_pFileBuf, m_pstECGFileHead, sizeof(struct ECG_FILE_HEAD));

	return 0;

}


int  CECGFile::rec_output_file(int flag)
{
	CFile m_FileECG;
	CString a_strFileName, a_strDirName,a_strDir, strTime;
	strTime = m_pstECGFileHead->cTime;
	a_strDirName =  strTime.Mid(0, 4) + strTime.Mid(5, 2) + strTime.Mid(8, 2);
	a_strDir = _T("D:\\") + a_strDirName;
	a_strFileName = a_strDir + _T("\\") + m_pstECGFileHead->cName + _T("_") + a_strDirName + _T(".ecg");
	//文件夹不存在则创建
	if (!PathFileExists(a_strDir))
	{
		CreateDirectory(a_strDir, NULL);
	}

	TRY
	{
		CFileFind finder;
		BOOL bWorking = finder.FindFile(a_strFileName);
		if (bWorking && (flag==0))
		{
			return 1;
		}
		if (m_FileECG.Open(a_strFileName, CFile::modeCreate | CFile::modeWrite, 0))
		{
			m_FileECG.Write(m_pFileBuf, m_pstECGFileHead->i32FileLength);
			m_FileECG.Flush();
		}
		m_FileECG.Close();
	}
	CATCH(CFileException, e)
	{
		m_FileECG.Abort();
	}
	END_CATCH

		return 0;
}




int CECGFile::input_file(CString file_name)
{
	struct ECG_FILE_HEAD *a_pECG_FILE_HEAD;

	int t = (sizeof(struct ECG_FILE_HEAD) + 12 * sizeof(struct ECG_FILE_DATA_INFO) + 31) / 32*32,  i;
	TRY
	{
		if (m_OpenFile.Open(file_name, CFile::modeRead, 0))
		{
		
			if (m_OpenFile.GetLength() >= (t + ECG_FILE_DATA_LENGTH * 2 * 12))
			{
				if (m_OpenFile.Read(m_pFileBuf, t + ECG_FILE_DATA_LENGTH * 2 * 12) == t + ECG_FILE_DATA_LENGTH * 2 * 12)
				{
					a_pECG_FILE_HEAD = (struct ECG_FILE_HEAD *)m_pFileBuf;


					a_pECG_FILE_HEAD->cID[31] = 0;
					a_pECG_FILE_HEAD->cName[31] = 0;
					a_pECG_FILE_HEAD->cAge[7] = 0;
					a_pECG_FILE_HEAD->cSex[7] = 0;
					a_pECG_FILE_HEAD->cWeight[7] = 0;
					a_pECG_FILE_HEAD->cHeight[7] = 0;
					a_pECG_FILE_HEAD->cTime[31] = 0;
					
					wcscpy_s(m_stInputECGData.cID, 32, a_pECG_FILE_HEAD->cID);
					wcscpy_s(m_stInputECGData.cName, 32, a_pECG_FILE_HEAD->cName);
					wcscpy_s(m_stInputECGData.cAge, 8, a_pECG_FILE_HEAD->cAge);
					wcscpy_s(m_stInputECGData.cSex, 8, a_pECG_FILE_HEAD->cSex);
					wcscpy_s(m_stInputECGData.cWeight, 8, a_pECG_FILE_HEAD->cWeight);
					wcscpy_s(m_stInputECGData.cHeight, 8, a_pECG_FILE_HEAD->cHeight);
					//wcscpy_s(m_stInputECGData.cPressSys, a_pECG_FILE_HEAD->cPressSys);
					//wcscpy_s(m_stInputECGData.cPressDia, a_pECG_FILE_HEAD->cPressDia);
					wcscpy_s(m_stInputECGData.cTime, 32, a_pECG_FILE_HEAD->cTime);
					m_stInputECGData.i32HR = a_pECG_FILE_HEAD->i32HR;
					m_stInputECGData.i32ST1 = a_pECG_FILE_HEAD->i32ST1;
					m_stInputECGData.i32ST2 = a_pECG_FILE_HEAD->i32ST2;

					for (i = 0; i < 12; i++)
					{
						memcpy(m_stInputECGData.pECGData[i], m_pFileBuf + t, ECG_FILE_DATA_LENGTH * 2);
						t += ECG_FILE_DATA_LENGTH * 2;
					}
				}
				

			}
			m_OpenFile.Close();
		}
	}
	CATCH(CFileException, e)
	{
		m_OpenFile.Abort();
	}
	END_CATCH
	return 0;
}