123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434 |
- #include "stdafx.h"
- //#include "stdafx.h"
- #include <comdef.h>
- #include <atlbase.h>
- //#include "resource.h"
- //#include "AboutBox.h"
- #include "ThreadPool.hpp"
- #include <strsafe.h>
- #include "SysLib.h"
- #include "ascprocess.h"
- #include "rtuprocess.h"
- #include "tcpprocess.h"
- #include "icpdasprocess.h"
- #include "SnmpProcess.h"
- #include "upsParadigmProcess.h"
- #include "stulzprocess.h"
- #include "DaiKinProcess.h"
- #include "PowerwareProcess.h"
- #include "greeprocess.h"
- #include "CarelProcess.h"
- #include "MostDevice.h"
- #include "NoticeQueue.h"
- #include "MainPro.h"
- const int SOCK_TCP = SOCK_STREAM-1;
- const int SOCK_UDP = SOCK_DGRAM-1;
- const int AF_IPV4 = 0;
- const int AF_IPV6 = 1;
- #pragma warning(disable : 4995)
- namespace ServerSocket
- {
- CServerSocket* g_pServerSocket[MAX_SERVER_LISTEN] = {0};
- CServerSocket::CServerSocket(void)
- {
- m_nSocketIndex = 0;
- m_nSockType = SOCK_TCP;
- m_nMode = AF_IPV4;
- InitializeCriticalSection( &m_csProcessData );
- m_SocketServer.SetInterface(this);
- }
- CServerSocket::~CServerSocket(void)
- {
- DeleteCriticalSection( &m_csProcessData );
- }
- ///////////////////////////////////////////////////////////////////////////////
- void CServerSocket::OnThreadBegin(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- SockAddrIn sockAddr;
- m_SocketServer->GetSockName(sockAddr);
- GetAddress( sockAddr, strAddr );
- }
- void CServerSocket::OnThreadExit(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- }
- void CServerSocket::OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr;
- CSocketHandle sockHandle;
- SockAddrIn sockAddr;
- if (newSocket != INVALID_SOCKET)
- {
- sockHandle.Attach( newSocket );
- sockHandle.GetPeerName( sockAddr );
- GetAddress( sockAddr, strAddr );
- sockHandle.Close();
- LOG4C((LOG_NOTICE, _T("Connection abandoned: %s\r\n"), strAddr ));
- }
- else
- {
- LOG4C((LOG_NOTICE, _T("Connection abandoned. Not a valid socket.\r\n"), strAddr ));
- }
- }
- void CServerSocket::OnAddConnection(CSocketHandle* pSH, SOCKET newSocket)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- }
- void CServerSocket::OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
- {
- //ASSERT( g_pTcpServer != NULL );
- ASSERT( pSH == m_SocketServer );
- (pSH);
- CString strAddr, strText;
- USES_CONVERSION;
- LPTSTR pszText = strText.GetBuffer(dwCount+1);
- ::StringCchCopyN(pszText, dwCount+1, A2CT(reinterpret_cast<LPCSTR>(pbData)), dwCount);
- strText.ReleaseBuffer();
- GetAddress( addr, strAddr );
- int nResult = 0;
- if (m_nSockType == SOCK_TCP)
- {
- // unsafe access to Socket list!
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- const SocketBufferList& sl = m_SocketServer.GetSocketList();
- for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #else
- const SocketList& sl = m_SocketServer.GetSocketList();
- for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #endif
- {
- EnterCriticalSection( &m_csProcessData );
- CSocketHandle sockHandle;
- sockHandle.m_nPendingSize = 0;
- memset(sockHandle.m_PendingBuffer, 0, SOCKET_BUFFSIZE);
- sockHandle.Attach( (*citer) );
- SockAddrIn sockAddr;
- sockHandle.GetPeerName(sockAddr);
- if( sockAddr == addr )
- {
- ProcessData( sockHandle, pbData, dwCount );
- }
- sockHandle.Detach();
- LeaveCriticalSection( &m_csProcessData );
- }
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr ) {
- //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
- } else {
- //AppendText( _T("Please change the port number to send message to a client.\r\n") );
- }
- }
- }
- void CServerSocket::OnConnectionDropped(CSocketHandle* pSH)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- }
- void CServerSocket::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
- {
- ASSERT( pSH == m_SocketServer );
- (pSH);
- _com_error err(dwError);
- LOG4C((LOG_NOTICE, _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() ));
- }
- int CServerSocket::OnCmdProcess(CSocketHandle &sockHandle, void *pData)
- {
- ProtocolHeader *pHeader = (ProtocolHeader *)pData;
- if( pHeader == NULL ) return -1;
- if( pHeader->nLen < 0 || pHeader->nLen > 65535 ) return -1;
- unsigned int tmp = CalcCheckSum(pHeader, pHeader->nLen);
- if(tmp != pHeader->Verify)
- {
- return -1;
- }
- switch (pHeader->nCmd)
- {
- case CMD_DEV_ONLINE: //读设备在线状态
- ProcessResDevOnline(sockHandle, pHeader);
- break;
- case CMD_SINGLE_VAR_ONLINE: //读单个变量在线状态命令
- ProcessResSingleVarOnline(sockHandle, pHeader);
- break;
- case CMD_MUL_VAR_ONLINE: //读多个变量在线状态命令
- ProcessResMulVarOnline(sockHandle, pHeader);
- break;
- case CMD_READ_SINGLE_VAR_DATA: //读单个变量内容或报警状态
- ProcessResReadSingleVarData(sockHandle, pHeader);
- break;
- case CMD_READ_MUL_VAR_DATA: //读多个变量内容或报警状态
- ProcessResReadMulVarData(sockHandle, pHeader);
- break;
- case CMD_WRITE_SINGLE_VAR_DATA: //设置单个变量值
- ProcessResWriteSingleVarData(sockHandle, pHeader);
- break;
- case CMD_WRITE_MUL_VAR_DATA: //设置多个变量值
- PrcocessResWriteMulVarData(sockHandle, pHeader);
- break;
- case CMD_DOG_DATA: //软件狗数据
- ProcessResSoftDog(sockHandle, pHeader);
- break;
- case CMD_ALARM_CONFIRM: //报警确认
- ProcessResAlarmConfirm(sockHandle, pHeader);
- break;
-
- //请求读特殊变量的数据
- case CMD_READ_SPECIAL_SINGLE_VAR_DATA:
- ProcessResReadSpecSingleVarData(sockHandle, pHeader);
- break;
- case CMD_HEART: //心跳包响应
- #if 0
- ProcessHeart(sockHandle, pHeader);
- #endif
- break;
- case CMD_NOTICE_READ_DB_VAR: //通知服务器再一次从数据库读取变量参数
- ProcessNoticeReadDBVar(sockHandle, pHeader);
- break;
- case CMD_NOTICE_READ_DB_DEV: //通知服务器再一次从数据库读取设备参数
- ProcessNoticeReadDBDev(sockHandle, pHeader);
- break;
- case CMD_NOTICE_READ_DB_USER: //通知服务器再一次从数据库读取用户参数
- ProcessNoticeReadDBUser(sockHandle, pHeader);
- break;
- case CMD_NOTICE_TEST_ALARM:
- ProcessNoticeTestAlarm(sockHandle, pHeader);
- break;
- default: return 0;
- }
- return 0;
- }
- void CServerSocket::ProcessData(CSocketHandle &sockHandle, const BYTE* pData, DWORD nLen)
- {
- DWORD nBuffIndex = 0;
- while( nBuffIndex < nLen )
- {
- //LOG4C((LOG_NOTICE, "接收长度=%d, 处理长度=%d, PendingSize=%d", nLen, nBuffIndex, sockHandle.m_nPendingSize));
- ProtocolHeader *pHeader; //当前协议包头
- DWORD nProcessedLen = 0; //当前循环处理了多少个字节
-
- if( sockHandle.m_nPendingSize > 0 ) // 开始组包
- {
- //LOG4C((LOG_NOTICE, "开始组包"));
- pHeader = (ProtocolHeader *)sockHandle.m_PendingBuffer;
- if( sockHandle.m_nPendingSize < sizeof(ProtocolHeader) ) //上一次接收到的长度小于包头
- {
- DWORD nLinkHeaderLen = sizeof( ProtocolHeader ) - sockHandle.m_nPendingSize;
- if( nLinkHeaderLen < nLen ) //这次可以收完包头
- {
- memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nLinkHeaderLen ); //这里已经收完Header
- nProcessedLen = pHeader->nLen - sockHandle.m_nPendingSize;
- if( nProcessedLen <= nLen ) //如果所需处理的长度小于等于当前包长度
- {
- memcpy( &sockHandle.m_PendingBuffer[ sizeof( ProtocolHeader ) ],
- & ( ( char *) pData )[ nLinkHeaderLen ],
- pHeader->nLen - sizeof( ProtocolHeader ) );
- //LOG4C((LOG_NOTICE, "收完所需的包"));
- }
- else
- {
- int nTemp = nLen - nLinkHeaderLen; //除去头剩余部分的长度
- if ( nTemp > 0 ) //刚好是Header的长度,不用拷贝内存,所以这里加了>0的判断
- {
- memcpy( &sockHandle.m_PendingBuffer[ sizeof( ProtocolHeader ) ],
- & ( ( char *) pData )[ nLinkHeaderLen ],
- nTemp );
- }
- sockHandle.m_nPendingSize += nLen;
- }
- }
- else //这次还是没有收完包头, 继续Pending
- {
- //LOG4C((LOG_NOTICE, "这次还是没有收完包头, 继续Pending"));
- memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nLen );
- sockHandle.m_nPendingSize += nLen;
- nProcessedLen = nLen;
- }
- }
- else //Header部分已经在阻塞的缓冲区中
- {
- nProcessedLen = pHeader->nLen - sockHandle.m_nPendingSize;
- if ( nProcessedLen <= nLen ) //如果需要处理的长度小于现有包的长度
- {
- memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nProcessedLen );
- sockHandle.m_nPendingSize = 0;
- }
- else //否则要继续阻塞
- {
- memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nLen );
- sockHandle.m_nPendingSize += nLen;
- }
- }
- }
- else //第一次接包
- {
- //LOG4C((LOG_NOTICE, "第一次接包"));
- pHeader = (ProtocolHeader *)&( (unsigned char *)pData )[nBuffIndex];
- if( nLen - nBuffIndex < sizeof(ProtocolHeader) ) // 没有收够包头,先记录当前收到的Buffer
- {
- //如果第一次接包就没有收够包头,认为是非法包,扔掉,就是说已处理的长度nProcessedLen = 0
- sockHandle.m_nPendingSize = nLen - nBuffIndex;
- memcpy(sockHandle.m_PendingBuffer, pHeader, sockHandle.m_nPendingSize);
- //LOG4C((LOG_NOTICE, "第一次接包就没有收够包头,认为是非法包,扔掉"));
- }
- else
- {
- nProcessedLen = pHeader->nLen;
- if( (int)pHeader->nLen > nLen - nBuffIndex )
- {
- memcpy(sockHandle.m_PendingBuffer, pHeader, nLen - nBuffIndex);
- //如果第一次接包,pHeader->nLen大于当前包的总长,认为是非法包,扔掉
- if( nBuffIndex == 0 )
- {
- //组包错误,则扔掉当前包
- //LOG4C((LOG_NOTICE, "第一次接包,服务器pHeader->nLen大于当前包的总长,认为是非法包,扔掉\r\n"));
- }
- sockHandle.m_nPendingSize = nLen - nBuffIndex;
- nProcessedLen = nLen - nBuffIndex;
- }
- else
- {
- //LOG4C((LOG_NOTICE, "正常包"));
- sockHandle.m_nPendingSize = 0;
- }
- }
- }
- if ( nProcessedLen == 0 )
- {
- // 没有收够包头,认为是非法包,扔掉
- //LOG4C((LOG_NOTICE, "没有收够包头,认为是非法包,扔掉"));
- break;
- }
- if ( sockHandle.m_nPendingSize == 0 )
- {
- if ( pHeader->nLen > SOCKET_BUFFSIZE )
- {
- // 包长度超过限制
- //LOG4C((LOG_NOTICE, "pHeader->nLen超过限制"));
- }
- if(-1 == OnCmdProcess( sockHandle, pHeader ))
- {
- LOG4C((LOG_NOTICE, "Error OnCmdProcess"));
- break;
- }
- }
- nBuffIndex += nProcessedLen;
- //LOG4C((LOG_NOTICE, "处理长度=%d, PendingSize=%d", nBuffIndex, sockHandle.m_nPendingSize));
- }
- }
- void CServerSocket::SocketStop()
- {
- m_SocketServer.Terminate();
- }
- int CServerSocket::SvrStart(CString strPort)
- {
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- if (!m_SocketServer.StartServer(NULL, strPort, nFamily, (m_nSockType+1) ))
- {
- LOG4C((LOG_NOTICE, _T("Failed to start server.")));
- };
- return 0;
- }
- bool CServerSocket::GetDestination(SockAddrIn& addrIn) const
- {
- CString strPort;
- //GetDlgItemText(IDC_SVR_PORT, strPort);
- int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
- return addrIn.CreateFrom(NULL, strPort, nFamily);
- }
- void CServerSocket::SocketSend(CSocketHandle &sockHandle, unsigned char *pMsg, int iLength)
- {
- if ( m_SocketServer.IsOpen() )
- {
- USES_CONVERSION;
- if (m_nSockType == SOCK_TCP)
- {
- // unsafe access to Socket list!
- const LPBYTE lpbData = (const LPBYTE)(pMsg);
- sockHandle.Write(lpbData, iLength, NULL);
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr ) {
- m_SocketServer.Write((const LPBYTE)*pMsg, iLength, sockAddr);
- } else {
-
- }
- }
- }
- else
- {
- }
- }
- void CServerSocket::GetAddress(const SockAddrIn& addrIn, CString& rString) const
- {
- TCHAR szIPAddr[MAX_PATH] = { 0 };
- CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
- rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
- }
- #if 0
- int CServerSocket::ProcessHeart(CSocketHandle &sockHandle, void *pBuffer)
- {
- ProtocolHeader *pHeader = (ProtocolHeader *)pBuffer;
- int nClientIndex = 0;
- CSocketHandle sockTmpHandle;
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- const SocketBufferList& sl = g_pTcpServer->m_SocketServer.GetSocketList();
- for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #else
- const SocketList& sl = g_pTcpServer->m_SocketServer.GetSocketList();
- for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #endif
- {
- sockTmpHandle.Attach( (*citer) );
- SockAddrIn sockTmpAddr, sockAddr;
- sockTmpHandle.GetPeerName(sockTmpAddr);
- sockHandle.GetPeerName(sockAddr);
- if( memcmp(&sockTmpAddr, &sockAddr, sizeof(SockAddrIn)) == 0 && sockTmpHandle.IsOpen() )
- {
- g_dwClientOnlineTick[nClientIndex] = GetTickCount();
- }
- sockTmpHandle.Detach();
- nClientIndex++;
- }
-
- return 0;
- }
- #endif
- int CServerSocket::ProcessResDevOnline(CSocketHandle &sockHandle, void *pBuffer)
- {
- DevOnline *tmpDevOnline = (DevOnline *)pBuffer;
- ReqDevOnline tmpReqline = {0};
- tmpReqline.Header.nCmd = tmpDevOnline->Header.nCmd;
- tmpReqline.Header.nCmdType = tmpDevOnline->Header.nCmdType;
- strcpy( (char *)tmpReqline.DevUid, (char *)tmpDevOnline->DevUid );
- tmpReqline.nVarID = tmpDevOnline->nVarID;
- //if( 17 == tmpReqline.nVarID )
- //{
- // LOG4C((LOG_NOTICE, "receive dev communication var"));
- //}
- int nDeviceIndex = -1, nVarIndex = -1;
- BOOL bFind = FindVar((char *)tmpReqline.DevUid, tmpReqline.nVarID, nDeviceIndex, nVarIndex);
- if( bFind == FALSE )
- {
- //LOG4C((LOG_NOTICE, "FindVar == FALSE"));
- return -1;
- }
- CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
- CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
- tmpReqline.nOnline = pBaseVar->m_nStatus;
- tmpReqline.nStatus = pBaseVar->m_nStatus;
- if( tmpReqline.nOnline == -1 )
- {
- tmpReqline.nOnline = 1;
- tmpReqline.nStatus = 1;
- //LOG4C((LOG_NOTICE, "tmpReqline.nOnline == -1"));
- //EnterCriticalSection( &g_csTcpReadOneData );
- //pBaseVar->m_nStatus = 1;
- //LeaveCriticalSection( &g_csTcpReadOneData );
- }
- tmpReqline.Header.Verify = 0;
- tmpReqline.Header.nLen = 44;
- int nSendLen = tmpReqline.Header.nLen;
-
- tmpReqline.Header.Verify = CalcCheckSum( &tmpReqline, nSendLen );
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqline, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqline, nSendLen);
- //LOG4C_HEX_DUMP((LOG_NOTICE, (char *)&tmpReqline, nSendLen));
- #endif
- return 0;
- }
- //单个变量应答设备在线状态
- int CServerSocket::ProcessResSingleVarOnline(CSocketHandle &sockHandle, void *pBuffer)
- {
- SingleVarOnline *tmpSnVarline = (SingleVarOnline *)pBuffer;
- ReqSingleVarOnline tmpReqSnVarline;
- memset(&tmpReqSnVarline, 0, sizeof(ReqSingleVarOnline) );
- tmpReqSnVarline.Header.nCmd = tmpSnVarline->Header.nCmd;
- tmpReqSnVarline.Header.nCmdType = tmpSnVarline->Header.nCmdType;
- strcpy((char *)tmpReqSnVarline.DevUid, (char *)tmpSnVarline->DevUid);
-
- tmpReqSnVarline.nOnline = 0x01;
- tmpReqSnVarline.nStatus = 0x02;
- tmpReqSnVarline.iVarid = tmpSnVarline->iVarid;
- tmpReqSnVarline.Header.Verify = 0;
- tmpReqSnVarline.Header.nLen = sizeof(ReqSingleVarOnline);
- int nSendLen = tmpReqSnVarline.Header.nLen;
- tmpReqSnVarline.Header.Verify = CalcCheckSum( &tmpReqSnVarline, nSendLen );
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSnVarline, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqSnVarline, nSendLen);
- #endif
-
- return 0;
- }
- //应答多个变量在线状态
- int CServerSocket::ProcessResMulVarOnline(CSocketHandle &sockHandle, void *pBuffer)
- {
- return 0;
- }
- //读单个变量内容或报警状态
- int CServerSocket::ProcessResReadSingleVarData(CSocketHandle &sockHandle, void *pBuffer)
- {
- ReadSingleVarData *pReadSnVar = (ReadSingleVarData *)pBuffer;
- ReqSingleVarData tmpReqSnVar;
-
- memset(&tmpReqSnVar, 0, sizeof(ReqSingleVarData));
- tmpReqSnVar.Header.nCmd = pReadSnVar->Header.nCmd;
- tmpReqSnVar.Header.nCmdType = pReadSnVar->Header.nCmdType;
- strcpy((char *)tmpReqSnVar.DevUid, (char *)pReadSnVar->DevUid);
- tmpReqSnVar.nVarID = pReadSnVar->nVarID;
- int nDeviceIndex = -1, nVarIndex = -1;
- BOOL bFind = FindVar((char *)pReadSnVar->DevUid, pReadSnVar->nVarID, nDeviceIndex, nVarIndex);
- if( bFind == FALSE ) return -1;
- CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
- CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
- if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE &&
- pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
- {
- if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 ||
- (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
- {
- return -1;
- }
- }
- tmpReqSnVar.nStatus = pBaseVar->m_nStatus;
- tmpReqSnVar.Data = pBaseVar->m_dbData;
- tmpReqSnVar.Header.Verify = 0;
- tmpReqSnVar.Header.nLen = sizeof(ReqSingleVarData);
- int nSendLen = tmpReqSnVar.Header.nLen;
- tmpReqSnVar.Header.Verify = CalcCheckSum( &tmpReqSnVar, nSendLen );
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
- #endif
- return 0;
- }
- //读多个变量内容或报警状态
- int CServerSocket::ProcessResReadMulVarData(CSocketHandle &sockHandle, void *pBuffer)
- {
- return 0;
- }
- //设置单个变量值
- int CServerSocket::ProcessResWriteSingleVarData(CSocketHandle &sockHandle, void *pBuffer)
- {
- WrSingleVarData *pWrSnVarData = (WrSingleVarData*)pBuffer;//请求数据包
- ReqWrSingleVarData tmpReqWrSnVar;//应答数据包
-
- //设置命令、命令类型、设备UID、变量UID
- memset(&tmpReqWrSnVar, 0, sizeof(ReqWrSingleVarData));
- tmpReqWrSnVar.Header.nCmd = pWrSnVarData->Header.nCmd;
- tmpReqWrSnVar.Header.nCmdType = pWrSnVarData->Header.nCmdType;
- strcpy((char *)tmpReqWrSnVar.DevUid, (char *)pWrSnVarData->DevUid);
- tmpReqWrSnVar.iVarid = pWrSnVarData->iVarid;
- int nDeviceIndex = -1, nVarIndex = -1;
- BOOL bFind = FindVar((char *)pWrSnVarData->DevUid, pWrSnVarData->iVarid, nDeviceIndex, nVarIndex);
- if( bFind == FALSE ) return -1;
- CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
- CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
- SETBASEPARAM SetBasePara;
- memset( &SetBasePara, 0, sizeof(SETBASEPARAM) );
- //网络设备
- char chDllName[MAX_PATH] = {0};
- strcpy(chDllName, (char *)(LPCTSTR)pDev->m_strProtocoldllname);
- StrLower(chDllName,chDllName);
- char szIniName[MAX_PATH] = {0};
- strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
- StrLower(szIniName, szIniName);
- if (pDev->m_bySpecialdevice == 0) // modbus rtu 设备
- {
- for( int i = RTU_THREAD_START; i < g_nRtuThreadIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- SuspendThread(g_hRequestDataThread[i]);
- }
- }
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if( RtuRequestWrData( (char *)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- for( int i = RTU_THREAD_START; i < g_nRtuThreadIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- ResumeThread(g_hRequestDataThread[i]);
- }
- }
- }
- else if( pDev->m_bySpecialdevice == 1 ) // modbus Ascii 设备
- {
- for( int i = ASC_THREAD_START; i < g_nAscThreadIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- SuspendThread(g_hRequestDataThread[i]);
- }
- }
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if (AscRequestWrData((char*)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
- {
- AscSingleResponseWriteData((char*)tmpReqWrSnVar.DevUid,tmpReqWrSnVar.iVarid);
- if (pBaseVar->m_dbData != pWrSnVarData->Data)//判断设置数据是否一致
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- for( int i = ASC_THREAD_START; i < g_nAscThreadIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- ResumeThread(g_hRequestDataThread[i]);
- }
- }
- }
- else if( pDev->m_bySpecialdevice == 2 ) // modbus tcp 设备
- {
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- //g_hRequestDataThread[g_nIcpdasIndex]
- #if 1
- ResetEvent( pDev->m_hSemNet );
- Sleep(5 * 1000); //等待5S,防止重置信号量之前,已经有变量在请求,保证变量已经读完
- #endif
- if( TcpRequestWriteData(pDev->m_iIpport,
- pDev->m_iDevideaddr,
- (char *)(LPCTSTR)pDev->m_strIp,
- pBaseVar->m_iRegisterNum,
- pBaseVar->m_nRegStartAddr,
- pBaseVar->m_iFuncID,
- (int)pWrSnVarData->Data) == 0 )
- {
- g_nIndex--;
- if( g_nIndex != 0 )
- {
- TRACE("ModbusTcp数据不同步!\r\n");
- }
- //TRACE(">>>>>>>>>>结束请求一次数据\r\n");
- #if 1
- SetEvent( pDev->m_hSemNet );
- #endif
- //SetEvent( g_hSemNet );
- //TRACE("变量ID=%d, 请求寄存器地址:%d, 变量值=%.1f\r\n", pReadSnVar->iVarid, atoi(chStartAddr), g_TcpReadOneData.dbData);
- //
- tmpReqWrSnVar.nStatus = 0;
- break;
- #if 0
- int nRet = TcpSingleResponseWriteData((char *)(LPCSTR)pBaseVar->m_strVarUID, pBaseVar->m_nVarID);
- if( nRet == 0 )
- {
- g_nIndex--;
- if( g_nIndex != 0 )
- {
- TRACE("ModbusTcp数据不同步!\r\n");
- }
- //TRACE(">>>>>>>>>>结束请求一次数据\r\n");
- SetEvent( g_hSemNet );
- //TRACE("变量ID=%d, 请求寄存器地址:%d, 变量值=%.1f\r\n", pReadSnVar->iVarid, atoi(chStartAddr), g_TcpReadOneData.dbData);
- //
- tmpReqWrSnVar.ReqVarData.nStatus = 0;
- break;
- }
- else
- {
- g_nIndex--;
- //TRACE("ModbusTcp响应数据错误!\r\n");
- SetEvent( g_hSemNet );
- tmpReqWrSnVar.ReqVarData.nStatus = -1;
- }
- #endif
- }
- #if 1
- else
- {
- SetEvent( pDev->m_hSemNet );
- }
- #endif
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- }
- else if( pDev->m_bySpecialdevice == 3 ) // snmp 设备
- {
- CString strSetValue;
- strSetValue.Format("%.1f", pWrSnVarData->Data);
- if( pSnmpDllSetValue(
- (char *)(LPCTSTR)pDev->m_strIp,
- (char *)(LPCTSTR)pBaseVar->m_strFields,
- (char *)(LPCTSTR)pBaseVar->m_strSnmpoi,
- "int",
- 1,
- 300,
- (char *)(LPCTSTR)strSetValue
- ) == 0 )
- {
- tmpReqWrSnVar.nStatus = 0;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- }
- else if( pDev->m_bySpecialdevice == 4 ) // RS232 设备
- {
- if (strcmp(chDllName, "icpdas.dll") ==0)
- {
- for( int i = ICPDAS_THREAD_START; i < g_nIcpdasIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- int nRet = SuspendThread(g_hRequestDataThread[i]);
- LOG4C((LOG_NOTICE, "SuspendThread return %d", nRet));
- }
- }
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if (RequestWrIcpdasData((char*)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- for( int i = ICPDAS_THREAD_START; i < g_nIcpdasIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- int nRet = ResumeThread(g_hRequestDataThread[i]);
- LOG4C((LOG_NOTICE, "ResumeThread return %d", nRet));
- }
- }
- }
- else if (strcmp(chDllName, "stulz.dll") ==0)
- {
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if (RequestWrStulzData((char*)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- }
- else if( strcmp(chDllName, "daikin.dll") == 0 )
- {
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if (RequestDaiKinWrData( (char *)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
-
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- }
- else if( strcmp(chDllName, "gree.dll") == 0 )
- {
- int nSetCount = 0;
- // 写10次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 10 ) break;
- if (RequestGreeWrData( (char *)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- }
- else if( strcmp(chDllName, "carel.dll") == 0 )
- {
- int nSetCount = 0;
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if (CarelControlRequest( (char *)tmpReqWrSnVar.DevUid,
- tmpReqWrSnVar.iVarid, pWrSnVarData->Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
- }
- else if ( strcmp(szIniName, "upsuh31.ini") == 0 ||
- strcmp(szIniName, "emerson_cm.ini") == 0 ||
- strcmp(szIniName, "dme3000.ini") == 0 ||
- strcmp(szIniName, "paradigm200a_rectifier.ini") == 0 ||
- strcmp(szIniName, "paradigm200a_inverter.ini") == 0 ||
- strcmp(szIniName, "paradigm200b.ini") == 0 ||
- strcmp(szIniName, "hipulseigmnet.ini") == 0 ||
- strcmp(szIniName, "hipulsecdt.ini") == 0 ||
- strcmp(szIniName, "prostar.ini") == 0 ||
- strcmp(szIniName, "joton.ini") == 0 ||
- strcmp(szIniName, "hiross.ini") == 0 ||
- strcmp(szIniName, "upslink.ini") == 0 ||
- strcmp(szIniName, "upsapcsilcon.ini") == 0 ||
- strcmp(szIniName, "upsuh11.ini") == 0 ||
- strcmp(szIniName, "weibo.ini") == 0 ||
- strcmp(szIniName, "daikin.ini") == 0 ||
- strcmp(szIniName, "scantak_3c.ini") == 0 ||
- strcmp(szIniName, "delta.ini") == 0 ||
- strcmp(szIniName, "sda_ii.ini") == 0 ||
- strcmp(szIniName, "m84c5z.ini") == 0 ||
- strcmp(szIniName, "batteryadu.ini") == 0 ||
- strcmp(szIniName, "upsmonitorlan.ini") == 0 ||
- strcmp(szIniName, "sk6000.ini") == 0 ||
- strcmp(szIniName, "batteryadu2000.ini") == 0 ||
- strcmp(szIniName, "santak_shiwei.ini") == 0 ||
- strcmp(szIniName, "santak_q4.ini") == 0 ||
- strcmp(szIniName, "ups_nxl.ini") == 0 ||
- strcmp(szIniName, "twoul33.ini") == 0 ||
- strcmp(szIniName, "oumai.ini") == 0 ||
- strcmp(szIniName, "santak_3a3120ks.ini") == 0 ||
- strcmp(szIniName, "tmaster.ini") == 0 ||
- strcmp(szIniName, "jb1501a64p.ini") == 0 ||
- strcmp(szIniName, "st_3c340ks.ini") == 0 ||
- strcmp(szIniName, "delta_v1.54.ini") == 0||
- strcmp(szIniName, "icp_i7017.ini") == 0 ||
- strcmp(szIniName, "fad.ini") == 0 ||
- strcmp(szIniName, "itrustadapt.ini" ) == 0 ||
- strcmp(szIniName, "uha3r_0200l.ini") == 0)
- {
- for( int i = MOSTDEVICE_THREAD_START; i < g_nMostDeviceIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- SuspendThread(g_hRequestDataThread[i]);
- }
- }
- int nSetCount = 0;
- char szMsg[MAX_VAR_MSG] = {0};
- //memcpy(szMsg, &pWrSnVarData->VarData.Data, sizeof(double));
- itoa((int)pWrSnVarData->Data, szMsg, 16);
- ByteToUpper(szMsg);
- // 写3次串口,如果不成功则放弃
- do
- {
- if( nSetCount > 3 ) break;
- if (MostDeviceRequestSet(pDev->m_iPort, pDev->m_iDevideaddr,
- (char*)(LPCTSTR)tmpReqWrSnVar.DevUid, tmpReqWrSnVar.iVarid,
- (char*)(LPCTSTR)pBaseVar->m_strRs232cmd,
- szIniName, szMsg) == 0)
- //if (CarelControlRequest( (char *)tmpReqWrSnVar.DevUid,
- // tmpReqWrSnVar.ReqVarData.iVarid, pWrSnVarData->VarData.Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
- {
- tmpReqWrSnVar.nStatus = 0;
- break;
- }
- else
- {
- tmpReqWrSnVar.nStatus = -1;
- }
- nSetCount++;
- }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
-
- for( int i = MOSTDEVICE_THREAD_START; i < g_nMostDeviceIndex; i++ )
- {
- if( NULL != g_hRequestDataThread[i] )
- {
- ResumeThread(g_hRequestDataThread[i]);
- }
- }
- }
- }
- tmpReqWrSnVar.Header.Verify = 0;//校验码
- tmpReqWrSnVar.Header.nLen = sizeof(ReqWrSingleVarData);//数据包长度
- int nSendLen = tmpReqWrSnVar.Header.nLen;
- tmpReqWrSnVar.Header.Verify = CalcCheckSum( &tmpReqWrSnVar, nSendLen );//进行校验
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqWrSnVar, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqWrSnVar, nSendLen);
- #endif
-
- return 0;
- }
- int CServerSocket::PrcocessResWriteMulVarData(CSocketHandle &sockHandle, void *pBuffer)
- {
- return 0;
- }
- int CServerSocket::ProcessResSoftDog(CSocketHandle &sockHandle, void *pBuffer)
- {
- Dog_Data *pDogData = (Dog_Data *)pBuffer;
- ReqDogData tmpReqDogData;
- int nSendLen = sizeof(ReqDogData);
- memset( &tmpReqDogData, 0, nSendLen);
- tmpReqDogData.Header.nCmd = pDogData->Header.nCmd;
- tmpReqDogData.Header.nCmdType = pDogData->Header.nCmdType;
- tmpReqDogData.Header.nLen = nSendLen;
- tmpReqDogData.nUseLimit = g_nUseTimeLimit;
- tmpReqDogData.Header.Verify = CalcCheckSum( &tmpReqDogData, nSendLen );
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqDogData, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqDogData, nSendLen);
- #endif
- return 0;
- }
- int CServerSocket::ProcessResAlarmConfirm(CSocketHandle &sockHandle, void *pBuffer)
- {
- AlarmConfirm *tag = (AlarmConfirm *)pBuffer;
- DeleteSendNoticeList( (char *)tag->DevUid,tag->iVarid,tag->nAlarmIndex );
- DeleteSendSmsList( (char *)tag->DevUid,tag->iVarid,tag->nAlarmIndex );
- return 0;
- }
- //请求读特殊变量的数据
- int CServerSocket::ProcessResReadSpecSingleVarData(CSocketHandle &sockHandle, void *pBuffer)
- {
- ReadSingleSpecialVarData *pReadSpecSnVar = (ReadSingleSpecialVarData *)pBuffer;
- ReqSingleSpecialVarData tmpReqSpecSnVar;
-
- memset(&tmpReqSpecSnVar, 0, sizeof(ReqSingleSpecialVarData));
- tmpReqSpecSnVar.Header.nCmd = pReadSpecSnVar->Header.nCmd;
- tmpReqSpecSnVar.Header.nCmdType = pReadSpecSnVar->Header.nCmdType;
- strcpy((char *)tmpReqSpecSnVar.DevUid, (char *)pReadSpecSnVar->DevUid);
- tmpReqSpecSnVar.iSpecialFlag = pReadSpecSnVar->iSpecialFlag;
- tmpReqSpecSnVar.nVarID = pReadSpecSnVar->nVarID;
- int nDeviceIndex = -1, nVarIndex = -1;
- BOOL bFind = FindVar((char *)pReadSpecSnVar->DevUid, pReadSpecSnVar->nVarID, nDeviceIndex, nVarIndex);
- if( bFind == FALSE )
- {
- return -1;
- }
- CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
- CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
- if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE &&
- pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
- {
- if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 ||
- (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
- {
- return -1;
- }
- }
- tmpReqSpecSnVar.nStatus = pBaseVar->m_nStatus;
- tmpReqSpecSnVar.Data = pBaseVar->m_dbData;
- tmpReqSpecSnVar.Header.Verify = 0;
- tmpReqSpecSnVar.Header.nLen = sizeof(ReqSingleSpecialVarData);
- int nSendLen = tmpReqSpecSnVar.Header.nLen;
- tmpReqSpecSnVar.Header.Verify = CalcCheckSum( &tmpReqSpecSnVar, nSendLen );
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSpecSnVar, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqSpecSnVar, nSendLen);
- #endif
- return 0;
- }
- //请求读特殊变量的数据
- int CServerSocket::ProcessResReadSpecMulVarData(CSocketHandle &sockHandle, void *pBuffer)
- {
- return 0;
- }
- //重新读取数据库通知
- int CServerSocket::ProcessNoticeReadDBVar(CSocketHandle &sockHandle, void *pBuffer)
- {
- LOG4C((LOG_NOTICE, "Enter ProcessNoticeReadDBVar 再次读取变量!"));
- NoticeReadDBVar *pNoticeReadDBVar = (NoticeReadDBVar *)pBuffer;
- int nDeviceIndex = -1, nVarIndex = -1;
- g_pDevicesManager->FindDevByUid((CString)pNoticeReadDBVar->DevUid, nDeviceIndex);
- if( -1 == nDeviceIndex ) return 0;
- //LOG4C((LOG_NOTICE, "nDeviceIndex = %d", nDeviceIndex));
- g_pDevicesManager->FindVar(nDeviceIndex, pNoticeReadDBVar->nVarID, nVarIndex);
- //LOG4C((LOG_NOTICE, "nVarIndex = %d", nVarIndex));
- if( -1 == nVarIndex ) return 0;
- switch( pNoticeReadDBVar->nOperate )
- {
- case NOTICE_NEW:
- break;
- case NOTICE_EDIT:
- EnterCriticalSection( &m_csProcessData );
- //LOG4C((LOG_NOTICE, "Uid = %s, varid = %d", pNoticeReadDBVar->DevUid, pNoticeReadDBVar->nVarID));
- if( strcmp((char *)pNoticeReadDBVar->DevUid, "") != 0 && pNoticeReadDBVar->nVarID > 0 )
- {
- CDBInterface::GetInstancePtr()->EditVarInfo(nDeviceIndex, nVarIndex, (char *)(LPCTSTR)pNoticeReadDBVar->DevUid, pNoticeReadDBVar->nVarID);
- }
- LeaveCriticalSection( &m_csProcessData );
- break;
- case NOTICE_DEL:
- break;
- }
- return 0;
- }
- int CServerSocket::ProcessNoticeReadDBDev(CSocketHandle &sockHandle, void *pBuffer)
- {
- LOG4C((LOG_NOTICE, "Enter ProcessNoticeReadDBDev 再次读取设备!"));
- NoticeReadDBDev *pNoticeReadDBDev = (NoticeReadDBDev *)pBuffer;
- switch( pNoticeReadDBDev->nOperate )
- {
- case NOTICE_NEW:
- break;
- case NOTICE_EDIT:
- EnterCriticalSection( &m_csProcessData );
- LeaveCriticalSection( &m_csProcessData );
- break;
- case NOTICE_DEL:
- break;
- }
- return 0;
- }
- int CServerSocket::ProcessNoticeReadDBUser(CSocketHandle &sockHandle, void *pBuffer)
- {
- EnterCriticalSection( &m_csProcessData );
- CDBInterface::GetInstancePtr()->UnInitUserRoleInfo();
- CDBInterface::GetInstancePtr()->InitUserRoleInfo();
- LeaveCriticalSection( &m_csProcessData );
- return 0;
- }
- int CServerSocket::ProcessNoticeTestAlarm(CSocketHandle &sockHandle, void *pBuffer)
- {
- char chTel[MAX_TEL_LENGTH] = {0};
- char chMobile[MAX_TEL_LENGTH] = {0};
- char chFax[MAX_TEL_LENGTH] = {0};
- char chEmail[MAX_EMAIL_LENGTH] = {0};
- list<string> list_tel, list_mobile, list_fax, list_email;
- list<string>::iterator it_tel, it_mobile, it_fax, it_email;
- int nUserCount = CDBInterface::GetInstancePtr()->GetUserInfoList(list_tel, list_mobile, list_fax, list_email);
- if( 0 == nUserCount )
- {
- LOG4C((LOG_NOTICE, "0 == nUserCount"));
- return -1;
- }
- if( 0 == list_tel.size() )
- {
- LOG4C((LOG_NOTICE, "0 == list_tel.size()"));
- return -1;
- }
- char szResultMsg[MAX_EQUIP_DESC + 1] = {0};
- int nResult = -1;
- memset(szResultMsg, 0, sizeof(szResultMsg));
- for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
- it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
- {
- strcpy(chTel, (*it_tel++).c_str());
- strcpy(chMobile, (*it_mobile++).c_str());
- strcpy(chFax, (*it_fax++).c_str());
- strcpy(chEmail, (*it_email++).c_str());
- if( g_nSendSms==1 )
- {
- if( strcmp(chMobile, "") != 0 )
- {
- CString strMobileTel;
- strMobileTel.Format("%s",chMobile );
- if( strMobileTel.GetLength()!=11 ) continue; //判断是不是手机号码 只根据长度
- if ( pSmsDllSendSms && g_bSuccesOpen)
- {
- nResult = pSmsDllSendSms(chMobile, (char *)(LPCTSTR)g_strHintSysRun, szResultMsg);
- }
- if( nResult != 0 ) // 发送失败
- {
- }
- }
- }
- if( g_nSendNotice == 1 )
- {
- if( strcmp(chTel, "") != 0 )
- {
- //LOG4C((LOG_NOTICE, "chTel = %s insertSendNoticeList", chTel));
- // 继续加入语音发送list
- EnterCriticalSection( &g_csVarNotice );
- InsertSendNoticeList("0.0.0.0", 1, chTel, (char *)(LPCTSTR)g_strHintSysRun,0);
- LeaveCriticalSection( &g_csVarNotice );
- }
- #if 0 //移动电话 不打电话
- if( strcmp(chMobile, "") != 0 )
- {
- //LOG4C((LOG_NOTICE, "chMobile = %s insertSendNoticeList", chMobile));
- // 继续加入语音发送list
- EnterCriticalSection( &g_csVarNotice );
- InsertSendNoticeList("0.0.0.0", 1, chMobile, (char *)(LPCTSTR)g_strHintSysRun);
- LeaveCriticalSection( &g_csVarNotice );
- }
- #endif
- }
- }
- for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
- it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
- {
- list_tel.erase(it_tel++);
- list_mobile.erase(it_mobile++);
- list_fax.erase(it_fax++);
- list_email.erase(it_email++);
- }
- return 0;
- }
- void CServerSocket::AutoSendDataToAllClient( CBaseVar *pBaseVar )
- {
- int nResult = 0;
- if (m_nSockType == SOCK_TCP)
- {
- // unsafe access to Socket list!
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- const SocketBufferList& sl = m_SocketServer.GetSocketList();
- for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #else
- const SocketList& sl = m_SocketServer.GetSocketList();
- for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #endif
- {
- //LOG4C((LOG_NOTICE, "Socket number = %d ", sl.size()));
- EnterCriticalSection( &m_csProcessData );
- CSocketHandle sockHandle;
- sockHandle.m_nPendingSize = 0;
- memset(sockHandle.m_PendingBuffer, 0, SOCKET_BUFFSIZE);
- sockHandle.Attach( (*citer) );
- //if( sockAddr == addr )
- {
- ProcessAutoSendVarDataToClient( sockHandle,pBaseVar );
- }
- sockHandle.Detach();
- LeaveCriticalSection( &m_csProcessData );
- }
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr ) {
- //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
- } else {
- //AppendText( _T("Please change the port number to send message to a client.\r\n") );
- }
- }
- }
- //主动发变量的值给客户端 没有简单变量,特殊变量之分
- int CServerSocket::ProcessAutoSendVarDataToClient(CSocketHandle &sockHandle, CBaseVar *pBaseVar )
- {
- ReqSingleVarData tmpReqSnVar;
-
- memset(&tmpReqSnVar, 0, sizeof(ReqSingleVarData));
- tmpReqSnVar.Header.nCmd = CMD_READ_SINGLE_VAR_DATA;
- tmpReqSnVar.Header.nCmdType = 0;
- strcpy((char *)tmpReqSnVar.DevUid, (char *)(LPCTSTR)pBaseVar->m_strVarUID);
- tmpReqSnVar.nVarID = pBaseVar->m_nVarID;
- tmpReqSnVar.nStatus = pBaseVar->m_nStatus;
- tmpReqSnVar.Data = pBaseVar->m_dbData;
- tmpReqSnVar.Header.Verify = 0;
- tmpReqSnVar.Header.nLen = sizeof(ReqSingleVarData);
- int nSendLen = tmpReqSnVar.Header.nLen;
- tmpReqSnVar.Header.Verify = CalcCheckSum( &tmpReqSnVar, nSendLen );
- #if 0
- if( g_pTcpServer != NULL )
- {
- g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
- }
- #else
- SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
- #endif
- return 0;
- }
- //有报警,自动发给客户端
- int CServerSocket::ProcessAutoSendAlarmDataToClient( CSocketHandle &sockHandle,CHAR *pDevUid, int iVarID, int iStatus,CHAR *pContent
- ,double dbParaValue, int nUpperLimit,int nLowerLimit, int nNormalState,int nAlarmState,int nAlarmIndex )
- {
- AutoSendAlarmData tmpAlarmData;
-
- memset(&tmpAlarmData, 0, sizeof(AutoSendAlarmData));
- tmpAlarmData.Header.nCmd = CMD_ALARM_VAR_DATA;
- tmpAlarmData.Header.nCmdType = 0;
- strcpy( (char *)tmpAlarmData.DevUid, pDevUid );
- tmpAlarmData.nVarID = iVarID;
- strcpy( (char *)tmpAlarmData.sContent, pContent );
- tmpAlarmData.nStatus = iStatus;
- tmpAlarmData.Data = dbParaValue;
- tmpAlarmData.nUpperLimit = nUpperLimit;
- tmpAlarmData.nLowerLimit = nLowerLimit;
- tmpAlarmData.nNormalState = nNormalState;
- tmpAlarmData.nAlarmState = nAlarmState;
- tmpAlarmData.nAlarmIndex = nAlarmIndex;
- tmpAlarmData.Header.Verify = 0;
- tmpAlarmData.Header.nLen = sizeof(AutoSendAlarmData);
- int nSendLen = tmpAlarmData.Header.nLen;
- tmpAlarmData.Header.Verify = CalcCheckSum( &tmpAlarmData, nSendLen );
- SocketSend(sockHandle, (unsigned char *)&tmpAlarmData, nSendLen);
- return 0;
- }
- void CServerSocket::AutoSendAlarmDataToClient( CHAR *pDevUid, int iVarID, int iStatus,CHAR *pContent,double dbParaValue,
- int nUpperLimit,int nLowerLimit, int nNormalState,int nAlarmState,int nAlarmIndex )
- {
- int nResult = 0;
- if (m_nSockType == SOCK_TCP)
- {
- // unsafe access to Socket list!
- #ifdef SOCKHANDLE_USE_OVERLAPPED
- const SocketBufferList& sl = m_SocketServer.GetSocketList();
- for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #else
- const SocketList& sl = m_SocketServer.GetSocketList();
- for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
- #endif
- {
- //LOG4C((LOG_NOTICE, "Socket number = %d ", sl.size()));
- EnterCriticalSection( &m_csProcessData );
- CSocketHandle sockHandle;
- sockHandle.m_nPendingSize = 0;
- memset(sockHandle.m_PendingBuffer, 0, SOCKET_BUFFSIZE);
- sockHandle.Attach( (*citer) );
- //if( sockAddr == addr )
- {
- ProcessAutoSendAlarmDataToClient( sockHandle,pDevUid,iVarID,iStatus,pContent,dbParaValue, nUpperLimit,nLowerLimit, nNormalState,nAlarmState,nAlarmIndex );
- }
- sockHandle.Detach();
- LeaveCriticalSection( &m_csProcessData );
- }
- }
- else
- {
- SockAddrIn servAddr, sockAddr;
- m_SocketServer->GetSockName(servAddr);
- GetDestination(sockAddr);
- if ( servAddr != sockAddr ) {
- //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
- } else {
- //AppendText( _T("Please change the port number to send message to a client.\r\n") );
- }
- }
- }
- };
|