// Real Client Demo vc6Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Real Client Demo vc6.h"
#include "Real Client Demo vc6Dlg.h"
#include "..\lib\launet.h"

#define OVERLAY_KEY	RGB(192,0,192)
#define WM_MYCOMMAND WM_USER+1

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

CHANNEL_CLIENTINFO pVSChann;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CRealClientDemoDlg dialog

CRealClientDemoDlg::CRealClientDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRealClientDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRealClientDemoDlg)
	m_sername = _T("video server");
	m_password = _T("888888");
	m_username = _T("888888");
	m_url = _T("192.168.1.122");
	m_port = 3000;
	m_channel = 0;
	m_trantype = 2;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hHandle = -1;
}

void CRealClientDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRealClientDemoDlg)
	DDX_Control(pDX, IDC_STATIC_Video, m_video);
	DDX_Text(pDX, IDC_EDIT_SERVERNAME, m_sername);
	DDX_Text(pDX, IDC_EDIT_PWD, m_password);
	DDX_Text(pDX, IDC_EDIT_USER, m_username);
	DDX_Text(pDX, IDC_EDIT_ADDR, m_url);
	DDX_Text(pDX, IDC_EDIT_PORT, m_port);
	DDX_CBIndex(pDX, IDC_COMBO_CHNL, m_channel);
	DDX_CBIndex(pDX, IDC_COMBO_CONNECTTYPE, m_trantype);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRealClientDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CRealClientDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BtnPlay, OnBtnPlay)
	ON_BN_CLICKED(IDC_BtnStop, OnBtnStop)
	ON_MESSAGE(WM_MYCOMMAND,OnMyCommand)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRealClientDemoDlg message handlers

BOOL CRealClientDemoDlg::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);
		}
	}
	VSNET_ClientStartup(WM_MYCOMMAND,m_hWnd);


	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CRealClientDemoDlg::OnBtnPlay() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	char sername[24];//={"videoServer"};
	char url[40];//={"192.168.1.122"};
	char user[20];//={"888888"};
	char password[20];//={"888888"};


	//CStringA strAsername(m_sername);
	//CStringA strAurl(m_url);
	//CStringA strAusername(m_username);
	//CStringA strApassword(m_password);
	
	sprintf(sername,"%s",m_sername);
	sprintf(url,"%s",m_url);
	sprintf(user,"%s",m_username);
	sprintf(password,"%s",m_password);
	
	pVSChann.m_hChMsgWnd  = NULL;
	pVSChann.m_nChmsgid   = 0;
	pVSChann.m_sername    = sername;
	pVSChann.m_username   = user;
	pVSChann.m_password   = password;
	pVSChann.m_playstart  = TRUE;
	pVSChann.m_tranType   = m_trantype + 1;
	pVSChann.m_useoverlay = FALSE;
	pVSChann.nColorKey    = OVERLAY_KEY;
	pVSChann.m_ch         = m_channel;
	pVSChann.m_buffnum    = 20;
	pVSChann.m_hVideohWnd = m_video.GetSafeHwnd();
	pVSChann.context      = NULL;
	pVSChann.m_messagecallback = NULL;
try
{
	if (m_hHandle != -1)
	{
		VSNET_ClientStop(m_hHandle);
	}
	m_hHandle = VSNET_ClientStart(url,&pVSChann,m_port);
	if (m_hHandle == -1)
	{
	  AfxMessageBox(_T("启动通道失败 ：\n句柄获取失败\n可能输入的IP不存在"));
	  return;
	}
	else
	{
	  VSNET_ClientSetWnd(m_hHandle,m_video.m_hWnd);
	  VSNET_ClientMediaData(m_hHandle,TRUE);
	  VSNET_ClientStartViewEx(m_hHandle);

	  GetDlgItem(IDC_BtnPlay)			->EnableWindow(FALSE);
	  GetDlgItem(IDC_BtnStop)			->EnableWindow(TRUE);
	  GetDlgItem(IDC_COMBO_CHNL)		->EnableWindow(FALSE);
	  GetDlgItem(IDC_COMBO_CONNECTTYPE) ->EnableWindow(FALSE);
	  GetDlgItem(IDC_EDIT_SERVERNAME)	->EnableWindow(FALSE);
	  GetDlgItem(IDC_EDIT_ADDR)			->EnableWindow(FALSE);
	  GetDlgItem(IDC_EDIT_PORT)			->EnableWindow(FALSE);
	  GetDlgItem(IDC_EDIT_USER)			->EnableWindow(FALSE);
	  GetDlgItem(IDC_EDIT_PWD)			->EnableWindow(FALSE);
	}

}
catch(...)
{
	  AfxMessageBox(_T("ERROR:启动通道失败"));
}
	
}

void CRealClientDemoDlg::OnBtnStop() 
{
	// TODO: Add your control notification handler code here
	if(!VSNET_ClientStop(m_hHandle))
		{
			AfxMessageBox(_T("尝试断开连接失败：VSNET_ClientStop()"));
			return;
		}
	UpdateData(true);
	GetDlgItem(IDC_BtnPlay)			 ->EnableWindow(TRUE);
	GetDlgItem(IDC_BtnStop)			 ->EnableWindow(FALSE);
	GetDlgItem(IDC_COMBO_CHNL)		 ->EnableWindow(TRUE);
	GetDlgItem(IDC_COMBO_CONNECTTYPE)->EnableWindow(TRUE);
    GetDlgItem(IDC_EDIT_SERVERNAME)  ->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_ADDR)		 ->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_PORT)	 	 ->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_USER)		 ->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_PWD)		 ->EnableWindow(TRUE);
	
}

LRESULT CRealClientDemoDlg::OnMyCommand(WPARAM wParam, LPARAM lParam)
{
	int nStart = 0 ,count = 0;
	char sername[24];
	char url[40];
	WORD port;
	DWORD m_wParam,m_lParam;
	int m_ch;
//	int i;
	if(!VSNET_ClientReadMessage(sername,url,&port,&m_ch,&m_wParam,&m_lParam)) return TRUE;
	
	/*
	if(m_wParam == LAUMSG_LINKMSG)
	{
		if(m_lParam == 0) //连接成功
		{
			
			TRACE("连接服务器%s,url:%s,port:%d,通道:%d,成功\n",sername,url,port,m_ch);
			//char str[50];
			//sprintf(str,"连接服务器%s,url:%s,port:%d,通道:%d,成功",sername,url,port,m_ch);
			//AfxMessageBox(str);
			VSNET_ClientPlayAudio(-1);
			VSNET_ClientSetDelayTime(-1,5);
		
			//if(!GetDlgItem(IDC_BTN_TRANSMIT_START)->IsWindowEnabled())
			//	VSNET_ClientMediaData(s_ChanSock[m_ch],FALSE);
		}
		else if(m_lParam == 2)
		{
			TRACE("连接服务器%s,url:%s,port:%d,通道:%d,失败\n",sername,url,port,m_ch);
			//char str[50];
			//sprintf(str,"连接服务器%s,url:%s,port:%d,通道:%d,失败",sername,url,port,m_ch);
			//AfxMessageBox(str);
		}
		else if(m_lParam == 3)
		{
			TRACE("连接服务器%s,port:%d,通道:%d,断开\n",url,port,m_ch);
		}
		else if(m_lParam == 4)
		{
			TRACE("端口有冲突\n");
		}
		else if(m_lParam == -102)
		{
			TRACE("用户名密码错误\n");
			//AfxMessageBox("用户名密码错误\n");
		}
		else if(m_lParam == -103)
		{
			TRACE("系统用户满员\n");
		}
		else if(m_lParam == -105)
		{
			TRACE("通道用户满员\n");
		}
		else if(m_lParam == -106)
		{
			TRACE("没有指定的通道\n");
		}
		else if(m_lParam == -112)
		{
			TRACE("没有找到服务器\n");
		}
	}
	else if(m_wParam == LAUMSG_VIDEOMOTION)
	{
		CString cstring;
		int i;
		UpdateData(TRUE);
		for(i=0;i<4;i++)
		{
			//if(!pVSChann)  continue;
			if( !strcmp(pVSChann.url,url) && (pVSChann.m_ch == m_ch) )
			{
				if(lParam&(1<<m_ch))
				{
					cstring.Format("%s 通道%d:视频移动\n",url,m_ch);
					m_StaticSensor+=cstring;		
					while(1)
					{
						nStart = m_StaticSensor.Find('\n',nStart);
						if(nStart != -1)
						{
							nStart++;
							count++;
						}
						else
							break;
					}
					if(count>5)
					{
						m_StaticSensor=m_StaticSensor.Right(m_StaticSensor.GetLength()-m_StaticSensor.Find('\n',0)-1);
					}
					UpdateData(FALSE);
					break;
				}
			}
		}	
	}
	else if(m_wParam == LAUMSG_VIDEOLOST)
	{
		CString cstring;
		int i;
		UpdateData(TRUE);

		for(i=0;i<4;i++)
		{
			//if( !pVSChann)  continue;
			if( !strcmp(pVSChann.url,url) && (pVSChann.m_ch == m_ch) )
			{
				if(lParam&(1<<m_ch))
				{
					cstring.Format("%s 通道%d:视频丢失\n",url,i);
					m_StaticSensor+=cstring;		
					while(1)
					{
						nStart = m_StaticSensor.Find('\n',nStart);
						if(nStart != -1)
						{
							nStart++;
							count++;
						}
						else
							break;
					}
					if(count>5)
					{
						m_StaticSensor=m_StaticSensor.Right(m_StaticSensor.GetLength()-m_StaticSensor.Find('\n',0)-1);
					}
					UpdateData(FALSE);
					break;
				}
			}
		}	
	}
	else if(m_wParam == LAUMSG_ALARM)  //探头报警
	{
		for(int i=0;i<4;i++)
		{
			nStart = 0;
			count = 0;
			if(lParam&(1<<i))
			{
				CString cstring;
				cstring.Format("%s 探头%d:探头\n",url,i);
				UpdateData(TRUE);
				m_StaticSensor+=cstring;
				while(1)
				{
					nStart = m_StaticSensor.Find('\n',nStart);
					if(nStart != -1)
					{
						nStart++;
						count++;
					}
					else
						break;
				}
				if(count>5)
				{
					m_StaticSensor=m_StaticSensor.Right(m_StaticSensor.GetLength()-m_StaticSensor.Find('\n',0)-1);
				}
				UpdateData(FALSE);
			}
		}
	}
	else if(m_wParam == LAUMSG_HIDEALARM)  //遮挡报警
	{
		CString cstring;
		int i;
		UpdateData(TRUE);

		for(i=0;i<4;i++)
		{
			//if( !pVSChann)  continue;
			if( !strcmp(pVSChann.url,url) && (pVSChann.m_ch == m_ch) )
			{
				if(lParam&(1<<m_ch))
				{
					cstring.Format("%s 通道%d:遮挡\n",url,i);
					m_StaticSensor+=cstring;		
					while(1)
					{
						nStart = m_StaticSensor.Find('\n',nStart);
						if(nStart != -1)
						{
							nStart++;
							count++;
						}
						else
							break;
					}
					if(count>5)
					{
						m_StaticSensor=m_StaticSensor.Right(m_StaticSensor.GetLength()-m_StaticSensor.Find('\n',0)-1);
					}
					UpdateData(FALSE);
					break;
				}
			}
		}	
	}

	else if(m_wParam == LAUMSG_EACHVIDEOMOTION)
	{
		VSNETVIDEOMOVE_BLOCK *pblock = (VSNETVIDEOMOVE_BLOCK*)m_lParam;
		TRACE("pblock m_detectall: %d\n",pblock->m_detectall);
	}
	else
	*/if(m_wParam == LAUMSG_OUTPUTSTATUS)
	{
		TRACE("output status : 0X%08X\n",lParam);
		//AfxMessageBox("output status : 0X%08X\n",lParam);
	}
	return TRUE;
}

void CRealClientDemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	if (m_hHandle != -1)
	{
		VSNET_ClientStop(m_hHandle);
	}
	VSNET_ClientCleanup();
}
