#include "XNetSDKTest.h"
#include "OPPTZControl.h"
#include "XNetSDKSyn.h"
#ifdef WIN32 
#include <Windows.h>
#else
#include <unistd.h>
#define Sleep(x) usleep((x) * 1000)
#endif

USE_NS_NETSDK_CFG

int XSDK_SetPtzTourPoint(XSDK_HANDLE hDevice, int nChannel, int nPresetPoint, int nStep, int nTourIndex, bool bAdd)
{
	char outbuffer[100] = {0};
	int len = 0;

	XSDK_CFG::OPPTZControl ptz;
	ptz.Command =  bAdd ? JPTZ_AddTour : JPTZ_DeleteTour;	//添加或删除预置点
	ptz.mParameter.Channel = nChannel;			//通道号
	ptz.mParameter.Preset = nPresetPoint;		//巡航点（已设置的预置点）
	ptz.mParameter.Step = nStep;				//步长
	ptz.mParameter.Tour = nTourIndex;			//巡航路线值

	const char *pCfg = ptz.ToString();
	return XSDK_DevSetSysConfigSyn(g_hDevice, JK_OPPTZControl, pCfg, strlen(pCfg), outbuffer, &len, 5000, EXCMD_PTZ_REQ);
}

int TestPTZPreset()
{
	printf("%s\r\n", __FUNCTION__);
	char c = 0;
	while((c = getchar()) != 'q')
	{
		switch(c)
		{
			///设置预置点1
		case 'a':
			{
				XSDK_DevPtzPreset(g_hDevice, 0, S_XPTZ_SetPreset, 1);
			}
			break;
			///转到预置点1
		case 'b':
			{
				XSDK_DevPtzPreset(g_hDevice, 0, S_XPTZ_GotoPreset, 1);
			}
			break;
			////清除预置点1
		case 'c':
			{
				XSDK_DevPtzPreset(g_hDevice, 0, S_XPTZ_ClearPreset, 1);
			}
			break;
			///设置预置点2,设置2前转动一下云台
		case 'd':
			{
				XSDK_DevPtzPreset(g_hDevice, 0, S_XPTZ_SetPreset, 2);
			}
			break;
			///转到预置点2
		case 'e':
			{
				XSDK_DevPtzPreset(g_hDevice, 0, S_XPTZ_GotoPreset, 2);
			}
			break;
			////清除预置点2
		case 'f':
			{
				XSDK_DevPtzPreset(g_hDevice, 0, S_XPTZ_ClearPreset, 2);
			}
			break;
		case 'g':
			{
				int nRet = 0;
				nRet = XSDK_SetPtzTourPoint(g_hDevice, 0, 1, 4, 1, true);

				if(nRet >= 0)
				{
					nRet = XSDK_SetPtzTourPoint(g_hDevice, 0, 2, 4, 1, true);
				}
			}
			break;
		case 'h':
			{
				int nRet = 0;
				XSDK_SetPtzTourPoint(g_hDevice, 0, 1, 4, 1, false);
					
				if(nRet >= 0)
				{
					nRet = XSDK_SetPtzTourPoint(g_hDevice, 0, 2, 4, 1, false);
				}

			}
			break;
			///巡航前需要设置至少两个预置点
		case 't':
			{
				XSDK_DevPtzTour(g_hDevice, 0, 1, true);

//				XSDK_DevPtzTour(g_hDevice, 0, 1, false);
			}
			break;
		}
	}
	return 0;
}
