// playback-dos.cpp : 定义控制台应用程序的入口点。
//

#include <stdio.h>
#include "stdafx.h"
#include "dhnetsdk.h"
#pragma comment(lib, "dhnetsdk.lib")

void __stdcall OnDisConnect(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser)
{
	int		nStatus;

	if (NULL == pchDVRIP)
		return;

	printf("device disconnect, IP: %s\n",pchDVRIP);
}

void __stdcall DownLoadPosCallBackFunc(LLONG lPlayHandle, DWORD dwTotalSize, DWORD dwDownLoadSize, LDWORD dwUser)
{
	if (dwDownLoadSize == -1)
	{
		printf("PlayBackPosCallBack: 100%\n");
	}
	else if (dwTotalSize* 100 / dwTotalSize == 50 || dwTotalSize * 100 / dwTotalSize == 100)
	{
		printf("PlayBackPosCallBack: %d%%\n", dwDownLoadSize*100/dwTotalSize);
	}
}

int __stdcall DataCallBackFunc(LLONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser)
{
	char szFileName[32] = {0};
	sprintf(szFileName, "%d.txt", lRealHandle);
	FILE *file =fopen(szFileName, "a+b");
	if (file)
	{
		fwrite(pBuffer, 1, dwBufSize, file);
		fclose(file);
	}

	//	printf("PlayBack: data++++, len=%d+++++++++++++++\n", dwBufSize);

	return 1;
}

void Clean(LLONG lPlayBackHandle, LLONG lLoginHandle)
{
	printf("please put any key to exit?");
	getchar();
	getchar();
	if (lPlayBackHandle != 0)
	{
		CLIENT_StopPlayBack(lPlayBackHandle);
		lPlayBackHandle = 0;
	}
	CLIENT_Logout(lLoginHandle);
	CLIENT_Cleanup();
}

int _tmain(int argc, _TCHAR* argv[])
{
	// sdk init
	CLIENT_Init(OnDisConnect, 0);


	// log in device
	int nError = 0;
	NET_DEVICEINFO stDevInfo = {0};

	int nChannel = 0;
	LLONG lLoginHandle = 0;

	char szDevIp[32] = {0};
	char szUserName[32] = {0};
	char szPassWord[32] = {0};
	int nPort = 0;
	printf("Device IP: \n");
	scanf("%s", szDevIp);
	printf("Device port:\n");
	scanf("%d", &nPort);
	printf("username:\n");
	scanf("%s",szUserName);
	printf("password:\n");
	scanf("%s",szPassWord);

	lLoginHandle = CLIENT_LoginEx(szDevIp, nPort, szUserName, szPassWord,0, NULL, &stDevInfo, &nError);
	if (lLoginHandle == 0)
	{
		printf("login device failed! nerror = %d\n", nError);
		Clean(NULL, lLoginHandle);
		return 0;
	}    
	else
	{
		printf("login device success, channel number:%d", stDevInfo.byChanNum);
	}

	int nPlayBackType = 0;
	char cContinue = 'y';
	LLONG lPlayBackHandle = 0;
	// playback
	while (cContinue =='y')
	{
		if (lPlayBackHandle != 0)
		{
			CLIENT_StopPlayBack(lPlayBackHandle);
			lPlayBackHandle = 0;
		}

		printf("please choose the way you playback:(1-playback by file, 2-play back by time)");
		scanf("%d", &nPlayBackType);

		if (nPlayBackType == 1)
		{
			// playback by file
			int nChannelId = 0;
			printf("Please input a channel ID:");
			scanf("%d",&nChannelId);
			int nRecordFileType = 0;
			char choose;
			printf("*******************************************\n");
			printf("*********A:all record file********************\n");
			printf("*********B:alarm ************************\n");
			printf("*********C:motion alarm********************\n");
			printf("*********D:all alarm************************\n");
			printf("*********E:queryrecord file by card id************************\n");
			printf("*********F:combination condition query********************\n");
			printf("*********G:query picture by card id******************\n");
			printf("*********H:picture************************\n");
			printf("*******************************************\n");
			printf("Please select:");
			scanf("%c", &choose);
			scanf("%c", &choose);
			switch(choose)
			{
			case 'A':
				nRecordFileType = 0;
				break;
			case 'B':
				nRecordFileType = 1;
				break;
			case 'C':
				nRecordFileType = 2;
				break;
			case 'D':
				nRecordFileType = 3;
				break;
			case 'E':
				nRecordFileType = 4;
				break;
			case 'F':
				nRecordFileType = 5;
				break;
			case 'G':
				nRecordFileType = 8;
				break;
			case 'H':
				nRecordFileType = 9;
				break;
			default:
				printf("input error\n");
				Clean(lPlayBackHandle, lLoginHandle);
				return 0;
			}
			NET_TIME tmStart = {0};
			printf("Please input the start time you want to query(2011-4-6 15:10:55)");
			scanf("%d-%d-%d %d:%d:%d",&(tmStart.dwYear),&(tmStart.dwMonth),&(tmStart.dwDay),&(tmStart.dwHour),&(tmStart.dwMinute),&(tmStart.dwSecond));
			NET_TIME tmEnd = {0};
			printf("Please input the end time you want to query(2011-4-6 15:53:32)");
			scanf("%d-%d-%d %d:%d:%d",&(tmEnd.dwYear),&(tmEnd.dwMonth),&(tmEnd.dwDay),&(tmEnd.dwHour),&(tmEnd.dwMinute),&(tmEnd.dwSecond));

			char cCardid = 0;
			if (nRecordFileType==4)
			{
				printf("Please input the card ID:");
				scanf("%c",&cCardid);
			} 

		 	NET_RECORDFILE_INFO szFileInfos[100] = {0};
		 	int nMaxlen = sizeof(NET_RECORDFILE_INFO) * 100;
		 	int nFileCount = 0;
			int waittime = 1200;
			bool bTime = FALSE;
			BOOL bRet = CLIENT_QueryRecordFile(lLoginHandle,nChannelId,nRecordFileType,&tmStart,&tmEnd,&cCardid,szFileInfos,nMaxlen, &nFileCount,waittime,bTime);
			if (bRet)
			{
				printf("Call CLIENT_QueryRecordFile success! nFileCount = %d\n", nFileCount);
				for(int i = 0; i < nFileCount; i++)
				{
					printf("\n********File info %d******************\n", i);
					printf("********channel id:%d*********************\t",szFileInfos[i].ch);
					printf("********file name:%s*********************\t",szFileInfos[i].filename);
					printf("********file lenth:%d*******************\t",szFileInfos[i].size);
					printf("********start time:%d-%d-%d %d:%d:%d****\t",szFileInfos[i].starttime.dwYear,szFileInfos[i].starttime.dwMonth,szFileInfos[i].starttime.dwDay,szFileInfos[i].starttime.dwHour,szFileInfos[i].starttime.dwMinute,szFileInfos[i].starttime.dwSecond);
					printf("********end time:%d-%d-%d %d:%d:%d****\t",szFileInfos[i].endtime.dwYear,szFileInfos[i].endtime.dwMonth,szFileInfos[i].endtime.dwDay,szFileInfos[i].endtime.dwHour,szFileInfos[i].endtime.dwMinute,szFileInfos[i].endtime.dwSecond);
					printf("********drive no.:%d*********************\t",szFileInfos[i].driveno);
					printf("********start cluster:%d*******************\t",szFileInfos[i].startcluster);
					printf("********file type:%d***************\t",szFileInfos[i].nRecordFileType);
					printf("********record file rank:%d*******************\t",szFileInfos[i].bImportantRecID);
					printf("********file hint:%d***************\t",szFileInfos[i].bHint);
				}

				int nIndex = 0;
				printf("please input the file index that you want to play(0 - %d)\n",nFileCount-1);
				scanf("%d", &nIndex);
				if(nIndex < 0 || nIndex >=	nFileCount)
				{
					printf("error input\n");
					Clean(lPlayBackHandle, lLoginHandle);
					return 0;
				}

				lPlayBackHandle = CLIENT_PlayBackByRecordFileEx(lLoginHandle,&szFileInfos[nIndex],NULL,DownLoadPosCallBackFunc,0,DataCallBackFunc,0);
				if (lPlayBackHandle != 0)
				{
					printf("play back by file successful!");
				}
				else
				{
					printf("play back by file failed!");
				}
			} 
			else
			{
				printf("Call CLIENT_QueryRecordFile failed!\n");
			}
		}
		else if (nPlayBackType == 2)
		{
			// play back by time
			int nChannelId = 0;
			printf("Please input a channel ID:");
			scanf("%d", &nChannelId);
			NET_TIME stStartTime = {0};
			NET_TIME stStopTime = {0};
			printf("Please input the start time you want to play back(2011-4-6 15:10:55)");
			scanf("%d-%d-%d %d:%d:%d",&(stStartTime.dwYear),&(stStartTime.dwMonth),&(stStartTime.dwDay),&(stStartTime.dwHour),&(stStartTime.dwMinute),&(stStartTime.dwSecond));
			printf("Please input the end time you want to query(2011-4-6 15:53:32)");
			scanf("%d-%d-%d %d:%d:%d",&(stStopTime.dwYear),&(stStopTime.dwMonth),&(stStopTime.dwDay),&(stStopTime.dwHour),&(stStopTime.dwMinute),&(stStopTime.dwSecond));

			lPlayBackHandle = CLIENT_PlayBackByTimeEx(lLoginHandle,nChannelId,&stStartTime,&stStopTime,NULL,DownLoadPosCallBackFunc,0,DataCallBackFunc,0);
			if (lPlayBackHandle != 0)
			{
				printf("play back by time success\n");
			}
			else
			{
				printf("play back by time failed\n");
			}
		}

		printf("do you want to continue?(y/n)");
		cContinue  = getchar();
		cContinue  = getchar();
	}

	Clean(lPlayBackHandle, lLoginHandle);

	return 0;
}

