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

📄 ptcweb.cpp

📁 MDF监控源码2
💻 CPP
字号:
// PtcWEB.cpp: implementation of the CPtcWEB class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mdf.h"
#include "PtcWEB.h"
#include "Protocol.h"
#include "ClientSock.h"
#include ".\ptcweb.h"
//#include "system.h"



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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CMDFApp theApp ;

//##ModelId=4652869E0282
CPtcWEB::CPtcWEB()
{

}

//##ModelId=4652869E0290
CPtcWEB::~CPtcWEB()
{
	
}

//##ModelId=4652869E0280
int CPtcWEB::Transact(sCommItem *pItem)
{
	if( pItem->InPack.CID2 > 0x10 && pItem->InPack.CID2 < 0xE0 )
	{
		Order(pItem) ; //处理来自于MDF的命令包暂缺
	}
	else
	{
		//Answer(pItem) ; //处理来自于MDF的响应包
	}
	return 0 ;
}

//##ModelId=4652869E0271
int CPtcWEB::Answer(sCommItem *pItem)
{
	return 0 ;
}

//##ModelId=4652869E0263
int CPtcWEB::Order(sCommItem *pItem)
{
	CString strTemp1,strTemp2 ;
	CString strTime,strError ;
	//CList<sCmd,sCmd> *pCmdList ;f_Cmd.ucCmd
	sCmd f_Cmd ;

	switch( pItem->InPack.CID2 )
	{
		case 0x21 :// 获取告警响应包处理								
			OCmd21H( pItem ) ;
			break ;
		case 0x22 ://获取监测中心时间
			OCmd22H( pItem ) ;
			break ;
		case 0x23 ://设置监测中心时间
			OCmd23H( pItem ) ;
			break ;
		case 0x30 ://重启监测中心  
			OCmd30H( pItem ) ;
			break ;
		case 0x31 ://发送短信
			OCmd31H( pItem ) ;
			break ;
		case 0x32 ://消除警报
			break ;
		case 0x33 ://刷新监测中心 
			break ;
		default :
			//##进行转发
			break ;
	}
		//写通讯记录
		/*
		strTemp1 = m_fun.GetCmdNote(pItem->pEQ,f_Cmd.ucCmd) ;
		theApp.m_pAdoDB->WriteCommRd(f_Cmd.strTime,0,1,"监测中心",strTemp1,pItem->pEQ->strScanPack,theApp.m_pComm->m_sHostIp.strIp,"收到响应包返回") ;
		//写响应包记录
		strTime = CTime::GetCurrentTime().Format ( "%Y-%m-%d %H:%M:%S" ) ;	//获取当前时间
		theApp.m_pAdoDB->WriteCommRd(strTime,0,2,pItem->pEQ->strIDAddr,strTemp2,pItem->strBuf,pItem->strSource,"命令成功执行") ;

		//清除命令队列里已响应的命令
		theApp.m_pComm->m_pEQPool->SubCommand(pItem->pEQ) ; //删除设备通讯命令
		pCmdList = (CList<sCmd,sCmd> *)pItem->pEQ->pCmdList ;
		if(pCmdList->IsEmpty() && pItem->pEQ->bIsTimeOut)
		{
			((CMDFDlg*)theApp.m_pMainWnd)->m_UI.m_DebugDlg.KillTimer(pItem->pEQ->nID) ;
			pItem->pEQ->bIsTimeOut = FALSE ;
		}
		SetEvent(pItem->pEQ->hCmdEvent) ;			
	}
	else
	{
	}
	*/
	return 0 ;
}

//##ModelId=4652869E0261
int CPtcWEB::OCmd21H(sCommItem *pItem)
{
	CString strSend ;
	Build(NULL,0x21,strSend) ;

	return 0 ;	
}

//##ModelId=4652869E0254
int CPtcWEB::OCmd22H(sCommItem *pItem)
{
	CString strSend ;
	Build(NULL,0x22,strSend) ;

	return 0 ;
}

//##ModelId=4652869E0252
int CPtcWEB::OCmd23H(sCommItem *pItem)
{
	CString strSend ;
	Build(NULL,0x23,strSend) ;
	return 0 ;
}


//##ModelId=4652869E0244
int CPtcWEB::OCmd30H(sCommItem *pItem)
{
	CString strSend ;
	Build(NULL,0x30,strSend) ;
	return 0 ;
}

//##ModelId=4652869E0242
int CPtcWEB::OCmd31H(sCommItem *pItem)
{
	int i,nNum ;
	CString strNum ;
	CString strSend ;
	UCHAR* pData ;
	UCHAR* pBuf ;
	pData = pItem->InPack.INFO ;
	nNum = *pData++ ;
	pBuf = new UCHAR[nNum+1] ;
	for( i= 0 ; i < nNum ; i++ )
	{
		*pBuf++ = m_fun.HexToASCII(pData++) ;
		
	}
	*pBuf = '\0' ;
	strNum = pBuf ;
	delete pBuf ;
	nNum = *pData++ ;
	pBuf = new UCHAR[nNum+1] ;
	for( i= 0 ; i < nNum ; i++ )
	{
		*pBuf++ = m_fun.HexToASCII(pData++) ;
	}
	*pBuf = '\0' ;
	strSend = pBuf ;
	//theApp.m_pComm->m_pCommPool->m_GSM.GsmAddMsg(strSend,strNum,strNum,nType) ;
	Build(NULL,0x31,strSend) ;

	//theApp.m_pComm->m_pCommPool->m_Web.SendData(strSend) ;

	return 0 ;

}

//##ModelId=4652869E0233
int CPtcWEB::Build( sCommEQ *pEQ , UCHAR ucCmd , CString& strSend )
{

	UCHAR* ucpSend ;
	DWORD dwCHKSUM ;
	DWORD len ;
	int nYear ;
	char cTemp[3] ;
	UCHAR cTimeSrc[10] ;
	char cTimeDst[10] ;

	CString strCHKSUM ;
	CString strTemp ;
	CString strTime ;
	CString strTip ;

	if(pEQ == NULL )
		return -1 ;	// ## 添加未知设备处理
	try
	{
		strSend.Empty() ;
		strSend = char(0X7E);
		strSend += "10" ;
		strSend += "00" ;
		strSend += "82" ;

		BytesToString(&ucCmd,cTemp,1) ;

		strSend += (LPSTR)cTemp ;

		switch (ucCmd)
		{
			case 0x21 :
				strTemp = m_fun.GetLENGTH(2) ;
				/*
				if( theApp.m_pComm->m_pEQPool->GetAllEQState() == 0 )
					strSend += strTemp + "00" ; 
				else
					strSend += strTemp + "FF" ;
					*/
				break ;
			case 0x22 : //设置监测模块时间
				strTemp = m_fun.GetLENGTH(14) ;	//生成LENGTH
				nYear = atoi(CTime::GetCurrentTime().Format("%Y")) ;

				cTimeSrc[0] = nYear >> 8 ;
				cTimeSrc[1] = nYear & 0xFF ;
				cTimeSrc[2] = atoi(CTime::GetCurrentTime().Format("%m")) ;
				cTimeSrc[3] = atoi(CTime::GetCurrentTime().Format("%d")) ;
				cTimeSrc[4] = atoi(CTime::GetCurrentTime().Format("%H")) ;
				cTimeSrc[5] = atoi(CTime::GetCurrentTime().Format("%M")) ;
				cTimeSrc[6] = atoi(CTime::GetCurrentTime().Format("%S")) ;
				BytesToString(cTimeSrc,cTimeDst,7) ;
				strTime = (LPSTR)cTimeDst ;
				strSend += strTemp + strTime ;
				break ;
			default :
				strTemp = m_fun.GetLENGTH(0) ;
				strSend += strTemp ;
				break ;
		}

		len = strSend.GetLength() - 1 ;	//数据内容长度

		ucpSend = (UCHAR*)strSend.GetBuffer(0) ;
		dwCHKSUM = m_fun.GetCHKSUM(++ucpSend,len) ;
		strSend += m_fun.CHKSUMToString(dwCHKSUM) ;
		strSend += char(0X0D) ;

		return 0 ;
	}
	catch(_com_error e)
	{
		strTip.Format( "异常:执行MDF响应命令ACmd4DH发生异常:%s ",(LPCSTR)e.Description() ) ;
		theApp.OutPut(strTip);
		return -1 ;
	}
}

//##ModelId=4652869E0223
int CPtcWEB::Custom(sCommItem *pItem)
{
	int nNo,nResult ;
	CString strCmd,strTip,strSend,strPhone,strMsg,strTime,strNote ;
	CString strData = pItem->strBuf ;
	CAdoDB cAdo ;

	try
	{
		nResult = 0 ;
		nNo = strData.Find("|") ;

		if( nNo >= 0 )
		{
			strCmd = strData.Left(nNo) ;
			strData.Delete(0,nNo+1) ;
			strSend.Format("%s|", strCmd ) ;

			if( strCmd == "CN" )
			{
				strSend += "00" ;
				strNote = "检测监测中心连接状态" ;
			}
			else if( strCmd == "GS" )
			{
				strSend += "00" ;
				strNote = "获取监测中心状态" ;
			}
			else if( strCmd == "SM" )
			{
				nNo = strData.Find("|") ;
				strPhone = strData.Left(nNo) ;
				strData.Delete(0,nNo+1) ;

				if( strPhone.IsEmpty() || strPhone.GetLength() < 11 )
				{
					strSend += "FF|Phone Error"+strPhone ;
					nResult = 1 ;
				}
				else if( strData.IsEmpty() || strData.IsEmpty() )
				{
					strSend += "FF|No Msg" ;
					nResult = 2 ;
				}
				else if( !theApp.m_Baseinfo.SMSOpen )
				{
					
					strSend += "FF|GSM Open Fail" ;
				}
				else if( theApp.m_Baseinfo.VoiceOpen)		//语音运行状态忙
				{
					
					strSend += "FF|GSM BUSY" ;
				}
				else
				{
					/*if(theApp.m_pComm->m_pCommPool->m_GSM.GetGsmRun())
					{
						theApp.m_pComm->m_pCommPool->m_GSM.GsmAddMsg(strData,strPhone,strPhone,pItem->pSock,MSG_TYPE_WEB) ;
						strSend += "00" ;
					}
					else
					{
						strSend += "FF|GSM Send Fail" ;
					}*/
				}
				strNote = "发送短消息" ;
			}
			else if( strCmd == "RF" )//刷新监测中心数据库
			{
				strSend += "00" ;
				strNote = "刷新数据库" ;
				//theApp.m_pComm->nServerState = SERVER_REFRESH ;
				
			}
			else if( strCmd == "RS" )//重起监测中心
			{
				strSend += "00" ;
				strNote = "重启数据库" ;
			}
			else if( strCmd == "TS" )//超时测试
			{
				return 0 ;
			}
			else
			{
				strNote = "未知命令" ;
				strSend += "FF|unknow command" ;
			}
		}
		else
			strSend.Format("%s|FF|Error Format",strData) ;


		strTime = CTime::GetCurrentTime().Format ( "%Y-%m-%d %H:%M:%S" ) ;
		strTip.Format("接收 Web 端命令:%s \r\n" , pItem->strBuf ) ;
		
		cAdo.WriteCommRd(strTime,CRDR_NORMAL,CRDT_WEB,"Web 服务器",strNote,pItem->strBuf,pItem->strSource,"命令包 成功执行") ;

		if( pItem->pSock != NULL )
			//theApp.m_pComm->m_pCommPool->m_Web.WebSend(pItem->pSock,strSend) ;//返回消息

		strSend += "\r\n" ;

		TRACE0(strSend) ;

		//cAdo.WriteCommRd(strTime,CRDR_NORMAL,CRDT_HOST,"监测中心",strNote,strSend,theApp.m_pComm->m_sHostIp.strIp,"返回 响应包") ;
		
		return 0 ;
	}
	catch(_com_error e)
	{
		strTip.Format( "异常:Web 数据包执行 PtcWEB::Custom 发生异常:%s ",(LPCSTR)e.Description() ) ;
		TRACE0(strTip) ;
		return -1 ;
	}

	return -1;
}

// 协议解析
//##ModelId=4652869E0297
int CPtcWEB::Analyse(CInfoFrame &frame,CString &strResult)
{
	int nNo,nResult ;
	CString strCmd,strTip,strSend,strPhone,strMsg,strTime,strNote ,strIP;
	CString strData = frame.ucBuf;
	LPCTSTR lpPhone,lpMsg;
	try
	{
		nResult = 0 ;
		nNo = strData.Find("|") ;

		if( nNo >= 0 )
		{
			strCmd = strData.Left(nNo) ;
			strData.Delete(0,nNo+1) ;
			strSend.Format("%s|", strCmd ) ;

			if( strCmd == "CN" )
			{
				strSend += "00" ;
				strNote = "检测监测中心连接状态" ;
			}
			else if( strCmd == "GS" )
			{
				strSend += "00" ;
				strNote = "获取监测中心状态" ;
			}
			else if( strCmd == "SM" )
			{
				nNo = strData.Find("|") ;
				strPhone = strData.Left(nNo) ;
				strData.Delete(0,nNo+1) ;

				if( strPhone.IsEmpty() || strPhone.GetLength() < 11 )
				{
					strSend += "FF|Phone Error"+strPhone ;
					nResult = 1 ;
				}
				else if( strData.IsEmpty() || strData.IsEmpty() )
				{
					strSend += "FF|No Msg" ;
					nResult = 2 ;
				}
				else if( !theApp.m_Baseinfo.SMSOpen )
				{
					
					strSend += "FF|GSM Open Fail" ;
				}
				else if( theApp.m_Baseinfo.VoiceOpen)		//语音运行状态忙
				{
					
					strSend += "FF|GSM BUSY" ;
				}
				else
				{
					lpPhone = strPhone.GetBuffer();
					lpMsg = strData.GetBuffer();
					::SendMessage(theApp.m_pMonitor->m_hWnd,WM_GSMMSG,(WPARAM)lpPhone,(LPARAM)lpMsg);
					strSend += "00" ;
					
					//strSend += "FF|GSM Send Fail" ;
					
				}
				strNote = "发送短消息" ;
			}
			else if( strCmd == "RF" )//刷新监测中心数据库
			{
				strSend += "00" ;
				strNote = "刷新数据库" ;
				//theApp.m_pComm->nServerState = SERVER_REFRESH ;
				//::SendMessage(theApp.m_pMainWnd->m_hWnd,WM_COMMRESTART,0,0); 
				//LPCTSTR lpStr;
				//lpStr = strSend.GetBuffer();
				
				PostMessage(theApp.m_pMainWnd->m_hWnd,WM_COMMRESTART,NULL,NULL); 
			}
			else if( strCmd == "RS" )//重起监测中心
			{
				strSend += "00" ;
				strNote = "重启数据库" ;
				strSend = "RS|00";

				//LPCTSTR lpStr;
				//lpStr = strSend.GetBuffer();
				PostMessage(theApp.m_pMainWnd->m_hWnd,WM_COMMRESTART,NULL,NULL); 
			}
			else if( strCmd == "TS" )//超时测试
			{
				return 0 ;
			}
			else
			{
				strNote = "未知命令" ;
				strSend += "FF|unknow command" ;
			}
		}
		else
			strSend.Format("%s|FF|Error Format",strData) ;

		
		strTime = CTime::GetCurrentTime().Format ( "%Y-%m-%d %H:%M:%S" ) ;
		strTip.Format("接收 Web 端命令:%s \r\n" , frame.ucBuf) ;
		
		strSend += "\r\n" ;
		strResult = strSend;

		TRACE0(strSend) ;

		//cAdo.WriteCommRd(strTime,CRDR_NORMAL,CRDT_HOST,"监测中心",strNote,strSend,theApp.m_pComm->m_sHostIp.strIp,"返回 响应包") ;
		
		return 0 ;
	}
	catch(_com_error e)
	{
		strTip.Format( "异常:Web 数据包执行 PtcWEB::Custom 发生异常:%s ",(LPCSTR)e.Description() ) ;
		TRACE0(strTip) ;
		return -1 ;
	}

	return 0;
}

⌨️ 快捷键说明

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