SOMS/test/HikTest/Alarm.cpp
2024-07-15 10:31:26 +08:00

183 lines
6.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Alarm.h"
#include <stdio.h>
#include <iostream>
#include "Windows.h"
#include "HCNetSDK.h"
using namespace std;
//时间解析宏定义
#define GET_YEAR(_time_) (((_time_)>>26) + 2000)
#define GET_MONTH(_time_) (((_time_)>>22) & 15)
#define GET_DAY(_time_) (((_time_)>>17) & 31)
#define GET_HOUR(_time_) (((_time_)>>12) & 31)
#define GET_MINUTE(_time_) (((_time_)>>6) & 63)
#define GET_SECOND(_time_) (((_time_)>>0) & 63)
void CALLBACK cbMessageCallback(LONG lCommand, NET_DVR_ALARMER* pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser)
{
switch (lCommand)
{
case COMM_ALARM_RULE: //行为分析报警信息
{
NET_VCA_RULE_ALARM struVcaAlarm = { 0 };
memcpy(&struVcaAlarm, pAlarmInfo, sizeof(NET_VCA_RULE_ALARM));
NET_DVR_TIME struAbsTime = { 0 };
struAbsTime.dwYear = GET_YEAR(struVcaAlarm.dwAbsTime);
struAbsTime.dwMonth = GET_MONTH(struVcaAlarm.dwAbsTime);
struAbsTime.dwDay = GET_DAY(struVcaAlarm.dwAbsTime);
struAbsTime.dwHour = GET_HOUR(struVcaAlarm.dwAbsTime);
struAbsTime.dwMinute = GET_MINUTE(struVcaAlarm.dwAbsTime);
struAbsTime.dwSecond = GET_SECOND(struVcaAlarm.dwAbsTime);
//保存抓拍场景图片
if (struVcaAlarm.dwPicDataLen > 0 && struVcaAlarm.pImage != NULL)
{
char cFilename[256] = { 0 };
HANDLE hFile;
DWORD dwReturn;
char chTime[128];
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);
sprintf_s(cFilename, "VcaAlarmPic[%s][%s].jpg", struVcaAlarm.struDevInfo.struDevIP.sIpV4, chTime);
hFile = CreateFile((LPCWSTR)cFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
break;
}
WriteFile(hFile, struVcaAlarm.pImage, struVcaAlarm.dwPicDataLen, &dwReturn, NULL);
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
WORD wEventType = struVcaAlarm.struRuleInfo.wEventTypeEx;
printf("\n\n");
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", \
lCommand, struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay, struAbsTime.dwHour, struAbsTime.dwMinute, \
struAbsTime.dwSecond, struVcaAlarm.struDevInfo.struDevIP.sIpV4, struVcaAlarm.struDevInfo.wPort, \
struVcaAlarm.struDevInfo.byIvmsChannel, struVcaAlarm.bySmart, wEventType);
NET_VCA_TARGET_INFO tmpTargetInfo;
memcpy(&tmpTargetInfo, &struVcaAlarm.struTargetInfo, sizeof(NET_VCA_TARGET_INFO));
printf("目标信息:ID[%d]RECT[%f][%f][%f][%f]\n",
tmpTargetInfo.dwID, tmpTargetInfo.struRect.fX, tmpTargetInfo.struRect.fY,
tmpTargetInfo.struRect.fWidth, tmpTargetInfo.struRect.fHeight);
switch (wEventType)
{
case ENUM_VCA_EVENT_INTRUSION: //区域入侵报警
{
printf("区域入侵报警: wDuration[%d], Sensitivity[%d]\n", \
struVcaAlarm.struRuleInfo.uEventParam.struIntrusion.wDuration, \
struVcaAlarm.struRuleInfo.uEventParam.struIntrusion.bySensitivity);
printf("规则区域: \n");
NET_VCA_POLYGON tempPolygon;
memcpy(&tempPolygon, &struVcaAlarm.struRuleInfo.uEventParam.struIntrusion.struRegion, sizeof(NET_VCA_POLYGON));
for (int i = 0; i < (int)tempPolygon.dwPointNum; i++)
{
printf("[%f, %f] ", tempPolygon.struPos[i].fX, tempPolygon.struPos[i].fY);
}
break;
}
default:
{
printf("其他报警,报警信息类型: 0x%x\n", lCommand);
break;
}
}
break;
}
default:
{
printf("其他报警,报警信息类型: 0x%x\n", lCommand);
break;
}
}
return;
}
void Alarm::TestMeasureTemp() {
//---------------------------------------
// 初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
//---------------------------------------
// 注册设备
LONG lUserID;
//登录参数,包括设备地址、登录用户、密码等
NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
struLoginInfo.bUseAsynLogin = 0; //同步登录方式
strcpy_s(struLoginInfo.sDeviceAddress, "192.168.81.1"); //设备IP地址
struLoginInfo.wPort = 8000; //设备服务端口
strcpy_s(struLoginInfo.sUserName, "admin"); //设备登录用户名
strcpy_s(struLoginInfo.sPassword, "abcd1234"); //设备登录密码
//设备信息, 输出参数
NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 };
lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
if (lUserID < 0)
{
printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
//设置报警回调函数
/*注:多台设备对接时也只需要调用一次设置一个回调函数,不支持不同设备的事件在不同的回调函数里面返回*/
NET_DVR_SetDVRMessageCallBack_V50(0, cbMessageCallback, NULL);
//启用布防
LONG lHandle;
NET_DVR_SETUPALARM_PARAM struAlarmParam = { 0 };
struAlarmParam.dwSize = sizeof(struAlarmParam);
//其他报警布防参数不需要设置,不支持
lHandle = NET_DVR_SetupAlarmChan_V41(lUserID, &struAlarmParam);
if (lHandle < 0)
{
printf("NET_DVR_SetupAlarmChan_V41 error, %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
//事件信息在回调函数里面获取
//控制台输入q退出程序否则一直运行
char c = 0;
while ('q' != c)
{
printf("input 'q' to quit\n");
printf("input: ");
scanf_s("%c", &c);
}
//撤销布防上传通道
if (!NET_DVR_CloseAlarmChan_V30(lHandle))
{
printf("NET_DVR_CloseAlarmChan_V30 error, %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
//注销用户
NET_DVR_Logout(lUserID);
//释放SDK资源
NET_DVR_Cleanup();
return;
}