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

USE_NS_NETSDK_CFG

int TestDevPTZ()
{
	printf("%s\r\n", __FUNCTION__);
	char c = 0;
	while((c = getchar()) != 'q')
	{
		switch(c)
		{
			//方向控制:左，步长4
		case 'a':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionLeft, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionLeft, 4, true);
			}
			break;
			//方向控制:右，步长4
		case 'b':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRight, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRight, 4, true);
			}
			break;
			//方向控制:上，步长4
		case 'c':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionUp, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRight, 4, true);
			}
			break;
			//方向控制:下，步长4
		case 'd':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionDown, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionDown, 4, true);
			}
			break;
			//方向控制:左上，步长4
		case 'e':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionLeftUp, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionLeftUp, 4, true);
			}
			break;
			//方向控制:右上，步长4
		case 'f':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRightUp, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRightUp, 4, true);
			}
			break;
			//方向控制:左下，步长4
		case 'g':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionLeftDown, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionLeftDown, 4, true);
			}
			break;
			//方向控制:右下，步长4
		case 'h':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRightDown, 4, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_DirectionRightDown, 4, true);
			}
			break;
			//光圈缩小
		case 'i':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_IrisSmall, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_IrisSmall, 0, true);
			}
			break;
			//光圈扩大
		case 'k':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_IrisLarge, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_IrisLarge, 0, true);
			}
			break;
			//变焦（焦点前调）
		case 'l':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_FocusNear, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_FocusNear, 0, true);
			}
			break;
			//变焦（焦点后调）
		case 'm':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_FocusFar, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_FocusNear, 0, false);
			}
			break;
			//变倍小
		case 'n':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_ZoomWide, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_ZoomWide, 0, true);
			}
			break;
			//变倍大 
		case 'o':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_ZoomTile, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_ZoomTile, 0, true);
			}
			break;
			//复位
		case 'p':
			{
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_Reset, 0, false);
				Sleep(300);
				XSDK_DevPtzControl(g_hDevice, 0, JPTZ_Reset, 0, true);
			}
			break;
			//云台协议
		case 'r':
			{
				//获取云台配置
				int  nResult = 0;
				char szOutBuffer[4096] = {0};
				int  nInOutSize = sizeof(szOutBuffer);
				char szOutBuffer1[4096] = {0};
				int  nInOutSize1 = sizeof(szOutBuffer1);
				
				nResult = XSDK_DevGetSysConfigSyn(g_hDevice, JK_Uart_PTZ, szOutBuffer, &nInOutSize, 4000, EXCMD_CONFIG_GET);

				if (nResult < 0)
				{
					break;
				}
				//获取云台支持的协议
				nResult = XSDK_DevGetSysConfigSyn(g_hDevice, "PTZProtocol", szOutBuffer1, &nInOutSize1, 4000, EXCMD_ABILITY_GET);

				JObjArrayObject<JStrObj> cfg(NULL, "PTZProtocol");
				cfg.Parse(szOutBuffer1);

				for(int i = 0;i < cfg.objs.Size();i++)
				{
					printf("PTZProtocol[%s]\r\n", cfg.objs[i].ToString());
				}
				
				
				JObjArrayObject<Uart_PTZ> uart(NULL, JK_Uart_PTZ);
				uart.Parse(szOutBuffer);

				printf("ProtocolName = %s\r\n", uart.objs[0].ProtocolName.Value());

				const char * ProtocolName = cfg.objs[0].ToString();
				uart.objs[0].ProtocolName.SetValue(ProtocolName);

				char outbuffer[100] = {0};
				int len = 0;
				const char *pCfg = uart.ToString();
				nResult = XSDK_DevSetSysConfigSyn(g_hDevice, JK_Uart_PTZ, pCfg, strlen(pCfg),outbuffer, &len, 4000);
				
			}
			break;
		}
	}
	return 0;
}
