#include "stdafx.h"
#include "ECGDateSaveFile.h"

char n_cMagic[32] ="RZZH3ZFTEXJ154CFILLCX2QU12KP09U";
CECGDateSaveFile::CECGDateSaveFile()
{
	
	int i = 0;
	m_i32SaveFlag=1;
	m_i32MakeFlag=1;
	m_i32ECGDataSave1FullFlag = 0;
	m_i32ECGDataSave2FullFlag = 0;
	m_i32FileErr = 0;

	for (i = 0; i < 12; i++)
	{
		m_stECGDataSave1.pECGData[i] = new int[ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE];
		m_stECGDataSave2.pECGData[i] = new int[ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE];
	}

	i = (sizeof(struct ECG_SAVE_HEAD) + 63) / 64 * 64;
	m_u32SecLength = (i + SAVE_DATA_SEC_LENGTH);
	m_pBuff = new UINT8[m_u32SecLength];

	

	if (check_file_head())
	{
		//错误

	}
	m_i32FileFlag = 0;
}


CECGDateSaveFile::~CECGDateSaveFile()
{
	int i = 0;
	for (i = 0; i < 12; i++)
	{
		delete m_stECGDataSave1.pECGData[i];
		delete m_stECGDataSave2.pECGData[i];
	}
	delete m_pBuff;
	m_FileECGDataSave.Flush();
	m_FileECGDataSave.Close();
}


int CECGDateSaveFile::make_save_data(CPatient &pat, struct GET_ECG_DATA *st)
{
	/*int flag = 0,i=0;
	g_CSSave.Lock();
	if ((m_i32MakeFlag == 1) && (m_i32ECGDataSave1FullFlag == 0))
	{
		m_i32MakeFlag = 2;
		flag = 1;
		
	}
	else if ((m_i32MakeFlag == 2) && (m_i32ECGDataSave2FullFlag == 0))
	{
		m_i32MakeFlag = 1;
		flag = 2;
	}
	g_CSSave.Unlock();
	
	if (flag == 1)
	{
		COleDateTime tm = COleDateTime::GetCurrentTime();
		CString cs1;

		cs1.Format(_T("%04d-%02d-%02d %d:%d:%d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
		wcscpy_s(m_stHeadSave1.cTime, 24, cs1.GetBuffer(cs1.GetLength()));

		wcscpy_s(m_stHeadSave1.cID, 32, pat.cID);
		wcscpy_s(m_stHeadSave1.cName, 32, pat.cName);
		wcscpy_s(m_stHeadSave1.cSex, 8, pat.cSex);
		wcscpy_s(m_stHeadSave1.cAge, 8, pat.cAge);
		m_stHeadSave1.i32HR = st->i16HR;
		m_stHeadSave1.i32ST1 = st->i16ST1;
		m_stHeadSave1.i32ST2 = st->i16ST2;
		for (i = 0; i < 12;i++)
		{
			memcpy(m_stECGDataSave1.pECGData[i], st->pECGData[i], st->i32Length * 2);
		}
		m_stECGDataSave1.i32DataLength = st->i32Length;
	} 
	else if (flag == 2)
	{
		COleDateTime tm = COleDateTime::GetCurrentTime();
		CString cs1;

		cs1.Format(_T("%04d-%02d-%02d %d:%d:%d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
		wcscpy_s(m_stHeadSave2.cTime, 24, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(m_stHeadSave2.cID, 32, pat.cID);
		wcscpy_s(m_stHeadSave2.cName, 32, pat.cName);
		wcscpy_s(m_stHeadSave2.cSex, 8, pat.cSex);
		wcscpy_s(m_stHeadSave2.cAge, 8, pat.cAge);
		m_stHeadSave2.i32HR = st->i16HR;
		m_stHeadSave2.i32ST1 = st->i16ST1;
		m_stHeadSave2.i32ST2 = st->i16ST2;
		for (i = 0; i < 12; i++)
		{
			memcpy(m_stECGDataSave2.pECGData[i], st->pECGData[i], st->i32Length * 2);
		}
		m_stECGDataSave2.i32DataLength = st->i32Length;
	}

	g_CSSave.Lock();

	if (flag == 1)
	{
		m_i32ECGDataSave1FullFlag = 1;
	}
	else if (flag == 2)
	{
		m_i32ECGDataSave2FullFlag = 1;
	}
	g_CSSave.Unlock();*/

	return 0;
}


int CECGDateSaveFile::do_save_data()
{
	int flag = 0, i = 0,j=0;
	g_CSSave.Lock();
	if ((m_i32SaveFlag == 1) && (m_i32ECGDataSave1FullFlag == 1))
	{
		m_i32SaveFlag = 2;
		flag = 1;

	}
	else if ((m_i32SaveFlag == 2) && (m_i32ECGDataSave2FullFlag == 1))
	{
		m_i32SaveFlag = 1;
		flag = 2;
	}
	g_CSSave.Unlock();

	if (flag == 1)
	{
		memcpy(m_pBuff, &m_stHeadSave1, sizeof(m_stHeadSave1));
		j = (sizeof(struct ECG_SAVE_HEAD) + 63) / 64 * 64;
		for (i = 0; i < 12; i++)
		{
			memcpy(m_pBuff + j, m_stECGDataSave1.pECGData[i], ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE * 2);
			j += ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE * 2;
		}
	}
	else if (flag == 2)
	{
		memcpy(m_pBuff, &m_stHeadSave2, sizeof(m_stHeadSave2));
		j = (sizeof(struct ECG_SAVE_HEAD) + 63) / 64 * 64;
		for (i = 0; i < 12; i++)
		{
			memcpy(m_pBuff + j, m_stECGDataSave2.pECGData[i], ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE * 2);
			j += ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE * 2;
		}
	}
	TRY
	{
		if( ((flag == 1) || (flag == 2)) && (m_i32FileErr==0))
		{
			m_FileECGDataSave.Seek(SAVE_DATA_FILE_DATA_ADD + (m_u32ECGDataFileW%SAVE_DATA_LENGTH)*m_u32SecLength, CFile::begin);
			m_FileECGDataSave.Write(m_pBuff, m_u32SecLength);
			m_FileECGDataSave.Flush();
			m_u32ECGDataFileR = m_u32ECGDataFileW;
			m_u32ECGDataFileW++;
		
			if (m_u32ECGDataFileLength < SAVE_DATA_LENGTH)
			{
				m_u32ECGDataFileLength++;
			}

			if (m_u32ECGDataFileW % 3 == 0)
			{
				COleDateTime tm = COleDateTime::GetCurrentTime();
				CString cs1;

				cs1.Format(_T("%04d-%02d-%02d %d:%d:%d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond());
				if (m_i32FileFlag == 0)
				{
					stSaveDataFileHead1.u32FileW = m_u32ECGDataFileW;
					stSaveDataFileHead1.u32Length = m_u32ECGDataFileLength;
					wcscpy_s(stSaveDataFileHead1.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

					m_FileECGDataSave.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
					m_FileECGDataSave.Write((&stSaveDataFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));
			
					m_i32FileFlag = 1;
				}
				else
				{
					stSaveDataFileHead2.u32FileW = m_u32ECGDataFileW;
					stSaveDataFileHead2.u32Length = m_u32ECGDataFileLength;

					wcscpy_s(stSaveDataFileHead2.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

					m_FileECGDataSave.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
					m_FileECGDataSave.Write((&stSaveDataFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));
					m_i32FileFlag = 0;
				}
				m_FileECGDataSave.Flush();
			}
		}
	}
	CATCH(CFileException, e)
	{
		m_i32FileErr = 1;
		m_FileECGDataSave.Abort();
	}
	END_CATCH

	g_CSSave.Lock();

	if (flag == 1)
	{
		m_i32ECGDataSave1FullFlag = 0;
	}
	else if (flag == 2)
	{
		m_i32ECGDataSave2FullFlag = 0;
	}
	g_CSSave.Unlock();
	
	return 0;
}

int  CECGDateSaveFile::check_file_head()
{
	int i = 0,flag1=0,flag2=0;
	CString a_str = _T("D://ECG_data_save.dat");
	TRY
	{
		//如果成功打开，则返回非零值，否则为0
		if (m_FileECGDataSave.Open(a_str, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite, 0))
		{
			i = (sizeof(struct ECG_SAVE_HEAD) + 63) / 64 * 64;
			if (m_FileECGDataSave.GetLength()>2 * SAVE_DATA_FILE_HEAD_2_ADD)
			{
				m_FileECGDataSave.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
				if (m_FileECGDataSave.Read(m_pBuff, i)==i)
				{
					memcpy((void *)(&stSaveDataFileHead1), m_pBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}
				m_FileECGDataSave.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
				if (m_FileECGDataSave.Read(m_pBuff, i)==i)
				{
					memcpy((void *)(&stSaveDataFileHead2), m_pBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}
				

				for (i = 0; i < 32; i++)
				{
					if (stSaveDataFileHead1.cMagic[i] != n_cMagic[i])
						flag1 = 1;
					if (stSaveDataFileHead2.cMagic[i] != n_cMagic[i])
						flag2 = 1;
				}
				for (i = 0; i < 32; i++)
				{
					stSaveDataFileHead1.cMagic[i] = n_cMagic[i];
					stSaveDataFileHead2.cMagic[i] = n_cMagic[i];
				}
				if ((flag1 == 0) && (flag2 == 0))
				{
					if (stSaveDataFileHead2.u32FileW > stSaveDataFileHead1.u32FileW)
					{
						m_u32ECGDataFileW = stSaveDataFileHead2.u32FileW;
						m_u32ECGDataFileLength = stSaveDataFileHead2.u32Length;
					}
					else
					{
						m_u32ECGDataFileW = stSaveDataFileHead1.u32FileW;
						m_u32ECGDataFileLength = stSaveDataFileHead1.u32Length;
					}
					m_u32ECGDataFileR = m_u32ECGDataFileW;

					return 0;

				}
				else if ((flag1 == 0))
				{
					m_u32ECGDataFileW = stSaveDataFileHead1.u32FileW;
					m_u32ECGDataFileLength = stSaveDataFileHead1.u32Length;
					m_u32ECGDataFileR = m_u32ECGDataFileW;
					return 1;
				}
				else if ((flag2 == 0))
				{
					m_u32ECGDataFileW = stSaveDataFileHead2.u32FileW;
					m_u32ECGDataFileLength = stSaveDataFileHead2.u32Length;
					m_u32ECGDataFileR = m_u32ECGDataFileW;
					return 1;
				}
				else
				{
					m_u32ECGDataFileW = 0;
					m_u32ECGDataFileLength = 0;
					m_u32ECGDataFileR = m_u32ECGDataFileW;
					return 2;
				}
			}
			else
			{

				m_u32ECGDataFileW = 0;
				m_u32ECGDataFileLength = 0;
				m_u32ECGDataFileR = m_u32ECGDataFileW;
				return 3;
			}

		}
		else
		{
			m_i32FileErr = 1;
		}
	}
	CATCH(CFileException, e)
	{
		m_i32FileErr = 1;
		m_FileECGDataSave.Abort();
	}
	END_CATCH
}

int CECGDateSaveFile::get_rec_data(struct GET_ECG_DATA *st, int sec, int flag, CWnd *wnd)
{
	int a_flag = 0, a_u32ECGDataFileR,i,j;
	struct ECG_SAVE_HEAD *a_pstECG_SAVE_HEAD;
	if (flag == REC_FORWARD)
	{
		if (m_u32ECGDataFileLength==0)
		{
			a_flag = 1;
		}
		else
		{
			a_flag = 0;
			if (m_u32ECGDataFileW - m_u32ECGDataFileR>sec/10)
			{
				m_u32ECGDataFileR = m_u32ECGDataFileR + sec / 10;
			}
			else
			{
				m_u32ECGDataFileR = m_u32ECGDataFileW - 1;
			}
			a_u32ECGDataFileR = m_u32ECGDataFileR%SAVE_DATA_LENGTH;
		}
	} 
	else
	{
		if (m_u32ECGDataFileLength == 0)
		{
			a_flag = 1;
		}
		else
		{
			a_flag = 0;
			if (m_u32ECGDataFileR- sec / 10 >= (m_u32ECGDataFileW - m_u32ECGDataFileLength))
			{
				m_u32ECGDataFileR = m_u32ECGDataFileR - sec / 10;
			}
			else
			{
				m_u32ECGDataFileR = m_u32ECGDataFileW - m_u32ECGDataFileLength;
			}
			a_u32ECGDataFileR = m_u32ECGDataFileR%SAVE_DATA_LENGTH;
		}
	}

	if ((a_flag == 0) && (m_i32FileErr==0))
	{
		TRY
		{
			m_FileECGDataSave.Seek(SAVE_DATA_FILE_DATA_ADD + a_u32ECGDataFileR*m_u32SecLength, CFile::begin);
			if (m_FileECGDataSave.Read(m_pBuff, m_u32SecLength))
			{
				a_pstECG_SAVE_HEAD = (struct ECG_SAVE_HEAD *)m_pBuff;
				
				j = (sizeof(struct ECG_SAVE_HEAD) + 63) / 64 * 64;

				for (i = 0; i < 12; i++)
				{
					memcpy(st->pECGData[i], m_pBuff + j, ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE * 2);
					j += ECG_DATA_SAVE_TIME*ECG_SAMPLE_RATE * 2;
				}
			}
			else
			{
				a_flag = 1;
			}
		}
		CATCH(CFileException, e)
		{
			a_flag = 1;
			m_i32FileErr = 1;
			m_FileECGDataSave.Abort();
		}
		END_CATCH
	}

	if (a_flag==0)
		st->i32Flag = GET_ECG_DATA_FLAG_OK;
	else
		st->i32Flag = GET_ECG_DATA_FLAG_ERR;

	
	return 0;
}