The access control events include device events, alarm input events, door events, card reader events, card swiping events, and so on. You can configure the linkage types (i.e., event linkage, card linkage, MAC linkage, and person linkage) and linkage actions (e.g., recording, alarm output, buzzing, capture, etc.) of event card linkage to execute the linked actions when the corresponding events occurred (e.g., door open or closed, card swiped, etc.). And then you can receive the event information from event sources in arming or listening mode.
Make sure you have called NET_DVR_Init to initialize the development environment.
Make sure you have called NET_DVR_Login_V40 to log in to device.
#include <stdio.h> #include <iostream> #include "Windows.h" #include "HCNetSDK.h" using namespace std; BOOL CALLBACK MSesGCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser) { //As the operations with long time comsumption are not allowed in the callback function, //do not call the API of HCNetSDK.DLL in the callback function. //The following code is for reference only, actually, processing data in the callback function is not suggested. //for example, process in the message response function as PostMessage switch (lCommand) { case COMM_ALARM_ACS://Alarm information of access controller { NET_DVR_ACS_ALARM_INFO struAcsAlarmInfo = {0}; memcpy(&struAcsAlarmInfo, pAlarmInfo, sizeof(struAcsAlarmInfo)); char szTime[50] = {0};//Alarm time sprintf(szTime, "%4d-%2d-%2d %2d:%2d:%2d", struAcsAlarmInfo.struTime.dwYear, struAcsAlarmInfo.struTime.dwMonth, struAcsAlarmInfo.struTime.dwDay, struAcsAlarmInfo.struTime.dwHour, struAcsAlarmInfo.struTime.dwMinute, struAcsAlarmInfo.struTime.dwSecond); char szCardNo[50] = {0};//Card No. sprintf(szCardNo, "CardNo:%s", (char *)struAcsAlarmInfo.struAcsEventInfo.byCardNo); BYTE byCardType = struAcsAlarmInfo.struAcsEventInfo.byCardType;//Card type DWORD dwCardReaderNo = struAcsAlarmInfo.struAcsEventInfo.dwCardReaderNo;//Card reader No. DWORD dwDoorNo = struAcsAlarmInfo.struAcsEventInfo.dwDoorNo;//Door No. if (struAcsAlarmInfo.dwPicDataLen > 0 && struAcsAlarmInfo.pPicData != NULL) { char filename[128]; FILE *fSnapPic=NULL; SYSTEMTIME t; GetLocalTime(&t); char chTime[128]; sprintf(filename,"%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d%3.3d",t.wYear,t.wMonth,t.wDay, t.wHour,t.wMinute,t.wSecond,t.wMilliseconds); //Save picture fSnapPic=fopen(filename,"wb"); fwrite(struAcsAlarmInfo.pPicData,struAcsAlarmInfo.dwPicDataLen,1,fSnapPic); fclose(fSnapPic); } //Handle other information in the alarm structure as desired... break; } default: break; } return true; } void main() { //--------------------------------------- //Initialize NET_DVR_Init(); //Set connection timeout and reconnection function NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); //--------------------------------------- //Log in to device LONG lUserID; //Login parameters, including device IP address, user name, password, and so on NET_DVR_USER_LOGIN_INFO struLoginInfo = {0}; struLoginInfo.bUseAsynLogin = 0; //Synchronous login mode strcpy(struLoginInfo.sDeviceAddress, "192.168.1.64"); //Device IP address struLoginInfo.wPort = 8000; //Device service port number strcpy(struLoginInfo.sUserName, "admin"); //User name strcpy(struLoginInfo.sPassword, "abcd1234"); //Password //Device information, output parameter 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; } //Set alarm callback function for capture linkage NET_DVR_SetDVRMessageCallBack_V31(MSesGCallback, NULL); //Set up channel for uploading alarm information NET_DVR_SETUPALARM_PARAM struSetupParam={0}; struSetupParam.dwSize=sizeof(NET_DVR_SETUPALARM_PARAM); LONG lHandle = NET_DVR_SetupAlarmChan_V41(lUserID,&struSetupParam); if (lHandle < 0) { printf("NET_DVR_SetupAlarmChan_V41 error: %d\n", NET_DVR_GetLastError()); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return; } //--------------------------------------- //Configure capture parameters NET_DVR_SNAPCFG struSnapCfg = {0}; struSnapCfg.dwSize = sizeof(NET_DVR_SNAPCFG); struSnapCfg.bySnapTimes = 2;//Capture times: 0-Not capture, non-0-Continuous capture, up to 5 times are allowed. struSnapCfg.wIntervalTime[0] = 1000;//Time interval of continuous capture, unit: ms, value range: [67,60000] struSnapCfg.struJpegPara.wPicSize = 5;//Picture resolution: 5-1280*720 if (!NET_DVR_ContinuousShoot(lUserID, &struSnapCfg)) { printf("NET_DVR_ContinuousShoot error: %d\n", NET_DVR_GetLastError()); return; } //--------------------------------------- //Set event and card No. linkage parameters NET_DVR_EVENT_CARD_LINKAGE_COND struEventCardLinkageCond = {0}; struEventCardLinkageCond.dwSize = sizeof(NET_DVR_EVENT_CARD_LINKAGE_COND); struEventCardLinkageCond.dwEventID = 1;//Event ID, starts from 1, increase when setting different event/card No. linkages NET_DVR_EVENT_CARD_LINKAGE_CFG_V50 struEventCardLinkageCfgV50 = {0}; struEventCardLinkageCfgV50.dwSize = sizeof(NET_DVR_EVENT_CARD_LINKAGE_CFG_V50); struEventCardLinkageCfgV50.byProMode = 0;//Linked event source: 0-event, 1-card No. struEventCardLinkageCfgV50.byCapturePic = 1;//Enable capture linkage?: 0-No, 1-Yes //Event source ID, 0xffffffff-all, other values: invalid, when the major type is device event; //door No., when the major type is door event; card reader ID, when the major type is card reader event; //zone alarm input ID or event alarm input ID, when the major type is alarm input event. struEventCardLinkageCfgV50.dwEventSourceID = 0xffffffff; //Event major type: 0-device event, 1-alarm input event, 2-door event, 3-card reader event struEventCardLinkageCfgV50.uLinkageInfo.struEventLinkage.wMainEventType = 2; //Event minor type: 10-open door by magnetic switch, here takes capturing triggered by door open as an example. struEventCardLinkageCfgV50.uLinkageInfo.struEventLinkage.wSubEventType = 10; DWORD dwStatus = 0; if (!NET_DVR_SetDeviceConfig(lUserID,NET_DVR_SET_EVENT_CARD_LINKAGE_CFG_V50, 1,&struEventCardLinkageCond,sizeof(struEventCardLinkageCond), &dwStatus,&struEventCardLinkageCfgV50,sizeof(struEventCardLinkageCfgV50))) { printf("NET_DVR_SET_EVENT_CARD_LINKAGE_CFG_V50, error: %d\n", NET_DVR_GetLastError()); return; } //--------------------------------------- //Set access controller parameters NET_DVR_ACS_CFG struAcsCfg = {0}; struAcsCfg.dwSize = sizeof(NET_DVR_ACS_CFG); struAcsCfg.byUploadCapPic = 1;//Upload picture or not when capture is triggered: 0-No, 1-Yes BOOL bRet = NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_ACS_CFG, 0, \ &struAcsCfg, sizeof(struAcsCfg)); if (!bRet) { printf("NET_DVR_SET_ACS_CFG, error:%d.\n", NET_DVR_GetLastError()); return; } //--------------------------------------- //Wait for 30s for receiving captured picture uploaded by device Sleep(30000); //Close alarm uploading channel 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; } //Log out NET_DVR_Logout(lUserID); //Release SDK resource NET_DVR_Cleanup(); return; }
Call NET_DVR_Logout and NET_DVR_Cleanup to log out and release the resource.