📄 adddevicedlg.cpp
字号:
// AddDeviceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NetSDKDemo.h"
#include "AddDeviceDlg.h"
#include "NetSDKDemoDlg.h"
#include "Winsock2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//添加设备页面的处理
/////////////////////////////////////////////////////////////////////////////
// AddDeviceDlg dialog
UserInfo *g_userinfo[10];
CString strPath;
inline void dbg_print_ex(int level, const char *msg, ...)
{
char buf[256];
va_list ap;
va_start(ap, msg); // use variable arg list
vsprintf(buf, msg, ap);
va_end( ap );
OutputDebugString(buf);
}
AddDeviceDlg::AddDeviceDlg(CWnd* pParent /*=NULL*/)
: CDialog(AddDeviceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(AddDeviceDlg)
m_devicename = _T("DVR");
m_deviceusername = _T("admin");
m_deviceuserpsw = _T("admin");
m_deviceport = 37777;
m_IPstr = _T("192.168.0.1");
m_devicedn = _T(".vicp.net");
//}}AFX_DATA_INIT
}
void AddDeviceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(AddDeviceDlg)
DDX_Control(pDX, IDC_WAY, m_way);
DDX_Control(pDX, IDC_IPSELECTION, m_ipsel);
DDX_Text(pDX, IDC_DEVICE_NAME, m_devicename);
DDV_MaxChars(pDX, m_devicename, 20);
DDX_Text(pDX, IDC_DEVICE_USERNAME, m_deviceusername);
DDV_MaxChars(pDX, m_deviceusername, 20);
DDX_Text(pDX, IDC_DEVICE_USERPSW, m_deviceuserpsw);
DDV_MaxChars(pDX, m_deviceuserpsw, 20);
DDX_Text(pDX, IDC_DEVICE_PORT, m_deviceport);
DDX_CBString(pDX, IDC_IPSELECTION, m_IPstr);
DDV_MaxChars(pDX, m_IPstr, 20);
DDX_Text(pDX, IDC_DEVICE_DN, m_devicedn);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(AddDeviceDlg, CDialog)
//{{AFX_MSG_MAP(AddDeviceDlg)
ON_BN_CLICKED(IDC_CONNECT_DEVICE, OnConnectDevice)
ON_BN_CLICKED(IDC_CANCEL_ADDDEVICE, OnCancelAdddevice)
ON_WM_CLOSE()
ON_CBN_SELCHANGE(IDC_IPSELECTION, OnSelchangeIpselection)
ON_CBN_SELCHANGE(IDC_WAY, OnSelchangeWay)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// AddDeviceDlg message handlers
//连接失败的类型
void AddDeviceDlg::GetConnectError(int error)
{
// char tmpchar[100];
// memset ((char *)tmpchar, 0, 100);
// sprintf(tmpchar,"%d", error);
// MessageBox(tmpchar);
// return;
//根据返回值不同判断错误类型
switch(error) {
case 1://密码不正确
MessageBox(MSG_ERROR_PASSWORD);
break;
case 2://用户名不存在
MessageBox(MSG_ERROR_USER);
break;
case 3://登录超时
MessageBox(MSG_ERROR_TIMEOUT);
break;
case 4://重复登录
MessageBox(MSG_ERROR_RELOGGIN);
break;
case 5://帐号被锁定
MessageBox(MSG_ERROR_LOCKED);
break;
case 6://帐号被列入黑名单
MessageBox(MSG_ERROR_BLACKLIST);
break;
case 7://系统忙,资源不足
MessageBox(MSG_ERROR_BUSY);
break;
case 9://找不到主机
MessageBox(MSG_ERROR_CONNECT);
break;
case 0:
default:
MessageBox(MSG_ERROR_NETWORK);
break;
}
}
//增加连接设备,连接成功增加到g_ptrdevicelist中
void AddDeviceDlg::OnConnectDevice()
{
//CString
int err; //存储可能的错误返回值
DeviceNode *inode;//临时设备信息节点
UpdateData(TRUE); //获取界面输入
inode = new DeviceNode;
memset((char *)inode, 0, sizeof(DeviceNode));
//将页面信息写入节点
sprintf(inode->IP, "%s", m_IPstr);
if(((this->m_way).GetCurSel())==1)
{
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
hostent* pHostent = gethostbyname(this->m_devicedn);
if(pHostent==NULL) return;
sockaddr_in sa;
memcpy(&sa.sin_addr.s_addr,pHostent->h_addr_list[0],pHostent->h_length);
CString sIP = inet_ntoa(sa.sin_addr);//IP
WSACleanup();
this->m_IPstr=sIP;
sprintf(inode->IP, "%s", sIP);
}
memcpy(inode->Name,m_devicename.GetBuffer(0),m_devicename.GetLength()+1);
memcpy(inode->UserNanme, m_deviceusername.GetBuffer(0), m_deviceusername.GetLength()+1);
//调用登录接口
// inode->LoginID = CLIENT_Login(inode->IP, (WORD)m_deviceport,
// m_deviceusername.GetBuffer(0), m_deviceuserpsw.GetBuffer(0), &inode->Info,&err);
inode->LoginID = CLIENT_LoginEx(inode->IP, (WORD)m_deviceport,
m_deviceusername.GetBuffer(0), m_deviceuserpsw.GetBuffer(0), 1, 0, &inode->Info,&err);
if(inode->LoginID > 0)
{
//登录成功,临时节点生效
//g_ptrdevicelist->AddTail(inode);
int r = CDevMgr::GetDevMgr().PushBack(inode);
if (r < 0)
{
AfxMessageBox("device's node add to list failed");
}
//开始侦听设备回调信息
BOOL ret = CLIENT_StartListen(inode->LoginID);
if(!ret)
{
((CNetSDKDemoDlg *)GetParent())->LastError();
MessageBox(MSG_ERROR_LISTEN);
}
//刷新内存和配置文件里的列表
bool bIsnew = true;
int counter;
CString strKey;
for (counter = 0; counter < m_ipsel.GetCount(); counter++)
{
CString strtemp;
m_ipsel.GetLBText(counter, strtemp);
if (strcmp(m_IPstr, g_userinfo[counter]->ip) == 0)
{
bIsnew = false;
UserInfo tmpnode;
strcpy (tmpnode.devicename, m_devicename.GetBuffer(0));
tmpnode.way=this->m_way.GetCurSel();
strcpy (tmpnode.ip, m_IPstr.GetBuffer(0));
strcpy (tmpnode.dn,this->m_devicedn.GetBuffer(0));
tmpnode.port = m_deviceport;
strcpy (tmpnode.username, m_deviceusername.GetBuffer(0));
strcpy (tmpnode.userpsw, m_deviceuserpsw.GetBuffer(0));
strKey.Format("%d", counter);
BOOL ret = WritePrivateProfileStruct("UserInfo" , strKey , (LPVOID)&tmpnode, sizeof(UserInfo), strPath);
}
}
for(counter = 0; counter < m_ipsel.GetCount(); counter++)
{
CString str;
str.Format("%s",g_userinfo[counter]->devicename);
if(str==_T("Deleted"))
{
bIsnew = false;
UserInfo tmpnode;
strcpy (tmpnode.devicename, m_devicename.GetBuffer(0));
tmpnode.way=this->m_way.GetCurSel();
strcpy (tmpnode.ip, m_IPstr.GetBuffer(0));
strcpy (tmpnode.dn,this->m_devicedn.GetBuffer(0));
tmpnode.port = m_deviceport;
strcpy (tmpnode.username, m_deviceusername.GetBuffer(0));
strcpy (tmpnode.userpsw, m_deviceuserpsw.GetBuffer(0));
strKey.Format("%d", counter);
BOOL ret = WritePrivateProfileStruct("UserInfo" , strKey , (LPVOID)&tmpnode, sizeof(UserInfo), strPath);
break;
}
}
//新的节点
if (bIsnew)
{
if (counter >= 10)
{
counter = 9;
}
UserInfo tmpnode;
strcpy (tmpnode.devicename, m_devicename.GetBuffer(0));
tmpnode.way=this->m_way.GetCurSel();
strcpy (tmpnode.ip, m_IPstr.GetBuffer(0));
strcpy (tmpnode.dn,this->m_devicedn.GetBuffer(0));
tmpnode.port = m_deviceport;
strcpy (tmpnode.username, m_deviceusername.GetBuffer(0));
strcpy (tmpnode.userpsw, m_deviceuserpsw.GetBuffer(0));
strKey.Format("%d", counter);
BOOL ret = WritePrivateProfileStruct("UserInfo" , strKey , (LPVOID)&tmpnode, sizeof(UserInfo), strPath);
}
for (int j = 0; j < 10; j++)
{
if (g_userinfo[j])
{
delete g_userinfo[j];
}
}
CDialog ::OnOK();
}
else
{
//登录失败的情况
((CNetSDKDemoDlg *)GetParent())->LastError();
GetConnectError(err);
delete inode;
}
}
//取消
void AddDeviceDlg::OnCancelAdddevice()
{
for (int j = 0; j < 10; j++)
{
if (g_userinfo[j])
{
delete g_userinfo[j];
}
}
CDialog::OnCancel();
}
//初始化页面
BOOL AddDeviceDlg::OnInitDialog()
{
g_SetWndStaticText(this);
CDialog::OnInitDialog();
this->m_way.SetCurSel(0);
//GetDlgItem(IDC_IPSELECTION)->EnableWindow(true);
//GetDlgItem(IDC_DEVICE_DN)->EnableWindow(false);
UserInfo tmpinfo;
for (int i = 0; i < 10; i++)
{
g_userinfo[i] = NULL;
CString strKey,strDev;
strKey.Format("%d", i);
strPath = g_strWorkDir + "\\userinfo.ini";
if (GetPrivateProfileStruct("UserInfo", strKey, (LPVOID)&tmpinfo, sizeof(UserInfo), strPath))
{
strDev.Format("%s",tmpinfo.devicename);
if(strDev!=_T("Deleted"))
{
g_userinfo[i] = new UserInfo;
memcpy(g_userinfo[i], &tmpinfo, sizeof(UserInfo));
m_ipsel.InsertString(i,tmpinfo.ip);
}
}
}
if (m_ipsel.GetCount() > 0)
{
m_devicename.Format("%s", g_userinfo[0]->devicename);
m_deviceport = g_userinfo[0]->port;
m_deviceusername.Format("%s", g_userinfo[0]->username);
m_deviceuserpsw.Format("%s", g_userinfo[0]->userpsw);
if(g_userinfo[0]->way==1)
{
this->m_way.SetCurSel(1);
this->m_devicedn=g_userinfo[0]->dn;
//GetDlgItem(IDC_IPSELECTION)->EnableWindow(false);
GetDlgItem(IDC_DEVICE_DN)->EnableWindow(true);
}
UpdateData(FALSE);
m_ipsel.SetCurSel(0);
}
return TRUE;
}
void AddDeviceDlg::OnClose()
{
for (int j = 0; j < 10; j++)
{
if (g_userinfo[j])
{
delete g_userinfo[j];
}
}
CDialog::OnClose();
}
void AddDeviceDlg::OnSelchangeIpselection()
{
int i=m_ipsel.GetCurSel();
m_IPstr.Format("%s", g_userinfo[i]->ip);
m_devicename.Format("%s", g_userinfo[i]->devicename);
m_deviceport = g_userinfo[i]->port;
m_deviceusername.Format("%s", g_userinfo[i]->username);
m_deviceuserpsw.Format("%s", g_userinfo[i]->userpsw);
if(g_userinfo[i]->way==1)
{
this->m_way.SetCurSel(1);
this->m_devicedn.Format("%s",g_userinfo[i]->dn);
//GetDlgItem(IDC_IPSELECTION)->EnableWindow(false);
//GetDlgItem(IDC_DEVICE_DN)->EnableWindow(true);
}
UpdateData(FALSE);
}
void AddDeviceDlg::OnSelchangeWay()
{
// TODO: Add your control notification handler code here
/*
if((this->m_way.GetCurSel())==0)
{
GetDlgItem(IDC_IPSELECTION)->EnableWindow(true);
GetDlgItem(IDC_DEVICE_DN)->EnableWindow(false);
}
else if((this->m_way.GetCurSel())==1)
{
m_IPstr = _T("255.255.255.0");
GetDlgItem(IDC_IPSELECTION)->EnableWindow(false);
GetDlgItem(IDC_DEVICE_DN)->EnableWindow(true);
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -