SATService.cpp 11 KB

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