SATExecutor.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. #include "StdAfx.h"
  2. #include "SATExecutor.h"
  3. #include "PythonExecutor.h"
  4. #include "CharEncoding.h"
  5. #include "SynSerial.h"
  6. #include "IRControl.h"
  7. CSATExecutor::CSATExecutor(void)
  8. {
  9. m_bLogin = FALSE;
  10. m_bStopWathTV = FALSE;
  11. m_hEventHearbeat = NULL;
  12. m_hThreadHearbeat = NULL;
  13. m_hEventExcuteScript = NULL;
  14. m_hThreadExcuteScript = NULL;
  15. }
  16. CSATExecutor::~CSATExecutor(void)
  17. {
  18. m_bStopWathTV = FALSE;
  19. EndofWork();
  20. }
  21. bool CSATExecutor::IsTaskExist(SATHTTP::STTask &task)
  22. {
  23. bool found = false;
  24. std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
  25. for ( ; it != m_vtTask.end(); it++ ) {
  26. // 不要使用nTaskId,这个值没啥用;//不要使用Id这个值,如果任务被停止会变更;
  27. if ( it->nInstanceId == task.nInstanceId ) {
  28. // 如果被中止,并变更状态为已执行;
  29. if ( _tcsicmp(task.strTaskType.c_str(), "3") == 0 ) {
  30. it->_nExecutionState = SATHTTP::EXECUTED;
  31. it->_nExecutionResult = SATHTTP::FAIL;
  32. }
  33. found = true;
  34. break;
  35. }
  36. }
  37. return found;
  38. }
  39. SATHTTP::STTask* CSATExecutor::IsThereATaskInProcess()
  40. {
  41. std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
  42. for ( ; it != m_vtTask.end(); it++ ) {
  43. // 执行中的任务;
  44. if ( it->_nExecutionState == SATHTTP::INEXECUTED ) {
  45. return &(*it);
  46. }
  47. }
  48. return NULL;
  49. }
  50. SATHTTP::STTask* CSATExecutor::GetFreeTask()
  51. {
  52. std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
  53. for ( ; it != m_vtTask.end(); it++ ) {
  54. // 未执行的任务;
  55. if ( it->_nExecutionState == SATHTTP::UNEXECUTED ) {
  56. return &(*it);
  57. }
  58. }
  59. return NULL;
  60. }
  61. void CSATExecutor::DelFinishedTask()
  62. {
  63. AutoThreadSection ats(&m_csTask);
  64. std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
  65. for ( ; it != m_vtTask.end(); it++ ) {
  66. // 状态为2的完成任务;
  67. if ( it->_nExecutionState == SATHTTP::EXECUTED ) {
  68. // 删除所有执行器对象;
  69. CPythonExecutor *pExecutor = NULL;
  70. for (std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin(); _case != it->Job.vtCases.end(); _case++ ) {
  71. pExecutor = (CPythonExecutor *)_case->_pExcutor;
  72. if ( pExecutor ) {
  73. // 结束所有工作线程;
  74. pExecutor->EndThread();
  75. // 释放内存;
  76. delete pExecutor;
  77. pExecutor = NULL;
  78. }
  79. }
  80. // 删除任务前,变更设备状态为空闲;
  81. SetDeviceStatus(it->Job.strDeviceId, SATDEV::Idle);
  82. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "\n<===============================================>\n"
  83. "end.删除已完成任务:%s\n<===============================================>\n", it->Job.strUniqueId.c_str());
  84. // 删除任务;
  85. if ( m_vtTask.size() > 1 )
  86. m_vtTask.erase(it);
  87. break;
  88. }
  89. }
  90. }
  91. SATHTTP::STCase* CSATExecutor::IsCaseScriptProcess(std::vector<SATHTTP::STCase> &vtCases)
  92. {
  93. std::vector<SATHTTP::STCase>::iterator it = vtCases.begin();
  94. for ( ; it != vtCases.end(); it++) {
  95. // 执行中的脚本;
  96. if ( it->_nExecutionState == SATHTTP::INEXECUTED ) {
  97. return &(*it);
  98. }
  99. }
  100. return NULL;
  101. }
  102. SATHTTP::STCase* CSATExecutor::GetFreeCaseScript(std::vector<SATHTTP::STCase> &vtCases)
  103. {
  104. std::vector<SATHTTP::STCase>::iterator it = vtCases.begin();
  105. for ( ; it != vtCases.end(); it++) {
  106. // 未执行的脚本;
  107. if ( it->_nExecutionState == SATHTTP::UNEXECUTED ) {
  108. return &(*it);
  109. }
  110. }
  111. return NULL;
  112. }
  113. SATHTTP::STCase* CSATExecutor::ExecuteFreeCaseScript(SATHTTP::STTask* pTask)
  114. {
  115. // 获取未执行的脚本;
  116. SATHTTP::STCase* pCase = GetFreeCaseScript(pTask->Job.vtCases);
  117. if (pCase) {
  118. #if 1 // 额外追加代码:清除之前的xml文件;
  119. #if 0
  120. std::string xmlpath = GLOBAL::g_stSATConfig.szCaseResultDir;
  121. #else
  122. std::string xmlpath = pCase->_strFileDir;
  123. #endif
  124. xmlpath.append("detail\\");
  125. xmlpath.append(pCase->_strFileName);
  126. xmlpath.append("_result.xml");
  127. // 删除文件;
  128. DeleteFile(xmlpath.c_str());
  129. #endif
  130. if ( !pCase->_pExcutor ) {
  131. CPythonExecutor *pExcutor = new CPythonExecutor();
  132. if ( pExcutor ) {
  133. pCase->_pExcutor = pExcutor;
  134. // 用例的日志文件路径;
  135. pCase->_strCaseLog = pCase->_strFileDir + pCase->_strFileName + ".txt";
  136. if ( pExcutor->InitScript(pCase->_strScriptPath, pCase->_strCaseLog, "") ) {
  137. // 设置用例对象;
  138. pExcutor->SetCaseObje(pCase);
  139. pExcutor->StartScript();
  140. }
  141. // 标记用例执行中;
  142. pCase->_nExecutionState = SATHTTP::INEXECUTED;
  143. // 标记任务为执行中;
  144. pTask->_nExecutionState = SATHTTP::INEXECUTED;
  145. // 记录开始时间;
  146. pCase->_ulStartTickCount = GetTickCount64();
  147. pCase->_strStartTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
  148. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "启动脚本(%s)", pCase->strCaseName.c_str());
  149. }
  150. }
  151. else {
  152. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "重新初始化脚本(%s)", pCase->strCaseName.c_str());
  153. // 重新初始化脚本;
  154. CPythonExecutor *pExcutor = (CPythonExecutor *)pCase->_pExcutor;
  155. if ( pExcutor->InitScript(pCase->_strScriptPath, pCase->_strCaseLog, "") ) {
  156. // 设置用例对象;
  157. pExcutor->SetCaseObje(pCase);
  158. pExcutor->StartScript();
  159. // 标记用例执行中;
  160. pCase->_nExecutionState = SATHTTP::INEXECUTED;
  161. }
  162. else { // 如果再次初始化失败,标记脚本失败;
  163. pCase->_nExecutionResult = SATHTTP::FAIL;
  164. }
  165. }
  166. }
  167. return pCase;
  168. }
  169. void CSATExecutor::AddDevices(const SATDEV::STDevice &stDevice)
  170. {
  171. if ( IsDeviceExist(stDevice.strName) )
  172. return;
  173. SATHTTP::STDevice http_dev;
  174. // 设备使用状态;
  175. if ( stDevice.nUsageState == SATDEV::Idle )
  176. http_dev.strStatus = "0";
  177. else if ( stDevice.nUsageState == SATDEV::InUse )
  178. http_dev.strStatus = "2"; // 注意避坑:0表示连接,即设备空闲;1表示设备断开;2表示设备繁忙
  179. // 设备序列号;
  180. http_dev.strDeviceSerial = stDevice.strName;
  181. // 余下变量固定值;
  182. http_dev.strComments = "";
  183. http_dev.strCPU = "";
  184. http_dev.strDeviceMac = "";
  185. http_dev.strElectric = "";
  186. http_dev.strHardwareVersion = "SAT-HV";
  187. http_dev.strLastJob = "";
  188. http_dev.strLastJobFinishTime = "";
  189. http_dev.strLastJobStartTime = "";
  190. http_dev.strLastTimeBreak = "";
  191. http_dev.strLastTimeConnected = "";
  192. http_dev.strManu = "";
  193. http_dev.strMemory = "";
  194. http_dev.strModel = "SATModel";
  195. http_dev.strPhoneNumber = "";
  196. http_dev.strSoftwareVersion = "0123456789";
  197. m_vtDevice.push_back(http_dev);
  198. }
  199. void CSATExecutor::DelDevices(const SATDEV::STDevice &stDevice)
  200. {
  201. std::vector<SATHTTP::STDevice>::iterator it = m_vtDevice.begin();
  202. for ( ; it != m_vtDevice.end(); it++ ) {
  203. if ( _tcsicmp(it->strDeviceSerial.c_str(), stDevice.strName.c_str()) == 0 ) {
  204. m_vtDevice.erase(it);
  205. break;
  206. }
  207. }
  208. }
  209. void CSATExecutor::SetDeviceStatus(std::string strDevName, SATDEV::DEVICE_USAGE_STATUS status)
  210. {
  211. std::vector<SATHTTP::STDevice>::iterator it = m_vtDevice.begin();
  212. for ( ; it != m_vtDevice.end(); it++ ) {
  213. if ( _tcsicmp(it->strDeviceSerial.c_str(), strDevName.c_str()) == 0 ) {
  214. if ( status == SATDEV::Idle )
  215. it->strStatus = "0";
  216. else if ( status == SATDEV::InUse )
  217. it->strStatus = "2"; // 注意避坑:0表示连接,即设备空闲;1表示设备断开;2表示设备繁忙
  218. break;
  219. }
  220. }
  221. // 同时,同步SATDevices的设备状态;
  222. CSATDevices::SetDeviceUsageStatus(strDevName, status);
  223. }
  224. bool CSATExecutor::IsDeviceExist(std::string strDevName)
  225. {
  226. bool bExist = false;
  227. std::vector<SATHTTP::STDevice>::iterator it = m_vtDevice.begin();
  228. for ( ; it != m_vtDevice.end(); it++ ) {
  229. if ( _tcsicmp(it->strDeviceSerial.c_str(), strDevName.c_str()) == 0 ) {
  230. bExist = true;
  231. break;
  232. }
  233. }
  234. return bExist;
  235. }
  236. bool CSATExecutor::Login(std::string user, std::string password, std::string actuator, bool bLogin /*= true*/)
  237. {
  238. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  239. url.append("/ajaxInteractiveManage!executeLogin.action");
  240. // 填充数据;
  241. m_stLoginReq.strUserName = user;
  242. m_stLoginReq.strPassword = password;
  243. m_stLoginReq.strStatus = bLogin ? "0" : "1"; // 0表示登录;
  244. m_stLoginReq.strDeleteStatus = "";
  245. m_stLoginReq.strIP = GLOBAL::g_strIPAddress;
  246. m_stLoginReq.strStorage = "";
  247. if ( bLogin ) {
  248. m_stLoginReq.strDisconnectTime = "";
  249. m_stLoginReq.strConnectTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
  250. } else {
  251. m_stLoginReq.strConnectTime = "";
  252. m_stLoginReq.strDisconnectTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
  253. }
  254. // 执行器名称;
  255. m_stLoginReq.strExecuteName = m_strActuatorName = actuator;
  256. // 执行器PC物理地址;
  257. m_stLoginReq.strMAC = GLOBAL::g_strMacs;
  258. m_stLoginReq.strCPU = "";
  259. if ( ::Login(url, m_stLoginReq, m_stLoginResp) ) {
  260. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "登录成功");
  261. m_bLogin = TRUE;
  262. return true;
  263. }
  264. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "登录失败");
  265. return false;
  266. }
  267. bool CSATExecutor::Logout(std::string user, std::string password)
  268. {
  269. return Login(user, password, false);
  270. }
  271. bool CSATExecutor::UpdateDevice()
  272. {
  273. SATHTTP::STUpdateDeviceReq stUpdateDeviceReq;
  274. SATHTTP::STUpdateDeviceResp stUpdateDeviceResp;
  275. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  276. url.append("/ajaxInteractiveManage!updateDeviceMessage.action");
  277. // 这个状态值要怎么设置?// 此处统一为0;
  278. stUpdateDeviceReq.strStatus = "0";
  279. stUpdateDeviceReq.strUserName = m_stLoginReq.strUserName;
  280. stUpdateDeviceReq.strIP = GLOBAL::g_strIPAddress;
  281. stUpdateDeviceReq.strStorage = "";
  282. stUpdateDeviceReq.strRunnerName = m_strActuatorName;
  283. // 这里使用PC物理地址;
  284. stUpdateDeviceReq.strMAC = GLOBAL::g_strMacs;
  285. // 这个reportType具体意义不明;
  286. stUpdateDeviceReq.strReportType = "1";
  287. stUpdateDeviceReq.strStartTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
  288. stUpdateDeviceReq.strMemory = "";
  289. stUpdateDeviceReq.strEndTime = "";
  290. stUpdateDeviceReq.strCPU = "";
  291. stUpdateDeviceReq.devicelist.assign(m_vtDevice.begin(), m_vtDevice.end());
  292. if ( !::UpdateDeviceMessage(url, stUpdateDeviceReq, stUpdateDeviceResp) )
  293. {
  294. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "更新设备信息失败");
  295. return false;
  296. }
  297. return true;
  298. }
  299. bool CSATExecutor::NotifyTaskStart(SATHTTP::STTask* pTask)
  300. {
  301. if ( !pTask )
  302. return false;
  303. SATHTTP::STNotifyJobStartReq stNotifyJobStartReq;
  304. SATHTTP::STNotifyJobStartResp stNotifyJobStartResp;
  305. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  306. url.append("/ajaxInteractiveManage!setResult.action");
  307. TCHAR szValue[36] = {0};
  308. _itoa_s(pTask->nDeviceId, szValue, 10);
  309. stNotifyJobStartReq.strDeviceId = szValue;
  310. _itoa_s(pTask->nExecuteId, szValue, 10);
  311. stNotifyJobStartReq.strExecuteId = szValue;
  312. // _itoa_s(stTask.nInstanceId, szValue, 10); // 误导:应该使用id而不是nInstanceId
  313. _itoa_s(pTask->Id, szValue, 10);
  314. stNotifyJobStartReq.strInstanceId = szValue;
  315. //_itoa_s(pTask->nTaskId, szValue, 10);
  316. stNotifyJobStartReq.strTaskId = pTask->Job.strTaskId;
  317. // 这个映像路径有何用?
  318. stNotifyJobStartReq.strSignalImageUrl = GLOBAL::g_stSATConfig.szScriptDir;
  319. stNotifyJobStartReq.strSignalImageUrl.append(pTask->strTaskName+"\\\\");
  320. stNotifyJobStartReq.strSignalImageUrl.append(pTask->Job.strUniqueId+"\\\\");
  321. stNotifyJobStartReq.strSignalImageUrl.append(pTask->Job.strDeviceId);
  322. stNotifyJobStartReq.strSignalImageUrl = CharEncoding::ASCII2UTF8(stNotifyJobStartReq.strSignalImageUrl.c_str());
  323. if ( SetResult(url, stNotifyJobStartReq, stNotifyJobStartResp) ) {
  324. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "更新任务状态成功:%s", pTask->Job.strUniqueId.c_str());
  325. return true;
  326. }
  327. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "更新任务状态失败:%s", pTask->Job.strUniqueId.c_str());
  328. return false;
  329. }
  330. bool CSATExecutor::UploadCaseImg(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase, std::string img)
  331. {
  332. TCHAR szValue[MAX_PATH] = {0};
  333. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  334. url.append("/ajaxInteractiveManage!saveResultImg.action");
  335. SATHTTP::STSaveImgReq stSaveImgReq;
  336. SATHTTP::STSaveImgResp stSaveImgResp;
  337. stSaveImgReq.strCaseId = pCase->strId;
  338. stSaveImgReq.strCaseRepeat = "0";
  339. _itoa_s(pCase->_nCaseStep, szValue, 10);
  340. stSaveImgReq.strCaseStep = szValue;
  341. // 实例Id;
  342. _itoa_s(pTask->nDeviceId, szValue, 10);
  343. stSaveImgReq.strDeviceId = szValue;//pTask->Job.strDeviceId;
  344. // 就是Task中的ExecuteId
  345. _itoa_s(pTask->nExecuteId, szValue, 10);
  346. stSaveImgReq.strExecuteId = szValue;
  347. // 实例Id;
  348. _itoa_s(pTask->nInstanceId, szValue, 10);
  349. stSaveImgReq.strInstanceId = szValue;
  350. stSaveImgReq.strJobRepeat = "0";
  351. // 注意避坑:roundnum必须赋值0或1;
  352. stSaveImgReq.strRoundNum = "0";
  353. stSaveImgReq.strImgType = "";
  354. stSaveImgReq.strUploads = img;
  355. // 上传用例图片;
  356. if ( SaveResultImg(url, stSaveImgReq, stSaveImgResp) ) {
  357. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传图片成功:任务=%s, 用例=%s, 图片=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), img.c_str());
  358. return true;
  359. }
  360. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传图片失败:任务=%s, 用例=%s, 图片=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), img.c_str());
  361. return false;
  362. }
  363. bool CSATExecutor::UploadCaseLog(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase)
  364. {
  365. TCHAR szValue[MAX_PATH] = {0};
  366. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  367. url.append("/ajaxInteractiveManage!saveCaseOrTaskLog.action");
  368. SATHTTP::STSaveLogReq stSaveLogReq;
  369. SATHTTP::STSaveLogResp stSaveLogResp;
  370. stSaveLogReq.strCaseId = pCase->strId;
  371. // 执行ID;
  372. _itoa_s(pTask->nExecuteId, szValue, 10);
  373. stSaveLogReq.strExecuteId = szValue;
  374. stSaveLogReq.strFileType = "caseLogFile";
  375. // 任务Id;
  376. //_itoa_s(pTask->nTaskId, szValue, 10);
  377. stSaveLogReq.strTaskId = pTask->Job.strTaskInstanceId;
  378. stSaveLogReq.strUserId = pTask->Job.strUserId;
  379. // 要上传的日志文件;
  380. stSaveLogReq.strUploads = pCase->_strCaseLog;
  381. if ( SaveCaseOrTaskLog(url, stSaveLogReq, stSaveLogResp) ) {
  382. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传日志成功:任务=%s, 用例=%s, 日志=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), pCase->_strCaseLog.c_str());
  383. return true;
  384. }
  385. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传日志失败:任务=%s, 用例=%s, 日志=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), pCase->_strCaseLog.c_str());
  386. return false;
  387. }
  388. bool CSATExecutor::ReportCaseItemFinish(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase, STCaseItem &caseItem)
  389. {
  390. TCHAR szValue[MAX_PATH] = {0};
  391. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  392. url.append("/ajaxInteractiveManage!setResultList.action");
  393. SATHTTP::STJobProcessReq stJobProcessReq;
  394. SATHTTP::STJobProcessResp stJobProcessResp;
  395. // 0 表示测试项Pass, 1 表示测试项Fail;
  396. stJobProcessReq.strResultState = caseItem.result ? "0" : "1";
  397. // 如果测试项失败,标记用例和任务失败;
  398. if ( !caseItem.result ) {
  399. pCase->_nExecutionResult = SATHTTP::FAIL;
  400. pTask->_nExecutionResult = SATHTTP::FAIL;
  401. }
  402. stJobProcessReq.strCaseScene = "";
  403. // 索引;
  404. _itoa_s(pCase->_nCaseStep, szValue, 10);
  405. stJobProcessReq.strCaseStep = szValue;
  406. stJobProcessReq.strApkMD5 = "";
  407. stJobProcessReq.strCrashTime = "";
  408. // 就是Task中的ExecuteId
  409. _itoa_s(pTask->nExecuteId, szValue, 10);
  410. stJobProcessReq.strRunnerId = szValue;
  411. stJobProcessReq.strCPUInfo = "0";
  412. stJobProcessReq.strRunnedActionNameList = "";
  413. stJobProcessReq.strArtificialResult = "";
  414. stJobProcessReq.strArtificialModify = "";
  415. stJobProcessReq.strRunnerName = "";
  416. stJobProcessReq.strTaskType = "FUNCTIONALITY";
  417. stJobProcessReq.strCaseRepeat = "0";
  418. stJobProcessReq.strApplicationGroup = "";
  419. // 实例Id;
  420. _itoa_s(pTask->Id, szValue, 10);
  421. stJobProcessReq.strInstanceId = szValue;
  422. stJobProcessReq.strCaseId = pCase->strId;
  423. // 进度;
  424. _itoa_s(100*(atoi(pCase->strIndex.c_str()))/pTask->Job.vtCases.size(), szValue, 10);
  425. stJobProcessReq.strProgress = szValue;
  426. // 需要将utf-8转gbk;
  427. stJobProcessReq.strReusltMessage = caseItem.name;//CharEncoding::UTF82ASCII(caseItem.name.c_str());
  428. stJobProcessReq.strJobRepeat = "0";
  429. stJobProcessReq.strScreenShot = "";
  430. stJobProcessReq.strStartTime = pCase->_strStartTime;
  431. stJobProcessReq.strCrashNumber = "";
  432. stJobProcessReq.strCaseName = pCase->strCaseName;
  433. //stJobProcessReq.strFailedReason = CharEncoding::UTF82ASCII(caseItem.remark.c_str());
  434. //stJobProcessReq.strFailedReason = (char*)CharEncoding::UTF82UNICODE(caseItem.remark.c_str());
  435. stJobProcessReq.strFailedReason = caseItem.remark;
  436. for (std::vector<std::string>::iterator it = caseItem.imgs.begin(); it != caseItem.imgs.end(); it++ ) {
  437. int npos = it->find_last_of('/');
  438. if ( std::string::npos != npos ) {
  439. stJobProcessReq.strImgName.append(it->substr(npos+1));
  440. stJobProcessReq.strImgName.append("&");
  441. }
  442. }
  443. if ( stJobProcessReq.strImgName.size() && stJobProcessReq.strImgName.at(stJobProcessReq.strImgName.size()-1) == '&')
  444. stJobProcessReq.strImgName.erase(stJobProcessReq.strImgName.size()-1);
  445. stJobProcessReq.strCaseIndex = pCase->strIndex;
  446. // 实例Id;
  447. _itoa_s(pTask->nDeviceId, szValue, 10);
  448. stJobProcessReq.strDeviceId = szValue;
  449. stJobProcessReq.strSceneIndex = "0";
  450. // 实例Id;
  451. //_itoa_s(pTask->nTaskId, szValue, 10);
  452. stJobProcessReq.strTaskId = pTask->Job.strTaskId;
  453. //stJobProcessReq.strAnalysis = CharEncoding::UTF82ASCII(caseItem.data.c_str());
  454. stJobProcessReq.strAnalysis = caseItem.data.c_str();
  455. // 设备名称:即DeviceSerial;
  456. stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名及其不范围,一会deviceid是deviceserial,一会是id;
  457. // 固定为:TOTAL
  458. stJobProcessReq.strInfoType = "";
  459. // 如果是Android设备,需要通过adb获取;
  460. //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
  461. stJobProcessReq.strEndTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
  462. stJobProcessReq.strRoundNumber = "1";
  463. stJobProcessReq.strResultType = "4"; // reportActionFinish;
  464. stJobProcessReq.strOperationStep = "";
  465. if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
  466. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传【用例测试项】结果完成【成功】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
  467. // 再上传图片;
  468. std::string img;
  469. for (std::vector<std::string>::iterator it = caseItem.imgs.begin(); it != caseItem.imgs.end(); it++ ) {
  470. #if 0
  471. img = GLOBAL::g_stSATConfig.szCaseResultDir;
  472. #else
  473. // 使用脚本路径;
  474. img = pCase->_strFileDir;
  475. #endif
  476. img.append(it->substr(3));
  477. img = GLOBAL::Replace(img, "/", "\\");
  478. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "图片路径=%s", img.c_str());
  479. UploadCaseImg(pTask, pCase, img);
  480. }
  481. return true;
  482. }
  483. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传【用例测试项】结果完成【失败】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
  484. return false;
  485. }
  486. bool CSATExecutor::ReportCaseFinish(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase, std::string strRunnedActionNameList)
  487. {
  488. TCHAR szValue[MAX_PATH] = {0};
  489. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  490. url.append("/ajaxInteractiveManage!setResultList.action");
  491. SATHTTP::STJobProcessReq stJobProcessReq;
  492. SATHTTP::STJobProcessResp stJobProcessResp;
  493. // 上报用例结果:0表示脚本成功执行, 1表示脚本出错或超时;
  494. if ( pCase->_nExecutionState == SATHTTP::EXECUTED ) {
  495. if ( pCase->_nExecutionResult == SATHTTP::SUCCESS ) {
  496. stJobProcessReq.strResultState = "0";
  497. stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本执行成功");
  498. }
  499. else {
  500. if ( pCase->_nExecutionResult == SATHTTP::ABNORMAL )
  501. stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本执行异常");
  502. else
  503. stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本执行失败");
  504. stJobProcessReq.strResultState = "1";
  505. // 同时标记任务失败;
  506. pTask->_nExecutionResult = SATHTTP::FAIL;
  507. }
  508. }
  509. else {
  510. stJobProcessReq.strResultState = "1"; // 脚本执行未完成,认为fail;
  511. // 同时标记任务失败;
  512. pTask->_nExecutionResult = SATHTTP::FAIL;
  513. stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本未执行");
  514. }
  515. stJobProcessReq.strCaseScene = "";
  516. // 索引;
  517. _itoa_s(pCase->_nCaseStep, szValue, 10);
  518. stJobProcessReq.strCaseStep = szValue;
  519. stJobProcessReq.strApkMD5 = "";
  520. stJobProcessReq.strCrashTime = "";
  521. // 就是Task中的ExecuteId
  522. _itoa_s(pTask->nExecuteId, szValue, 10);
  523. stJobProcessReq.strRunnerId = szValue;
  524. stJobProcessReq.strCPUInfo = "0";
  525. stJobProcessReq.strRunnedActionNameList = strRunnedActionNameList;
  526. stJobProcessReq.strArtificialResult = "";
  527. stJobProcessReq.strArtificialModify = "";
  528. stJobProcessReq.strRunnerName = "";
  529. stJobProcessReq.strTaskType = "FUNCTIONALITY";
  530. stJobProcessReq.strCaseRepeat = "";
  531. stJobProcessReq.strApplicationGroup = "";
  532. // 实例Id;注意避坑;
  533. _itoa_s(pTask->Id, szValue, 10);
  534. stJobProcessReq.strInstanceId = szValue;
  535. stJobProcessReq.strCaseId = pCase->strId;
  536. // 进度(###需要修改此处###);
  537. _itoa_s(100*(atoi(pCase->strIndex.c_str()))/pTask->Job.vtCases.size(), szValue, 10);
  538. stJobProcessReq.strProgress = szValue;
  539. // 需要将utf-8转gbk;
  540. stJobProcessReq.strReusltMessage = CharEncoding::ASCII2UTF8("测试用例结果:");
  541. stJobProcessReq.strJobRepeat = "";
  542. stJobProcessReq.strScreenShot = "";
  543. stJobProcessReq.strStartTime = pCase->_strStartTime;
  544. stJobProcessReq.strCrashNumber = "";
  545. stJobProcessReq.strCaseName = pCase->strCaseName;
  546. stJobProcessReq.strImgName = "";
  547. stJobProcessReq.strCaseIndex = pCase->strIndex;
  548. // 实例Id;
  549. _itoa_s(pTask->nDeviceId, szValue, 10);
  550. stJobProcessReq.strDeviceId = szValue;
  551. stJobProcessReq.strSceneIndex = "0";
  552. // 实例Id;
  553. //_itoa_s(pTask->nTaskId, szValue, 10);
  554. stJobProcessReq.strTaskId = pTask->Job.strTaskId;
  555. stJobProcessReq.strAnalysis = "";
  556. // 设备名称:即DeviceSerial;
  557. stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名极其不范围,一会deviceid是deviceserial,一会是id;
  558. // 固定为:TOTAL
  559. stJobProcessReq.strInfoType = "TOTAL";
  560. // 如果是Android设备,需要通过adb获取;
  561. //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
  562. stJobProcessReq.strEndTime = pCase->_strEndTime;//CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
  563. stJobProcessReq.strRoundNumber = "1";
  564. stJobProcessReq.strResultType = "5"; // reportCaseFinish;
  565. stJobProcessReq.strOperationStep = "";
  566. if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
  567. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传用例结果完成【成功】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
  568. return true;
  569. }
  570. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传用例结果完成【失败】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
  571. return false;
  572. }
  573. bool CSATExecutor::ReportCaseResult(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase)
  574. {
  575. // 获取xml文件内容;
  576. #if 0
  577. std::string xmlpath = GLOBAL::g_stSATConfig.szCaseResultDir;
  578. #else
  579. // 使用脚本自身路径作为根目录;
  580. std::string xmlpath = pCase->_strFileDir;
  581. #endif
  582. xmlpath.append("detail\\");
  583. xmlpath.append(pCase->_strFileName);
  584. #if 0
  585. // 文件名加下PID做为后缀;
  586. if ( pCase->__dwPythonPID > 0)
  587. {
  588. TCHAR szPID[16] = "_";
  589. _ltoa_s(pCase->__dwPythonPID, &szPID[1], 15, 10);
  590. xmlpath.append(szPID);
  591. }
  592. #endif
  593. xmlpath.append("_result.xml");
  594. std::vector<STCaseItem> vtCaseItem;
  595. if ( GetCaseXMLResult(xmlpath, vtCaseItem) )
  596. pCase->_nExecutionResult = SATHTTP::SUCCESS;
  597. // 如果测试项空,任务失败;
  598. if ( vtCaseItem.size() == 0 ) {
  599. // 将默认成功值改为异常;
  600. if ( pCase->_nExecutionResult == SATHTTP::SUCCESS )
  601. pCase->_nExecutionResult = SATHTTP::ABNORMAL;
  602. // 同时标记任务失败;
  603. pTask->_nExecutionResult = SATHTTP::FAIL;
  604. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("测试项空,用例名:%s"), pCase->strCaseName.c_str());
  605. }
  606. int nIndex = 1;
  607. TCHAR szValue[MAX_PATH] = {0};
  608. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  609. url.append("/ajaxInteractiveManage!setResultList.action");
  610. SATHTTP::STJobProcessReq stJobProcessReq;
  611. SATHTTP::STJobProcessResp stJobProcessResp;
  612. // 遍历所有用例测试项;
  613. std::string strRunnedActionNameList;
  614. std::vector<STCaseItem>::iterator it = vtCaseItem.begin();
  615. for (; it != vtCaseItem.end();) {
  616. // 上报测试项结果(包含相片);
  617. ReportCaseItemFinish(pTask, pCase, *it);
  618. strRunnedActionNameList.append(it->name);
  619. if ( ++it != vtCaseItem.end() )
  620. strRunnedActionNameList.append("&");
  621. }
  622. // 上报用例完成;
  623. ReportCaseFinish(pTask, pCase, strRunnedActionNameList);
  624. // 上传用例日志;
  625. UploadCaseLog(pTask, pCase);
  626. // 删除执行对象;
  627. if ( pCase->_pExcutor )
  628. delete pCase->_pExcutor;
  629. pCase->_pExcutor = NULL;
  630. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("用例已完成,执行器对象删除成功,脚本用例名为=%s"), pCase->strCaseName.c_str());
  631. return true;
  632. }
  633. bool CSATExecutor::UploadTaskLog(SATHTTP::STTask* pTask)
  634. {
  635. TCHAR szValue[MAX_PATH] = {0};
  636. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  637. url.append("/ajaxInteractiveManage!saveCaseOrTaskLog.action");
  638. SATHTTP::STSaveLogReq stSaveLogReq;
  639. SATHTTP::STSaveLogResp stSaveLogResp;
  640. stSaveLogReq.strCaseId = "";
  641. // 执行ID;
  642. _itoa_s(pTask->nExecuteId, szValue, 10);
  643. stSaveLogReq.strExecuteId = szValue;
  644. stSaveLogReq.strFileType = "taskLogFile";
  645. // 任务Id;
  646. //_itoa_s(pTask->nTaskId, szValue, 10);
  647. stSaveLogReq.strTaskId = pTask->Job.strTaskInstanceId;
  648. stSaveLogReq.strUserId = pTask->Job.strUserId;
  649. // 要上传的日志文件;
  650. stSaveLogReq.strUploads = "D:\\sat\\log.txt";//pTask->_strTaskLog;
  651. if ( SaveCaseOrTaskLog(url, stSaveLogReq, stSaveLogResp) ) {
  652. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("任务已完成,上传任务日志【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
  653. return true;
  654. }
  655. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("任务已完成,上传任务日志【失败】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
  656. return false;
  657. }
  658. bool CSATExecutor::ReportTaskStart(SATHTTP::STTask* pTask)
  659. {
  660. TCHAR szValue[MAX_PATH] = {0};
  661. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  662. url.append("/ajaxInteractiveManage!setResultList.action");
  663. SATHTTP::STJobProcessReq stJobProcessReq;
  664. SATHTTP::STJobProcessResp stJobProcessResp;
  665. // 需要处理###
  666. stJobProcessReq.strResultState = ""; // 脚本执行中,认为fail;
  667. stJobProcessReq.strCaseScene = "";
  668. // 索引;
  669. stJobProcessReq.strCaseStep = "0";
  670. stJobProcessReq.strApkMD5 = "";
  671. stJobProcessReq.strCrashTime = "";
  672. // 就是Task中的ExecuteId
  673. _itoa_s(pTask->nExecuteId, szValue, 10);
  674. stJobProcessReq.strRunnerId = szValue;
  675. stJobProcessReq.strCPUInfo = "0";
  676. stJobProcessReq.strRunnedActionNameList = "";
  677. stJobProcessReq.strArtificialResult = "";
  678. stJobProcessReq.strArtificialModify = "";
  679. stJobProcessReq.strRunnerName = "";
  680. stJobProcessReq.strTaskType = "FUNCTIONALITY";
  681. stJobProcessReq.strCaseRepeat = "";
  682. stJobProcessReq.strApplicationGroup = "";
  683. // 实例Id;注意避坑;
  684. _itoa_s(pTask->Id, szValue, 10);
  685. stJobProcessReq.strInstanceId = szValue;
  686. stJobProcessReq.strCaseId = "";
  687. // 进度(###需要修改此处###);
  688. stJobProcessReq.strProgress = "0";
  689. // 需要将utf-8转gbk;
  690. stJobProcessReq.strReusltMessage = CharEncoding::ASCII2UTF8("任务开始");
  691. stJobProcessReq.strJobRepeat = "";
  692. stJobProcessReq.strScreenShot = "";
  693. stJobProcessReq.strStartTime = pTask->strStartTime;
  694. stJobProcessReq.strCrashNumber = "";
  695. stJobProcessReq.strCaseName = "";
  696. stJobProcessReq.strFailedReason = "";
  697. stJobProcessReq.strImgName = "";
  698. stJobProcessReq.strCaseIndex = "";
  699. // 实例Id;
  700. _itoa_s(pTask->nDeviceId, szValue, 10);
  701. stJobProcessReq.strDeviceId = szValue;
  702. stJobProcessReq.strSceneIndex = "";
  703. // 实例Id;
  704. //_itoa_s(pTask->nTaskId, szValue, 10);
  705. stJobProcessReq.strTaskId = pTask->Job.strTaskId;
  706. stJobProcessReq.strAnalysis = "";
  707. // 设备名称:即DeviceSerial;
  708. stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名及其不范围,一会deviceid是deviceserial,一会是id;
  709. // 固定为:TOTAL
  710. stJobProcessReq.strInfoType = "TOTAL";
  711. // 如果是Android设备,需要通过adb获取;
  712. //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
  713. stJobProcessReq.strEndTime = "";// CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
  714. stJobProcessReq.strRoundNumber = "1";
  715. stJobProcessReq.strResultType = "0"; // reportJobStart;
  716. stJobProcessReq.strOperationStep = "";
  717. if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
  718. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务开始【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
  719. return true;
  720. }
  721. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务开始【失败】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
  722. return false;
  723. }
  724. bool CSATExecutor::ReportTaskFinish(SATHTTP::STTask* pTask)
  725. {
  726. TCHAR szValue[MAX_PATH] = {0};
  727. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  728. url.append("/ajaxInteractiveManage!setResultList.action");
  729. SATHTTP::STJobProcessReq stJobProcessReq;
  730. SATHTTP::STJobProcessResp stJobProcessResp;
  731. // 需要处理(只要有用例失败,任务就失败)
  732. if ( pTask->_nExecutionState == SATHTTP::EXECUTED ) {
  733. if ( pTask->_nExecutionResult == SATHTTP::SUCCESS )
  734. stJobProcessReq.strResultState = "0"; // 脚本成功;
  735. else
  736. stJobProcessReq.strResultState = "1"; // 脚本失败;
  737. }
  738. else
  739. stJobProcessReq.strResultState = "1"; // 脚本失败;
  740. stJobProcessReq.strCaseScene = "";
  741. // 索引;
  742. stJobProcessReq.strCaseStep = "0";
  743. stJobProcessReq.strApkMD5 = "";
  744. stJobProcessReq.strCrashTime = "";
  745. // 就是Task中的ExecuteId
  746. _itoa_s(pTask->nExecuteId, szValue, 10);
  747. stJobProcessReq.strRunnerId = szValue;
  748. stJobProcessReq.strCPUInfo = "0";
  749. stJobProcessReq.strRunnedActionNameList = "";
  750. stJobProcessReq.strArtificialResult = "";
  751. stJobProcessReq.strArtificialModify = "";
  752. stJobProcessReq.strRunnerName = "";
  753. stJobProcessReq.strTaskType = "FUNCTIONALITY";
  754. stJobProcessReq.strCaseRepeat = "0";
  755. stJobProcessReq.strApplicationGroup = "";
  756. // 实例Id;注意避坑;
  757. _itoa_s(pTask->Id, szValue, 10);
  758. stJobProcessReq.strInstanceId = szValue;
  759. stJobProcessReq.strCaseId = "";
  760. // 进度(###需要修改此处###);
  761. stJobProcessReq.strProgress = "100";
  762. // 需要将utf-8转gbk;
  763. stJobProcessReq.strReusltMessage = CharEncoding::ASCII2UTF8("任务结束");
  764. stJobProcessReq.strJobRepeat = "0";
  765. stJobProcessReq.strScreenShot = "";
  766. // 注意避坑:实际结束时间也要放在该字段中;
  767. stJobProcessReq.strStartTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
  768. stJobProcessReq.strCrashNumber = "";
  769. stJobProcessReq.strCaseName = "";
  770. stJobProcessReq.strFailedReason = "";
  771. stJobProcessReq.strImgName = "";
  772. stJobProcessReq.strCaseIndex = "";
  773. // 实例Id;
  774. _itoa_s(pTask->nDeviceId, szValue, 10);
  775. stJobProcessReq.strDeviceId = szValue;
  776. stJobProcessReq.strSceneIndex = "0";
  777. // 实例Id;
  778. //_itoa_s(pTask->nTaskId, szValue, 10);
  779. stJobProcessReq.strTaskId = pTask->Job.strTaskId;
  780. stJobProcessReq.strAnalysis = "";
  781. // 设备名称:即DeviceSerial;
  782. stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名及其不范围,一会deviceid是deviceserial,一会是id;
  783. // 固定为:TOTAL
  784. stJobProcessReq.strInfoType = "";
  785. // 如果是Android设备,需要通过adb获取;
  786. //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
  787. stJobProcessReq.strEndTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
  788. stJobProcessReq.strRoundNumber = "1";
  789. stJobProcessReq.strResultType = "1"; // reportJobFinish;
  790. stJobProcessReq.strOperationStep = "";
  791. if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
  792. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务结束【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
  793. return true;
  794. }
  795. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务结束【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
  796. return false;
  797. }
  798. bool CSATExecutor::GetCaseXMLResult(std::string xmlpath, std::vector<STCaseItem> &vtCaseItem)
  799. {
  800. // 解析xml;
  801. tinyxml2::XMLDocument doc;
  802. if (tinyxml2::XML_SUCCESS != doc.LoadFile(xmlpath.c_str()) ) {
  803. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "没有测试项对应的xml文件:%s", xmlpath.c_str());
  804. return false;
  805. }
  806. tinyxml2::XMLElement *pXmlRoot = NULL;
  807. if ((pXmlRoot = doc.RootElement()) == NULL)
  808. return false;
  809. if (_tcsicmp(pXmlRoot->Value(), "results") != 0)
  810. return false;
  811. // 获取属性;
  812. bool bFinishStatus = false;
  813. const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
  814. while (pAttr) {
  815. if (_tcsicmp(pAttr->Name(), "caseFinish") == 0) {
  816. // 整个测试项ok;
  817. if ( _tcsicmp(pAttr->Value(), _T("1")) == 0 )
  818. bFinishStatus = true;
  819. break;
  820. }
  821. pAttr = pAttr->Next();
  822. }
  823. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "开始获取测试项内容:%s", xmlpath.c_str());
  824. tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
  825. while (pXmlElent) {
  826. if (_tcsicmp(pXmlElent->Value(), _T("item")) == 0) {
  827. STCaseItem cItem;
  828. tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
  829. while (pItem) {
  830. if (_tcsicmp(pItem->Value(), _T("name")) == 0)
  831. {
  832. cItem.name = pItem->GetText();
  833. }
  834. else if (_tcsicmp(pItem->Value(), _T("result")) == 0)
  835. {
  836. if ( _tcsicmp(pItem->GetText(), _T("Pass")) == 0 )
  837. cItem.result = true;
  838. else
  839. cItem.result = false;
  840. }
  841. else if (_tcsicmp(pItem->Value(), _T("data")) == 0)
  842. {
  843. cItem.data = pItem->GetText();
  844. }
  845. else if (_tcsicmp(pItem->Value(), _T("log")) == 0)
  846. {
  847. cItem.log = pItem->GetText();
  848. }
  849. else if (_tcsicmp(pItem->Value(), _T("remark")) == 0)
  850. {
  851. cItem.remark = pItem->GetText();
  852. }
  853. else if (_tcsicmp(pItem->Value(), _T("screen")) == 0)
  854. {
  855. cItem.imgs.push_back(pItem->GetText());
  856. }
  857. pItem = pItem->NextSiblingElement();
  858. }
  859. // 压入容器;
  860. if ( cItem.name.size() )
  861. vtCaseItem.push_back(cItem);
  862. }
  863. pXmlElent = pXmlElent->NextSiblingElement();
  864. }
  865. return bFinishStatus;
  866. }
  867. int CSATExecutor::AttachTaskInfo2Buffer(SATPROTO::TaskInfo (&pbuff)[SATPROTO::MAX_TASKS])
  868. {
  869. int count = 0;
  870. if ( pbuff ) {
  871. AutoThreadSection ats(&m_csTask);
  872. std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
  873. for ( ;it != m_vtTask.end(); it++ ) {
  874. // 任务状态;
  875. //pbuff[count].nStatus = it->_nExecutionState;
  876. // 任务id;
  877. pbuff[count].nTaskId = it->Id;
  878. // 任务编号;
  879. memcpy_s(pbuff[count].szTaskNo, SATPROTO::MAX_NAME, it->Job.strUniqueId.c_str(), it->Job.strUniqueId.size());
  880. // 任务名称;
  881. memcpy_s(pbuff[count].szTaskName, SATPROTO::MAX_NAME, it->strTaskName.c_str(), it->strTaskName.size());
  882. // 任务状态;
  883. if ( it->_nExecutionState == SATHTTP::UNEXECUTED )
  884. {
  885. _stprintf_s(pbuff[count].szStatus, SATPROTO::MAX_STATUS, _T("未执行"));
  886. }
  887. else if (it->_nExecutionState == SATHTTP::INEXECUTED)
  888. {
  889. _stprintf_s(pbuff[count].szStatus, SATPROTO::MAX_STATUS, _T("执行中"));
  890. }
  891. else if (it->_nExecutionState == SATHTTP::EXECUTED)
  892. {
  893. _stprintf_s(pbuff[count].szStatus, SATPROTO::MAX_STATUS, _T("已执行"));
  894. }
  895. int nIndex = 0;
  896. // 用例数量;
  897. pbuff[count].nCaseSize = it->Job.vtCases.size();
  898. for (std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin(); _case != it->Job.vtCases.end(); _case++) {
  899. // 用例状态;
  900. // pbuff[count].ssCases[nIndex].nStatus = _case->_nExecutionState;
  901. //pbuff[count].ssCases[nIndex].nCaseId = _case->_nCaseStep;
  902. // 用例名称;
  903. memcpy_s(pbuff[count].ssCases[nIndex].szCaseName, SATPROTO::MAX_NAME, _case->strCaseName.c_str(), _case->strCaseName.size());
  904. // 用例日志路径;
  905. memcpy_s(pbuff[count].ssCases[nIndex].szCaseLogPath, MAX_PATH, _case->_strCaseLog.c_str(), _case->_strCaseLog.size());
  906. // 用例执行状态;
  907. if (_case->_nExecutionState == SATHTTP::UNEXECUTED)
  908. {
  909. _stprintf_s(pbuff[count].ssCases[nIndex].szExecStatus, SATPROTO::MAX_STATUS, _T("未执行"));
  910. }
  911. else if (_case->_nExecutionState == SATHTTP::INEXECUTED)
  912. {
  913. _stprintf_s(pbuff[count].ssCases[nIndex].szExecStatus, SATPROTO::MAX_STATUS, _T("执行中"));
  914. }
  915. else if (_case->_nExecutionState == SATHTTP::EXECUTED)
  916. {
  917. _stprintf_s(pbuff[count].ssCases[nIndex].szExecStatus, SATPROTO::MAX_STATUS, _T("已执行"));
  918. }
  919. // 用例执行结果;
  920. if ( _case->_nExecutionResult == SATHTTP::NONE )
  921. {
  922. _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("无"));
  923. }
  924. else if ( _case->_nExecutionResult == SATHTTP::SUCCESS )
  925. {
  926. _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("成功"));
  927. }
  928. else if ( _case->_nExecutionResult == SATHTTP::FAIL )
  929. {
  930. _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("失败"));
  931. }
  932. else if ( _case->_nExecutionResult == SATHTTP::ABNORMAL )
  933. {
  934. _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("异常"));
  935. }
  936. else if ( _case->_nExecutionResult == SATHTTP::OVERTIME )
  937. {
  938. _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("超时"));
  939. }
  940. nIndex++;
  941. // 超过MAX_CASES退出;
  942. if ( nIndex == SATPROTO::MAX_CASES )
  943. break;
  944. }
  945. count++;
  946. // 超过MAX_TASKS退出;
  947. if ( count == SATPROTO::MAX_TASKS )
  948. break;
  949. }
  950. }
  951. return count;
  952. }
  953. void CSATExecutor::StartWork()
  954. {
  955. m_hEventHearbeat = CreateEvent(NULL, TRUE, FALSE, NULL);
  956. if ( m_hEventHearbeat == NULL ) {
  957. _tprintf_s(_T("创建事件失败\n"));
  958. return;
  959. }
  960. m_hEventExcuteScript = CreateEvent(NULL, TRUE, FALSE, NULL);
  961. if ( m_hEventExcuteScript == NULL ) {
  962. _tprintf_s(_T("创建事件失败2\n"));
  963. return;
  964. }
  965. m_hThreadHearbeat = CreateThread(NULL, 0, HearbeatThread, this, 0, NULL);
  966. if ( m_hThreadHearbeat == NULL) {
  967. SetEvent(m_hEventHearbeat);
  968. SetEvent(m_hEventExcuteScript);
  969. CloseHandle(m_hEventHearbeat);
  970. CloseHandle(m_hEventExcuteScript);
  971. m_hEventHearbeat = NULL;
  972. m_hEventExcuteScript = NULL;
  973. _tprintf_s(_T("创建线程失败\n"));
  974. return;
  975. }
  976. m_hThreadExcuteScript = CreateThread(NULL, 0, ExecuteScriptThread, this, 0, NULL);
  977. if ( m_hThreadExcuteScript == NULL ) {
  978. SetEvent(m_hEventHearbeat);
  979. SetEvent(m_hEventExcuteScript);
  980. CloseHandle(m_hEventHearbeat);
  981. CloseHandle(m_hEventExcuteScript);
  982. m_hEventHearbeat = NULL;
  983. m_hEventExcuteScript = NULL;
  984. WaitForSingleObject(m_hThreadHearbeat,INFINITE);
  985. if (m_hThreadHearbeat)
  986. CloseHandle(m_hThreadHearbeat);
  987. m_hThreadHearbeat = NULL;
  988. _tprintf_s(_T("创建线程失败2\n"));
  989. return;
  990. }
  991. }
  992. void CSATExecutor::EndofWork()
  993. {
  994. //////////////////////////////////////////////////////////////////////////
  995. // 设置事件有信号;
  996. if ( m_hEventHearbeat )
  997. SetEvent(m_hEventHearbeat);
  998. // 等待线程结束;
  999. if ( m_hThreadHearbeat ) {
  1000. WaitForSingleObject(m_hThreadHearbeat, INFINITE);
  1001. CloseHandle(m_hThreadHearbeat);
  1002. m_hThreadHearbeat = NULL;
  1003. }
  1004. // 关闭事件句柄;
  1005. if ( m_hEventHearbeat ) {
  1006. CloseHandle(m_hEventHearbeat);
  1007. m_hEventHearbeat = NULL;
  1008. }
  1009. //////////////////////////////////////////////////////////////////////////
  1010. // 设置事件有信号;
  1011. if ( m_hEventExcuteScript )
  1012. SetEvent(m_hEventExcuteScript);
  1013. // 等待线程结束;
  1014. if ( m_hThreadExcuteScript ) {
  1015. WaitForSingleObject(m_hThreadExcuteScript, INFINITE);
  1016. CloseHandle(m_hThreadExcuteScript);
  1017. m_hThreadExcuteScript = NULL;
  1018. }
  1019. // 关闭事件句柄;
  1020. if ( m_hEventExcuteScript ) {
  1021. CloseHandle(m_hEventExcuteScript);
  1022. m_hEventExcuteScript = NULL;
  1023. }
  1024. // 如果有脚本在执行,结束脚本;
  1025. CPythonExecutor *pExecutor = NULL;
  1026. for ( std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin(); it != m_vtTask.end(); it++ ) {
  1027. for ( std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin(); _case != it->Job.vtCases.end(); _case++ ) {
  1028. if ( (pExecutor = (CPythonExecutor *)_case->_pExcutor) ) {
  1029. pExecutor->EndThread();
  1030. delete pExecutor;
  1031. pExecutor = NULL;
  1032. }
  1033. }
  1034. }
  1035. m_vtTask.clear();
  1036. }
  1037. DWORD CSATExecutor::HearbeatThread(LPVOID lpVoid)
  1038. {
  1039. CSATExecutor *that = (CSATExecutor*)lpVoid;
  1040. if ( !that ) return 0;
  1041. do {
  1042. // 如果未启用电视监听,启用实时读取配置文件;
  1043. if ( that->m_bStopWathTV ) {
  1044. GLOBAL::GetIniInfo();
  1045. }
  1046. // 没有登录成功,不查询;
  1047. if ( !that->m_bLogin ) continue;
  1048. // 更新设备;
  1049. that->UpdateDevice();
  1050. // 脚本保存目录;
  1051. std::string strScriptSaveDir;
  1052. SATHTTP::STHeartbeatReq stHeartbeatReq;
  1053. SATHTTP::STHeartbeatResp stHeartbeatResp;
  1054. std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
  1055. url.append("/ajaxInteractiveManage!executeHeartbeat.action");
  1056. stHeartbeatReq.strUserName = that->m_stLoginReq.strUserName;
  1057. stHeartbeatReq.strRunnerMac = that->m_stLoginReq.strMAC;
  1058. stHeartbeatReq.devicelist.assign(that->m_vtDevice.begin(), that->m_vtDevice.end());
  1059. if ( Heartbeat(url, stHeartbeatReq, stHeartbeatResp) ) {
  1060. std::vector<SATHTTP::STTask>::iterator it = stHeartbeatResp.vtTask.begin();
  1061. for ( ; it != stHeartbeatResp.vtTask.end(); it++ ) {
  1062. if (!that->IsTaskExist(*it)) {
  1063. // 将任务压入队列中;
  1064. AutoThreadSection ats(&that->m_csTask);
  1065. // 同时下载任务;
  1066. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("【任务脚本下载】:%s"), it->Job.strUniqueId.c_str());
  1067. std::string host = GLOBAL::g_stSATConfig.szCaseServer;
  1068. std::string url = host + "/ajaxInteractiveManage!getCaseFileListUrl.action";
  1069. std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin();
  1070. for ( int i = 1; _case != it->Job.vtCases.end(); _case++) {
  1071. // 下载脚本;
  1072. _case->_nCaseStep = i++;
  1073. SATHTTP::STScriptUrlResp stScriptUrlResp;
  1074. // 脚本保存路径;
  1075. strScriptSaveDir = GLOBAL::g_stSATConfig.szScriptDir;
  1076. strScriptSaveDir.append(it->Job.strProjectName+"\\");
  1077. strScriptSaveDir.append(it->Job.strUniqueId+"\\");
  1078. // 把冒号改成下划线;
  1079. strScriptSaveDir.append(GLOBAL::Replace(it->Job.strDeviceId, "_", ":")+"\\");
  1080. if ( DownloadScript(url, _case->strId, strScriptSaveDir, stScriptUrlResp) ) {
  1081. _case->_strFileDir = stScriptUrlResp._strFileDir;
  1082. _case->_strFileName = stScriptUrlResp._strFileName;
  1083. _case->_strScriptPath = stScriptUrlResp._strScripFile;
  1084. } else {
  1085. // 脚本下载失败时,需要标记脚本为已运行且异常;
  1086. _case->_nExecutionState = SATHTTP::EXECUTED;
  1087. _case->_nExecutionResult = SATHTTP::ABNORMAL;
  1088. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("【下载脚本失败】:%s"), _case->strCaseName.c_str());
  1089. }
  1090. }
  1091. that->m_vtTask.push_back(*it);
  1092. // 通知SAT服务器,脚本开始执行;
  1093. // 待开发:同时将任务存储到数据库中;
  1094. /*
  1095. db process
  1096. */
  1097. }
  1098. // 进行一次删除操作;
  1099. that->DelFinishedTask();
  1100. }
  1101. }
  1102. } while ( WaitForSingleObject(that->m_hEventHearbeat, 10000) == WAIT_TIMEOUT );
  1103. return 0;
  1104. }
  1105. DWORD CSATExecutor::ExecuteScriptThread(LPVOID lpVoid)
  1106. {
  1107. CSATExecutor *that = (CSATExecutor*)lpVoid;
  1108. if ( !that ) return 0;
  1109. do {
  1110. // 删除已完成的任务;
  1111. that->DelFinishedTask();
  1112. // 是否有任务在执行;
  1113. SATHTTP::STTask *pTask = that->IsThereATaskInProcess();
  1114. if ( pTask ) {// 有任务在执行中;
  1115. if ( pTask->_bConcurrent ) {// 并发;
  1116. }
  1117. else {// 串行;
  1118. // 是否有脚本用例在执行;
  1119. SATHTTP::STCase* pCase = that->IsCaseScriptProcess(pTask->Job.vtCases);
  1120. if ( pCase ) {
  1121. CPythonExecutor *pExcutor = (CPythonExecutor*)pCase->_pExcutor;
  1122. if ( pExcutor ) {
  1123. if ( pExcutor->IsScriptOver() ) {
  1124. // 标记脚本已执行;
  1125. pCase->_nExecutionState = SATHTTP::EXECUTED;
  1126. // 如果脚本异常,标记任务失败;
  1127. if ( pCase->_nExecutionResult == SATHTTP::ABNORMAL ) {
  1128. // 设置任务执行结果失败;
  1129. pTask->_nExecutionResult = SATHTTP::FAIL;
  1130. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "用例脚本异常退出(%s)", pCase->strCaseName.c_str());
  1131. }
  1132. // 上报服务器,完成脚本用例,并上传用例结果;
  1133. that->ReportCaseResult(pTask, pCase);
  1134. }
  1135. else {
  1136. // 判断是否超时;
  1137. ULONGLONG ulCurTickCount = GetTickCount64();
  1138. if ( ulCurTickCount - pExcutor->GetActiveTickCount() > GLOBAL::g_stSATConfig.dwScriptTimeout ) {
  1139. // 标记任务已执行;
  1140. pCase->_nExecutionState = SATHTTP::EXECUTED;
  1141. // 结束进程;
  1142. pExcutor->EndThread();
  1143. // 超时中断;
  1144. pCase->_nExecutionResult = SATHTTP::OVERTIME;
  1145. // 上报用例结果;
  1146. that->ReportCaseResult(pTask, pCase);
  1147. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("脚本超时:%s"), pCase->strCaseName.c_str());
  1148. }
  1149. }
  1150. }
  1151. }
  1152. else {
  1153. // 没有在执行的用例,开始执行新的用例;
  1154. pCase = that->ExecuteFreeCaseScript(pTask);
  1155. if ( NULL == pCase ) {
  1156. // 没有空闲的用例可执行,说明所有用例已执行完成;
  1157. pTask->_nExecutionState = SATHTTP::EXECUTED;
  1158. // 上报任务完成;
  1159. that->ReportTaskFinish(pTask);
  1160. // 上报任务结果;
  1161. that->UploadTaskLog(pTask);
  1162. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("没有可执行的用例脚本,任务【%s】"), pTask->Job.strUniqueId.c_str());
  1163. }
  1164. }
  1165. }
  1166. }
  1167. else {
  1168. // 获取空闲的任务;
  1169. pTask = that->GetFreeTask();
  1170. if ( pTask ) {
  1171. // 是否支持并发;
  1172. if ( pTask->_bConcurrent ) {
  1173. // 暂时全部一起并发;
  1174. std::vector<SATHTTP::STCase>::iterator _case = pTask->Job.vtCases.begin();
  1175. for ( ; _case != pTask->Job.vtCases.end(); _case++) {
  1176. if (!_case->_pExcutor) {
  1177. CPythonExecutor *pExcutor = new CPythonExecutor();
  1178. if ( pExcutor ) {
  1179. _case->_pExcutor = pExcutor;
  1180. pExcutor->InitScript(_case->_strScriptPath, _case->_strFileDir + "\\" + _case->_strFileName + ".txt", "");
  1181. pExcutor->StartScript();
  1182. // 标记用例执行中;
  1183. _case->_nExecutionState = SATHTTP::INEXECUTED;
  1184. }
  1185. }
  1186. }
  1187. // 标记任务为执行中;
  1188. pTask->_nExecutionState = SATHTTP::INEXECUTED;
  1189. }
  1190. else {
  1191. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("\n<===============================================>\n# 1、开始执行任务:%s, 用例数量:%ld#\n<===============================================>\n"),
  1192. pTask->Job.strUniqueId.c_str(),
  1193. pTask->Job.vtCases.size());
  1194. // 是否有用例脚本在执行;
  1195. SATHTTP::STCase* pCase = that->IsCaseScriptProcess(pTask->Job.vtCases);
  1196. if ( !pCase ) {
  1197. // 执行空闲用例脚本;
  1198. that->ExecuteFreeCaseScript(pTask);
  1199. }
  1200. }
  1201. // 通知SAT服务器,脚本开始执行;
  1202. that->NotifyTaskStart(pTask);
  1203. // 通知SAT服务器,脚本开始;
  1204. that->ReportTaskStart(pTask);
  1205. // 将设备变更成繁忙状态;
  1206. that->SetDeviceStatus(pTask->Job.strDeviceId, SATDEV::InUse);
  1207. }
  1208. }
  1209. } while ( WaitForSingleObject(that->m_hEventExcuteScript, 10000) == WAIT_TIMEOUT );
  1210. return 0;
  1211. }
  1212. DWORD CSATExecutor::WathTVPortThread(PVOID lpVoid)
  1213. {
  1214. CSATExecutor *that = (CSATExecutor*)lpVoid;
  1215. if ( !that )
  1216. return 0;
  1217. CSynSerial tv;
  1218. std::string buffer;
  1219. TCHAR szLastPort[10] = {0};
  1220. _tcscpy_s(szLastPort, GLOBAL::g_stSATConfig.szTVPort);
  1221. std::vector<std::string> vtNotifyReboot;
  1222. std::vector<std::string> vtNotifyShutdown;
  1223. DWORD dwTickCount = GetTickCount();
  1224. while( !that->m_bStopWathTV ) {
  1225. // 实时读取配置文件;
  1226. GLOBAL::GetIniInfo();
  1227. if ( !GLOBAL::g_stSATConfig.bWatchTVPort ) {
  1228. Sleep(5000);
  1229. continue;
  1230. }
  1231. if ( GetTickCount() - dwTickCount > 5000 ) { // 5秒更新一次;
  1232. vtNotifyReboot.clear();
  1233. vtNotifyShutdown.clear();
  1234. dwTickCount = GetTickCount();
  1235. GLOBAL::Split(GLOBAL::g_stSATConfig.szTVReboot, _T(";"), vtNotifyReboot);
  1236. GLOBAL::Split(GLOBAL::g_stSATConfig.szTVShutdown, _T(";"), vtNotifyShutdown);
  1237. }
  1238. // 串口是否变更;
  1239. if ( _tcsicmp(szLastPort, GLOBAL::g_stSATConfig.szTVPort) ) {
  1240. _tcscpy_s(szLastPort, GLOBAL::g_stSATConfig.szTVPort);
  1241. // 重启打开串口;
  1242. tv.CloseSerialPort();
  1243. }
  1244. // 如果串口打开失败重新打开;
  1245. if ( !tv.IsOpen() ) {
  1246. tv.OpenSerialPort(szLastPort, 115200, 8, 0, 1, 0, 1000);
  1247. }
  1248. if ( tv.IsOpen() ) {
  1249. BYTE szBuffer[1024] = {0};
  1250. DWORD dwBuffer = tv.ReadComm(szBuffer, 1024, 1000);
  1251. if ( dwBuffer != 0 ) {
  1252. int nType = 0; // 0表示无异常类型,-1表示关机异常,1表示重启异常;
  1253. bool bAbnormal = false;
  1254. buffer = (char*)szBuffer;
  1255. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  1256. // 遍历是否有符合的子串;
  1257. for ( std::vector<std::string>::iterator it = vtNotifyReboot.begin(); it != vtNotifyReboot.end(); it++ )
  1258. {
  1259. if ( _tcsstr(buffer.c_str(), it->c_str()) ) {
  1260. nType = 1;
  1261. bAbnormal = true;
  1262. break;
  1263. }
  1264. }
  1265. if ( !bAbnormal ) {
  1266. for ( std::vector<std::string>::iterator it = vtNotifyShutdown.begin(); it != vtNotifyShutdown.end(); it++ )
  1267. {
  1268. if ( _tcsstr(buffer.c_str(), it->c_str()) ) {
  1269. nType = -1;
  1270. bAbnormal = true;
  1271. break;
  1272. }
  1273. }
  1274. }
  1275. // 1分钟前,是否有收到脚本通知;
  1276. if ( GLOBAL::g_PyNotify.notify ) {
  1277. __int64 nd = gmt - GLOBAL::g_PyNotify.datetime;
  1278. #ifdef _DEBUG
  1279. TRACE3("判断时间%ld-通知时间%ld=相差时间%ld\n\n", gmt, GLOBAL::g_PyNotify.datetime, nd);
  1280. #endif
  1281. if ( nd < 60)
  1282. {
  1283. // 监测的类型与通知类型是否一致;
  1284. if ( (nType == 1 && GLOBAL::g_PyNotify.report_type == _T("reboot")) ||
  1285. (nType == -1 && GLOBAL::g_PyNotify.report_type == _T("shutdown")) )
  1286. {
  1287. bAbnormal = false;
  1288. // 重置;
  1289. GLOBAL::g_PyNotify.notify = false;
  1290. GLOBAL::g_PyNotify.datetime = 0;
  1291. GLOBAL::g_PyNotify.report_type.clear();
  1292. }
  1293. }
  1294. }
  1295. if (bAbnormal) {
  1296. if ( nType == -1 ) {
  1297. // 遥控:重启电视机;
  1298. if ( Connect("127.0.0.1", 40000) )
  1299. sendSignal("POWER", 1, 1000);
  1300. GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "出现异常关机重启现象");
  1301. }
  1302. // 压入容器中;
  1303. GLOBAL::PyNotice tlog;
  1304. if ( nType == -1 )
  1305. tlog.report_type = "Abnormal shutdown";
  1306. else if ( nType == 1 )
  1307. tlog.report_type = "Abnormal restart";
  1308. tlog.datetime = gmt; // 发生时间;
  1309. struct tm gmtm = {0};
  1310. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  1311. TCHAR szDataTime[64] = {0};
  1312. _stprintf_s(szDataTime,_T("Time of exception:%04d-%02d-%02d %02d:%02d:%02d"), gmtm.tm_year + 1900, gmtm.tm_mon+1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec);
  1313. tlog.report_data = szDataTime;
  1314. // 最后,重置;
  1315. GLOBAL::g_PyNotify.notify = false;
  1316. GLOBAL::g_PyNotify.datetime = 0;
  1317. GLOBAL::g_PyNotify.report_type.clear();
  1318. }
  1319. }
  1320. }
  1321. Sleep(200);
  1322. }
  1323. return 0;
  1324. }