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

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

// CDialogAlmRec 对话框

IMPLEMENT_DYNAMIC(CDialogAlmRec, CDialogEx)

CDialogAlmRec::CDialogAlmRec(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDialogAlmRec::IDD, pParent)
{
	m_stGetAlm.pECGData[0] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
	m_stGetAlm.pECGData[1] = new UINT16[CThreadDataAlmRec::m_i32SaveAlmECGWaveLength];
	
	m_stGetAlm.pPlethData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmPlethWaveLength];
	m_stGetAlm.pRespData = new UINT8[CThreadDataAlmRec::m_i32SaveAlmRespWaveLength];
}

CDialogAlmRec::~CDialogAlmRec()
{
	delete m_stGetAlm.pECGData[0];
	delete m_stGetAlm.pECGData[1];
	
	delete m_stGetAlm.pPlethData;
	delete m_stGetAlm.pRespData;
}

void CDialogAlmRec::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_NAME, m_CStaticName);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_TIME, m_CStaticTime);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_ALM_INFO, m_CStaticAlmInfo);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_HR, m_CStaticHR);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_ST1, m_CStaticST1);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_ST2, m_CStaticST2);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_RR, m_CStaticRR);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_SPO2, m_CStaticSpo2);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_PR, m_CStaticPR);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_TEMP, m_CStaticTemp);
	DDX_Control(pDX, IDC_STATIC_ALM_REC_AIROXY, m_CStaticAirOxy);
}


BOOL CDialogAlmRec::OnInitDialog()
{
	CString a_cs;
	CDialogEx::OnInitDialog();


	font.CreateFont(16,                                          //   nHeight     
		0,                                                   //   nWidth     
		0,                                                   //   nEscapement   
		0,                                                   //   nOrientation     
		FW_NORMAL,                                   //   nWeight     
		TRUE,                                           //   bItalic     
		FALSE,                                           //   bUnderline     
		0,                                                   //   cStrikeOut     
		ANSI_CHARSET,                             //   nCharSet     
		OUT_DEFAULT_PRECIS,                 //   nOutPrecision     
		CLIP_DEFAULT_PRECIS,               //   nClipPrecision     
		DEFAULT_QUALITY,                       //   nQuality     
		DEFAULT_PITCH | FF_SWISS,     //   nPitchAndFamily       
		_T("宋体"));


	m_CStaticName.SetFont(&font);
	m_CStaticTime.SetFont(&font);
	m_CStaticAlmInfo.SetFont(&font);
	m_CStaticHR.SetFont(&font);
	m_CStaticST1.SetFont(&font);
	m_CStaticST2.SetFont(&font);
	m_CStaticRR.SetFont(&font);
	m_CStaticSpo2.SetFont(&font);
	m_CStaticPR.SetFont(&font);
	m_CStaticTemp.SetFont(&font);


	/*m_stGetAlm.pECGData[0] = new UINT16[1000];
	m_stGetAlm.pECGData[1] = new UINT16[1000];
	m_stGetAlm.pECGData[2] = new UINT16[1000];
	m_stGetAlm.pRespData = new UINT8[250];
	m_stGetAlm.pPlethData = new UINT8[250];*/

	m_pCAlmRecWave = new CAlmRecWave;
	m_pCAlmRecWave->Create(NULL, WS_CHILD | WS_VISIBLE | SS_NOTIFY, CRect(5, 125, CAlmRecWave::m_i32Width + 5, CAlmRecWave::m_i32Height + 125), this, 123);
	m_pCAlmRecWave->ShowWindow(SW_SHOW);
	m_pCAlmRecWave->Draw_Wave();
	m_pCAlmRecWave->Invalidate(TRUE);

	m_stALM_SEARCH_PARA.u8SearchType = GET_ALM_SELECT;
	((CThreadDataAlmRec *)(theApp.m_pThrdDataSave))->wndSaveAlm = this;
	((CThreadDataAlmRec *)(theApp.m_pThrdDataSave))->Get_Alm(&m_stALM_SEARCH_PARA);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常:  OCX 属性页应返回 FALSE
}

BEGIN_MESSAGE_MAP(CDialogAlmRec, CDialogEx)
	ON_MESSAGE(WM_MONITOR_STATUS, OnMonitorStatus)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDOK, &CDialogAlmRec::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_ALM_REC_LEFT, &CDialogAlmRec::OnBnClickedButtonAlmRecLeft)
	ON_BN_CLICKED(IDC_BUTTON_ALM_REC_RIGHT, &CDialogAlmRec::OnBnClickedButtonAlmRecRight)
	ON_BN_CLICKED(IDC_BUTTON_ALM_REC_PRE, &CDialogAlmRec::OnBnClickedButtonAlmRecPre)
	ON_BN_CLICKED(IDC_BUTTON_ALM_REC_NEXT, &CDialogAlmRec::OnBnClickedButtonAlmRecNext)
	ON_BN_CLICKED(IDC_BUTTON_ALM_REC_CHANGE, &CDialogAlmRec::OnBnClickedButtonAlmRecChange)
END_MESSAGE_MAP()


// CDialogAlmRec 消息处理程序


LRESULT  CDialogAlmRec::OnMonitorStatus(WPARAM wPara, LPARAM lPara)
{
	switch (wPara)
	{
	case WM_REC_GET_OK:
		Show_Result();

		break;
	}

	return 0;
}

void CDialogAlmRec::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO:  在此处添加消息处理程序代码
	m_pCAlmRecWave->DestroyWindow();
	delete m_pCAlmRecWave;
	font.DeleteObject();
	/*delete m_stGetAlm.pECGData[0];
	delete m_stGetAlm.pECGData[1];
	delete m_stGetAlm.pECGData[2];
	delete m_stGetAlm.pRespData;
	delete m_stGetAlm.pPlethData;*/
}

void CDialogAlmRec::OnBnClickedOk()
{
	// TODO:  在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}


void CDialogAlmRec::OnBnClickedButtonAlmRecLeft()
{
	// TODO:  在此添加控件通知处理程序代码
	m_pCAlmRecWave->Move_Draw_Section(-1);
	m_pCAlmRecWave->Draw_Wave();
}

void CDialogAlmRec::OnBnClickedButtonAlmRecRight()
{
	// TODO:  在此添加控件通知处理程序代码
	m_pCAlmRecWave->Move_Draw_Section(1);
	m_pCAlmRecWave->Draw_Wave();
}


void CDialogAlmRec::OnBnClickedButtonAlmRecPre()
{
	// TODO:  在此添加控件通知处理程序代码
	m_stALM_SEARCH_PARA.u8SearchType = GET_ALM_PRE;
	((CThreadDataAlmRec *)(theApp.m_pThrdDataSave))->wndSaveAlm = this;
	((CThreadDataAlmRec *)(theApp.m_pThrdDataSave))->Get_Alm(&m_stALM_SEARCH_PARA);
}


void CDialogAlmRec::OnBnClickedButtonAlmRecNext()
{
	// TODO:  在此添加控件通知处理程序代码
	m_stALM_SEARCH_PARA.u8SearchType = GET_ALM_NEXT;
	((CThreadDataAlmRec *)(theApp.m_pThrdDataSave))->wndSaveAlm = this;
	((CThreadDataAlmRec *)(theApp.m_pThrdDataSave))->Get_Alm(&m_stALM_SEARCH_PARA);
}


void CDialogAlmRec::OnBnClickedButtonAlmRecChange()
{
	// TODO:  在此添加控件通知处理程序代码
	m_pCAlmRecWave->show_change();
	m_pCAlmRecWave->Draw_Wave();
}


void CDialogAlmRec::Show_Result()
{
	CString a_cs;
	float a_fTemp = 0.0f;


	if (m_stGetAlm.stInfo.u8Flag == 0xAA)
	{
		a_cs = _T("---");
		m_CStaticName.SetWindowTextW(a_cs);
		m_CStaticTime.SetWindowTextW(a_cs);

		m_CStaticHR.SetWindowTextW(a_cs);

		m_CStaticST1.SetWindowTextW(a_cs);

		m_CStaticST2.SetWindowTextW(a_cs);

		m_CStaticRR.SetWindowTextW(a_cs);

		m_CStaticSpo2.SetWindowTextW(a_cs);

		m_CStaticPR.SetWindowTextW(a_cs);

		m_CStaticTemp.SetWindowTextW(a_cs);

		m_CStaticAirOxy.SetWindowTextW(a_cs);

		m_CStaticAlmInfo.SetWindowTextW(_T("无相关报警项"));
	}
	else
	{
		m_CStaticName.SetWindowTextW(m_stGetAlm.stPara.cName);
		m_CStaticTime.SetWindowTextW(m_stGetAlm.stPara.cTime);


		if (m_stGetAlm.stPara.i16HR == CData::m_i16HRInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_cs.Format(_T("%d bpm"), m_stGetAlm.stPara.i16HR);
		}
		m_CStaticHR.SetWindowTextW(a_cs);


		if (m_stGetAlm.stPara.i16ST1 == CData::m_i16STInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_fTemp = m_stGetAlm.stPara.i16ST1;
			a_fTemp = a_fTemp / 100.0f;
			a_cs.Format(_T("%0.2f "), a_fTemp);
		}
		m_CStaticST1.SetWindowTextW(a_cs);


		if (m_stGetAlm.stPara.i16ST2 == CData::m_i16STInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_fTemp = m_stGetAlm.stPara.i16ST2;
			a_fTemp = a_fTemp / 100.0f;
			a_cs.Format(_T("%0.2f "), a_fTemp);
		}
		m_CStaticST2.SetWindowTextW(a_cs);


		if (m_stGetAlm.stPara.i16RR == CData::m_i16RRInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_cs.Format(_T("%d rpm"), m_stGetAlm.stPara.i16RR);
		}
		m_CStaticRR.SetWindowTextW(a_cs);

		if (m_stGetAlm.stPara.i16Spo2 == CData::m_i16Spo2InvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_cs.Format(_T("%d %s"), m_stGetAlm.stPara.i16Spo2, _T("%"));
		}
		m_CStaticSpo2.SetWindowTextW(a_cs);

		if (m_stGetAlm.stPara.i16PR == CData::m_i16PRInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_cs.Format(_T("%d bpm"), m_stGetAlm.stPara.i16PR);
		}
		m_CStaticPR.SetWindowTextW(a_cs);


		if (m_stGetAlm.stPara.i16Temp == CData::m_i16TempInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_fTemp = m_stGetAlm.stPara.i16Temp;
			a_fTemp = a_fTemp / 10.0f;
			a_cs.Format(_T("%0.1f C"), a_fTemp);
		}
		m_CStaticTemp.SetWindowTextW(a_cs);

		if (m_stGetAlm.stPara.i8AirOxy == CData::m_i16AirOxyInvalidValue)
		{
			a_cs = _T("未测出");
		}
		else
		{
			a_cs.Format(_T("%d %s"), m_stGetAlm.stPara.i8AirOxy, _T("%"));
		}
		m_CStaticAirOxy.SetWindowTextW(a_cs);
		Show_Alm_Info();
	}
	

	m_pCAlmRecWave->copy_data(&m_stGetAlm);
	m_pCAlmRecWave->Draw_Wave();
}

//显示报警提示信息
void CDialogAlmRec::Show_Alm_Info()
{
	CString a_cs;
	float a_f1 = 0.0f, a_f2 = 0.0f;
	switch (m_stGetAlm.stInfo.u8Type)
	{
	case ALM_TYPE_ECG:
		if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_HR_L)
		{
			a_cs.Format(_T("心律过低 当前值为 %d bpm 报警阈值为 %d bpm"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		} 
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_HR_H)
		{
			a_cs.Format(_T("心律过高 当前值为 %d bpm 报警阈值为 %d bpm"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		break;

	case ALM_TYPE_RESP:
		if (m_stGetAlm.stInfo.u8SubType == ALM_RESP_RR_L)
		{
			a_cs.Format(_T("呼吸率过低 当前值为 %d rpm 报警阈值为 %d rpm"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_RESP_RR_H)
		{
			a_cs.Format(_T("呼吸率过高 当前值为 %d rpm 报警阈值为 %d rpm"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_RESP_APNEA)
		{
			a_cs.Format(_T("呼吸窒息"));
		}
		break;

	case ALM_TYPE_OXY:
		if (m_stGetAlm.stInfo.u8SubType == ALM_OXY_OXY_L)
		{
			a_cs.Format(_T("血氧饱和度过低 当前值为 %d %s 报警阈值为 %d %s"), m_stGetAlm.stInfo.i16Result, _T("%"), m_stGetAlm.stInfo.i16Threshold, _T("%"));
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_OXY_OXY_H)
		{
			a_cs.Format(_T("血氧饱和度过高 当前值为 %d %s 报警阈值为 %d %s"), m_stGetAlm.stInfo.i16Result, _T("%"), m_stGetAlm.stInfo.i16Threshold, _T("%"));
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_OXY_PR_L)
		{
			a_cs.Format(_T("脉率过低 当前值为 %d bpm 报警阈值为 %d bpm"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_OXY_PR_H)
		{
			a_cs.Format(_T("脉率过高 当前值为 %d bpm 报警阈值为 %d bpm"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		break;

	case ALM_TYPE_TEMP:
		a_f1 = m_stGetAlm.stInfo.i16Result;
		a_f1 = a_f1 / 10.0f;
		if (m_stGetAlm.stInfo.u8SubType == ALM_TEMP_L)
		{
			a_f2 = m_stGetAlm.stInfo.i16Threshold;
			a_f2 = a_f2 / 10.0f;
			a_cs.Format(_T("体温过低 当前值为 %0.1f ℃ 报警阈值为 %0.1f ℃"), a_f1, a_f2);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_TEMP_H)
		{
			a_f2 = m_stGetAlm.stInfo.i16Threshold;
			a_f2 = a_f2 / 10.0f;
			a_cs.Format(_T("体温过高 当前值为 %0.1f ℃ 报警阈值为 %0.1f ℃"), a_f1, a_f2);
		}
		break;

	case ALM_TYPE_NIBP:
		if (m_stGetAlm.stInfo.u8SubType == ALM_NIBP_SBP_L)
		{
			a_cs.Format(_T("收缩压过低 当前值为 %d mmHg 报警阈值为 %d mmHg"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_NIBP_SBP_H)
		{
			a_cs.Format(_T("收缩压过高 当前值为 %d mmHg 报警阈值为 %d mmHg"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_NIBP_DBP_L)
		{
			a_cs.Format(_T("舒张压过低 当前值为 %d mmHg 报警阈值为 %d mmHg"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_NIBP_DBP_H)
		{
			a_cs.Format(_T("舒张压过高 当前值为 %d mmHg 报警阈值为 %d mmHg"), m_stGetAlm.stInfo.i16Result, m_stGetAlm.stInfo.i16Threshold);
		}
		
		break;

	case ALM_TYPE_AIROXY:
		if (m_stGetAlm.stInfo.u8SubType == ALM_AIROXY_OXY_L)
		{
			a_cs.Format(_T("氧浓度过低 当前值为 %d %s 报警阈值为 %d %s"), m_stGetAlm.stInfo.i16Result, _T("%"), m_stGetAlm.stInfo.i16Threshold, _T("%"));
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_AIROXY_OXY_H)
		{
			a_cs.Format(_T("氧浓度过高 当前值为 %d %s 报警阈值为 %d %s"), m_stGetAlm.stInfo.i16Result, _T("%"), m_stGetAlm.stInfo.i16Threshold, _T("%"));
		}
		break;

	case ALM_TYPE_ST:

		a_f1 = m_stGetAlm.stInfo.i16Result;
		a_f1 = a_f1 / 100.0f;
		if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_ST1_L)
		{
			a_f2 = m_stGetAlm.stInfo.i16Threshold;
			a_f2 = a_f2 / 100.0f;
			a_cs.Format(_T("ST1过低 当前值为 %0.2f 报警阈值为 %0.2f"), a_f1, a_f2);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_ST1_H)
		{
			a_f2 = m_stGetAlm.stInfo.i16Threshold;
			a_f2 = a_f2 / 100.0f;
			a_cs.Format(_T("ST1过高 当前值为 %0.2f 报警阈值为 %0.2f"), a_f1, a_f2);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_ST2_L)
		{
			a_f2 = m_stGetAlm.stInfo.i16Threshold;
			a_f2 = a_f2 / 100.0f;
			a_cs.Format(_T("ST2过低 当前值为 %0.2f 报警阈值为 %0.2f"), a_f1, a_f2);
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_ST2_H)
		{
			a_f2 = m_stGetAlm.stInfo.i16Threshold;
			a_f2 = a_f2 / 100.0f;
			a_cs.Format(_T("ST1过高 当前值为 %0.2f 报警阈值为 %0.2f"), a_f1, a_f2);
		}
		
		break;

	case ALM_TYPE_ARRA:
		if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_BIGEMINY)
		{
			a_cs = _T("二连律");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_TRIGEMINY)
		{
			a_cs = _T("三连律");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_SLOW)
		{
			a_cs = _T("心动过缓");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_FAST)
		{
			a_cs = _T("心动过速");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_RONT)
		{
			a_cs = _T("室早");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_RVONT)
		{
			a_cs = _T("室速");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_PAIR)
		{
			a_cs = _T("室早成对");
		}
		else if (m_stGetAlm.stInfo.u8SubType == ALM_ECG_ASYSTOLE)
		{
			a_cs = _T("停搏");
		}
		break;

	default:
		break;
	}

	m_CStaticAlmInfo.SetWindowTextW(a_cs);
}


void CDialogAlmRec::Copy_Alm_Search(struct ALM_SEARCH_PARA *st)
{

	memcpy(&m_stALM_SEARCH_PARA, st, sizeof(struct ALM_SEARCH_PARA));
}