Alarm.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "Alarm.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include "Windows.h"
  5. #include "HCNetSDK.h"
  6. using namespace std;
  7. //时间解析宏定义
  8. #define GET_YEAR(_time_) (((_time_)>>26) + 2000)
  9. #define GET_MONTH(_time_) (((_time_)>>22) & 15)
  10. #define GET_DAY(_time_) (((_time_)>>17) & 31)
  11. #define GET_HOUR(_time_) (((_time_)>>12) & 31)
  12. #define GET_MINUTE(_time_) (((_time_)>>6) & 63)
  13. #define GET_SECOND(_time_) (((_time_)>>0) & 63)
  14. void CALLBACK cbMessageCallback(LONG lCommand, NET_DVR_ALARMER* pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser)
  15. {
  16. switch (lCommand)
  17. {
  18. case COMM_ALARM_RULE: //行为分析报警信息
  19. {
  20. NET_VCA_RULE_ALARM struVcaAlarm = { 0 };
  21. memcpy(&struVcaAlarm, pAlarmInfo, sizeof(NET_VCA_RULE_ALARM));
  22. NET_DVR_TIME struAbsTime = { 0 };
  23. struAbsTime.dwYear = GET_YEAR(struVcaAlarm.dwAbsTime);
  24. struAbsTime.dwMonth = GET_MONTH(struVcaAlarm.dwAbsTime);
  25. struAbsTime.dwDay = GET_DAY(struVcaAlarm.dwAbsTime);
  26. struAbsTime.dwHour = GET_HOUR(struVcaAlarm.dwAbsTime);
  27. struAbsTime.dwMinute = GET_MINUTE(struVcaAlarm.dwAbsTime);
  28. struAbsTime.dwSecond = GET_SECOND(struVcaAlarm.dwAbsTime);
  29. //保存抓拍场景图片
  30. if (struVcaAlarm.dwPicDataLen > 0 && struVcaAlarm.pImage != NULL)
  31. {
  32. char cFilename[256] = { 0 };
  33. HANDLE hFile;
  34. DWORD dwReturn;
  35. char chTime[128];
  36. sprintf_s(chTime, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d", struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay, struAbsTime.dwHour, struAbsTime.dwMinute, struAbsTime.dwSecond);
  37. sprintf_s(cFilename, "VcaAlarmPic[%s][%s].jpg", struVcaAlarm.struDevInfo.struDevIP.sIpV4, chTime);
  38. hFile = CreateFile((LPCWSTR)cFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  39. if (hFile == INVALID_HANDLE_VALUE)
  40. {
  41. break;
  42. }
  43. WriteFile(hFile, struVcaAlarm.pImage, struVcaAlarm.dwPicDataLen, &dwReturn, NULL);
  44. CloseHandle(hFile);
  45. hFile = INVALID_HANDLE_VALUE;
  46. }
  47. WORD wEventType = struVcaAlarm.struRuleInfo.wEventTypeEx;
  48. printf("\n\n");
  49. printf("行为分析报警[0x%x]: Abs[%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d] Dev[ip:%s,port:%d,ivmsChan:%d] Smart[%d] EventTypeEx[%d]\n", \
  50. lCommand, struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay, struAbsTime.dwHour, struAbsTime.dwMinute, \
  51. struAbsTime.dwSecond, struVcaAlarm.struDevInfo.struDevIP.sIpV4, struVcaAlarm.struDevInfo.wPort, \
  52. struVcaAlarm.struDevInfo.byIvmsChannel, struVcaAlarm.bySmart, wEventType);
  53. NET_VCA_TARGET_INFO tmpTargetInfo;
  54. memcpy(&tmpTargetInfo, &struVcaAlarm.struTargetInfo, sizeof(NET_VCA_TARGET_INFO));
  55. printf("目标信息:ID[%d]RECT[%f][%f][%f][%f]\n",
  56. tmpTargetInfo.dwID, tmpTargetInfo.struRect.fX, tmpTargetInfo.struRect.fY,
  57. tmpTargetInfo.struRect.fWidth, tmpTargetInfo.struRect.fHeight);
  58. switch (wEventType)
  59. {
  60. case ENUM_VCA_EVENT_INTRUSION: //区域入侵报警
  61. {
  62. printf("区域入侵报警: wDuration[%d], Sensitivity[%d]\n", \
  63. struVcaAlarm.struRuleInfo.uEventParam.struIntrusion.wDuration, \
  64. struVcaAlarm.struRuleInfo.uEventParam.struIntrusion.bySensitivity);
  65. printf("规则区域: \n");
  66. NET_VCA_POLYGON tempPolygon;
  67. memcpy(&tempPolygon, &struVcaAlarm.struRuleInfo.uEventParam.struIntrusion.struRegion, sizeof(NET_VCA_POLYGON));
  68. for (int i = 0; i < (int)tempPolygon.dwPointNum; i++)
  69. {
  70. printf("[%f, %f] ", tempPolygon.struPos[i].fX, tempPolygon.struPos[i].fY);
  71. }
  72. break;
  73. }
  74. default:
  75. {
  76. printf("其他报警,报警信息类型: 0x%x\n", lCommand);
  77. break;
  78. }
  79. }
  80. break;
  81. }
  82. default:
  83. {
  84. printf("其他报警,报警信息类型: 0x%x\n", lCommand);
  85. break;
  86. }
  87. }
  88. return;
  89. }
  90. void Alarm::TestMeasureTemp() {
  91. //---------------------------------------
  92. // 初始化
  93. NET_DVR_Init();
  94. //设置连接时间与重连时间
  95. NET_DVR_SetConnectTime(2000, 1);
  96. NET_DVR_SetReconnect(10000, true);
  97. //---------------------------------------
  98. // 注册设备
  99. LONG lUserID;
  100. //登录参数,包括设备地址、登录用户、密码等
  101. NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
  102. struLoginInfo.bUseAsynLogin = 0; //同步登录方式
  103. strcpy_s(struLoginInfo.sDeviceAddress, "192.168.81.1"); //设备IP地址
  104. struLoginInfo.wPort = 8000; //设备服务端口
  105. strcpy_s(struLoginInfo.sUserName, "admin"); //设备登录用户名
  106. strcpy_s(struLoginInfo.sPassword, "abcd1234"); //设备登录密码
  107. //设备信息, 输出参数
  108. NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 };
  109. lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
  110. if (lUserID < 0)
  111. {
  112. printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
  113. NET_DVR_Cleanup();
  114. return;
  115. }
  116. //设置报警回调函数
  117. /*注:多台设备对接时也只需要调用一次设置一个回调函数,不支持不同设备的事件在不同的回调函数里面返回*/
  118. NET_DVR_SetDVRMessageCallBack_V50(0, cbMessageCallback, NULL);
  119. //启用布防
  120. LONG lHandle;
  121. NET_DVR_SETUPALARM_PARAM struAlarmParam = { 0 };
  122. struAlarmParam.dwSize = sizeof(struAlarmParam);
  123. //其他报警布防参数不需要设置,不支持
  124. lHandle = NET_DVR_SetupAlarmChan_V41(lUserID, &struAlarmParam);
  125. if (lHandle < 0)
  126. {
  127. printf("NET_DVR_SetupAlarmChan_V41 error, %d\n", NET_DVR_GetLastError());
  128. NET_DVR_Logout(lUserID);
  129. NET_DVR_Cleanup();
  130. return;
  131. }
  132. //事件信息在回调函数里面获取
  133. //控制台输入q退出程序,否则一直运行
  134. char c = 0;
  135. while ('q' != c)
  136. {
  137. printf("input 'q' to quit\n");
  138. printf("input: ");
  139. scanf_s("%c", &c);
  140. }
  141. //撤销布防上传通道
  142. if (!NET_DVR_CloseAlarmChan_V30(lHandle))
  143. {
  144. printf("NET_DVR_CloseAlarmChan_V30 error, %d\n", NET_DVR_GetLastError());
  145. NET_DVR_Logout(lUserID);
  146. NET_DVR_Cleanup();
  147. return;
  148. }
  149. //注销用户
  150. NET_DVR_Logout(lUserID);
  151. //释放SDK资源
  152. NET_DVR_Cleanup();
  153. return;
  154. }