#include "XNetSDKTest.h"
#include "XNetSDKSyn.h"

XSDK_HANDLE g_hDevice = 0;
SXMediaRecordByFileReq info = {0};
bool bFindFile = false;

#define TEST_DAS_SERVER 1
int CALLBACK Main_MessageCallBack(XSDK_HANDLE hDevice, int nMsgId, int nParam1, int nParam2, int nParam3, const char *szString, void *pObject, int nSeq, void *pUserData)
{
	switch(nMsgId)
	{
	case ESXSDK_DEV_FIND_FILE:
		{
			OnDevFindFile(nParam1, (SXSDKQueryRecordRes *)pObject, nParam2, nSeq);
		}
		break;
	case ESXSDK_DEV_LOGIN:
		{
			printf("ESXSDK_DEV_LOGIN_Ret[%d]\r\n", nParam1);
			if (nParam1 == EXSDK_ER_UserOrPassword)
			{
				XSDK_DevLogout(hDevice);
			}

			if (nParam1 < 0)
			{
				printf("ESXSDK_DEV_LOGIN__Error\r\n");
			}
		}
		break;
	case ESXSDK_ON_DEV_STATE:
		{
			printf("ESXSDK_ON_DEV_STATE[%s]\r\n", nParam1 == 6 ? "ESTATE_DEV_Logined" : "ESTATE_DEV_NetDisConnect");
		}
		break;
	case ESXSDK_DEV_GENERAL_COMMAND:	// 通用交互返回
	case ESXSDK_DEV_GET_SYS_CONFIG:		// 设备系统配置获取
	case ESXSDK_DEV_SET_SYS_CONFIG:		// 设备系统配置设置
	case ESXSDK_DEV_GET_CHN_CONFIG:		// 设备通道配置获取
	case ESXSDK_DEV_SET_CHN_CONFIG:		// 设备通道配置设置
		{
			OnDevConfig(nMsgId, nParam1, szString, (const char *)pObject, nParam2, nSeq);
		}
		break;
	case ESXSDK_ON_SEARCH_DEVICES:
		{
			OnDevSearchResult(nParam1, (SXSDK_CONFIG_NET_COMMON *)pObject, nParam2);
		}
		break;
	case EXCMD_ALARM_REQ:
		{
			OnDevAlarmCallback(hDevice, szString);
		}
		break;
	case ESXSDK_DEV_UPGRADE:
		{
			OnUpgradeCallback((EUpgradeStep)nParam1, nParam2, nParam3);
		}
		break;
	case ESXSDK_ON_DAS_SERVER_START:
		{
			OnDASServerStart(hDevice, nParam1);
		}
		break;
	case ESXSDK_ON_DAS_DEVICE_REGIST:
		{
#ifdef TEST_DAS_SERVER
			g_hDevice = hDevice;

#endif
			OnDASDeviceReg(hDevice, (SXSDKDASDeviceInfo *)pObject);
		}
		break;
	case ESXSDK_DEV_SNAP:
		{
			OnDevSnap(nParam1, (const char *)pObject, nParam2, nSeq);
		}
		break;
	case EXCMD_RS485_TRANSPARENT_DATA_REQ:
	case EXCMD_RS232_TRANSPARENT_DATA_REQ:
		{
			OnTransCommCallback(hDevice, szString);
		}
		break;
	default:
		printf("[Dev:%ld][MSG:%d][Ret:%d][Param2:%d][Param3:%d][szParam:%s][Seq:%d]\r\n", hDevice, nMsgId, nParam1, nParam2, nParam3, szString == NULL ? "" : szString, nSeq);
		break;
	}
	return 0;
}

/*****************************************************************************************************
*																									 *
*注:																								 *
*获取到的数据或用于设置的数据可使用本的demo带有的JObject进行解析，也可自己网上找CJson源码集成进行解析*
*																									 *
******************************************************************************************************/

int main(int argc, char *argv[])
{
	SXSDKInitParam initParam = {0};
	//initParam.nLogLevel = 3;
	initParam.pMsgCallBack = Main_MessageCallBack;
	initParam.pUserData = (void *)0x123456;
	XSDK_Init(&initParam);

	// 测试主动注册
#ifdef TEST_DAS_SERVER
	TestDASServer();
#else
        printf("start login===============");
	const char *szDevID = "000002";
	int nDevPort = 8989;
	const char *szUser = "admin";
	const char *szPwd = "";
	if (false && argc > 4)
	{
		szDevID = argv[1];
		nDevPort = atoi(argv[2]);
		szUser = argv[3];
		szPwd = argv[4];
	}

	SXSDKLoginParam devParam = {0};
	strcpy(devParam.sDevId, szDevID);
	devParam.nDevPort = nDevPort;
	strcpy(devParam.sUserName, szUser);
	strcpy(devParam.sPassword, szPwd);

	g_hDevice = XSDK_DevLoginSyn(&devParam, 10000);
//	g_hDevice = XSDK_DevLogin(&devParam, 4000, 0);
        printf("g_hDevice::" + g_hDevice);

#endif

	//// 测试设备配置
	//TestDevSystemConfig();

	////测试相机相关配置
	//TestCameraConfig();

	////测试编码格式配置
	// TestEncodeConfig();

	//// 测试设备云台功能
	//TestDevPTZ();

	//// 测试录像查询
	//TestDevFindFile();

	//// 设备设备报警回调
	//TestDevAlarmCallback();

	//// 测试实时视频
	 TestMediaRealPlay();

	//// 设备录像回放
	// TestMediaRecordPlay();

	//// 测试人脸抓图(带有人脸功能的设备专用)
	//TestMediaFaceImage();
	
	//// 设备搜索并修改搜索到的设备IP（默认修改搜索到的第一个设备，密码是写死的需要再代码里修改）
	//TestDevSearch();

	//测试修改设备密码
	//TestModifyPassword();

	//测试恢复默认配置
	//TestSetDefaultConfig();

	//测试录像下载
	//TestDownload();

	//测试控制设备
	//TestOPMachine();

	// 测试设备通道抓图
	//TestDevSnap();

	//测试升级设备程序
	//TestUpgrade();

	//测试打开透明串口通道
	//TestOpenTransChannel();

	//测试向串口写数据
	//TestSerialWrite();

	////测试对讲
	//TestDevTalk();
	
	//日志查询
	//TestOPLogQuery();
	
	///视频叠加
	//TestOSDInfo();

	////图像参数配置
	//TestVideoCfg();

	////云台预置点及巡航
	//TestPTZPreset();

	/////按文件名称下载（一般用于下载存储在SD卡上的图片）
	//TestDownloadByName();

	if (g_hDasServer)
	{
		XSDK_StopDASServer(g_hDasServer);
		g_hDasServer = 0;
	}

	XSDK_DevLogout(g_hDevice);
	XSDK_UnInit();

	return 0;
}

