#include "hdKingAPI.h"
#include "error_code.h"
#include <stdio.h>
#include <string.h>
#include <Windows.h>

//************************************
// Method:    main
// FullName:  main
// Access:    public 
// Returns:   int32
// Qualifier:
//************************************
int32 main()
{ 
	int32 nRet = RD_SUCCESS;
	HD3Connection connection;
	uint32 nID = 0;
	HD3PtTagProp ptTag;
	HD3Mask mask;
	const char* szTagName_1 = "szTagName_1";
	const char* szTagName_2 = "szTagName_2";
	const int32 nREC_NUM = 2;
	int32 nNum = 0;
	HD3Record snapshotsSaved[nREC_NUM];
	uint32 nTagIDs[nREC_NUM];
	int32 nErrorCodes[nREC_NUM];
	HD3Record snapshotsQueried[nREC_NUM];
	int32 i = 0;
	memset(&connection, 0x00, sizeof(connection));
	strncpy(connection.szAddress, "168.2.9.95", HD3_LEN_ADDRESS * HD3_BASE - 1);
	connection.nPort = 5673;
	connection.nTimeout = 3;
	memset(&ptTag, 0x00, sizeof(ptTag));
	memset(&mask, 0x00, sizeof(mask));
	memset(snapshotsSaved, 0x00, nREC_NUM * sizeof(HD3Record));
	memset(nErrorCodes, 0x00, nREC_NUM * sizeof(int32));
	memset(snapshotsQueried, 0x00, nREC_NUM * sizeof(HD3Record));

	//connect
	nRet = nt3_connect(&connection);
	if (nRet != RD_SUCCESS)
	{
		printf("connect to server failed, error code[%d]!\n", nRet);
		return -1;
	}
	printf("connect to server successful!\n");

	nRet = sc3_login("admin", "admin");

	//add two tag , type is int32 and string
	nID = 0;
	memset(&ptTag, 0x00, sizeof(ptTag));
	memset(&mask, 0x00, sizeof(mask));
	strncpy(ptTag.szTagName, szTagName_1, HD3_LEN_TAG_NAME * HD3_BASE - 1);
	ptTag.nTagType = HD3_TAG_TYPE_INT32;
	mask.nCommMask = HD3M_COMM_PROP_TAG_NAME | HD3M_COMM_PROP_TAG_TYPE;
	nRet = pt3_add_tag(&ptTag, &mask, NULL, &nID);
	if (nRet != RD_SUCCESS)
	{
		printf("add tags[%s] failed, error code [%d]!\n", ptTag.szTagName, nRet);
		nt3_disconnect();
		return -1;
	}
	printf("add tags[%s] successful!\n", ptTag.szTagName);

	nID = 0;
	memset(&ptTag, 0x00, sizeof(ptTag));
	memset(&mask, 0x00, sizeof(mask));
	strncpy(ptTag.szTagName, szTagName_2, HD3_LEN_TAG_NAME * HD3_BASE - 1);
	ptTag.nTagType = HD3_TAG_TYPE_STRING;
	mask.nCommMask = HD3M_COMM_PROP_TAG_NAME | HD3M_COMM_PROP_TAG_TYPE;
	nRet = pt3_add_tag(&ptTag, &mask, "",  &nID);
	if (nRet != RD_SUCCESS)
	{
		printf("add tags[%s] failed, error code [%d]!\n", ptTag.szTagName, nRet);
		nt3_disconnect();
		return -1;
	}
	printf("add tags[%s] successful!\n", ptTag.szTagName);

	//query tag ids
	nRet = tag3_query_id_by_name(szTagName_1, &nTagIDs[0]);
	if (nRet != RD_SUCCESS)
	{
		printf("query tag id failed, tag name[%s], error code[%d]!\n", szTagName_1, nRet);
		nt3_disconnect();
		return -1;
	}
	printf("tag[%s] query tag id successful, tag id[%d]!\n",  szTagName_1, nTagIDs[0]);

	nRet = tag3_query_id_by_name(szTagName_2, &nTagIDs[1]);
	if (nRet != RD_SUCCESS)
	{
		printf("query tag id failed, tag name[%s], error code[%d]!\n", szTagName_2, nRet);
		nt3_disconnect();
		return -1;
	}
	printf("tag[%s] query tag id successful, tag id[%d]!\n",  szTagName_2, nTagIDs[1]);

	Sleep(21000);

	//save one snapshot
	snapshotsSaved[0].nTagType = HD3_TAG_TYPE_INT32;
	snapshotsSaved[0].nSec = 3600;
	snapshotsSaved[0].nMsec = 0;
	snapshotsSaved[0].nQuality = 192;
	snapshotsSaved[0].value.nInt32 = 111;
	nRet = sn3_save_snapshot(nTagIDs[0], &(snapshotsSaved[0]));
	if (nRet != RD_SUCCESS)
	{
		printf("save snapshot failed, tag id [%d], error code [%d]!\n", nTagIDs[0], nRet);
	}
	printf("save snapshot successful, tag id[%d]!\n", nTagIDs[0]);

	//query one snapshot
	nRet = sn3_query_snapshot(nTagIDs[0], &(snapshotsQueried[0]));
	if (nRet != RD_SUCCESS)
	{
		printf("query snapshot failed, tag id [%d], error code [%d]!\n", nTagIDs[0], nRet);
	}
	printf("query snapshot successful, tag id[%d]!\n", nTagIDs[0]);

	//save disorder snapshots
	snapshotsSaved[0].nSec = 3600 - 60;
	snapshotsSaved[0].nMsec = 0;
	snapshotsSaved[0].nQuality = 192;
	snapshotsSaved[0].value.nInt32 = 111;
	snapshotsSaved[1].nSec = 3600 + 60;
	snapshotsSaved[1].nMsec = 0;
	snapshotsSaved[1].nQuality = 192;
	snapshotsSaved[1].value.nInt32 = 222;	
	snapshotsSaved[0].nTagType = HD3_TAG_TYPE_INT32;
	snapshotsSaved[1].nTagType = HD3_TAG_TYPE_INT32;

	nRet = sn3_save_disorder_snapshots(nREC_NUM, nTagIDs[0], snapshotsSaved, nErrorCodes);
	if (nRet != RD_SUCCESS)
	{
		printf("save disorder snapshots failed,  tag id [%d], error code [%d]!\n", nTagIDs[0], nRet);
	}
	printf("save disorder snapshots successful, tag id [%d]!\n", nTagIDs[0]);

	// save snapshots
	for (i = 0; i < nREC_NUM; i ++)
	{
		snapshotsSaved[i].nSec = 7200;
		snapshotsSaved[i].nMsec = 0;
		snapshotsSaved[i].nQuality = 192;
	}
	snapshotsSaved[0].value.nInt32 = 123;	
	snapshotsSaved[1].value.strBlob.pBuf = new char[HD3_LEN_VAR_TYPE_VALUE];
	strcpy(snapshotsSaved[1].value.strBlob.pBuf, "this is a string");
	snapshotsSaved[1].value.strBlob.nLenBuf = strlen(snapshotsSaved[1].value.strBlob.pBuf) + 1;
	snapshotsSaved[1].nTagType = HD3_TAG_TYPE_STRING;

	nRet = sn3_save_snapshots(nREC_NUM, nTagIDs, snapshotsSaved, nErrorCodes);
	if (RD_SUCCESS == nRet)
	{
		printf("save snapshots successful!\n");
	}
	else if (EC_HD_API_SAVE_SNAPSHOTS_FAILED == nRet)
	{
		for (i = 0; i < nREC_NUM; i ++)
		{
			if (nErrorCodes[i] != RD_SUCCESS)
			{
				printf("tag[%d] save snapshot failed, error code [%d]!\n", nTagIDs[i], nErrorCodes[i]);
			}			
		}
	}
	else
	{
		printf("save snapshots failed, error code[%d]!\n", nRet);
		nt3_disconnect();
		delete[] snapshotsSaved[1].value.strBlob.pBuf;
		snapshotsSaved[1].value.strBlob.pBuf = NULL;
		return -1;
	}
	delete[] snapshotsSaved[1].value.strBlob.pBuf;
	snapshotsSaved[1].value.strBlob.pBuf = NULL;

	//query sanpshots
	snapshotsQueried[1].value.strBlob.pBuf = new char[HD3_LEN_VAR_TYPE_VALUE];
	snapshotsQueried[1].value.strBlob.nLenBuf = HD3_LEN_VAR_TYPE_VALUE;
	nRet = sn3_query_snapshots(nREC_NUM, nTagIDs, snapshotsQueried, nErrorCodes);
	if (RD_SUCCESS == nRet)
	{
		printf("query snapshots successful!\n");
	}
	else if (EC_HD_API_SAVE_SNAPSHOTS_FAILED == nRet)
	{
		for (i = 0; i < nREC_NUM; i ++)
		{
			printf("tag[%d] query snapshot failed, error code [%d]!\n", nTagIDs[i], nErrorCodes[i]);
		}
	}
	else
	{
		printf("query snapshots failed, error code[%d]!\n", nRet);
		nt3_disconnect();
		delete[] snapshotsQueried[1].value.strBlob.pBuf;
		snapshotsQueried[1].value.strBlob.pBuf = NULL;
		return -1;
	}
	delete[] snapshotsQueried[1].value.strBlob.pBuf;
	snapshotsQueried[1].value.strBlob.pBuf = NULL;	

	//disconnect
	nt3_disconnect();

	return 0;
} 
