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

📄 dialdlg.cpp

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

#include "stdafx.h"
#include "Dial.h"
#include "DialDlg.h"
#include "string.h"
#include "../../../../../api/vc6.0/inc/Shpa3api.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDialDlg dialog

CDialDlg::CDialDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDialDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialDlg)
	m_PhoNumLen = 4;
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialDlg)
	DDX_Control(pDX, IDC_LIST_USER_CH, m_UserChList);
	DDX_Control(pDX, IDC_LIST_TRK_CH, m_TrkChList);
	DDX_Text(pDX, IDC_PHONE_NUM_LEN, m_PhoNumLen);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDialDlg, CDialog)
	//{{AFX_MSG_MAP(CDialDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_EN_CHANGE(IDC_PHONE_NUM_LEN, OnChangePhoneNumLen)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialDlg message handlers

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

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	if(!InitCtiBoard())  return false; 
	
	InitTrunkChList();	
	InitUserChList();

	SetTimer(1000, 10, 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 CDialDlg::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();
	}
}

HCURSOR CDialDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDialDlg::OnChangePhoneNumLen() 
{
	UpdateData(true);
}


void CDialDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	DoUserWork();
	DrawUserChState();
	DrawTrunkChState();
	
	CDialog::OnTimer(nIDEvent);
}

BOOL CDialDlg::InitCtiBoard()
{
	//Initialization of CTI driver
	char CurPath[260],ShIndex[260],ShConfig[260];
	GetCurrentDirectory(200,CurPath);
	strcpy(ShConfig,CurPath);
	strcpy(ShIndex,CurPath);
	strcat(ShConfig,"\\ShConfig.ini");
	strcat(ShIndex,"\\ShIndex.ini");
	if( SsmStartCti(ShConfig,ShIndex) != 0) 
	{
		CString str1;
		SsmGetLastErrMsg(str1.GetBuffer(200));
		AfxMessageBox(str1, MB_OK) ;
		PostQuitMessage(0);
		return false;
    }

	//Initialization of channels on trunk-board

	nTotalCh = SsmGetMaxCh(); 
	for(int i=0; i<nTotalCh; i++)
	{
		ChInfo[i].EnCalled = false;

		if(SsmGetChType(i) == 2 ) // user channel
		{
			SsmSetASDT(i, 1);
			strcpy(ChInfo[i].pPhoNumBuf,"");
			ChInfo[i].nStep = USER_IDLE;
		}
		else
		{
			
			int nDirection;
			if( SsmGetAutoCallDirection(i,&nDirection) == 1 ) //enable auto connection 
			{
				if( nDirection == 1 || nDirection == 2 ) //enable dial
				{
					  ChInfo[i].InUse = 0;
					  strcpy(ChInfo[i].DtmfBuf,"");
					  SsmSetMinVocDtrEnergy( i, 30000);					  
					  ChInfo[i].EnCalled = true;
				}
			}
		}		
	}

	return true;
}


void CDialDlg::DoUserWork()
{
	int ch;

	for(int i=0; i<nTotalCh; i++)
	{
		if( SsmGetChType(i) != 2) continue;

		switch( ChInfo[i].nStep )
		{
		case USER_IDLE:
			strcpy(ChInfo[i].pPhoNumBuf,"");
			SsmClearRxDtmfBuf(i);
			if(SsmGetHookState(i) == 1)					//user-pickup detected
				ChInfo[i].nStep = USER_GET_PHONE_NUM;
			break;

		case USER_GET_PHONE_NUM:
			if( SsmGetRxDtmfLen(i) >= m_PhoNumLen )
			{
				SsmGetDtmfStr(i,ChInfo[i].pPhoNumBuf);		//retrieve phone num
				ChInfo[i].pPhoNumBuf[m_PhoNumLen] = '\0';
				if((ch=myGetAnIdleChannel()) == -1)			//no idle trunk channel available
				{
					SsmSendTone(i,1);						// send busy tone	
					ChInfo[i].nStep = USER_WAIT_HANGUP;
				}
				else 
				{
					SsmPickup(ch);
					ChInfo[ch].InUse = 1;
					
					ChInfo[i].nToTrkCh = ch; 
					ChInfo[i].nStep = USER_WAIT_DIAL_TONE;
				}
			}	
			else if(SsmGetHookState(i) == 0)			//user-hangup detected
			{
				SsmClearRxDtmfBuf(i);
				ChInfo[i].nStep = USER_IDLE;
			}
			break;

		case USER_WAIT_DIAL_TONE:
			ch = ChInfo[i].nToTrkCh;
			if ( SsmAutoDial(ch, ChInfo[i].pPhoNumBuf) ==0  )
			{
				ChInfo[ch].InUse = 2;
				ChInfo[i].nStep = USER_WAIT_REMOTE_PICKUP;
			}
			else 
			{
				SsmHangup(ch);
				ChInfo[ch].InUse = 0;
				SsmClearRxDtmfBuf(ch);

				SsmSendTone(i,1);				//send busy tone
				ChInfo[i].nStep = USER_WAIT_HANGUP;
			}
			break;

		case USER_WAIT_REMOTE_PICKUP: 
			ch = ChInfo[i].nToTrkCh;
			ChInfo[ch].lineState = SsmGetChState(ch);
			if(SsmChkAutoDial(ch) == 7)		//remote user pickup
			{
				SsmTalkWith(i, ch);
				ChInfo[ch].InUse = 4;
				
				ChInfo[i].nStep = USER_TALKING;
			}
			else if(SsmGetHookState(i) == 0)		//user hangup
			{
				SsmHangup(ch);
				ChInfo[ch].InUse = 0;
				SsmClearRxDtmfBuf(ch);
				
				SsmClearRxDtmfBuf(i);
				ChInfo[i].nStep = USER_IDLE;
			}

			else if((ChInfo[ch].lineState == S_CALL_PENDING) || (SsmChkAutoDial(ch) ==10)) //busy tone or 30s time out
			{
				SsmHangup(ch);
				ChInfo[ch].InUse = 0;
				SsmClearRxDtmfBuf(ch);
				
				SsmSendTone(i,1);					//send busy tone
				ChInfo[i].nStep = USER_WAIT_HANGUP;
			}
			break ;

		case USER_TALKING:
			ch = ChInfo[i].nToTrkCh;
			ChInfo[ch].lineState = SsmGetChState(ch);
			if( !SsmGetHookState(i) ||		//user hang up
				 ChInfo[ch].lineState == S_CALL_PENDING ) 	//remote user hangup				 
			{
				SsmHangup(ch);
				SsmStopTalkWith(ch, i);
				SsmClearRxDtmfBuf(ch);
				ChInfo[ch].InUse = 0;
				
				if( !SsmGetHookState(i) )				//user hangup first
				{
					ChInfo[i].nStep = USER_IDLE;
				}
				else										//remote user hangup first
				{
					SsmSendTone(i,1);				//send busy tone
					ChInfo[i].nStep = USER_WAIT_HANGUP;
				}
			}
			break;
			
		case USER_WAIT_HANGUP:
			if(SsmGetHookState(i) == 0)					//user hangup
			{
				SsmStopSendTone(i);					//stop sending busy tone
				SsmClearRxDtmfBuf(i);
				ChInfo[i].nStep = USER_IDLE;
			}
			break;

		default:
			ChInfo[i].nStep = USER_IDLE;
			break;
		}//end switch
	}//end for
}

void CDialDlg::InitTrunkChList()
{
	//Initialization of trunk-channel-state list control
	int ColumnWidth[4] = {40, 85, 60};
	LV_COLUMN lvc;
	lvc.mask =  LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	
	lvc.iSubItem = 0;
	lvc.pszText = "ChannelId" ;
	lvc.cx = ColumnWidth[0];
	m_TrkChList.InsertColumn(0,&lvc);

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

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

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

void CDialDlg::InitUserChList()
{
	int ColumnWidth[4] = {40, 85, 60};
	LV_COLUMN lvc;
	lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

	lvc.iSubItem = 0;
	lvc.pszText = "ChannelId";
	lvc.cx = ColumnWidth[0];
	m_UserChList.InsertColumn(0,&lvc);

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

	lvc.iSubItem = 2;
	lvc.pszText = "CalledId";
	lvc.cx = ColumnWidth[2];
	m_UserChList.InsertColumn(2,&lvc);
	
	char dig[3];
	for(int i=0; i<nTotalCh; i++)
	{	
		if (SsmGetChType(i)==2) m_UserChList.InsertItem(i, _itoa(i,dig,10));
	}
}

void CDialDlg::DrawUserChState()
{
	CString state ;
	char tmpstr[51];

	for(int i=0,nIndex=0; i<nTotalCh; i++)
	{
		if( SsmGetChType(i) != 2) continue;
		
		switch( ChInfo[i].nStep )
		{
		case USER_IDLE:					state = "Idle";			break ;
		case USER_GET_PHONE_NUM:		state = "ReceiveNumber"; break ;
		case USER_WAIT_DIAL_TONE:		state = "WaitForDialTone";	break ;
		case USER_WAIT_REMOTE_PICKUP:	state = "WaitCalledPartyPickup"; break ;
		case USER_TALKING:				state = "Talking";		break ;
		case USER_WAIT_HANGUP:			state = "WaitStationHangup"; break ;		
		}
		
		m_UserChList.GetItemText(nIndex,1,tmpstr,50); 
		if(state != tmpstr) 
			m_UserChList.SetItemText(nIndex, 1, state.GetBuffer(50) );

		m_UserChList.GetItemText(nIndex,2,tmpstr,50); 
		if(strcmp(ChInfo[i].pPhoNumBuf,tmpstr) != 0) 
			m_UserChList.SetItemText(nIndex, 2, ChInfo[i].pPhoNumBuf);
		
		nIndex++;
	}
}
		

void CDialDlg::DrawTrunkChState()
{
	CString state;
	char tmpstr[51];

	for(int i=0, nIndex = 0; i<nTotalCh; i++)
	{
		if( !ChInfo[i].EnCalled ) continue;

		switch(ChInfo[i].InUse)
		{
		case 0:		state="Idle";         break;
		case 1:		state="Off-hook";         break;
		case 2:		state="Dialing";         break;
		case 3:		state="WaitCalledPartyPickup"; break;
		case 4:		state="Talking";         break;
		}

		m_TrkChList.GetItemText(nIndex,1,tmpstr,50);
		if(state != tmpstr) 
			m_TrkChList.SetItemText(nIndex, 1, state.GetBuffer(50) );

		SsmGetDtmfStr(nIndex,ChInfo[i].DtmfBuf);
		m_TrkChList.GetItemText(nIndex,2,tmpstr,50);
		if(strcmp(ChInfo[i].DtmfBuf,tmpstr) != 0) 
			m_TrkChList.SetItemText(nIndex, 2, ChInfo[i].DtmfBuf);

		nIndex++;
	}
}

void CDialDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	SsmCloseCti();	
}

int CDialDlg::myGetAnIdleChannel() // find an idle trunk channel
{
	for(int i=0; i<nTotalCh; i++)
	{
		if( !ChInfo[i].InUse && ChInfo[i].EnCalled  ) break;
	}
	
	if(i==nTotalCh) return -1;
	return i;
}

⌨️ 快捷键说明

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