// TestSDKDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestSDK.h"
#include "TestSDKDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestSDKDlg dialog

CTestSDKDlg::CTestSDKDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestSDKDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestSDKDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestSDKDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestSDKDlg)
	DDX_Control(pDX, IDC_LISTMSG, m_ListMsg);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestSDKDlg, CDialog)
	//{{AFX_MSG_MAP(CTestSDKDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
	ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
	ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
	ON_BN_CLICKED(IDC_BUTTON15, OnButton15)
	ON_BN_CLICKED(IDC_BUTTON18, OnButton18)
	ON_BN_CLICKED(IDC_BUTTON16, OnButton16)
	ON_BN_CLICKED(IDC_BUTTON19, OnButton19)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestSDKDlg message handlers

BOOL CTestSDKDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	GetDlgItem(IDC_EDIT3)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON13)->EnableWindow(false);
	GetDlgItem(IDC_EDIT2)->SetWindowText("80");
	GetDlgItem(IDC_EDIT1)->SetWindowText("1");
	m_ListMsg.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
	
	m_ListMsg.InsertColumn(0,"发生时间",LVCFMT_LEFT,140);
	m_ListMsg.InsertColumn(1,"信息",LVCFMT_LEFT,550);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestSDKDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CTestSDKDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestSDKDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CTestSDKDlg::InsertDebugMes(CString sMsg)
{
	CString strTime;
	CTime   timTemp;
	timTemp = CTime::GetCurrentTime();
	strTime = timTemp.Format("%Y-%m-%d %H:%M:%S");
	m_ListMsg.InsertItem(LVIF_IMAGE| LVIF_TEXT|LVIF_STATE, 0, strTime,0,0,0,0);
	m_ListMsg.SetItemText(0, 1,sMsg );
}

void CTestSDKDlg::OnButton1() 
{
	CString sMsg;
	int nRet ;
	char ctmp[256]={0};
	nRet = Syn_SetPhotoPath(0,ctmp);
	sMsg.Format("照片存放路径设置为 C:,nRet = %d",nRet);
	InsertDebugMes(sMsg);
	
}

void CTestSDKDlg::OnButton2() 
{
	CString sMsg;
	int nRet ;
	char ctmp[256]={0};
	nRet = Syn_SetPhotoPath(1,ctmp);
	if(nRet == 0)
	{
		sMsg.Format("照片存放路径设置为 当前路径,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片存放路径设置错误,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton3() 
{
	CString sMsg;
	int nRet ;
	LPMALLOC   lpMalloc;   
	if(::SHGetMalloc(&lpMalloc)!=NOERROR)   
	{   
		AfxMessageBox("选择下载目录操作出错");   
		return;   
	}   
	char   szDisplayName[_MAX_PATH];   
	char   szBuffer[_MAX_PATH];   
	BROWSEINFO   browseInfo;   
	browseInfo.hwndOwner=this->m_hWnd;   
	browseInfo.pidlRoot=NULL;   
	browseInfo.pszDisplayName=szDisplayName;   
	browseInfo.lpszTitle="请选择照片存放路径路径";   
	browseInfo.ulFlags=BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;   
	browseInfo.lpfn=NULL;   
	browseInfo.lParam=0;   
	LPITEMIDLIST   lpItemIDList;   
	if((lpItemIDList=::SHBrowseForFolder(&browseInfo))!=NULL)   
	{   
		if(::SHGetPathFromIDList(lpItemIDList,szBuffer))   
		{   
			if(szBuffer[0]=='\0')   
			{   
				sMsg="选取路径失败！";
				InsertDebugMes(sMsg);
				return;   
			}   
		}   
		else   
		{   
			sMsg="选取路径失败！";
			InsertDebugMes(sMsg);
			return;   
		}   
		lpMalloc->Free(lpItemIDList);   
		lpMalloc->Release();   
	}   
	else
	{
		sMsg="没有选取路径！";
		InsertDebugMes(sMsg);
		return;   
	}
	nRet = Syn_SetPhotoPath(2,szBuffer);
	sMsg.Format("照片存放路径设置为 %s,nRet = %d",szBuffer,nRet);
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton4() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoType(0);
	if (nRet == 0)
	{
		sMsg.Format("照片保存格式设置为 Bmp,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton5() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoType(1);
	if (nRet == 0)
	{
		sMsg.Format("照片保存格式设置为 Jpeg,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton6() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoType(2);
	if (nRet == 0)
	{
		sMsg.Format("照片保存格式设置为 Base64,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton7() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoName(0);
	if (nRet == 0)
	{
		sMsg.Format("照片保存文件名格式设置为 tmp,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存文件名格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton8() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoName(1);
	if (nRet == 0)
	{
		sMsg.Format("照片保存文件名格式设置为 姓名,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存文件名格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton9() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoName(2);
	if (nRet == 0)
	{
		sMsg.Format("照片保存文件名格式设置为 身份证号,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存文件名格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton10() 
{
	int nRet;
	CString sMsg;
	nRet = Syn_SetPhotoName(3);
	if (nRet == 0)
	{
		sMsg.Format("照片保存文件名格式设置为 姓名_身份证号,nRet = %d",nRet);
	}
	else
	{
		sMsg.Format("照片保存文件名格式设置失败,nRet = %d",nRet);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton11() 
{
	int nRet,nRet2;
	unsigned int uiCurrBaud; 
	CString sMsg,sMsg2;
	nRet = Syn_FindReader();
	if ( nRet == 0)
	{
		sMsg = "没有找打读卡器";
	}
	else
	{
		Beep(4000,200);
		if (nRet >1000)
		{
			GetDlgItem(IDC_EDIT3)->EnableWindow(false);
			GetDlgItem(IDC_BUTTON13)->EnableWindow(false);
			sMsg.Format("读卡器连接在USB端口 %d",nRet); 
		}
		else
		{
			Sleep(200);
			nRet2 = Syn_GetCOMBaud( nRet ,&uiCurrBaud);
			sMsg2.Format("%u",uiCurrBaud);
			GetDlgItem(IDC_EDIT3)->SetWindowText(sMsg2);
			GetDlgItem(IDC_EDIT3)->EnableWindow(true);
			GetDlgItem(IDC_BUTTON13)->EnableWindow(true);
			sMsg.Format("读卡器连接在串口 %d,当前SAM波特率为 %u",nRet,uiCurrBaud); 
		}
		sMsg2.Format("%d",nRet);
		GetDlgItem(IDC_EDIT1)->SetWindowText(sMsg2);
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton12() 
{
	int nRet;
	int iPort;
	int iMaxByte;
	CString sMsg,sMsg2;
	GetDlgItem(IDC_EDIT1)->GetWindowText(sMsg2);
	iPort = atoi(sMsg2);
	GetDlgItem(IDC_EDIT2)->GetWindowText(sMsg2);
	iMaxByte = atoi(sMsg2);
	nRet = Syn_OpenPort( iPort );
	if (nRet == 0)
	{
		if ( Syn_GetSAMStatus( iPort ,0 ) == 0)
		{
			if(Syn_SetMaxRFByte( iPort ,iMaxByte,0) == 0)
			{
				sMsg.Format("设置最大通讯字节数成功,最大通讯字节数为 %d",iMaxByte);
			}
			else
			{
				sMsg = "设置最大通讯字节数出错!";
			}

		}
		else
		{
			sMsg.Format("端口 %d 上没有连接读卡器!",iPort);
		}
	}
	else
	{
		sMsg = "打开端口错误！";
	}
	Syn_ClosePort( iPort );
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton13() 
{
	int iPort;
	unsigned int iCurrBaud,iSetBaud;
	CString sMsg,sMsg2;
	GetDlgItem(IDC_EDIT1)->GetWindowText(sMsg2);
	iPort = atoi(sMsg2);
	if (Syn_GetCOMBaud(iPort,&iCurrBaud)!=0 )
	{
		sMsg="获得当前读卡器波特率失败";
		InsertDebugMes(sMsg);
		return;
	}
	GetDlgItem(IDC_EDIT3)->GetWindowText(sMsg2);
	iSetBaud = atoi(sMsg2);
	Syn_ClosePort( iPort );
	if(Syn_SetCOMBaud( iPort , iCurrBaud , iSetBaud) == 0)
	{
		sMsg.Format("设置读卡器波特率成功,当前波特率为 %d ",iSetBaud);
	}
	else
	{
		sMsg = "设置读卡器波特率失败";
	}
	InsertDebugMes(sMsg);
}

void CTestSDKDlg::OnButton14() 
{
	int nRet,iPort;
	CString sMsg;
	GetDlgItem(IDC_EDIT1)->GetWindowText(sMsg);
	iPort = atoi(sMsg);
	nRet = Syn_OpenPort(iPort);
	if (nRet == 0)
	{
		nRet = Syn_ResetSAM( iPort , 0 );
		if (nRet == 0)
		{
			sMsg = "复位SAM模块成功";
		}
		else
		{
			sMsg = "复位SAM模块失败";
		}
	}
	else
	{
		sMsg = "打开端口错误";
	}
	Syn_ClosePort( iPort );
	InsertDebugMes( sMsg );
}

void CTestSDKDlg::OnButton15() 
{
	int nRet,iPort;	
	unsigned char cSAMID[16];
	unsigned int uSAMID[4];
	memset(cSAMID,0,16);
	CString sMsg;
	GetDlgItem(IDC_EDIT1)->GetWindowText(sMsg);
	iPort = atoi(sMsg);
	nRet = Syn_OpenPort( iPort );
	if (nRet == 0)
	{
		nRet = Syn_GetSAMID( iPort , cSAMID,0 );
		if (nRet == 0)
		{
			memcpy(uSAMID,cSAMID,16);
			sMsg.Format("按16字节获得SAM模块ID成功,SAM模块ID为: %X%X-%X%X-%u-%u-%u",cSAMID[1],cSAMID[0],cSAMID[3],cSAMID[2],uSAMID[1],uSAMID[2],uSAMID[3]);
		}
		else
		{
			sMsg = "读取SAM ID号错误";
		}
	}
	else
	{
		sMsg = "打开端口错误";
	}
	Syn_ClosePort( iPort );
	InsertDebugMes( sMsg );
}

void CTestSDKDlg::OnButton18() 
{
	int nRet,iPort;
	CString sMsg;
	GetDlgItem(IDC_EDIT1)->GetWindowText(sMsg);
	iPort = atoi(sMsg);
	nRet = Syn_OpenPort(iPort);
	if (nRet == 0)
	{
		nRet = Syn_GetSAMStatus( iPort , 0 );
		if (nRet == 0)
		{
			sMsg = "SAM模块状态正常";
		}
		else
		{
			sMsg = "获得SAM模块状态失败";
		}
	}
	else
	{
		sMsg = "打开端口错误";
	}
	Syn_ClosePort( iPort );
	InsertDebugMes( sMsg );
}

void CTestSDKDlg::OnButton16() 
{
	int nRet,iPort;
	CString sMsg;
	unsigned char pucIIN[4];
	unsigned char pucSN[8];
	IDCardData idcardData;
	GetDlgItem(IDC_EDIT1)->GetWindowText(sMsg);
	Syn_SetSexType(1);
	Syn_SetNationType(1);
	Syn_SetBornType(2);
	Syn_SetUserLifeBType(3);
	Syn_SetUserLifeEType(4,1);

	iPort = atoi(sMsg);
	nRet = Syn_OpenPort(iPort);
	if (nRet == 0)
	{
		if ( Syn_SetMaxRFByte( iPort ,80 , 0) == 0)
		{
			Syn_StartFindIDCard( iPort , pucIIN , 0 );
			Syn_SelectIDCard( iPort , pucSN , 0 );
			if (Syn_ReadMsg( iPort , 0 , &idcardData) == 0)
			{
				sMsg= "读取身份证信息成功!";
				InsertDebugMes(sMsg);
				sMsg.Format("姓名:%s",idcardData.Name);
				InsertDebugMes(sMsg);
				sMsg.Format("性别:%s",idcardData.Sex);
				InsertDebugMes(sMsg);
				sMsg.Format("民族:%s",idcardData.Nation);
				InsertDebugMes(sMsg);
				sMsg.Format("出生:%s",idcardData.Born);
				InsertDebugMes(sMsg);
				sMsg.Format("住址:%s",idcardData.Address);
				InsertDebugMes(sMsg);
				sMsg.Format("身份证号:%s",idcardData.IDCardNo);
				InsertDebugMes(sMsg);
				sMsg.Format("发证机关:%s",idcardData.GrantDept);
				InsertDebugMes(sMsg);
				sMsg.Format("有效期开始:%s",idcardData.UserLifeBegin);
				InsertDebugMes(sMsg);
				sMsg.Format("有效期结束:%s",idcardData.UserLifeEnd);
				InsertDebugMes(sMsg);
				sMsg.Format("照片:%s",idcardData.PhotoFileName);
				InsertDebugMes(sMsg);
			}
			else
			{
				sMsg = "读取身份证信息错误!";
				InsertDebugMes(sMsg);
			}
		}
	}
	else
	{
		sMsg = "打开端口错误";
		InsertDebugMes(sMsg);
	}
	Syn_ClosePort( iPort );
}

void CTestSDKDlg::OnButton19() 
{
	m_ListMsg.DeleteAllItems();	
}

