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

📄 gateagentdlg.cpp

📁 PDA通讯网关服务器源码程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// GateAgentDlg.cpp : implementation file
//
//集成业务处理层和数据展示层
#include "stdafx.h"
#include "GateAgent.h"
#include "GateAgentDlg.h"
#include "Log.h"
#include "DlgSet.h"
#include "DlgTest.h"
#include "Config.h"
#include "SQLBase.h"
#include "PubConst.h"
#include "ParamSet.h"
#include "SmsOperater.h"
#include <string.h>
#include "MainThread.h"
#include "Socket.h"


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

#define MAXCOMNUM  8;


st_smsCom g_smsCom[MAX_COM]; //保存短消息句柄函数


bool                       g_bRun=false;
int                        g_nMax=0;
int						   g_iSendPudDataTime=0;
int                        g_iTestNum=0;//如果是演示版本,则处理数据20次则退出系统

extern CString g_strTel;
extern CString g_strText;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//extern int initGate(char *strInput,char *strOutput);

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 void OnSys();
	afx_msg void OnClose();
	virtual void OnOK();
	//}}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)
	ON_BN_CLICKED(ID_SYS, OnSys)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGateAgentDlg dialog

CGateAgentDlg::CGateAgentDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGateAgentDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGateAgentDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGateAgentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGateAgentDlg)
	DDX_Control(pDX, IDC_LIST_SHOW, m_clcView);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGateAgentDlg, CDialog)
	//{{AFX_MSG_MAP(CGateAgentDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDC_MENU_RUN, OnMenuRun)
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
	ON_COMMAND(ID_MENU_LOG, OnMenuLog)
	ON_COMMAND(ID_MENU_CONFIG, OnMenuConfig)
	ON_COMMAND(ID_MENU_TEST, OnMenuTest)
	ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
	ON_WM_CLOSE()
	ON_COMMAND(IDC_SHOW, OnShow)
	ON_WM_SIZE()
	ON_COMMAND(IDC_MENU_SHUT, OnMenuShut)
	ON_COMMAND(IDC_CLOSE, OnTrayClose)
	ON_COMMAND(ID_MENU_CLEAR, OnMenuClear)
	ON_UPDATE_COMMAND_UI(ID_MENU_TEST, OnUpdateMenuTest)
	ON_COMMAND(ID_MENU_CAL, OnMenuCal)
	ON_COMMAND(ID_MENU_PARAM, OnMenuParam)
	ON_COMMAND(IDD_MENU_HELP, OnMenuHelp)
	ON_COMMAND(IDD_MENU_CLEAR, OnMenuClear)
	ON_COMMAND(IDD_MENU_CLEARSIM, OnMenuClearsim)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_TRAY_NOTIFICATION,OnTrayNotification)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGateAgentDlg message handlers

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

	if (!AfxSocketInit())
	{
		return FALSE;
	}

	// 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);
		}
	}

	// 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
	g_bRun = false;
	// TODO: Add extra initialization here
	m_trayIcon.Create(this, WM_USER_TRAY_NOTIFICATION, "终端零售通短信网关", 
	                  m_hIcon, IDR_MENU_TRAY);//在这里实现了当鼠标放在托盘上时

	m_CPubBase.OnInitGrid(&m_clcView,true,"");
	
	m_bDel=false;
	OnMenuRun();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


void CGateAgentDlg::OnMenuRun() 
{
	CString strTmp,strShow;
	int iRet = -100;
	char chErr[100]={0};
	if(	g_bRun)
	{
		OnInsertMess("系统服务已经启动了,目前系统处于服务状态!");
		return ;
	}
	
	//从配置文件当中读取有关信息
	if (OnReadBase()<0) return ;

	//连接狗服务器函数

	m_bTest=false;

	//连接数据库
	if (!m_sqlDB.OnDBInit(m_sDBServer.GetBuffer(0),m_sDBUser.GetBuffer(0),m_sDBPwd.GetBuffer(0),2,m_sDataBaseName.GetBuffer(0),chErr))
	{
		strShow.Format("%s,请查看数据库相关配置,系统启动失败,不能进入服务状态!",chErr);
		OnInsertMess(strShow.GetBuffer(0),true,true);
		CDlgSet dlgSet;
		dlgSet.DoModal();			
		return ;
	}


	if (OnReadSysCfg()<0) //从数据库当中读写配置文件
	{
		OnInsertMess("读取相关配置文件失败!",true,true);
		return ;
	}
	
	if (OnStartMainThread()<0) return ;
	//定时启动清空临时数据库函数,删除昨天的数据
	SetTimer(2,100000,NULL);
	
}

void CGateAgentDlg::OnInsertMess(char *strShow, bool bShow , bool bWritelog)
{
	m_CPubBase.OnInsertMess(&m_clcView,strShow,bShow,bWritelog);
}

void CGateAgentDlg::OnMenuExit() 
{
	if (!g_bRun)
	{
		OnInsertMess("系统服务没有启动,不需要停止!",true,false);
		return ;
	}

	int  nRetCode = MessageBox("确定要停止服务吗,系统将不能服务了?","警告",MB_YESNO|MB_ICONQUESTION);
	if(nRetCode!=IDYES)
	{
		 return;
	}

	try
	{
		KillTimer(1);
		m_sqlDB.OnDBClose();
	}
	catch(...)
	{
	}
	OnInsertMess("系统已经停止服务了!",true,false);
	g_bRun = false;
}

void CGateAgentDlg::OnMenuLog() 
{
	CLog logTool;
	logTool.DoModal();	
}

void CGateAgentDlg::OnMenuConfig() 
{
	CDlgSet dlgSet;
	dlgSet.DoModal();	
}

void CGateAgentDlg::OnMenuParam() 
{
	CParamSet dlg;
	dlg.DoModal();
}

//系统商的测试工具
void CGateAgentDlg::OnMenuTest() 
{
	CString strTmp;
	CDlgTest dlgTest;
	if (dlgTest.DoModal()!=IDOK)
	{
		return ;
	}

	g_strTel.TrimLeft();
	g_strTel.TrimRight();
	if (g_strTel.GetLength()<10)
	{
		AfxMessageBox("手机号码不正确!请重新输入.");
		return ;
	}

	//调用发送函数
	int iRet =OnSend(0,g_strTel.GetBuffer(0),g_strText.GetBuffer(0),"0",g_strText.GetLength());
	if (iRet<0)
	{
		strTmp.Format("%s发送内容[%s]失败!",g_strTel,g_strText);		
	}
	strTmp.Format("%s发送内容[%s]成功!",g_strTel,g_strText);
//	OnInsertMess(strTmp.GetBuffer(0),true,false);
}

void CGateAgentDlg::OnMenuAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}


BEGIN_EVENTSINK_MAP(CGateAgentDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CGateAgentDlg)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()


void CGateAgentDlg::OnClose() 
{
	if (g_bRun)
	{
		g_bRun=false;
		try
		{
			KillTimer(1);
			Sleep(500);
			for(int i=0;i<MAX_COM;i++)
			{
				if (g_smsCom[i].iCom>1)//找到设备的短信中心号码,然后逐个关闭
				{
					m_cSmsOper.OnCloseComm(g_smsCom[i].iCom);
				}
			}
			m_sqlDB.OnDBClose();
			CoUninitialize();
		}
		catch(...)
		{
			PostQuitMessage(0);
		}
	}	
	PostQuitMessage(0);
	exit(0);
	CDialog::OnClose();
}

int CGateAgentDlg::OnReadBase()
{
	//读数据库信息	
	CCfg cfgFile;
	if(!cfgFile.OnReadCfg("DATABASE", "Server", m_sDBServer))
	{
		AfxMessageBox("读取数据库服务器名称失败!");
		return -1;
	}
	if(!cfgFile.OnReadCfg("DATABASE", "User", m_sDBUser))
	{
		AfxMessageBox("读取数据库用户名称失败!");
		return -1;
	}
	if(!cfgFile.OnReadCfg("DATABASE", "DatabaseName", m_sDataBaseName))
	{
		AfxMessageBox("读取数据库名失败!");
		return -1;
	}
	if(!cfgFile.OnReadCfg("DATABASE", "Password", m_sDBPwd))
	{
		AfxMessageBox("读取数据库密码名称失败!");
		return -1;
	}

	if(!cfgFile.OnReadCfg("DEVICE", "MobileType", &m_iMobileType))
	{
		AfxMessageBox("读取手机类型失败!");
		return -1;
	}
	if(!cfgFile.OnReadCfg("SYSTEM", "NumFront", m_sNumFront))
	{
		AfxMessageBox("读取小灵通前缀失败!");
		return -1;
	}

	if(!cfgFile.OnReadCfg("SYSTEM", "SendPudData", &g_iSendPudDataTime))
	{
		AfxMessageBox("读取发送pdu时间失败!");
		return -1;
	}
	
	if(!cfgFile.OnReadCfg("SYSTEM", "DogPort", &m_DogPort))
	{
		AfxMessageBox("读取DogPort失败!");
		return -1;
	}

	m_iSendTime=g_iSendPudDataTime+200;
		
	return 0;
}



//处理接受到的消息
int CGateAgentDlg::OnDoWith(int nHandle,char *pchTelNum,char *pchTime,char *pchText,char *pchFlag)
{	
	CString strTmp,strMess,strTel,strTime;
	strTmp.Format("20%s",pchTime);
	strTime=strTmp.Mid(0,14);
	
	//如果为演示版本,则处理10次后,系统退出
    if (m_bTest==true)
	{
		g_iTestNum++;
		if (g_iTestNum>110)
		{
			OnInsertMess("对不起!此系统为演示版本,如购买正式版本,请速与长沙同享信息科技有限责任公司联系!");
			OnClose();
			exit(0);
			return 0;
		}
	}
	//检查手机的合法性
	if (pchTelNum==NULL)
	{
		OnInsertMess("接收到的手机号码为空!", true,true);
		return -1;
	}
	//检查电话号码
	strTel=m_CPubBase.OnCheckIsChina(pchTelNum,m_sNumFront);
	
	//检查消息合法性
	strMess.Format("%s",pchText);
	strMess.TrimLeft();
	strMess.TrimRight();
	//如果接收到的消息中含有中文的逗号,系统自动将逗号处理成英文模式的
	m_CPubBase.OnCheckInputValidAndReplace(strMess);
	if (strMess.IsEmpty()||strMess.GetLength()>140)
	{
		OnInsertMess("收到消息为空或者消息太长!", true,true);
		return -1;
	}		
	strTmp.Format("收到%s:%s",strTel,strMess);
	OnInsertMess(strTmp.GetBuffer(0),true,m_bWrite);

	//区分消息类型
	int iRetSmsType=m_CPubBase.OnAnalyzeText(strMess,m_sDiv.GetBuffer(0),m_strNoDiv.GetBuffer(0),m_nIsCustCard);
	//检查手机号码是否合法
	CString strEmpcode,strTerm,strBelong2,strName;
	if (OnCheckIsLogin(iRetSmsType,strTel.GetBuffer(0),strEmpcode,strTerm,strBelong2,strName)<0) return -1;

	//保持临时消息,以免后面更改了
	CString strToken=strMess;

	//收到消息后,不管什么都插入原始记录表格
	char chSQLCmd[SQLLENGTH]={0};
	_snprintf(chSQLCmd, SQLLENGTH-1,
		      "insert into me_Record (vcMovetel, vcDatetime, vcText, cState,vcNO,vcTercode,vcBelong2)\
			  values('%s','%s','%s','%02d','%s','%s','%s')" ,strTel.GetBuffer(0),strTime.GetBuffer(0),strMess.GetBuffer(0),iRetSmsType,strEmpcode.GetBuffer(0),strTerm.GetBuffer(0),strBelong2.GetBuffer(0));
	int iRet= OnDbDoWith(chSQLCmd);
	if (iRet<0)

⌨️ 快捷键说明

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