// ThreadDataAlmRec.cpp : 实现文件
//

#include "stdafx.h"
#include "Verbpo.h"
#include "ThreadDataAlmRec.h"
#include "DialogAlmRec.h"

#define SAVE_DATA_FILE_HEAD_1_ADD   (0)
#define SAVE_DATA_FILE_HEAD_2_ADD   (1024*64)
#define SAVE_DATA_FILE_DATA_ADD   (1024*64+SAVE_DATA_FILE_HEAD_2_ADD)

#define SAVE_ALM_FILE_HEAD_1_ADD			(0)
#define SAVE_ALM_FILE_HEAD_2_ADD			(1024*64)
#define SAVE_ALM_FILE_INFO_ADD				(1024*64+SAVE_ALM_FILE_HEAD_2_ADD)
#define SAVE_ALM_INFO_SECTION_LENGTH		(4*1024)
#define SAVE_ALM_INFO_SECTION_ALL_LENGTH	((4*1024)*200)

#define SAVE_ALM_FILE_DATA_ADD		(SAVE_ALM_FILE_INFO_ADD+SAVE_ALM_INFO_SECTION_ALL_LENGTH+64*1024)
#define SAVE_ALM_FILE_DATA_LENGTH	 (32*1024)

#define GET_NIBP_IDLE   1
#define GET_NIBP_BUSY   2

#define GET_SYS_IDLE   1
#define GET_SYS_BUSY   2


#define GET_ALM_IDLE   1
#define GET_ALM_BUSY   2



static TCHAR n_cMagic[32] = _T("RZZH3ZFTEXJ154CFILLCX2QU12KP09U");
static TCHAR n_cVersion[8] = _T("V 1.00");

static TCHAR n_cPowerON[24] = _T("监护仪开机");
// CThreadDataAlmRec


UINT CThreadDataAlmRec::m_u32SaveDataFileItemLength = (12UL * 3600UL);
UINT CThreadDataAlmRec::m_u32SaveNIBPFileItemLength = 300UL;
UINT CThreadDataAlmRec::m_u32SaveSysFileItemLength = 200UL;
UINT CThreadDataAlmRec::m_u32SaveAlmFileItemLength = 200UL;

INT CThreadDataAlmRec::m_i32SaveAlmECGWaveLength = (12 * 500), CThreadDataAlmRec::m_i32SaveAlmRespWaveLength = (12 * 125), CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength = (12 * 125);


IMPLEMENT_DYNCREATE(CThreadDataAlmRec, CWinThread)

CThreadDataAlmRec::CThreadDataAlmRec()
{
	m_pSaveDataBuff = new UCHAR[1024 * 1024];
	m_u32SaveDataFileW = 0, m_u32SaveDataFileR = 0, m_u32SaveDataFileLength = 0;
	//6.6K
	m_u32SaveDataSecLength = ((CData::m_i32SaveDataECGWaveL * 6 + CData::m_i32SaveDataRespWaveL + CData::m_i32SaveDataPlethWaveL + (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128) + 8 * 1024 - 1) / (8 * 1024)* (8 * 1024);

	m_pSaveNIBPBuff = new UCHAR[1024 * 32];
	m_u32SaveNIBPFileW = 0, m_u32SaveNIBPFileR = 0, m_u32SaveNIBPFileLength = 0, m_u32SaveNIBPReadNum = 0;
	m_u32SaveNIBPSecLength = (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128;


	//存储数据文件的读写地址
	m_u32SaveSysFileW = 0, m_u32SaveSysFileR = 0, m_u32SaveSysFileLength = 0, m_u32SaveSysReadNum = 0;
	m_u32SaveSysSecLength = (sizeof(struct SYS_DATA) + 127) / 128 * 128;
	m_pSaveSysBuff = new UCHAR[1024 * 32];


	m_u8GetNIBPFlag = GET_NIBP_IDLE;
	m_u8GetSysFlag = GET_SYS_IDLE;

	m_i32AlmFileFlag = 0;
	m_i32AlmFileHeadFlag = 0;
	//存储数据文件的读写地址
	m_u32SaveAlmFileW = 0, m_u32SaveAlmFileR = 0, m_u32SaveAlmFileSelect = 0, m_u32SaveAlmFileLength = 0, m_u32SaveAlmReadNum = 0;
	m_u8GetAlmFlag = GET_ALM_IDLE;
	//第一次 select 是否 找到 一个 符合条件 的报警项
	m_u8SelectAlmFlag = 0;
	m_i32AlmGetFlag = 0;
	m_i32AlmSelectClose = 0;
	m_u32SaveAlmSecLength = CData::m_i32SaveDataECGWaveL * 6 + CData::m_i32SaveDataRespWaveL + CData::m_i32SaveDataPlethWaveL
		+ (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128
		+ (sizeof(struct SAVE_ALM_FILE_INFO) + 64) / 64 * 64;
	m_u32SaveAlmInfoLength = SAVE_ALM_INFO_SECTION_LENGTH;
	m_pSaveAlmBuff = new UCHAR[1024 * 48];
	m_stpSAVE_ALM_FILE_INFO = new struct SAVE_ALM_FILE_INFO[200];

	m_stGetAlm.pECGData[0] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
	m_stGetAlm.pECGData[1] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
	m_stGetAlm.pECGData[2] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];

	m_stGetAlm.pPlethData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength];
	m_stGetAlm.pRespData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmRespWaveLength];
}


CThreadDataAlmRec::~CThreadDataAlmRec()
{
	delete m_pSaveDataBuff;

	delete m_pSaveNIBPBuff;

	delete m_pSaveSysBuff;

	delete m_pSaveAlmBuff;

	delete m_stGetAlm.pECGData[0];
	delete m_stGetAlm.pECGData[1];
	delete m_stGetAlm.pECGData[2];
	delete m_stpSAVE_ALM_FILE_INFO;

	delete m_stGetAlm.pPlethData;
	delete m_stGetAlm.pRespData;
}

BOOL CThreadDataAlmRec::InitInstance()
{
	// TODO:    在此执行任意逐线程初始化
	m_i32FileFlag = 0;
	Init_Save_Data();
	Init_Save_NIBP();
	Init_Save_Sys();
	Init_Save_Alm();
	return TRUE;
}

int CThreadDataAlmRec::ExitInstance()
{
	list<struct SAVE_DATA>::iterator i;
	list<struct SAVE_ALM>::iterator j;
	// TODO:    在此执行任意逐线程清理
	TRY
	{
		if (m_i32FileFlag == 0)
		{
			m_FSaveData.Flush();
			m_FSaveData.Close();
		}
		if (m_i32NIBPFileFlag == 0)
		{
			m_FSaveNIBP.Flush();
			m_FSaveNIBP.Close();
		}
		if (m_i32SysFileFlag == 0)
		{
			m_FSaveSys.Flush();
			m_FSaveSys.Close();
		}
		if (m_i32AlmFileFlag == 0)
		{
			m_FSaveAlm.Flush();
			m_FSaveAlm.Close();
		}
	}
	CATCH(CFileException, e)
	{
	}
	END_CATCH
		m_CSSaveData.Lock();

	for (i = m_stlSaveData.begin(); (i != m_stlSaveData.end()); i++)
	{
		delete i->pECGData[0];
		delete i->pECGData[1];
		delete i->pECGData[2];
		delete i->pRespData;
		delete i->pPlethData;

	}

	for (j = m_stlSaveAlm.begin(); (j != m_stlSaveAlm.end()); j++)
	{
		delete j->pECGData[0];
		delete j->pECGData[1];
		delete j->pRespData;
		delete j->pPlethData;

	}

	m_stlSaveData.clear();
	m_pstlGetSaveData.clear();
	m_CSSaveData.Unlock();

	m_CSSaveNIBP.Lock();
	m_stlSaveNIBP.clear();
	m_stlGetNIBP.clear();
	m_CSSaveNIBP.Unlock();

	m_CSSaveNIBP.Lock();
	m_stlSaveSys.clear();
	m_stlGetSys.clear();
	m_CSSaveNIBP.Unlock();

	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CThreadDataAlmRec, CWinThread)
END_MESSAGE_MAP()


// CThreadDataAlmRec 消息处理程序

//数据实时保存 初始化
INT CThreadDataAlmRec::Init_Save_Data()
{
	INT a_i32Return = 0;

	a_i32Return = Check_Save_Data_File();

	if ((a_i32Return == 2) || (a_i32Return == 3))
	{
		//实时保存文件出错误
		a_i32Return = Reset_Save_Data_File();
	}


	return a_i32Return;
}


//检查实时数据文件是否完整
INT   CThreadDataAlmRec::Check_Save_Data_File()
{
	int i = 0, flag1 = 0, flag2 = 0;

	TRY
	{
		//如果成功打开，则返回非零值，否则为0
		if (m_FSaveData.Open(theApp.m_csSaveDataFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite, 0))
		{
			i = (sizeof(struct SAVE_DATA_FILE_HEAD) + 255) / 256 * 256;
			if (m_FSaveData.GetLength() > 2 * SAVE_DATA_FILE_HEAD_2_ADD)
			{
				//读取文件头 1
				m_FSaveData.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
				if (m_FSaveData.Read(m_pSaveDataBuff, i) == i)
				{
					memcpy((void *)(&stSaveDataFileHead1), m_pSaveDataBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//读取文件头 2
				m_FSaveData.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
				if (m_FSaveData.Read(m_pSaveDataBuff, i) == i)
				{
					memcpy((void *)(&stSaveDataFileHead2), m_pSaveDataBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//判断文件头 类型是否正确
				for (i = 0; i < 8; i++)
				{
					if (stSaveDataFileHead1.cMagic[i] != n_cMagic[i])
						flag1 = 1;
					if (stSaveDataFileHead2.cMagic[i] != n_cMagic[i])
						flag2 = 1;
				}

				for (i = 0; i < 8; i++)
				{
					stSaveDataFileHead1.cMagic[i] = n_cMagic[i];
					stSaveDataFileHead2.cMagic[i] = n_cMagic[i];

					stSaveDataFileHead1.cVersion[i] = n_cVersion[i];
					stSaveDataFileHead2.cVersion[i] = n_cVersion[i];
				}

				//两个文件头 都正确
				if ((flag1 == 0) && (flag2 == 0))
				{
					if (stSaveDataFileHead2.u32FileW > stSaveDataFileHead1.u32FileW)
					{
						m_u32SaveDataFileW = stSaveDataFileHead2.u32FileW;
						m_u32SaveDataFileLength = stSaveDataFileHead2.u32Length;
					}
					else
					{
						m_u32SaveDataFileW = stSaveDataFileHead1.u32FileW;
						m_u32SaveDataFileLength = stSaveDataFileHead1.u32Length;
					}
					m_u32SaveDataFileR = m_u32SaveDataFileW;

					return 0;

				}
				//文件头1 正确 文件头 2 错误
				else if ((flag1 == 0))
				{
					m_u32SaveDataFileW = stSaveDataFileHead1.u32FileW;
					m_u32SaveDataFileLength = stSaveDataFileHead1.u32Length;
					m_u32SaveDataFileR = m_u32SaveDataFileW;

					return 1;
				}
				//文件头2 正确 文件头1 错误
				else if ((flag2 == 0))
				{
					m_u32SaveDataFileW = stSaveDataFileHead2.u32FileW;
					m_u32SaveDataFileLength = stSaveDataFileHead2.u32Length;
					m_u32SaveDataFileR = m_u32SaveDataFileW;

					return 1;
				}

				//两个文件头 都错误
				else
				{
					m_u32SaveDataFileW = 0;
					m_u32SaveDataFileLength = 0;
					m_u32SaveDataFileR = m_u32SaveDataFileW;

					//两个文件头 都错误
					return 2;
				}
			}
			else
			{

				m_u32SaveDataFileW = 0;
				m_u32SaveDataFileLength = 0;
				m_u32SaveDataFileR = m_u32SaveDataFileW;

				//文件长度错误
				return 3;
			}

		}
		else
		{
			m_i32FileFlag = 1;
			//文件 打开 错误
			return 4;
		}
	}
	CATCH(CFileException, e)
	{
		m_i32FileFlag = 1;
		m_FSaveData.Abort();
	}
	END_CATCH

		return 0;
}

//复位实时数据文件
INT   CThreadDataAlmRec::Reset_Save_Data_File()
{
	int i = 0;
	COleDateTime tm = COleDateTime::GetCurrentTime();
	CString cs1;
	INT a_Flag = 0;
	cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	if (m_i32FileFlag == 0)
	{
		stSaveDataFileHead1.u32FileW = m_u32SaveDataFileW;
		stSaveDataFileHead1.u32Length = m_u32SaveDataFileLength;
		wcscpy_s(stSaveDataFileHead1.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveDataFileHead1.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		stSaveDataFileHead2.u32FileW = m_u32SaveDataFileW;
		stSaveDataFileHead2.u32Length = m_u32SaveDataFileLength;
		wcscpy_s(stSaveDataFileHead2.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveDataFileHead2.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		for (i = 0; i < 8; i++)
		{
			stSaveDataFileHead1.cMagic[i] = n_cMagic[i];
			stSaveDataFileHead2.cMagic[i] = n_cMagic[i];

			stSaveDataFileHead1.cVersion[i] = n_cVersion[i];
			stSaveDataFileHead2.cVersion[i] = n_cVersion[i];
		}


		TRY
		{
			m_FSaveData.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
			m_FSaveData.Write((&stSaveDataFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveData.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
			m_FSaveData.Write((&stSaveDataFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveData.Flush();

		}
		CATCH(CFileException, e)
		{
			a_Flag = 1;
			m_i32FileFlag = 1;
			m_FSaveData.Abort();
		}
		END_CATCH
	}

	return a_Flag;
}

//提交一份实时数据 保存项
INT CThreadDataAlmRec::Save_Data(struct SAVE_DATA * st)
{
	struct SAVE_DATA a_stSAVE_DATA;
	INT a_i32Return = 0;

	m_CSSaveData.Lock();
	if (m_stlSaveData.size() > 32)
	{
		a_i32Return = 1;
	}
	m_CSSaveData.Unlock();

	if (a_i32Return == 0)
	{
		CString cs1;
		COleDateTime tm = COleDateTime::GetCurrentTime();
		cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());


		memcpy(&(a_stSAVE_DATA.stPara), &(st->stPara), sizeof(struct SAVE_DATA_PARA));
		wcscpy_s(a_stSAVE_DATA.stPara.cTime, 20, cs1);

		a_stSAVE_DATA.pECGData[0] = new UINT16[CData::m_i32SaveDataECGWaveL];
		a_stSAVE_DATA.pECGData[1] = new UINT16[CData::m_i32SaveDataECGWaveL];
		a_stSAVE_DATA.pECGData[2] = new UINT16[CData::m_i32SaveDataECGWaveL];

		a_stSAVE_DATA.pRespData = new UINT8[CData::m_i32SaveDataRespWaveL];
		a_stSAVE_DATA.pPlethData = new UINT8[CData::m_i32SaveDataPlethWaveL];

		memcpy(a_stSAVE_DATA.pECGData[0], st->pECGData[0], CData::m_i32SaveDataECGWaveL * 2);
		memcpy(a_stSAVE_DATA.pECGData[1], st->pECGData[1], CData::m_i32SaveDataECGWaveL * 2);
		memcpy(a_stSAVE_DATA.pECGData[2], st->pECGData[2], CData::m_i32SaveDataECGWaveL * 2);
		memcpy(a_stSAVE_DATA.pRespData, st->pRespData, CData::m_i32SaveDataRespWaveL);
		memcpy(a_stSAVE_DATA.pPlethData, st->pPlethData, CData::m_i32SaveDataPlethWaveL);

		m_CSSaveData.Lock();
		m_stlSaveData.push_back(a_stSAVE_DATA);
		m_CSSaveData.Unlock();
	}


	return a_i32Return;
}

//进行实时数据保存
INT CThreadDataAlmRec::Do_Save()
{
	list<struct SAVE_DATA>::iterator i;
	INT j = 0, a_i32Flag = 0;

	m_CSSaveData.Lock();
	if (m_stlSaveData.size())
	{
		i = m_stlSaveData.begin();
		a_i32Flag = 1;
	}
	m_CSSaveData.Unlock();

	if (a_i32Flag)
	{
		//复制参数值
		memcpy(m_pSaveDataBuff, &(i->stPara), sizeof(struct SAVE_DATA_PARA));
		j = (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128;

		//复制心电波形数据
		memcpy(m_pSaveDataBuff + j, i->pECGData[0], CData::m_i32SaveDataECGWaveL * 2);
		j += CData::m_i32SaveDataECGWaveL * 2;
		//复制心电波形数据
		memcpy(m_pSaveDataBuff + j, i->pECGData[1], CData::m_i32SaveDataECGWaveL * 2);
		j += CData::m_i32SaveDataECGWaveL * 2;
		//复制心电波形数据
		memcpy(m_pSaveDataBuff + j, i->pECGData[2], CData::m_i32SaveDataECGWaveL * 2);
		j += CData::m_i32SaveDataECGWaveL * 2;
		//复制呼吸波形数据
		memcpy(m_pSaveDataBuff + j, i->pRespData, CData::m_i32SaveDataRespWaveL);
		j += CData::m_i32SaveDataRespWaveL;
		//复制血氧波形数据
		memcpy(m_pSaveDataBuff + j, i->pPlethData, CData::m_i32SaveDataPlethWaveL);
		j += CData::m_i32SaveDataPlethWaveL;

		//回收内存
		delete i->pECGData[0];
		delete i->pECGData[1];
		delete i->pECGData[2];
		delete i->pRespData;
		delete i->pPlethData;

		m_CSSaveData.Lock();
		m_stlSaveData.pop_front();
		m_CSSaveData.Unlock();

		TRY
		{
			if ((m_i32FileFlag == 0))
			{
				//保存实时数据 到文件中
				m_FSaveData.Seek(SAVE_DATA_FILE_DATA_ADD + (m_u32SaveDataFileW%CThreadDataAlmRec::m_u32SaveDataFileItemLength)*m_u32SaveDataSecLength, CFile::begin);
				m_FSaveData.Write(m_pSaveDataBuff, m_u32SaveDataSecLength);
				m_FSaveData.Flush();
				//m_u32SaveDataFileR = m_u32SaveDataFileW;
				m_u32SaveDataFileW = (m_u32SaveDataFileW + 1) % CThreadDataAlmRec::m_u32SaveDataFileItemLength;

				if (m_u32SaveDataFileLength < CThreadDataAlmRec::m_u32SaveDataFileItemLength)
				{
					m_u32SaveDataFileLength++;
				}

				//更新文件的 读写 长度 数据
				if (m_u32SaveDataFileW % 1 == 0)
				{
					COleDateTime tm = COleDateTime::GetCurrentTime();
					CString cs1;

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

						m_FSaveData.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
						m_FSaveData.Write((&stSaveDataFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

						m_i32FileHeadFlag = 1;
					}
					else
					{
						stSaveDataFileHead2.u32FileW = m_u32SaveDataFileW;
						stSaveDataFileHead2.u32Length = m_u32SaveDataFileLength;

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

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


	return 0;
}
//获得一份实时数据
INT CThreadDataAlmRec::Get_Data(struct SAVE_DATA * st)
{
	INT a_i32Return = 0;

	m_CSSaveData.Lock();
	m_pstlGetSaveData.push_back(st);
	m_CSSaveData.Unlock();

	return a_i32Return;
}

//从实时数据保存的文件中 读出一份数据
INT CThreadDataAlmRec::Do_Get()
{
	list<struct SAVE_DATA *>::iterator i;
	INT j = 0, a_i32Flag = 0, a_flag = 0;
	INT a_i32dir = 0, sec = 0;
	struct SAVE_DATA * a_pSAVE_DATA;

	m_CSSaveData.Lock();
	if (m_pstlGetSaveData.size())
	{
		i = m_pstlGetSaveData.begin();
		a_i32Flag = 1;
	}
	m_CSSaveData.Unlock();



	if (a_i32Flag)
	{
		a_i32dir = (*i)->i32dir;
		sec = (*i)->i32time;
		if (a_i32dir == REC_CURRENT)
		{	
			//获得当前最新的实时数据保存点
			m_u32SaveDataFileR = (m_u32SaveDataFileW + CThreadDataAlmRec::m_u32SaveDataFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveDataFileItemLength;
		}
		//向前 即读取更新的数据
		else if (a_i32dir == REC_FORWARD)
		{
			//判断前面是否还有 sec条 记录           
			if (((m_u32SaveDataFileW + CThreadDataAlmRec::m_u32SaveDataFileItemLength - m_u32SaveDataFileR) % CThreadDataAlmRec::m_u32SaveDataFileItemLength) >= sec)
			{
				//设置新的读取点               
				m_u32SaveDataFileR = (m_u32SaveDataFileR + sec) % CThreadDataAlmRec::m_u32SaveDataFileItemLength;
			}
			else
			{
				//设置新的读取点 为  当前最新的实时数据保存点               			
				m_u32SaveDataFileR = (m_u32SaveDataFileW + CThreadDataAlmRec::m_u32SaveDataFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveDataFileItemLength;
			}
		}
		//向后 即读取更老的数据
		else
		{
			//数据保存的环形文件 已经 写满 
			if (m_u32SaveDataFileLength >= CThreadDataAlmRec::m_u32SaveDataFileItemLength)
			{
				//判断后面是否还有 sec条 记录           
				if (((m_u32SaveDataFileR + CThreadDataAlmRec::m_u32SaveDataFileItemLength - m_u32SaveDataFileW) % CThreadDataAlmRec::m_u32SaveDataFileItemLength) >= sec)
				{
					m_u32SaveDataFileR = ((m_u32SaveDataFileR + CThreadDataAlmRec::m_u32SaveDataFileItemLength - sec) % CThreadDataAlmRec::m_u32SaveDataFileItemLength);
				}
				else
				{
					m_u32SaveDataFileR =m_u32SaveDataFileW;
				}
			}
			else
			{
				//判断后面是否还有 sec条 记录           
				if (m_u32SaveDataFileR >= sec)
				{
					m_u32SaveDataFileR = m_u32SaveDataFileR - sec;
				}
				else
				{
					m_u32SaveDataFileR = 0;
				}
			}
		}

		if (m_i32FileFlag == 0)
		{
			TRY
			{
				m_FSaveData.Seek(SAVE_DATA_FILE_DATA_ADD + ((m_u32SaveDataFileR + CThreadDataAlmRec::m_u32SaveDataFileItemLength) % CThreadDataAlmRec::m_u32SaveDataFileItemLength) * m_u32SaveDataSecLength, CFile::begin);
				if (m_FSaveData.Read(m_pSaveDataBuff, m_u32SaveDataSecLength))
				{
					memcpy(&((*i)->stPara), m_pSaveDataBuff, sizeof(struct SAVE_DATA_PARA));
					j = (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128;

					//复制心电波形数据
					memcpy((*i)->pECGData[0], m_pSaveDataBuff + j, CData::m_i32SaveDataECGWaveL * 2);
					j += CData::m_i32SaveDataECGWaveL * 2;
					//复制心电波形数据
					memcpy((*i)->pECGData[1], m_pSaveDataBuff + j, CData::m_i32SaveDataECGWaveL * 2);
					j += CData::m_i32SaveDataECGWaveL * 2;

					//复制心电波形数据
					memcpy((*i)->pECGData[2], m_pSaveDataBuff + j, CData::m_i32SaveDataECGWaveL * 2);
					j += CData::m_i32SaveDataECGWaveL * 2;
					//复制呼吸波形数据
					memcpy((*i)->pRespData, m_pSaveDataBuff + j, CData::m_i32SaveDataRespWaveL);
					j += CData::m_i32SaveDataRespWaveL;
					//复制血氧波形数据
					memcpy((*i)->pPlethData, m_pSaveDataBuff + j, CData::m_i32SaveDataPlethWaveL);
					j += CData::m_i32SaveDataPlethWaveL;

					a_flag = 0;
				}
				else
				{
					a_flag = 1;
				}
			}
			CATCH(CFileException, e)
			{
				a_flag = 1;
				m_i32FileFlag = 1;
				m_FSaveData.Abort();
			}
			END_CATCH

			if ((*i)->wnd)
			{
				(*i)->wnd->PostMessage(WM_MONITOR_STATUS, WM_REC_GET_OK, 0);
			}

			//出队列
			m_CSSaveData.Lock();
			m_pstlGetSaveData.pop_front();
			m_CSSaveData.Unlock();
		}
	}

	return 0;
}

//血压实时保存 初始化
INT CThreadDataAlmRec::Init_Save_NIBP()
{
	INT a_i32Return = 0;

	a_i32Return = Check_Save_NIBP_File();

	if ((a_i32Return == 2) || (a_i32Return == 3))
	{
		//实时保存文件出错误
		a_i32Return = Reset_Save_NIBP_File();
	}


	return a_i32Return;
}

//检查实时血压文件是否完整
INT CThreadDataAlmRec::Check_Save_NIBP_File()
{
	int i = 0, flag1 = 0, flag2 = 0;

	TRY
	{
		//如果成功打开，则返回非零值，否则为0
		if (m_FSaveNIBP.Open(theApp.m_csSaveNIBPFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite, 0))
		{
			i = (sizeof(struct SAVE_DATA_FILE_HEAD) + 255) / 256 * 256;
			if (m_FSaveNIBP.GetLength() > 2 * SAVE_DATA_FILE_HEAD_2_ADD)
			{
				//读取文件头 1
				m_FSaveNIBP.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
				if (m_FSaveNIBP.Read(m_pSaveNIBPBuff, i) == i)
				{
					memcpy((void *)(&stSaveNIBPFileHead1), m_pSaveNIBPBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//读取文件头 2
				m_FSaveNIBP.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
				if (m_FSaveNIBP.Read(m_pSaveNIBPBuff, i) == i)
				{
					memcpy((void *)(&stSaveNIBPFileHead2), m_pSaveNIBPBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//判断文件头 类型是否正确
				for (i = 0; i < 8; i++)
				{
					if (stSaveNIBPFileHead1.cMagic[i] != n_cMagic[i])
						flag1 = 1;
					if (stSaveNIBPFileHead2.cMagic[i] != n_cMagic[i])
						flag2 = 1;
				}

				for (i = 0; i < 8; i++)
				{
					stSaveNIBPFileHead1.cMagic[i] = n_cMagic[i];
					stSaveNIBPFileHead2.cMagic[i] = n_cMagic[i];

					stSaveNIBPFileHead1.cVersion[i] = n_cVersion[i];
					stSaveNIBPFileHead2.cVersion[i] = n_cVersion[i];
				}

				//两个文件头 都正确
				if ((flag1 == 0) && (flag2 == 0))
				{
					if (stSaveNIBPFileHead2.u32FileW > stSaveNIBPFileHead1.u32FileW)
					{
						m_u32SaveNIBPFileW = stSaveNIBPFileHead2.u32FileW;
						m_u32SaveNIBPFileLength = stSaveNIBPFileHead2.u32Length;
					}
					else
					{
						m_u32SaveNIBPFileW = stSaveNIBPFileHead1.u32FileW;
						m_u32SaveNIBPFileLength = stSaveNIBPFileHead1.u32Length;
					}
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;

					return 0;

				}
				//文件头1 正确 文件头 2 错误
				else if ((flag1 == 0))
				{
					m_u32SaveNIBPFileW = stSaveNIBPFileHead1.u32FileW;
					m_u32SaveNIBPFileLength = stSaveNIBPFileHead1.u32Length;
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;

					return 1;
				}
				//文件头2 正确 文件头1 错误
				else if ((flag2 == 0))
				{
					m_u32SaveNIBPFileW = stSaveNIBPFileHead2.u32FileW;
					m_u32SaveNIBPFileLength = stSaveNIBPFileHead2.u32Length;
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;

					return 1;
				}

				//两个文件头 都错误
				else
				{
					m_u32SaveNIBPFileW = 0;
					m_u32SaveNIBPFileLength = 0;
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;

					//两个文件头 都错误
					return 2;
				}
			}
			else
			{

				m_u32SaveNIBPFileW = 0;
				m_u32SaveNIBPFileLength = 0;
				m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;

				//文件长度错误
				return 3;
			}

		}
		else
		{
			m_i32NIBPFileFlag = 1;
			//文件 打开 错误
			return 4;
		}
	}
	CATCH(CFileException, e)
	{
		m_i32NIBPFileFlag = 1;
		m_FSaveNIBP.Abort();
	}
	END_CATCH

		return 0;
}

//复位实时数据文件
INT CThreadDataAlmRec::Reset_Save_NIBP_File()
{
	int i = 0;
	COleDateTime tm = COleDateTime::GetCurrentTime();
	CString cs1;
	INT a_Flag = 0;
	cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	if (m_i32NIBPFileFlag == 0)
	{
		stSaveNIBPFileHead1.u32FileW = m_u32SaveNIBPFileW;
		stSaveNIBPFileHead1.u32Length = m_u32SaveNIBPFileLength;
		wcscpy_s(stSaveNIBPFileHead1.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveNIBPFileHead1.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		stSaveNIBPFileHead2.u32FileW = m_u32SaveNIBPFileW;
		stSaveNIBPFileHead2.u32Length = m_u32SaveNIBPFileLength;
		wcscpy_s(stSaveNIBPFileHead2.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveNIBPFileHead2.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		for (i = 0; i < 8; i++)
		{
			stSaveNIBPFileHead1.cMagic[i] = n_cMagic[i];
			stSaveNIBPFileHead2.cMagic[i] = n_cMagic[i];

			stSaveNIBPFileHead1.cVersion[i] = n_cVersion[i];
			stSaveNIBPFileHead2.cVersion[i] = n_cVersion[i];
		}


		TRY
		{
			m_FSaveNIBP.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
			m_FSaveNIBP.Write((&stSaveNIBPFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveNIBP.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
			m_FSaveNIBP.Write((&stSaveNIBPFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveNIBP.Flush();

		}
		CATCH(CFileException, e)
		{
			a_Flag = 1;
			m_i32NIBPFileFlag = 1;
			m_FSaveNIBP.Abort();
		}
		END_CATCH
	}

	return a_Flag;
}

//提交一份血压数据 保存项
INT CThreadDataAlmRec::Save_NIBP(struct NIBP_DATA * st)
{
	struct NIBP_DATA a_stNIBP_DATA;
	INT a_i32Return = 0;

	m_CSSaveNIBP.Lock();
	if (m_stlSaveNIBP.size() > 32)
	{
		a_i32Return = 1;
	}
	m_CSSaveNIBP.Unlock();

	if (a_i32Return == 0)
	{
		CString cs1;
		COleDateTime tm = COleDateTime::GetCurrentTime();

		memcpy(&a_stNIBP_DATA, st, sizeof(struct NIBP_DATA));

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

		m_CSSaveNIBP.Lock();
		m_stlSaveNIBP.push_back(a_stNIBP_DATA);
		m_CSSaveNIBP.Unlock();
	}

	return a_i32Return;
}

//进行血压数据保存
INT CThreadDataAlmRec::Do_Save_NIBP()
{
	list<struct NIBP_DATA>::iterator i;
	INT j = 0, a_i32Flag = 0;

	m_CSSaveNIBP.Lock();
	if (m_stlSaveNIBP.size())
	{
		i = m_stlSaveNIBP.begin();
		a_i32Flag = 1;
	}
	m_CSSaveNIBP.Unlock();

	if (a_i32Flag)
	{
		//复制参数值
		memcpy(m_pSaveNIBPBuff, (struct NIBP_DATA *)(&(*i)), sizeof(struct NIBP_DATA));
		j = (sizeof(struct NIBP_DATA) + 127) / 128 * 128;


		m_CSSaveNIBP.Lock();
		m_stlSaveNIBP.pop_front();
		m_CSSaveNIBP.Unlock();

		TRY
		{
			if ((m_i32NIBPFileFlag == 0))
			{
				//保存实时数据 到文件中
				m_FSaveNIBP.Seek(SAVE_DATA_FILE_DATA_ADD + (m_u32SaveNIBPFileW%CThreadDataAlmRec::m_u32SaveNIBPFileItemLength)*j, CFile::begin);
				m_FSaveNIBP.Write(m_pSaveNIBPBuff, j);
				m_FSaveNIBP.Flush();
				//m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;
				m_u32SaveNIBPFileW++;

				if (m_u32SaveNIBPFileLength < CThreadDataAlmRec::m_u32SaveNIBPFileItemLength)
				{
					m_u32SaveNIBPFileLength++;
				}

				//更新文件的 读写 长度 数据
				if (m_u32SaveNIBPFileW % 1 == 0)
				{
					COleDateTime tm = COleDateTime::GetCurrentTime();
					CString cs1;

					cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
					if (m_i32NIBPFileHeadFlag == 0)
					{
						stSaveNIBPFileHead1.u32FileW = m_u32SaveNIBPFileW;
						stSaveNIBPFileHead1.u32Length = m_u32SaveNIBPFileLength;

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

						m_FSaveNIBP.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
						m_FSaveNIBP.Write((&stSaveNIBPFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

						m_i32NIBPFileHeadFlag = 1;
					}
					else
					{
						stSaveNIBPFileHead2.u32FileW = m_u32SaveNIBPFileW;
						stSaveNIBPFileHead2.u32Length = m_u32SaveNIBPFileLength;

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

						m_FSaveNIBP.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
						m_FSaveNIBP.Write((&stSaveNIBPFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));

						m_i32NIBPFileHeadFlag = 0;
					}
					m_FSaveNIBP.Flush();
				}
			}
		}
		CATCH(CFileException, e)
		{
			m_i32NIBPFileFlag = 1;
			m_FSaveNIBP.Abort();
		}
		END_CATCH
	}

	return 0;
}

//获得一份血压数据
INT CThreadDataAlmRec::Get_NIBP(INT dir)
{
	UINT a_u32Data2 = 0;

	m_CSSaveNIBP.Lock();
	if (m_u8GetNIBPFlag == GET_NIBP_IDLE)
	{
		if (dir == REC_CURRENT)
		{
			if (m_u32SaveNIBPFileLength >= 10)
			{
				m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;
				m_u32SaveNIBPReadNum = 10;
			}
			else
			{
				m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;
				m_u32SaveNIBPReadNum = m_u32SaveNIBPFileLength;
			}
		}
		else if (dir == REC_FORWARD)
		{

			//判断前面是否还有 10条 记录           
			if (((m_u32SaveNIBPFileW + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - m_u32SaveNIBPFileR) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength) >= 10)
			{
				//前面仍然还有10条             
				//设置新的读取点               
				m_u32SaveNIBPFileR = (m_u32SaveNIBPFileR + 10) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength;
				m_u32SaveNIBPReadNum = 10;

			}
			else
			{
				//前面记录的已经不够10条        
				//设置新的读取点                
				//m_u32SaveNIBPReadNum = (m_u32SaveNIBPFileW + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - m_u32SaveNIBPFileR) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength;
				//m_u32SaveNIBPFileR=m_u32SaveNIBPFileW;

				if (m_u32SaveNIBPFileLength >= 10)
				{
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;
					m_u32SaveNIBPReadNum = 10;
				}
				else
				{
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;
					m_u32SaveNIBPReadNum = m_u32SaveNIBPFileLength;
				}
			}
		}
		else
		{

			if (m_u32SaveNIBPFileLength >= CThreadDataAlmRec::m_u32SaveNIBPFileItemLength)
			{
				//判断后面是否还有 10条 记录           
				if (((m_u32SaveNIBPFileR + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - m_u32SaveNIBPFileW) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength) >= 10)
				{
					a_u32Data2 = ((m_u32SaveNIBPFileR + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - 10) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength);
					//判断后面是否还有 10条 记录                
					if (((a_u32Data2 + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - m_u32SaveNIBPFileW) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength) >= 10)
					{
						//后面仍然还有10条             
						//设置新的读取点                            
						m_u32SaveNIBPFileR = ((m_u32SaveNIBPFileR + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - 10) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength);
						//可以读取10条                
						m_u32SaveNIBPReadNum = 10;
					}
					else
					{
						//后面记录的已经不够10条        
						//设置新的读取点                      
						m_u32SaveNIBPFileR = ((m_u32SaveNIBPFileW + 10) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength);
						//可以读取10条               
						m_u32SaveNIBPReadNum = 10;
					}
				}
				else
				{
					m_u32SaveNIBPFileR = ((m_u32SaveNIBPFileW + 10) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength);
					//可以读取10条               
					m_u32SaveNIBPReadNum = 10;
				}
			}
			else
			{
				//判断后面是否还有 10条 记录           
				if (m_u32SaveNIBPFileR >= 10)
				{
					a_u32Data2 = m_u32SaveNIBPFileR - 10;
					//判断后面是否还有 10条 记录                
					if (a_u32Data2 >= 10)
					{
						//后面仍然还有10条             
						//设置新的读取点                            
						m_u32SaveNIBPFileR = m_u32SaveNIBPFileR - 10;
						//可以读取10条                
						m_u32SaveNIBPReadNum = 10;
					}
					else
					{
						//后面记录的已经不够10条        
						//设置新的读取点                      
						m_u32SaveNIBPFileR = 10;
						//可以读取10条               
						m_u32SaveNIBPReadNum = 10;
					}
				}
				else
				{
					m_u32SaveNIBPFileR = m_u32SaveNIBPFileW;
					m_u32SaveNIBPReadNum = m_u32SaveNIBPFileLength;
				}
			}
		}

		m_u8GetNIBPFlag = GET_NIBP_BUSY;
		m_CSSaveNIBP.Unlock();

		return 1;
	}
	m_CSSaveNIBP.Unlock();

	return 0;
}

//从血压数据保存的文件中 读出一份数据
INT CThreadDataAlmRec::Do_Get_NIBP()
{
	UINT8 a_u8GetNIBPFlag = 0;
	UINT i = 0;
	UINT a_u32NIBP_RESULTFileR;
	struct NIBP_DATA a_stNIBP_RESULT;

	m_CSSaveNIBP.Lock();
	a_u8GetNIBPFlag = m_u8GetNIBPFlag;
	m_CSSaveNIBP.Unlock();

	if (a_u8GetNIBPFlag == GET_NIBP_BUSY)
	{
		m_stlGetNIBP.clear();
		a_u32NIBP_RESULTFileR = (m_u32SaveNIBPFileR + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength;;
		for (i = 0; (i < m_u32SaveNIBPReadNum) && (i < 10); i++)
		{
			m_FSaveNIBP.Seek(SAVE_DATA_FILE_DATA_ADD + a_u32NIBP_RESULTFileR*m_u32SaveNIBPSecLength, CFile::begin);
			m_FSaveNIBP.Read((char *)(&a_stNIBP_RESULT), sizeof(struct NIBP_DATA));
			m_stlGetNIBP.push_back(a_stNIBP_RESULT);

			a_u32NIBP_RESULTFileR = (a_u32NIBP_RESULTFileR + CThreadDataAlmRec::m_u32SaveNIBPFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveNIBPFileItemLength;
		}

		m_CSSaveNIBP.Lock();
		m_u8GetNIBPFlag = GET_NIBP_IDLE;
		m_CSSaveNIBP.Unlock();

		wndSaveNIBP->PostMessage(WM_MONITOR_STATUS, WM_REC_GET_OK, 0);
		//emit signal_Get_NIBP();
	}

	return 0;
}



//系统 信息 保存 初始化
INT CThreadDataAlmRec::Init_Save_Sys()
{
	INT a_i32Return = 0;

	a_i32Return = Check_Save_Sys_File();

	if ((a_i32Return == 2) || (a_i32Return == 3))
	{
		//实时保存文件出错误
		a_i32Return = Reset_Save_Sys_File();
	}


	return a_i32Return;
}

//检查实时系统 信息文件是否完整
INT CThreadDataAlmRec::Check_Save_Sys_File()
{
	int i = 0, flag1 = 0, flag2 = 0;

	TRY
	{
		//如果成功打开，则返回非零值，否则为0
		if (m_FSaveSys.Open(theApp.m_csSaveSysPFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite, 0))
		{
			i = (sizeof(struct SAVE_DATA_FILE_HEAD) + 255) / 256 * 256;
			if (m_FSaveSys.GetLength() > 2 * SAVE_DATA_FILE_HEAD_2_ADD)
			{
				//读取文件头 1
				m_FSaveSys.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
				if (m_FSaveSys.Read(m_pSaveSysBuff, i) == i)
				{
					memcpy((void *)(&stSaveSysFileHead1), m_pSaveSysBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//读取文件头 2
				m_FSaveSys.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
				if (m_FSaveSys.Read(m_pSaveSysBuff, i) == i)
				{
					memcpy((void *)(&stSaveSysFileHead2), m_pSaveSysBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//判断文件头 类型是否正确
				for (i = 0; i < 8; i++)
				{
					if (stSaveSysFileHead1.cMagic[i] != n_cMagic[i])
						flag1 = 1;
					if (stSaveSysFileHead2.cMagic[i] != n_cMagic[i])
						flag2 = 1;
				}

				for (i = 0; i < 8; i++)
				{
					stSaveSysFileHead1.cMagic[i] = n_cMagic[i];
					stSaveSysFileHead2.cMagic[i] = n_cMagic[i];

					stSaveSysFileHead1.cVersion[i] = n_cVersion[i];
					stSaveSysFileHead2.cVersion[i] = n_cVersion[i];
				}

				//两个文件头 都正确
				if ((flag1 == 0) && (flag2 == 0))
				{
					if (stSaveSysFileHead2.u32FileW > stSaveSysFileHead1.u32FileW)
					{
						m_u32SaveSysFileW = stSaveSysFileHead2.u32FileW;
						m_u32SaveSysFileLength = stSaveSysFileHead2.u32Length;
					}
					else
					{
						m_u32SaveSysFileW = stSaveSysFileHead1.u32FileW;
						m_u32SaveSysFileLength = stSaveSysFileHead1.u32Length;
					}
					m_u32SaveSysFileR = m_u32SaveSysFileW;

					return 0;

				}
				//文件头1 正确 文件头 2 错误
				else if ((flag1 == 0))
				{
					m_u32SaveSysFileW = stSaveSysFileHead1.u32FileW;
					m_u32SaveSysFileLength = stSaveSysFileHead1.u32Length;
					m_u32SaveSysFileR = m_u32SaveSysFileW;

					return 1;
				}
				//文件头2 正确 文件头1 错误
				else if ((flag2 == 0))
				{
					m_u32SaveSysFileW = stSaveSysFileHead2.u32FileW;
					m_u32SaveSysFileLength = stSaveSysFileHead2.u32Length;
					m_u32SaveSysFileR = m_u32SaveSysFileW;

					return 1;
				}

				//两个文件头 都错误
				else
				{
					m_u32SaveSysFileW = 0;
					m_u32SaveSysFileLength = 0;
					m_u32SaveSysFileR = m_u32SaveSysFileW;

					//两个文件头 都错误
					return 2;
				}
			}
			else
			{

				m_u32SaveSysFileW = 0;
				m_u32SaveSysFileLength = 0;
				m_u32SaveSysFileR = m_u32SaveSysFileW;

				//文件长度错误
				return 3;
			}

		}
		else
		{
			m_i32SysFileFlag = 1;
			//文件 打开 错误
			return 4;
		}
	}
	CATCH(CFileException, e)
	{
		m_i32SysFileFlag = 1;
		m_FSaveSys.Abort();
	}
	END_CATCH

		return 0;
}

//复位系统 信息文件
INT CThreadDataAlmRec::Reset_Save_Sys_File()
{
	int i = 0;
	COleDateTime tm = COleDateTime::GetCurrentTime();
	CString cs1;
	INT a_Flag = 0;
	cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	if (m_i32SysFileFlag == 0)
	{
		stSaveSysFileHead1.u32FileW = m_u32SaveSysFileW;
		stSaveSysFileHead1.u32Length = m_u32SaveSysFileLength;
		wcscpy_s(stSaveSysFileHead1.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveSysFileHead1.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		stSaveSysFileHead2.u32FileW = m_u32SaveSysFileW;
		stSaveSysFileHead2.u32Length = m_u32SaveSysFileLength;
		wcscpy_s(stSaveSysFileHead2.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveSysFileHead2.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		for (i = 0; i < 8; i++)
		{
			stSaveSysFileHead1.cMagic[i] = n_cMagic[i];
			stSaveSysFileHead2.cMagic[i] = n_cMagic[i];

			stSaveSysFileHead1.cVersion[i] = n_cVersion[i];
			stSaveSysFileHead2.cVersion[i] = n_cVersion[i];
		}


		TRY
		{
			m_FSaveSys.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
			m_FSaveSys.Write((&stSaveSysFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveSys.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
			m_FSaveSys.Write((&stSaveSysFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveSys.Flush();

		}
		CATCH(CFileException, e)
		{
			a_Flag = 1;
			m_i32SysFileFlag = 1;
			m_FSaveSys.Abort();
		}
		END_CATCH
	}

	return a_Flag;
}

//提交一份系统 信息数据 保存项
INT CThreadDataAlmRec::Save_Sys(struct SYS_DATA * st)
{
	struct SYS_DATA a_stSysSave;
	INT a_i32Return = 0;

	m_CSSaveSys.Lock();
	if (m_stlSaveSys.size() > 32)
	{
		a_i32Return = 1;
	}
	m_CSSaveSys.Unlock();

	if (a_i32Return == 0)
	{
		CString cs1;
		COleDateTime tm = COleDateTime::GetCurrentTime();

		memcpy(&a_stSysSave, st, sizeof(struct SYS_DATA));

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

		m_CSSaveSys.Lock();
		m_stlSaveSys.push_back(a_stSysSave);
		m_CSSaveSys.Unlock();
	}

	return a_i32Return;
}

//进行系统 信息数据保存
INT CThreadDataAlmRec::Do_Save_Sys()
{
	list<struct SYS_DATA>::iterator i;
	INT j = 0, a_i32Flag = 0;

	m_CSSaveSys.Lock();
	if (m_stlSaveSys.size())
	{
		i = m_stlSaveSys.begin();
		a_i32Flag = 1;
	}
	m_CSSaveSys.Unlock();

	if (a_i32Flag)
	{
		//复制参数值
		memcpy(m_pSaveSysBuff, (struct SYS_DATA *)(&(*i)), sizeof(struct SYS_DATA));
		j = (sizeof(struct SYS_DATA) + 127) / 128 * 128;


		m_CSSaveSys.Lock();
		m_stlSaveSys.pop_front();
		m_CSSaveSys.Unlock();

		TRY
		{
			if ((m_i32SysFileFlag == 0))
			{
				//保存实时数据 到文件中
				m_FSaveSys.Seek(SAVE_DATA_FILE_DATA_ADD + (m_u32SaveSysFileW%CThreadDataAlmRec::m_u32SaveSysFileItemLength)*j, CFile::begin);
				m_FSaveSys.Write(m_pSaveSysBuff, j);
				m_FSaveSys.Flush();
				//m_u32SaveSysFileR = m_u32SaveSysFileW;
				m_u32SaveSysFileW++;

				if (m_u32SaveSysFileLength < CThreadDataAlmRec::m_u32SaveSysFileItemLength)
				{
					m_u32SaveSysFileLength++;
				}

				//更新文件的 读写 长度 数据
				if (m_u32SaveSysFileW % 1 == 0)
				{
					COleDateTime tm = COleDateTime::GetCurrentTime();
					CString cs1;

					cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
					if (m_i32SysFileHeadFlag == 0)
					{
						stSaveSysFileHead1.u32FileW = m_u32SaveSysFileW;
						stSaveSysFileHead1.u32Length = m_u32SaveSysFileLength;

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

						m_FSaveSys.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
						m_FSaveSys.Write((&stSaveSysFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

						m_i32SysFileHeadFlag = 1;
					}
					else
					{
						stSaveSysFileHead2.u32FileW = m_u32SaveSysFileW;
						stSaveSysFileHead2.u32Length = m_u32SaveSysFileLength;

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

						m_FSaveSys.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
						m_FSaveSys.Write((&stSaveSysFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));

						m_i32SysFileHeadFlag = 0;
					}
					m_FSaveSys.Flush();
				}
			}
		}
		CATCH(CFileException, e)
		{
			m_i32SysFileFlag = 1;
			m_FSaveSys.Abort();
		}
		END_CATCH
	}

	return 0;
}

//获得一份系统 信息数据
INT CThreadDataAlmRec::Get_Sys(INT dir)
{
	UINT a_u32Data2 = 0;

	m_CSSaveSys.Lock();
	if (m_u8GetSysFlag == GET_SYS_IDLE)
	{
		if (dir == REC_CURRENT)
		{
			if (m_u32SaveSysFileLength >= 10)
			{
				m_u32SaveSysFileR = m_u32SaveSysFileW;
				m_u32SaveSysReadNum = 10;
			}
			else
			{
				m_u32SaveSysFileR = m_u32SaveSysFileW;
				m_u32SaveSysReadNum = m_u32SaveSysFileLength;
			}
		}
		else if (dir == REC_FORWARD)
		{
			//判断前面是否还有 10条 记录           
			if (((m_u32SaveSysFileW + CThreadDataAlmRec::m_u32SaveSysFileItemLength - m_u32SaveSysFileR) % CThreadDataAlmRec::m_u32SaveSysFileItemLength) >= 10)
			{
				//前面仍然还有10条             
				//设置新的读取点               
				m_u32SaveSysFileR = (m_u32SaveSysFileR + 10) % CThreadDataAlmRec::m_u32SaveSysFileItemLength;
				m_u32SaveSysReadNum = 10;

			}
			else
			{
				//前面记录的已经不够10条        
				//设置新的读取点                
				//m_u32SaveSysReadNum = (m_u32SaveSysFileW + CThreadDataAlmRec::m_u32SaveSysFileItemLength - m_u32SaveSysFileR) % CThreadDataAlmRec::m_u32SaveSysFileItemLength;
				//m_u32SaveSysFileR = m_u32SaveSysFileW;
				if (m_u32SaveSysFileLength >= 10)
				{
					m_u32SaveSysFileR = m_u32SaveSysFileW;
					m_u32SaveSysReadNum = 10;
				}
				else
				{
					m_u32SaveSysFileR = m_u32SaveSysFileW;
					m_u32SaveSysReadNum = m_u32SaveSysFileLength;
				}
			}
		}
		else
		{
			if (m_u32SaveSysFileLength >= CThreadDataAlmRec::m_u32SaveSysFileItemLength)
			{
				//判断后面是否还有 10条 记录           
				if (((m_u32SaveSysFileR + CThreadDataAlmRec::m_u32SaveSysFileItemLength - m_u32SaveSysFileW) % CThreadDataAlmRec::m_u32SaveSysFileItemLength) >= 10)
				{
					a_u32Data2 = ((m_u32SaveSysFileR + CThreadDataAlmRec::m_u32SaveSysFileItemLength - 10) % CThreadDataAlmRec::m_u32SaveSysFileItemLength);
					//判断后面是否还有 10条 记录                
					if (((a_u32Data2 + CThreadDataAlmRec::m_u32SaveSysFileItemLength - m_u32SaveSysFileW) % CThreadDataAlmRec::m_u32SaveSysFileItemLength) >= 10)
					{
						//后面仍然还有10条             
						//设置新的读取点                            
						m_u32SaveSysFileR = ((m_u32SaveSysFileR + CThreadDataAlmRec::m_u32SaveSysFileItemLength - 10) % CThreadDataAlmRec::m_u32SaveSysFileItemLength);
						//可以读取10条                
						m_u32SaveSysReadNum = 10;
					}
					else
					{
						//后面记录的已经不够10条        
						//设置新的读取点                      
						m_u32SaveSysFileR = ((m_u32SaveSysFileW + 10) % CThreadDataAlmRec::m_u32SaveSysFileItemLength);
						//可以读取10条               
						m_u32SaveSysReadNum = 10;
					}
				}
				else
				{
					m_u32SaveSysFileR = ((m_u32SaveSysFileW + 10) % CThreadDataAlmRec::m_u32SaveSysFileItemLength);
					//可以读取10条               
					m_u32SaveSysReadNum = 10;
				}
			}
			else
			{
				//判断后面是否还有 10条 记录           
				if (m_u32SaveSysFileR >= 10)
				{
					a_u32Data2 = m_u32SaveSysFileR - 10;
					//判断后面是否还有 10条 记录                
					if (a_u32Data2 >= 10)
					{
						//后面仍然还有10条             
						//设置新的读取点                            
						m_u32SaveSysFileR = m_u32SaveSysFileR - 10;
						//可以读取10条                
						m_u32SaveSysReadNum = 10;
					}
					else
					{
						//后面记录的已经不够10条        
						//设置新的读取点                      
						m_u32SaveSysFileR = 10;
						//可以读取10条               
						m_u32SaveSysReadNum = 10;
					}
				}
				else
				{
					m_u32SaveSysFileR = m_u32SaveSysFileW;
					m_u32SaveSysReadNum = m_u32SaveSysFileLength;
				}
			}
		}

		m_u8GetSysFlag = GET_SYS_BUSY;
		m_CSSaveSys.Unlock();

		return 1;
	}
	m_CSSaveSys.Unlock();

	return 0;
}

//从系统 信息数据保存的文件中 读出一份数据
INT CThreadDataAlmRec::Do_Get_Sys()
{
	UINT8 a_u8GetSysFlag = 0;
	UINT i = 0;
	UINT a_u32SaveSysFileR;
	struct SYS_DATA a_stSysSave;

	m_CSSaveSys.Lock();
	a_u8GetSysFlag = m_u8GetSysFlag;
	m_CSSaveSys.Unlock();

	if (a_u8GetSysFlag == GET_SYS_BUSY)
	{
		m_stlGetSys.clear();
		a_u32SaveSysFileR = (m_u32SaveSysFileR + CThreadDataAlmRec::m_u32SaveSysFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveSysFileItemLength;;
		for (i = 0; (i < m_u32SaveSysReadNum) && (i < 10); i++)
		{
			m_FSaveSys.Seek(SAVE_DATA_FILE_DATA_ADD + a_u32SaveSysFileR*m_u32SaveNIBPSecLength, CFile::begin);
			m_FSaveSys.Read((char *)(&a_stSysSave), sizeof(struct SYS_DATA));
			m_stlGetSys.push_back(a_stSysSave);

			a_u32SaveSysFileR = (a_u32SaveSysFileR + CThreadDataAlmRec::m_u32SaveSysFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveSysFileItemLength;
		}

		m_CSSaveSys.Lock();
		m_u8GetSysFlag = GET_SYS_IDLE;
		m_CSSaveSys.Unlock();

		wndSaveSys->PostMessage(WM_MONITOR_STATUS, WM_REC_GET_OK, 0);
		//emit signal_Get_NIBP();
	}

	return 0;
}



INT CThreadDataAlmRec::Add_Sys_PowerON()
{
	CString cs1;
	struct SYS_DATA a_stSYS_DATA;
	COleDateTime tm = COleDateTime::GetCurrentTime();

	cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	wcscpy_s(a_stSYS_DATA.cTime, 20, cs1);
	wcscpy_s(a_stSYS_DATA.cInfo, 20, n_cPowerON);

	return Save_Sys(&a_stSYS_DATA);
}



//报警 信息 保存 初始化
INT CThreadDataAlmRec::Init_Save_Alm()
{
	INT a_i32Return = 0,i=0;

	a_i32Return = Check_Save_Alm_File();

	if ((a_i32Return == 2) || (a_i32Return == 3))
	{
		//报警 保存文件出错误
		a_i32Return = Reset_Save_Alm_File();
		//读取报警Info部分
		for (i = 0; i < CThreadDataAlmRec::m_u32SaveAlmFileItemLength; i++)
		{
			m_FSaveAlm.Seek(SAVE_ALM_FILE_INFO_ADD + i*SAVE_ALM_INFO_SECTION_LENGTH, CFile::begin);
			if (m_FSaveAlm.Read(m_pSaveAlmBuff, SAVE_ALM_INFO_SECTION_LENGTH) == SAVE_ALM_INFO_SECTION_LENGTH)
			{
				memcpy((void *)((m_stpSAVE_ALM_FILE_INFO + i)), m_pSaveAlmBuff, sizeof(struct SAVE_ALM_FILE_INFO));
			}
		}
	}

	return a_i32Return;
}

//检查报警 信息文件是否完整
INT CThreadDataAlmRec::Check_Save_Alm_File()
{
	int i = 0, flag1 = 0, flag2 = 0;
	INT a_i32Return = 0;
	struct SAVE_ALM_FILE_INFO * a_pstSAVE_ALM_FILE_INFO;
	//还需检测 Alm infonation section 
	TRY
	{
		//如果成功打开，则返回非零值，否则为0
		if (m_FSaveAlm.Open(theApp.m_csSaveAlmPFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite, 0))
		{
			i = (sizeof(struct SAVE_DATA_FILE_HEAD) + 255) / 256 * 256;
			if (m_FSaveAlm.GetLength() > 2 * SAVE_DATA_FILE_HEAD_2_ADD)
			{
				//读取文件头 1
				m_FSaveAlm.Seek(SAVE_DATA_FILE_HEAD_1_ADD, CFile::begin);
				if (m_FSaveAlm.Read(m_pSaveAlmBuff, i) == i)
				{
					memcpy((void *)(&stSaveAlmFileHead1), m_pSaveAlmBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//读取文件头 2
				m_FSaveAlm.Seek(SAVE_DATA_FILE_HEAD_2_ADD, CFile::begin);
				if (m_FSaveAlm.Read(m_pSaveAlmBuff, i) == i)
				{
					memcpy((void *)(&stSaveAlmFileHead2), m_pSaveAlmBuff, sizeof(struct SAVE_DATA_FILE_HEAD));
				}

				//判断文件头 类型是否正确
				for (i = 0; i < 8; i++)
				{
					if (stSaveAlmFileHead1.cMagic[i] != n_cMagic[i])
						flag1 = 1;
					if (stSaveAlmFileHead2.cMagic[i] != n_cMagic[i])
						flag2 = 1;
				}

				for (i = 0; i < 8; i++)
				{
					stSaveAlmFileHead1.cMagic[i] = n_cMagic[i];
					stSaveAlmFileHead2.cMagic[i] = n_cMagic[i];

					stSaveAlmFileHead1.cVersion[i] = n_cVersion[i];
					stSaveAlmFileHead2.cVersion[i] = n_cVersion[i];
				}

				//两个文件头 都正确
				if ((flag1 == 0) && (flag2 == 0))
				{
					if (stSaveAlmFileHead2.u32FileW > stSaveAlmFileHead1.u32FileW)
					{
						m_u32SaveAlmFileW = stSaveAlmFileHead2.u32FileW;
						m_u32SaveAlmFileLength = stSaveAlmFileHead2.u32Length;
					}
					else
					{
						m_u32SaveAlmFileW = stSaveAlmFileHead1.u32FileW;
						m_u32SaveAlmFileLength = stSaveAlmFileHead1.u32Length;
					}
					m_u32SaveAlmFileR = m_u32SaveAlmFileW;

					a_i32Return = 0;

				}
				//文件头1 正确 文件头 2 错误
				else if ((flag1 == 0))
				{
					m_u32SaveAlmFileW = stSaveAlmFileHead1.u32FileW;
					m_u32SaveAlmFileLength = stSaveAlmFileHead1.u32Length;
					m_u32SaveAlmFileR = m_u32SaveAlmFileW;

					a_i32Return = 1;
				}
				//文件头2 正确 文件头1 错误
				else if ((flag2 == 0))
				{
					m_u32SaveAlmFileW = stSaveAlmFileHead2.u32FileW;
					m_u32SaveAlmFileLength = stSaveAlmFileHead2.u32Length;
					m_u32SaveAlmFileR = m_u32SaveAlmFileW;

					a_i32Return = 1;
				}

				//两个文件头 都错误
				else
				{
					m_u32SaveAlmFileW = 0;
					m_u32SaveAlmFileLength = 0;
					m_u32SaveAlmFileR = m_u32SaveAlmFileW;

					//两个文件头 都错误
					a_i32Return = 2;
				}

				if ((a_i32Return == 0) || (a_i32Return == 1))
				{
					//读取报警Info部分
					for (i = 0; i < CThreadDataAlmRec::m_u32SaveAlmFileItemLength; i++)
					{
						m_FSaveAlm.Seek(SAVE_ALM_FILE_INFO_ADD + i*SAVE_ALM_INFO_SECTION_LENGTH, CFile::begin);
						if (m_FSaveAlm.Read(m_pSaveAlmBuff, sizeof(struct SAVE_ALM_FILE_INFO)) == sizeof(struct SAVE_ALM_FILE_INFO))
						{
							memcpy((void *)((m_stpSAVE_ALM_FILE_INFO + i)), m_pSaveAlmBuff, sizeof(struct SAVE_ALM_FILE_INFO));
						}
					}
					//判断下次写入的位置
					if (m_stpSAVE_ALM_FILE_INFO[m_u32SaveAlmFileW].u8Flag == 0xEE)
					{

					}
				}
			}
			else
			{

				m_u32SaveAlmFileW = 0;
				m_u32SaveAlmFileLength = 0;
				m_u32SaveAlmFileR = m_u32SaveAlmFileW;

				//文件长度错误
				a_i32Return = 3;
			}

		}
		else
		{
			m_i32AlmFileFlag = 1;
			//文件 打开 错误
			a_i32Return = 4;
		}
	}
	CATCH(CFileException, e)
	{
		m_i32SysFileFlag = 1;
		m_FSaveSys.Abort();
	}
	END_CATCH

		return a_i32Return;
}

//复位报警 信息文件
INT CThreadDataAlmRec::Reset_Save_Alm_File()
{
	int i = 0;
	struct SAVE_ALM_FILE_INFO a_stSAVE_ALM_FILE_INFO;
	COleDateTime tm = COleDateTime::GetCurrentTime();
	CString cs1;
	INT a_Flag = 0;
	cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	if (m_i32AlmFileFlag == 0)
	{
		m_u32SaveAlmFileW = 0;
		m_u32SaveAlmSecLength = 0;

		stSaveAlmFileHead1.u32FileW = m_u32SaveAlmFileW;
		stSaveAlmFileHead1.u32Length = m_u32SaveAlmSecLength;
		wcscpy_s(stSaveAlmFileHead1.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveAlmFileHead1.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		stSaveAlmFileHead2.u32FileW = m_u32SaveAlmFileW;
		stSaveAlmFileHead2.u32Length = m_u32SaveAlmSecLength;
		wcscpy_s(stSaveAlmFileHead2.cModifyFileDate, 32, cs1.GetBuffer(cs1.GetLength()));
		wcscpy_s(stSaveAlmFileHead2.cCreateFileDate, 32, cs1.GetBuffer(cs1.GetLength()));

		for (i = 0; i < 8; i++)
		{
			stSaveAlmFileHead1.cMagic[i] = n_cMagic[i];
			stSaveAlmFileHead2.cMagic[i] = n_cMagic[i];

			stSaveAlmFileHead1.cVersion[i] = n_cVersion[i];
			stSaveAlmFileHead2.cVersion[i] = n_cVersion[i];
		}

		//初始化报警保存文件 ALM INFO Section
		wcscpy_s(a_stSAVE_ALM_FILE_INFO.cTime, 20, cs1);
		a_stSAVE_ALM_FILE_INFO.u8Flag = 0xEE;
		a_stSAVE_ALM_FILE_INFO.u8Type=0x00;
		a_stSAVE_ALM_FILE_INFO.u8SubType = 0x00;
		a_stSAVE_ALM_FILE_INFO.u16FileW = 0;
		a_stSAVE_ALM_FILE_INFO.i16Threshold = 0;
		a_stSAVE_ALM_FILE_INFO.i16Result = 0;	
		TRY
		{
			//写入 报警 Info区域
			for (i = 0; i < CThreadDataAlmRec::m_u32SaveAlmFileItemLength; i++)
			{
				a_stSAVE_ALM_FILE_INFO.u16FileW = i;

				m_FSaveAlm.Seek(SAVE_ALM_FILE_INFO_ADD + i*SAVE_ALM_INFO_SECTION_LENGTH, CFile::begin);
				m_FSaveAlm.Write(&a_stSAVE_ALM_FILE_INFO, sizeof(struct SAVE_ALM_FILE_INFO));
			}

			//写入报警文件 的文件头
			m_FSaveAlm.Seek(SAVE_ALM_FILE_HEAD_1_ADD, CFile::begin);
			m_FSaveAlm.Write((&stSaveAlmFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

			m_FSaveAlm.Seek(SAVE_ALM_FILE_HEAD_2_ADD, CFile::begin);
			m_FSaveAlm.Write((&stSaveAlmFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));


			m_FSaveAlm.Flush();

		}
		CATCH(CFileException, e)
		{
			a_Flag = 1;
			m_i32AlmFileFlag = 1;
			m_FSaveAlm.Abort();
		}
		END_CATCH
	}

	return a_Flag;
}

//提交一份报警 信息数据 保存项
INT CThreadDataAlmRec::Alm_Save(struct SAVE_ALM *st)
{
	struct SAVE_ALM a_stSAVE_ALM;
	INT a_i32Return = 0;

	m_CSSaveAlm.Lock();
	if (m_stlSaveAlm.size() > 32)
	{
		a_i32Return = 1;
	}
	m_CSSaveAlm.Unlock();

	if (a_i32Return == 0)
	{
		
		//备份 报警报警数据，并且放入报警队列中
		memcpy(&(a_stSAVE_ALM.stInfo), &(st->stInfo), sizeof(struct SAVE_ALM_FILE_INFO));
		memcpy(&(a_stSAVE_ALM.stPara), &(st->stPara), sizeof(struct SAVE_DATA_PARA));
		
		a_stSAVE_ALM.pECGData[0] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
		a_stSAVE_ALM.pECGData[1] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
		//a_stSAVE_ALM.pECGData[2] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];

		a_stSAVE_ALM.pRespData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmRespWaveLength];
		a_stSAVE_ALM.pPlethData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength];

		memcpy(a_stSAVE_ALM.pECGData[0], st->pECGData[0], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
		memcpy(a_stSAVE_ALM.pECGData[1], st->pECGData[1], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
		//memcpy(a_stSAVE_ALM.pECGData[2], st->pECGData[2], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
		memcpy(a_stSAVE_ALM.pRespData, st->pRespData, CThreadDataAlmRec::m_i32SaveAlmRespWaveLength);
		memcpy(a_stSAVE_ALM.pPlethData, st->pPlethData, CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength);

		m_CSSaveAlm.Lock();
		m_stlSaveAlm.push_back(a_stSAVE_ALM);
		m_CSSaveAlm.Unlock();
	}


	return a_i32Return;
}

//进行报警 信息数据保存
INT CThreadDataAlmRec::Do_Alm_Save()
{
	list<struct SAVE_ALM>::iterator i;
	INT j = 0, a_i32Flag = 0, a_i32AlmGetFlag;

	m_CSSaveAlm.Lock();
	if (m_stlSaveAlm.size())
	{
		i = m_stlSaveAlm.begin();
		a_i32Flag = 1;
	}
	a_i32AlmGetFlag = m_i32AlmGetFlag;
	m_CSSaveAlm.Unlock();

	if (a_i32Flag)
	{
		//是否达到读取点
		if (((m_u32SaveAlmFileW + 1) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength) == m_u32SaveAlmFileSelect)
		{
			//到达读取点
			if (a_i32AlmGetFlag)
			{
				//到达读取点，并且正在读取报警记录
				//则不更新，丢去此报警
			}
			else
			{
				
				//没有在读取报警记录
				//写入报警记录
				Write_ALM((struct SAVE_ALM *)(&(*i)));
			}
		}
		else
		{
			//写入报警记录
			Write_ALM((struct SAVE_ALM *)(&(*i)));
		}

		//回收内存
		delete i->pECGData[0];
		delete i->pECGData[1];
		//delete i->pECGData[2];
		delete i->pRespData;
		delete i->pPlethData;

		m_CSSaveAlm.Lock();
		m_stlSaveAlm.pop_front();
		m_CSSaveAlm.Unlock();

	}




	if (a_i32Flag)
	{

	}


	return 0;
}

//搜索符合条件的报警项
INT CThreadDataAlmRec::Find_Alm(struct ALM_SEARCH_PARA *st)
{
	INT i = 0, j = 0, a_i32L = 0, a_i32R=0;
	
	if (m_u32SaveAlmFileLength)
	{
		a_i32R = (m_u32SaveAlmFileW + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength;

		//报警 Info 没有存满
		if (m_u32SaveAlmFileLength < CThreadDataAlmRec::m_u32SaveAlmFileItemLength)
		{
			for (i = 0; (i < m_u32SaveAlmFileLength) && (a_i32R >= 0); i++, a_i32R--)
			{
				//报警类型符合搜索条件
				if (m_stpSAVE_ALM_FILE_INFO[a_i32R].u8Type == st->u8AlmType)
				{
					if ((wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeFirst, 20) >= 0) && (wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeLast, 20) <= 0))
					{
						//报警发生的时间，符合搜索的时间条件

						j = a_i32R;

						return j;
					}
				}
				
			}
		}
		else
		{
			for (i = 0; (i < CThreadDataAlmRec::m_u32SaveAlmFileItemLength - 50) ; i++, a_i32R = (a_i32R + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - 1) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength)
			{
				//报警类型符合搜索条件
				if (m_stpSAVE_ALM_FILE_INFO[a_i32R].u8Type == st->u8AlmType)
				{
					if ((wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeFirst, 20) >= 0) && (wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeLast, 20) <= 0))
					{
						//报警发生的时间，符合搜索的时间条件
						j = a_i32R;

						break;
					}
				}
				//是否有 条件 匹配的 项
				if ((i<CThreadDataAlmRec::m_u32SaveAlmFileItemLength-50))
				{
					return j;
				}
			}
		}
	}
	
	return -1;
}

//搜索报警
INT CThreadDataAlmRec::Select_Alm(struct ALM_SEARCH_PARA *st)
{
	INT i = 0, a_i32FindIndex = 0;

	//将 m_i32AlmGetFlag 置位
	m_CSSaveAlm.Lock();
	m_i32AlmGetFlag = 1;
	m_CSSaveAlm.Unlock();

	if (m_i32AlmFileFlag==0)
	{
		//从内存中的 Info中，寻找是否有符合项
		//搜索符合条件的，第一个报警项
		a_i32FindIndex = Find_Alm(st);
		if (a_i32FindIndex>=0)
		{
			//读取第一个符合的 alm 记录 记入其位置
			m_u32SaveAlmFileR = a_i32FindIndex;
			m_u32SaveAlmFileSelect = a_i32FindIndex;
			m_u8SelectAlmFlag = 1;
			Read_Alm();
		}
		else
		{
			//没有搜索到符合条件的Alm项
			m_stGetAlm.stInfo.u8Flag = 0xAA;
			//如果没有任何符合的 返回无符合的提示 m_i32AlmGetFlag复位
			m_u8SelectAlmFlag = 0;
		}


	}

	return 0;
}

//获得一份报警 信息数据
INT CThreadDataAlmRec::Get_Alm(struct ALM_SEARCH_PARA *s_para)
{
	//如果 Select_Alm 操作找到一个符合的 alm 记录，才能进行Get_Alm操作

	m_CSSaveAlm.Lock();
	if (m_u8GetAlmFlag == GET_ALM_IDLE)
	{
		memcpy(&m_stAlmPara, s_para, sizeof(struct ALM_SEARCH_PARA));
		m_u8GetAlmFlag = GET_ALM_BUSY;
	}
	else
	{
		if (s_para->u8SearchType == GET_ALM_CLOSE_SELECT)
		{
			//关闭 搜索
			m_i32AlmSelectClose = 1;
		}
	}
	m_CSSaveAlm.Unlock();


	return 0;
}

//从报警 信息数据保存的文件中 读出一份数据
INT CThreadDataAlmRec::Do_Get_Alm()
{
	struct SYS_DATA a_stSysSave;
	UINT8 a_u8GetAlmFlag;

	m_CSSaveAlm.Lock();
	a_u8GetAlmFlag = m_u8GetAlmFlag;
	m_CSSaveAlm.Unlock();

	if (a_u8GetAlmFlag == GET_ALM_BUSY)
	{
		m_i32AlmGetFlag = 1;
		if (m_stAlmPara.u8SearchType == GET_ALM_SELECT)
		{
			//第一次 搜索符合条件的 alm 记录
			Select_Alm(&m_stAlmPara);
			if (wndSaveAlm)
			{
				if (wndSaveAlm->IsKindOf(RUNTIME_CLASS(CDialogAlmRec)))
				{
					Copy_SAVE_ALM(&(((CDialogAlmRec *)wndSaveAlm)->m_stGetAlm),
						&m_stGetAlm);
				}
				//通知 回顾 的窗口
				wndSaveAlm->PostMessage(WM_MONITOR_STATUS, WM_REC_GET_OK, 0);
			}
			
		}
		else if (m_stAlmPara.u8SearchType == GET_ALM_PRE)
		{
			if (m_u8SelectAlmFlag)
			{
				Do_Get_Alm_Pre(&m_stAlmPara);
			}
			if (wndSaveAlm)
			{
				if (wndSaveAlm->IsKindOf(RUNTIME_CLASS(CDialogAlmRec)))
				{
					Copy_SAVE_ALM(&(((CDialogAlmRec *)wndSaveAlm)->m_stGetAlm),
						&m_stGetAlm);
				}
				//通知 回顾 的窗口
				wndSaveAlm->PostMessage(WM_MONITOR_STATUS, WM_REC_GET_OK, 0);
			}
		}
		else if (m_stAlmPara.u8SearchType == GET_ALM_NEXT)
		{
			if (m_u8SelectAlmFlag)
			{
				Do_Get_Alm_Next(&m_stAlmPara);
			}
			if (wndSaveAlm)
			{
				if (wndSaveAlm->IsKindOf(RUNTIME_CLASS(CDialogAlmRec)))
				{
					Copy_SAVE_ALM(&(((CDialogAlmRec *)wndSaveAlm)->m_stGetAlm),
						&m_stGetAlm);
				}
				//通知 回顾 的窗口
				wndSaveAlm->PostMessage(WM_MONITOR_STATUS, WM_REC_GET_OK, 0);
			}
		}
		else
		{
			m_CSSaveAlm.Lock();
			if (m_i32AlmSelectClose == 1)
			{
				//关闭 搜索
				m_i32AlmGetFlag = 0;
				m_i32AlmSelectClose = 0;
			}
			m_CSSaveAlm.Unlock();
		}
		m_CSSaveAlm.Lock();
		m_u8GetAlmFlag = GET_ALM_IDLE;
		m_CSSaveAlm.Unlock();
	}
	else
	{
	}
	return 0;
}

//进行 报警保存记录数据 的拷贝
void CThreadDataAlmRec::Copy_SAVE_ALM(struct SAVE_ALM *dst, struct SAVE_ALM *sst)
{
	memcpy(&(dst->stInfo), &(sst->stInfo), sizeof(struct SAVE_ALM_FILE_INFO));
	
	memcpy(&(dst->stPara), &(sst->stPara), sizeof(struct SAVE_DATA_PARA));
	

	//复制心电波形数据
	memcpy(dst->pECGData[0], sst->pECGData[0], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
	
	//复制心电波形数据
	memcpy(dst->pECGData[1], sst->pECGData[1], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
	
	//复制心电波形数据
	//memcpy(dst->pECGData[2], m_pSaveAlmBuff + j, CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
	
	//复制呼吸波形数据
	memcpy(dst->pRespData, sst->pRespData, CThreadDataAlmRec::m_i32SaveAlmRespWaveLength);
	
	//复制血氧波形数据
	memcpy(dst->pPlethData, sst->pPlethData, CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength);

}

//获得上一项符合条件的报警项
//如果没有上一项，则返回当前项
INT CThreadDataAlmRec::Do_Get_Alm_Pre(struct ALM_SEARCH_PARA *st)
{
	INT i = 0, a_i32R = 0;

	if (((m_u32SaveAlmFileSelect + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - m_u32SaveAlmFileR) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength) <100)
	{
		a_i32R = m_u32SaveAlmFileR;
		for (; ((m_u32SaveAlmFileSelect + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - a_i32R) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength) <100;)
		{
			a_i32R = (a_i32R - 1) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength;
			if (m_stpSAVE_ALM_FILE_INFO[a_i32R].u8Type == st->u8AlmType)
			{
				if ((wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeFirst, 20) >= 0) && (wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeLast, 20) <= 0))
				{
					//报警发生的时间，符合搜索的时间条件
					m_u32SaveAlmFileR = a_i32R;
					break;
				}
			}
			//是否有 条件 匹配的 项
			if (a_i32R >= m_u32SaveAlmFileSelect)
			{
				m_u32SaveAlmFileR = a_i32R;
			}
		}		
	}
	return Read_Alm();
}

//获得下一项符合条件的报警项
//如果没有下一项，则返回当前项
INT CThreadDataAlmRec::Do_Get_Alm_Next(struct ALM_SEARCH_PARA *st)
{
	INT i = 0, a_i32R = 0;

	if (((m_u32SaveAlmFileSelect + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - m_u32SaveAlmFileR) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength) >=1)
	{
		a_i32R = m_u32SaveAlmFileR;
		for (; ((m_u32SaveAlmFileSelect + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - a_i32R) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength) >= 1;)
		{
			a_i32R = (a_i32R + 1) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength;
			if (m_stpSAVE_ALM_FILE_INFO[a_i32R].u8Type == st->u8AlmType)
			{
				if ((wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeFirst, 20) >= 0) && (wcsncmp(m_stpSAVE_ALM_FILE_INFO[a_i32R].cTime, st->cTimeLast, 20) <= 0))
				{
					//报警发生的时间，符合搜索的时间条件
					m_u32SaveAlmFileR = a_i32R;
					break;
				}
			}
			//是否有 条件 匹配的 项
			if (((a_i32R + CThreadDataAlmRec::m_u32SaveAlmFileItemLength - m_u32SaveAlmFileSelect) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength) < 150)
			{
				m_u32SaveAlmFileR = a_i32R;
			}
		}
	}

	return Read_Alm();
}

//读取已经记录的报警 信息
INT CThreadDataAlmRec::Read_Alm()
{
	INT j = 0, a_flag = 0;

	if (m_i32AlmFileFlag==0)
	{
		TRY
		{
			m_FSaveAlm.Seek(SAVE_ALM_FILE_DATA_ADD + (m_u32SaveAlmFileR)* SAVE_ALM_FILE_DATA_LENGTH,CFile::begin);
			if (m_FSaveAlm.Read(m_pSaveAlmBuff, SAVE_ALM_FILE_DATA_LENGTH) == SAVE_ALM_FILE_DATA_LENGTH)
			{
				memcpy(&(m_stGetAlm.stInfo), m_pSaveAlmBuff+j, sizeof(struct SAVE_ALM_FILE_INFO));
				j = (sizeof(struct SAVE_ALM_FILE_INFO) + 127) / 128 * 128;

				memcpy(&(m_stGetAlm.stPara), m_pSaveAlmBuff+j, sizeof(struct SAVE_DATA_PARA));
				j += (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128;


				//复制心电波形数据
				memcpy(m_stGetAlm.pECGData[0], m_pSaveAlmBuff + j, CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
				j += CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2;
				//复制心电波形数据
				memcpy(m_stGetAlm.pECGData[1], m_pSaveAlmBuff + j, CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
				j += CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2;
				//复制心电波形数据
				//memcpy(m_stGetAlm.pECGData[2], m_pSaveAlmBuff + j, CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
				//j += CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2;

				//复制呼吸波形数据
				memcpy(m_stGetAlm.pRespData, m_pSaveAlmBuff + j, CThreadDataAlmRec::m_i32SaveAlmRespWaveLength);
				j += CThreadDataAlmRec::m_i32SaveAlmRespWaveLength;

				//复制血氧波形数据
				memcpy(m_stGetAlm.pPlethData, m_pSaveAlmBuff + j, CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength);
				j += CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength;

				a_flag = 0;

			}
			else
			{
				a_flag = 1;
			}
		}
		CATCH(CFileException, e)
		{
			a_flag = 1;
			m_i32AlmFileFlag = 1;
			m_FSaveAlm.Abort();
		}
		END_CATCH
	}

	return a_flag;
}

//写入报警，更新报警信息记录
INT CThreadDataAlmRec::Write_ALM(struct SAVE_ALM *st)
{
	struct SAVE_ALM_FILE_INFO a_stSAVE_ALM_FILE_INFO;
	INT j = 0,i=0;

	TRY
	{
		memcpy(&a_stSAVE_ALM_FILE_INFO, &(st->stInfo), sizeof(struct SAVE_ALM_FILE_INFO));

		//alm info 特殊标记
		a_stSAVE_ALM_FILE_INFO.u16FileW = m_u32SaveAlmFileW;
		a_stSAVE_ALM_FILE_INFO.u8Flag = 0xEE;
		//内存中 ALM Info 更新
		memcpy((m_stpSAVE_ALM_FILE_INFO + m_u32SaveAlmFileW), &a_stSAVE_ALM_FILE_INFO, sizeof(struct SAVE_ALM_FILE_INFO));
		//flash中更新
		memcpy(m_pSaveAlmBuff, &a_stSAVE_ALM_FILE_INFO, sizeof(struct SAVE_ALM_FILE_INFO));
		//移动alm info 指针
		m_FSaveAlm.Seek(SAVE_ALM_FILE_INFO_ADD + (m_u32SaveAlmFileW%CThreadDataAlmRec::m_u32SaveAlmFileItemLength)*m_u32SaveAlmInfoLength, CFile::begin);
		m_FSaveAlm.Write(m_pSaveAlmBuff, m_u32SaveAlmInfoLength);
		m_FSaveAlm.Flush();


		//write data flush
		memcpy(m_pSaveAlmBuff, &(st->stInfo), sizeof(struct SAVE_ALM_FILE_INFO));
		j = (sizeof(struct SAVE_ALM_FILE_INFO) + 127) / 128 * 128;

		memcpy(m_pSaveAlmBuff+j, &(st->stPara), sizeof(struct SAVE_DATA_PARA));
		j += (sizeof(struct SAVE_DATA_PARA) + 127) / 128 * 128;

		//复制心电波形数据
		memcpy(m_pSaveAlmBuff + j, st->pECGData[0], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
		j += CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2;
		//复制心电波形数据
		memcpy(m_pSaveAlmBuff + j, st->pECGData[1], CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2);
		j += CThreadDataAlmRec::m_i32SaveAlmECGWaveLength * 2;
		
		//复制呼吸波形数据
		memcpy(m_pSaveAlmBuff + j, st->pRespData, CThreadDataAlmRec::m_i32SaveAlmRespWaveLength);
		j += CThreadDataAlmRec::m_i32SaveAlmRespWaveLength;

		//复制血氧波形数据
		memcpy(m_pSaveAlmBuff + j, st->pPlethData, CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength);
		j += CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength;
		//移动alm info 指针
		INT ff = SAVE_ALM_FILE_DATA_ADD + (m_u32SaveAlmFileW)*SAVE_ALM_FILE_DATA_LENGTH;
		m_FSaveAlm.Seek(SAVE_ALM_FILE_DATA_ADD + (m_u32SaveAlmFileW)*SAVE_ALM_FILE_DATA_LENGTH, CFile::begin);
		m_FSaveAlm.Write(m_pSaveAlmBuff, SAVE_ALM_FILE_DATA_LENGTH);
		m_FSaveAlm.Flush();

		//update file write index length
		m_u32SaveAlmFileW = (m_u32SaveAlmFileW + 1) % CThreadDataAlmRec::m_u32SaveAlmFileItemLength;

		if (m_u32SaveAlmFileLength < CThreadDataAlmRec::m_u32SaveAlmFileItemLength)
		{
			m_u32SaveAlmFileLength++;
		}

		for (i = 0; i < 8; i++)
		{
			stSaveAlmFileHead1.cMagic[i] = n_cMagic[i];
			stSaveAlmFileHead2.cMagic[i] = n_cMagic[i];

			stSaveAlmFileHead1.cVersion[i] = n_cVersion[i];
			stSaveAlmFileHead2.cVersion[i] = n_cVersion[i];
		}

		//更新文件的 读写 长度 数据
		if (m_u32SaveAlmFileW % 1 == 0)
		{
			COleDateTime tm = COleDateTime::GetCurrentTime();
			CString cs1;

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

				m_FSaveAlm.Seek(SAVE_ALM_FILE_HEAD_1_ADD, CFile::begin);
				m_FSaveAlm.Write((&stSaveAlmFileHead1), sizeof(struct SAVE_DATA_FILE_HEAD));

				m_i32AlmFileHeadFlag = 1;
			}
			else
			{
				stSaveAlmFileHead2.u32FileW = m_u32SaveAlmFileW;
				stSaveAlmFileHead2.u32Length = m_u32SaveAlmFileLength;

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

				m_FSaveAlm.Seek(SAVE_ALM_FILE_HEAD_2_ADD, CFile::begin);
				m_FSaveAlm.Write((&stSaveAlmFileHead2), sizeof(struct SAVE_DATA_FILE_HEAD));
				m_i32AlmFileHeadFlag = 0;
			}
			m_FSaveAlm.Flush();
		}


		// update alm info
		a_stSAVE_ALM_FILE_INFO.u8Flag = 0x00;
		memcpy(m_pSaveAlmBuff, &a_stSAVE_ALM_FILE_INFO, sizeof(struct SAVE_ALM_FILE_INFO));
		//移动alm info 指针
		m_FSaveAlm.Seek(SAVE_ALM_FILE_INFO_ADD + (a_stSAVE_ALM_FILE_INFO.u16FileW)*m_u32SaveAlmInfoLength, CFile::begin);
		m_FSaveAlm.Write(m_pSaveAlmBuff, m_u32SaveAlmInfoLength);
		m_FSaveAlm.Flush();

		//内存中 ALM Info 更新
		memcpy((m_stpSAVE_ALM_FILE_INFO + a_stSAVE_ALM_FILE_INFO.u16FileW), &a_stSAVE_ALM_FILE_INFO, sizeof(struct SAVE_ALM_FILE_INFO));


	}
	CATCH(CFileException, e)
	{
		m_i32AlmFileFlag = 1;
		m_FSaveAlm.Abort();
	}
	END_CATCH

	return 0;
}

int CThreadDataAlmRec::Run()
{

	while (1)
	{
		Do_Get_NIBP();
		Do_Save_NIBP();
		//进行系统 信息数据保存
		Do_Save_Sys();
		//从系统 信息数据保存的文件中 读出一份数据
		Do_Get_Sys();
		//进行实时数据保存
		Do_Save();
		//从实时数据保存的文件中 读出一份数据
		Do_Get();
		//进行报警 信息数据保存
		Do_Alm_Save();
		//从报警 信息数据保存的文件中 读出一份数据
		Do_Get_Alm();
		if (m_i32Exit)
		{
			return ExitInstance();
		}
		Sleep(40);
	}
	return 0;
}