SnmpProcess.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "stdafx.h"
  2. #include "SnmpProcess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. HANDLE g_SnmpRequestThread = NULL;
  7. DWORD g_dwSnmpVariantTick = 0;
  8. CRITICAL_SECTION g_csSnmpReadOneData;
  9. int SnmpCommandSend();
  10. int SnmpResponseValue();
  11. BOOL LoadSnmpDll(CString strpath)
  12. {
  13. g_hSnmpLibModule = NULL;
  14. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  15. wsprintf(strFile, "%s\\Dll\\Snmp.dll", strpath);
  16. InitializeCriticalSection( &g_csSnmpReadOneData );
  17. g_hSnmpLibModule = AfxLoadLibrary(strFile);
  18. if (NULL != g_hSnmpLibModule)
  19. {
  20. pSnmpDllGetValue = (SNMP_DLL_GetVale)::GetProcAddress(g_hSnmpLibModule, "SNMP_DLL_GetVale");
  21. pSnmpDllSetValue = (SNMP_DLL_SetValue)::GetProcAddress(g_hSnmpLibModule, "SNMP_DLL_SetValue");
  22. }
  23. return TRUE;
  24. }
  25. void UnInitSnmpDll()
  26. {
  27. DeleteCriticalSection( &g_csSnmpReadOneData );
  28. if( NULL != g_hSnmpLibModule )
  29. {
  30. AfxFreeLibrary( g_hSnmpLibModule );
  31. g_hSnmpLibModule = NULL;
  32. }
  33. }
  34. int ParseSnmpData(char *pDevUid, int nVarID, char *pMsg)
  35. {
  36. int nDeviceIndex = -1, nVarIndex = -1;
  37. BOOL bFind = FindVar(pDevUid, nVarID, nDeviceIndex, nVarIndex);
  38. if( bFind == FALSE ) return -1;
  39. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  40. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  41. EnterCriticalSection(&g_csSnmpReadOneData);
  42. pDev->m_dwOnlineTick = GetTickCount();
  43. LeaveCriticalSection(&g_csSnmpReadOneData);
  44. DWORD nValue = 0;
  45. double fCoef = pBaseVar->m_dbCoefficient;
  46. int nDataLen = pBaseVar->m_iDataLen;
  47. int nVarItemID = pBaseVar->m_nVarItemID;
  48. if( nVarItemID > 0 )//BIT位变量
  49. {
  50. int nStartBit = pBaseVar->m_nStartBit;
  51. int nEndBit = pBaseVar->m_nEndBit;
  52. int nStartReg, nEndReg;
  53. if( nEndBit - nStartBit < 7 ) // 一般告警状态会这样子定义,Snmp码暂时没有碰到,没有经过调试,待测试
  54. {
  55. if( nDataLen == 1 )
  56. {
  57. char chBuffer[8] = {0};
  58. memset(chBuffer, 0, sizeof(chBuffer));
  59. nValue = atoi(pMsg);
  60. itoa(nValue, chBuffer, 2);
  61. CString strTemp;
  62. switch( nEndBit - nStartBit )
  63. {
  64. case 0:
  65. strTemp.Format("%c", chBuffer[nStartBit]);
  66. break;
  67. case 1:
  68. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  69. break;
  70. case 2:
  71. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  72. break;
  73. case 3:
  74. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  75. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  76. break;
  77. case 4:
  78. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  79. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  80. break;
  81. case 5:
  82. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  83. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  84. break;
  85. case 6:
  86. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  87. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  88. break;
  89. }
  90. nValue = atoi(strTemp);
  91. //strTemp.Format("%d",
  92. }
  93. else if( nDataLen == 2 )
  94. {
  95. char chBuffer[16] = {0};
  96. memset(chBuffer, 0, sizeof(chBuffer));
  97. nValue = atoi(pMsg);
  98. itoa(nValue, chBuffer, 2);
  99. CString strTemp;
  100. switch( nEndBit - nStartBit )
  101. {
  102. case 0:
  103. strTemp.Format("%c", chBuffer[nStartBit]);
  104. break;
  105. case 1:
  106. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  107. break;
  108. case 2:
  109. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  110. break;
  111. case 3:
  112. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  113. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  114. break;
  115. case 4:
  116. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  117. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  118. break;
  119. case 5:
  120. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  121. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  122. break;
  123. case 6:
  124. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  125. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  126. break;
  127. }
  128. nValue = atoi(strTemp);
  129. }
  130. //itoa(ntohs(g_vtMGEEquipData[nEquipIndex].MGEStatus.Status1), buffer, 16);
  131. }
  132. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  133. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  134. {
  135. nStartReg = (int)nStartBit / 8;
  136. nEndReg = (int)(nEndBit + 1) / 8;
  137. nValue = (pMsg[nStartReg] << 4) | ( pMsg[nStartReg + 1] & 0x00FF );
  138. }
  139. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  140. {
  141. nStartReg = (int)nStartBit / 8;
  142. nEndReg = (int)(nEndBit + 1) / 8;
  143. nValue = ( ( (pMsg[nStartReg] << 4) | ( pMsg[nStartReg + 1] & 0x00FF ) ) << 8 ) |
  144. ( ( (pMsg[nStartReg + 2] << 4) | ( pMsg[nStartReg + 3] & 0x00FF ) ) & 0x0000FFFF );
  145. }
  146. EnterCriticalSection( &g_csSnmpReadOneData );
  147. pBaseVar->m_dbData = (double)(nValue * fCoef);
  148. if( pBaseVar->m_nRearm != 0 )
  149. {
  150. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  151. }
  152. LeaveCriticalSection( &g_csSnmpReadOneData );
  153. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  154. }
  155. else
  156. {
  157. // 联合类型变量,SNMP协议不做特殊处理
  158. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  159. {
  160. }
  161. else if( pBaseVar->m_nVarTypeID == TIME_TYPE_ID ) // 时间类型(例如 01:30:10.00 )
  162. {
  163. CString strTime = pMsg, strTmp;
  164. int nFindIndex = strTime.Find(":");
  165. int nHour = 0, nMinute = 0, nSecond = 0;
  166. if( nFindIndex != -1 ) // 读取小时
  167. {
  168. strTmp = strTime.Mid(0, nFindIndex);
  169. nHour = atoi(strTmp);
  170. strTime = strTime.Mid(nFindIndex + 1, strTime.GetLength() - nFindIndex);
  171. nFindIndex = strTime.Find(":");
  172. if( nFindIndex != -1 ) // 读取分钟
  173. {
  174. strTmp = strTime.Mid(0, nFindIndex);
  175. nMinute = atoi(strTmp);
  176. strTime = strTime.Mid(nFindIndex + 1, strTime.GetLength() - nFindIndex);
  177. }
  178. }
  179. nValue = nHour * 60 + nMinute;
  180. EnterCriticalSection( &g_csSnmpReadOneData );
  181. pBaseVar->m_dbData = (double)(nValue * fCoef);
  182. if( pBaseVar->m_nRearm != 0 )
  183. {
  184. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  185. }
  186. LeaveCriticalSection( &g_csSnmpReadOneData );
  187. }
  188. else if( SNMP_STRING_ID == pBaseVar->m_nVarTypeID )
  189. {
  190. CString strMsg = CString(pMsg);
  191. strMsg.MakeLower();
  192. if( 0 == strMsg.Compare("on") )
  193. {
  194. EnterCriticalSection( &g_csSnmpReadOneData );
  195. pBaseVar->m_dbData = 1;
  196. LeaveCriticalSection( &g_csSnmpReadOneData );
  197. }
  198. else if( 0 == strMsg.Compare("off") )
  199. {
  200. EnterCriticalSection( &g_csSnmpReadOneData );
  201. pBaseVar->m_dbData = 0;
  202. LeaveCriticalSection( &g_csSnmpReadOneData );
  203. }
  204. //LOG4C((LOG_NOTICE, "%s, value = %d", pBaseVar->m_strDesc, (int)pBaseVar->m_dbData));
  205. }
  206. else if( SNMP_NPM_TYPE == pBaseVar->m_nVarTypeID )
  207. {
  208. CString strTemp = CString(pMsg);
  209. //LOG4C((LOG_NOTICE, "value = %s", strTemp));
  210. strTemp = strTemp.Mid(51);
  211. strTemp.MakeLower();
  212. //LOG4C((LOG_NOTICE, "value = %s", strTemp));
  213. if( strTemp.Find("yes") != -1 )
  214. {
  215. EnterCriticalSection( &g_csSnmpReadOneData );
  216. pBaseVar->m_dbData = 1;
  217. LeaveCriticalSection( &g_csSnmpReadOneData );
  218. }
  219. else if( strTemp.Find("no") != -1 )
  220. {
  221. EnterCriticalSection( &g_csSnmpReadOneData );
  222. pBaseVar->m_dbData = 0;
  223. LeaveCriticalSection( &g_csSnmpReadOneData );
  224. }
  225. else
  226. {
  227. EnterCriticalSection( &g_csSnmpReadOneData );
  228. pBaseVar->m_dbData = atof(strTemp);
  229. LeaveCriticalSection( &g_csSnmpReadOneData );
  230. }
  231. CString strValue;
  232. strValue.Format("%.1f", pBaseVar->m_dbData);
  233. //LOG4C((LOG_NOTICE, "%s, value = %s", pBaseVar->m_strDesc, strValue));
  234. }
  235. else
  236. {
  237. EnterCriticalSection( &g_csSnmpReadOneData );
  238. pBaseVar->m_dbData = (double)(atof(pMsg) * fCoef);
  239. if( pBaseVar->m_nRearm != 0 )
  240. {
  241. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  242. }
  243. LeaveCriticalSection( &g_csSnmpReadOneData );
  244. }
  245. }
  246. EnterCriticalSection(&g_csSnmpReadOneData);
  247. pDev->m_dwOnlineTick = GetTickCount();
  248. LeaveCriticalSection(&g_csSnmpReadOneData);
  249. return 0;
  250. }