#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(); }