// testZNET100Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "testZNET100.h"
#include "testZNET100Dlg.h"
#include "ZNE100DLL.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()

/////////////////////////////////////////////////////////////////////////////
// CTestZNET100Dlg dialog

CTestZNET100Dlg::CTestZNET100Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestZNET100Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestZNET100Dlg)
		// 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 CTestZNET100Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestZNET100Dlg)
	DDX_Control(pDX, IDC_COMBOSTOPBITS, m_ctlComboStopBits);
	DDX_Control(pDX, IDC_COMBODATABITS, m_ctlComboDataBits);
	DDX_Control(pDX, IDC_COMBOCHECKBITS, m_ctlComboCheckBits);
	DDX_Control(pDX, IDC_COMBOBAUD, m_ctlComboBaud);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestZNET100Dlg, CDialog)
	//{{AFX_MSG_MAP(CTestZNET100Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_SPECIFYSEARCH, OnButtonSpecifysearch)
	ON_BN_CLICKED(IDC_BUTTON_GETDEVINFO, OnButtonGetdevinfo)
	ON_BN_CLICKED(IDC_BUTTON_CHGDEVINFO, OnButtonChgdevinfo)
	ON_BN_CLICKED(IDC_BUTTON_CHGZNE, OnButtonChgzne)
	ON_BN_CLICKED(IDC_BUTTON_GETIOSTAT, OnButtonGetiostat)
	ON_BN_CLICKED(IDC_BUTTON_SETIOSTAT, OnButtonSetiostat)
	ON_BN_CLICKED(IDC_BUTTON_RESETDEV, OnButtonResetdev)
	ON_BN_CLICKED(IDC_BUTTON_GETADC, OnButtonGetadc)
	ON_BN_CLICKED(IDC_BUTTON_RESETTCP, OnButtonResettcp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestZNET100Dlg message handlers

BOOL CTestZNET100Dlg::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
	
	// TODO: Add extra initialization here
	m_ctlComboBaud.SetCurSel(0);
	m_ctlComboCheckBits.SetCurSel(0);
	m_ctlComboDataBits.SetCurSel(0);
	m_ctlComboStopBits.SetCurSel(0);

	ZNET_Init(); //初始化连接，一般在程序启动后运行此函数

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

void CTestZNET100Dlg::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 CTestZNET100Dlg::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 CTestZNET100Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestZNET100Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	int count=5;
	DEVINFO devinfo[50];
	int len=50;
	int getlen;
	CString str="搜索到设备:\n",strtmp;

	ZNET_SearchBroadcast();  //广播搜索
	while(count--) //搜索超时时间设为5秒
	{
		while((getlen=ZNET_RecDevice(devinfo,&len))>0) //此时表示搜索到设备
		{
			for(int i=0;i<getlen;i++)
			{
				strtmp.Format("IP:%s MAC:%s\n",devinfo[i].szip,devinfo[i].szmacaddr);
				str+=strtmp;
			}
		}
		Sleep(1000);
	}
	ZNET_Stop(); //广播搜索，指定IP搜索操作完后都必须调用此函数来停止
	MessageBox(str);
}

void CTestZNET100Dlg::OnDestroy() 
{
	ZNET_Uninit(); //关闭连接，一般在程序退出时调用此函数
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
}

void CTestZNET100Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
//	CDialog::OnOK();
}

void CTestZNET100Dlg::OnButtonSpecifysearch() 
{
	// TODO: Add your control notification handler code here
	CString strip;
	GetDlgItem(IDC_EDIT_IP1)->GetWindowText(strip);
	int count=5;
	DEVINFO devinfo[50];
	int len=50;
	int getlen;
	CString str="搜索到设备:\n",strtmp;

	ZNET_SearchIP((LPTSTR)(LPCTSTR)strip);  //指定IP搜索
	while(count--) //搜索超时时间设为5秒
	{
		while((getlen=ZNET_RecDevice(devinfo,&len))>0) //此时表示搜索到设备
		{
			for(int i=0;i<getlen;i++)
			{
				if(strip.CompareNoCase(devinfo[i].szip)==0)
				{
					strtmp.Format("IP:%s MAC:%s\n",devinfo[i].szip,devinfo[i].szmacaddr);
					str+=strtmp;
					goto ext;
				}
			}
		}
		Sleep(1000);
	}
ext:
	ZNET_Stop(); //广播搜索，指定IP搜索操作完后都必须调用此函数来停止
	MessageBox(str);
}

void CTestZNET100Dlg::OnButtonGetdevinfo() 
{
	// TODO: Add your control notification handler code here
	CString strip;
	GetDlgItem(IDC_EDIT_IP1)->GetWindowText(strip);
	DEVINFO devinfo;
	CString str="设备信息:\n",strtmp;

	if(ZNET_GetDeviceInformation((LPTSTR)(LPCTSTR)strip,&devinfo)==1)
	{
		strtmp.Format("设备名称:%s\n",devinfo.szname);
		str+=strtmp;
		strtmp.Format("设备IP:%s\n",devinfo.szip);
		str+=strtmp;
		strtmp.Format("网关:%s\n",devinfo.szgateip);
		str+=strtmp;
		strtmp.Format("子网掩码:%s\n",devinfo.szmask);
		str+=strtmp;
		strtmp.Format("MAC地址:%s\n",devinfo.szmacaddr);
		str+=strtmp;
		strtmp.Format("工作端口:%d\n",devinfo.workport);
		str+=strtmp;
	}
	MessageBox(str);
	
}

void CTestZNET100Dlg::OnButtonChgdevinfo() 
{
	// TODO: Add your control notification handler code here
	CString strip,strpwd,strnewip,strtmp;
	GetDlgItem(IDC_EDIT_IP2)->GetWindowText(strip);
	GetDlgItem(IDC_EDIT_PWD)->GetWindowText(strpwd);
	GetDlgItem(IDC_EDIT_NEWIP)->GetWindowText(strnewip);
	GetDlgItem(IDC_EDIT_WORKPORT)->GetWindowText(strtmp);
	int port=atoi(strtmp);
	DEVINFO devinfo;
	int res;

	if(ZNET_GetDeviceInformation((LPTSTR)(LPCTSTR)strip,&devinfo)==1)
	{
		strcpy(devinfo.szip,strnewip);
		devinfo.workport=port;
		if((res=ZNET_ModifyDevice((LPTSTR)(LPCTSTR)strip,(LPTSTR)(LPCTSTR)strpwd,
			devinfo))==1)
		{
			MessageBox("更改成功");
		}
	}
}

void CTestZNET100Dlg::OnButtonChgzne() 
{
	// TODO: Add your control notification handler code here
	CString strip,strpwd;
	GetDlgItem(IDC_EDIT_PWD3)->GetWindowText(strpwd);
	GetDlgItem(IDC_EDIT_IP3)->GetWindowText(strip);
	DEVINFO devinfo;
	int res;

	if(ZNET_GetDeviceInformation((LPTSTR)(LPCTSTR)strip,&devinfo)==1)
	{
		devinfo.zne100info.baud=m_ctlComboBaud.GetCurSel();
		devinfo.zne100info.checkbits=m_ctlComboCheckBits.GetCurSel();
		devinfo.zne100info.databits=5+m_ctlComboDataBits.GetCurSel();
		devinfo.zne100info.stopbits=1+m_ctlComboStopBits.GetCurSel();
		if((res=ZNET_ModifyDevice((LPTSTR)(LPCTSTR)strip,(LPTSTR)(LPCTSTR)strpwd,
			devinfo))==1)
		{
			MessageBox("更改成功");
		}
	}

}

void CTestZNET100Dlg::OnButtonGetiostat() 
{
	// TODO: Add your control notification handler code here
	CString strip,strport,strtmp;
	BYTE dir,elevel;
	GetDlgItem(IDC_EDIT_IP4)->GetWindowText(strip);
	GetDlgItem(IDC_EDIT_IOCTLPORT)->GetWindowText(strport);
	if(ZNET_GetIOStat((LPTSTR)(LPCTSTR)strip,atoi(strport),&dir,&elevel)==0)
	{
		MessageBox("获取IO口状态失败");
		return;
	}
	strtmp.Format("%02X",dir);
	GetDlgItem(IDC_EDIT_IODIR)->SetWindowText(strtmp);
	strtmp.Format("%02X",elevel);
	GetDlgItem(IDC_EDIT_IOELEVEL)->SetWindowText(strtmp);
}

void CTestZNET100Dlg::OnButtonSetiostat() 
{
	// TODO: Add your control notification handler code here
	CString strip,strport,strtmp;
	BYTE dir,elevel;
	int itmp;
	GetDlgItem(IDC_EDIT_IP4)->GetWindowText(strip);
	GetDlgItem(IDC_EDIT_IOCTLPORT)->GetWindowText(strport);
	GetDlgItem(IDC_EDIT_IODIR)->GetWindowText(strtmp);
	sscanf(strtmp,"%x",&itmp);
	dir=itmp;
	GetDlgItem(IDC_EDIT_IOELEVEL)->GetWindowText(strtmp);
	sscanf(strtmp,"%x",&itmp);
	elevel=itmp;
	if(ZNET_SetIOStat((LPTSTR)(LPCTSTR)strip,atoi(strport),2,&dir,&elevel)==0)
	{
		MessageBox("更改IO口状态失败");
		return;
	}
	strtmp.Format("%02X",dir);
	GetDlgItem(IDC_EDIT_IODIR)->SetWindowText(strtmp);
	strtmp.Format("%02X",elevel);
	GetDlgItem(IDC_EDIT_IOELEVEL)->SetWindowText(strtmp);
}

void CTestZNET100Dlg::OnButtonResetdev() 
{
	// TODO: Add your control notification handler code here
	CString strip;
	GetDlgItem(IDC_EDIT_IP4)->GetWindowText(strip);
	if(ZNET_ResetDevice((LPTSTR)(LPCTSTR)strip)!=1)
		MessageBox("复位设备失败");
}

void CTestZNET100Dlg::OnButtonGetadc() 
{
	// TODO: Add your control notification handler code here
	CString strip,strport,strtmp;
	WORD adc0,adc1;
	GetDlgItem(IDC_EDIT_IP4)->GetWindowText(strip);
	GetDlgItem(IDC_EDIT_IOCTLPORT)->GetWindowText(strport);
	if(ZNET_GetADC((LPTSTR)(LPCTSTR)strip,atoi(strport),&adc0,&adc1)==0)
	{
		MessageBox("获取ADC值失败");
		return;
	}
	strtmp.Format("%04X",adc0);
	GetDlgItem(IDC_EDIT_ADC0)->SetWindowText(strtmp);
	strtmp.Format("%04X",adc1);
	GetDlgItem(IDC_EDIT_ADC1)->SetWindowText(strtmp);

}

void CTestZNET100Dlg::OnButtonResettcp() 
{
	// TODO: Add your control notification handler code here
	CString strip,strport;
	GetDlgItem(IDC_EDIT_IP4)->GetWindowText(strip);
	GetDlgItem(IDC_EDIT_IOCTLPORT)->GetWindowText(strport);
	if(ZNET_ResetDevTCP((LPTSTR)(LPCTSTR)strip,atoi(strport))==0)
	{
		MessageBox("复位设备失败");
	}
	else
		MessageBox("复位设备成功");
}
