test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "stdafx.h"
  2. #include <direct.h>
  3. bool MKDIR(LPCTSTR dir)
  4. {
  5. //////////////////////////////////////////////////////////////////////////
  6. // 创建目录;
  7. int nleft = 0;
  8. int nIndex = -1;
  9. std::string strdir = dir;
  10. strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));
  11. if (strdir.at(strdir.size() - 1) != _T('\\'))
  12. strdir.append(_T("\\"));
  13. // 共享路径和硬盘盘符;
  14. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  15. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  16. else if (strdir.at(2) == _T('\\'))
  17. nleft = 3;
  18. do
  19. {
  20. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  21. if (nIndex != std::string::npos)
  22. {
  23. if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  24. {
  25. return false;
  26. }
  27. nleft += nIndex + 1;
  28. }
  29. } while (nIndex != -1);
  30. return true;
  31. };
  32. __if_not_exists(NotifyJobStart)
  33. {
  34. int NotifyJobStart(std::string url, const SATParameters::STNotifyJobStartReq &stNotifyJobStartReq, SATParameters::STNotifyJobStartResp &stNotifyJobStartResp) throw()
  35. {
  36. return SetResult(url, stNotifyJobStartReq, stNotifyJobStartResp);
  37. }
  38. }
  39. __if_not_exists(ProcessJob)
  40. {
  41. int ProcessJob(std::string url, const SATParameters::STJobProcessReq &stJobProcessReq, SATParameters::STJobProcessResp &stJobProcessResp) throw()
  42. {
  43. return SetResultList(url, stJobProcessReq, stJobProcessResp);
  44. }
  45. }
  46. int test()
  47. {
  48. //std::string host = "http://10.118.158.52:8580";//"http://10.118.158.198:8090";
  49. std::string host = "http://10.126.16.60:8580";
  50. SATParameters::STDevice stDevice;
  51. stDevice.strStatus = "0";
  52. stDevice.strDeviceSerial = "192.168.1.119:5555";
  53. stDevice.strHardwareVersion = "MS6488";
  54. stDevice.strManu = "MStar";
  55. stDevice.strDeviceMac = "";
  56. stDevice.strLastJobStartTime = "";
  57. stDevice.strLastTimeBreak = "";
  58. stDevice.strComments = "";
  59. stDevice.strCPU = "";
  60. stDevice.strSoftwareVersion = "1522659174";
  61. stDevice.strPhoneNumber = "";
  62. stDevice.strLastJobFinishTime = "";
  63. stDevice.strMemory = "";
  64. stDevice.strModel = "Generic Linux on tcl_ms6488_cn_64_n";
  65. stDevice.strLastJob = "";
  66. stDevice.strLastTimeConnected = "2019-12-16 10:16:01";
  67. stDevice.strElectric = "";
  68. // ##########################先登录##############################;
  69. SATParameters::STLoginReq stLoginReq;
  70. SATParameters::STLoginResp stLoginResp;
  71. std::string url = host + "/btc_execute_se/ajaxInteractiveManage!executeLogin.action";
  72. // 示例值;
  73. stLoginReq.strUserName = "superAdmin";
  74. stLoginReq.strStatus = "0";
  75. stLoginReq.strDeleteStatus = "";
  76. stLoginReq.strIP = "10.118.158.175";
  77. stLoginReq.strStorage = "";
  78. stLoginReq.strConnectTime = "";
  79. stLoginReq.strExecuteName = "";
  80. stLoginReq.strMAC = "40:16:7e:23:10:53";
  81. stLoginReq.strDisconnectTime = "";
  82. stLoginReq.strPassword = "123456";
  83. stLoginReq.strCPU = "";
  84. if ( !Login(url, stLoginReq, stLoginResp) )
  85. {
  86. system("pause");
  87. return -1;
  88. }
  89. printf("登录返回:code=%s, msg=%s, str=%s, portCount=%d, companyid=%d, runnertoken=%s\n",
  90. stLoginResp.strCode.c_str(),
  91. stLoginResp.strMessage.c_str(),
  92. stLoginResp.strStr.c_str(),
  93. stLoginResp.nPortCount,
  94. stLoginResp.nCompanyId,
  95. stLoginResp.strRunnerToken.c_str()
  96. );
  97. // ##########################更新设备信息##############################;
  98. SATParameters::STUpdateDeviceReq stUpdateDeviceReq;
  99. SATParameters::STUpdateDeviceResp stUpdateDeviceResp;
  100. __if_not_exists(url)
  101. {
  102. std::string url = host + "/btc_execute_se/ajaxInteractiveManage!updateDeviceMessage.action";
  103. }
  104. __if_exists(url)
  105. {
  106. url = host + "/btc_execute_se/ajaxInteractiveManage!updateDeviceMessage.action";
  107. }
  108. stUpdateDeviceReq.strStatus = "0";
  109. stUpdateDeviceReq.strUserName = "superAdmin";
  110. stUpdateDeviceReq.strIP = "10.118.158.175";
  111. stUpdateDeviceReq.strStorage = "228092536KB";
  112. stUpdateDeviceReq.strRunnerName = "SAT-Admin";
  113. stUpdateDeviceReq.strMAC = "40:16:7e:23:10:53";
  114. stUpdateDeviceReq.strReportType = "1";
  115. stUpdateDeviceReq.strStartTime = "2019-12-16 19:15:30";
  116. stUpdateDeviceReq.strMemory = "8938544KB";
  117. stUpdateDeviceReq.strEndTime = "";
  118. stUpdateDeviceReq.strCPU = "24.7%";
  119. stUpdateDeviceReq.devicelist.push_back(stDevice);
  120. if ( UpdateDeviceMessage(url, stUpdateDeviceReq, stUpdateDeviceResp) )
  121. {
  122. printf("更新设备信息成功:code=%s, msg=%s, str=%s\n",
  123. stLoginResp.strCode.c_str(),
  124. stLoginResp.strMessage.c_str(),
  125. stLoginResp.strStr.c_str());
  126. }
  127. else
  128. {
  129. printf("更新设备信息失败!\n");
  130. }
  131. //system("pause");
  132. //return 1;
  133. while(true)
  134. {
  135. // ##########################心跳包机制##############################;
  136. SATParameters::STHeartbeatReq stHeartbeatReq;
  137. SATParameters::STHeartbeatResp stHeartbeatResp;
  138. __if_not_exists(url)
  139. {
  140. std::string url = host + "/btc_execute_se/ajaxInteractiveManage!executeHeartbeat.action";
  141. }
  142. __if_exists(url)
  143. {
  144. url = host + "/btc_execute_se/ajaxInteractiveManage!executeHeartbeat.action";
  145. }
  146. // 示例值;
  147. //stDevice.strStatus = "0";
  148. //stDevice.strDeviceSerial = "192.168.1.119:5555";
  149. stHeartbeatReq.strUserName = "superAdmin";
  150. stHeartbeatReq.strRunnerMac = "40:16:7e:23:10:53";
  151. stHeartbeatReq.devicelist.push_back(stDevice);
  152. if ( Heartbeat(url, stHeartbeatReq, stHeartbeatResp) )
  153. {
  154. printf("登录返回:code=%s, msg=%s, str=%s\n",
  155. stHeartbeatResp.strCode.c_str(),
  156. stHeartbeatResp.strMessage.c_str(),
  157. stHeartbeatResp.strStr.c_str());
  158. // 判断是否有任务获取成功;
  159. if ( stHeartbeatResp.vtTask.size() )
  160. {
  161. // #########################################################################################
  162. // 通知SAT变更任务状态;
  163. SATParameters::STTask stTask = stHeartbeatResp.vtTask[0];
  164. SATParameters::STNotifyJobStartReq stNotifyJobStartReq;
  165. SATParameters::STNotifyJobStartResp stNotifyJobStartResp;
  166. __if_not_exists(url)
  167. {
  168. std::string url = host + "/btc_execute_se/ajaxInteractiveManage!setResult.action";
  169. }
  170. __if_exists(url)
  171. {
  172. url = host + "/btc_execute_se/ajaxInteractiveManage!setResult.action";
  173. }
  174. TCHAR szValue[36] = {0};
  175. _itoa_s(stTask.nDeviceId, szValue, 10);
  176. stNotifyJobStartReq.strDeviceId = szValue;
  177. _itoa_s(stTask.nExecuteId, szValue, 10);
  178. stNotifyJobStartReq.strExecuteId = szValue;
  179. // _itoa_s(stTask.nInstanceId, szValue, 10); // 误导:应该使用id而不是nInstanceId
  180. _itoa_s(stTask.Id, szValue, 10);
  181. stNotifyJobStartReq.strInstanceId = szValue;
  182. _itoa_s(stTask.nTaskId, szValue, 10);
  183. stNotifyJobStartReq.strTaskId = szValue;
  184. stNotifyJobStartReq.strSignalImageUrl = "D:\\\\SAT\\\\Runner\\\\btc_runner_se\\\\runner\\\\output\\\\";//ODF_NPI_RT2841\\\\20191216101613370\\\\192.168.1.119_5555";
  185. stNotifyJobStartReq.strSignalImageUrl.append(stTask.strTaskName+"\\\\");
  186. stNotifyJobStartReq.strSignalImageUrl.append(stTask.Job.strUniqueId+"\\\\");
  187. stNotifyJobStartReq.strSignalImageUrl.append(stTask.Job.strDeviceId);
  188. for( int n = 0; n < 2; n++)
  189. {
  190. // 查询任务是否已在SAT上存在;
  191. if ( NotifyJobStart(url, stNotifyJobStartReq, stNotifyJobStartResp) )
  192. {
  193. // 更新设备为忙碌状态;
  194. // 忙碌状态,此时请求Heartbeat将不会查询任务;
  195. stDevice.strStatus = "2";
  196. printf("NotifyJobStart 成功\n");
  197. }
  198. else
  199. {
  200. printf("NotifyJobStart 失败\n");
  201. }
  202. }
  203. stDevice.strStatus = "0";
  204. // #########################################################################################
  205. // 开始Job;
  206. SATParameters::STJobProcessReq stJobProcessReq;
  207. SATParameters::STJobProcessResp stJobProcessResp;
  208. __if_not_exists(url)
  209. {
  210. std::string url = host + "/btc_execute_se/ajaxInteractiveManage!setResultList.action";
  211. }
  212. __if_exists(url)
  213. {
  214. url = host + "/btc_execute_se/ajaxInteractiveManage!setResultList.action";
  215. }
  216. {
  217. stJobProcessReq.strResultState = "1";
  218. stJobProcessReq.strCaseScene = "";
  219. stJobProcessReq.strCaseStep = "0";
  220. stJobProcessReq.strApkMD5 = "";
  221. stJobProcessReq.strCrashTime = "";
  222. // 就是Task中的ExecuteId
  223. _itoa_s(stTask.nExecuteId, szValue, 10);
  224. stJobProcessReq.strRunnerId = szValue;
  225. stJobProcessReq.strCPUInfo = "0";
  226. stJobProcessReq.strRunnedActionNameList = "";
  227. stJobProcessReq.strArtificialResult = "";
  228. stJobProcessReq.strArtificialModify = "";
  229. stJobProcessReq.strRunnerName = "";
  230. stJobProcessReq.strTaskType = "FUNCTIONALITY";
  231. stJobProcessReq.strCaseRepeat = "";
  232. stJobProcessReq.strApplicationGroup = "";
  233. // 实例Id;
  234. _itoa_s(stTask.nInstanceId, szValue, 10);
  235. stJobProcessReq.strInstanceId = szValue;
  236. stJobProcessReq.strCaseId = "";
  237. stJobProcessReq.strProgress = "0";
  238. stJobProcessReq.strReusltMessage = "任务开始";
  239. stJobProcessReq.strJobRepeat = "";
  240. stJobProcessReq.strScreenShot = "";
  241. stJobProcessReq.strStartTime = "2019-12-16 10:16:43";
  242. stJobProcessReq.strCrashNumber = "";
  243. stJobProcessReq.strCaseName = "";
  244. stJobProcessReq.strFailedReason = "";
  245. stJobProcessReq.strImgName = "";
  246. stJobProcessReq.strCaseIndex = "";
  247. // 实例Id;
  248. _itoa_s(stTask.nDeviceId, szValue, 10);
  249. stJobProcessReq.strDeviceId = szValue;
  250. stJobProcessReq.strSceneIndex = "";
  251. // 实例Id;
  252. _itoa_s(stTask.nTaskId, szValue, 10);
  253. stJobProcessReq.strTaskId = szValue;
  254. stJobProcessReq.strAnalysis = "";
  255. // 设备名称:即DeviceSerial;
  256. stJobProcessReq.strDevnceName = stDevice.strDeviceSerial;
  257. // 固定为:TOTAL
  258. stJobProcessReq.strInfoType = "TOTAL";
  259. // 如果是Android设备,需要通过adb获取;
  260. stJobProcessReq.strMemoryInfo = stDevice.strMemory;
  261. stJobProcessReq.strEndTime = "2019-12-16 10:18:20";
  262. stJobProcessReq.strRoundNumber = "1";
  263. stJobProcessReq.strResultType = "0";
  264. stJobProcessReq.strOperationStep = "";
  265. }
  266. //
  267. if ( ProcessJob(url, stJobProcessReq, stJobProcessResp) )
  268. {
  269. // 开始下载脚本;
  270. __if_not_exists(url)
  271. {
  272. std::string url = host + "/btc_caseManagement_se/ajaxInteractiveManage!getCaseFileListUrl.action";
  273. }
  274. __if_exists(url)
  275. {
  276. url = host + "/btc_caseManagement_se/ajaxInteractiveManage!getCaseFileListUrl.action";
  277. }
  278. SATParameters::STScriptUrlResp stScriptUrlResp;
  279. std::vector<SATParameters::STCase>::iterator it = stTask.Job.vtCases.begin();
  280. for ( ; it != stTask.Job.vtCases.end(); it++ )
  281. {
  282. if ( DownloadScript(url, it->strId, "D:\\SAT\\", stScriptUrlResp) )
  283. {
  284. printf("获取脚本下载地址成功:%s\n路径:%s\n", stScriptUrlResp.strURL.c_str(), stScriptUrlResp._strScripFile.c_str());
  285. DWORD dwScriptId = RunPyScript(stScriptUrlResp._strScripFile.c_str(), "", "D:\\SAT\\log.txt", TRUE);
  286. if ( dwScriptId != -1 )
  287. {
  288. ULONGLONG ulTickCount = GetTickCount64();
  289. while( !IsPyScriptOver(dwScriptId, TRUE) )
  290. {
  291. Sleep(1000);
  292. //if ( GetTickCount64() - ulTickCount > 1800000)
  293. if ( GetTickCount64() - ulTickCount > 1200000)
  294. {
  295. break;
  296. }
  297. }
  298. }
  299. CleanPyScript(dwScriptId);
  300. }
  301. else
  302. {
  303. printf("获取脚本下载地址失败\n");
  304. }
  305. }
  306. }
  307. }
  308. }
  309. Sleep(30000);
  310. }
  311. system("pause");
  312. }