// ReadSensorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ReadSensor.h"
#include "ReadSensorDlg.h"
#include "afxdialogex.h"
#include "global.h"
#include "ComHandle.h"
#include <string>
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CReadSensorDlg dialog

ComHandle g_com;
float g_pressure;
float g_tem;
int g_readtype;


CReadSensorDlg::CReadSensorDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CReadSensorDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReadSensorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RICHEDIT21, m_richedit);
	DDX_Control(pDX, IDC_COMBO1, CB_Port);
	DDX_Control(pDX, IDC_EDIT1, ED_PRESSURE);
	DDX_Control(pDX, IDC_EDIT2, ED_TEM);
	DDX_Control(pDX, IDC_CHECK1, BTN_Loop);
	DDX_Control(pDX, IDC_EDIT3, ED_LoopTime);
}

BEGIN_MESSAGE_MAP(CReadSensorDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPENCOMM, &CReadSensorDlg::OnBnClickedOpencomm)
	ON_BN_CLICKED(IDC_READ_PRESSURE, &CReadSensorDlg::OnBnClickedReadPressure)
	ON_BN_CLICKED(IDC_READ_TEM, &CReadSensorDlg::OnBnClickedReadTem)
	ON_BN_CLICKED(IDC_READ_ALL, &CReadSensorDlg::OnBnClickedReadAll)
	ON_BN_CLICKED(IDC_CHECK1, &CReadSensorDlg::OnBnClickedCheck1)
	ON_MESSAGE(WM_DATARECEIVED, OnMyMessage)
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_EDIT3, &CReadSensorDlg::OnEnChangeEdit3)
	ON_BN_CLICKED(IDC_CLEAR, &CReadSensorDlg::OnBnClickedClear)
	ON_BN_CLICKED(IDC_Reflash, &CReadSensorDlg::OnBnClickedReflash)
END_MESSAGE_MAP()


// CReadSensorDlg message handlers

BOOL CReadSensorDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here


	g_com.linkRichEdit(&m_richedit);



	std::string strport = getConfig("port");




	CString strCom[255];
	CString strTemp;
	CString strPort;

	HANDLE hCom;
	int index = 0;
	for (int i = 0; i<255; i++)
	{
		strTemp.Format("\\\\.\\COM%d", i + 1);
		strPort.Format("%d", i + 1);
		hCom = CreateFile(strTemp, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
			OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
		if (INVALID_HANDLE_VALUE == hCom)
			continue;


		strCom[i] = strTemp;
		CB_Port.AddString(strPort);

		//if (string(strPort.GetString()) == strport)
		//{
		//	CB_Port.SetCurSel(index);

		//}

		

		index++;

		CloseHandle(hCom);
	}

	for (int i = 0; i < CB_Port.GetCount(); i++)
	{
		CString str;
		CB_Port.GetLBText(i, str);
		if (string(str.GetString()) == strport)
		{
			CB_Port.SetCurSel(i);
		}
	}

	BTN_Loop.SetCheck(getConfig_INT("Loop"));


	ED_LoopTime.SetWindowText(getConfig("LoopTime").c_str());


	if (BTN_Loop.GetCheck())
	{
		CString str;
		ED_LoopTime.GetWindowText(str);
		int time = atoi(str);

		if (time > 0)
		{
			SetTimer(1, time, NULL);
		}
	}





	g_readtype = getConfig_INT("ReadType");


	OnBnClickedOpencomm();



	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CReadSensorDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CReadSensorDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CReadSensorDlg::OnBnClickedOpencomm()
{
	int portno = 0;

	CString strport;
	CB_Port.GetWindowText(strport);

	portno = atoi(strport.GetString());

	g_com.opencom(portno);

	if (!strport.IsEmpty())
	{
		saveConfig("port", strport.GetString());
	}
}


void CReadSensorDlg::OnBnClickedReadPressure()
{
	ED_PRESSURE.SetWindowText("");
	ED_TEM.SetWindowText("");


	g_readtype = 0;
	saveConfig("ReadType", g_readtype);

	BYTE buf[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x38};
	g_com.senddata(buf, _countof(buf));
}


void CReadSensorDlg::OnBnClickedReadTem()
{
	ED_PRESSURE.SetWindowText("");
	ED_TEM.SetWindowText("");


	g_readtype = 1;
	saveConfig("ReadType", g_readtype);
	BYTE buf[] = { 0x02, 0x03, 0x00, 0x04, 0x00, 0x02, 0x85, 0xF9 };
	g_com.senddata(buf, _countof(buf));
}


void CReadSensorDlg::OnBnClickedReadAll()
{
	ED_PRESSURE.SetWindowText("");
	ED_TEM.SetWindowText("");

	g_readtype = 2;
	saveConfig("ReadType", g_readtype);
	BYTE buf[] = { 0x02, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x3A };
	g_com.senddata(buf, _countof(buf));

}


void CReadSensorDlg::OnBnClickedCheck1()
{

	saveConfig("Loop", BTN_Loop.GetCheck());

	if (BTN_Loop.GetCheck())
	{
		ED_LoopTime.EnableWindow(FALSE);

		CString str;
		ED_LoopTime.GetWindowText(str);
		int time = atoi(str);

		if (time > 0)
		{
			SetTimer(1, time, NULL);
		}
	}
	else
	{
		ED_LoopTime.EnableWindow(TRUE);
		KillTimer(1);
	}

}

LRESULT CReadSensorDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
	char pre[10] = { 0 };
	char tem[10] = { 0 };
	if (g_readtype == 0)
	{
		sprintf_s(pre, "%.4f", g_pressure);
		ED_PRESSURE.SetWindowText(pre);
	}
	else if (g_readtype == 1)
	{
		ED_TEM.SetWindowText(to_string(g_tem).c_str());
	}
	else if (g_readtype == 2)
	{
		ED_PRESSURE.SetWindowText(to_string(g_pressure).c_str());
		ED_TEM.SetWindowText(to_string(g_tem).c_str());
	}


	return 1;
}

void CReadSensorDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default

	if (g_readtype == 0)
	{
		OnBnClickedReadPressure();
	}
	else if (g_readtype == 1)
	{
		OnBnClickedReadTem();
	}
	else if (g_readtype == 2)
	{
		OnBnClickedReadAll();
	}





	CDialogEx::OnTimer(nIDEvent);
}


void CReadSensorDlg::OnEnChangeEdit3()
{

	CString str;
	ED_LoopTime.GetWindowText(str);
	int time = atoi(str);

	if (time > 0)
	{
		saveConfig("LoopTime", time);
	}

	if (BTN_Loop.GetCheck())
	{

		if (time > 0)
		{
			SetTimer(1, time, NULL);
		}
	}
}


void CReadSensorDlg::OnBnClickedClear()
{
	ED_PRESSURE.SetWindowText("");
	ED_TEM.SetWindowText("");


	g_readtype = 10;
	//saveConfig("ReadType", g_readtype);
	BYTE buf[] = { 0x02, 0x06, 0x00, 0x00, 0x00, 0x00, 0x89, 0xF9 };
	g_com.senddata(buf, _countof(buf));
}


void CReadSensorDlg::OnBnClickedReflash()
{
	CB_Port.ResetContent();
	g_com.closecom();

	std::string strport = getConfig("port");


	CString strCom[255];
	CString strTemp;
	CString strPort;

	HANDLE hCom;
	int index = 0;
	for (int i = 0; i<255; i++)
	{
		strTemp.Format("\\\\.\\COM%d", i + 1);
		strPort.Format("%d", i + 1);
		hCom = CreateFile(strTemp, GENERIC_READ | GENERIC_WRITE, 0, NULL,
			OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
		if (INVALID_HANDLE_VALUE == hCom)
			continue;


		strCom[i] = strTemp;
		CB_Port.AddString(strPort);

		//if (string(strPort.GetString()) == strport)
		//{
		//	CB_Port.SetCurSel(index);
		//	
		//}



		index++;

		CloseHandle(hCom);
	}

	for (int i = 0; i < CB_Port.GetCount(); i++)
	{
		CString str;
		CB_Port.GetLBText(i, str);
		if (string(str.GetString()) == strport)
		{
			CB_Port.SetCurSel(i);
		}
	}

	OnBnClickedOpencomm();
}