違章檢測模組流程

樓蘭公子發表於2020-11-03
違章檢測,包括以下功能:1)道路違章取證:違停、逆行、壓線、變道、機佔非、掉頭;2)交通資料採集:車流量、車道平均速度、車頭時距、車頭間距、車道時間佔有率、車道空間佔有率、車輛型別、排隊長度、交通狀態;3)道路事件檢測:拋灑物、行人、路障、施工、擁堵檢測。相關檢測功能都是由前端裝置實現的,SDK獲取裝置上傳事件資料有兩種方式:報警佈防方式和報警監聽方式。報警佈防方式,是指SDK主動連線裝置建立報警上傳通道,裝置發生報警之後傳送給SDK;報警監聽方式,裝置傳送報警時主動連線SDK並且上傳報警資訊,SDK在設定的埠上監聽和接收。

登入裝置之後,通過NET_DVR_STDXMLConfig(命令:GET /ISAPI/System/capabilities)獲取裝置系統總能力集,違章檢測對應能力集節點:<DeviceCap>中<aideventsupport>、<tfseventsupport>、<isvehiclestatisticssupport>。 
違章取證、交通資料採集、道路事件檢測相關引數配置,建議WEB網頁直接訪問裝置進配置介面進行設定。 
報警佈防方式獲取事件和交通資料,具體實現方法:
1) 先呼叫NET_DVR_SetDVRMessageCallBack_V31設定報警回撥函式,在SDK初始化之後即可以呼叫,多臺裝置對接時也只需要呼叫一次設定一個回撥函式,回撥函式裡面接收資料之後可以通過報警裝置資訊(pAlarmer)判斷區分裝置。
2) 每臺裝置分別登入,分別呼叫NET_DVR_SetupAlarmChan_V41進行佈防,佈防即建立裝置跟客戶端之間報警上傳的連線通道,這樣裝置發生報警之後通過該連線上傳報警資訊,SDK在報警回撥函式中接收和處理報警資訊資料即可。對接智慧交通攝像機,佈防時可以選擇佈防等級,一級佈防(byLevel為0)最大連線數為1個,二級佈防(byLevel為1)最大連線數為3個,如果已經達到上限了,再佈防就會失敗,SDK將返回28的錯誤號。
3) 程式退出前或者不需要接收報警資訊時呼叫NET_DVR_CloseAlarmChan_V30進行撤防,釋放資源,此時連線斷開,裝置將不再上傳報警資訊。 
道路違章取證的報警資訊型別為COMM_ALARM_TFS,對應報警資訊結構體:NET_DVR_TFS_ALARM;交通資料採集的報警資訊型別為COMM_ALARM_TPS_V41,對應報警資訊結構體:NET_DVR_TPS_ALARM_V41;道路事件檢測的報警資訊型別為COMM_ALARM_AID_V41,對應報警資訊結構體:NET_DVR_AID_ALARM_V41。 
#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)
int iNum = 0;
BOOL CALLBACK MSesGCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser)
{
	int i = 0;
	char filename[100];
	FILE *fSnapPic = NULL;
	FILE *fSnapPicPlate = NULL;
	//以下程式碼僅供參考,實際應用中不建議在該回撥函式中直接處理資料儲存檔案,
	//例如可以使用訊息的方式(PostMessage)在訊息響應函式裡進行處理。
	switch (lCommand)
	{
	case COMM_ALARM_TFS:
	{
		NET_DVR_TFS_ALARM struTfsAlarm = { 0 };
		memcpy(&struTfsAlarm, pAlarmInfo, sizeof(struTfsAlarm));
		printf("車牌號: %s\n", struTfsAlarm.struPlateInfo.sLicense);//車牌號
		switch (struTfsAlarm.struPlateInfo.byColor)//車牌顏色
		{
		case VCA_BLUE_PLATE:
			printf("車輛顏色: 藍色\n");
			break;
		case VCA_YELLOW_PLATE:
			printf("車輛顏色: 黃色\n");
			break;
		case VCA_WHITE_PLATE:
			printf("車輛顏色: 白色\n");
			break;
		case VCA_BLACK_PLATE:
			printf("車輛顏色: 黑色\n");
			break;
		default:
			break;
		}
		for (i = 0; i < struTfsAlarm.dwPicNum; i++)
		{
			//儲存場景圖
			if ((struTfsAlarm.struPicInfo[i].dwDataLen != 0) && (struTfsAlarm.struPicInfo[i].byType == 1) || (struTfsAlarm.struPicInfo[i].byType == 2))
			{
				sprintf(filename, "testITSpic%d_%d.jpg", iNum, i);
				fSnapPic = fopen(filename, "wb");
				fwrite(struTfsAlarm.struPicInfo[i].pBuffer, struTfsAlarm.struPicInfo[i].dwDataLen, 1, fSnapPic);
				iNum++;
				fclose(fSnapPic);
			}
			//車牌小圖片
			if ((struTfsAlarm.struPicInfo[i].dwDataLen != 0) && (struTfsAlarm.struPicInfo[i].byType == 0))
			{
				sprintf(filename, "testPicPlate%d_%d.jpg", iNum, i);
				fSnapPicPlate = fopen(filename, "wb");
				fwrite(struTfsAlarm.struPicInfo[i].pBuffer, struTfsAlarm.struPicInfo[i].dwDataLen, 1, fSnapPicPlate);
				iNum++;
				fclose(fSnapPicPlate);
			}
			//其他資訊處理......
		}
		//其他資訊處理......
		break;
	}
	case COMM_ALARM_TPS_V41:
	{
		NET_DVR_TPS_ALARM_V41 struTpsAlarm = { 0 };
		memcpy(&struTpsAlarm, pAlarmInfo, sizeof(struTpsAlarm));
		NET_DVR_TIME  struAbsTime = { 0 };
		struAbsTime.dwYear = GET_YEAR(struTpsAlarm.dwAbsTime);
		struAbsTime.dwMonth = GET_MONTH(struTpsAlarm.dwAbsTime);
		struAbsTime.dwDay = GET_DAY(struTpsAlarm.dwAbsTime);
		struAbsTime.dwHour = GET_HOUR(struTpsAlarm.dwAbsTime);
		struAbsTime.dwMinute = GET_MINUTE(struTpsAlarm.dwAbsTime);
		struAbsTime.dwSecond = GET_SECOND(struTpsAlarm.dwAbsTime);
		NET_DVR_TIME  struStartTime = { 0 };
		struStartTime.dwYear = GET_YEAR(struTpsAlarm.dwStartTime);
		struStartTime.dwMonth = GET_MONTH(struTpsAlarm.dwStartTime);
		struStartTime.dwDay = GET_DAY(struTpsAlarm.dwStartTime);
		struStartTime.dwHour = GET_HOUR(struTpsAlarm.dwStartTime);
		struStartTime.dwMinute = GET_MINUTE(struTpsAlarm.dwStartTime);
		struStartTime.dwSecond = GET_SECOND(struTpsAlarm.dwStartTime);
		NET_DVR_TIME  struStopTime = { 0 };
		struStopTime.dwYear = GET_YEAR(struTpsAlarm.dwStopTime);
		struStopTime.dwMonth = GET_MONTH(struTpsAlarm.dwStopTime);
		struStopTime.dwDay = GET_DAY(struTpsAlarm.dwStopTime);
		struStopTime.dwHour = GET_HOUR(struTpsAlarm.dwStopTime);
		struStopTime.dwMinute = GET_MINUTE(struTpsAlarm.dwStopTime);
		struStopTime.dwSecond = GET_SECOND(struTpsAlarm.dwStopTime);
		printf("Tps alarm v41 Dev ip[%s]port[%d]Chan[%d] IvmsChan[%d] Abs[%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d] LaneNum[%d]SceneId[%d] MonitoringSiteID[%s] DeviceID[%s] StartTime[%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d] StopTime[%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d]", \
			struTpsAlarm.struDevInfo.struDevIP.sIpV4, struTpsAlarm.struDevInfo.wPort, struTpsAlarm.struDevInfo.byChannel, struTpsAlarm.struDevInfo.byIvmsChannel,
			struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay, struAbsTime.dwHour, struAbsTime.dwMinute, struAbsTime.dwSecond, struTpsAlarm.struTPSInfo.dwLanNum,
			struTpsAlarm.struTPSInfo.dwSceneID, struTpsAlarm.byMonitoringSiteID, struTpsAlarm.byDeviceID,
			struStartTime.dwYear, struStartTime.dwMonth, struStartTime.dwDay, struStartTime.dwHour, struStartTime.dwMinute, struStartTime.dwSecond,
			struStopTime.dwYear, struStopTime.dwMonth, struStopTime.dwDay, struStopTime.dwHour, struStopTime.dwMinute, struStopTime.dwSecond);
		break;
	}
	case COMM_ALARM_AID_V41:
	{
		NET_DVR_AID_ALARM_V41 struAidAlarm = { 0 };
		memcpy(&struAidAlarm, pAlarmInfo, sizeof(struAidAlarm));
		NET_DVR_TIME struAbsTime = { 0 };
		struAbsTime.dwYear = GET_YEAR(struAidAlarm.dwAbsTime);
		struAbsTime.dwMonth = GET_MONTH(struAidAlarm.dwAbsTime);
		struAbsTime.dwDay = GET_DAY(struAidAlarm.dwAbsTime);
		struAbsTime.dwHour = GET_HOUR(struAidAlarm.dwAbsTime);
		struAbsTime.dwMinute = GET_MINUTE(struAidAlarm.dwAbsTime);
		struAbsTime.dwSecond = GET_SECOND(struAidAlarm.dwAbsTime);
		BYTE   tempMonitoringSiteID[MONITORSITE_ID_LEN + 1] = { 0 };
		memcpy(tempMonitoringSiteID, struAidAlarm.byMonitoringSiteID, sizeof(struAidAlarm.byMonitoringSiteID));
		printf("Aid AlarmV41 xmlLen[%d]IvmsChan[%d]ID[%d]Name[%s]Type[%d]LaneNo[%d] Abs[%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d] \
        Dev IP[%s]Port[%d]Chan[%d] Direction Start[%0.3f,%0.3f]Stop[%0.3f,%0.3f] SceneID[%d] SceneName[%s] Direction[%d] PTZ[%d,%d,%d] MonitoringSiteID[%s] DeviceID[%s] VehicleEnterState[%d] State[%d] ParkingID[%s]",
		struAidAlarm.dwXmlLen, struAidAlarm.struDevInfo.byIvmsChannel, struAidAlarm.struAIDInfo.byRuleID, struAidAlarm.struAIDInfo.byRuleName,
		struAidAlarm.struAIDInfo.dwAIDType, struAidAlarm.byLaneNo, struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay,
		struAbsTime.dwHour, struAbsTime.dwMinute, struAbsTime.dwSecond, struAidAlarm.struDevInfo.struDevIP.sIpV4,
		struAidAlarm.struDevInfo.wPort, struAidAlarm.struDevInfo.byChannel,
		struAidAlarm.struAIDInfo.struDirect.struStartPoint.fX, struAidAlarm.struAIDInfo.struDirect.struStartPoint.fY,
		struAidAlarm.struAIDInfo.struDirect.struEndPoint.fX, struAidAlarm.struAIDInfo.struDirect.struEndPoint.fY,
	    struAidAlarm.struSceneInfo.dwSceneID, struAidAlarm.struSceneInfo.bySceneName, struAidAlarm.struSceneInfo.byDirection,
	    struAidAlarm.struSceneInfo.struPtzPos.wPanPos, struAidAlarm.struSceneInfo.struPtzPos.wTiltPos, struAidAlarm.struSceneInfo.struPtzPos.wZoomPos,
		tempMonitoringSiteID, struAidAlarm.byDeviceID, struAidAlarm.struAIDInfo.byVehicleEnterState,
		struAidAlarm.struAIDInfo.byState, struAidAlarm.struAIDInfo.byParkingID);
		break;
	}
	default:
		break;
	}
	return TRUE;
}
void main()
{
	//---------------------------------------
	//初始化
	NET_DVR_Init();
	//設定連線時間與重連時間
	NET_DVR_SetConnectTime(2000, 1);
	NET_DVR_SetReconnect(10000, true);
	//---------------------------------------
	//註冊裝置
	//登入引數,包括裝置地址、登入使用者、密碼等
	LONG lUserID = -1;
	NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
	struLoginInfo.bUseAsynLogin = 0; //同步登入方式
	strcpy(struLoginInfo.sDeviceAddress, "192.0.0.64"); //裝置IP地址
	struLoginInfo.wPort = 8000; //裝置服務埠
	strcpy(struLoginInfo.sUserName, "admin"); //裝置登入使用者名稱
	strcpy(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_V31(MSesGCallback, NULL);
	//啟用佈防
	NET_DVR_SETUPALARM_PARAM struSetupParam = { 0 };
	struSetupParam.dwSize = sizeof(NET_DVR_SETUPALARM_PARAM);
	struSetupParam.byLevel = 0; //佈防優先順序:0- 一等級(高),1- 二等級(中)   
	LONG lHandle = NET_DVR_SetupAlarmChan_V41(lUserID, &struSetupParam);
	if (lHandle < 0)
	{
		printf("NET_DVR_SetupAlarmChan_V41 failed, error code: %d\n", NET_DVR_GetLastError());
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
	printf("佈防成功!\n");
	Sleep(20000); //等待接收資料
	//---------------------------------------
	//退出
	//撤銷佈防上傳通道
	if (!NET_DVR_CloseAlarmChan_V30(lHandle))
	{
		printf("NET_DVR_CloseAlarmChan_V30 failed, error code: %d\n", NET_DVR_GetLastError());
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
	//登出使用者
	NET_DVR_Logout(lUserID);
	//釋放SDK資源
	NET_DVR_Cleanup();
	return;
}

 

相關文章