1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480 |
- #include "StdAfx.h"
- #include "SATExecutor.h"
- #include "PythonExecutor.h"
- #include "CharEncoding.h"
- #include "SynSerial.h"
- #include "IRControl.h"
- CSATExecutor::CSATExecutor(void)
- {
- m_bLogin = FALSE;
- m_bStopWathTV = FALSE;
- m_hEventHearbeat = NULL;
- m_hThreadHearbeat = NULL;
- m_hEventExcuteScript = NULL;
- m_hThreadExcuteScript = NULL;
- }
- CSATExecutor::~CSATExecutor(void)
- {
- m_bStopWathTV = FALSE;
- EndofWork();
- }
- bool CSATExecutor::IsTaskExist(SATHTTP::STTask &task)
- {
- bool found = false;
- std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
- for ( ; it != m_vtTask.end(); it++ ) {
- // 不要使用nTaskId,这个值没啥用;//不要使用Id这个值,如果任务被停止会变更;
- if ( it->nInstanceId == task.nInstanceId ) {
- // 如果被中止,并变更状态为已执行;
- if ( _tcsicmp(task.strTaskType.c_str(), "3") == 0 ) {
- it->_nExecutionState = SATHTTP::EXECUTED;
- it->_nExecutionResult = SATHTTP::FAIL;
- }
- found = true;
- break;
- }
- }
- return found;
- }
- SATHTTP::STTask* CSATExecutor::IsThereATaskInProcess()
- {
- std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
- for ( ; it != m_vtTask.end(); it++ ) {
- // 执行中的任务;
- if ( it->_nExecutionState == SATHTTP::INEXECUTED ) {
- return &(*it);
- }
- }
- return NULL;
- }
- SATHTTP::STTask* CSATExecutor::GetFreeTask()
- {
- std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
- for ( ; it != m_vtTask.end(); it++ ) {
- // 未执行的任务;
- if ( it->_nExecutionState == SATHTTP::UNEXECUTED ) {
- return &(*it);
- }
- }
- return NULL;
- }
- void CSATExecutor::DelFinishedTask()
- {
- AutoThreadSection ats(&m_csTask);
- std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
- for ( ; it != m_vtTask.end(); it++ ) {
- // 状态为2的完成任务;
- if ( it->_nExecutionState == SATHTTP::EXECUTED ) {
- // 删除所有执行器对象;
- CPythonExecutor *pExecutor = NULL;
- for (std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin(); _case != it->Job.vtCases.end(); _case++ ) {
- pExecutor = (CPythonExecutor *)_case->_pExcutor;
- if ( pExecutor ) {
- // 结束所有工作线程;
- pExecutor->EndThread();
- // 释放内存;
- delete pExecutor;
- pExecutor = NULL;
- }
- }
- // 删除任务前,变更设备状态为空闲;
- SetDeviceStatus(it->Job.strDeviceId, SATDEV::Idle);
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "\n<===============================================>\n"
- "end.删除已完成任务:%s\n<===============================================>\n", it->Job.strUniqueId.c_str());
- // 删除任务;
- if ( m_vtTask.size() > 1 )
- m_vtTask.erase(it);
- break;
- }
- }
- }
- SATHTTP::STCase* CSATExecutor::IsCaseScriptProcess(std::vector<SATHTTP::STCase> &vtCases)
- {
- std::vector<SATHTTP::STCase>::iterator it = vtCases.begin();
- for ( ; it != vtCases.end(); it++) {
- // 执行中的脚本;
- if ( it->_nExecutionState == SATHTTP::INEXECUTED ) {
- return &(*it);
- }
- }
- return NULL;
- }
- SATHTTP::STCase* CSATExecutor::GetFreeCaseScript(std::vector<SATHTTP::STCase> &vtCases)
- {
- std::vector<SATHTTP::STCase>::iterator it = vtCases.begin();
- for ( ; it != vtCases.end(); it++) {
- // 未执行的脚本;
- if ( it->_nExecutionState == SATHTTP::UNEXECUTED ) {
- return &(*it);
- }
- }
- return NULL;
- }
- SATHTTP::STCase* CSATExecutor::ExecuteFreeCaseScript(SATHTTP::STTask* pTask)
- {
- // 获取未执行的脚本;
- SATHTTP::STCase* pCase = GetFreeCaseScript(pTask->Job.vtCases);
- if (pCase) {
- #if 1 // 额外追加代码:清除之前的xml文件;
- #if 0
- std::string xmlpath = GLOBAL::g_stSATConfig.szCaseResultDir;
- #else
- std::string xmlpath = pCase->_strFileDir;
- #endif
- xmlpath.append("detail\\");
- xmlpath.append(pCase->_strFileName);
- xmlpath.append("_result.xml");
- // 删除文件;
- DeleteFile(xmlpath.c_str());
- #endif
- if ( !pCase->_pExcutor ) {
- CPythonExecutor *pExcutor = new CPythonExecutor();
- if ( pExcutor ) {
- pCase->_pExcutor = pExcutor;
- // 用例的日志文件路径;
- pCase->_strCaseLog = pCase->_strFileDir + pCase->_strFileName + ".txt";
- if ( pExcutor->InitScript(pCase->_strScriptPath, pCase->_strCaseLog, "") ) {
- // 设置用例对象;
- pExcutor->SetCaseObje(pCase);
- pExcutor->StartScript();
- }
- // 标记用例执行中;
- pCase->_nExecutionState = SATHTTP::INEXECUTED;
- // 标记任务为执行中;
- pTask->_nExecutionState = SATHTTP::INEXECUTED;
- // 记录开始时间;
- pCase->_ulStartTickCount = GetTickCount64();
- pCase->_strStartTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "启动脚本(%s)", pCase->strCaseName.c_str());
- }
- }
- else {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "重新初始化脚本(%s)", pCase->strCaseName.c_str());
- // 重新初始化脚本;
- CPythonExecutor *pExcutor = (CPythonExecutor *)pCase->_pExcutor;
- if ( pExcutor->InitScript(pCase->_strScriptPath, pCase->_strCaseLog, "") ) {
- // 设置用例对象;
- pExcutor->SetCaseObje(pCase);
- pExcutor->StartScript();
- // 标记用例执行中;
- pCase->_nExecutionState = SATHTTP::INEXECUTED;
- }
- else { // 如果再次初始化失败,标记脚本失败;
- pCase->_nExecutionResult = SATHTTP::FAIL;
- }
- }
- }
- return pCase;
- }
- void CSATExecutor::AddDevices(const SATDEV::STDevice &stDevice)
- {
- if ( IsDeviceExist(stDevice.strName) )
- return;
- SATHTTP::STDevice http_dev;
- // 设备使用状态;
- if ( stDevice.nUsageState == SATDEV::Idle )
- http_dev.strStatus = "0";
- else if ( stDevice.nUsageState == SATDEV::InUse )
- http_dev.strStatus = "2"; // 注意避坑:0表示连接,即设备空闲;1表示设备断开;2表示设备繁忙
- // 设备序列号;
- http_dev.strDeviceSerial = stDevice.strName;
- // 余下变量固定值;
- http_dev.strComments = "";
- http_dev.strCPU = "";
- http_dev.strDeviceMac = "";
- http_dev.strElectric = "";
- http_dev.strHardwareVersion = "SAT-HV";
- http_dev.strLastJob = "";
- http_dev.strLastJobFinishTime = "";
- http_dev.strLastJobStartTime = "";
- http_dev.strLastTimeBreak = "";
- http_dev.strLastTimeConnected = "";
- http_dev.strManu = "";
- http_dev.strMemory = "";
- http_dev.strModel = "SATModel";
- http_dev.strPhoneNumber = "";
- http_dev.strSoftwareVersion = "0123456789";
- m_vtDevice.push_back(http_dev);
- }
- void CSATExecutor::DelDevices(const SATDEV::STDevice &stDevice)
- {
- std::vector<SATHTTP::STDevice>::iterator it = m_vtDevice.begin();
- for ( ; it != m_vtDevice.end(); it++ ) {
- if ( _tcsicmp(it->strDeviceSerial.c_str(), stDevice.strName.c_str()) == 0 ) {
- m_vtDevice.erase(it);
- break;
- }
- }
- }
- void CSATExecutor::SetDeviceStatus(std::string strDevName, SATDEV::DEVICE_USAGE_STATUS status)
- {
- std::vector<SATHTTP::STDevice>::iterator it = m_vtDevice.begin();
- for ( ; it != m_vtDevice.end(); it++ ) {
- if ( _tcsicmp(it->strDeviceSerial.c_str(), strDevName.c_str()) == 0 ) {
- if ( status == SATDEV::Idle )
- it->strStatus = "0";
- else if ( status == SATDEV::InUse )
- it->strStatus = "2"; // 注意避坑:0表示连接,即设备空闲;1表示设备断开;2表示设备繁忙
- break;
- }
- }
- // 同时,同步SATDevices的设备状态;
- CSATDevices::SetDeviceUsageStatus(strDevName, status);
- }
- bool CSATExecutor::IsDeviceExist(std::string strDevName)
- {
- bool bExist = false;
- std::vector<SATHTTP::STDevice>::iterator it = m_vtDevice.begin();
- for ( ; it != m_vtDevice.end(); it++ ) {
- if ( _tcsicmp(it->strDeviceSerial.c_str(), strDevName.c_str()) == 0 ) {
- bExist = true;
- break;
- }
- }
- return bExist;
- }
- bool CSATExecutor::Login(std::string user, std::string password, std::string actuator, bool bLogin /*= true*/)
- {
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!executeLogin.action");
- // 填充数据;
- m_stLoginReq.strUserName = user;
- m_stLoginReq.strPassword = password;
- m_stLoginReq.strStatus = bLogin ? "0" : "1"; // 0表示登录;
- m_stLoginReq.strDeleteStatus = "";
- m_stLoginReq.strIP = GLOBAL::g_strIPAddress;
- m_stLoginReq.strStorage = "";
- if ( bLogin ) {
- m_stLoginReq.strDisconnectTime = "";
- m_stLoginReq.strConnectTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
- } else {
- m_stLoginReq.strConnectTime = "";
- m_stLoginReq.strDisconnectTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
- }
- // 执行器名称;
- m_stLoginReq.strExecuteName = m_strActuatorName = actuator;
- // 执行器PC物理地址;
- m_stLoginReq.strMAC = GLOBAL::g_strMacs;
- m_stLoginReq.strCPU = "";
- if ( ::Login(url, m_stLoginReq, m_stLoginResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "登录成功");
- m_bLogin = TRUE;
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "登录失败");
- return false;
- }
- bool CSATExecutor::Logout(std::string user, std::string password)
- {
- return Login(user, password, false);
- }
- bool CSATExecutor::UpdateDevice()
- {
- SATHTTP::STUpdateDeviceReq stUpdateDeviceReq;
- SATHTTP::STUpdateDeviceResp stUpdateDeviceResp;
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!updateDeviceMessage.action");
- // 这个状态值要怎么设置?// 此处统一为0;
- stUpdateDeviceReq.strStatus = "0";
- stUpdateDeviceReq.strUserName = m_stLoginReq.strUserName;
- stUpdateDeviceReq.strIP = GLOBAL::g_strIPAddress;
- stUpdateDeviceReq.strStorage = "";
- stUpdateDeviceReq.strRunnerName = m_strActuatorName;
- // 这里使用PC物理地址;
- stUpdateDeviceReq.strMAC = GLOBAL::g_strMacs;
- // 这个reportType具体意义不明;
- stUpdateDeviceReq.strReportType = "1";
- stUpdateDeviceReq.strStartTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
- stUpdateDeviceReq.strMemory = "";
- stUpdateDeviceReq.strEndTime = "";
- stUpdateDeviceReq.strCPU = "";
- stUpdateDeviceReq.devicelist.assign(m_vtDevice.begin(), m_vtDevice.end());
- if ( !::UpdateDeviceMessage(url, stUpdateDeviceReq, stUpdateDeviceResp) )
- {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "更新设备信息失败");
- return false;
- }
-
- return true;
- }
- bool CSATExecutor::NotifyTaskStart(SATHTTP::STTask* pTask)
- {
- if ( !pTask )
- return false;
- SATHTTP::STNotifyJobStartReq stNotifyJobStartReq;
- SATHTTP::STNotifyJobStartResp stNotifyJobStartResp;
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!setResult.action");
- TCHAR szValue[36] = {0};
- _itoa_s(pTask->nDeviceId, szValue, 10);
- stNotifyJobStartReq.strDeviceId = szValue;
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stNotifyJobStartReq.strExecuteId = szValue;
- // _itoa_s(stTask.nInstanceId, szValue, 10); // 误导:应该使用id而不是nInstanceId
- _itoa_s(pTask->Id, szValue, 10);
- stNotifyJobStartReq.strInstanceId = szValue;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stNotifyJobStartReq.strTaskId = pTask->Job.strTaskId;
- // 这个映像路径有何用?
- stNotifyJobStartReq.strSignalImageUrl = GLOBAL::g_stSATConfig.szScriptDir;
- stNotifyJobStartReq.strSignalImageUrl.append(pTask->strTaskName+"\\\\");
- stNotifyJobStartReq.strSignalImageUrl.append(pTask->Job.strUniqueId+"\\\\");
- stNotifyJobStartReq.strSignalImageUrl.append(pTask->Job.strDeviceId);
- stNotifyJobStartReq.strSignalImageUrl = CharEncoding::ASCII2UTF8(stNotifyJobStartReq.strSignalImageUrl.c_str());
- if ( SetResult(url, stNotifyJobStartReq, stNotifyJobStartResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "更新任务状态成功:%s", pTask->Job.strUniqueId.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "更新任务状态失败:%s", pTask->Job.strUniqueId.c_str());
- return false;
- }
- bool CSATExecutor::UploadCaseImg(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase, std::string img)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!saveResultImg.action");
- SATHTTP::STSaveImgReq stSaveImgReq;
- SATHTTP::STSaveImgResp stSaveImgResp;
- stSaveImgReq.strCaseId = pCase->strId;
- stSaveImgReq.strCaseRepeat = "0";
- _itoa_s(pCase->_nCaseStep, szValue, 10);
- stSaveImgReq.strCaseStep = szValue;
- // 实例Id;
- _itoa_s(pTask->nDeviceId, szValue, 10);
- stSaveImgReq.strDeviceId = szValue;//pTask->Job.strDeviceId;
- // 就是Task中的ExecuteId
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stSaveImgReq.strExecuteId = szValue;
- // 实例Id;
- _itoa_s(pTask->nInstanceId, szValue, 10);
- stSaveImgReq.strInstanceId = szValue;
- stSaveImgReq.strJobRepeat = "0";
- // 注意避坑:roundnum必须赋值0或1;
- stSaveImgReq.strRoundNum = "0";
- stSaveImgReq.strImgType = "";
- stSaveImgReq.strUploads = img;
- // 上传用例图片;
- if ( SaveResultImg(url, stSaveImgReq, stSaveImgResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传图片成功:任务=%s, 用例=%s, 图片=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), img.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传图片失败:任务=%s, 用例=%s, 图片=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), img.c_str());
- return false;
- }
- bool CSATExecutor::UploadCaseLog(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!saveCaseOrTaskLog.action");
- SATHTTP::STSaveLogReq stSaveLogReq;
- SATHTTP::STSaveLogResp stSaveLogResp;
- stSaveLogReq.strCaseId = pCase->strId;
- // 执行ID;
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stSaveLogReq.strExecuteId = szValue;
- stSaveLogReq.strFileType = "caseLogFile";
- // 任务Id;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stSaveLogReq.strTaskId = pTask->Job.strTaskInstanceId;
- stSaveLogReq.strUserId = pTask->Job.strUserId;
- // 要上传的日志文件;
- stSaveLogReq.strUploads = pCase->_strCaseLog;
- if ( SaveCaseOrTaskLog(url, stSaveLogReq, stSaveLogResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传日志成功:任务=%s, 用例=%s, 日志=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), pCase->_strCaseLog.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传日志失败:任务=%s, 用例=%s, 日志=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str(), pCase->_strCaseLog.c_str());
- return false;
- }
- bool CSATExecutor::ReportCaseItemFinish(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase, STCaseItem &caseItem)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!setResultList.action");
- SATHTTP::STJobProcessReq stJobProcessReq;
- SATHTTP::STJobProcessResp stJobProcessResp;
- // 0 表示测试项Pass, 1 表示测试项Fail;
- stJobProcessReq.strResultState = caseItem.result ? "0" : "1";
- // 如果测试项失败,标记用例和任务失败;
- if ( !caseItem.result ) {
- pCase->_nExecutionResult = SATHTTP::FAIL;
- pTask->_nExecutionResult = SATHTTP::FAIL;
- }
- stJobProcessReq.strCaseScene = "";
- // 索引;
- _itoa_s(pCase->_nCaseStep, szValue, 10);
- stJobProcessReq.strCaseStep = szValue;
- stJobProcessReq.strApkMD5 = "";
- stJobProcessReq.strCrashTime = "";
- // 就是Task中的ExecuteId
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stJobProcessReq.strRunnerId = szValue;
- stJobProcessReq.strCPUInfo = "0";
- stJobProcessReq.strRunnedActionNameList = "";
- stJobProcessReq.strArtificialResult = "";
- stJobProcessReq.strArtificialModify = "";
- stJobProcessReq.strRunnerName = "";
- stJobProcessReq.strTaskType = "FUNCTIONALITY";
- stJobProcessReq.strCaseRepeat = "0";
- stJobProcessReq.strApplicationGroup = "";
- // 实例Id;
- _itoa_s(pTask->Id, szValue, 10);
- stJobProcessReq.strInstanceId = szValue;
- stJobProcessReq.strCaseId = pCase->strId;
- // 进度;
- _itoa_s(100*(atoi(pCase->strIndex.c_str()))/pTask->Job.vtCases.size(), szValue, 10);
- stJobProcessReq.strProgress = szValue;
- // 需要将utf-8转gbk;
- stJobProcessReq.strReusltMessage = caseItem.name;//CharEncoding::UTF82ASCII(caseItem.name.c_str());
- stJobProcessReq.strJobRepeat = "0";
- stJobProcessReq.strScreenShot = "";
- stJobProcessReq.strStartTime = pCase->_strStartTime;
- stJobProcessReq.strCrashNumber = "";
- stJobProcessReq.strCaseName = pCase->strCaseName;
- //stJobProcessReq.strFailedReason = CharEncoding::UTF82ASCII(caseItem.remark.c_str());
- //stJobProcessReq.strFailedReason = (char*)CharEncoding::UTF82UNICODE(caseItem.remark.c_str());
- stJobProcessReq.strFailedReason = caseItem.remark;
- for (std::vector<std::string>::iterator it = caseItem.imgs.begin(); it != caseItem.imgs.end(); it++ ) {
- int npos = it->find_last_of('/');
- if ( std::string::npos != npos ) {
- stJobProcessReq.strImgName.append(it->substr(npos+1));
- stJobProcessReq.strImgName.append("&");
- }
- }
- if ( stJobProcessReq.strImgName.size() && stJobProcessReq.strImgName.at(stJobProcessReq.strImgName.size()-1) == '&')
- stJobProcessReq.strImgName.erase(stJobProcessReq.strImgName.size()-1);
- stJobProcessReq.strCaseIndex = pCase->strIndex;
- // 实例Id;
- _itoa_s(pTask->nDeviceId, szValue, 10);
- stJobProcessReq.strDeviceId = szValue;
- stJobProcessReq.strSceneIndex = "0";
- // 实例Id;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stJobProcessReq.strTaskId = pTask->Job.strTaskId;
- //stJobProcessReq.strAnalysis = CharEncoding::UTF82ASCII(caseItem.data.c_str());
- stJobProcessReq.strAnalysis = caseItem.data.c_str();
- // 设备名称:即DeviceSerial;
- stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名及其不范围,一会deviceid是deviceserial,一会是id;
- // 固定为:TOTAL
- stJobProcessReq.strInfoType = "";
- // 如果是Android设备,需要通过adb获取;
- //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
- stJobProcessReq.strEndTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
- stJobProcessReq.strRoundNumber = "1";
- stJobProcessReq.strResultType = "4"; // reportActionFinish;
- stJobProcessReq.strOperationStep = "";
- if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传【用例测试项】结果完成【成功】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
- // 再上传图片;
- std::string img;
- for (std::vector<std::string>::iterator it = caseItem.imgs.begin(); it != caseItem.imgs.end(); it++ ) {
- #if 0
- img = GLOBAL::g_stSATConfig.szCaseResultDir;
- #else
- // 使用脚本路径;
- img = pCase->_strFileDir;
- #endif
- img.append(it->substr(3));
- img = GLOBAL::Replace(img, "/", "\\");
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "图片路径=%s", img.c_str());
- UploadCaseImg(pTask, pCase, img);
- }
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传【用例测试项】结果完成【失败】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
- return false;
- }
- bool CSATExecutor::ReportCaseFinish(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase, std::string strRunnedActionNameList)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!setResultList.action");
- SATHTTP::STJobProcessReq stJobProcessReq;
- SATHTTP::STJobProcessResp stJobProcessResp;
- // 上报用例结果:0表示脚本成功执行, 1表示脚本出错或超时;
- if ( pCase->_nExecutionState == SATHTTP::EXECUTED ) {
- if ( pCase->_nExecutionResult == SATHTTP::SUCCESS ) {
- stJobProcessReq.strResultState = "0";
- stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本执行成功");
- }
- else {
- if ( pCase->_nExecutionResult == SATHTTP::ABNORMAL )
- stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本执行异常");
- else
- stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本执行失败");
- stJobProcessReq.strResultState = "1";
- // 同时标记任务失败;
- pTask->_nExecutionResult = SATHTTP::FAIL;
- }
- }
- else {
- stJobProcessReq.strResultState = "1"; // 脚本执行未完成,认为fail;
- // 同时标记任务失败;
- pTask->_nExecutionResult = SATHTTP::FAIL;
- stJobProcessReq.strFailedReason = CharEncoding::ASCII2UTF8("脚本未执行");
- }
-
- stJobProcessReq.strCaseScene = "";
- // 索引;
- _itoa_s(pCase->_nCaseStep, szValue, 10);
- stJobProcessReq.strCaseStep = szValue;
- stJobProcessReq.strApkMD5 = "";
- stJobProcessReq.strCrashTime = "";
- // 就是Task中的ExecuteId
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stJobProcessReq.strRunnerId = szValue;
- stJobProcessReq.strCPUInfo = "0";
- stJobProcessReq.strRunnedActionNameList = strRunnedActionNameList;
- stJobProcessReq.strArtificialResult = "";
- stJobProcessReq.strArtificialModify = "";
- stJobProcessReq.strRunnerName = "";
- stJobProcessReq.strTaskType = "FUNCTIONALITY";
- stJobProcessReq.strCaseRepeat = "";
- stJobProcessReq.strApplicationGroup = "";
- // 实例Id;注意避坑;
- _itoa_s(pTask->Id, szValue, 10);
- stJobProcessReq.strInstanceId = szValue;
- stJobProcessReq.strCaseId = pCase->strId;
- // 进度(###需要修改此处###);
- _itoa_s(100*(atoi(pCase->strIndex.c_str()))/pTask->Job.vtCases.size(), szValue, 10);
- stJobProcessReq.strProgress = szValue;
- // 需要将utf-8转gbk;
- stJobProcessReq.strReusltMessage = CharEncoding::ASCII2UTF8("测试用例结果:");
- stJobProcessReq.strJobRepeat = "";
- stJobProcessReq.strScreenShot = "";
- stJobProcessReq.strStartTime = pCase->_strStartTime;
- stJobProcessReq.strCrashNumber = "";
- stJobProcessReq.strCaseName = pCase->strCaseName;
-
- stJobProcessReq.strImgName = "";
- stJobProcessReq.strCaseIndex = pCase->strIndex;
- // 实例Id;
- _itoa_s(pTask->nDeviceId, szValue, 10);
- stJobProcessReq.strDeviceId = szValue;
- stJobProcessReq.strSceneIndex = "0";
- // 实例Id;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stJobProcessReq.strTaskId = pTask->Job.strTaskId;
- stJobProcessReq.strAnalysis = "";
- // 设备名称:即DeviceSerial;
- stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名极其不范围,一会deviceid是deviceserial,一会是id;
- // 固定为:TOTAL
- stJobProcessReq.strInfoType = "TOTAL";
- // 如果是Android设备,需要通过adb获取;
- //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
- stJobProcessReq.strEndTime = pCase->_strEndTime;//CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
- stJobProcessReq.strRoundNumber = "1";
- stJobProcessReq.strResultType = "5"; // reportCaseFinish;
- stJobProcessReq.strOperationStep = "";
- if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传用例结果完成【成功】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "上传用例结果完成【失败】:任务=%s, 用例=%s", pTask->Job.strUniqueId.c_str(), pCase->strCaseName.c_str());
- return false;
- }
- bool CSATExecutor::ReportCaseResult(SATHTTP::STTask* pTask, SATHTTP::STCase *pCase)
- {
- // 获取xml文件内容;
- #if 0
- std::string xmlpath = GLOBAL::g_stSATConfig.szCaseResultDir;
- #else
- // 使用脚本自身路径作为根目录;
- std::string xmlpath = pCase->_strFileDir;
- #endif
- xmlpath.append("detail\\");
- xmlpath.append(pCase->_strFileName);
- #if 0
- // 文件名加下PID做为后缀;
- if ( pCase->__dwPythonPID > 0)
- {
- TCHAR szPID[16] = "_";
- _ltoa_s(pCase->__dwPythonPID, &szPID[1], 15, 10);
- xmlpath.append(szPID);
- }
- #endif
- xmlpath.append("_result.xml");
- std::vector<STCaseItem> vtCaseItem;
- if ( GetCaseXMLResult(xmlpath, vtCaseItem) )
- pCase->_nExecutionResult = SATHTTP::SUCCESS;
- // 如果测试项空,任务失败;
- if ( vtCaseItem.size() == 0 ) {
- // 将默认成功值改为异常;
- if ( pCase->_nExecutionResult == SATHTTP::SUCCESS )
- pCase->_nExecutionResult = SATHTTP::ABNORMAL;
- // 同时标记任务失败;
- pTask->_nExecutionResult = SATHTTP::FAIL;
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("测试项空,用例名:%s"), pCase->strCaseName.c_str());
- }
-
- int nIndex = 1;
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!setResultList.action");
- SATHTTP::STJobProcessReq stJobProcessReq;
- SATHTTP::STJobProcessResp stJobProcessResp;
- // 遍历所有用例测试项;
- std::string strRunnedActionNameList;
- std::vector<STCaseItem>::iterator it = vtCaseItem.begin();
- for (; it != vtCaseItem.end();) {
- // 上报测试项结果(包含相片);
- ReportCaseItemFinish(pTask, pCase, *it);
- strRunnedActionNameList.append(it->name);
- if ( ++it != vtCaseItem.end() )
- strRunnedActionNameList.append("&");
- }
- // 上报用例完成;
- ReportCaseFinish(pTask, pCase, strRunnedActionNameList);
- // 上传用例日志;
- UploadCaseLog(pTask, pCase);
- // 删除执行对象;
- if ( pCase->_pExcutor )
- delete pCase->_pExcutor;
- pCase->_pExcutor = NULL;
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("用例已完成,执行器对象删除成功,脚本用例名为=%s"), pCase->strCaseName.c_str());
- return true;
- }
- bool CSATExecutor::UploadTaskLog(SATHTTP::STTask* pTask)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!saveCaseOrTaskLog.action");
- SATHTTP::STSaveLogReq stSaveLogReq;
- SATHTTP::STSaveLogResp stSaveLogResp;
- stSaveLogReq.strCaseId = "";
- // 执行ID;
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stSaveLogReq.strExecuteId = szValue;
- stSaveLogReq.strFileType = "taskLogFile";
- // 任务Id;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stSaveLogReq.strTaskId = pTask->Job.strTaskInstanceId;
- stSaveLogReq.strUserId = pTask->Job.strUserId;
- // 要上传的日志文件;
- stSaveLogReq.strUploads = "D:\\sat\\log.txt";//pTask->_strTaskLog;
- if ( SaveCaseOrTaskLog(url, stSaveLogReq, stSaveLogResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("任务已完成,上传任务日志【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("任务已完成,上传任务日志【失败】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
- return false;
- }
- bool CSATExecutor::ReportTaskStart(SATHTTP::STTask* pTask)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!setResultList.action");
- SATHTTP::STJobProcessReq stJobProcessReq;
- SATHTTP::STJobProcessResp stJobProcessResp;
- // 需要处理###
- stJobProcessReq.strResultState = ""; // 脚本执行中,认为fail;
- stJobProcessReq.strCaseScene = "";
- // 索引;
- stJobProcessReq.strCaseStep = "0";
- stJobProcessReq.strApkMD5 = "";
- stJobProcessReq.strCrashTime = "";
- // 就是Task中的ExecuteId
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stJobProcessReq.strRunnerId = szValue;
- stJobProcessReq.strCPUInfo = "0";
- stJobProcessReq.strRunnedActionNameList = "";
- stJobProcessReq.strArtificialResult = "";
- stJobProcessReq.strArtificialModify = "";
- stJobProcessReq.strRunnerName = "";
- stJobProcessReq.strTaskType = "FUNCTIONALITY";
- stJobProcessReq.strCaseRepeat = "";
- stJobProcessReq.strApplicationGroup = "";
- // 实例Id;注意避坑;
- _itoa_s(pTask->Id, szValue, 10);
- stJobProcessReq.strInstanceId = szValue;
- stJobProcessReq.strCaseId = "";
- // 进度(###需要修改此处###);
- stJobProcessReq.strProgress = "0";
- // 需要将utf-8转gbk;
- stJobProcessReq.strReusltMessage = CharEncoding::ASCII2UTF8("任务开始");
- stJobProcessReq.strJobRepeat = "";
- stJobProcessReq.strScreenShot = "";
- stJobProcessReq.strStartTime = pTask->strStartTime;
- stJobProcessReq.strCrashNumber = "";
- stJobProcessReq.strCaseName = "";
- stJobProcessReq.strFailedReason = "";
- stJobProcessReq.strImgName = "";
- stJobProcessReq.strCaseIndex = "";
- // 实例Id;
- _itoa_s(pTask->nDeviceId, szValue, 10);
- stJobProcessReq.strDeviceId = szValue;
- stJobProcessReq.strSceneIndex = "";
- // 实例Id;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stJobProcessReq.strTaskId = pTask->Job.strTaskId;
- stJobProcessReq.strAnalysis = "";
- // 设备名称:即DeviceSerial;
- stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名及其不范围,一会deviceid是deviceserial,一会是id;
- // 固定为:TOTAL
- stJobProcessReq.strInfoType = "TOTAL";
- // 如果是Android设备,需要通过adb获取;
- //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
- stJobProcessReq.strEndTime = "";// CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
- stJobProcessReq.strRoundNumber = "1";
- stJobProcessReq.strResultType = "0"; // reportJobStart;
- stJobProcessReq.strOperationStep = "";
- if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务开始【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务开始【失败】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
- return false;
- }
- bool CSATExecutor::ReportTaskFinish(SATHTTP::STTask* pTask)
- {
- TCHAR szValue[MAX_PATH] = {0};
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!setResultList.action");
- SATHTTP::STJobProcessReq stJobProcessReq;
- SATHTTP::STJobProcessResp stJobProcessResp;
- // 需要处理(只要有用例失败,任务就失败)
- if ( pTask->_nExecutionState == SATHTTP::EXECUTED ) {
- if ( pTask->_nExecutionResult == SATHTTP::SUCCESS )
- stJobProcessReq.strResultState = "0"; // 脚本成功;
- else
- stJobProcessReq.strResultState = "1"; // 脚本失败;
- }
- else
- stJobProcessReq.strResultState = "1"; // 脚本失败;
- stJobProcessReq.strCaseScene = "";
- // 索引;
- stJobProcessReq.strCaseStep = "0";
- stJobProcessReq.strApkMD5 = "";
- stJobProcessReq.strCrashTime = "";
- // 就是Task中的ExecuteId
- _itoa_s(pTask->nExecuteId, szValue, 10);
- stJobProcessReq.strRunnerId = szValue;
- stJobProcessReq.strCPUInfo = "0";
- stJobProcessReq.strRunnedActionNameList = "";
- stJobProcessReq.strArtificialResult = "";
- stJobProcessReq.strArtificialModify = "";
- stJobProcessReq.strRunnerName = "";
- stJobProcessReq.strTaskType = "FUNCTIONALITY";
- stJobProcessReq.strCaseRepeat = "0";
- stJobProcessReq.strApplicationGroup = "";
- // 实例Id;注意避坑;
- _itoa_s(pTask->Id, szValue, 10);
- stJobProcessReq.strInstanceId = szValue;
- stJobProcessReq.strCaseId = "";
- // 进度(###需要修改此处###);
- stJobProcessReq.strProgress = "100";
- // 需要将utf-8转gbk;
- stJobProcessReq.strReusltMessage = CharEncoding::ASCII2UTF8("任务结束");
- stJobProcessReq.strJobRepeat = "0";
- stJobProcessReq.strScreenShot = "";
- // 注意避坑:实际结束时间也要放在该字段中;
- stJobProcessReq.strStartTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
- stJobProcessReq.strCrashNumber = "";
- stJobProcessReq.strCaseName = "";
- stJobProcessReq.strFailedReason = "";
- stJobProcessReq.strImgName = "";
- stJobProcessReq.strCaseIndex = "";
- // 实例Id;
- _itoa_s(pTask->nDeviceId, szValue, 10);
- stJobProcessReq.strDeviceId = szValue;
- stJobProcessReq.strSceneIndex = "0";
- // 实例Id;
- //_itoa_s(pTask->nTaskId, szValue, 10);
- stJobProcessReq.strTaskId = pTask->Job.strTaskId;
- stJobProcessReq.strAnalysis = "";
- // 设备名称:即DeviceSerial;
- stJobProcessReq.strDevnceName = pTask->Job.strDeviceId; // 命名及其不范围,一会deviceid是deviceserial,一会是id;
- // 固定为:TOTAL
- stJobProcessReq.strInfoType = "";
- // 如果是Android设备,需要通过adb获取;
- //stJobProcessReq.strMemoryInfo = stDevice.strMemory;
- stJobProcessReq.strEndTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));//"2019-12-16 10:18:20";
- stJobProcessReq.strRoundNumber = "1";
- stJobProcessReq.strResultType = "1"; // reportJobFinish;
- stJobProcessReq.strOperationStep = "";
- if ( SetResultList(url, stJobProcessReq, stJobProcessResp) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务结束【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
- return true;
- }
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("上报任务结束【成功】,任务编号为=%s"), pTask->Job.strUniqueId.c_str());
- return false;
- }
- bool CSATExecutor::GetCaseXMLResult(std::string xmlpath, std::vector<STCaseItem> &vtCaseItem)
- {
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.LoadFile(xmlpath.c_str()) ) {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "没有测试项对应的xml文件:%s", xmlpath.c_str());
- return false;
- }
- tinyxml2::XMLElement *pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) == NULL)
- return false;
-
- if (_tcsicmp(pXmlRoot->Value(), "results") != 0)
- return false;
- // 获取属性;
- bool bFinishStatus = false;
- const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
- while (pAttr) {
- if (_tcsicmp(pAttr->Name(), "caseFinish") == 0) {
- // 整个测试项ok;
- if ( _tcsicmp(pAttr->Value(), _T("1")) == 0 )
- bFinishStatus = true;
- break;
- }
- pAttr = pAttr->Next();
- }
-
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "开始获取测试项内容:%s", xmlpath.c_str());
- tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
- while (pXmlElent) {
- if (_tcsicmp(pXmlElent->Value(), _T("item")) == 0) {
- STCaseItem cItem;
- tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
- while (pItem) {
- if (_tcsicmp(pItem->Value(), _T("name")) == 0)
- {
- cItem.name = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("result")) == 0)
- {
- if ( _tcsicmp(pItem->GetText(), _T("Pass")) == 0 )
- cItem.result = true;
- else
- cItem.result = false;
- }
- else if (_tcsicmp(pItem->Value(), _T("data")) == 0)
- {
- cItem.data = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("log")) == 0)
- {
- cItem.log = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("remark")) == 0)
- {
- cItem.remark = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("screen")) == 0)
- {
- cItem.imgs.push_back(pItem->GetText());
- }
- pItem = pItem->NextSiblingElement();
- }
- // 压入容器;
- if ( cItem.name.size() )
- vtCaseItem.push_back(cItem);
- }
- pXmlElent = pXmlElent->NextSiblingElement();
- }
-
- return bFinishStatus;
- }
- int CSATExecutor::AttachTaskInfo2Buffer(SATPROTO::TaskInfo (&pbuff)[SATPROTO::MAX_TASKS])
- {
- int count = 0;
- if ( pbuff ) {
- AutoThreadSection ats(&m_csTask);
- std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin();
- for ( ;it != m_vtTask.end(); it++ ) {
- // 任务状态;
- //pbuff[count].nStatus = it->_nExecutionState;
- // 任务id;
- pbuff[count].nTaskId = it->Id;
- // 任务编号;
- memcpy_s(pbuff[count].szTaskNo, SATPROTO::MAX_NAME, it->Job.strUniqueId.c_str(), it->Job.strUniqueId.size());
- // 任务名称;
- memcpy_s(pbuff[count].szTaskName, SATPROTO::MAX_NAME, it->strTaskName.c_str(), it->strTaskName.size());
- // 任务状态;
- if ( it->_nExecutionState == SATHTTP::UNEXECUTED )
- {
- _stprintf_s(pbuff[count].szStatus, SATPROTO::MAX_STATUS, _T("未执行"));
- }
- else if (it->_nExecutionState == SATHTTP::INEXECUTED)
- {
- _stprintf_s(pbuff[count].szStatus, SATPROTO::MAX_STATUS, _T("执行中"));
- }
- else if (it->_nExecutionState == SATHTTP::EXECUTED)
- {
- _stprintf_s(pbuff[count].szStatus, SATPROTO::MAX_STATUS, _T("已执行"));
- }
- int nIndex = 0;
- // 用例数量;
- pbuff[count].nCaseSize = it->Job.vtCases.size();
- for (std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin(); _case != it->Job.vtCases.end(); _case++) {
- // 用例状态;
- // pbuff[count].ssCases[nIndex].nStatus = _case->_nExecutionState;
- //pbuff[count].ssCases[nIndex].nCaseId = _case->_nCaseStep;
- // 用例名称;
- memcpy_s(pbuff[count].ssCases[nIndex].szCaseName, SATPROTO::MAX_NAME, _case->strCaseName.c_str(), _case->strCaseName.size());
- // 用例日志路径;
- memcpy_s(pbuff[count].ssCases[nIndex].szCaseLogPath, MAX_PATH, _case->_strCaseLog.c_str(), _case->_strCaseLog.size());
- // 用例执行状态;
- if (_case->_nExecutionState == SATHTTP::UNEXECUTED)
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szExecStatus, SATPROTO::MAX_STATUS, _T("未执行"));
- }
- else if (_case->_nExecutionState == SATHTTP::INEXECUTED)
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szExecStatus, SATPROTO::MAX_STATUS, _T("执行中"));
- }
- else if (_case->_nExecutionState == SATHTTP::EXECUTED)
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szExecStatus, SATPROTO::MAX_STATUS, _T("已执行"));
- }
- // 用例执行结果;
- if ( _case->_nExecutionResult == SATHTTP::NONE )
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("无"));
- }
- else if ( _case->_nExecutionResult == SATHTTP::SUCCESS )
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("成功"));
- }
- else if ( _case->_nExecutionResult == SATHTTP::FAIL )
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("失败"));
- }
- else if ( _case->_nExecutionResult == SATHTTP::ABNORMAL )
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("异常"));
- }
- else if ( _case->_nExecutionResult == SATHTTP::OVERTIME )
- {
- _stprintf_s(pbuff[count].ssCases[nIndex].szResultStatus, SATPROTO::MAX_STATUS, _T("超时"));
- }
- nIndex++;
- // 超过MAX_CASES退出;
- if ( nIndex == SATPROTO::MAX_CASES )
- break;
- }
- count++;
- // 超过MAX_TASKS退出;
- if ( count == SATPROTO::MAX_TASKS )
- break;
- }
- }
- return count;
- }
- void CSATExecutor::StartWork()
- {
- m_hEventHearbeat = CreateEvent(NULL, TRUE, FALSE, NULL);
- if ( m_hEventHearbeat == NULL ) {
- _tprintf_s(_T("创建事件失败\n"));
- return;
- }
- m_hEventExcuteScript = CreateEvent(NULL, TRUE, FALSE, NULL);
- if ( m_hEventExcuteScript == NULL ) {
- _tprintf_s(_T("创建事件失败2\n"));
- return;
- }
- m_hThreadHearbeat = CreateThread(NULL, 0, HearbeatThread, this, 0, NULL);
- if ( m_hThreadHearbeat == NULL) {
- SetEvent(m_hEventHearbeat);
- SetEvent(m_hEventExcuteScript);
- CloseHandle(m_hEventHearbeat);
- CloseHandle(m_hEventExcuteScript);
- m_hEventHearbeat = NULL;
- m_hEventExcuteScript = NULL;
- _tprintf_s(_T("创建线程失败\n"));
- return;
- }
- m_hThreadExcuteScript = CreateThread(NULL, 0, ExecuteScriptThread, this, 0, NULL);
- if ( m_hThreadExcuteScript == NULL ) {
- SetEvent(m_hEventHearbeat);
- SetEvent(m_hEventExcuteScript);
- CloseHandle(m_hEventHearbeat);
- CloseHandle(m_hEventExcuteScript);
- m_hEventHearbeat = NULL;
- m_hEventExcuteScript = NULL;
- WaitForSingleObject(m_hThreadHearbeat,INFINITE);
- if (m_hThreadHearbeat)
- CloseHandle(m_hThreadHearbeat);
- m_hThreadHearbeat = NULL;
- _tprintf_s(_T("创建线程失败2\n"));
- return;
- }
- }
- void CSATExecutor::EndofWork()
- {
- //////////////////////////////////////////////////////////////////////////
- // 设置事件有信号;
- if ( m_hEventHearbeat )
- SetEvent(m_hEventHearbeat);
- // 等待线程结束;
- if ( m_hThreadHearbeat ) {
- WaitForSingleObject(m_hThreadHearbeat, INFINITE);
- CloseHandle(m_hThreadHearbeat);
- m_hThreadHearbeat = NULL;
- }
- // 关闭事件句柄;
- if ( m_hEventHearbeat ) {
- CloseHandle(m_hEventHearbeat);
- m_hEventHearbeat = NULL;
- }
- //////////////////////////////////////////////////////////////////////////
- // 设置事件有信号;
- if ( m_hEventExcuteScript )
- SetEvent(m_hEventExcuteScript);
- // 等待线程结束;
- if ( m_hThreadExcuteScript ) {
- WaitForSingleObject(m_hThreadExcuteScript, INFINITE);
- CloseHandle(m_hThreadExcuteScript);
- m_hThreadExcuteScript = NULL;
- }
- // 关闭事件句柄;
- if ( m_hEventExcuteScript ) {
- CloseHandle(m_hEventExcuteScript);
- m_hEventExcuteScript = NULL;
- }
- // 如果有脚本在执行,结束脚本;
- CPythonExecutor *pExecutor = NULL;
- for ( std::list<SATHTTP::STTask>::iterator it = m_vtTask.begin(); it != m_vtTask.end(); it++ ) {
- for ( std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin(); _case != it->Job.vtCases.end(); _case++ ) {
- if ( (pExecutor = (CPythonExecutor *)_case->_pExcutor) ) {
- pExecutor->EndThread();
- delete pExecutor;
- pExecutor = NULL;
- }
- }
- }
- m_vtTask.clear();
- }
- DWORD CSATExecutor::HearbeatThread(LPVOID lpVoid)
- {
- CSATExecutor *that = (CSATExecutor*)lpVoid;
- if ( !that ) return 0;
-
- do {
- // 如果未启用电视监听,启用实时读取配置文件;
- if ( that->m_bStopWathTV ) {
- GLOBAL::GetIniInfo();
- }
- // 没有登录成功,不查询;
- if ( !that->m_bLogin ) continue;
- // 更新设备;
- that->UpdateDevice();
- // 脚本保存目录;
- std::string strScriptSaveDir;
- SATHTTP::STHeartbeatReq stHeartbeatReq;
- SATHTTP::STHeartbeatResp stHeartbeatResp;
- std::string url = GLOBAL::g_stSATConfig.szExecuteServer;
- url.append("/ajaxInteractiveManage!executeHeartbeat.action");
- stHeartbeatReq.strUserName = that->m_stLoginReq.strUserName;
- stHeartbeatReq.strRunnerMac = that->m_stLoginReq.strMAC;
- stHeartbeatReq.devicelist.assign(that->m_vtDevice.begin(), that->m_vtDevice.end());
- if ( Heartbeat(url, stHeartbeatReq, stHeartbeatResp) ) {
- std::vector<SATHTTP::STTask>::iterator it = stHeartbeatResp.vtTask.begin();
- for ( ; it != stHeartbeatResp.vtTask.end(); it++ ) {
- if (!that->IsTaskExist(*it)) {
- // 将任务压入队列中;
- AutoThreadSection ats(&that->m_csTask);
- // 同时下载任务;
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("【任务脚本下载】:%s"), it->Job.strUniqueId.c_str());
- std::string host = GLOBAL::g_stSATConfig.szCaseServer;
- std::string url = host + "/ajaxInteractiveManage!getCaseFileListUrl.action";
- std::vector<SATHTTP::STCase>::iterator _case = it->Job.vtCases.begin();
- for ( int i = 1; _case != it->Job.vtCases.end(); _case++) {
- // 下载脚本;
- _case->_nCaseStep = i++;
- SATHTTP::STScriptUrlResp stScriptUrlResp;
- // 脚本保存路径;
- strScriptSaveDir = GLOBAL::g_stSATConfig.szScriptDir;
- strScriptSaveDir.append(it->Job.strProjectName+"\\");
- strScriptSaveDir.append(it->Job.strUniqueId+"\\");
- // 把冒号改成下划线;
- strScriptSaveDir.append(GLOBAL::Replace(it->Job.strDeviceId, "_", ":")+"\\");
- if ( DownloadScript(url, _case->strId, strScriptSaveDir, stScriptUrlResp) ) {
- _case->_strFileDir = stScriptUrlResp._strFileDir;
- _case->_strFileName = stScriptUrlResp._strFileName;
- _case->_strScriptPath = stScriptUrlResp._strScripFile;
- } else {
- // 脚本下载失败时,需要标记脚本为已运行且异常;
- _case->_nExecutionState = SATHTTP::EXECUTED;
- _case->_nExecutionResult = SATHTTP::ABNORMAL;
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("【下载脚本失败】:%s"), _case->strCaseName.c_str());
- }
- }
- that->m_vtTask.push_back(*it);
-
- // 通知SAT服务器,脚本开始执行;
- // 待开发:同时将任务存储到数据库中;
- /*
- db process
- */
- }
- // 进行一次删除操作;
- that->DelFinishedTask();
- }
- }
- } while ( WaitForSingleObject(that->m_hEventHearbeat, 10000) == WAIT_TIMEOUT );
- return 0;
- }
- DWORD CSATExecutor::ExecuteScriptThread(LPVOID lpVoid)
- {
- CSATExecutor *that = (CSATExecutor*)lpVoid;
- if ( !that ) return 0;
- do {
- // 删除已完成的任务;
- that->DelFinishedTask();
- // 是否有任务在执行;
- SATHTTP::STTask *pTask = that->IsThereATaskInProcess();
- if ( pTask ) {// 有任务在执行中;
- if ( pTask->_bConcurrent ) {// 并发;
- }
- else {// 串行;
- // 是否有脚本用例在执行;
- SATHTTP::STCase* pCase = that->IsCaseScriptProcess(pTask->Job.vtCases);
- if ( pCase ) {
- CPythonExecutor *pExcutor = (CPythonExecutor*)pCase->_pExcutor;
- if ( pExcutor ) {
- if ( pExcutor->IsScriptOver() ) {
- // 标记脚本已执行;
- pCase->_nExecutionState = SATHTTP::EXECUTED;
- // 如果脚本异常,标记任务失败;
- if ( pCase->_nExecutionResult == SATHTTP::ABNORMAL ) {
- // 设置任务执行结果失败;
- pTask->_nExecutionResult = SATHTTP::FAIL;
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "用例脚本异常退出(%s)", pCase->strCaseName.c_str());
- }
- // 上报服务器,完成脚本用例,并上传用例结果;
- that->ReportCaseResult(pTask, pCase);
- }
- else {
- // 判断是否超时;
- ULONGLONG ulCurTickCount = GetTickCount64();
- if ( ulCurTickCount - pExcutor->GetActiveTickCount() > GLOBAL::g_stSATConfig.dwScriptTimeout ) {
- // 标记任务已执行;
- pCase->_nExecutionState = SATHTTP::EXECUTED;
- // 结束进程;
- pExcutor->EndThread();
- // 超时中断;
- pCase->_nExecutionResult = SATHTTP::OVERTIME;
- // 上报用例结果;
- that->ReportCaseResult(pTask, pCase);
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("脚本超时:%s"), pCase->strCaseName.c_str());
- }
- }
- }
- }
- else {
- // 没有在执行的用例,开始执行新的用例;
- pCase = that->ExecuteFreeCaseScript(pTask);
- if ( NULL == pCase ) {
- // 没有空闲的用例可执行,说明所有用例已执行完成;
- pTask->_nExecutionState = SATHTTP::EXECUTED;
- // 上报任务完成;
- that->ReportTaskFinish(pTask);
- // 上报任务结果;
- that->UploadTaskLog(pTask);
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("没有可执行的用例脚本,任务【%s】"), pTask->Job.strUniqueId.c_str());
- }
- }
- }
- }
- else {
- // 获取空闲的任务;
- pTask = that->GetFreeTask();
- if ( pTask ) {
- // 是否支持并发;
- if ( pTask->_bConcurrent ) {
- // 暂时全部一起并发;
- std::vector<SATHTTP::STCase>::iterator _case = pTask->Job.vtCases.begin();
- for ( ; _case != pTask->Job.vtCases.end(); _case++) {
- if (!_case->_pExcutor) {
- CPythonExecutor *pExcutor = new CPythonExecutor();
- if ( pExcutor ) {
- _case->_pExcutor = pExcutor;
- pExcutor->InitScript(_case->_strScriptPath, _case->_strFileDir + "\\" + _case->_strFileName + ".txt", "");
- pExcutor->StartScript();
- // 标记用例执行中;
- _case->_nExecutionState = SATHTTP::INEXECUTED;
- }
- }
- }
- // 标记任务为执行中;
- pTask->_nExecutionState = SATHTTP::INEXECUTED;
- }
- else {
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, _T("\n<===============================================>\n# 1、开始执行任务:%s, 用例数量:%ld#\n<===============================================>\n"),
- pTask->Job.strUniqueId.c_str(),
- pTask->Job.vtCases.size());
- // 是否有用例脚本在执行;
- SATHTTP::STCase* pCase = that->IsCaseScriptProcess(pTask->Job.vtCases);
- if ( !pCase ) {
- // 执行空闲用例脚本;
- that->ExecuteFreeCaseScript(pTask);
- }
- }
- // 通知SAT服务器,脚本开始执行;
- that->NotifyTaskStart(pTask);
- // 通知SAT服务器,脚本开始;
- that->ReportTaskStart(pTask);
- // 将设备变更成繁忙状态;
- that->SetDeviceStatus(pTask->Job.strDeviceId, SATDEV::InUse);
- }
- }
- } while ( WaitForSingleObject(that->m_hEventExcuteScript, 10000) == WAIT_TIMEOUT );
- return 0;
- }
- DWORD CSATExecutor::WathTVPortThread(PVOID lpVoid)
- {
- CSATExecutor *that = (CSATExecutor*)lpVoid;
- if ( !that )
- return 0;
- CSynSerial tv;
- std::string buffer;
- TCHAR szLastPort[10] = {0};
- _tcscpy_s(szLastPort, GLOBAL::g_stSATConfig.szTVPort);
- std::vector<std::string> vtNotifyReboot;
- std::vector<std::string> vtNotifyShutdown;
- DWORD dwTickCount = GetTickCount();
- while( !that->m_bStopWathTV ) {
- // 实时读取配置文件;
- GLOBAL::GetIniInfo();
- if ( !GLOBAL::g_stSATConfig.bWatchTVPort ) {
- Sleep(5000);
- continue;
- }
- if ( GetTickCount() - dwTickCount > 5000 ) { // 5秒更新一次;
- vtNotifyReboot.clear();
- vtNotifyShutdown.clear();
- dwTickCount = GetTickCount();
- GLOBAL::Split(GLOBAL::g_stSATConfig.szTVReboot, _T(";"), vtNotifyReboot);
- GLOBAL::Split(GLOBAL::g_stSATConfig.szTVShutdown, _T(";"), vtNotifyShutdown);
- }
- // 串口是否变更;
- if ( _tcsicmp(szLastPort, GLOBAL::g_stSATConfig.szTVPort) ) {
- _tcscpy_s(szLastPort, GLOBAL::g_stSATConfig.szTVPort);
- // 重启打开串口;
- tv.CloseSerialPort();
- }
- // 如果串口打开失败重新打开;
- if ( !tv.IsOpen() ) {
- tv.OpenSerialPort(szLastPort, 115200, 8, 0, 1, 0, 1000);
- }
- if ( tv.IsOpen() ) {
- BYTE szBuffer[1024] = {0};
- DWORD dwBuffer = tv.ReadComm(szBuffer, 1024, 1000);
- if ( dwBuffer != 0 ) {
- int nType = 0; // 0表示无异常类型,-1表示关机异常,1表示重启异常;
- bool bAbnormal = false;
- buffer = (char*)szBuffer;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- // 遍历是否有符合的子串;
- for ( std::vector<std::string>::iterator it = vtNotifyReboot.begin(); it != vtNotifyReboot.end(); it++ )
- {
- if ( _tcsstr(buffer.c_str(), it->c_str()) ) {
- nType = 1;
- bAbnormal = true;
- break;
- }
- }
- if ( !bAbnormal ) {
- for ( std::vector<std::string>::iterator it = vtNotifyShutdown.begin(); it != vtNotifyShutdown.end(); it++ )
- {
- if ( _tcsstr(buffer.c_str(), it->c_str()) ) {
- nType = -1;
- bAbnormal = true;
- break;
- }
- }
- }
- // 1分钟前,是否有收到脚本通知;
- if ( GLOBAL::g_PyNotify.notify ) {
- __int64 nd = gmt - GLOBAL::g_PyNotify.datetime;
- #ifdef _DEBUG
- TRACE3("判断时间%ld-通知时间%ld=相差时间%ld\n\n", gmt, GLOBAL::g_PyNotify.datetime, nd);
- #endif
- if ( nd < 60)
- {
- // 监测的类型与通知类型是否一致;
- if ( (nType == 1 && GLOBAL::g_PyNotify.report_type == _T("reboot")) ||
- (nType == -1 && GLOBAL::g_PyNotify.report_type == _T("shutdown")) )
- {
- bAbnormal = false;
- // 重置;
- GLOBAL::g_PyNotify.notify = false;
- GLOBAL::g_PyNotify.datetime = 0;
- GLOBAL::g_PyNotify.report_type.clear();
- }
- }
- }
- if (bAbnormal) {
- if ( nType == -1 ) {
- // 遥控:重启电视机;
- if ( Connect("127.0.0.1", 40000) )
- sendSignal("POWER", 1, 1000);
- GLOBAL::WriteTextLog(GLOBAL::SAT_EXE, "出现异常关机重启现象");
- }
- // 压入容器中;
- GLOBAL::PyNotice tlog;
- if ( nType == -1 )
- tlog.report_type = "Abnormal shutdown";
- else if ( nType == 1 )
- tlog.report_type = "Abnormal restart";
- tlog.datetime = gmt; // 发生时间;
- struct tm gmtm = {0};
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- TCHAR szDataTime[64] = {0};
- _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);
- tlog.report_data = szDataTime;
- // 最后,重置;
- GLOBAL::g_PyNotify.notify = false;
- GLOBAL::g_PyNotify.datetime = 0;
- GLOBAL::g_PyNotify.report_type.clear();
- }
- }
- }
- Sleep(200);
- }
- return 0;
- }
|