////\uFEFF//DevList.cpp : \u5B9E\u73B0\u6587\u4EF6
////
//
#include "stdafx.h"
#include "NetSDKDemo.h"
#include "DevListDlg.h"
#include "NetSDKDLL.h"
#include "DllPlayer.h"
#include "DeviceAttributeDlg.h"
#include "NetSDKDemoDlg.h"
#include "RecordDlg.h"
#include "WndCtrl.h"
#include "DevConfig.h"
#include "VoiceCtrl.h"
#include "NetSDKLog.h"
#include "AddDevice.h"
#include "RemoteCfgDlg.h"
#include <MMSystem.h>
#include <process.h>
#include <vector>
using namespace std;
#define WM_TREE_STATUS 10001
// CDevListDlg \u5BF9\u8BDD\u6846
struct NetSatateEvent
{
	int iEvent;
	CString strDescribe;
};

static const int g_iMaxEvent = 51;
static NetSatateEvent g_strEvent[51] =
{
	{EVENT_CONNECTING, _T("\u8FDE\u63A5\u4E2D")},
	{EVENT_CONNECTOK, _T("\u8FDE\u63A5\u6210\u529F")},
	{EVENT_CONNECTFAILED, _T("\u8FDE\u63A5\u5931\u8D25")},
	{EVENT_SOCKETERROR, _T("sock\u5931\u8D25")},
	{EVENT_LOGINOK, _T("\u767B\u5F55\u6210\u529F")},
	{EVENT_LOGINFAILED, _T("\u767B\u5F55\u5931\u8D25")},
	{EVENT_STARTAUDIOOK, _T("\u5BF9\u8BB2\u542F\u52A8\u6210\u529F")},
	{EVENT_STARTAUDIOFAILED, _T("\u5BF9\u8BB2\u542F\u52A8\u5931\u8D25")},
	{EVENT_STOPAUDIOOK, _T("\u505C\u6B62\u5BF9\u8BB2\u6210\u529F")},
	{EVENT_STOPAUDIOFAILED, _T("\u505C\u6B62\u5BF9\u8BB2\u5931\u8D25")},
	{EVENT_SENDPTZOK, _T("\u53D1\u9001\u4E91\u53F0\u63A7\u5236\u547D\u4EE4\u6210\u529F")},
	{EVENT_SENDPTZFAILED, _T("\u53D1\u9001\u4E91\u53F0\u63A7\u5236\u547D\u4EE4\u5931\u8D25")},
	{EVENT_SENDAUXOK, _T("\u53D1\u9001\u8F85\u52A9\u901A\u9053\u6210\u529F")},
	{EVENT_SENDAUXFAILED, _T("\u53D1\u9001\u8F85\u52A9\u901A\u9053\u5931\u8D25")},
	{EVENT_UPLOADOK, _T("\u4E0A\u4F20\u6587\u4EF6\u6210\u529F")},
	{EVENT_UPLOADFAILED, _T("\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25")},
	{EVENT_DOWNLOADOK , _T("\u4E0B\u8F7D\u6210\u529F")},
	{EVENT_DOWNLOADFAILED , _T("\u4E0B\u8F7D\u5931\u8D25")},
	{EVENT_REMOVEOK , _T("\u5220\u9664\u6587\u4EF6\u6210\u529F")},
	{EVENT_REMOVEFAILED , _T("\u5220\u9664\u6587\u4EF6\u5931\u8D25")},
	{EVENT_SENDPTZERROR , _T("\u4E91\u53F0\u64CD\u4F5C\u5931\u8D25")},
	{EVENT_PTZPRESETINFO, _T("\u9884\u7F6E\u70B9\u4FE1\u606F")},
	{EVNET_PTZNOPRESETINFO, _T("\u6CA1\u6709\u9884\u7F6E\u70B9")},
	{EVENT_PTZALARM, _T("\u544A\u8B66\u4FE1\u606F")},
	{EVENT_RECVVIDEOPARAM, _T("\u89C6\u9891\u53C2\u6570\u8FD4\u56DE")},
	{EVENT_RECVAUDIOPARAM, _T("\u97F3\u9891\u53C2\u6570\u8FD4\u56DE")},
	{EVENT_CONNECTRTSPERROR, _T("\u8BF7\u6C42\u5B9E\u65F6\u89C6\u9891\u5931\u8D25")},
	{EVENT_CONNECTRTSPOK, _T("\u8BFB\u6C42\u5A92\u4F53\u6D41\u6210\u529F")},
	{EVENT_RTSPTHREADEXIT, _T("\u5A92\u4F53\u6D41\u7EBF\u7A0B\u9000\u51FA")},
	{EVENT_URLERROR, _T("\u89E3\u91CA\u8FDE\u63A5\u5A92\u4F53\u6D41URL\u65F6\u5931\u8D25")},
	{EVENT_RECVVIDEOAUDIOPARAM, _T("\u6536\u5230\u97F3\u89C6\u9891\u53C2\u6570")},
	{EVENT_LOGIN_USERERROR, _T("\u767B\u5F55\u7528\u6237\u6216\u5BC6\u7801\u9519\u8BEF")},
	{EVENT_LOGOUT_FINISH, _T("\u767B\u5F55\u7EBF\u7A0B\u5DF2\u9000\u51FA\u4E3B\u5FAA\u73AF")},
	{EVENT_LOGIN_RECONNECT, _T("\u8FDB\u884C\u91CD\u65B0\u767B\u5F55")},
	{EVENT_LOGIN_HEARTBEAT_LOST, _T("\u5FC3\u8DF3\u4E22\u5931")},
	{EVENT_STARTAUDIO_ISBUSY, _T("\u8BBE\u5907\u5BF9\u8BB2\u88AB\u5360\u7528")},
	{EVENT_STARTAUDIO_PARAMERROR, _T("\u5BF9\u8BB2\u97F3\u9891\u53C2\u6570\u9519\u8BEF")},
	{EVENT_STARTAUDIO_AUDIODDISABLED, _T("\u8BBE\u5907\u4E0D\u652F\u6301\u5BF9\u8BB2")},
	{EVENT_CONNECT_RTSPSERVER_ERROR, _T("\u8FDE\u63A5\u9519\u8BEF\uFF0C\u53EF\u80FD\u662FIP\u6216\u7AEF\u53E3\u9519\u8BEF")},
	{EVENT_CREATE_RTSPCLIENT_ERROR, _T("\u521B\u5EFArtsp\u5BA2\u6237\u7AEF\u9519\u8BEF")},
	{EVENT_GET_RTSP_CMDOPTION_ERROR, _T("\u8FDE\u63A5rtsp\u670D\u52A1\u5668\uFF0C\u8FD4\u56DE\u9519\u8BEF")},
	{EVENT_RTSP_AUTHERROR, _T("\u8BA4\u8BC1\u5931\u8D25\uFF0C\u53EF\u80FD\u662F\u7528\u6237\u6216\u5BC6\u7801\u9519\u8BEF")},
	{EVNET_RECORD_FILEBEGIN, _T("\u5F00\u59CB\u5F55\u4E00\u4E2A\u6587\u4EF6")},
	{EVENT_RECORD_FILEEND, _T("\u5F55\u5B8C\u4E00\u4E2A\u6587\u4EF6")},
	{EVENT_RECORD_TASKEND, _T("\u6240\u6709\u5F55\u50CF\u5DF2\u5B8C\u6210\uFF0C\u5F55\u50CF\u5C06\u505C\u6B62")},
	{EVENT_RECORD_DISKFREESPACE_TOOLOW, _T("\u786C\u76D8\u53EF\u7528\u7A7A\u95F4\u592A\u5C0F,\u6B64\u65F6\u5E76\u4E0D\u4F1A\u8FDB\u5165\u5F55\u50CF\uFF0C\u800C\u4E00\u76F4\u5728\u7B49\u5F85\u6709\u8DB3\u591F\u7684\u7A7A\u95F4")},
	{EVNET_RECORD_FILEBEGIN_ERROR, _T("\u521B\u5EFA\u6587\u4EF6\u65F6\u51FA\u9519")},
	{EVNET_RECORD_WRITE_FILE_ERROR, _T("\u5199\u5165\u6587\u4EF6\u65F6\u51FA\u9519")},
	{EVENT_RECORD_INITAVIHEAD_ERROR, _T("\u5199\u5165\u6587\u4EF6\u5934\u65F6\u51FA\u9519")},
	{EVENT_RECORD_MEDIA_PARAM_ERROR, _T("\u89C6\u9891\u53C2\u6570\u9519\u8BEF\uFF0C\u53EF\u80FD\u662F\u89C6\u9891\u6D41\u8BF7\u6C42\u5931\u8D25")},
	{-1, NULL}
};

// int  CALLBACK  OnMediaDataRecv(long nPort,char * pBuf,long nSize,FRAME_INFO * pFrameInfo, void * pUser,long nReserved2)
// {
// 	TRACE("nSize  = %d\n", nSize);
// 	return 0;
// }



extern CNetSDKDemoApp theApp;
IMPLEMENT_DYNAMIC(CDevListDlg, CDialog)

CDevListDlg::CDevListDlg(unsigned int iW, unsigned int iH,CWnd* pParent /*=NULL*/)
	: CDialog(CDevListDlg::IDD, pParent)
{
	m_rcClient  =  CRect(0, 0, iW, iH);
	m_lLastIPCCount = 0;
	m_hLoopThread = NULL;
	m_uiLoopThreadID = 0;
	m_iWidth = 0;
	m_iHeight = 0; 
	m_pImageList = NULL;
	m_hMSGProc = NULL;
	m_hLoopWnd = NULL;
	m_iElapse = 0;
	InitializeCriticalSection(&m_csDevListEvent);
	InitializeCriticalSection(&m_csSectionTItem);
}

CDevListDlg::~CDevListDlg()
{	
	if (m_hLoopThread)
	{
		PostThreadMessage(m_uiLoopThreadID, WM_QUIT, 0, 0);
		if (WaitForSingleObject(m_hLoopThread, 200) == WAIT_TIMEOUT)
			TerminateThread(m_hLoopThread, -1);
		CloseHandle(m_hLoopThread);
		m_hLoopThread = NULL;
		m_uiLoopThreadID = 0;
	}

	PostThreadMessage(m_uiMSGProc, WM_QUIT, 0, 0);
	if (WaitForSingleObject(m_hMSGProc, 200) == WAIT_TIMEOUT)
		TerminateThread(m_hMSGProc, -1);
	CloseHandle(m_hMSGProc);
	m_hMSGProc = NULL;
	m_uiMSGProc = 0;

	SAFE_DELETE(m_pImageList);

	KillTimer(WM_TIMEID);

	DeleteCriticalSection(&m_csDevListEvent);
	DeleteCriticalSection(&m_csSectionTItem);
	
	DestroyWindow();
}


void CDevListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE_DEVLIST, m_ctlDevList);
}


BEGIN_MESSAGE_MAP(CDevListDlg, CDialog)
	ON_NOTIFY(NM_RCLICK, IDC_TREE_DEVLIST, &CDevListDlg::OnNMRclickTreeDevlist)
	ON_WM_CREATE()
	ON_NOTIFY(NM_DBLCLK, IDC_TREE_DEVLIST, &CDevListDlg::OnNMDblclkTreeDevlist)
	ON_COMMAND(ID_MENU1_32775_OPENVIDEO, &CDevListDlg::OnMenu132775Openvideo)
	ON_COMMAND(ID_MENU1_32776_CLOSEVIDEO, &CDevListDlg::OnMenu132776Closevideo)
	ON_WM_TIMER()
	ON_COMMAND(ID_MENU1_32789_AUTORECONNECT, &CDevListDlg::OnMenu132789Autoreconnect)
	ON_NOTIFY(NM_CLICK, IDC_TREE_DEVLIST, &CDevListDlg::OnNMClickTreeDevlist)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_DEVLIST, &CDevListDlg::OnTvnSelchangedTreeDevlist)
	ON_COMMAND(ID_MENU1_START_CTRL, &CDevListDlg::OnMenu1StartCtrl)
	ON_COMMAND(ID_MENU1_STOP_CTRL, &CDevListDlg::OnMenu1StopCtrl)
	ON_COMMAND(ID_MENU1_32838_SUBSTREAM, &CDevListDlg::OnMenu32831Substream)
	ON_COMMAND(ID_MENU1_32837_MAINSTREAM, &CDevListDlg::OnMenu32830Mainstream)
	ON_COMMAND(ID_MENU1_32844_DELETE, &CDevListDlg::OnMenu132844Delete)
	ON_COMMAND(ID_MENU1_32852_ADDDVR, &CDevListDlg::OnMenu132852Adddvr)
	ON_COMMAND(ID_MENU1_32854_TCP, &CDevListDlg::OnMenu132854Tcp)
	ON_COMMAND(ID_MENU_REM_CFG, &CDevListDlg::OnMenuRemCfg)
END_MESSAGE_MAP()


// CDevListDlg \u6D88\u606F\u5904\u7406\u7A0B\u5E8F

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

	// TODO:  \u5728\u6B64\u6DFB\u52A0\u989D\u5916\u7684\u521D\u59CB\u5316

	m_ctlDevList.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
	
	int   cx=GetSystemMetrics(SM_CXHSCROLL); 
	m_ctlDevList.MoveWindow(1, 1, m_rcClient.Width()-5, m_rcClient.Height()-5);
	

	TVINSERTSTRUCT tvInsert;
	tvInsert.hParent = NULL;
	tvInsert.hInsertAfter = NULL;
	tvInsert.item.mask = TVIF_TEXT;
	tvInsert.item.pszText = _T("Device List");
	
	m_hDevice = m_ctlDevList.InsertItem(&tvInsert);
	m_ctlDevList.Expand(m_hDevice,TVE_EXPAND);
	
	CBitmap *pBitmap;   
	m_pImageList=new  CImageList();
	m_pImageList->Create(16, 16, ILC_MASK, 0, 16);

	pBitmap = new  CBitmap;
	pBitmap->LoadBitmap(IDB_BITMAP1);

	m_pImageList->Add(pBitmap, RGB(255,0,255));
	m_ctlDevList.SetImageList(m_pImageList, TVSIL_NORMAL);
	m_ctlDevList.SetItemImage(m_hDevice,0,0);

	delete pBitmap;
	
	m_menu.LoadMenu(IDR_MENU1);
	
	m_hMSGProc = (HANDLE)_beginthreadex(NULL, 0, DevListMsgProc, this, 0, &m_uiMSGProc);


	return TRUE;  // return TRUE unless you set the focus to a control
	// \u5F02\u5E38: OCX \u5C5E\u6027\u9875\u5E94\u8FD4\u56DE FALSE
}

unsigned int __stdcall CDevListDlg::DevListMsgProc(void *pUser)
{
	CDevListDlg *pDevListDlg = (CDevListDlg *)pUser;
	
	while (1)
	{
		int iImagePos = 2, iLoginStatus = 0, iOpenStream = 0, iVideoChannel = 0;
		BOOL bDVSDevice = FALSE;
		HTREEITEM hTreeItem = NULL;
		HTREEITEM hSubItem = NULL;
		MSG msg;
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
				break;
		}

		CSDKEvent *pEvent = theApp.m_MSGDevList.Event_RemoveHead();
		if (pEvent == NULL)
		{
			Sleep(100);
			continue;
		}
		
		for (int i=0; i<g_iMaxEvent; i++)
		{
			if (g_strEvent[i].iEvent == pEvent->nStateCode)
			{
				//TRACE("%s\n", g_strEvent[i].strDescribe);
				break;
			}
		}
		
		switch(pEvent->nStateCode)
		{
		case EVENT_LOGOUT_FINISH:
			{
				CMapAutoLock Lock;
				POSITION pos = theApp.m_DevList.GetStartPosition();
				while (pos)
				{
					CString strKey;
					CDevInfo *pDevInfo = NULL;
					theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
					if (pDevInfo->iUserID == pEvent->lUser)
					{
						hSubItem = pDevInfo->hSubItem;
						hTreeItem = pDevInfo->hTreeItem;
						pDevInfo->iLoginStatus = 0;
						pDevInfo->iUserID = 0;
						break;
					}
				}
			}
			
			if (hSubItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hSubItem, 2,2);
			else if (hTreeItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 2,2);
			break;
// 		case EVENT_CONNECTFAILED:
// 		case EVENT_SOCKETERROR:
		case EVENT_LOGINFAILED:
			{
				CMapAutoLock Lock;
				POSITION pos = theApp.m_DevList.GetStartPosition();
				while (pos)
				{
					CString strKey;
					CDevInfo *pDevInfo = NULL;
					theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
					if (pDevInfo->iUserID == pEvent->lUser)
					{
						hSubItem = pDevInfo->hSubItem;
						hTreeItem = pDevInfo->hTreeItem;
						pDevInfo->iLoginStatus = -1;
						pDevInfo->iUserID = 0;
						break;
					}
				}
			}
			if (hSubItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hSubItem, 2,2);
			else if (hTreeItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 2,2);
			break;
		case EVENT_LOGINOK:
			{
				CMapAutoLock Lock;
				POSITION pos = theApp.m_DevList.GetStartPosition();
				while (pos)
				{
					CString strKey;
					CDevInfo *pDevInfo = NULL;
					theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
					if (pDevInfo->iUserID == pEvent->lUser)
					{
						hSubItem = pDevInfo->hSubItem;
						hTreeItem = pDevInfo->hTreeItem;
						pDevInfo->iLoginStatus = 1;
						iOpenStream = pDevInfo->iOpenStream;

						pDevInfo->iLoginStatus = 1;
						break;
					}
				}
			}
			if (iOpenStream > 0)	break;
			if (hSubItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hSubItem, 3,3);
			else if (hTreeItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 3,3);
			break;
		case EVENT_CONNECTRTSPOK:
			{
				CMapAutoLock Lock;
				POSITION pos = theApp.m_DevList.GetStartPosition();
				while (pos)
				{
					CString strKey;
					CDevInfo *pDevInfo = NULL;
					theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
					if (pDevInfo->lRealHandle == pEvent->lUser)
					{
						hSubItem = pDevInfo->hSubItem;
						hTreeItem = pDevInfo->hTreeItem; 
						pDevInfo->iOpenStream = 1; 
						break;
					}
				}
			}
			if (hSubItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hSubItem, 7,7);
			else if (hTreeItem)
				pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 7,7);
			break;
		case EVENT_CONNECT_RTSPSERVER_ERROR:
		case EVENT_CREATE_RTSPCLIENT_ERROR:
		case EVENT_CONNECTRTSPERROR:
		case EVENT_GET_RTSP_CMDOPTION_ERROR:
		case EVENT_RTSP_AUTHERROR:
			{
				CMapAutoLock Lock;
				POSITION pos = theApp.m_DevList.GetStartPosition();
				while (pos)
				{
					CString strKey;
					CDevInfo *pDevInfo = NULL;
					theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
					if (pDevInfo->lRealHandle == pEvent->lUser)
					{
						hSubItem = pDevInfo->hSubItem;
						hTreeItem = pDevInfo->hTreeItem;
						pDevInfo->iOpenStream = -1;
						pDevInfo->iOpenAudio = 0;
						iLoginStatus = pDevInfo->iLoginStatus;
						iOpenStream = pDevInfo->iOpenStream;
						break;
					}
				}
			}
			if (hSubItem)
				hTreeItem = hSubItem;
			
			if (hTreeItem)
			{
				if (iOpenStream <= 0 && iLoginStatus > 0)
					pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 6,6);
				else if (iOpenStream <= 0 && iLoginStatus <= 0)
					pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 2,2);
				else 
					pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 7,7);
			}
			break;
		case EVENT_RTSPTHREADEXIT:
			{
				CMapAutoLock Lock;
				POSITION pos = theApp.m_DevList.GetStartPosition();
				while (pos)
				{
					CString strKey;
					CDevInfo *pDevInfo = NULL;
					theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
					if (pDevInfo->lRealHandle == pEvent->lUser)
					{
						hSubItem = pDevInfo->hSubItem;
						hTreeItem = pDevInfo->hTreeItem;
						pDevInfo->iOpenStream = -1;
						pDevInfo->iOpenStream = 0; //\u53D6\u6D41\u5931\u8D25
						pDevInfo->iOpenAudio = 0;
						pDevInfo->lRealHandle = 0;
						//pDevInfo->hRealPlayWnd = NULL;
						iOpenStream = pDevInfo->iOpenStream;
						iLoginStatus = pDevInfo->iLoginStatus;
						break;
					}
				}
			}
			if (hSubItem)
				hTreeItem = hSubItem;
			if (hTreeItem)
			{
				if (iLoginStatus > 0)
					pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 3,3);
				else
					pDevListDlg->m_ctlDevList.SetItemImage(hTreeItem, 2,2);
			}
			
			break;
		}
		SAFE_DELETE(pEvent);
	}

	OutputDebugString("DevListMsgProc exit!!!\n");
	return 0;
}

typedef vector<HTREEITEM> VEC_TREE_ITEM;

void ExpandAllTreeNodes(CTreeCtrl *pTree, HTREEITEM hRoot, BOOL bExpand, VEC_TREE_ITEM& in_out_vec)
{
	UINT nCode = bExpand?TVE_EXPAND:TVE_COLLAPSE;

	if (NULL == hRoot)
	{
		// \u53EF\u80FD\u6709\u591A\u4E2A\u6839\u8282\u70B9
		hRoot = pTree->GetRootItem();
		while(NULL != hRoot)
		{
			ExpandAllTreeNodes(pTree, hRoot, bExpand, in_out_vec);
			hRoot = pTree->GetNextItem(hRoot, TVGN_NEXT);
		}
		return;
	}

	HTREEITEM hSon = NULL;
	pTree->Expand(hRoot,nCode);  

	// \u904D\u5386\u5B50\u8282\u70B9
	hSon = pTree->GetNextItem(hRoot,TVGN_CHILD);
	if (NULL == hSon)
	{
		in_out_vec.push_back(hRoot);
	}
	while(NULL != hSon)
	{
		//   pTree->Expand(hSon, nCode);
		TRACE("cur:%s,Expand son:%s\n", pTree->GetItemText(hRoot), pTree->GetItemText(hSon));
		ExpandAllTreeNodes(pTree, hSon, bExpand, in_out_vec);   
		hSon = pTree->GetNextItem(hSon,TVGN_NEXT);
	}
}

BOOL CDevListDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u4E13\u7528\u4EE3\u7801\u548C/\u6216\u8C03\u7528\u57FA\u7C7B
	if (pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == 13)
			return TRUE;
		if (pMsg->wParam == 27)
		{
			::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN, 27, 0);
			return TRUE;
		}
	}
	else if (pMsg->message == WM_IP_MODIFY)
	{
		return TRUE;
	}
	else if (WM_TREE_STATUS == pMsg->message )
	{
		EnterCriticalSection(&m_csSectionTItem);
		HTREEITEM hTr = m_lsTreeItem.RemoveHead();
		int iCount = m_lsTreeItem.GetCount();
		LeaveCriticalSection(&m_csSectionTItem);
		if (hTr)
		{
			m_ctlDevList.SetItemImage(hTr, 2, 2);
		}
		if (iCount > 0)
			PostMessage(WM_TREE_STATUS, 0, 0);
		return TRUE;
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CDevListDlg::OnNMRclickTreeDevlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u63A7\u4EF6\u901A\u77E5\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	*pResult = 0;
	CPoint point;
	GetCursorPos(&point);

	CPoint pointInTree = point;

	m_ctlDevList.ScreenToClient(&pointInTree);

	CPoint pt;
	UINT nFlags;
	::GetCursorPos(&pt);
	::ScreenToClient(m_ctlDevList.m_hWnd, &pt);

	HTREEITEM hItem= m_ctlDevList.HitTest(pt, &nFlags);
	if (hItem == m_hDevice) 
	{

		m_menu.EnableMenuItem( ID_MENU1_START_CTRL,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_STOP_CTRL,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32844_DELETE,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32775_OPENVIDEO,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32776_CLOSEVIDEO,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32789_AUTORECONNECT,MF_GRAYED);

		m_menu.EnableMenuItem(ID_MENU1_32838_SUBSTREAM, MF_GRAYED);

		m_menu.EnableMenuItem(ID_MENU1_32837_MAINSTREAM, MF_GRAYED);
			
		m_menu.EnableMenuItem(ID_MENU1_32854_TCP, MF_GRAYED);

		m_menu.EnableMenuItem(ID_MENU1_32852_ADDDVR, MF_ENABLED);

		m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_UNCHECKED | MF_BYCOMMAND|MF_GRAYED);
		//m_ctlDevList.SelectItem(item);

		m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | 

			TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
		return ;
	}
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);
	
	BOOL bFound = FALSE;
	BOOL bAutoConnect = FALSE, bSubStream = TRUE, bTCPConnect = FALSE;
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			bAutoConnect = pDevInfo->bAutoReconnect;
			bSubStream = pDevInfo->bSubStream; 
			bTCPConnect = pDevInfo->bTCPConnect;
		}
	}
	
	if (!bFound)
	{
		AfxMessageBox("\u672A\u627E\u5230\u8BE5\u8BBE\u5907!!!");
		return ;
	}
	

	HTREEITEM item;

	UINT flag = TVHT_ONITEM;

	item = m_ctlDevList.HitTest(pointInTree, &flag);
	
	if(item != NULL && item != m_ctlDevList.GetFirstVisibleItem())

	{
		
		m_menu.EnableMenuItem( ID_MENU1_START_CTRL,MF_ENABLED);

		m_menu.EnableMenuItem( ID_MENU1_STOP_CTRL,MF_ENABLED);

		m_menu.EnableMenuItem( ID_MENU1_32844_DELETE,MF_ENABLED);

		m_menu.EnableMenuItem( ID_MENU1_32775_OPENVIDEO,MF_ENABLED);

		m_menu.EnableMenuItem( ID_MENU1_32776_CLOSEVIDEO,MF_ENABLED);

		m_menu.EnableMenuItem( ID_MENU1_32789_AUTORECONNECT,MF_ENABLED);
		
		m_menu.EnableMenuItem(ID_MENU1_32838_SUBSTREAM, MF_ENABLED);

		m_menu.EnableMenuItem(ID_MENU1_32837_MAINSTREAM, MF_ENABLED);

		m_menu.EnableMenuItem(ID_MENU1_32852_ADDDVR, MF_ENABLED);

		m_menu.EnableMenuItem(ID_MENU1_32854_TCP, MF_ENABLED);

		if (bAutoConnect)
			m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_CHECKED | MF_BYCOMMAND);
		else	
			m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_UNCHECKED | MF_BYCOMMAND);

		if (bTCPConnect)
			m_menu.CheckMenuItem(ID_MENU1_32854_TCP, MF_CHECKED | MF_BYCOMMAND);
		else	
			m_menu.CheckMenuItem(ID_MENU1_32854_TCP, MF_UNCHECKED | MF_BYCOMMAND);

		if (bSubStream)
		{
			m_menu.CheckMenuItem(ID_MENU1_32838_SUBSTREAM, MF_CHECKED | MF_BYCOMMAND);
			m_menu.CheckMenuItem(ID_MENU1_32837_MAINSTREAM, MF_UNCHECKED | MF_BYCOMMAND);
		}
		else
		{
			m_menu.CheckMenuItem(ID_MENU1_32838_SUBSTREAM, MF_UNCHECKED | MF_BYCOMMAND);
			m_menu.CheckMenuItem(ID_MENU1_32837_MAINSTREAM, MF_CHECKED | MF_BYCOMMAND);
		}
		m_ctlDevList.SelectItem(item);

		m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | 

			TPM_RIGHTBUTTON, point.x, point.y, this, NULL);

	}

	else
	{
		m_menu.EnableMenuItem( ID_MENU1_START_CTRL,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_STOP_CTRL,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32844_DELETE,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32775_OPENVIDEO,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32776_CLOSEVIDEO,MF_GRAYED);

		m_menu.EnableMenuItem( ID_MENU1_32789_AUTORECONNECT,MF_GRAYED);
		
		m_menu.EnableMenuItem(ID_MENU1_32838_SUBSTREAM, MF_GRAYED);

		m_menu.EnableMenuItem(ID_MENU1_32837_MAINSTREAM, MF_GRAYED);

		m_menu.EnableMenuItem(ID_MENU1_32852_ADDDVR, MF_ENABLED);
			
		m_menu.EnableMenuItem(ID_MENU1_32854_TCP, MF_GRAYED);

		if (bSubStream)
		{
			m_menu.CheckMenuItem(ID_MENU1_32838_SUBSTREAM, MF_CHECKED | MF_BYCOMMAND);
			m_menu.CheckMenuItem(ID_MENU1_32837_MAINSTREAM, MF_UNCHECKED | MF_BYCOMMAND);
		}
		else
		{
			m_menu.CheckMenuItem(ID_MENU1_32838_SUBSTREAM, MF_UNCHECKED | MF_BYCOMMAND);
			m_menu.CheckMenuItem(ID_MENU1_32837_MAINSTREAM, MF_CHECKED | MF_BYCOMMAND);
		}
		if (bAutoConnect)
			m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_CHECKED | MF_BYCOMMAND);
		else	
			m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_UNCHECKED | MF_BYCOMMAND);
		
		if (bTCPConnect)
			m_menu.CheckMenuItem(ID_MENU1_32854_TCP, MF_CHECKED | MF_BYCOMMAND);
		else	
			m_menu.CheckMenuItem(ID_MENU1_32854_TCP, MF_UNCHECKED | MF_BYCOMMAND);

		m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | 

			TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
	}
}

int CDevListDlg::LogoutMedia(LPCTSTR strKey)
{
	// \u5173\u95ED\u63A7\u5236\u8FDE\u63A5
	int iUserID = 0;

	CMapAutoLock MapLock;
	CDevInfo *pDevInfo = NULL;
	BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
	if (bFound)
	{
		iUserID = pDevInfo->iUserID;
		pDevInfo->ReInit();
	}
		

	if (iUserID != 0)
	{
		IP_NET_DVR_Logout(iUserID);
		TRACE("------------------------------IP_NET_DVR_Logout............\n");
	}

	return 0;
}

int	CDevListDlg::ChangeState(CString strKey)
{
	return 0;
}

void CDevListDlg::TravelItem(HTREEITEM hItem, int iStatus, char *pStrIP, BOOL bChild)     
{     
	HTREEITEM hCurItem = m_ctlDevList.GetChildItem(hItem);  
	HTREEITEM hNextItem;  
	while(hCurItem)  
	{  
		CString strItemText = m_ctlDevList.GetItemText(hCurItem);
		if (strItemText.Compare(pStrIP) == 0)
		{
			if (m_ctlDevList.ItemHasChildren(hCurItem))
			{
				hNextItem = hCurItem;  

				TravelItem(hNextItem, iStatus, pStrIP, TRUE);  
			}
			else
			{	
				if (iStatus == 1
					|| iStatus == 3)
				{
					m_ctlDevList.SetItemImage(hCurItem, 3, 3);
				}
				
				else if (iStatus == 2)
				{
					m_ctlDevList.SetItemImage(hCurItem, 10, 10);
				}
				else if (iStatus == -2)
					m_ctlDevList.SetItemImage(hCurItem, 6, 6);
				else
					m_ctlDevList.SetItemImage(hCurItem, 2, 2);
			}
		}
		
		else if (bChild && iStatus == 0)
		{
			m_ctlDevList.SetItemImage(hCurItem, 3, 3);
		}
		
		hCurItem = m_ctlDevList.GetNextSiblingItem(hCurItem);  
	}  
} 

HTREEITEM CDevListDlg::FindTreeItem(HTREEITEM item,CString& strText)   
{   
	HTREEITEM   hFind;   
	if(item == NULL)   
		return  NULL;  
	
	while(item!=NULL)   
	{   
		if(m_ctlDevList.GetItemText(item) == strText)   
			return item;  

		if(m_ctlDevList.ItemHasChildren(item))   
		{   
			item = m_ctlDevList.GetChildItem(item);   
			hFind = FindTreeItem(item,strText);   
			if(hFind)   
			{   
				return hFind;   
			}   
			else   
				item= m_ctlDevList.GetNextSiblingItem(m_ctlDevList.GetParentItem(item));   
		}   
		else   
		{   
			item = m_ctlDevList.GetNextSiblingItem(item);   
			if(item == NULL)   
				return NULL;   
		}   
	}  

	return   item;   
}

void CDevListDlg::DClickTree()
{
	NMHDR nmhdr = {0};
	long lResult = 0;
	OnNMDblclkTreeDevlist(&nmhdr, &lResult);
	return;
}
extern BOOL g_isLoop;
extern HWND g_LoopWnd;
long CDevListDlg::playDevi(CString& strKey)
{
	HWND hWnd = ((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndManager->GetCurSelWnd();

	if (NULL != hWnd)
	{
 		if (g_isLoop && g_LoopWnd == hWnd)
 		{
 			g_isLoop = FALSE;
 		}
		
		return playDevi(strKey, hWnd);
	}

	return -1;
}

long CDevListDlg::playDevi(CString& strKey, HWND hWnd)
{
	CString userKey;
	((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndManager->GetCurWndUser(userKey);
	HWND hCurWnd = ((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndManager->GetCurSelWnd();
	if (!userKey.IsEmpty() && hCurWnd == hWnd)
	{
		if (0 != CloseMedia(userKey))
		{
			return -1;
		}
	}

	CDevInfo *pDevInfo = NULL;
	BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
	
	if (!bFound || NULL == pDevInfo)
	{
		AfxMessageBox("\u672A\u627E\u5230\u6307\u5B9A\u8BBE\u5907!!!");
		return -1;
	}

	if (strstr(pDevInfo->m_IPCEntry.deviceType, "DEC") != NULL)
	{
		if (!g_isLoop || g_LoopWnd != hWnd)
		{
			AfxMessageBox("解码设备，不能播放!");
		}
		return -2;
	}

	if (pDevInfo->iUserID <= 0 || pDevInfo->iLoginStatus <= 0)
	{
		long lRet = LoginDevi(pDevInfo);
		if (lRet != 0)
		{
			return -1;
		}
	}

	if (NULL != pDevInfo->lRealHandle || pDevInfo->lPlayerID > 0)
	{
		CloseMediaInfo(pDevInfo);
	}
	
	pDevInfo->hRealPlayWnd = hWnd;
	if (NULL == hWnd)
	{
		AfxMessageBox("NULL == hWnd");
	}
	((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndManager->SetWndUser(hWnd, strKey);


	IPC_ENTRY DevEntry = {0};
	memcpy(&DevEntry, &pDevInfo->m_IPCEntry, sizeof(IPC_ENTRY));
	char *pStrFindIPC = strstr(DevEntry.deviceType, "IPC");
	char *pStrFindNVS = strstr(DevEntry.deviceType, "NVS");
	char *pStrFindDVS = strstr(DevEntry.deviceType, "DVS");
	char *pStrFindNVR = strstr(DevEntry.deviceType, "NVR");
	long lVideoChannel = 0;
	if (pStrFindIPC || pStrFindNVS)
	{
		if (pDevInfo->bSubStream)
			lVideoChannel = 1;// \u5B50\u7801\u6D41
		else
			lVideoChannel = 0;// \u4E3B\u7801\u6D41
	}
	else if ( pStrFindDVS)
	{
		CString strIP = strKey;
		if (strcmp(strIP.Right(7), "Stream1") == 0)
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x00;
		}
		else if (strcmp(strIP.Right(7), "Stream2") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x01;
		}
		else if (strcmp(strIP.Right(7), "Stream3") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x02;
		}
		else if (strcmp(strIP.Right(7), "Stream4") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x03;
		}
		else if (strcmp(strIP.Right(7), "Stream5") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x04;
		}
		else 
			return INVALIDATE_DEV;
	}
	else if (pStrFindNVR)
	{
		lVideoChannel = 0;
		lVideoChannel = (2 << 16);


		long SubStreamTmp = pDevInfo->bSubStream == 1 ? 1 : 0;
		lVideoChannel |= (SubStreamTmp << 8);
		
		long lChannelTmp = atol(strKey.Right(2).GetBuffer(0));
		lVideoChannel |= lChannelTmp;

		strcpy(DevEntry.lanCfg.IPAddress, "tps://");
		strcat(DevEntry.lanCfg.IPAddress, pDevInfo->m_IPCEntry.lanCfg.IPAddress);
		
	}
	else
	{
		return INVALIDATE_DEV; // \u8BE5\u8BBE\u5907\u65E0\u6CD5\u8FDB\u884C\u89C6\u9891\u64AD\u653E
	}

	USRE_VIDEOINFO user;
	memset(&user, 0x00, sizeof(USRE_VIDEOINFO));
	user.bIsTcp = 1; 
	user.nVideoChannle = lVideoChannel;
	user.nVideoPort = DevEntry.streamCfg.videoPort;
	//user.pUserData = this;
	user.pUserData = (void*)pDevInfo;


	//DevEntry.lanCfg.IPAddress;
	long lRealHandle = IP_NET_DVR_RealPlayEx(pDevInfo->iUserID ,DevEntry.lanCfg.IPAddress, 
											DevEntry.userCfg.accounts[0].userName, 
											DevEntry.userCfg.accounts[0].password, RealDataCallBackFuncDevi, &user, NULL);
	if(lRealHandle == 0)
	{
		return -2;
	}
		
	pDevInfo->lRealHandle = lRealHandle;
	m_pDevInfo = pDevInfo;

	IP_NET_DVR_SetAutoReconnect(lRealHandle, FALSE);

	
	((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->ReflashTreeImage();
	return 0;
}

long	CDevListDlg::LoginDevi(CString& strKey)
{
	CDevInfo *pDevInfo = NULL;
	BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);

	if (!bFound || NULL == pDevInfo)
	{
		AfxMessageBox("\u672A\u627E\u5230\u6307\u5B9A\u8BBE\u5907!!!");
		return -1;
	}

	return  LoginDevi(pDevInfo);
	
}


long CDevListDlg::LoginDevi(CDevInfo* inDevInfo)
{

	CDevInfo* pDevInfo = inDevInfo;
	if (NULL == pDevInfo)
	{
		return -1;
	}

// 	if (pDevInfo->isNVR == 2)
// 	{
// 		pDevInfo = pDevInfo->pNVRParent;
// 	}


	long lUserID = IP_NET_DVR_Login(
		pDevInfo->m_IPCEntry.lanCfg.IPAddress, 
		pDevInfo->m_IPCEntry.streamCfg.ptzPort,
		pDevInfo->m_IPCEntry.userCfg.accounts[0].userName,
		pDevInfo->m_IPCEntry.userCfg.accounts[0].password,
		NULL);

	if (lUserID <= 0)
	{
		pDevInfo->iUserID = 0;
		return -2;
	}

	pDevInfo->iUserID = lUserID;
	

	if (pDevInfo->isNVR == 0 && WaitForSingleObject(g_EVENT_LOGIN_STATUS, 1000) == WAIT_TIMEOUT)
	{
		pDevInfo->iUserID = 0;
		pDevInfo->iLoginStatus = 0;
		return -3;
	}

	return 0;
}


int CDevListDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  \u5728\u6B64\u6DFB\u52A0\u60A8\u4E13\u7528\u7684\u521B\u5EFA\u4EE3\u7801

	return 0;
}

/*\u53CC\u51FB\u64AD\u653E*/
void CDevListDlg::OnNMDblclkTreeDevlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u63A7\u4EF6\u901A\u77E5\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801Z
	*pResult = 0;

	CPoint   pt;   
	GetCursorPos(&pt);   
	m_ctlDevList.ScreenToClient(&pt);
	HTREEITEM   hTreeItemSelected   =   m_ctlDevList.HitTest(pt);
	//m_ctlDevList.Select(hTreeItemSelected, TVGN_DROPHILITE);

	HTREEITEM hItem = hTreeItemSelected;
	//HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) 
	{
		return;
	}

	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
	{
		strKey = m_ctlDevList.GetItemText(hItem);
	}
		
	playDevi(strKey);
	//((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->ReflashTreeImage();
	
	return;
}

//////////////////////////////////////////////////////////////////////////
// Return: 0 -- \u8BBE\u5907\u767B\u9646\u6210\u529F
//         1 -- \u8BBE\u5907\u5DF2\u7ECF\u8FDE\u63A5
//		   -1 -- \u767B\u9646\u5931\u8D25\uFF01\u53EF\u80FD\u539F\u56E0\u5982\u4E0B\uFF1A\n1\u3001	szDVRIP\u4E3A\u7A7A\n2\u3001	wDVRPort\u4E0D\u57281-65535\u4E4B\u95F4\uFF08\u5373\u975E\u6CD5\u7AEF\u53E3\uFF09\u3002\n3\u3001	\u6B64IP\u548C\u7AEF\u53E3\u5DF2\u7ECF\u767B\u5F55\u8FC7\u4E00\u6B21\uFF0C\u91CD\u590D\u767B\u5F55\u3002
//         -2 -- \u672A\u627E\u5230\u6307\u5B9A\u8BBE\u5907
int CDevListDlg::LoginMedia(LPCTSTR strKey)
{
	IPC_ENTRY DevEntry;

	BOOL bFound = FALSE;
	int lUserID = 0;

	CDevInfo *pDevInfo = NULL;
	{
		CMapAutoLock MapLock;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			lUserID = pDevInfo->iUserID;
			memcpy(&DevEntry, &pDevInfo->m_IPCEntry, sizeof(IPC_ENTRY));
		}

	}
	

	if (pDevInfo->iLoginStatus == 1) return IN_WORKING;//\u8BBE\u5907\u5DF2\u7ECF\u767B\u5F55


	for (int i=0; i<DevEntry.userCfg.count; i++)
	{
		if ( (strcmp(DevEntry.userCfg.accounts[i].group, "Administrator") == 0)
			&& (strcmp(DevEntry.userCfg.accounts[i].status, "Enable") == 0)) 
		{
			lUserID = IP_NET_DVR_Login(
				DevEntry.lanCfg.IPAddress, 
				DevEntry.streamCfg.ptzPort,
				DevEntry.userCfg.accounts[i].userName,
				DevEntry.userCfg.accounts[i].password,
				NULL);
			if (lUserID > 0)
			{
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.Lookup(strKey, pDevInfo);
				
				if (NULL != pDevInfo)
				{
					pDevInfo->iUserID = lUserID;
				}
				
			} 
			OutputDebugString("Func: IP_NET_DVR_Login ......\n");
			break;
		}
	}
	
	if (lUserID == 0) 
		return OPEN_FAILED; // \u767B\u9646\u5931\u8D25
	

	return OPEN_OK;
}

/*\u542F\u52A8\u64AD\u653E\u6D41*/
void CDevListDlg::OnMenu132775Openvideo()
{

	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);
	
	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);

	playDevi(strKey);
	ReflashTreeImage();
}

void CDevListDlg::OnMenu132776Closevideo()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);


	CloseMedia(strKey);
	return;
}

LONG __stdcall CDevListDlg::RealDataCallBackFunc(LONG lRealHandle,DWORD dwDataType,
									BYTE *pBuffer,DWORD dwBufSize,LPFRAME_EXTDATA  pExtData)
{
	CDevListDlg *pDevListDlg = (CDevListDlg *)pExtData->pUserData;
	static long s_LastPlayerID = -1;
	if (dwDataType == 0)
	{
		// Video
		long lPlayerID = 0;
		long lInputStreamHandle = 0;

		{
			CDevInfo *pDevInfo = NULL;
			CMapAutoLock MapLock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while(pos)
			{
				CDevInfo *pDevInfo = NULL;
				CString strKey;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
				if (pDevInfo->lRealHandle == lRealHandle)
				{
					lPlayerID = pDevInfo->lPlayerID;
					lInputStreamHandle = pDevInfo->lInputStreamHandle;
					break;
				}
			}
		}
	
		if (lPlayerID != 0 )
		{
			long lInputErr = IP_TPS_InputVideoData(lPlayerID, pBuffer, dwBufSize, pExtData->bIsKey, pExtData->timestamp);
			if (lInputErr != 0) TRACE("IP_TPS_InputVideoData failed!!!!!\n");
		}
		

		if (lInputStreamHandle != 0)
		{
			long lInputErr = IP_NET_DVR_InputRecordStream(lInputStreamHandle, pBuffer, dwBufSize, TRUE, pExtData->bIsKey, pExtData->timestamp);
			if (lInputErr != 0)	TRACE("IP_NET_DVR_InputRecordStream failed!!!\n");
		}

	}
	
	else if(dwDataType==1)
	{
		// Audio
		long lPlayerID = 0;
		int iPlaySound = 0;
		CString strKey;
		long lInputStreamHandle = 0;
		{
			CMapAutoLock MapLock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
				if (pDevInfo->lRealHandle == lRealHandle)
				{
					lPlayerID = pDevInfo->lPlayerID;
					iPlaySound = pDevInfo->iOpenAudio;
					lInputStreamHandle = pDevInfo->lInputStreamHandle;
					break;
				}
			}
		}
	
		if ((lPlayerID > 0) && (iPlaySound > 0))
		{
			if (iPlaySound == 1)
			{		
				IP_TPS_PlaySound(lPlayerID);
				iPlaySound = 2;
				CDevInfo *pDevInfo = NULL;
				CMapAutoLock Lock;
				BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
				if (bFound)
				{	
					pDevInfo->iOpenAudio = iPlaySound;
				}
			}
			long lInputErr = IP_TPS_InputAudioData(lPlayerID, pBuffer, dwBufSize, pExtData->timestamp);
			if (lInputErr != 0) TRACE("IP_TPS_InputAudioData failed  lInputErr = %d!!!!!\n", lInputErr);
			
		}

		if (lInputStreamHandle != 0)
		{
			long lInputErr = IP_NET_DVR_InputRecordStream(lInputStreamHandle, pBuffer, dwBufSize, FALSE, pExtData->bIsKey, pExtData->timestamp);
			if (lInputErr != 0)	TRACE("IP_NET_DVR_InputRecordStream failed!!!\n");
		}
	}
	else if(dwDataType==2)
	{
		STREAM_AV_PARAM *pavParam = (STREAM_AV_PARAM *)pBuffer;

		BOOL bNew = TRUE;
		CString strKey;
		long lPlayerID = 0;
		HWND hWnd = NULL;
		{
			CMapAutoLock Lock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);

				if (pDevInfo->lRealHandle == lRealHandle)
				{
					lPlayerID = pDevInfo->lPlayerID;
					hWnd = pDevInfo->hRealPlayWnd;
					break;
				}
			}
		}
		
		IP_TPS_StopSound();
		IP_TPS_Stop(lPlayerID);

		srand( (unsigned)time( NULL ) );
		int RANGE_MIN = 1;
		int RANGE_MAX = 9999;
		lPlayerID = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
			
		if (pavParam->bHaveVideo)
		{
			int isAudioParam = 0;
			int nBufPoolSize = 40;
			long lRet = IP_TPS_OpenStream(lPlayerID, (BYTE *)&(pavParam->videoParam), sizeof(VIDEO_PARAM), isAudioParam, nBufPoolSize);
			if (lRet != 0)
			{
				TRACE("Video IP_TPS_OpenStream faild!!!\n"); 
				return 0;
			}
		}

		if (pavParam->bHaveAudio)
		{
			int isAudioParam = 1;
			int nBufPoolSize = 40;
			long lRet = IP_TPS_OpenStream(lPlayerID, (BYTE *)&(pavParam->audioParam), sizeof(AUDIO_PARAM), isAudioParam, nBufPoolSize);
			if (lRet != 0)
				TRACE("Audio IP_TPS_OpenStream faild!!!\n");
		}
		
		if (strKey.GetLength() > 0)
		{
			CMapAutoLock MapLock;
			CDevInfo *pDevInfo = NULL;		
			BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
			if (bFound)
			{
				pDevInfo->lPlayerID = lPlayerID;
				SAFE_FREE(pDevInfo->pAVParam);
				pDevInfo->pAVParam = (char *)malloc(dwBufSize);
				memcpy(pDevInfo->pAVParam, pavParam, dwBufSize);
				//m_nAVParamLen  = dwBufSize;
			}
		}
		if (hWnd != NULL && ::IsWindow(hWnd))
		{
			int iRetPlay = IP_TPS_Play(lPlayerID, hWnd);
		}
		
	}
	return 0;
}


LONG __stdcall CDevListDlg::RealDataCallBackFuncDevi(LONG lRealHandle,DWORD dwDataType,
												 BYTE *pBuffer,DWORD dwBufSize,LPFRAME_EXTDATA  pExtData)
{
	CDevInfo *pDevInfo = (CDevInfo *)pExtData->pUserData;
	if (pDevInfo->lRealHandle <= 0)
	{
		return 0;
	}

	if (dwDataType == 0)
	{
		if (pDevInfo->lPlayerID != 0 )
		{
			
			long lInputErr = IP_TPS_InputVideoData(pDevInfo->lPlayerID, pBuffer, dwBufSize, pExtData->bIsKey, pExtData->timestamp);
			
			if (lInputErr != 0) 
			{
				TRACE("IP_TPS_InputVideoData failed!!!!!\n");
			}
			pDevInfo->iAlive = 2;

		}


		if (pDevInfo->lInputStreamHandle != 0)
		{
			
			long lInputErr = IP_NET_DVR_InputRecordStream(pDevInfo->lInputStreamHandle, pBuffer, dwBufSize, TRUE, pExtData->bIsKey, pExtData->timestamp);
			
			if (lInputErr != 0)	TRACE("IP_NET_DVR_InputRecordStream failed!!!\n");
		}

	}

	else if(dwDataType==1)
	{
		if ((pDevInfo->lPlayerID > 0) && (pDevInfo->iOpenAudio > 0))
		{
			if (pDevInfo->iOpenAudio == 1)
			{		
				
				IP_TPS_PlaySound(pDevInfo->lPlayerID);
				
				pDevInfo->iOpenAudio = 2;

			}
			
			long lInputErr = IP_TPS_InputAudioData(pDevInfo->lPlayerID, pBuffer, dwBufSize, pExtData->timestamp);
			
			if (lInputErr != 0) TRACE("IP_TPS_InputAudioData failed  lInputErr = %d!!!!!\n", lInputErr);

		}

		if (pDevInfo->lInputStreamHandle != 0)
		{
			
			long lInputErr = IP_NET_DVR_InputRecordStream(pDevInfo->lInputStreamHandle, pBuffer, dwBufSize, FALSE, pExtData->bIsKey, pExtData->timestamp);
			
			if (lInputErr != 0)	
			{
				TRACE("IP_NET_DVR_InputRecordStream failed!!!\n");
				return 0;
			}
			pDevInfo->iAlive = 2;
		}
	}
	else if(dwDataType==2)
	{
		STREAM_AV_PARAM *pavParam = (STREAM_AV_PARAM *)pBuffer;

		if (pDevInfo->lPlayerID <= 0)
		{
			srand( (unsigned)time( NULL ) );
			int RANGE_MIN = 1;
			int RANGE_MAX = 9999;
			pDevInfo->lPlayerID = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
		}
		if (pavParam->bHaveVideo)
		{
			int isAudioParam = 0;
			int nBufPoolSize = 40;
			
			long lRet = IP_TPS_OpenStream(pDevInfo->lPlayerID, (BYTE *)&(pavParam->videoParam), sizeof(VIDEO_PARAM), isAudioParam, nBufPoolSize);
			
			if (lRet != 0)
			{
				TRACE("Video IP_TPS_OpenStream faild!!!\n"); 
				return 0;
			}
			pDevInfo->iAlive = 2;
		}
		if (pavParam->bHaveAudio)
		{
			int isAudioParam = 1;
			int nBufPoolSize = 40;
			
			long lRet = IP_TPS_OpenStream(pDevInfo->lPlayerID, (BYTE *)&(pavParam->audioParam), sizeof(AUDIO_PARAM), isAudioParam, nBufPoolSize);
			
			if (lRet != 0)
				TRACE("Audio IP_TPS_OpenStream faild!!!\n");
		}

		if (NULL == pDevInfo->pAVParam)
		{
			pDevInfo->pAVParam = (char *)malloc(dwBufSize);
			if (NULL == pDevInfo->pAVParam)
			{
				return -1;
			}
			
		}
		memcpy(pDevInfo->pAVParam, pavParam, sizeof(STREAM_AV_PARAM));

		if (pDevInfo->hRealPlayWnd != NULL && ::IsWindow(pDevInfo->hRealPlayWnd))
		{
			
			int iRetPlay = IP_TPS_Play(pDevInfo->lPlayerID, pDevInfo->hRealPlayWnd);
			
			pDevInfo->iAlive = 2;
		}
	}
	return 0;
}



//////////////////////////////////////////////////////////////////////////
// return 
//         0 \u6253\u5F00\u6210\u529F
//        -1 \u64AD\u653E\u5931\u8D25
//        -2 \u672A\u627E\u5230\u6307\u5B9A\u8BBE\u5907
//        -3 \u7A97\u53E3\u4E0D\u53EF\u7528
//        -4 \u7A97\u53E3\u88AB\u5360\u7528
//        -5 \u8BE5\u8BBE\u5907\u65E0\u6CD5\u8FDB\u884C\u89C6\u9891\u64AD\u653E
int CDevListDlg::OpenMediaEx(LPCTSTR strKey, HWND hPlayWnd)
{
	if (!IsWindow(hPlayWnd)) return WND_INVALIDATE;

	long lUserID = 0;
	long lRealHandle = 0;
	long lPlayerID = 0;
	IPC_ENTRY DevEntry;
	BOOL bSubStream = FALSE;
	{
		CDevInfo *pDevInfo = NULL;
		CMapAutoLock MapLock;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			lUserID = pDevInfo->iUserID;
			lRealHandle = pDevInfo->lRealHandle;
			lPlayerID = pDevInfo->lPlayerID;
			memcpy(&DevEntry, &pDevInfo->m_IPCEntry, sizeof(IPC_ENTRY));
			bSubStream = pDevInfo->bSubStream;
		}
	}
	
	if (lUserID == 0)   return NO_LOGIN; // \u8BBE\u5907\u672A\u767B\u5F55

	if (lRealHandle != 0)
	{
		IP_NET_DVR_StopRealPlay(lRealHandle);
		lRealHandle=0;
	}

 	if (lPlayerID > 0)
 	{
 		IP_TPS_Stop(lPlayerID);
 		lPlayerID = -1;
 	}

	//return IN_WORKING;

	char *pStrFindIPC = strstr(DevEntry.deviceType, "IPC");
	char *pStrFindNVS = strstr(DevEntry.deviceType, "NVS");
	char *pStrFindDVS = strstr(DevEntry.deviceType, "DVS");
	long lVideoChannel = 0;
	if (pStrFindIPC || pStrFindNVS)
	{
		if (bSubStream)
			lVideoChannel = 1;// \u5B50\u7801\u6D41
		else
			lVideoChannel = 0;// \u4E3B\u7801\u6D41
	}
	else if ( pStrFindDVS)
	{
		CString strIP = strKey;
		if (strcmp(strIP.Right(7), "Stream1") == 0)
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x00;
		}
		else if (strcmp(strIP.Right(7), "Stream2") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x01;
		}
		else if (strcmp(strIP.Right(7), "Stream3") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x02;
		}
		else if (strcmp(strIP.Right(7), "Stream4") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x03;
		}
		else if (strcmp(strIP.Right(7), "Stream5") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x04;
		}
		else 
			return INVALIDATE_DEV;
	}
	else
	{
		return INVALIDATE_DEV; // \u8BE5\u8BBE\u5907\u65E0\u6CD5\u8FDB\u884C\u89C6\u9891\u64AD\u653E
	}

	USRE_VIDEOINFO user;
	memset(&user, 0x00, sizeof(USRE_VIDEOINFO));
	user.bIsTcp = 0; 
	user.nVideoChannle = lVideoChannel;
	user.nVideoPort = DevEntry.streamCfg.videoPort;
	user.pUserData = this;
	
	for (int i=0; i<DevEntry.userCfg.count; i++)
	{
		if ( (strcmp(DevEntry.userCfg.accounts[i].group, "Administrator") == 0)
			&& (strcmp(DevEntry.userCfg.accounts[i].status, "Enable") == 0)) 
		{
			char szUrl[512];
			memset(szUrl, 0, 512);
			//sprintf(szUrl, "tps://%s",DevEntry.lanCfg.IPAddress);
			sprintf(szUrl, "%s",DevEntry.lanCfg.IPAddress);
			lRealHandle = IP_NET_DVR_RealPlayEx(lUserID,szUrl, 
				DevEntry.userCfg.accounts[i].userName,
				DevEntry.userCfg.accounts[i].password,
				RealDataCallBackFunc,
				&user,NULL);
			break;
		}
	}

	if (lRealHandle == 0)  return OPEN_FAILED; //\u64AD\u653E\u5931\u8D25

	{
		CMapAutoLock MapLock;
		CDevInfo *pDevInfo = NULL;
		theApp.m_DevList.Lookup(strKey, pDevInfo);
		pDevInfo->lRealHandle = lRealHandle;
		pDevInfo->hRealPlayWnd = hPlayWnd;
	}

	return OPEN_OK;
}
//////////////////////////////////////////////////////////////////////////
int CDevListDlg::OpenMedia(LPCTSTR strKey, HWND hPlayWnd)
{
	if (!IsWindow(hPlayWnd)) return WND_INVALIDATE;

	long lUserID = 0, lRealHandle = 0;
	int iDevType = -1,  iOpenStream = 0;
	IPC_ENTRY DevEntry;
	BOOL bSubStream = TRUE, bTCPConnect = TRUE;
	{
		CDevInfo *pDevInfo = NULL;
		CMapAutoLock MapLock;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			lUserID = pDevInfo->iUserID;
			lRealHandle = pDevInfo->lRealHandle;
			iDevType = pDevInfo->iDevType;
			bSubStream = pDevInfo->bSubStream;
			bTCPConnect = pDevInfo->bTCPConnect;
			memcpy(&DevEntry, &pDevInfo->m_IPCEntry, sizeof(IPC_ENTRY));
		}
	}
	
	if (lUserID == 0)   return NO_LOGIN; // \u8BBE\u5907\u672A\u767B\u5F55
	if (lRealHandle != 0 )	return IN_WORKING; //\u89C6\u9891\u5DF2\u5F00\u542F

	int iSubStream = 0;
	char *pStrFindIPC = strstr(DevEntry.deviceType, "IPC");
	char *pStrFindNVS = strstr(DevEntry.deviceType, "NVS");
	char *pStrFindDVS = strstr(DevEntry.deviceType, "DVS");
	long lVideoChannel = 0, lDevType = 0;
	
	if (pStrFindIPC || pStrFindNVS)
	{
		lDevType = 0;

		if (bSubStream)
		{
			lVideoChannel |= 0x01;
			iSubStream = 1;
		}
		else
		{
			lVideoChannel = 0;
			iSubStream = 0;
		}
	}
	else if ( pStrFindDVS)
	{
		CString strIP = strKey;
		if (strcmp(strIP.Right(7), "Stream1") == 0)
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x00;
		}
		else if (strcmp(strIP.Right(7), "Stream2") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x01;
		}
		else if (strcmp(strIP.Right(7), "Stream3") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x02;
		}
		else if (strcmp(strIP.Right(7), "Stream4") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x03;
		}
		else if (strcmp(strIP.Right(7), "Stream5") == 0)	
		{
			lVideoChannel |= 0xffff+0x01;
			lVideoChannel |= 0x04;
		}
		else 
			return INVALIDATE_DEV;
	}
	else
	{
		return INVALIDATE_DEV; // \u8BE5\u8BBE\u5907\u65E0\u6CD5\u8FDB\u884C\u89C6\u9891\u64AD\u653E
	}

	USRE_VIDEOINFO user;
	memset(&user, 0x00, sizeof(USRE_VIDEOINFO));
	user.bIsTcp = bTCPConnect; 
	user.nVideoChannle = lVideoChannel;
	user.nVideoPort = DevEntry.streamCfg.videoPort;
	user.pUserData = this;	
	lRealHandle = IP_NET_DVR_RealPlay(lUserID, NULL, RealDataCallBackFunc, &user, NULL);

	if (lRealHandle == 0) return OPEN_FAILED; //\u6253\u5F00\u89C6\u9891\u6D41\u5931\u8D25
	{
		CMapAutoLock MapLock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			pDevInfo->iDevType = lDevType;
			pDevInfo->lRealHandle = lRealHandle;
			pDevInfo->hRealPlayWnd = hPlayWnd;
			pDevInfo->iVideoChannle = iSubStream;
		}
	}

	return OPEN_OK;
}

void  CDevListDlg::PTZCtrl(PTZ_CMD_TYPE PtzCmd, int iPanSpeed, int iTiltSpeed)
{

	HWND hWnd =((CNetSDKDemoDlg *)theApp.m_pMainWnd)->GetCurSelWnd();
	if (hWnd)
	{
		CString StrKey = ((CNetSDKDemoDlg *)theApp.m_pMainWnd)->GetWndUser(hWnd);
		int iUserID = 0;
		{
			CDevInfo *pDevInfo = NULL;
			CMapAutoLock MapLock;
			BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
			if (bFound)
			{
				iUserID = pDevInfo->iUserID;
			}
		}
		
		if (iUserID != 0)
		{
			//IP_NET_DVR_PTZControl(iUserID, PtzCmd, iPanSpeed, iTiltSpeed);
			IP_NET_DVR_PTZControl(iUserID, PtzCmd, iTiltSpeed, iPanSpeed);
		}
	}
}

void CDevListDlg::PresetCtrl(PTZ_PRESET_TYPE PresetCmd, int iPresetNum)
{
	HWND hWnd = ((CNetSDKDemoDlg *)GetParent())->GetCurSelWnd();
	if (hWnd)
	{

		CString StrKey = ((CNetSDKDemoDlg *)GetParent())->GetWndUser(hWnd);
		int iUserID = 0;
		{
			CMapAutoLock MapLock;
			CDevInfo *pDevInfo = NULL;
			BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
			if (bFound)
			{
				iUserID = pDevInfo->iUserID;
			}
		}

		if(iUserID != 0)
		{
			IP_NET_DVR_PTZPreset(iUserID, PresetCmd, iPresetNum);
		}
	}
}

void CDevListDlg::XMLCtrl(char *pStrXML)
{
	HWND hWnd = ((CNetSDKDemoDlg *)GetParent())->GetCurSelWnd();
	if (hWnd)
	{
		CString StrKey = ((CNetSDKDemoDlg *)GetParent())->GetWndUser(hWnd);
		int iUserID = 0;
		{
			CMapAutoLock MapLock;
			CDevInfo *pDevInfo = NULL;
			BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
			if (bFound)
				iUserID = pDevInfo->iUserID;
		}

		if(iUserID != 0)
		{
			IP_NET_DVR_PTZControlEx(iUserID, pStrXML);
		}
	}
}

void CDevListDlg::OnMenu132789Autoreconnect()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	
	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);

	BOOL bFound = FALSE;
	int iUserID = 0;
	{
		CDevInfo *pDevInfo = NULL;
		CMapAutoLock Lock;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		iUserID = pDevInfo->iUserID;
	}

	if (!bFound)
	{
		AfxMessageBox("\u672A\u627E\u5230\u8BBE\u5907!!!");
		return ;
	}
	
	if (iUserID == 0)
	{
		AfxMessageBox("\u8BBE\u5907\u6CA1\u6709\u767B\u5F55!!!");
		return ;
	}

	MENUITEMINFO mItemInfo;

	UINT state = m_menu.GetMenuState(ID_MENU1_32789_AUTORECONNECT, MF_BYCOMMAND);
	if(state == 0xFFFFFFFF)
		return;
	
	BOOL bAutoConnect = TRUE;
	if (state & MF_CHECKED)
	{
		m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_UNCHECKED | MF_BYCOMMAND);
		IP_NET_DVR_SetAutoReconnect(iUserID, 0);
		bAutoConnect = FALSE;
	}
	else
	{
		m_menu.CheckMenuItem(ID_MENU1_32789_AUTORECONNECT, MF_CHECKED | MF_BYCOMMAND);
		IP_NET_DVR_SetAutoReconnect(iUserID, 1);
		bAutoConnect = TRUE; 
	}

	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		pDevInfo->bAutoReconnect = bAutoConnect;
	}

	TRACE("Func: IP_NET_DVR_SetAutoReconnect....\n");
}

long CDevListDlg::CloseMedia(LPCTSTR strKey)
{
	CDevInfo *pDevInfo = NULL;
	BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);

	if (bFound)
	{
		CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;
		HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();

		if (pDevInfo->m_pRemotePlayBack != NULL && pDevInfo->m_pRemotePlayBack->m_hWnd == hWnd) 
		{
			return ((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pRecordDlg->CloseMedia(strKey);
		}
		else
		{
			return CloseMediaInfo(pDevInfo);
		}
		
	}

	return -1;
}

long CDevListDlg::CloseMediaInfo(CDevInfo* pDevInfo)
{
	if (NULL == pDevInfo)
	{
		return -1;
	}

	if (pDevInfo->lRealHandle != 0)
	{
		//IP_NET_DVR_StopRealPlay(lRealHandle);

		ResetEvent(g_EVENT_STOP_MEDIA);
		long lRet = IP_NET_DVR_StopRealPlay(pDevInfo->lRealHandle);
		pDevInfo->lRealHandle = 0;
#if 0
		if (0 == lRet)
		{		
			if (WaitForSingleObject(g_EVENT_STOP_MEDIA, 2000) == WAIT_TIMEOUT)
			{
				return -2;
			}
			
		}
#endif
// 		else
// 		{
// 			return -3;
// 		}
		
	}

	pDevInfo->lRealHandle = 0;
	pDevInfo->iOpenStream = 0;
	pDevInfo->iAlive = 1;
	pDevInfo->iOpenAudio = 0;

	if (pDevInfo->lPlayerID > 0)
	{
		IP_TPS_Stop(pDevInfo->lPlayerID);
		pDevInfo->lPlayerID = 0;

// 		if (0 == IP_TPS_Stop(pDevInfo->lPlayerID)) 
// 		{
// 			pDevInfo->lPlayerID = 0;
// 		}
		
	}

	if (NULL != pDevInfo->hRealPlayWnd)
	{
		CNetSDKDemoDlg* pNetSDKDemoDlg = (CNetSDKDemoDlg*)theApp.m_pMainWnd;
		pNetSDKDemoDlg->SetWndUser(pDevInfo->hRealPlayWnd, NULL);
		pDevInfo->hRealPlayWnd = NULL;
	}


	ReflashTreeImage();

	return 0;
	//Sleep(200);
	
}

void CDevListDlg::ReflashTreeImage()
{
	static VEC_TREE_ITEM vecItem;
	vecItem.clear();
	ExpandAllTreeNodes(&m_ctlDevList, NULL, TRUE, vecItem);
	if (vecItem.size() <= 0)
	{
		return;
	}
	
	CString csTxt;
	CDevInfo* pDevInfo = NULL;
	for (int i = 0; i < vecItem.size(); ++i)
	{
		pDevInfo = NULL;
		csTxt = m_ctlDevList.GetItemText(vecItem[i]);



		if (-1 != csTxt.Find("Stream") || -1 != csTxt.Find("Channel"))
		{
			HTREEITEM parent = m_ctlDevList.GetParentItem(vecItem[i]);
			CString csTxtParent = m_ctlDevList.GetItemText(parent);
			csTxtParent += ':';
			csTxtParent += csTxt;

			csTxt = csTxtParent;
		}

		theApp.m_DevList.Lookup(csTxt, pDevInfo);
		if (NULL != pDevInfo)
		{
			//\u5728\u7EBF\u4E14\u767B\u9646
			if (pDevInfo->iLoginStatus == 1)
			{
				if (pDevInfo->iAlive == 2) //\u5B9E\u65F6\u89C6\u9891 \u7F6E\u7EA2
				{
					m_ctlDevList.SetItemImage(vecItem[i], 7, 7);
				}
				else//\u6CA1\u6709\u89C6\u9891\uFF0C\u7F6E\u7EFF
				{
					m_ctlDevList.SetItemImage(vecItem[i], 3, 3);
				}


			}
			else //\u5728\u7EBF \u4F46\u6CA1\u767B\u9646\uFF0C \u7F6E\u7070
			{
				m_ctlDevList.SetItemImage(vecItem[i], 2, 2);
			}

		}
		
	}


	return;
}

void CDevListDlg::OnNMClickTreeDevlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u63A7\u4EF6\u901A\u77E5\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
//	*pResult = 0;
	
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);

}

void CDevListDlg::OnTvnSelchangedTreeDevlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	// TODO: \u5728\u6B64\u6DFB\u52A0\u63A7\u4EF6\u901A\u77E5\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	*pResult = 0;
	
	CRecordDlg *pRecordDlg = ((CNetSDKDemoDlg *)GetParent())->m_pRecordDlg;
	CUpDownLoad *pUpDownLoad = ((CNetSDKDemoDlg *)GetParent())->m_pUpDownLoad;
	CVoiceCtrl *pVoiceCtrl = ((CNetSDKDemoDlg *)GetParent())->m_pVoiceCtrl;
	CDevConfig *pDevConfig = ((CNetSDKDemoDlg *)GetParent())->m_pDevConfig;
	CNetSDKLog *pNetSDKLog = ((CNetSDKDemoDlg *)GetParent())->m_pNetSDKLog;

	HTREEITEM hItem = m_ctlDevList.GetSelectedItem();
	if(hItem)
	{
		CString strIP = m_ctlDevList.GetItemText(pNMTreeView->itemNew.hItem);
		if (-1 != strIP.Find("Stream"))
		{
			HTREEITEM parent = m_ctlDevList.GetParentItem(pNMTreeView->itemNew.hItem);
			CString csTxtParent = m_ctlDevList.GetItemText(parent);
 			csTxtParent += ':';
 			csTxtParent += strIP;

			strIP = csTxtParent;
		}

		if (pRecordDlg)
			pRecordDlg->SetSelIP(strIP);
		if (pUpDownLoad)
			pUpDownLoad->SetSelIP(strIP);
		if (pVoiceCtrl)
			pVoiceCtrl->SetSelIP(strIP);
		if (pDevConfig)
		{
			pDevConfig->SetSelIP(strIP);
		}
		if (pNetSDKLog)
		{
			pNetSDKLog->SetSelIP(strIP);
		}
		
	}
}




void CDevListDlg::OnMenu1StartCtrl()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801

	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);
	
	LoginDevi(strKey);
	ReflashTreeImage();
	return;
#if 0
	BOOL bFound = FALSE;
	{
		CDevInfo *pDevInfo = NULL;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
	}

	if (!bFound) 
	{
		AfxMessageBox("\u6CA1\u6709\u627E\u5230\u8BBE\u5907!!!");
		return ;
	}

	int iLogin = LoginMedia(strKey);

	if (iLogin == IN_WORKING)
	{
		AfxMessageBox("\u8BBE\u5907\u5DF2\u7ECF\u767B\u5F55!!!");
	}
	return ;
#endif
}

void CDevListDlg::OnMenu1StopCtrl()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801

	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);


	CloseMedia(strKey);
	int iLogin = LogoutMedia(strKey);
	
	m_ctlDevList.SetItemImage(hItem, 2, 2);

	return ;
}

void CDevListDlg::OnMenu32831Substream()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	MENUITEMINFO mItemInfo;

	UINT state = m_menu.GetMenuState(ID_MENU1_32838_SUBSTREAM, MF_BYCOMMAND);
	if(state == 0xFFFFFFFF)
		return;

	BOOL bAutoConnect = TRUE;
	if (state & MF_CHECKED)
	{
	}
	else
	{
		HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
		BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
		if (bHasChild) return;
		HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

		CString strKey, strSubKey;
		if (hParent != m_hDevice)
		{
			strSubKey = m_ctlDevList.GetItemText(hItem);
			strKey = m_ctlDevList.GetItemText(hParent);
			strKey += (CString)_T(":")+strSubKey;
		}
		else
			strKey = m_ctlDevList.GetItemText(hItem);

		CString strDevType;

		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{	
			strDevType.Format("%s", pDevInfo->m_IPCEntry.deviceType);
			if (strcmp(strDevType.Left(3), "NVS") == 0 ||strcmp(strDevType.Left(3), "IPC") == 0 || strcmp(strDevType.Left(3), "NVR") == 0)
			{
				m_menu.CheckMenuItem(ID_MENU1_32838_SUBSTREAM, MF_CHECKED | MF_BYCOMMAND);
				m_menu.CheckMenuItem(ID_MENU1_32837_MAINSTREAM, MF_UNCHECKED | MF_BYCOMMAND);
				pDevInfo->bSubStream = TRUE;		
			}
		}
	}
}

void CDevListDlg::OnMenu32830Mainstream()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	MENUITEMINFO mItemInfo;

	UINT state = m_menu.GetMenuState(ID_MENU1_32837_MAINSTREAM, MF_BYCOMMAND);
	if(state == 0xFFFFFFFF)
		return;

	BOOL bAutoConnect = TRUE;
	if (state & MF_CHECKED)
	{
	}
	else
	{
		HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
		BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
		if (bHasChild) return;
		HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

		CString strKey, strSubKey;
		if (hParent != m_hDevice)
		{
			strSubKey = m_ctlDevList.GetItemText(hItem);
			strKey = m_ctlDevList.GetItemText(hParent);
			strKey += (CString)_T(":")+strSubKey;
		}
		else
			strKey = m_ctlDevList.GetItemText(hItem);


		CString strDevType;

		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{	
			strDevType.Format("%s", pDevInfo->m_IPCEntry.deviceType);
			if (strcmp(strDevType.Left(3), "NVS") == 0 || strcmp(strDevType.Left(3), "IPC") == 0 || strcmp(strDevType.Left(3), "NVR") == 0)
			{
				m_menu.CheckMenuItem(ID_MENU1_32838_SUBSTREAM, MF_UNCHECKED | MF_BYCOMMAND);
				m_menu.CheckMenuItem(ID_MENU1_32837_MAINSTREAM, MF_CHECKED | MF_BYCOMMAND);
				pDevInfo->bSubStream = FALSE;		
			}
		}
	}

}


BOOL CDevListDlg::ADDIPC(IPC_ENTRY &entry)
{	
	CTreeCtrl&	treeCtrl = ((CNetSDKDemoDlg*)theApp.m_pMainWnd)->m_pWndDevList->m_ctlDevList;

	if (FindTreeItem(treeCtrl.GetRootItem(), CString( entry.lanCfg.IPAddress)))
	{
		return FALSE;
	}

	HTREEITEM hTreeItem = NULL, hSubItem = NULL;
	if (strncmp(entry.deviceType, "DVS", 3) == 0)
	{
		CString strKey;
		hTreeItem = m_ctlDevList.InsertItem(TVIF_TEXT,entry.lanCfg.IPAddress, 0, 0, 0, 0, 0, m_hDevice, NULL);

		hSubItem = m_ctlDevList.InsertItem(_T("Stream1"), 2, 2, hTreeItem, TVI_SORT);
		{
			CDevInfo *pDevInfo = new CDevInfo(&entry);
			pDevInfo->hTreeItem = hTreeItem;
			pDevInfo->hSubItem = hSubItem;

			CMapAutoLock Lock;
			strKey.Format("%s:Stream1", entry.lanCfg.IPAddress);
			theApp.m_DevList.SetAt(strKey, pDevInfo);	
			
			LoginDevi(pDevInfo);
		}

		hSubItem = m_ctlDevList.InsertItem(_T("Stream2"), 2, 2, hTreeItem, TVI_SORT);
		{
			CDevInfo *pDevInfo = new CDevInfo(&entry);
			pDevInfo->hTreeItem = hTreeItem;
			pDevInfo->hSubItem = hSubItem;
			LoginDevi(pDevInfo);
			CMapAutoLock Lock;
			strKey.Format("%s:Stream2", entry.lanCfg.IPAddress);
			theApp.m_DevList.SetAt(strKey, pDevInfo);	
		}
	
		hSubItem = m_ctlDevList.InsertItem(_T("Stream3"), 2, 2, hTreeItem, TVI_SORT);
		{
			CDevInfo *pDevInfo = new CDevInfo(&entry);
			pDevInfo->hTreeItem = hTreeItem;
			pDevInfo->hSubItem = hSubItem;

			CMapAutoLock Lock;
			strKey.Format("%s:Stream3", entry.lanCfg.IPAddress);
			theApp.m_DevList.SetAt(strKey, pDevInfo);

			LoginDevi(pDevInfo);
		}

		hSubItem = m_ctlDevList.InsertItem(_T("Stream4"), 2, 2, hTreeItem, TVI_SORT);
		{
			CDevInfo *pDevInfo = new CDevInfo(&entry);
			pDevInfo->hTreeItem = hTreeItem;
			pDevInfo->hSubItem = hSubItem;

			CMapAutoLock Lock;
			strKey.Format("%s:Stream4", entry.lanCfg.IPAddress);
			theApp.m_DevList.SetAt(strKey, pDevInfo);

			LoginDevi(pDevInfo);	
		}

		hSubItem = m_ctlDevList.InsertItem(_T("Stream5"), 2, 2, hTreeItem, TVI_SORT);
		{
			CDevInfo *pDevInfo = new CDevInfo(&entry);
			pDevInfo->hTreeItem = hTreeItem;
			pDevInfo->hSubItem = hSubItem;

			CMapAutoLock Lock;
			strKey.Format("%s:Stream5", entry.lanCfg.IPAddress);
			theApp.m_DevList.SetAt(strKey, pDevInfo);

			LoginDevi(pDevInfo);	
		}
	}
	else if (strncmp(entry.deviceType, "NVR", 3) == 0)
	{
		CString strKey;
		CString ChannelFulName;
		CDevInfo * DevInfoTmp[16] = {0};
		hTreeItem = m_ctlDevList.InsertItem(TVIF_TEXT,entry.lanCfg.IPAddress, 0, 0, 0, 0, 0, m_hDevice, NULL);

		CDevInfo *pNVRSevice = new CDevInfo(&entry);
		pNVRSevice->isNVR = 1;
		theApp.m_DevList.SetAt(entry.lanCfg.IPAddress, pNVRSevice);
		//LoginDevi(pNVRSevice);
		for (int i = 1; i <= 16; i++)
		{
			strKey.Format("Channel_%02d", i);
			hSubItem = m_ctlDevList.InsertItem(strKey.GetBuffer(0), 2, 2, hTreeItem, TVI_SORT);
			
			CDevInfo *pNVRChannel = new CDevInfo(&entry);
			if (NULL == pNVRChannel)
			{
				continue;
			}
			pNVRChannel->hSubItem = hSubItem;
			pNVRChannel->isNVR = 2;
			
			ChannelFulName = CString(entry.lanCfg.IPAddress) + ":" + strKey;
			theApp.m_DevList.SetAt(ChannelFulName, pNVRChannel);

			DevInfoTmp[i-1] = pNVRChannel;
		}

		LoginDevi(pNVRSevice);

	}
	else
	{
		hTreeItem = m_ctlDevList.InsertItem(entry.lanCfg.IPAddress, 2, 2, m_hDevice, TVI_SORT);
		{
			CDevInfo *pDevInfo = new CDevInfo(&entry);
			pDevInfo->hTreeItem = hTreeItem;
			pDevInfo->hSubItem = hSubItem;

			CMapAutoLock Lock;
			theApp.m_DevList.SetAt(entry.lanCfg.IPAddress, pDevInfo);

			LoginDevi(pDevInfo);
		}
	}
	m_ctlDevList.Expand(m_hDevice, TVE_EXPAND);
	m_ctlDevList.Expand(hTreeItem, TVE_EXPAND);
	m_ctlDevList.SelectItem(hTreeItem);
	//((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->ReflashTreeImage();
	//UpdateWindow();
	return 0;
}
extern BOOL g_isLoop;
void CDevListDlg::OnMenu132844Delete()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	//if (m_hLoopThread != NULL)
	if (g_isLoop == TRUE)
	{
		AfxMessageBox("\u8BBE\u5907\u8F6E\u8BE2\u64AD\u653E\uFF0C\u8BF7\u52FF\u5220\u9664\uFF01\uFF01\uFF01");
		return ;
	}

	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	//删除NVR设备
	if (-1 != m_ctlDevList.GetItemText(hItem).Find("Channel_"))
	{

			HTREEITEM hNextItem;
			HTREEITEM hChildItem = m_ctlDevList.GetChildItem(hParent);
			CString parentName = m_ctlDevList.GetItemText(hParent);
			CDevInfo *pDevInfo = NULL;
			CString Name;

			while (hChildItem != NULL)
			{
				hNextItem = m_ctlDevList.GetNextItem(hChildItem, TVGN_NEXT);
				
				
				Name = parentName + ":" + m_ctlDevList.GetItemText(hChildItem);
				theApp.m_DevList.Lookup(Name, pDevInfo);
				if (NULL != pDevInfo && (pDevInfo->iOpenStream == 1 || pDevInfo->iAlive == 1 || pDevInfo->iAlive == 2))
				{
					CloseMedia(Name);
					Sleep(500);
				}
					
				theApp.m_DevList.RemoveKey(Name);
				m_ctlDevList.DeleteItem(hChildItem);
				hChildItem = hNextItem;
			}

			m_ctlDevList.DeleteItem(hParent);
			theApp.m_DevList.RemoveKey(parentName);
		return;
	}

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);


	BOOL bFound = FALSE;
	BOOL bOpenStream = FALSE;
	BOOL bDownLoad = FALSE;

	{
		CDevInfo *pDevInfo = NULL;
		CMapAutoLock Lock;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			(pDevInfo->iOpenStream==0)?bOpenStream=FALSE:bOpenStream=TRUE;
			(pDevInfo->iDownLoad==0)?bDownLoad=FALSE:bDownLoad=TRUE;
		}
	}
	
	if (bOpenStream)
	{
		AfxMessageBox("\u8BE5\u8BBE\u5907\u6B63\u5728\u4F7F\u7528\u4E2D, \u8BF7\u5173\u95ED\u5B9E\u65F6\u6D41!!!");
		return ;
	}
	if (bDownLoad)
	{
		AfxMessageBox("\u8BE5\u8BBE\u5907\u6B63\u5728\u4F7F\u7528\u4E2D\uFF0C \u8BF7\u5173\u95ED\u4E0B\u8F7D!!!");
		return ;
	}

	m_ctlDevList.DeleteItem(hItem);
	bHasChild = m_ctlDevList.ItemHasChildren(hParent);
	if (!bHasChild && hParent != m_hDevice)	m_ctlDevList.DeleteItem(hParent);
	
	{
		CDevInfo *pDevInfo = NULL;
		CMapAutoLock Lock;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			theApp.m_DevList.RemoveKey(strKey);
			SAFE_DELETE(pDevInfo);
		}
	}
}


unsigned int __stdcall LoopThreadProc(void *pUser);
BOOL g_isLoop = FALSE;
HWND g_LoopWnd = NULL;
void CDevListDlg::LoopPlay(BOOL bLoop, HWND hLoopWnd, INT IElapse)
{	
	if (bLoop)
	{
		m_iElapse =max(2, IElapse); 
		//if (m_hLoopThread)	return ;
		m_hLoopWnd = hLoopWnd;
		g_isLoop = TRUE;
		m_hLoopThread = (HANDLE)_beginthreadex(NULL, 0, LoopThreadProc, this, 0, &m_uiLoopThreadID);
	}
	else
	{
		g_isLoop = FALSE;
		return;
	}
}

void CDevListDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u6D88\u606F\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801\u548C/\u6216\u8C03\u7528\u9ED8\u8BA4\u503C
// 	switch(nIDEvent)
// 	{
// 	case WM_TIMER_UPGRADE:
// 		break;
// 	default:
// 		break;
// 	}

	CDialog::OnTimer(nIDEvent);
}

unsigned int __stdcall LoopThreadProc(void *pUser)
{
	CDevListDlg *pDevListDlg = (CDevListDlg *)pUser;
	if (pDevListDlg == NULL) return 0l;
	DWORD dwStartTime = 0;
	POSITION posStart = NULL;

	//CDevListDlg *m_pWndDevList
	CTreeCtrl* pTree = &((CNetSDKDemoDlg*)theApp.m_pMainWnd)->m_pWndDevList->m_ctlDevList;
	static VEC_TREE_ITEM vecItem;
	vecItem.clear();
	ExpandAllTreeNodes(pTree, NULL, TRUE, vecItem);

	if (vecItem.size() <= 0  ||
		(vecItem.size() == 1 && pTree->GetItemText(vecItem[0]) == CString("Device List")))
	{
		AfxMessageBox(_T("设备列表为空"));
		g_isLoop = FALSE;
		((CNetSDKDemoDlg*)theApp.m_pMainWnd)->m_pMenuDlg->m_ctlLoopBtn.SetWindowText("\u542F\u52A8\u8F6E\u8BE2");
		return -1;
	}

	CString csTxt;

	int i = vecItem.size();
	//NMHDR nmhdr = {0};
	//long lresult = 0;

	HWND hWnd = ((CNetSDKDemoDlg*)theApp.m_pMainWnd)->m_pWndManager->GetCurSelWnd();
	g_LoopWnd = hWnd;

	//HWND CWndManager::GetCurSelWnd()
	while(1)
	{
		i %= vecItem.size();

		pTree->SelectItem(vecItem[i]);

		if (pTree->ItemHasChildren(vecItem[i]))
		{
			i++;
			continue;
		}

		csTxt = pTree->GetItemText(vecItem[i]);

		if (-1 != csTxt.Find("Stream") || -1 != csTxt.Find("Channel"))
		{
#if 0
			HTREEITEM parent = pTree->GetParentItem(vecItem[i]);
			CString csTxtParent = pTree->GetItemText(parent);
			csTxtParent += ':'; 			
			csTxtParent += csTxt;

			csTxt = csTxtParent;
#endif
			i++;
			continue;
		}
		
		//pDevListDlg->playDevi(pTree->GetItemText(vecItem[i]), hWnd);
		pDevListDlg->playDevi(csTxt, hWnd);
		//pDevListDlg->DClickTree();

		
		DWORD beginTime = GetTickCount();

		while(GetTickCount() - beginTime < pDevListDlg->m_iElapse*1000)
		{
			Sleep(300);
			if (g_isLoop == FALSE)
			{
				//pDevListDlg->CloseMedia(csTxt);
				((CNetSDKDemoDlg*)theApp.m_pMainWnd)->m_pMenuDlg->m_ctlLoopBtn.SetWindowText("启动轮巡");
				g_LoopWnd = NULL;
				return 0;
			}
		}
		

		CString strkey;
		strkey = pTree->GetItemText(vecItem[i]);

		((CNetSDKDemoDlg*)theApp.m_pMainWnd)->m_pWndDevList->CloseMedia(strkey);
		Sleep(300);
		i++;
	}
	
	return 0;

#if 0
	while (1)
	{
		MSG  whMSG;
		if (PeekMessage(&whMSG, NULL, NULL, NULL, PM_REMOVE))
		{
			if (whMSG.message == WM_QUIT)
				break;
		}
		CString strKey = "";
		CDevInfo *pDevInfo = NULL;
		HTREEITEM hTree = NULL, hSub = NULL, hMain = NULL;
		{
			CMapAutoLock lock;
			if (posStart == NULL)	
				posStart = theApp.m_DevList.GetStartPosition();	
			else	
			{
				theApp.m_DevList.GetNextAssoc(posStart, strKey, pDevInfo);
				if (pDevInfo)
				{
					hSub = pDevInfo->hSubItem;
					hTree = pDevInfo->hTreeItem;
					if (hSub) hMain = hMain;
					else if (hTree) hMain = hTree;
				}
			}
		}
		if (pDevInfo == NULL)
		{
			Sleep(100);
			continue;
		}
		
		long lRetlogin = pDevListDlg->LoginMedia(strKey);
		if (lRetlogin == IN_WORKING) continue;
		if (lRetlogin != OPEN_OK)
		{
			pDevListDlg->LogoutMedia(strKey);
			continue;
		}
			
		int ret = pDevListDlg->OpenMedia(strKey, pDevListDlg->m_hLoopWnd);	
		if (ret != OPEN_OK)
		{
			pDevListDlg->LogoutMedia(strKey);
			CString strMsg;
			strMsg.Format("OpenMedia failed. DevIp=%s, ret=%d", strKey, ret);
			AfxMessageBox(strMsg);
			continue;
		}
		
		
		dwStartTime = timeGetTime();
		while (1)
		{
			MSG  whwhMSG;
			if (PeekMessage(&whwhMSG, NULL, NULL, NULL, PM_NOREMOVE))
			{
				if (whwhMSG.message == WM_QUIT)
				{
					pDevListDlg->CloseMedia(strKey);
					pDevListDlg->LogoutMedia(strKey);
					EnterCriticalSection(&pDevListDlg->m_csSectionTItem);
					pDevListDlg->m_lsTreeItem.AddTail(hMain);
					LeaveCriticalSection(&pDevListDlg->m_csSectionTItem);
					pDevListDlg->PostMessage(WM_TREE_STATUS, 0, 0);
					break;
				}
			}
			DWORD dwElaps = timeGetTime() - dwStartTime;
			DWORD dwE  = pDevListDlg->m_iElapse*1000;
			if (dwE > dwElaps)
			{
				Sleep(100);
			}
			else 
			{
				pDevListDlg->CloseMedia(strKey);
				pDevListDlg->LogoutMedia(strKey);
				EnterCriticalSection(&pDevListDlg->m_csSectionTItem);
				pDevListDlg->m_lsTreeItem.AddTail(hMain);
				LeaveCriticalSection(&pDevListDlg->m_csSectionTItem);
				pDevListDlg->PostMessage(WM_TREE_STATUS, 0, 0);
				break;
			}
		}
	}
#endif
	return 0l;
}
void CDevListDlg::OnMenu132852Adddvr()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	CAddDevice dlgAdd(this);
	
	dlgAdd.DoModal();
}

void CDevListDlg::OnMenu132854Tcp()
{
	// TODO: \u5728\u6B64\u6DFB\u52A0\u547D\u4EE4\u5904\u7406\u7A0B\u5E8F\u4EE3\u7801
	MENUITEMINFO mItemInfo;

	UINT state = m_menu.GetMenuState(ID_MENU1_32854_TCP, MF_BYCOMMAND);
	if(state == 0xFFFFFFFF)
		return;
	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);

	BOOL bAutoConnect = TRUE;
	if (state & MF_CHECKED)
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{	
			pDevInfo->bTCPConnect = FALSE;		
		}
	}
	else
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{	
			pDevInfo->bTCPConnect = TRUE;		
		}
	}
}

void CDevListDlg::OnMenuRemCfg()
{
	// TODO: \u8FDC\u7A0B\u914D\u7F6E\u8BBE\u5907

	HTREEITEM hItem= m_ctlDevList.GetSelectedItem();
	BOOL bHasChild = m_ctlDevList.ItemHasChildren(hItem);
	if (bHasChild) return;
	HTREEITEM hParent = m_ctlDevList.GetParentItem(hItem);

	CString strKey, strSubKey;
	if (hParent != m_hDevice)
	{
		strSubKey = m_ctlDevList.GetItemText(hItem);
		strKey = m_ctlDevList.GetItemText(hParent);
		strKey += (CString)_T(":")+strSubKey;
	}
	else
		strKey = m_ctlDevList.GetItemText(hItem);

	BOOL bFound = FALSE;	
	CDevInfo *pDevInfo = NULL;
	{
		CMapAutoLock Lock;
		bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
	}

	if (!bFound)
	{
		AfxMessageBox("\u672A\u627E\u5230\u6307\u5B9A\u8BBE\u5907!!!");
		return ;
	}
	if(pDevInfo)
	{
		CRemoteCfgDlg oRemCfgDlg(pDevInfo);
		oRemCfgDlg.DoModal();
	}
}
