DevicesManager.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "stdafx.h"
  2. #include "kernel.h"
  3. #include "sharedefine.h"
  4. #include "Global.h"
  5. #include "NetWork.h"
  6. #include "TCPClient.h"
  7. #pragma warning(push)
  8. #pragma warning(disable:4995)
  9. #pragma warning(pop)
  10. const int AF_IPV4 = 0;
  11. const int AF_IPV6 = 1;
  12. const int SOCK_TCP = SOCK_STREAM-1;
  13. const int SOCK_UDP = SOCK_DGRAM-1;
  14. //---------------------------------------------- CDevicesManager ----
  15. CDevicesManager::CDevicesManager()
  16. {
  17. m_bIsPastDue = false;
  18. }
  19. CDevicesManager::~CDevicesManager()
  20. {
  21. ClearUp();
  22. }
  23. void CDevicesManager::Load(char *pStrDirectroy)
  24. {
  25. ClearUp();
  26. CFile file;
  27. CString fileName = CString(pStrDirectroy) + "\\" + _PROJECTDIR;
  28. fileName = fileName + DEVICE_FILE;
  29. if (file.Open(fileName, CFile::modeRead))
  30. {
  31. CArchive ar(&file,CArchive::load);
  32. m_channels.Serialize(ar);
  33. ar.Close();
  34. file.Close();
  35. }
  36. }
  37. void CDevicesManager::Store(char *pStrDirectroy)
  38. {
  39. CFile file;
  40. CFileException e;
  41. CString fileName = _PROJECTDIR;
  42. fileName = fileName + DEVICE_FILE;
  43. fileName = CString(pStrDirectroy) + "\\" + fileName;
  44. if(file.Open(fileName, CFile::modeWrite|CFile::modeCreate, &e))
  45. {
  46. CArchive ar(&file,CArchive::store);
  47. m_channels.Serialize(ar);
  48. ar.Close();
  49. file.Close();
  50. }
  51. else
  52. {
  53. #ifdef _DEBUG
  54. afxDump << "File could not be opened " << e.m_cause << "\n";
  55. #endif
  56. }
  57. }
  58. void CDevicesManager::FreshData(void)
  59. {
  60. int nSize = m_channels.GetSize();
  61. for(int i = 0; i < nSize; i++)
  62. {
  63. CChannel* pChannel = m_channels[i];
  64. pChannel->FreshData();
  65. }
  66. }
  67. void CDevicesManager::ClearUp()
  68. {
  69. for (int i = 0; i < m_channels.GetSize(); i++)
  70. {
  71. MTVERIFY(delete m_channels[i]);
  72. }
  73. m_channels.RemoveAll();
  74. }
  75. int CDevicesManager::FindCommPort(CString strName)
  76. {
  77. int nRet = -1;
  78. for( int i = 0; i < m_channels.GetSize(); i++ )
  79. {
  80. if( m_channels[i]->m_strName == strName ) // 端口
  81. {
  82. nRet = i;
  83. }
  84. }
  85. return nRet;
  86. }
  87. BOOL CDevicesManager::FindCommPort(CString strName, int nAddr, int &nChannlIndex, int &nDeviceIndex)
  88. {
  89. BOOL bResult = FALSE;
  90. for( int i = 0; i < m_channels.GetSize(); i++ )
  91. {
  92. for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  93. {
  94. if( m_channels[i]->m_Devices[j]->m_Address == nAddr && // 地址
  95. m_channels[i]->m_strName == strName) // 端口
  96. {
  97. bResult = TRUE;
  98. nChannlIndex = i;
  99. nDeviceIndex = j;
  100. }
  101. }
  102. }
  103. return bResult;
  104. }
  105. BOOL CDevicesManager::FindDev(CString strName, int &nChannlIndex, int &nDeviceIndex)
  106. {
  107. BOOL bResult = FALSE;
  108. for( int i = 0; i < m_channels.GetSize(); i++ )
  109. {
  110. for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  111. {
  112. if( m_channels[i]->m_Devices[j]->m_strName == strName )
  113. {
  114. bResult = TRUE;
  115. nChannlIndex = i;
  116. nDeviceIndex = j;
  117. }
  118. }
  119. }
  120. return bResult;
  121. }
  122. BOOL CDevicesManager::FindDev(CString strIP, int m_nPort, int &nChannlIndex, int &nDeviceIndex)
  123. {
  124. BOOL bResult = FALSE;
  125. for( int i = 0; i < m_channels.GetSize(); i++ )
  126. {
  127. for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  128. {
  129. if( ((CNetwork*)m_channels[i])->m_Devices[j]->m_strDevIP == strIP &&
  130. ((CNetwork*)m_channels[i])->m_Devices[j]->m_nNetPort == m_nPort )
  131. {
  132. bResult = TRUE;
  133. nChannlIndex = i;
  134. nDeviceIndex = j;
  135. }
  136. }
  137. }
  138. return bResult;
  139. }
  140. int CDevicesManager::GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
  141. {
  142. int iResult = iNormalState;
  143. if ( (iUpperLimit > 0 || iLowerLimit > 0 ) && iUpperLimit > iLowerLimit )
  144. {
  145. //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
  146. if( iParaValue > iUpperLimit )
  147. {
  148. iResult = UPPER_LIMIT_ID; // 上限告警
  149. }
  150. if( iParaValue < iLowerLimit )
  151. {
  152. iResult = LOWER_LIMIT_ID; // 下限告警
  153. }
  154. }
  155. else if( (int)iParaValue != iNormalState )
  156. {
  157. iResult = (int)iParaValue;
  158. }
  159. return iResult;
  160. }
  161. // 连接服务器;
  162. BOOL CDevicesManager::Connection(LPCTSTR strAddr, LPCTSTR strPort)
  163. {
  164. int nTCPIndex = atoi(strPort)-64320;
  165. return CTCPClient::GetInstancePtr(nTCPIndex)->Connection(strAddr, strPort);
  166. }
  167. void CDevicesManager::DisConnection( LPCTSTR strPort )
  168. {
  169. int nTCPIndex = atoi(strPort)-64320;
  170. CTCPClient::GetInstancePtr(nTCPIndex)->DisConnection();
  171. }
  172. BOOL CDevicesManager::GetOpenStatus(LPCTSTR strPort)
  173. {
  174. int nTCPIndex = atoi(strPort)-64320;
  175. return CTCPClient::GetInstancePtr(nTCPIndex)->m_SocketClient.IsOpen();
  176. //AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
  177. }
  178. BOOL CDevicesManager::GetSocketStatus(LPCTSTR strPort)
  179. {
  180. int nTCPIndex = atoi(strPort)-64320;
  181. return CTCPClient::GetInstancePtr(nTCPIndex)->m_bSocket;
  182. }
  183. void CDevicesManager::SetSocketStatus(BOOL bSocket,LPCTSTR strPort)
  184. {
  185. int nTCPIndex = atoi(strPort)-64320;
  186. CTCPClient::GetInstancePtr(nTCPIndex)->m_bSocket = bSocket;
  187. }
  188. BOOL CDevicesManager::SendNoticeToServer( int iCmd,int iOperateType,CString sUserName,CString sUID,int iVarID,LPCTSTR strPort )
  189. {
  190. int nTCPIndex = atoi(strPort)-64320;
  191. return CTCPClient::GetInstancePtr(nTCPIndex)->SendNoticeToServer( iCmd,iOperateType,sUserName,sUID,iVarID );
  192. }
  193. BOOL CDevicesManager::GetDogInfo(LPCTSTR strPort)
  194. {
  195. int nTCPIndex = atoi(strPort)-64320;
  196. return CTCPClient::GetInstancePtr(nTCPIndex)->GetDogInfo();
  197. }
  198. BOOL CDevicesManager::TestAlarm(LPCTSTR strPort)
  199. {
  200. int nTCPIndex = atoi(strPort)-64320;
  201. return CTCPClient::GetInstancePtr(nTCPIndex)->TestAlarm();
  202. }
  203. BOOL CDevicesManager::ConfigAlarm( CString sUID,int iVarID,int iStatus,int iAlarmIndex )
  204. {
  205. return CTCPClient::GetInstancePtr(0)->ConfigAlarm( sUID,iVarID,iStatus,iAlarmIndex );
  206. }
  207. void CDevicesManager::Release(LPCTSTR strPort)
  208. {
  209. int nTCPIndex = atoi(strPort)-64320;
  210. CTCPClient::GetInstancePtr(nTCPIndex)->Release();
  211. }