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

#include "stdafx.h"
#include "R2kDemo.h"
#include "ParaSet.h"


// CParaSet 对话框

IMPLEMENT_DYNAMIC(CParaSet, CPropertyPage)

CParaSet::CParaSet()
	: CPropertyPage(CParaSet::IDD)
	, m_deviceNo(_T(""))
	, m_neighJudge(_T(""))
	, m_accesPwd(_T(""))
	, m_dataLength(0)
	, m_setAB(FALSE)
	, m_setOut1(FALSE)
	, m_setOut2(FALSE)
	, m_in1(FALSE)
	, m_in2(FALSE)
	, m_heart(_T(""))
{

}

CParaSet::~CParaSet()
{
}

void CParaSet::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_BANK, m_bank);
	DDX_Control(pDX, IDC_COMBO_BEGIN, m_begin);
	DDX_Control(pDX, IDC_COMBO_LENGTH, m_length);
	DDX_Text(pDX, IDC_EDIT_DEVNO, m_deviceNo);
	DDX_Text(pDX, IDC_EDIT_NEGJUG, m_neighJudge);
	DDX_Text(pDX, IDC_EDIT_PWD, m_accesPwd);
	DDX_Check(pDX, IDC_CHK_ABMODE, m_setAB);
	DDX_Check(pDX, IDC_CHK_OUT1, m_setOut1);
	DDX_Check(pDX, IDC_CHK_OUT2, m_setOut2);
	DDX_Check(pDX, IDC_CHK_INPUT1, m_in1);
	DDX_Check(pDX, IDC_CHK_INPUT2, m_in2);
	DDX_Text(pDX, IDC_EDIT_HEART, m_heart);
}


BEGIN_MESSAGE_MAP(CParaSet, CPropertyPage)
	ON_CBN_SELENDCANCEL(IDC_COMBO_BANK, &CParaSet::OnCbnSelendcancelComboBank)
	ON_CBN_SELCHANGE(IDC_COMBO_BEGIN, &CParaSet::OnCbnSelchangeComboBegin)
	ON_BN_CLICKED(IDC_BTN_SETZONE, &CParaSet::OnBnClickedBtnSetzone)
	ON_BN_CLICKED(IDC_BTN_SETDEVNO, &CParaSet::OnBnClickedBtnSetdevno)
	ON_BN_CLICKED(IDC_BTN_SETNEGJUG, &CParaSet::OnBnClickedBtnSetnegjug)
	ON_BN_CLICKED(IDC_BTN_SETPWD, &CParaSet::OnBnClickedBtnSetpwd)
	ON_BN_CLICKED(IDC_BTN_GET, &CParaSet::OnBnClickedBtnGet)
	ON_BN_CLICKED(IDC_BTN_SET, &CParaSet::OnBnClickedBtnSet)
	ON_BN_CLICKED(IDC_BTN_SETABMODE, &CParaSet::OnBnClickedBtnSetabmode)
	ON_BN_CLICKED(IDC_BTN_CLRBUF, &CParaSet::OnBnClickedBtnClrbuf)
	ON_BN_CLICKED(IDC_BTN_SETHEART, &CParaSet::OnBnClickedBtnSetheart)
END_MESSAGE_MAP()


// CParaSet 消息处理程序

BOOL CParaSet::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	m_bank.AddString("EPC");
	m_bank.AddString("TID");
	m_bank.AddString("User");

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

void CParaSet::OnCbnSelendcancelComboBank()
{
		CString str;
		m_bank.GetWindowText(str);
		int start = 0 ,  end = 0;
	
		if (str == CString("EPC"))
		{
			start = 2; 
			end = 7;
			m_dataLength = 8;  // 有效数据长度(word)
		}
		else if ( str == CString ("TID"))
		{
			start = 0; 
			end = 5;
			m_dataLength = 6;
		}
		else if ( str == CString ("User"))
		{
			start = 0; 
			end = 31;
			m_dataLength = 32;
		}
	
		m_begin.ResetContent(); // 删除以前的选项
	
		for (int i = start  ; i <= end; ++i)
		{
			str.Format("%d",  i );
			m_begin.AddString(str);
		}
}

void CParaSet::OnCbnSelchangeComboBegin() 
{
	m_length.ResetContent();
	// 计算数据范围
	CString  str;
	m_begin.GetWindowText(str); // 获取起始点文本
	int begin = 1;
	int end = m_dataLength -  atoi(str.GetBuffer()); // 最大数据长度

	CString temp;
	for (int index = begin; index <=  end; ++index)
	{
		temp.Format("%d", index);
		m_length.AddString(temp);
	}
}

BOOL CParaSet::OnSetActive()
{
	// 得到主界面上的连接状态
	BOOL bConnected = ((CR2kDemoApp*)AfxGetApp())->m_connected;

	//设置各个按钮状态
	GetDlgItem(IDC_BTN_SETABMODE)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_SETDEVNO)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_SETNEGJUG)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_SETZONE)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_SETPWD)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_SET)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_GET)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_CLRBUF)->EnableWindow(bConnected);
	GetDlgItem(IDC_BTN_SETHEART)->EnableWindow(bConnected);

	return CPropertyPage::OnSetActive();
}

void CParaSet::OnBnClickedBtnSetzone()
{
	UpdateData();
	// 区域号
	int index = m_bank.GetCurSel();
	if (-1 == index)	// 没有选择时，bank 为255(-1)
	{
		MessageBox(_T("请选择操作区域！"), _T("错误"), 0);
		return;
	}
	unsigned bank = index + 1; // EPC, TID, User分别对应区域号1、2、3

	// 起始位置
	 index = m_begin.GetCurSel();
	if (-1 == index)	// 没有选择有效项目
	{
		MessageBox(_T("请选择起始地址！"), _T("错误"), 0);
		return;
	}
	// bank为EPC(1)时，起始地址为2
	unsigned char begin =  (1 == bank) ?  (2 + index) : index ;

	// 长度
	index = m_length.GetCurSel();
	if (-1 == index)	// 没有选择有效项目
	{
		MessageBox(_T("请选择长度！"), _T("错误"), 0);
		return;
	}
	unsigned char length =index + 1; // 索引从0开始
	if (R_OK == SetReadZone(bank, begin, length))
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("设置寻卡区域成功.");
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("设置寻卡区域失败.");
	}		
}

void CParaSet::OnBnClickedBtnSetdevno()
{
	UpdateData();
	if (m_deviceNo.GetLength() == 0)
	{
		MessageBox(_T("请输入有效数值"),  _T("错误"), 0);
		return;
	}
	unsigned int deviceNo = GetDlgItemInt(IDC_EDIT_DEVNO);
	if (deviceNo > 254)
	{
		MessageBox(_T("请设置254以下的设备号"), _T("错误"), 0);
		return;
	}
	if (R_OK == SetDeviceNo(deviceNo))
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("设备号设置成功.");
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("设备号设置失败.");
	}		
}

void CParaSet::OnBnClickedBtnSetnegjug()
{
	UpdateData();
	if (m_neighJudge.GetLength() == 0)
	{
		MessageBox(_T("请输入有效数值"),  _T("错误"), 0);
		return;
	}
	unsigned int  intTime  = GetDlgItemInt(IDC_EDIT_NEGJUG);
	if ( intTime > 254)
	{
		MessageBox(_T("请设置254以下的值"), _T("错误"), 0);
		return;
	}
	if (R_OK == SetNeighJudgeTime(intTime))
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("相邻判定设置成功.");
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("相邻判定设置失败.");
	}	
}

void CParaSet::OnBnClickedBtnSetpwd()
{
	UpdateData();
	if (m_accesPwd.GetLength() < 8)
	{
		MessageBox(_T("密码位数不足8位"),  _T("错误"), 0);
		return;
	}
	if (R_OK == SetReadPassword(m_accesPwd.GetBuffer()))
	{
		SetDlgItemText(IDC_STA_RESULT, _T("密码设置成功"));
	}
	else
	{
		SetDlgItemText(IDC_STA_RESULT, _T("密码设置失败"));
	}
}

void CParaSet::OnBnClickedBtnGet()
{
	unsigned char out[4] = {0};
	if (R_OK == GetDI(&out[0]))
	{
		m_in1 = out[0];
		m_in2 = out[1];
		UpdateData(FALSE);
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("读取成功.");
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("读取失败.");
	}	
}

void CParaSet::OnBnClickedBtnSet()
{
	UpdateData();
	CString res = CString("");
	if (R_OK == SetDO(1, m_setOut1))
	{
		res = CString("输出口1设置成功\n");
	}
	else
	{
		res = CString("输出口1设置失败\n");
	}	
	if (R_OK == SetDO(2, m_setOut2))
	{
		res +=  CString("输出口2设置成功.");
	}
	else
	{
		res +=  CString("输出口2设置失败.");
	}	
	GetDlgItem(IDC_STA_RESULT)->SetWindowText(res);	
}

void CParaSet::OnBnClickedBtnSetabmode()
{
	UpdateData();
	if (R_OK == SetABMode(m_setAB))
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("AB模式设置成功.");
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("AB模式设置失败.");
	}		
}

void CParaSet::OnBnClickedBtnClrbuf()
{
	if (R_OK == ResetTagBuffer())
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("缓冲区清理成功.");
	}
	else
	{
		GetDlgItem(IDC_STA_RESULT)->SetWindowText("缓冲区清理失败.");
	}	
}

void CParaSet::OnBnClickedBtnSetheart()
{
	UpdateData();
	if (m_heart.GetLength() == 0)
	{
		MessageBox(_T("请输入有效数值"),  _T("错误"));
		return;
	}
	unsigned int hearttime = GetDlgItemInt(IDC_EDIT_HEART);
	if (hearttime > 254)
	{
		MessageBox(_T("请输入254以下的数值"), _T("错误"), 0);
		return;
	}
	CString str;
	str.Format("hearttime = %d", hearttime);
	MessageBox(str, _T(""), 0);
	if (R_OK == SetAlive(hearttime))
	{
		SetDlgItemText(IDC_STA_RESULT, _T("心跳时间设置成功"));
	}
	else
	{
		SetDlgItemText(IDC_STA_RESULT,  _T("心跳时间设置失败"));
	}
}
