// SATHTTP.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "SATHTTP.h"
#include <direct.h>

CCurlClient g_curl;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

__if_not_exists(NotifyJobStart)
{
	int NotifyJobStart(std::string url, const SATHTTP::STNotifyJobStartReq &stNotifyJobStartReq, SATHTTP::STNotifyJobStartResp &stNotifyJobStartResp) throw()
	{
		return SetResult(url, stNotifyJobStartReq, stNotifyJobStartResp);
	}
}

__if_not_exists(ProcessJob)
{
	int ProcessJob(std::string url, const SATHTTP::STJobProcessReq &stJobProcessReq, SATHTTP::STJobProcessResp &stJobProcessResp) throw()
	{
		return SetResultList(url, stJobProcessReq, stJobProcessResp);
	}
}

#ifdef __CONSOLE__

// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

bool MKDIR(LPCTSTR dir);

__if_not_exists(NotifyJobStart)
{
	int NotifyJobStart(std::string url, const SATHTTP::STNotifyJobStartReq &stNotifyJobStartReq, SATHTTP::STNotifyJobStartResp &stNotifyJobStartResp) throw()
	{
		return SetResult(url, stNotifyJobStartReq, stNotifyJobStartResp);
	}
}

__if_not_exists(ProcessJob)
{
	int ProcessJob(std::string url, const SATHTTP::STJobProcessReq &stJobProcessReq, SATHTTP::STJobProcessResp &stJobProcessResp) throw()
	{
		return SetResultList(url, stJobProcessReq, stJobProcessResp);
	}
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
		UrlPars ups;
		std::string host = "10.118.159.132:8580/btc_task_se/downLoadFile!downLoadFile.action?fileDir=/RT_2841/预抄写_RT2841/DVB&fileName=A5600007AAB_DVB_DTV_EPG.py";
		CCurlClient::ParseURL(host, ups);

		for ( std::map<std::string,std::string>::iterator it = ups.pars.begin(); it != ups.pars.end(); it++ ) {
			it->second = CharEncoding::EnCode_UTF8URL(it->second.c_str());
		}

		printf("----\n");
	}

	system("pause");

	return nRetCode;
}

#endif //__CONSOLE__

bool MKDIR(LPCTSTR dir) 
{
	//////////////////////////////////////////////////////////////////////////
	// 创建目录;
	int nleft = 0;
	int nIndex = -1;
	std::string strdir = dir;
	strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));

	if (strdir.at(strdir.size() - 1) != _T('\\'))
		strdir.append(_T("\\"));

	// 共享路径和硬盘盘符;
	if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
		nleft = strdir.find_first_of(_T("\\"), 2) + 1;	// 去除共享主机名;
	else if (strdir.at(2) == _T('\\'))
		nleft = 3;

	do
	{
		nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));

		if (nIndex != string::npos)
		{
			if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
			{
				return false;
			}

			nleft += nIndex + 1;
		}
	} while (nIndex != -1);

	return true;
};

SATHTTP_API int Login(std::string url, const SATHTTP::STLoginReq &stLoginReq, SATHTTP::STLoginResp &stLoginResp)
{
	if ( !g_curl.IsInit() )
		g_curl.Initialize();

	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "function", "aaaa"));
	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", "EXECUTE"));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "userName", stLoginReq.strUserName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "status", stLoginReq.strStatus.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "deleteStatus", stLoginReq.strDeleteStatus.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "ip", stLoginReq.strIP.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "memory", stLoginReq.strMemory.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "storage", stLoginReq.strStorage.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "connectTime", stLoginReq.strConnectTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "executeName", stLoginReq.strExecuteName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "mac", stLoginReq.strMAC.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "disconnectTime", stLoginReq.strDisconnectTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "password", stLoginReq.strPassword.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "cpu", stLoginReq.strCPU.c_str()));

		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stLoginResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stLoginResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stLoginResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					if ( stLoginResp.strCode == "00" )
					{				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL)
						{
							stLoginResp.nPortCount = cJSON_GetObjectItem(pObj, "portCount") ? cJSON_GetObjectItem(pObj, "portCount")->valueint : -1;
							stLoginResp.nCompanyId = cJSON_GetObjectItem(pObj, "companyId") ? cJSON_GetObjectItem(pObj, "companyId")->valueint : -1;
							stLoginResp.strRunnerToken = cJSON_GetObjectItem(pObj, "runnerToken") ? cJSON_GetObjectItem(pObj, "runnerToken")->valuestring : "";
						}

						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}

SATHTTP_API int UpdateDeviceMessage(std::string url, const SATHTTP::STUpdateDeviceReq &stUpdateDeviceReq, SATHTTP::STUpdateDeviceResp &stUpdateDeviceResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", "Execute"));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "status", stUpdateDeviceReq.strStatus.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "userName", stUpdateDeviceReq.strUserName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "ip", stUpdateDeviceReq.strIP.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "storage", stUpdateDeviceReq.strStorage.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runnerName", stUpdateDeviceReq.strRunnerName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "mac", stUpdateDeviceReq.strMAC.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "reportType", stUpdateDeviceReq.strReportType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "startTime", stUpdateDeviceReq.strStartTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "memory", stUpdateDeviceReq.strMemory.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "endTime", stUpdateDeviceReq.strEndTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "cpu", stUpdateDeviceReq.strCPU.c_str()));
		cJSON *pDeviceList = cJSON_CreateArray();
		if ( pDeviceList )
		{
			cJSON_AddItemToObject(pData, "devicelist", pDeviceList);
			for ( std::vector<SATHTTP::STDevice>::const_iterator it = stUpdateDeviceReq.devicelist.begin(); it != stUpdateDeviceReq.devicelist.end(); it++ )
			{
				cJSON *pObj = cJSON_CreateObject();
				if ( pObj )
				{
					ASSERT(cJSON_AddStringToObject(pObj, "status", it->strStatus.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "deviceSerial", it->strDeviceSerial.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "hardwareVersion", it->strHardwareVersion.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "manu", it->strManu.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "deviceMac", it->strDeviceMac.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "lastJobStartTime", it->strLastJobStartTime.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "lastTimeBreak", it->strLastTimeBreak.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "comments", it->strComments.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "cpu", it->strCPU.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "softwareVersion", it->strSoftwareVersion.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "phoneNumber", it->strPhoneNumber.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "lastJobFinishTime", it->strLastJobFinishTime.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "memory", it->strMemory.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "model", it->strModel.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "lastJob", it->strLastJob.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "lastTimeConnected", it->strLastTimeConnected.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "electric", it->strElectric.c_str()));

					cJSON_AddItemToObject(pDeviceList, "", pObj);
				}
			}
		}

		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stUpdateDeviceResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stUpdateDeviceResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stUpdateDeviceResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					if ( stUpdateDeviceResp.strCode == "00" )
					{				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL)
						{
							
						}

						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}


SATHTTP_API int Heartbeat(std::string url, const SATHTTP::STHeartbeatReq &stHeartbeatReq, SATHTTP::STHeartbeatResp &stHeartbeatResp) 
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", "EXECUTE"));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "userName", stHeartbeatReq.strUserName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runnerMac", stHeartbeatReq.strRunnerMac.c_str()));
		cJSON *pDeviceList = cJSON_CreateArray();
		if ( pDeviceList )
		{
			cJSON_AddItemToObject(pData, "deviceList", pDeviceList);
			for ( std::vector<SATHTTP::STDevice>::const_iterator it = stHeartbeatReq.devicelist.begin(); it != stHeartbeatReq.devicelist.end(); it++ )
			{
				cJSON *pObj = cJSON_CreateObject();
				if ( pObj )
				{
					ASSERT(cJSON_AddStringToObject(pObj, "status", it->strStatus.c_str()));
					ASSERT(cJSON_AddStringToObject(pObj, "deviceSerial", it->strDeviceSerial.c_str()));

					cJSON_AddItemToObject(pDeviceList, "", pObj);
				}
			}
		}

		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stHeartbeatResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stHeartbeatResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stHeartbeatResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					if ( stHeartbeatResp.strCode == "00" )
					{				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL)
						{							
							int nSise = cJSON_GetArraySize(pObj);
							for ( int i = 0; i < nSise; i++ )
							{
								cJSON *pTask = cJSON_GetArrayItem(pObj, i);
								if ( pTask )
								{
									SATHTTP::STTask stTask;
									stTask.strInstanceName = cJSON_GetObjectItem(pTask, "instanceName") ? cJSON_GetObjectItem(pTask, "instanceName")->valuestring : "";
									stTask.strExecutionStatus = cJSON_GetObjectItem(pTask, "executionStatus") ? cJSON_GetObjectItem(pTask, "executionStatus")->valuestring : "";
									stTask.strInstanceType = cJSON_GetObjectItem(pTask, "instanceType") ? cJSON_GetObjectItem(pTask, "instanceType")->valuestring : "";
									stTask.nExecuteId = cJSON_GetObjectItem(pTask, "executeId") ? cJSON_GetObjectItem(pTask, "executeId")->valueint : -1;
									stTask.strRequestIP = cJSON_GetObjectItem(pTask, "requestIp") ? cJSON_GetObjectItem(pTask, "requestIp")->valuestring : "";
									stTask.strTaskUseType = cJSON_GetObjectItem(pTask, "taskUseType") ? cJSON_GetObjectItem(pTask, "taskUseType")->valuestring : "";
									stTask.nDeviceId = cJSON_GetObjectItem(pTask, "deviceId") ? cJSON_GetObjectItem(pTask, "deviceId")->valueint : -1;
									stTask.strDiffTaskInstanceFlag = cJSON_GetObjectItem(pTask, "diffTaskInstanceFlag") ? cJSON_GetObjectItem(pTask, "diffTaskInstanceFlag")->valuestring : "";
									stTask.strTaskType = cJSON_GetObjectItem(pTask, "taskType") ? cJSON_GetObjectItem(pTask, "taskType")->valuestring : "";
									stTask.nInstanceId = cJSON_GetObjectItem(pTask, "instanceId") ? cJSON_GetObjectItem(pTask, "instanceId")->valueint : -1;
									stTask.strProgress = cJSON_GetObjectItem(pTask, "progress") ? cJSON_GetObjectItem(pTask, "progress")->valuestring : "";
									stTask.strStartTime = cJSON_GetObjectItem(pTask, "startTime") ? cJSON_GetObjectItem(pTask, "startTime")->valuestring : "";
									stTask.strStopTime = cJSON_GetObjectItem(pTask, "stopTime") ? cJSON_GetObjectItem(pTask, "stopTime")->valuestring : "";
									stTask.strTaskName = cJSON_GetObjectItem(pTask, "taskName") ? cJSON_GetObjectItem(pTask, "taskName")->valuestring : "";
									stTask.Id = cJSON_GetObjectItem(pTask, "id") ? cJSON_GetObjectItem(pTask, "id")->valueint : -1;
									stTask.fd = cJSON_GetObjectItem(pTask, "fd") ? cJSON_GetObjectItem(pTask, "fd")->valueint : -1;
									stTask.nTaskId = cJSON_GetObjectItem(pTask, "taskId") ? cJSON_GetObjectItem(pTask, "taskId")->valueint : -1;
									//stTask.strTaskId = cJSON_GetObjectItem(pTask, "taskId") ? cJSON_GetObjectItem(pTask, "taskId")->valuestring : "0";
									// email其实可忽略;
									cJSON *pEmals = cJSON_GetObjectItem(pTask, "emails");
									if ( pEmals )
									{
										int nEmailSize = cJSON_GetArraySize(pEmals);
										for ( int ii = 0; ii < nEmailSize; ii++ )
										{
											cJSON *pEmail = cJSON_GetArrayItem(pEmals, ii);
											if ( pEmail )
											{
												std::string email = pEmail->valuestring;
												stTask.vtEmails.push_back(email);
											}
										}
									}
									// Job内容;
									cJSON *pJob = cJSON_GetObjectItem(pTask, "taskJson");
									if ( pJob )
									{
										pJob = cJSON_GetObjectItem(pJob, "job");
										if ( pJob )
										{
											stTask.Job.strCloudPort = cJSON_GetObjectItem(pJob, "cloudPort") ? cJSON_GetObjectItem(pJob, "cloudPort")->valuestring : "";
											stTask.Job.strExecuteIP = cJSON_GetObjectItem(pJob, "executeIp") ? cJSON_GetObjectItem(pJob, "executeIp")->valuestring : "";
											stTask.Job.strMonkeyPctSysKeys = cJSON_GetObjectItem(pJob, "monkeyPctSyskeys") ? cJSON_GetObjectItem(pJob, "monkeyPctSyskeys")->valuestring : "";
											stTask.Job.strRedRatDeviceName = cJSON_GetObjectItem(pJob, "redRatDeviceName") ? cJSON_GetObjectItem(pJob, "redRatDeviceName")->valuestring : "";
											stTask.Job.strExecuteId = cJSON_GetObjectItem(pJob, "executeId") ? cJSON_GetObjectItem(pJob, "executeId")->valuestring : "";
											stTask.Job.strMonkeyPctAppSwitch = cJSON_GetObjectItem(pJob, "monkeyPctAppswitch") ? cJSON_GetObjectItem(pJob, "monkeyPctAppswitch")->valuestring : "";
											stTask.Job.strDeviceId = cJSON_GetObjectItem(pJob, "deviceId") ? cJSON_GetObjectItem(pJob, "deviceId")->valuestring : "";
											stTask.Job.strLoopNumber = cJSON_GetObjectItem(pJob, "loopNumber") ? cJSON_GetObjectItem(pJob, "loopNumber")->valuestring : "";
											stTask.Job.strTaskType = cJSON_GetObjectItem(pJob, "taskType") ? cJSON_GetObjectItem(pJob, "taskType")->valuestring : "";
											stTask.Job.strMonkeyFrequency = cJSON_GetObjectItem(pJob, "monkeyFrequency") ? cJSON_GetObjectItem(pJob, "monkeyFrequency")->valuestring : "";
											stTask.Job.strCloudVersion = cJSON_GetObjectItem(pJob, "cloudVersion") ? cJSON_GetObjectItem(pJob, "cloudVersion")->valuestring : "";
											stTask.Job.strTaskInstanceName = cJSON_GetObjectItem(pJob, "taskInstanceName") ? cJSON_GetObjectItem(pJob, "taskInstanceName")->valuestring : "";
											stTask.Job.strRepeatRunTime = cJSON_GetObjectItem(pJob, "repeatRunTime") ? cJSON_GetObjectItem(pJob, "repeatRunTime")->valuestring : "";
											stTask.Job.strMonkeyTime = cJSON_GetObjectItem(pJob, "monkeyTime") ? cJSON_GetObjectItem(pJob, "monkeyTime")->valuestring : "";
											stTask.Job.strOutEquipmentId = cJSON_GetObjectItem(pJob, "outEquipmentId") ? cJSON_GetObjectItem(pJob, "outEquipmentId")->valuestring : "";
											stTask.Job.strPoweroff = cJSON_GetObjectItem(pJob, "poweroff") ? cJSON_GetObjectItem(pJob, "poweroff")->valuestring : "";
											stTask.Job.strFilePath = cJSON_GetObjectItem(pJob, "filePath") ? cJSON_GetObjectItem(pJob, "filePath")->valuestring : "";
											stTask.Job.strScriptFailRepeart = cJSON_GetObjectItem(pJob, "scriptFailRepeat") ? cJSON_GetObjectItem(pJob, "scriptFailRepeat")->valuestring : "";
											stTask.Job.strTaskInstanceId = cJSON_GetObjectItem(pJob, "taskInstanceId") ? cJSON_GetObjectItem(pJob, "taskInstanceId")->valuestring : "";
											stTask.Job.strUserId = cJSON_GetObjectItem(pJob, "userId") ? cJSON_GetObjectItem(pJob, "userId")->valuestring : "";
											stTask.Job.strDiffTaskInstanceFlag = cJSON_GetObjectItem(pJob, "diffTaskInstanceFlag") ? cJSON_GetObjectItem(pJob, "diffTaskInstanceFlag")->valuestring : "";
											stTask.Job.strBusCode = cJSON_GetObjectItem(pJob, "busCode") ? cJSON_GetObjectItem(pJob, "busCode")->valuestring : "";
											stTask.Job.strRound = cJSON_GetObjectItem(pJob, "round") ? cJSON_GetObjectItem(pJob, "round")->valuestring : "";
											stTask.Job.strControlType = cJSON_GetObjectItem(pJob, "controlType") ? cJSON_GetObjectItem(pJob, "controlType")->valuestring : "";
											stTask.Job.strMonkeyPctMajornav = cJSON_GetObjectItem(pJob, "monkeyPctMajornav") ? cJSON_GetObjectItem(pJob, "monkeyPctMajornav")->valuestring : "";
											stTask.Job.strTaskName = cJSON_GetObjectItem(pJob, "taskName") ? cJSON_GetObjectItem(pJob, "taskName")->valuestring : "";
											stTask.Job.strMonkeyType = cJSON_GetObjectItem(pJob, "monkeyType") ? cJSON_GetObjectItem(pJob, "monkeyType")->valuestring : "";
											stTask.Job.strMonkeyDictate = cJSON_GetObjectItem(pJob, "monkeyDictate") ? cJSON_GetObjectItem(pJob, "monkeyDictate")->valuestring : "";
											stTask.Job.strDetectedDeviceName = cJSON_GetObjectItem(pJob, "detectedDeviceName") ? cJSON_GetObjectItem(pJob, "detectedDeviceName")->valuestring : "";
											stTask.Job.strExecuteName = cJSON_GetObjectItem(pJob, "executeName") ? cJSON_GetObjectItem(pJob, "executeName")->valuestring : "";
											stTask.Job.strProjectName = cJSON_GetObjectItem(pJob, "projectName") ? cJSON_GetObjectItem(pJob, "projectName")->valuestring : "";
											stTask.Job.strProjectId = cJSON_GetObjectItem(pJob, "projectId") ? cJSON_GetObjectItem(pJob, "projectId")->valuestring : "";
											stTask.Job.strUniqueId = cJSON_GetObjectItem(pJob, "uniqueId") ? cJSON_GetObjectItem(pJob, "uniqueId")->valuestring : "";
											stTask.Job.strTaskId = cJSON_GetObjectItem(pJob, "taskId") ? cJSON_GetObjectItem(pJob, "taskId")->valuestring : "";
											stTask.Job.strSoftwareVersion = cJSON_GetObjectItem(pJob, "softwareVersion") ? cJSON_GetObjectItem(pJob, "softwareVersion")->valuestring : "";
											stTask.Job.strMonkeyPctNav = cJSON_GetObjectItem(pJob, "monkeyPctNav") ? cJSON_GetObjectItem(pJob, "monkeyPctNav")->valuestring : "";
											// emails,已重复,忽略;
											// cJSON *pEmals = cJSON_GetObjectItem(pJob, "emails");
											// case list;
											cJSON *pCaseList = cJSON_GetObjectItem(pJob, "caseList");
											if ( pCaseList )
											{
												int nCaseSize = cJSON_GetArraySize(pCaseList);
												for ( int ii = 0; ii < nCaseSize; ii++ )
												{
													cJSON *pCase = cJSON_GetArrayItem(pCaseList, ii);
													if ( pCase )
													{
														SATHTTP::STCase stCase;
														stCase.strIndex = cJSON_GetObjectItem(pCase, "index") ? cJSON_GetObjectItem(pCase, "index")->valuestring : "";
														stCase.strCaseName = cJSON_GetObjectItem(pCase, "caseName") ? cJSON_GetObjectItem(pCase, "caseName")->valuestring : "";
														stCase.strMainFilePath = cJSON_GetObjectItem(pCase, "mainFilePath") ? cJSON_GetObjectItem(pCase, "mainFilePath")->valuestring : "";
														stCase.strId = cJSON_GetObjectItem(pCase, "id") ? cJSON_GetObjectItem(pCase, "id")->valuestring : "";
														stCase.strCaseType = cJSON_GetObjectItem(pCase, "caseType") ? cJSON_GetObjectItem(pCase, "caseType")->valuestring : "";
														// 压入Job;
														stTask.Job.vtCases.push_back(stCase);
													}
												}
											}
										}
									}

									// 保存任务信息;
									stHeartbeatResp.vtTask.push_back(stTask);
								}
							}

							// 释放内存;
							cJSON_Delete(pRoot);

							// 返回结果;
							return TRUE;
						}
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}

SATHTTP_API int SetResult(std::string url, const SATHTTP::STNotifyJobStartReq &stNotifyJobStartReq, SATHTTP::STNotifyJobStartResp &stNotifyJobStartResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", ""));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", ""));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "instanceId", stNotifyJobStartReq.strInstanceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "signalImageUrl", stNotifyJobStartReq.strSignalImageUrl.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "deviceId", stNotifyJobStartReq.strDeviceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "taskId", stNotifyJobStartReq.strTaskId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "executeId", stNotifyJobStartReq.strExecuteId.c_str()));
				
		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(pText);
	//post_data.append(CharEncoding::EnCode_UTF8URL(pText));
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stNotifyJobStartResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stNotifyJobStartResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stNotifyJobStartResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					if ( stNotifyJobStartResp.strCode == "00" )
					{				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL)
						{
							
						}

						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}

SATHTTP_API int SetResultList(std::string url, const SATHTTP::STJobProcessReq &stJobProcessReq, SATHTTP::STJobProcessResp &stJobProcessResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", "Execute"));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", "setResultList"));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "result_state", stJobProcessReq.strResultState.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "case_scene", stJobProcessReq.strCaseScene.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "case_step", stJobProcessReq.strCaseStep.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "apkMd5", stJobProcessReq.strApkMD5.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "crash_time", stJobProcessReq.strCrashTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runner_id", stJobProcessReq.strRunnerId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "cpu_info", stJobProcessReq.strCPUInfo.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runnedActionNameList", stJobProcessReq.strRunnedActionNameList.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "artificial_result", stJobProcessReq.strArtificialResult.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "artificial_modify", stJobProcessReq.strArtificialModify.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runner_name", stJobProcessReq.strRunnerName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "task_type", stJobProcessReq.strTaskType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "case_repeat", stJobProcessReq.strCaseRepeat.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "application_group", stJobProcessReq.strApplicationGroup.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "instanceId", stJobProcessReq.strInstanceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "case_id", stJobProcessReq.strCaseId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "progress", stJobProcessReq.strProgress.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "result_message", stJobProcessReq.strReusltMessage.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "job_repeat", stJobProcessReq.strJobRepeat.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "screenshot", stJobProcessReq.strScreenShot.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "start_time", stJobProcessReq.strStartTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "crash_number", stJobProcessReq.strCrashNumber.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "case_name", stJobProcessReq.strCaseName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "failed_reason", stJobProcessReq.strFailedReason.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "img_name", stJobProcessReq.strImgName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "case_index", stJobProcessReq.strCaseIndex.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "device_id", stJobProcessReq.strDeviceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "scene_index", stJobProcessReq.strSceneIndex.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "task_id", stJobProcessReq.strTaskId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "analysis", stJobProcessReq.strAnalysis.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "device_name", stJobProcessReq.strDevnceName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "info_type", stJobProcessReq.strInfoType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "memory_info", stJobProcessReq.strMemoryInfo.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "end_time", stJobProcessReq.strEndTime.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "round_number", stJobProcessReq.strRoundNumber.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "result_type", stJobProcessReq.strResultType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "operation_step", stJobProcessReq.strOperationStep.c_str()));

		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_GB2312URL(pText));
	//post_data.append(pText);
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stJobProcessResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stJobProcessResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stJobProcessResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					if ( stJobProcessResp.strCode == "00" )
					{				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL)
						{
							stJobProcessResp.strIsDelete = cJSON_GetObjectItem(pObj, "isDelete") ? (cJSON_GetObjectItem(pObj, "isDelete")->valuestring ? cJSON_GetObjectItem(pObj, "isDelete")->valuestring : "") : "";
						}

						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}


SATHTTP_API int GetCaseFileListUrl(std::string url, std::string strCaseId, SATHTTP::STScriptUrlResp &stScriptUrlResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", "Execute"));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", "getCaseFileListUrl"));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "testCaseId", strCaseId.c_str()));
		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stScriptUrlResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stScriptUrlResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stScriptUrlResp.strProjectId = cJSON_GetObjectItem(pMsg, "strProjectId") ? (cJSON_GetObjectItem(pMsg, "strProjectId")->valuestring ? cJSON_GetObjectItem(pMsg, "strProjectId")->valuestring : "") : "";
					stScriptUrlResp.strCaseType = cJSON_GetObjectItem(pMsg, "caseType") ? (cJSON_GetObjectItem(pMsg, "caseType")->valuestring ? cJSON_GetObjectItem(pMsg, "caseType")->valuestring : "") : "";
					if ( stScriptUrlResp.strCode == "00" )
					{				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL)
						{
							if ( cJSON_GetArraySize(pObj) >= 1 )
							{
								cJSON *pScripURL = cJSON_GetArrayItem(pObj, 0);
								if ( pScripURL )
								{
									stScriptUrlResp.strURL = cJSON_GetObjectItem(pScripURL, "url") ? (cJSON_GetObjectItem(pScripURL, "url")->valuestring ? cJSON_GetObjectItem(pScripURL, "url")->valuestring : "") : "";
									// 同时下载脚本目录到指定目录中;

								}
							}
						}

						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}

SATHTTP_API int DownloadScript(std::string url, std::string strCaseId, std::string strSaveDir, SATHTTP::STScriptUrlResp &stScriptUrlResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", "Execute"));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", "getCaseFileListUrl"));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "testCaseId", strCaseId.c_str()));
		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_UTF8URL(pText));
#ifdef _DEBUG
	OutputDebugString((std::string(pText)+"\n").c_str());
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() ) {
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK ) {
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());

			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL) {
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg ) {
					stScriptUrlResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stScriptUrlResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stScriptUrlResp.strProjectId = cJSON_GetObjectItem(pMsg, "strProjectId") ? (cJSON_GetObjectItem(pMsg, "strProjectId")->valuestring ? cJSON_GetObjectItem(pMsg, "strProjectId")->valuestring : "") : "";
					stScriptUrlResp.strCaseType = cJSON_GetObjectItem(pMsg, "caseType") ? (cJSON_GetObjectItem(pMsg, "caseType")->valuestring ? cJSON_GetObjectItem(pMsg, "caseType")->valuestring : "") : "";
					if ( stScriptUrlResp.strCode == "00" ) {				
						cJSON *pObj = cJSON_GetObjectItem(pMsg, "data");
						if (pObj != NULL) {
							if ( cJSON_GetArraySize(pObj) >= 1 ) {
								cJSON *pScripURL = cJSON_GetArrayItem(pObj, 0);
								if ( pScripURL ) {
									stScriptUrlResp.strURL = cJSON_GetObjectItem(pScripURL, "url") ? (cJSON_GetObjectItem(pScripURL, "url")->valuestring ? cJSON_GetObjectItem(pScripURL, "url")->valuestring : "") : "";
									std::string strFileDir;
									std::string strFileName;
									int nPos1 = 0, nPos2 = 0;
									nPos1 = stScriptUrlResp.strURL.find("fileDir=");
									nPos2 = stScriptUrlResp.strURL.find("&fileName=");
									if ( nPos1 != std::string::npos && nPos2 != std::string::npos ) {
										strFileDir = stScriptUrlResp.strURL.substr(nPos1 + strlen("fileDir="), nPos2 - nPos1 - strlen("fileDir="));
										strFileName = stScriptUrlResp.strURL.substr(nPos2 + strlen("&fileName="));
										// 保存目录;
										stScriptUrlResp._strFileDir = strSaveDir;
										strSaveDir.append(strFileName);
										// 创建目录;
										if ( MKDIR(strSaveDir.c_str()) ) {
											// 同时下载脚本目录到指定目录中;
#ifdef _DEBUG
											OutputDebugString((stScriptUrlResp.strURL+"\n").c_str());
#endif
											// url地址可能包含中文,此处需要转码;
											stScriptUrlResp.strURL = CCurlClient::ParseURLAndEncode(stScriptUrlResp.strURL);
											if ( g_curl.Download(stScriptUrlResp.strURL, strSaveDir, 20000) ) {
												stScriptUrlResp._strFileName = strFileName.substr(0, strFileName.find_first_of('.'));
												stScriptUrlResp._strScripFile = strSaveDir;
												// 释放内存;
												cJSON_Delete(pRoot);
												// 返回结果;
												return TRUE;
											}
										}
									}							
								}
							}
						}						
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}


SATHTTP_API int StopTaskFromRunner(std::string url, std::string strInstnaceId, SATHTTP::STStopTaskResp &stStopTaskResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", ""));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", ""));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "instanceId", strInstnaceId.c_str()));
		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	std::string post_data = "requestMsg=";
	post_data.append(CharEncoding::EnCode_UTF8URL(pText));

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.Post(url, post_data, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stStopTaskResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stStopTaskResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stStopTaskResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					stStopTaskResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					if ( stStopTaskResp.strCode == "00" )
					{				
						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}

SATHTTP_API int SaveCaseOrTaskLog(std::string url, const SATHTTP::STSaveLogReq &stSaveLogReq, SATHTTP::STSaveLogResp &stSaveLogResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "useId", stSaveLogReq.strUserId.c_str()));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "fileType", stSaveLogReq.strFileType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseId", stSaveLogReq.strCaseId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "taskId", stSaveLogReq.strTaskId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "executeId", stSaveLogReq.strExecuteId.c_str()));

		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	//std::string post_data = CharEncoding::EnCode_UTF8URL(pText);
	// POST表单提交,此处不需要转url格式,注意避坑！
	std::string post_data = pText;
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.PostFile(url, post_data, stSaveLogReq.strUploads, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stSaveLogResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stSaveLogResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stSaveLogResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					stSaveLogResp.strData = cJSON_GetObjectItem(pMsg, "data") ? (cJSON_GetObjectItem(pMsg, "data")->valuestring ? cJSON_GetObjectItem(pMsg, "data")->valuestring : "") : "";
					if ( stSaveLogResp.strCode == "00" )
					{				
						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}


SATHTTP_API int SaveResultFile(std::string url, const SATHTTP::STSaveFileReq &stSaveFileReq, SATHTTP::STSaveFileResp &stSaveFileResp) 
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", ""));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", ""));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "roundNum", stSaveFileReq.strRoundNum.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseRepeat", stSaveFileReq.strCaseRepeat.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "jobRepeat", stSaveFileReq.strJobRepeat.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "instanceId", stSaveFileReq.strInstanceId.c_str()));

		ASSERT(cJSON_AddStringToObject(pData, "deviceId", stSaveFileReq.strDeviceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "executeId", stSaveFileReq.strExecuteId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "taskName", stSaveFileReq.strTaskName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "sceneIndex", stSaveFileReq.strSceneIndex.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "taskId", stSaveFileReq.strTaskId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseId", stSaveFileReq.strCaseId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "deviceName", stSaveFileReq.strDeviceName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runnerName", stSaveFileReq.strRunnerName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runnerIP", stSaveFileReq.strRunnerIP.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "runnerMac", stSaveFileReq.strRunnerMAC.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseName", stSaveFileReq.strCaseName.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseIndex", stSaveFileReq.strCaseIndex.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "taskRepeat", stSaveFileReq.strTaskRepeat.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "busCode", stSaveFileReq.strBusCode.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "fileType", stSaveFileReq.strFileType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseStep", stSaveFileReq.strCaseStep.c_str()));

		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	//std::string post_data = CharEncoding::EnCode_UTF8URL(pText);
	// POST表单提交,此处不需要转url格式,注意避坑！
	std::string post_data = pText;
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.PostFile(url, post_data, stSaveFileReq.strUploads, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stSaveFileResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stSaveFileResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stSaveFileResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					stSaveFileResp.strData = cJSON_GetObjectItem(pMsg, "data") ? (cJSON_GetObjectItem(pMsg, "data")->valuestring ? cJSON_GetObjectItem(pMsg, "data")->valuestring : "") : "";
					if ( stSaveFileResp.strCode == "00" )
					{				
						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}

SATHTTP_API int SaveResultImg(std::string url, const SATHTTP::STSaveImgReq &stSaveImgReq, SATHTTP::STSaveImgResp &stSaveImgResp)
{
	// 转化为Json;
	cJSON *pRoot = cJSON_CreateObject();
	if ( pRoot == NULL )
	{
		return -1;
	}

	ASSERT(cJSON_AddStringToObject(pRoot, "sysCode", ""));
	ASSERT(cJSON_AddStringToObject(pRoot, "function", ""));
	cJSON *pData = cJSON_CreateObject();
	if ( pData )
	{
		ASSERT(cJSON_AddStringToObject(pData, "instanceId", stSaveImgReq.strInstanceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "deviceId", stSaveImgReq.strDeviceId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "roundNum", stSaveImgReq.strRoundNum.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseId", stSaveImgReq.strCaseId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "step", stSaveImgReq.strCaseStep.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "type", stSaveImgReq.strTaskType.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "executeId", stSaveImgReq.strExecuteId.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "caseRepeat", stSaveImgReq.strCaseRepeat.c_str()));
		ASSERT(cJSON_AddStringToObject(pData, "jobRepeat", stSaveImgReq.strJobRepeat.c_str()));
		// 添加到根结点中;
		cJSON_AddItemToObject(pRoot, "data", pData);
	}

	char *pText = cJSON_Print(pRoot);
	//std::string post_data = CharEncoding::EnCode_UTF8URL(pText);
	// POST表单提交,此处不需要转url格式,注意避坑！
	std::string post_data = pText;
#ifdef _DEBUG
	//OutputDebugString(CString(pText)+"\n");
#endif

	// 释放堆内存;
	if (pText)
		delete pText;
	pText = NULL;

	if ( pRoot )
		cJSON_Delete(pRoot);
	pRoot = NULL;		

	std::string reponse;	
	CURLcode curlCode = CURLE_OK;
	if ( g_curl.IsInit() )
	{
		if (_tcsstr(url.c_str(), "https://") || _tcsstr(url.c_str(), "HTTPS://") )
			curlCode = g_curl.Posts(url, post_data, reponse);
		else
			curlCode = g_curl.PostFile(url, post_data, stSaveImgReq.strUploads, reponse);

		if ( curlCode == CURLE_OK )
		{
			reponse = CharEncoding::DeCode_URLUTF8(reponse.c_str());
#ifdef _DEBUG
			//OutputDebugString(CString(reponse.c_str())+"\n");
#endif
			// 解析返回值;
			pRoot = cJSON_Parse(reponse.c_str());
			if (pRoot != NULL)
			{
				cJSON *pMsg = cJSON_GetObjectItem(pRoot, "responseMsg");
				if ( pMsg )
				{
					stSaveImgResp.strCode = cJSON_GetObjectItem(pMsg, "code") ? cJSON_GetObjectItem(pMsg, "code")->valuestring : "";
					stSaveImgResp.strMessage = cJSON_GetObjectItem(pMsg, "message") ? cJSON_GetObjectItem(pMsg, "message")->valuestring : "";
					stSaveImgResp.strStr = cJSON_GetObjectItem(pMsg, "str") ? (cJSON_GetObjectItem(pMsg, "str")->valuestring ? cJSON_GetObjectItem(pMsg, "str")->valuestring : "") : "";
					stSaveImgResp.strData = cJSON_GetObjectItem(pMsg, "data") ? (cJSON_GetObjectItem(pMsg, "data")->valuestring ? cJSON_GetObjectItem(pMsg, "data")->valuestring : "") : "";
					if ( stSaveImgResp.strCode == "00" )
					{				
						// 释放内存;
						cJSON_Delete(pRoot);
						// 返回结果;
						return TRUE;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pRoot);	
			}
		}
	}

	return FALSE;
}
