海康NVR SDK獲取所有攝像頭ip及配置

#include <iostream>
#include <thread>
#include "HCNetSDK.h"

#pragma comment(lib,"GdiPlus.lib")
#pragma comment(lib,"HCCore.lib")
#pragma comment(lib,"HCNetSDK.lib")
#pragma comment(lib,"PlayCtrl.lib")

int main(int argc, char **argv)
{
	NET_DVR_Init();
	NET_DVR_SetConnectTime(2000, 1);
	NET_DVR_SetReconnect(10000, true);

	NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
	struLoginInfo.bUseAsynLogin = 0; //同步登錄方式
	::strcpy(struLoginInfo.sDeviceAddress, "10.10.1.9"); //設備IP地址
	struLoginInfo.wPort = 8000; //設備服務端口
	::strcpy(struLoginInfo.sUserName, "admin"); //設備登錄用戶名
	::strcpy(struLoginInfo.sPassword, "123ABCabc"); //設備登錄密碼

	//設備信息, 輸出參數
	NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 };
	int userId = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
	if (userId == -1)
	{
		DWORD code = NET_DVR_GetLastError();
		std::cout << "NET_DVR_Login_V40 failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;
		NET_DVR_Logout(userId);
		//釋放SDK資源
		NET_DVR_Cleanup();
		::getchar();
		return -1;
	}
	std::cout << "login success user " << userId << std::endl;

	DWORD bytesReturned = 0;
	NET_DVR_DEVICECFG devCfg;
	devCfg.dwSize = sizeof(NET_DVR_DEVICECFG);
	bool resCode = NET_DVR_GetDVRConfig(userId, NET_DVR_GET_DEVICECFG, -1, &devCfg, sizeof(NET_DVR_DEVICECFG), &bytesReturned);
	if (!resCode)
	{
		DWORD code = NET_DVR_GetLastError();
		std::cout << "NET_DVR_GetDVRConfig failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;
		NET_DVR_Logout(userId);
		//釋放SDK資源
		NET_DVR_Cleanup();
		::getchar();
		return -1;
	}
	std::cout << "設備名: " << devCfg.sDVRName << " 序列號: " << devCfg.sSerialNumber << " 設備ID號: " << devCfg.dwDVRID << " 設備類型: " << (int)devCfg.byDVRType << std::endl;

	NET_DVR_IPPARACFG_V40 ipcfg;
	bytesReturned = 0;
	ipcfg.dwSize = sizeof(NET_DVR_IPPARACFG_V40);
	int iGroupNO = 0;
	resCode = NET_DVR_GetDVRConfig(userId, NET_DVR_GET_IPPARACFG_V40, iGroupNO, &ipcfg, sizeof(NET_DVR_IPPARACFG_V40), &bytesReturned);
	if (!resCode)
	{
		DWORD code = NET_DVR_GetLastError();
		std::cout << "NET_DVR_GetDVRConfig failed " << NET_DVR_GetErrorMsg((LONG*)(&code)) << std::endl;
		NET_DVR_Logout(userId);
		//釋放SDK資源
		NET_DVR_Cleanup();
		::getchar();
		return -1;
	}
	std::cout << "設備組 " << ipcfg.dwGroupNum << " 數字通道個數 " << ipcfg.dwDChanNum << " 起始通道 " << ipcfg.dwStartDChan << std::endl;
	for (int i = 0; i < ipcfg.dwDChanNum; i++)
	{
		switch (ipcfg.struStreamMode[i].byGetStreamType)
		{
		case 0:
			if (ipcfg.struStreamMode[i].uGetStream.struChanInfo.byEnable)
			{
				int byIPID = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPID;
				int byIPIDHigh = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPIDHigh;
				int iDevInfoIndex = byIPIDHigh * 256 + byIPID - 1 - iGroupNO * 64;
				printf("通道號%d 在線 攝像頭取流, IP: %s\n", i + 1, ipcfg.struIPDevInfo[iDevInfoIndex].struIP.sIpV4);
			}
			else {
				int byIPID = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPID;
				int byIPIDHigh = ipcfg.struStreamMode[i].uGetStream.struChanInfo.byIPIDHigh;
				int iDevInfoIndex = byIPIDHigh * 256 + byIPID - 1 - iGroupNO * 64;
				std::cout << "IP: " << ipcfg.struIPDevInfo[iDevInfoIndex].struIP.sIpV4 << " 離線" << std::endl;
			}
			break;
		case 1:
			if (ipcfg.struStreamMode[i].uGetStream.struPUStream.struStreamMediaSvrCfg.byValid)
			{
				printf("IP channel %d connected with the IP device by stream server.\n", i + 1);
				printf("IP of stream server: %s, IP of IP device: %s\n", ipcfg.struStreamMode[i].uGetStream.\
					struPUStream.struStreamMediaSvrCfg.struDevIP.sIpV4, ipcfg.struStreamMode[i].uGetStream.\
					struPUStream.struDevChanInfo.struIP.sIpV4);
			}
			break;
		}

		NET_DVR_RECORD_CHECK_COND recordChk;
		NET_DVR_STREAM_INFO sInfo;
		::memset(sInfo.byID, 0, STREAM_ID_LEN);
		sInfo.dwChannel = i + 1;
		sInfo.dwSize = sizeof(NET_DVR_STREAM_INFO);
		NET_DVR_TIME_EX startT, endT;
		startT.wYear = 2020;
		startT.byMonth = 7;
		startT.byDay = 1;
		startT.byHour = 0;
		startT.byMinute = 0;
		startT.bySecond = 0;
		endT.wYear = 2020;
		endT.byMonth = 7;
		endT.byDay = 1;
		endT.byHour = 23;
		endT.byMinute = 59;
		endT.bySecond = 59;
		recordChk.dwSize = sizeof(NET_DVR_RECORD_CHECK_COND);
		recordChk.struStreamInfo = sInfo;
		recordChk.byCheckType = 0;
		recordChk.struBeginTime = startT;
		recordChk.struEndTime = endT;
		LONG recordHandle = NET_DVR_StartRemoteConfig(userId, NET_DVR_RECORD_CHECK, &recordChk, sizeof(NET_DVR_RECORD_CHECK_COND), [](DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData) {
			switch (dwType)
			{
			case NET_SDK_CALLBACK_TYPE_STATUS:
				std::cout << "status len " << dwBufLen << std::endl;
				break;
			case NET_SDK_CALLBACK_TYPE_PROGRESS:
				std::cout << "progress len " << dwBufLen << std::endl;
				break;
			case NET_SDK_CALLBACK_TYPE_DATA:
				std::cout << "data len " << dwBufLen << std::endl;
				break;
			default:
				break;
			}
		}, nullptr);

		NET_DVR_RECORD_SEGMENT_RET recordData;
		recordData.dwSize = sizeof(NET_DVR_RECORD_SEGMENT_RET);
		LONG code = 1000;
		while (code == 1000)
		{
			code = NET_DVR_GetNextRemoteConfig(recordHandle, &recordData, sizeof(NET_DVR_RECORD_SEGMENT_RET));
			while (code == 1001)
			{
				code = NET_DVR_GetNextRemoteConfig(recordHandle, &recordData, sizeof(NET_DVR_RECORD_SEGMENT_RET));
				std::this_thread::sleep_for(std::chrono::milliseconds(500));
			}
			std::cout << "錄像大小 " << recordData.dwRecordTotalSize << std::endl;
			recordData.dwRecordTotalSize = 0;
		}
		std::cout << "用戶名 " << ipcfg.struIPDevInfo[i].sUserName << " 密碼 " << ipcfg.struIPDevInfo[i].sPassword << " 設備ID " << (int)ipcfg.struIPDevInfo[i].szDeviceID << " ip地址 " << ipcfg.struIPDevInfo[i].struIP.sIpV4 << " 端口 " << ipcfg.struIPDevInfo[i].wDVRPort << " 設備類型: " << (int)ipcfg.struIPDevInfo[i].byCameraType << std::endl << std::endl;
	}

	::getchar();
	NET_DVR_Logout(userId);
	//釋放SDK資源
	NET_DVR_Cleanup();

	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章