⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atrk4dlg.cpp

📁 三汇CTI示例程序源码
💻 CPP
字号:
// ATrk4Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ATrk4.h"
#include "ATrk4Dlg.h"
#include "../../../../../api/vc6.0/inc/shpa3api.h"

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

/////////////////////////////////////////////////////////////////////////////
// CATrk4Dlg dialog

CATrk4Dlg::CATrk4Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CATrk4Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CATrk4Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CATrk4Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CATrk4Dlg)
	DDX_Control(pDX, IDC_LIST_TRK, m_TrkChList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CATrk4Dlg, CDialog)
	//{{AFX_MSG_MAP(CATrk4Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CATrk4Dlg message handlers

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

	// 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
	if(InitCtiSystem() == false)
	{
		PostQuitMessage(0);
		return false;
	}
	InitATrunkCh();

	InitDTrunkListCtrl();
	SetTimer( 1000, 50, NULL );
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CATrk4Dlg::InitDTrunkListCtrl()
{
	static int ColumnWidth[5] = {36, 85, 70, 70, 200};
	LV_COLUMN lvc;
	lvc.mask =  LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	
	lvc.iSubItem = 0;
	lvc.pszText = "Channel" ;
	lvc.cx = ColumnWidth[0];
	m_TrkChList.InsertColumn(0,&lvc);

	lvc.iSubItem = 1;
	lvc.pszText = "ChannelState";
	lvc.cx = ColumnWidth[1];
	m_TrkChList.InsertColumn(1,&lvc);

	lvc.iSubItem = 2;
	lvc.pszText = "CallerId";
	lvc.cx = ColumnWidth[2];
	m_TrkChList.InsertColumn(2,&lvc);

	lvc.iSubItem = 3;
	lvc.pszText = "DTMF";
	lvc.cx = ColumnWidth[3];
	m_TrkChList.InsertColumn(3,&lvc);

	lvc.iSubItem = 4;
	lvc.pszText = "ErrorMsg";
	lvc.cx = ColumnWidth[4];
	m_TrkChList.InsertColumn(4,&lvc);


	char dig[3];
	for(int i = 0; i < nTotATrkCh; i++)
	{
		if ( ATrkCh[i].EnCalled )
		m_TrkChList.InsertItem(i,_itoa(i,dig,10));
	}
}

void CATrk4Dlg::UpDateATrunkChListCtrl()
{
	CString state;
	char tmpstr[30];
	int nIndex;

	nIndex = 0;
	for(int i=0; i<nTotATrkCh; i++)
	{
		if ( ATrkCh[i].EnCalled )
		{
			switch( ATrkCh[i].Step ) 
			{
			case TRK_IDLE:					state = "Idle";				break ;
			case TRK_WAIT_CONNECT:			state = "WaitForConn";			break ;
			case TRK_CHECK_PLAY_WELCOM:		state = "PlaySalutatory";		break ;
			case TRK_CHECK_PLAY_SELECT:		state = "WaitForPlaySelect";		break ;
			case TRK_CHECK_REC_START:		state = "WatiForRecordStart";		break ;
			case TRK_CHECK_REC_END:			state = "RecordSpeech";			break ;
			case TRK_CHECK_PLAY_RECORD:		state = "PlaySpeech";			break ;
			case TRK_CHECK_PLAY_BYEBYE:		state = "PlayBye";		break ;
			case TRK_CHECK_PLAY_PASSWORD:	state = "PlayPassword";			break ;
			default:						state = "Undefined";			break;
			}
			m_TrkChList.GetItemText( nIndex, 1, tmpstr, 29 ) ;
			if ( state != tmpstr ) 
				m_TrkChList.SetItemText( nIndex, 1, state.GetBuffer(30) );
						 
			m_TrkChList.GetItemText( nIndex, 2, tmpstr, 29 ) ;		//display caller ID
			if ( tmpstr != ATrkCh[i].pCallerId ) 
				m_TrkChList.SetItemText( nIndex, 2, ATrkCh[i].pCallerId );
						 
			if( ATrkCh[i].Step == TRK_IDLE )
			{
				state = "";
				if ( state != tmpstr ) 
					m_TrkChList.SetItemText( nIndex, 3, state.GetBuffer(30) );				
			}
			else
			{
				state.Empty();
				if( SsmGetDtmfStr(i, state.GetBuffer(50)) > 0 )		//receive and display DTMF keys
				{
				
					m_TrkChList.GetItemText( nIndex, 3, tmpstr, 29 );
					if ( state != tmpstr ) 
						m_TrkChList.SetItemText( nIndex, 3, state.GetBuffer(30) );				
				}
			}
			
			
			m_TrkChList.GetItemText( nIndex, 4, tmpstr, 29 );		//display error messages occured
			if ( ATrkCh[i].pErrMsg != tmpstr ) 
				m_TrkChList.SetItemText( nIndex, 4, ATrkCh[i].pErrMsg );
			nIndex = nIndex + 1;
		}
	}
}

void CATrk4Dlg::InitATrunkCh()
{
	nTotATrkCh =SsmGetMaxCh();
	for(int i=0; i<nTotATrkCh; i++)
	{
		ATrkCh[i].EnCalled = false;

		int nDirection;
		if( SsmGetAutoCallDirection(i,&nDirection) == 1 ) //auto connection is allowed
		{
			if( nDirection == 0 || nDirection == 2 ) //enable call in
			{
				  ATrkCh[i].Step = TRK_IDLE;				  
				  ATrkCh[i].EnCalled = true;
			}
		}		
	}
}

bool CATrk4Dlg::InitCtiSystem()
{
	int nIsSsmStartCtiOK = SsmStartCti("ShConfig.ini", "ShIndex.ini");
	if(nIsSsmStartCtiOK != 0)
	{
		SsmGetLastErrMsg(szErrMsg);
		AfxMessageBox(szErrMsg, MB_OK, 0);
		return FALSE;
	}
	
	if(SsmGetMaxUsableBoard() != SsmGetMaxCfgBoard())
	{
		SsmGetLastErrMsg(szErrMsg);
		AfxMessageBox(szErrMsg, MB_OK, 0);
	}
	
	return true;
}

void CATrk4Dlg::AppErrorHandler(int ch)
{
	SsmHangup(ch);
	SsmGetLastErrMsg(ATrkCh[ch].pErrMsg.GetBuffer(200));
	ATrkCh[ch].Step = TRK_IDLE;
}

void CATrk4Dlg::ScanATrunkCh()
{
	CFileFind cf;
	CString csDtmfStr;
	char tmp[100];

	for(int i=0; i<nTotATrkCh; i++ ) 
	{
		if ( ATrkCh[i].EnCalled )
		{
			ATrkCh[i].lineState = SsmGetChState(i);
			
			switch(ATrkCh[i].Step)
			{
			case TRK_IDLE:
				if( ATrkCh[i].lineState == S_CALL_RINGING )  //ring detected
				{
					SsmPickup(i);
					SsmGetCallerId(i, ATrkCh[i].pCallerId.GetBuffer(100));
					ATrkCh[i].dwTimeOutCounter = 0;
					ATrkCh[i].Step = TRK_WAIT_CONNECT;
				}
				else
				{
					ATrkCh[i].pCallerId = "";
				}
				break;

			case TRK_WAIT_CONNECT:
				if( ATrkCh[i].lineState == S_CALL_TALKING )
				{
					ATrkCh[i].Step = TRK_PLAY_WELCOM;
				}
				break;
			
			case TRK_PLAY_WELCOM:
				SsmClearFileList(i);
				if (SsmAddToFileList(i,"..\\..\\..\\..\\..\\DemoVoc\\Welcom1.voc",-1,0,0xffffffff)==-1 || 
					SsmAddToFileList(i,"..\\..\\..\\..\\..\\DemoVoc\\Welcom2.voc",-1,0,0xffffffff)==-1) 
					AppErrorHandler(i);
				else 
					if(SsmPlayFileList(i) == -1) 
						AppErrorHandler(i);
					else 
						ATrkCh[i].Step = TRK_CHECK_PLAY_WELCOM;
				
			case TRK_CHECK_PLAY_WELCOM:
				if(SsmCheckPlay(i) == 1 || SsmCheckPlay(i) == 2) //end of playing "welcom"
					ATrkCh[i].Step = TRK_PLAY_SELECT;
				break;

			case TRK_PLAY_SELECT:
				SsmSetDtmfStopPlay(i,1);	//enable DTMF-Stop-Play function
				SsmClearRxDtmfBuf(i);
				
				ATrkCh[i].pErrMsg = "";
				if(SsmPlayIndexString(i,"Select") == -1)
					AppErrorHandler(i);
				else
				{
					ATrkCh[i].dwTimeOutCounter = 0;
					ATrkCh[i].Step = TRK_CHECK_PLAY_SELECT;
				}
				break;
			
			case TRK_CHECK_PLAY_SELECT:
				if(SsmGetRxDtmfLen(i) >= 1) 
				{				
					if( SsmCheckPlay(i) >0)

					{
						SsmGet1stDtmf(i,tmp);
						SsmClearRxDtmfBuf(i);
						switch(*tmp)
						{
						case '1': //DTMF=1: record to file "test.voc"
							if(SsmPlayIndexString(i,"RecPrompt") == -1)	//play "press any key to start, and any key again to stop".
								AppErrorHandler(i);
							else 
							{
								ATrkCh[i].dwTimeOutCounter = 0;
								ATrkCh[i].Step = TRK_CHECK_REC_START;
							}
							break;

						case '2':		//DTMF=2: play recorded file "test.voc"
							if(cf.FindFile("test.voc", 0) != 0) 
								if(SsmPlayFile(i,"test.voc",-1,0,0xFFFFFFFF) == -1) 
									AppErrorHandler(i);
								else
									ATrkCh[i].Step = TRK_CHECK_PLAY_RECORD;
							else 
								if(SsmPlayIndexString(i,"FileNotFound") == -1)	//play "file not found" message
									AppErrorHandler(i);
								else
									ATrkCh[i].Step = TRK_CHECK_PLAY_RECORD;
							break ;

						case '3':			//DTMF=3: play user's password,some sample digital
							SsmSetDtmfStopPlay( i, 0); //disable DTMF-Stop-Play function while playing password
							if(SsmPlayIndexString(i,"YourPasswordIs,1,2,3,4") == -1)	//play "your password is 1234".
							{	
								AppErrorHandler(i);
							}
							else 
								ATrkCh[i].Step = TRK_CHECK_PLAY_PASSWORD;
							break;
						
						case '0':			//DTMF='0':  end of service,play "Byebye"
							if ( SsmPlayIndexString(i, "Byebye" )== -1 ) 
								AppErrorHandler(i);
							else
								ATrkCh[i].Step = TRK_CHECK_PLAY_BYEBYE;
							break;							
						
						default:	
							break;
						}
					}
				}
				else if(SsmCheckPlay(i) ==1) // end of playing "Select"
				{
					if( ATrkCh[i].lineState == S_CALL_PENDING ) //check if remote user hangup
					{	
						SsmHangup(i);
						ATrkCh[i].Step = TRK_IDLE;
					} 
					else if(++ATrkCh[i].dwTimeOutCounter > 100) // wait DTMF key for maximum 10 seconds 
					{
						if ( SsmPlayIndexString( i, "Byebye" )== -1 ) 
							AppErrorHandler(i);
						else
							ATrkCh[i].Step = TRK_CHECK_PLAY_BYEBYE;
					}
				} 
				break;

			case TRK_CHECK_PLAY_PASSWORD:
				if(SsmCheckPlay(i) != 0 )				//this channel is not playing index now
					ATrkCh[i].Step = TRK_PLAY_SELECT;
				break ;

			case TRK_CHECK_REC_START:
				if( SsmGetRxDtmfLen(i) >= 1 ) 
				{
					SsmClearRxDtmfBuf(i);
					if( SsmRecToFile(i,"test.voc",-1,0L,10L*8000L,10,0) == -1) //start from 16000L,maxmium 10 seconds
						AppErrorHandler(i);
					else
						ATrkCh[i].Step = TRK_CHECK_REC_END;
				}
				else if(++ATrkCh[i].dwTimeOutCounter > 300) //wait 15 seconds for DTMF-key
				{
					if ( SsmPlayIndexString( i, "Byebye" )== -1 ) 
						AppErrorHandler(i);
					else
						ATrkCh[i].Step = TRK_CHECK_PLAY_BYEBYE;
				}
				break ;

			case TRK_CHECK_REC_END:
				if(SsmGetRxDtmfLen(i)>=1 || SsmChkRecToFile(i)==0)
				{
					SsmStopRecToFile(i);
					ATrkCh[i].Step = TRK_PLAY_SELECT;
				}
				break ;
				
			case TRK_CHECK_PLAY_RECORD:
				if(SsmCheckPlay(i) >0)
					ATrkCh[i].Step = TRK_PLAY_SELECT;
				break;

			case TRK_CHECK_PLAY_BYEBYE:
				if(SsmCheckPlay(i) != 0) 
				{
					SsmHangup(i);
					ATrkCh[i].Step = TRK_IDLE;
				}
				break ;
			} //end of switch(ATrkCh[i].Step)
		}
	}//end of for(int i=0; i<nTotATrkCh; i++ )
}

void CATrk4Dlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	SsmCloseCti();	
}

void CATrk4Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	UpDateATrunkChListCtrl();
	ScanATrunkCh();	

	UpdateData(FALSE);
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -