DevicesManager.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. m_nSlaveVersion = 0;
  19. m_bIsBlanceSmall = true;
  20. m_sBanceContent = "";
  21. }
  22. CDevicesManager::~CDevicesManager()
  23. {
  24. ClearUp();
  25. }
  26. void CDevicesManager::Load(char *pStrDirectroy)
  27. {
  28. ClearUp();
  29. CFile file;
  30. CString fileName = CString(pStrDirectroy) + "\\" + _PROJECTDIR;
  31. fileName = fileName + DEVICE_FILE;
  32. if (file.Open(fileName, CFile::modeRead))
  33. {
  34. CArchive ar(&file,CArchive::load);
  35. m_channels.Serialize(ar);
  36. ar.Close();
  37. file.Close();
  38. }
  39. }
  40. void CDevicesManager::Store(char *pStrDirectroy)
  41. {
  42. CFile file;
  43. CFileException e;
  44. CString fileName = _PROJECTDIR;
  45. fileName = fileName + DEVICE_FILE;
  46. fileName = CString(pStrDirectroy) + "\\" + fileName;
  47. DeleteFile( fileName );
  48. if(file.Open(fileName, CFile::modeWrite|CFile::modeCreate, &e))
  49. {
  50. CArchive ar(&file,CArchive::store);
  51. m_channels.Serialize(ar);
  52. ar.Close();
  53. file.Close();
  54. }
  55. else
  56. {
  57. #ifdef _DEBUG
  58. afxDump << "File could not be opened " << e.m_cause << "\n";
  59. #endif
  60. }
  61. }
  62. void CDevicesManager::FreshData(void)
  63. {
  64. int nSize = m_channels.GetSize();
  65. for(int i = 0; i < nSize; i++)
  66. {
  67. CChannel* pChannel = m_channels[i];
  68. pChannel->FreshData();
  69. }
  70. }
  71. void CDevicesManager::ClearUp()
  72. {
  73. int nSize = m_channels.GetSize();
  74. for (int i = 0; i < m_channels.GetSize(); i++)
  75. {
  76. //int n = m_channels[i]->m_Devices.GetSize();
  77. //for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  78. //{
  79. // delete m_channels[i]->m_Devices[j];
  80. //}
  81. MTVERIFY(delete m_channels[i]);
  82. }
  83. m_channels.RemoveAll();
  84. }
  85. int CDevicesManager::FindCommPort(CString strName)
  86. {
  87. int nRet = -1;
  88. for( int i = 0; i < m_channels.GetSize(); i++ )
  89. {
  90. if( m_channels[i]->m_strName == strName ) // 端口
  91. {
  92. nRet = i;
  93. break;
  94. }
  95. }
  96. return nRet;
  97. }
  98. BOOL CDevicesManager::FindCommPort(CString strName, int nAddr, int &nChannlIndex, int &nDeviceIndex)
  99. {
  100. BOOL bResult = FALSE;
  101. bool bBreak = false;
  102. for( int i = 0; i < m_channels.GetSize(); i++ )
  103. {
  104. for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  105. {
  106. if( m_channels[i]->m_Devices[j]->m_Address == nAddr && // 地址
  107. m_channels[i]->m_strName == strName) // 端口
  108. {
  109. bResult = TRUE;
  110. nChannlIndex = i;
  111. nDeviceIndex = j;
  112. bBreak = true;
  113. break;
  114. }
  115. }
  116. if( bBreak )
  117. break;
  118. }
  119. return bResult;
  120. }
  121. BOOL CDevicesManager::FindDev(CString strName, int &nChannlIndex, int &nDeviceIndex)
  122. {
  123. BOOL bResult = FALSE;
  124. bool bBreak = 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( m_channels[i]->m_Devices[j]->m_strName == strName )
  130. {
  131. bResult = TRUE;
  132. nChannlIndex = i;
  133. nDeviceIndex = j;
  134. bBreak = true;
  135. break;
  136. }
  137. }
  138. if( bBreak )
  139. break;
  140. }
  141. return bResult;
  142. }
  143. BOOL CDevicesManager::FindDev(CString strIP, int m_nPort, int &nChannlIndex, int &nDeviceIndex)
  144. {
  145. BOOL bResult = FALSE;
  146. bool bBreak = false;
  147. for( int i = 0; i < m_channels.GetSize(); i++ )
  148. {
  149. for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  150. {
  151. if( ((CNetwork*)m_channels[i])->m_Devices[j]->m_strDevIP == strIP &&
  152. ((CNetwork*)m_channels[i])->m_Devices[j]->m_nNetPort == m_nPort )
  153. {
  154. bResult = TRUE;
  155. nChannlIndex = i;
  156. nDeviceIndex = j;
  157. bBreak = true;
  158. break;
  159. }
  160. }
  161. if( bBreak )
  162. break;
  163. }
  164. return bResult;
  165. }
  166. bool CDevicesManager::IsExistDev( CString sDevName )//判断设备是否存在
  167. {
  168. if( sDevName=="" )
  169. return FALSE;
  170. bool bRet = false;
  171. for( int i = 0; i < m_channels.GetSize(); i++ )
  172. {
  173. for( int j = 0; j < m_channels[i]->m_Devices.GetSize(); j++ )
  174. {
  175. if( m_channels[i]->m_Devices[j]->m_strName == sDevName )
  176. {
  177. return true;
  178. }
  179. }
  180. }
  181. return bRet;
  182. }
  183. int CDevicesManager::GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
  184. {
  185. int iResult = iNormalState;
  186. if ( (iUpperLimit > 0 || iLowerLimit > 0 ) && iUpperLimit > iLowerLimit )
  187. {
  188. //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
  189. if( iParaValue > iUpperLimit )
  190. {
  191. iResult = UPPER_LIMIT_ID; // 上限告警
  192. }
  193. if( iParaValue < iLowerLimit )
  194. {
  195. iResult = LOWER_LIMIT_ID; // 下限告警
  196. }
  197. }
  198. else if( (int)iParaValue != iNormalState )
  199. {
  200. iResult = (int)iParaValue;
  201. }
  202. return iResult;
  203. }
  204. void CDevicesManager::SetDBType(char *pDBType)
  205. {
  206. m_strDBType = CString(pDBType);
  207. CDBInterface::GetInstancePtr()->SetDBType( pDBType );
  208. }
  209. INT CDevicesManager::GetDevInfo(void)
  210. {
  211. CDBInterface::GetInstancePtr()->GetDevInfo( );
  212. return 0;
  213. }
  214. BOOL CDevicesManager::Connection(LPCTSTR strAddr, LPCTSTR strPort)
  215. {
  216. int nTCPIndex = atoi(strPort)-64320;
  217. return CTCPClient::GetInstancePtr(nTCPIndex)->Connection(strAddr, strPort);
  218. }
  219. void CDevicesManager::DisConnection( LPCTSTR strPort )
  220. {
  221. int nTCPIndex = atoi(strPort)-64320;
  222. CTCPClient::GetInstancePtr(nTCPIndex)->DisConnection();
  223. }
  224. BOOL CDevicesManager::GetOpenStatus(LPCTSTR strPort)
  225. {
  226. int nTCPIndex = atoi(strPort)-64320;
  227. return CTCPClient::GetInstancePtr(nTCPIndex)->m_SocketClient.IsOpen();
  228. //AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
  229. }
  230. BOOL CDevicesManager::GetSocketStatus(LPCTSTR strPort)
  231. {
  232. int nTCPIndex = atoi(strPort)-64320;
  233. return CTCPClient::GetInstancePtr(nTCPIndex)->m_bSocket;
  234. }
  235. void CDevicesManager::SetSocketStatus(BOOL bSocket,LPCTSTR strPort)
  236. {
  237. int nTCPIndex = atoi(strPort)-64320;
  238. CTCPClient::GetInstancePtr(nTCPIndex)->m_bSocket = bSocket;
  239. }
  240. BOOL CDevicesManager::SendNoticeToServer( int iCmd,int iOperateType,CString sUserName,CString sUID,int iVarID,LPCTSTR strPort )
  241. {
  242. int nTCPIndex = atoi(strPort)-64320;
  243. return CTCPClient::GetInstancePtr(nTCPIndex)->SendNoticeToServer( iCmd,iOperateType,sUserName,sUID,iVarID );
  244. }
  245. BOOL CDevicesManager::GetServerVer(LPCTSTR strPort)
  246. {
  247. int nTCPIndex = atoi(strPort)-64320;
  248. return CTCPClient::GetInstancePtr(nTCPIndex)->GetServerVer();
  249. }
  250. BOOL CDevicesManager::TestAlarm(LPCTSTR strPort)
  251. {
  252. int nTCPIndex = atoi(strPort)-64320;
  253. return CTCPClient::GetInstancePtr(nTCPIndex)->TestAlarm();
  254. }
  255. BOOL CDevicesManager::ConfigAlarm( CString sUID,int iVarID,int iStatus,int iAlarmIndex )
  256. {
  257. return CTCPClient::GetInstancePtr(0)->ConfigAlarm( sUID,iVarID,iStatus,iAlarmIndex );
  258. }
  259. void CDevicesManager::Release(LPCTSTR strPort)
  260. {
  261. CDBInterface::GetInstancePtr()->Release();//####
  262. int nTCPIndex = atoi(strPort)-64320;
  263. CTCPClient::GetInstancePtr(nTCPIndex)->Release();
  264. }