123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- #include "stdafx.h"
- #include "kernel.h"
- //#include "sharedefine.h"
- #include "Global.h"
- #include "NetWork.h"
- #include "TCPClient.h"
- #pragma warning(push)
- #pragma warning(disable:4995)
- #pragma warning(pop)
- const int AF_IPV4 = 0;
- const int AF_IPV6 = 1;
- const int SOCK_TCP = SOCK_STREAM-1;
- const int SOCK_UDP = SOCK_DGRAM-1;
- //---------------------------------------------- CDevicesManager ----
- CDevicesManager::CDevicesManager()
- {
- m_bIsPastDue = false;
- m_nSlaveVersion = 0;
- m_bIsBlanceSmall = true;
- m_sBanceContent = "";
- }
- CDevicesManager::~CDevicesManager()
- {
- ClearUp();
- }
- void CDevicesManager::Load(char *pStrDirectroy)
- {
- ClearUp();
- CFile file;
- CString fileName = CString(pStrDirectroy) + "\\" + _PROJECTDIR;
- fileName = fileName + DEVICE_FILE;
- if (file.Open(fileName, CFile::modeRead))
- {
- CArchive ar(&file,CArchive::load);
- m_channels.Serialize(ar);
- ar.Close();
- file.Close();
- }
- }
- void CDevicesManager::Store(char *pStrDirectroy)
- {
- CFile file;
- CFileException e;
- CString fileName = _PROJECTDIR;
- fileName = fileName + DEVICE_FILE;
- fileName = CString(pStrDirectroy) + "\\" + fileName;
- DeleteFile( fileName );
- if(file.Open(fileName, CFile::modeWrite|CFile::modeCreate, &e))
- {
- CArchive ar(&file,CArchive::store);
- m_channels.Serialize(ar);
- ar.Close();
- file.Close();
- }
- else
- {
- #ifdef _DEBUG
- afxDump << "File could not be opened " << e.m_cause << "\n";
- #endif
- }
- }
- void CDevicesManager::FreshData(void)
- {
- int nSize = m_channels.GetSize();
- for(int i = 0; i < nSize; i++)
- {
- CChannel* pChannel = m_channels[i];
- pChannel->FreshData();
- }
- }
- void CDevicesManager::ClearUp()
- {
- int nSize = m_channels.GetSize();
- for (int i = 0; i < m_channels.GetSize(); i++)
- {
- //int n = m_channels[i]->m_Devices.GetSize();
- //for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
- //{
- // delete m_channels[i]->m_Devices[j];
- //}
- MTVERIFY(delete m_channels[i]);
- }
- m_channels.RemoveAll();
- }
- int CDevicesManager::FindCommPort(CString strName)
- {
- int nRet = -1;
- for( int i = 0; i < m_channels.GetSize(); i++ )
- {
- if( m_channels[i]->m_strName == strName ) // 端口
- {
- nRet = i;
- break;
- }
- }
- return nRet;
- }
- BOOL CDevicesManager::FindCommPort(CString strName, int nAddr, int &nChannlIndex, int &nDeviceIndex)
- {
- BOOL bResult = FALSE;
- bool bBreak = false;
- for( int i = 0; i < m_channels.GetSize(); i++ )
- {
- for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
- {
- if( m_channels[i]->m_Devices[j]->m_Address == nAddr && // 地址
- m_channels[i]->m_strName == strName) // 端口
- {
- bResult = TRUE;
- nChannlIndex = i;
- nDeviceIndex = j;
- bBreak = true;
- break;
- }
- }
- if( bBreak )
- break;
- }
- return bResult;
- }
- BOOL CDevicesManager::FindDev(CString strName, int &nChannlIndex, int &nDeviceIndex)
- {
- BOOL bResult = FALSE;
- bool bBreak = false;
- for( int i = 0; i < m_channels.GetSize(); i++ )
- {
- for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
- {
- if( m_channels[i]->m_Devices[j]->m_strName == strName )
- {
- bResult = TRUE;
- nChannlIndex = i;
- nDeviceIndex = j;
-
- bBreak = true;
- break;
- }
- }
- if( bBreak )
- break;
- }
- return bResult;
- }
- BOOL CDevicesManager::FindDev(CString strIP, int m_nPort, int &nChannlIndex, int &nDeviceIndex)
- {
- BOOL bResult = FALSE;
- bool bBreak = false;
- for( int i = 0; i < m_channels.GetSize(); i++ )
- {
- for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
- {
- if( ((CNetwork*)m_channels[i])->m_Devices[j]->m_strDevIP == strIP &&
- ((CNetwork*)m_channels[i])->m_Devices[j]->m_nNetPort == m_nPort )
- {
- bResult = TRUE;
- nChannlIndex = i;
- nDeviceIndex = j;
-
- bBreak = true;
- break;
- }
- }
- if( bBreak )
- break;
- }
- return bResult;
- }
- bool CDevicesManager::IsExistDev( CString sDevName )//判断设备是否存在
- {
- if( sDevName=="" )
- return FALSE;
- bool bRet = false;
- for( int i = 0; i < m_channels.GetSize(); i++ )
- {
- for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
- {
- if( m_channels[i]->m_Devices[j]->m_strName == sDevName )
- {
- return true;
- }
- }
- }
- return bRet;
- }
- int CDevicesManager::GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
- {
- int iResult = iNormalState;
- if ( (iUpperLimit > 0 || iLowerLimit > 0 ) && iUpperLimit > iLowerLimit )
- {
- //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
- if( iParaValue > iUpperLimit )
- {
- iResult = UPPER_LIMIT_ID; // 上限告警
- }
- if( iParaValue < iLowerLimit )
- {
- iResult = LOWER_LIMIT_ID; // 下限告警
- }
- }
- else if( (int)iParaValue != iNormalState )
- {
- iResult = (int)iParaValue;
- }
- return iResult;
- }
- void CDevicesManager::SetDBType(char *pDBType)
- {
- m_strDBType = CString(pDBType);
- CDBInterface::GetInstancePtr()->SetDBType( pDBType );
- }
- INT CDevicesManager::GetDevInfo(void)
- {
- CDBInterface::GetInstancePtr()->GetDevInfo( );
- return 0;
- }
- BOOL CDevicesManager::Connection(LPCTSTR strAddr, LPCTSTR strPort)
- {
- int nTCPIndex = atoi(strPort)-64320;
- return CTCPClient::GetInstancePtr(nTCPIndex)->Connection(strAddr, strPort);
- }
- void CDevicesManager::DisConnection( LPCTSTR strPort )
- {
- int nTCPIndex = atoi(strPort)-64320;
- CTCPClient::GetInstancePtr(nTCPIndex)->DisConnection();
- }
- BOOL CDevicesManager::GetOpenStatus(LPCTSTR strPort)
- {
- int nTCPIndex = atoi(strPort)-64320;
- return CTCPClient::GetInstancePtr(nTCPIndex)->m_SocketClient.IsOpen();
- //AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
- }
- BOOL CDevicesManager::GetSocketStatus(LPCTSTR strPort)
- {
- int nTCPIndex = atoi(strPort)-64320;
- return CTCPClient::GetInstancePtr(nTCPIndex)->m_bSocket;
- }
- void CDevicesManager::SetSocketStatus(BOOL bSocket,LPCTSTR strPort)
- {
- int nTCPIndex = atoi(strPort)-64320;
- CTCPClient::GetInstancePtr(nTCPIndex)->m_bSocket = bSocket;
- }
- BOOL CDevicesManager::SendNoticeToServer( int iCmd,int iOperateType,CString sUserName,CString sUID,int iVarID,LPCTSTR strPort )
- {
- int nTCPIndex = atoi(strPort)-64320;
- return CTCPClient::GetInstancePtr(nTCPIndex)->SendNoticeToServer( iCmd,iOperateType,sUserName,sUID,iVarID );
- }
- BOOL CDevicesManager::GetServerVer(LPCTSTR strPort)
- {
- int nTCPIndex = atoi(strPort)-64320;
- return CTCPClient::GetInstancePtr(nTCPIndex)->GetServerVer();
- }
- BOOL CDevicesManager::TestAlarm(LPCTSTR strPort)
- {
- int nTCPIndex = atoi(strPort)-64320;
- return CTCPClient::GetInstancePtr(nTCPIndex)->TestAlarm();
- }
- BOOL CDevicesManager::ConfigAlarm( CString sUID,int iVarID,int iStatus,int iAlarmIndex )
- {
- return CTCPClient::GetInstancePtr(0)->ConfigAlarm( sUID,iVarID,iStatus,iAlarmIndex );
- }
- void CDevicesManager::Release(LPCTSTR strPort)
- {
- CDBInterface::GetInstancePtr()->Release();//####
- int nTCPIndex = atoi(strPort)-64320;
- CTCPClient::GetInstancePtr(nTCPIndex)->Release();
- }
|