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

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "AddDevice.h"
#include "NetSDKDLL.h"

struct IPC
{
	IPC_ENTRY m_IPCEntry;
	int m_iIndex; //搜索索引
};
// CAddDevice 对话框
BOOL   checkip(LPCTSTR   lp);
IMPLEMENT_DYNAMIC(CAddDevice, CDialog)

CAddDevice::CAddDevice(CWnd* pParent)
	: CDialog(CAddDevice::IDD, pParent)
	, m_pDevListDlg((CDevListDlg*)pParent)
	, m_strIPAddr(_T("192.168.0.123"))
	, m_lPTZPort(8091)
	, m_strUserName(_T("admin"))
	, m_strPassWord(_T("123456"))
	, m_lVideoPort(554)
{

}

CAddDevice::~CAddDevice()
{
}

void CAddDevice::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_ED_IP, m_strIPAddr);
	DDX_Text(pDX, IDC_ED_PORT, m_lPTZPort);
	DDX_Control(pDX, IDC_CK_TCP, m_ctlBTCP);
	DDX_Text(pDX, IDC_ED_USERNAME, m_strUserName);
	DDX_Text(pDX, IDC_ED_PASSWORD, m_strPassWord);
	DDX_Text(pDX, IDC_ED_PORT2, m_lVideoPort);
	DDX_Control(pDX, IDC_COMBO2, m_cbDeviceType);
}


BEGIN_MESSAGE_MAP(CAddDevice, CDialog)
	ON_BN_CLICKED(IDC_BTN_ADDDEV, &CAddDevice::OnBnClickedBtnAdddev)
	ON_CBN_SELCHANGE(IDC_COMBO2, &CAddDevice::OnCbnSelchangeCombo2)
END_MESSAGE_MAP()


// CAddDevice 消息处理程序

void CAddDevice::OnBnClickedBtnAdddev()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	

	if (m_lVideoPort < 1 || m_lVideoPort >65535)
	{
		AfxMessageBox("非法视频端口！");
		return ;
	}
 	
	if (m_lPTZPort < 1 || m_lPTZPort >65535)
	{
		AfxMessageBox("非法PTZ端口！");
		return ;
	}
 

	
	if (!checkip(m_strIPAddr))
	{
		AfxMessageBox("非法IP地址！");
		return ;
	}
// 	long lLogin = IP_NET_DVR_Login(m_strIPAddr.GetBuffer(), m_lPTZPort, m_strUserName.GetBuffer(), m_strPassWord.GetBuffer(), NULL);
	

	int iSel = m_cbDeviceType.GetCurSel();
	
	IPC_ENTRY Entry;
	memset(&Entry, 0x00, sizeof(IPC_ENTRY));
	sprintf_s(Entry.lanCfg.IPAddress, MAX_IP_NAME_LEN, "%s", m_strIPAddr.GetBuffer());
	Entry.streamCfg.ptzPort = m_lPTZPort;
	Entry.streamCfg.videoPort = m_lVideoPort;
	Entry.userCfg.count = 1;
	if (iSel == 0)
	{
		sprintf_s(Entry.deviceType, MAX_DEVICETYPE_LEN, "%s", "IPC");
	}
	else if (iSel == 1)
	{
		sprintf_s(Entry.deviceType, MAX_DEVICETYPE_LEN, "%s", "DVS");
	}
	else if (iSel == 2)
	{
		sprintf_s(Entry.deviceType, MAX_DEVICETYPE_LEN, "%s", "Decorder");
	}
	else 
	{
		sprintf_s(Entry.deviceType, MAX_DEVICETYPE_LEN, "%s", "NVR");
	}
	sprintf_s(Entry.userCfg.accounts[0].group, GROUP_NAME_MAX_LEN, "%s", "Administrator");
	sprintf_s(Entry.userCfg.accounts[0].status, ACCOUNT_STATUS_MAX_LEN, "%s", "Enable");
	sprintf_s(Entry.userCfg.accounts[0].password, ACCOUNT_PASSWORD_MAX_LEN, "%s", m_strPassWord);
	sprintf_s(Entry.userCfg.accounts[0].userName, ACCOUNT_NAME_MAX_LEN, "%s", m_strUserName);
	
	////+		deviceType	0x0012e260 "NVS-DM36X-HD"	char [32]
	m_pDevListDlg->ADDIPC(Entry);
	//((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->ReflashTreeImage();

	
	m_pDevListDlg->ReflashTreeImage();

}

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

	// TODO:  在此添加额外的初始化
	m_ctlBTCP.EnableWindow(FALSE);
	m_ctlBTCP.ShowWindow(SW_HIDE);
	m_cbDeviceType.SetCurSel(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


BOOL   checkip(LPCTSTR   lp) 
{ 
	char   lpszip[100]; 
	bool   bret   =   TRUE; 

	if(lp==NULL||_tcslen(lp)>=100) 
	{
		bret   =   FALSE; 
	}
	else
	{ 
		_tcscpy(lpszip,lp); 
		char   seps[]       =   _T("."); 
		char   *token; 
		int   nfields=0; 
		token   =   _tcstok(   lpszip,   seps   ); 
		while(   token   !=   NULL   ) 
		{ 
			nfields   ++; 
			int   ipf=atoi(token); 
			if(ipf==0&&_tcsicmp(token, _T("0"))!=0) 
			{ 
				bret   =   false; 
				break; 
			} 
			if(ipf<0 || ipf>255) 
			{ 
				bret   =   false; 
				break; 
			} 
			token   =   _tcstok(   NULL,   seps   ); 
		} 
		if(nfields!=4) 
			bret   =   false; 
	} 
	return   bret;	
} 
BOOL CAddDevice::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if(pMsg->message == WM_KEYDOWN)
	{	
		if (pMsg->wParam == 13)
		{
			OnBnClickedBtnAdddev();
			return TRUE;
		}
		if (pMsg->wParam == 27)
			return TRUE;
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CAddDevice::OnCbnSelchangeCombo2()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	int iSel = m_cbDeviceType.GetCurSel();
	
	if (3 == iSel)
	{
		m_lPTZPort = 555;
		m_lVideoPort = 554;
	}
	else
	{
		m_lPTZPort = 8091;
		m_lVideoPort = 554;
	}
	UpdateData(FALSE);
}
