TestService.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. #include "TestService.h"
  2. #include "pid.h"
  3. #include "valve.h"
  4. #include "ballvalve.h"
  5. #include "PressureSensor.h"
  6. #include<QDateTime>
  7. #include<QTime>
  8. #include<QEventLoop>
  9. #include<QMessageBox>
  10. #include<cmath>
  11. #include <QSignalSpy>
  12. #include <QDebug>
  13. #include <QFile>
  14. #include <QDir>
  15. TestService::TestService(QThread *parent)
  16. :QThread(parent)
  17. ,m_SerialUi3(nullptr)
  18. ,m_SerialUi4(nullptr)
  19. ,m_pTimer_1(nullptr)
  20. ,m_pTimer_2(nullptr)
  21. ,m_compartmentid(0)
  22. {
  23. m_bRunning=false;
  24. for(int i=0; i<MAX_COMPARTMENT_NUM; i++ ){
  25. m_tank.m_comparts[i].m_id = i;
  26. }
  27. //moveToThread(&m_Thread);
  28. //connect(this, SIGNAL(operate()), this, SLOT(doTest()));
  29. }
  30. TestService::~TestService()
  31. {
  32. }
  33. void TestService::init()
  34. {
  35. m_pTimer_1 = new QTimer(this);
  36. connect(m_pTimer_1, SIGNAL(timeout()), this, SLOT(OnTimer1()));
  37. //m_pTimer_2 = new QTimer(this);
  38. // connect(m_pTimer_2, SIGNAL(timeout()), this, SLOT(OnTimer2()));
  39. if(!m_SerialUi3.serialConnect("COM3", "9600", "8", "0", "1")){
  40. QString str = "打开串口COM3失败";
  41. emit onNotice(str);
  42. }
  43. #if 1
  44. if(!m_SerialUi4.serialConnect("COM4", "9600", "8", "2", "1")){
  45. QString str = "打开串口COM4失败";
  46. emit onNotice(str);
  47. }
  48. #endif
  49. connect(&m_report, SIGNAL(onProgress(QString)), this, SLOT(receive_reportprogress(QString)));
  50. }
  51. bool TestService::setTankinfo(const QVariantList& list)
  52. {
  53. QString standard_name;
  54. QString licenseplate_str;
  55. QString companyname_str;
  56. int compartment_num = 0;
  57. int total_volume = 0;
  58. int volume[MAX_COMPARTMENT_NUM] ={0};
  59. for(int i=0; i<list.count(); i++){
  60. QString item_string = list[i].toString();
  61. if(0 == i){
  62. standard_name = item_string;
  63. }else if(1 == i){
  64. licenseplate_str = item_string;
  65. }else if(2 == i){
  66. companyname_str = item_string;
  67. }else if(3 == i){
  68. total_volume = item_string.toInt();
  69. }else if(4 == i){
  70. compartment_num = item_string.toInt();
  71. }else{
  72. if(i< (8+5))
  73. volume[i-5] = item_string.toInt();
  74. }
  75. }
  76. //check invalid
  77. bool ret_bool = true;
  78. if( compartment_num > 0 && compartment_num <= MAX_COMPARTMENT_NUM){
  79. int volume_sum =0;
  80. for(int i=0; i<compartment_num; i++){
  81. volume_sum += volume[i];
  82. }
  83. if(total_volume != volume_sum){
  84. ret_bool = false;
  85. }
  86. }else{
  87. ret_bool = false;
  88. }
  89. if(true == ret_bool){
  90. //m_standardname = standard_name;
  91. qDebug("setTankinfo: standard_name:[%s]", standard_name.toStdString().c_str());
  92. m_tank.licenseplate_str = licenseplate_str;
  93. qDebug("setTankinfo: licenseplate:[%s]", licenseplate_str.toStdString().c_str());
  94. m_tank.companyname_str = companyname_str;
  95. qDebug("setTankinfo: companyname:[%s]", companyname_str.toStdString().c_str());
  96. m_tank.total_volume = total_volume;
  97. qDebug("setTankinfo: total_volume:[%d]", total_volume);
  98. m_tank.compartment_num = compartment_num;
  99. qDebug("setTankinfo: compartment_num:[%d]", compartment_num);
  100. //Standard standard;
  101. if(g_StandardManager->getStandard(standard_name, m_standard)){
  102. for(int i=0; i<compartment_num; i++){
  103. m_tank.m_comparts[i].m_volume = volume[i];
  104. qDebug("setTankinfo: volume [%d]:[%d]", i, volume[i]);
  105. for(int j=0; j<m_standard.item_count; j++){
  106. if(volume[i] >= m_standard.item[j].oilcompartment_volume){
  107. m_tank.m_comparts[i].m_standarditem = m_standard.item[j];
  108. qDebug("setTankinfo: compart [%d]: volume[%d]", i, m_standard.item[j].oilcompartment_volume);
  109. qDebug("setTankinfo: compart [%d]: sys[%f]", i, m_standard.item[j].system_threshold);
  110. qDebug("setTankinfo: compart [%d]: valve[%f]", i, m_standard.item[j].valve_threshold);
  111. }
  112. }
  113. }
  114. }else{
  115. QString str = "所选标准不存在!";
  116. emit onNotice(str);
  117. ret_bool = false;
  118. }
  119. }
  120. return ret_bool;
  121. }
  122. bool TestService::tstart(int compartmentid, int direction)
  123. {
  124. if(m_bRunning){
  125. QString str = "当前有任务正在进行中...";
  126. emit onNotice(str);
  127. return false;
  128. }
  129. m_compartmentid = compartmentid-1;
  130. if(0 == direction)
  131. m_direction = Test_Direction_Positive;
  132. else
  133. m_direction = Test_Direction_Negative;
  134. m_stage = Test_Stage_SysTest;
  135. m_step = Test_Step_Start;
  136. m_pressuremutex.lock();
  137. m_pressurelist.clear(); //用来计算平均压力
  138. m_pressuremutex.unlock();
  139. QMessageBox::StandardButton result = QMessageBox::information(NULL, "提示:", "请对油气回收系统相关部件进行检查", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
  140. switch (result)
  141. {
  142. case QMessageBox::Ok:
  143. qDebug()<<"Ok";
  144. m_pTimer_1->start(1*1000); // 1s
  145. m_bRunning = true;
  146. start();
  147. break;
  148. case QMessageBox::Abort:
  149. qDebug()<<"Abort";
  150. return false;
  151. break;
  152. default:
  153. qDebug()<<"default";
  154. return false;
  155. break;
  156. }
  157. return true;
  158. }
  159. bool TestService::tselfstart(int compartmentid, int direction)
  160. {
  161. if(m_bRunning){
  162. QString str = "当前有任务正在进行中...";
  163. emit onNotice(str);
  164. return false;
  165. }
  166. m_compartmentid = compartmentid-1;
  167. if(0 == direction)
  168. m_direction = Test_Direction_Positive;
  169. else
  170. m_direction = Test_Direction_Negative;
  171. m_stage = Test_Stage_SysTest;
  172. m_step = Test_Step_Start;
  173. m_pressuremutex.lock();
  174. m_pressurelist.clear(); //用来计算平均压力
  175. m_pressuremutex.unlock();
  176. QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "系统即将自检,请确认所有部件都己接好", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
  177. switch (result)
  178. {
  179. case QMessageBox::Ok:
  180. qDebug()<<"Ok";
  181. m_pTimer_1->start(1*1000); // 1s
  182. m_bRunning = true;
  183. start();
  184. break;
  185. case QMessageBox::Abort:
  186. qDebug()<<"Abort";
  187. return false;
  188. break;
  189. default:
  190. qDebug()<<"default";
  191. return false;
  192. break;
  193. }
  194. return true;
  195. }
  196. bool TestService::tstop(int compartmentid)
  197. {
  198. if(m_compartmentid != compartmentid-1){
  199. return false;
  200. }
  201. if(m_pTimer_1->isActive())
  202. m_pTimer_1->stop();
  203. //if(m_pTimer_2->isActive())
  204. // m_pTimer_2->stop();
  205. if(m_bRunning){
  206. //stop();
  207. m_bRunning = false;
  208. }
  209. return true;
  210. }
  211. bool TestService::reportpic_Ready(int compartmentid)
  212. {
  213. Compartment& com = m_tank.get_compartment(compartmentid-1);
  214. com.m_bpicReady = true;
  215. return true;
  216. }
  217. bool TestService::check_alltest_ok()
  218. {
  219. bool ret_flag = true;
  220. for(int i=0; i< m_tank.compartment_num; i++){
  221. Compartment& com = m_tank.get_compartment(i);
  222. if(com.m_bpicReady == false ){
  223. ret_flag = false;
  224. break;
  225. }
  226. }
  227. if(false == ret_flag){
  228. QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "有油仓还未进行检测,是否强制生成报告?", QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
  229. switch (result)
  230. {
  231. case QMessageBox::Yes:
  232. ret_flag = true;
  233. break;
  234. case QMessageBox::No:
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. return ret_flag;
  241. }
  242. void TestService::receive_reportprogress(QString qstr)
  243. {
  244. qDebug() << "TestService::receive_reportprogress ";
  245. qDebug() << qstr;
  246. emit onGenreportProgress(qstr);
  247. }
  248. bool TestService::genReport(QString word_path, QString pic_dir)
  249. {
  250. if(word_path.contains(".doc")){
  251. return m_report.make_Word(m_tank, word_path, pic_dir);
  252. }else if(word_path.contains(".pdf")){
  253. return m_report.make_PDF(m_tank, word_path, pic_dir);
  254. }
  255. QString str = "word_path invalid!";
  256. qDebug() << str;
  257. return false;
  258. }
  259. QJsonObject TestService::get_vrs_items()
  260. {
  261. return m_tank.get_vrs_items();
  262. }
  263. QJsonObject TestService::get_bls_items()
  264. {
  265. return m_tank.get_bls_items();
  266. }
  267. bool TestService::set_item_result(const QString& jsonStr )
  268. {
  269. return m_tank.set_item_result(jsonStr);
  270. }
  271. void TestService::push_pressurelist(float pressure)
  272. {
  273. m_pressuremutex.lock();
  274. if(m_pressurelist.size() == AVERAGE_PRESSURE_COUNT){
  275. m_pressurelist.removeAt(0);
  276. }
  277. m_pressurelist.append(pressure);
  278. m_pressuremutex.unlock();
  279. }
  280. void TestService::clear_pressurelist()
  281. {
  282. m_pressuremutex.lock();
  283. m_pressurelist.clear(); //用来计算平均压力
  284. m_pressuremutex.unlock();
  285. }
  286. float TestService::calculate_averagepressure()
  287. {
  288. float average = 0.0;
  289. m_pressuremutex.lock();
  290. int n = m_pressurelist.size();
  291. if(n>0){
  292. float sum = 0.0;
  293. for(int i=0; i<n; i++){
  294. sum += m_pressurelist.at(i);
  295. //qDebug("push_pressurelist 333 size[%f]", m_pressurelist.at(i));
  296. }
  297. average = sum/n;
  298. }
  299. m_pressuremutex.unlock();
  300. return average;
  301. }
  302. bool TestService::valve_open(Valve_ID id)
  303. {
  304. if(Valve_ID_All == id){
  305. Valve v(&m_SerialUi3, ALL_VALVE);
  306. return v.Open();
  307. }else{
  308. Valve v(&m_SerialUi3, id);
  309. return v.Open();
  310. }
  311. }
  312. bool TestService::valve_close(Valve_ID id)
  313. {
  314. if(Valve_ID_All == id){
  315. Valve v(&m_SerialUi3, ALL_VALVE);
  316. return v.Close();
  317. }else{
  318. Valve v(&m_SerialUi3, id);
  319. return v.Close();
  320. }
  321. }
  322. QString TestService::read_PressureValue()
  323. {
  324. float pressure = 0.0;
  325. static int timeout_count =0;
  326. PressureSensor ps(&m_SerialUi4);
  327. if(ps.Read(pressure)){
  328. int _ipressure = pressure*100;
  329. pressure = _ipressure/100.0;
  330. push_pressurelist(pressure);
  331. timeout_count=0;
  332. }else{
  333. pressure = calculate_averagepressure();
  334. qDebug("Read_PressureValue failed");
  335. timeout_count++;
  336. if(timeout_count >= AVERAGE_PRESSURE_COUNT){
  337. //data is to old, clear
  338. clear_pressurelist();
  339. qDebug("clear_pressurelist ");
  340. }
  341. }
  342. QString pressure_str = QString("%1").arg(pressure, 4,'f',2,QLatin1Char('0'));
  343. return pressure_str;
  344. }
  345. bool TestService::pressure_zero()
  346. {
  347. PressureSensor ps(&m_SerialUi4);
  348. return ps.Set_Zero();
  349. }
  350. bool TestService::ballvalve_control(QString position_str)
  351. {
  352. bool ok;
  353. ushort position = position_str.toUShort(&ok, 10);
  354. BallValve ball_v(&m_SerialUi4);
  355. return ball_v.SetPosition(position);
  356. }
  357. QJsonObject TestService::ballvalve_status()
  358. {
  359. unsigned char status;
  360. unsigned short value = 0;
  361. QJsonObject rootObj;
  362. BallValve ball_v(&m_SerialUi4);
  363. ball_v.GetSP(status, value);
  364. if(ball_v.GetSP(status, value)){
  365. switch(status){
  366. case BALLVALVE_STATUS_OPENING:
  367. rootObj.insert("status", "opening");
  368. break;
  369. case BALLVALVE_STATUS_CLOSING:
  370. rootObj.insert("status", "closing");
  371. break;
  372. case BALLVALVE_STATUS_OPENEND:
  373. rootObj.insert("status", "opened");
  374. break;
  375. case BALLVALVE_STATUS_CLOSEEND:
  376. rootObj.insert("status", "closed");
  377. break;
  378. case BALLVALVE_STATUS_BREAK:
  379. rootObj.insert("status", "break");
  380. break;
  381. case BALLVALVE_STATUS_FAULT:
  382. rootObj.insert("status", "fault");
  383. break;
  384. default:
  385. rootObj.insert("status", "unkown");
  386. break;
  387. }
  388. rootObj.insert("position", value);
  389. }else{
  390. rootObj.insert("status", "unkown");
  391. rootObj.insert("position", 0);
  392. }
  393. return rootObj;
  394. }
  395. #if 0
  396. bool TestService::OpenSerial3()
  397. {
  398. m_SerialPort3 = new QSerialPort();
  399. m_SerialPort3->setPortName("COM3");
  400. m_SerialPort3->setBaudRate(9600, QSerialPort::AllDirections);
  401. m_SerialPort3->setDataBits(QSerialPort::Data8);
  402. m_SerialPort3->setParity(QSerialPort::NoParity);
  403. m_SerialPort3->setStopBits(QSerialPort::OneStop);
  404. //m_SerialPort3->setFlowControl(QSerialPort::NoFlowControl);//Set to no flow control
  405. if (m_SerialPort3->open(QIODevice::ReadWrite) == false){
  406. qDebug("open COM3 failed");
  407. //QMessageBox::warning(this, "警告", "打开串口COM3失败");
  408. QString str = "打开串口COM3失败";
  409. emit onNotice(str);
  410. return false;
  411. }
  412. else
  413. {
  414. //controlserial->close();
  415. //connect(m_SerialPort3,SIGNAL(readyRead()),this,SLOT(ReadSerialport3()));
  416. return true;
  417. }
  418. }
  419. bool TestService::OpenSerial4()
  420. {
  421. m_SerialPort4 = new QSerialPort();
  422. m_SerialPort4->setPortName("COM4");
  423. m_SerialPort4->setBaudRate(9600, QSerialPort::AllDirections);
  424. m_SerialPort4->setDataBits(QSerialPort::Data8);
  425. m_SerialPort4->setParity(QSerialPort::EvenParity);
  426. m_SerialPort4->setStopBits(QSerialPort::OneStop);
  427. m_SerialPort4->setFlowControl(QSerialPort::NoFlowControl);//Set to no flow control
  428. if (m_SerialPort4->open(QIODevice::ReadWrite) == false){
  429. qDebug("open COM4 failed");
  430. //QMessageBox::warning(this, "警告", "打开串口COM4失败");
  431. QString str = "打开串口COM4失败,无法读取压力";
  432. emit onNotice(str);
  433. return false;
  434. }
  435. else
  436. {
  437. //readserial->close();
  438. //connect(m_SerialPort4,SIGNAL(readyRead()),this,SLOT(ReadSerialport4()));
  439. return true;
  440. }
  441. }
  442. #endif
  443. void TestService::OnTimer1()
  444. {
  445. float pressure = 0.00;
  446. static int timeout_count =0;
  447. PressureSensor ps(&m_SerialUi4);
  448. if(ps.Read(pressure)){
  449. //int _ipressure = pressure*100;
  450. //if(_ipressure<100){
  451. // _ipressure=0;
  452. //}
  453. //pressure = _ipressure/100.0;
  454. push_pressurelist(pressure);
  455. timeout_count=0;
  456. }else{
  457. qDebug("Read_PressureValue failed");
  458. pressure = calculate_averagepressure();
  459. timeout_count++;
  460. if(timeout_count >= AVERAGE_PRESSURE_COUNT){
  461. //data is to old, clear
  462. clear_pressurelist();
  463. qDebug("clear_pressurelist ");
  464. }
  465. }
  466. int _ipressure = 0;
  467. if(pressure >= 4.5){
  468. _ipressure = pressure*10;
  469. pressure = _ipressure/10.0;
  470. }else{
  471. _ipressure = pressure*100;
  472. pressure = _ipressure/100.0;
  473. }
  474. //int _ipressure = pressure*100;
  475. //pressure = _ipressure/100.0;
  476. QString pressure_str = QString("%1").arg(pressure, 4,'f',2,QLatin1Char('0'));
  477. PressureItem item;
  478. //item.m_pressure = pressure_str;
  479. item.m_testDirection = m_direction;
  480. item.m_testStage = m_stage;
  481. item.m_testStep = m_step;
  482. QVariantMap map;
  483. map.clear();
  484. map.insert("pressure", pressure_str);
  485. map.insert("direction", item.m_testDirection);
  486. map.insert("stage", item.m_testStage);
  487. map.insert("step", item.m_testStep);
  488. //QString pressure_str = QString("%1").arg(pressure, 5,'f',3,QLatin1Char('0'));
  489. //m_tank.m_comparts[m_compartmentid].m_currentpressure= pressure_str.toFloat();
  490. emit sigPressure(m_compartmentid+1, map);
  491. }
  492. void TestService::OnTimer2()
  493. {
  494. //qDebug("OnTimer2 >>>enter ");
  495. //float pressure;
  496. //Read_pressure(pressure);
  497. //QThread::sleep(5);
  498. //emit Pressure_changed(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
  499. //qDebug("OnTimer2 >>>leave ");
  500. }
  501. Ret_Value TestService::PositivePressure_Add(const TestExpect& expect)
  502. {
  503. Ret_Value ret_v = Ret_OK;
  504. int count = 0;
  505. int stable_counts = 0;
  506. int exceed_times =0;
  507. QElapsedTimer timer;
  508. timer.start();
  509. float prev_pressure = 0;
  510. float current_pressure = 0;
  511. double current_speed = 0;
  512. unsigned char bvstatus;
  513. unsigned short bvposition;
  514. unsigned short bvoffset = 100;
  515. int holding_time = 60;
  516. PressureSensor ps(&m_SerialUi4);
  517. BallValve bv(&m_SerialUi4);
  518. bool bv_ready = false;
  519. bv.SetPosition(bvoffset);
  520. PID* pPIDouter = NULL;
  521. PID* pPIDinner = NULL;
  522. //pPIDouter = new PID(1, 0.2, 0, 0.2, 0, 0);
  523. if(expect.volume <= 1000){
  524. pPIDouter = new PID(1, 0.2, 0, 0.2, 0, 0);
  525. pPIDinner = new PID(1, 200, 0, 1000, 50, 20, 40, 2);
  526. holding_time = 30;
  527. }else if(expect.volume <= 5000){
  528. pPIDouter = new PID(1, 0.2, 0, 0.2, 0, 0);
  529. pPIDinner = new PID(1, 900, 0, 3000, 50, 30, 60, 10);
  530. holding_time = 60;
  531. }else if(expect.volume <= 10000){
  532. pPIDouter = new PID(1, 0.2, 0, 0.2, 0, 0);
  533. pPIDinner = new PID(1, 900, 0, 4500, 80, 60, 60, 20);
  534. holding_time = 60;
  535. }else{
  536. pPIDouter = new PID(1, 0.2, 0, 0.2, 0, 0);
  537. pPIDinner = new PID(1, 900, 0, 4500, 40, 60, 50, 20);
  538. holding_time = 30;
  539. }
  540. prev_pressure = calculate_averagepressure();
  541. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  542. bool bOpen_intake = false;
  543. //bOpen_intake = in_v.Open();
  544. //等待压力达到目标值附近
  545. while(m_bRunning){
  546. //等待球阀,到达指定位置 100
  547. if(false == bv_ready){
  548. if(false == bv.GetSP(bvstatus, bvposition)){
  549. qDebug(" PositivePressure_Add, bv.GetSP failed");
  550. }else{
  551. if(BALLVALVE_STATUS_FAULT == bvstatus){
  552. ret_v = Ret_Failed;
  553. break;
  554. }
  555. if((BALLVALVE_STATUS_OPENEND == bvstatus) || (BALLVALVE_STATUS_CLOSEEND == bvstatus) ){
  556. int ps_offset = bvoffset;
  557. int cu_position = bvposition;
  558. if(abs(cu_position-ps_offset) < 10){
  559. bv_ready=true;
  560. }
  561. }else{
  562. bv.SetPosition(bvoffset);
  563. }
  564. }
  565. }else{
  566. current_pressure = calculate_averagepressure();
  567. current_speed = current_pressure - prev_pressure;
  568. prev_pressure = current_pressure;
  569. qDebug(" PositivePressure_Add, current_pressure : %f target: %f", current_pressure, expect.target);
  570. if(current_pressure > expect.target+0.05){
  571. count++;
  572. if(bOpen_intake){
  573. qDebug(" PositivePressure_Add, in_v.Close ");
  574. if(in_v.Close()){
  575. bOpen_intake = false;
  576. exceed_times++;
  577. }
  578. //bOpen_intake = false;
  579. }
  580. if(exceed_times <= 1){
  581. stable_counts = 15;
  582. }else if(2 == exceed_times){
  583. stable_counts =10;
  584. }else{
  585. stable_counts =5;
  586. }
  587. //stable_counts = 15;
  588. //系统自测及容积较小时,球阀最好归零,否则因球阀不太紧漏气, 进气阀与球阀之间的气体会压向系统,导致压力升高
  589. //当容器过大时, 如果压力超出后又降下来或系统存在漏气,需要重新加压,设置 bvoffset,能让重新加压速度快起来。
  590. if(expect.volume < 100){
  591. if(bv.SetPosition(0))
  592. bvposition = 0;
  593. qDebug(" PositivePressure_Add, bv.SetPosition 0");
  594. }else {
  595. if(bvoffset != bvposition){
  596. if(bv.SetPosition(bvoffset))
  597. bvposition = bvoffset;
  598. qDebug(" PositivePressure_Add, bv.SetPosition bvoffset");
  599. }
  600. }
  601. }else{
  602. if(current_pressure >= expect.target && current_pressure <= expect.target + 0.05 ){
  603. count++;
  604. }else{
  605. count=0;
  606. if(stable_counts > 0){
  607. stable_counts--;
  608. }
  609. if(false == bOpen_intake && 0 == stable_counts){
  610. bOpen_intake = in_v.Open();
  611. qDebug(" PositivePressure_Add, in_v.Open 222 ");
  612. pPIDinner->reset();
  613. }
  614. }
  615. if(NULL != pPIDouter && NULL != pPIDinner && 0 == stable_counts){
  616. double speed_target = pPIDouter->calculate(expect.target+0.02, current_pressure);
  617. qDebug(" PositivePressure_Add, pPIDinner speed_target:%f, current_speed:%f", speed_target, current_speed);
  618. double inc = pPIDinner->calculate(speed_target+0.009, current_speed);
  619. unsigned short position = bvoffset +inc;
  620. //qDebug(" PositivePressure_Add, position : %d", position);
  621. if(bvposition != position){
  622. qDebug(" PositivePressure_Add, position : %d", position);
  623. if(bv.SetPosition(position))
  624. bvposition = position;
  625. }
  626. }else{
  627. if(0 == stable_counts){
  628. qDebug(" PositivePressure_Add, NULL = pPIDouter NULL == pPIDinner ");
  629. }
  630. //qDebug(" PositivePressure_Add, NULL = pPIDouter NULL == pPIDinner ");
  631. }
  632. }
  633. }
  634. if(count >= holding_time){ //about 60s
  635. break;
  636. }
  637. QThread::msleep(1000);
  638. int remainingtime = expect.timeout*1000 - timer.elapsed();
  639. if(remainingtime <= 0){
  640. qDebug(" add_pressure timeout, Rough valve will colse");
  641. ret_v = Ret_Timeout;
  642. break;
  643. }
  644. }
  645. if(!m_bRunning){
  646. // be stoped
  647. ret_v = Ret_Exit;
  648. }
  649. if(NULL != pPIDouter){
  650. delete pPIDouter;
  651. }
  652. if(NULL != pPIDinner){
  653. delete pPIDinner;
  654. }
  655. in_v.Close();
  656. bv.SetPosition(0);
  657. return ret_v;
  658. }
  659. Ret_Value TestService::PositivePressure_Add_V2(const TestExpect& expect)
  660. {
  661. Ret_Value ret_v = Ret_OK;
  662. int count = 0;
  663. QElapsedTimer timer;
  664. timer.start();
  665. float current_pressure = 0;
  666. unsigned char bvstatus;
  667. unsigned short bvposition;
  668. unsigned short bvoffset = 120;
  669. int holding_time = 60;
  670. PressureSensor ps(&m_SerialUi4);
  671. BallValve bv(&m_SerialUi4);
  672. bool bv_ready = false;
  673. bv.SetPosition(bvoffset);
  674. PID* pPID = NULL;
  675. if(expect.volume <= 200){
  676. pPID = new PID(1, 200, 0, 0, 2, 0.4);
  677. holding_time = 60;
  678. }else if(expect.volume <= 4000){
  679. pPID = new PID(1, 500, 0, 0, 5, 0.6);
  680. holding_time = 60;
  681. }else if(expect.volume <= 6000){
  682. pPID = new PID(1, 800, 0, 0, 6, 1.0);
  683. holding_time = 120;
  684. }else if(expect.volume <= 8000){
  685. pPID = new PID(1, 800, 0, 0, 7, 2);
  686. holding_time = 180;
  687. }else{
  688. pPID = new PID(1, 800, 0, 0, 8, 3);
  689. holding_time = 180;
  690. }
  691. Valve in_v(&m_SerialUi3, VALVE_INTAKE);
  692. bool bOpen_intake = false;
  693. //bOpen_intake = in_v.Open();
  694. //等待压力达到目标值附近
  695. while(m_bRunning){
  696. //等待球阀,到达指定位置 100
  697. if(false == bv_ready){
  698. if(false == bv.GetSP(bvstatus, bvposition)){
  699. qDebug(" PositivePressure_Add, bv.GetSP failed");
  700. }else{
  701. if(BALLVALVE_STATUS_FAULT == bvstatus){
  702. ret_v = Ret_Failed;
  703. break;
  704. }
  705. if((BALLVALVE_STATUS_OPENEND == bvstatus) || (BALLVALVE_STATUS_CLOSEEND == bvstatus) ){
  706. int ps_offset = bvoffset;
  707. int cu_position = bvposition;
  708. if(abs(cu_position-ps_offset) < 10){
  709. bv_ready=true;
  710. }
  711. }else{
  712. bv.SetPosition(bvoffset);
  713. }
  714. }
  715. }else{
  716. current_pressure = calculate_averagepressure();
  717. qDebug(" PositivePressure_Add, current_pressure : %f target: %f", current_pressure, expect.target);
  718. if(current_pressure >= expect.target+0.05){
  719. count++;
  720. if(bOpen_intake){
  721. qDebug(" PositivePressure_Add, in_v.Close ");
  722. if(in_v.Close())
  723. bOpen_intake = false;
  724. }
  725. //系统自测及容积较小时,球阀最好归零,否则因球阀不太紧漏气, 进气阀与球阀之间的气体会压向系统,导致压力升高
  726. //当容器过大时, 如果压力超出后又降下来或系统存在漏气,需要重新加压,设置 bvoffset,能让重新加压速度快起来。
  727. if(expect.volume < 100){
  728. if(bv.SetPosition(0))
  729. bvposition = 0;
  730. qDebug(" PositivePressure_Add, bv.SetPosition 0");
  731. }else {
  732. if(bvoffset != bvposition){
  733. if(bv.SetPosition(bvoffset))
  734. bvposition = bvoffset;
  735. qDebug(" PositivePressure_Add, bv.SetPosition bvoffset");
  736. }
  737. }
  738. }else if((current_pressure < expect.target+0.05) & (current_pressure >= expect.target) ){
  739. count++;
  740. }else{
  741. count = 0;
  742. if(false == bOpen_intake){
  743. bOpen_intake = in_v.Open();
  744. qDebug(" PositivePressure_Add, in_v.Open ");
  745. pPID->reset();
  746. }else{
  747. if(NULL != pPID){
  748. double inc = pPID->calculate_v2(expect.target+0.05, current_pressure);
  749. unsigned short position = bvoffset +inc;
  750. //qDebug(" PositivePressure_Add, position : %d", position);
  751. if(bvposition != position){
  752. qDebug(" PositivePressure_Add, position : %d", position);
  753. if(bv.SetPosition(position))
  754. bvposition = position;
  755. }
  756. }else{
  757. qDebug(" PositivePressure_Add, NULL = pPID ");
  758. }
  759. }
  760. }
  761. }
  762. if(count >= holding_time){ //about 60s
  763. break;
  764. }
  765. QThread::msleep(1000);
  766. int remainingtime = expect.timeout*1000 - timer.elapsed();
  767. if(remainingtime <= 0){
  768. qDebug(" add_pressure timeout, Rough valve will colse");
  769. ret_v = Ret_Timeout;
  770. break;
  771. }
  772. }
  773. if(!m_bRunning){
  774. // be stoped
  775. ret_v = Ret_Exit;
  776. }
  777. if(NULL != pPID){
  778. delete pPID;
  779. }
  780. in_v.Close();
  781. bv.SetPosition(0);
  782. return ret_v;
  783. }
  784. Ret_Value TestService::Pressure_Relief(const TestExpect& expect)
  785. {
  786. Ret_Value ret_v = Ret_OK;
  787. bool bOpen_ventv = false;
  788. Valve vent_v(&m_SerialUi3, VALVE_VENT);
  789. float current_pressure;
  790. QElapsedTimer timer;
  791. timer.start();
  792. int count = 0;
  793. //打开泄压阀
  794. bOpen_ventv = vent_v.Open();
  795. //等待压力清零
  796. while(m_bRunning){
  797. if(false == bOpen_ventv){
  798. bOpen_ventv = vent_v.Open();
  799. }
  800. current_pressure = calculate_averagepressure();
  801. if(current_pressure <= 0.02){
  802. qDebug(" Pressure_Relief 111 finished");
  803. count++;
  804. }else{
  805. count=0;
  806. }
  807. if(count > 10){
  808. //关闭泄压阀
  809. if(vent_v.Close()){
  810. break;
  811. }
  812. }
  813. QThread::msleep(1000);
  814. int remainingtime = expect.timeout*1000 - timer.elapsed();
  815. if(remainingtime < 0){
  816. ret_v = Ret_Timeout;
  817. qDebug("timeout, Pressure_Relief Timeout");
  818. break;
  819. }
  820. }
  821. if(!m_bRunning){
  822. ret_v = Ret_Exit;
  823. }
  824. vent_v.Close();
  825. return ret_v;
  826. }
  827. Ret_Value TestService::Pressure_Relief()
  828. {
  829. Ret_Value ret_v = Ret_OK;
  830. bool bOpen_ventv = false;
  831. Valve vent_v(&m_SerialUi3, VALVE_VENT);
  832. float current_pressure;
  833. QElapsedTimer timer;
  834. timer.start();
  835. int count = 0;
  836. //打开泄压阀
  837. bOpen_ventv = vent_v.Open();
  838. //等待压力清零
  839. while(1){
  840. if(false == bOpen_ventv){
  841. bOpen_ventv = vent_v.Open();
  842. }
  843. if(false == m_pTimer_1->isActive())
  844. OnTimer1();
  845. current_pressure = calculate_averagepressure();
  846. if(current_pressure < 0.1){
  847. qDebug(" Pressure_Relief finished 222");
  848. count++;
  849. }else{
  850. count=0;
  851. }
  852. if(count > 5){
  853. //关闭泄压阀
  854. vent_v.Close();
  855. break;
  856. }
  857. QThread::msleep(1000);
  858. int remainingtime = 200*1000 - timer.elapsed();
  859. if(remainingtime < 0){
  860. ret_v = Ret_Timeout;
  861. qDebug("timeout, Pressure_Relief Timeout");
  862. break;
  863. }
  864. }
  865. vent_v.Close();
  866. qDebug(" Pressure_Relief leave");
  867. return ret_v;
  868. }
  869. Ret_Value TestService::Pressure_Keep(int keeptime, float& start_value, float& end_value)
  870. {
  871. int count = 0;
  872. Ret_Value ret_v = Ret_OK;
  873. start_value = calculate_averagepressure();
  874. int _ipressure = start_value*10;
  875. start_value = _ipressure/10.0;
  876. while((count < keeptime) && m_bRunning){
  877. QThread::msleep(1000);
  878. count++;
  879. }
  880. end_value = calculate_averagepressure();
  881. _ipressure = end_value*10;
  882. end_value = _ipressure/10.0;
  883. if(!m_bRunning){
  884. // be stoped
  885. ret_v = Ret_Exit;
  886. }
  887. return ret_v;
  888. }
  889. bool TestService::self_checking()
  890. {
  891. //读继电器状态
  892. Valve all_v(&m_SerialUi3, ALL_VALVE);
  893. if(false == all_v.Close()){
  894. return false;
  895. }
  896. //读取球阀状态
  897. BallValve ball_v(&m_SerialUi4);
  898. unsigned char status;
  899. unsigned short value;
  900. if(false == ball_v.GetSP(status, value)){
  901. return false;
  902. }
  903. if(status == BALLVALVE_STATUS_FAULT){
  904. return false;
  905. }
  906. ball_v.SetPosition(0);
  907. PressureSensor ps(&m_SerialUi4);
  908. float pressure = 0.0;
  909. if(false == ps.Read(pressure)){
  910. return false;
  911. }
  912. return true;
  913. }
  914. bool TestService::test_init()
  915. {
  916. //bool ret_v=true;
  917. Valve all_v(&m_SerialUi3, ALL_VALVE);
  918. all_v.Close();
  919. BallValve ball_v(&m_SerialUi4);
  920. unsigned char status;
  921. unsigned short value;
  922. if(ball_v.GetSP(status, value)){
  923. if(value > 20){
  924. ball_v.SetPosition(0);
  925. int wait_time = (1+value/100);
  926. QThread::msleep(wait_time*1000);
  927. }
  928. }else{
  929. return false;
  930. }
  931. return true;
  932. }
  933. int TestService::make_messagebox(Test_Direction direction, Test_Step step, Ret_Value ret_v)
  934. {
  935. int ret_btn = QMessageBox::NoButton;
  936. QString notice_str;
  937. if(Test_Direction_Positive == direction){
  938. switch(step){
  939. case Test_Step_AddPressure:
  940. if(Ret_Timeout == ret_v){
  941. notice_str = "加压超时, 是否重试?";
  942. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  943. }else if(Ret_Failed == ret_v){
  944. notice_str = "继电器通信失败, 和序终止";
  945. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  946. }
  947. break;
  948. case Test_Step_AdjustPressure:
  949. if( Ret_Timeout == ret_v ){
  950. notice_str = "比例阀调压精调超时, 是否重试?";
  951. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  952. //emit onNotice(notice_str);
  953. }else if(Ret_Failed == ret_v){
  954. notice_str = "继电器通信失败, 程序终止";
  955. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  956. //emit onNotice(notice_str);
  957. }
  958. break;
  959. default:
  960. break;
  961. }
  962. }else {
  963. }
  964. return ret_btn;
  965. }
  966. void TestService::run()
  967. {
  968. qDebug("doTest >>>enter ");
  969. QString notice_str;
  970. QElapsedTimer q_time;
  971. int ret_btn = QMessageBox::NoButton;
  972. TestExpect texpect;
  973. bool bQuit = false;
  974. q_time.start();
  975. Ret_Value ret_v = Ret_OK;
  976. if(m_compartmentid >= 0){
  977. if(m_direction == Test_Direction_Positive){
  978. while(m_bRunning && (bQuit == false)){
  979. switch(m_step){
  980. case Test_Step_Start:
  981. {
  982. m_tank.get_compartment(m_compartmentid).clear_result();
  983. if(false == self_checking()){
  984. ret_btn = m_msgbox.information(NULL, "提示:", "通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  985. }else{
  986. test_init();
  987. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀", QMessageBox::Ok, QMessageBox::Ok);
  988. }
  989. switch (ret_btn)
  990. {
  991. case QMessageBox::Ok:
  992. m_step = Test_Step_AddPressure;
  993. break;
  994. default:
  995. break;
  996. }
  997. }
  998. break;
  999. case Test_Step_AddPressure:
  1000. {
  1001. if(m_stage == Test_Stage_SysTest){
  1002. emit sigState(m_compartmentid+1, "回收系统测试,开始加压", "");
  1003. }else{
  1004. emit sigState(m_compartmentid+1, "回收阀测试,开始加压", "");
  1005. }
  1006. texpect.target = m_standard.positive_target;
  1007. texpect.volume = m_tank.m_comparts[m_compartmentid].m_volume;
  1008. texpect.timeout = m_tank.m_comparts[m_compartmentid].get_addpressure_timeout();
  1009. ret_v = PositivePressure_Add(texpect);
  1010. if(Ret_OK == ret_v){
  1011. if(m_stage == Test_Stage_SysTest){
  1012. m_step = Test_Step_KeepPressure;
  1013. }else if(m_stage == Test_Stage_ValveTest){
  1014. m_msgbox.information(NULL, "提示:", "即将泄压, 请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1015. m_step = Test_Step_reliefPressure;
  1016. }
  1017. }else if(Ret_Exit == ret_v){
  1018. bQuit = true;
  1019. }else{
  1020. ret_btn = make_messagebox(m_direction, m_step, ret_v);
  1021. switch (ret_btn)
  1022. {
  1023. case QMessageBox::Retry:
  1024. qDebug()<<"Retry";
  1025. break;
  1026. case QMessageBox::Abort:
  1027. qDebug()<<"Abort";
  1028. bQuit = true;
  1029. break;
  1030. case QMessageBox::Ok:
  1031. qDebug()<<"OK";
  1032. bQuit = true;
  1033. break;
  1034. default:
  1035. break;
  1036. }
  1037. }
  1038. }
  1039. break;
  1040. case Test_Step_KeepPressure:
  1041. {
  1042. if(m_stage == Test_Stage_SysTest){
  1043. emit sigState(m_compartmentid+1, "油气回收系统测试,开始保压", "");
  1044. }else{
  1045. emit sigState(m_compartmentid+1, "油气回收阀测试,开始保压", "");
  1046. }
  1047. // 检测 5分钟都没超限,就是通过了
  1048. //float delta = 0;
  1049. //float threshold = 0.0;
  1050. //bool bPassed = true;
  1051. // 检测 5分钟都没超限,就是通过了
  1052. float delta = 0;
  1053. float start_value = 0.0;
  1054. float end_value = 0.0;
  1055. bool bPassed = true;
  1056. //ret_v = Pressure_Keep2(5*60, start_value, end_value);
  1057. if(m_stage == Test_Stage_SysTest){
  1058. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1059. m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = start_value;
  1060. m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = end_value;
  1061. delta = start_value - end_value;
  1062. m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = delta;
  1063. if(Ret_Exit == ret_v){
  1064. bQuit = true;
  1065. } else {
  1066. if(delta < m_tank.m_comparts[m_compartmentid].m_standarditem.system_threshold){
  1067. bPassed = true;
  1068. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
  1069. }else{
  1070. bPassed = false;
  1071. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = false;
  1072. }
  1073. m_tank.m_comparts[m_compartmentid].m_result.sys_test_finished = true;
  1074. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1075. emit sigSystemResult(m_compartmentid+1, delta_str, bPassed);
  1076. emit sigState(m_compartmentid+1, "油气回收系统检测,结束", "");
  1077. ret_btn = QMessageBox::NoButton;
  1078. notice_str = "系统密闭性已经检测完成,是否进行阀门密闭性检测?";
  1079. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1080. switch (ret_btn)
  1081. {
  1082. case QMessageBox::Yes:
  1083. m_stage = Test_Stage_ValveTest;
  1084. if(calculate_averagepressure() < m_standard.positive_target){
  1085. m_step = Test_Step_AddPressure;
  1086. }else{
  1087. m_msgbox.information(NULL, "提示:", "即将泄压, 请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1088. m_step = Test_Step_reliefPressure; //如果压力还在4.5以上, 可以不调压
  1089. }
  1090. break;
  1091. case QMessageBox::No:
  1092. m_step = Test_Step_End;
  1093. break;
  1094. default:
  1095. break;
  1096. }
  1097. }
  1098. }else{
  1099. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1100. m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = start_value;
  1101. m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = end_value;
  1102. delta = end_value - start_value;
  1103. m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = delta;
  1104. if(Ret_Exit == ret_v){
  1105. bQuit = true;
  1106. }else {
  1107. if(delta <= m_tank.m_comparts[m_compartmentid].m_standarditem.valve_threshold){
  1108. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
  1109. bPassed = true;
  1110. }else{
  1111. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = false;
  1112. bPassed = false;
  1113. }
  1114. m_tank.m_comparts[m_compartmentid].m_result.valve_test_finished = true;
  1115. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1116. emit sigValveResult(m_compartmentid+1, delta_str, bPassed);
  1117. emit sigState(m_compartmentid+1, "油气回收阀测试,结束", "");
  1118. m_step = Test_Step_End;
  1119. }
  1120. }
  1121. }
  1122. break;
  1123. case Test_Step_reliefPressure:
  1124. {
  1125. if(m_stage == Test_Stage_SysTest){
  1126. emit sigState(m_compartmentid+1, "油气回收系统测试结束,开始泄压", "");
  1127. }else if(m_stage == Test_Stage_ValveTest){
  1128. emit sigState(m_compartmentid+1, "油气回收阀测试,开始泄压", "");
  1129. }
  1130. texpect.volume = 0;
  1131. texpect.target = 0.0;
  1132. texpect.timeout = 120;//m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
  1133. ret_v = Pressure_Relief(texpect);
  1134. if(Ret_Exit == ret_v){
  1135. bQuit = true;
  1136. }else if(Ret_OK == ret_v){
  1137. m_step = Test_Step_KeepPressure;
  1138. if(m_stage == Test_Stage_ValveTest){
  1139. m_step = Test_Step_KeepPressure;
  1140. }else{
  1141. m_step = Test_Step_End;
  1142. }
  1143. }else{
  1144. ret_btn = QMessageBox::NoButton;
  1145. notice_str = "泄压超时, 是否重试?";
  1146. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1147. switch (ret_btn)
  1148. {
  1149. case QMessageBox::Retry:
  1150. break;
  1151. case QMessageBox::Abort:
  1152. bQuit = true;
  1153. break;
  1154. case QMessageBox::Ok:
  1155. bQuit = true;
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. break;
  1163. default:
  1164. break;
  1165. }
  1166. if(Test_Step_End == m_step){
  1167. break;
  1168. }
  1169. }
  1170. }else{
  1171. //负压测试, 暂不支持
  1172. m_stage = Test_Stage_SysTest;
  1173. qDebug(" negative test is not support now ");
  1174. }
  1175. }else{
  1176. //self test
  1177. if(m_direction == Test_Direction_Positive){
  1178. while(m_bRunning && (bQuit == false)){
  1179. switch(m_step){
  1180. case Test_Step_Start:
  1181. {
  1182. emit sigState(m_compartmentid+1, "开始", "");
  1183. if(false == self_checking()){
  1184. ret_btn = m_msgbox.information(NULL, "提示:", "通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  1185. }else{
  1186. test_init();
  1187. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀", QMessageBox::Ok, QMessageBox::Ok);
  1188. }
  1189. switch (ret_btn)
  1190. {
  1191. case QMessageBox::Ok:
  1192. qDebug()<<"Yes";
  1193. m_step = Test_Step_AddPressure;
  1194. break;
  1195. case QMessageBox::Abort:
  1196. bQuit = true;
  1197. break;
  1198. }
  1199. }
  1200. break;
  1201. case Test_Step_AddPressure:
  1202. {
  1203. emit sigState(m_compartmentid+1, "加压", "");
  1204. QThread::msleep(2000);
  1205. texpect.volume = 0;
  1206. texpect.target = 4.50;
  1207. texpect.timeout = 3*60; //5分钟
  1208. ret_v = PositivePressure_Add(texpect);
  1209. if(Ret_Exit == ret_v){
  1210. bQuit = true;
  1211. }else if(Ret_OK == ret_v){
  1212. m_step = Test_Step_KeepPressure;
  1213. }else{
  1214. ret_btn = QMessageBox::NoButton;
  1215. if(Ret_Timeout == ret_v){
  1216. notice_str = "正压加压超时, 是否重试?";
  1217. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1218. }else{
  1219. notice_str = "继电器通信失败, 程序终止";
  1220. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1221. }
  1222. switch (ret_btn)
  1223. {
  1224. case QMessageBox::Retry:
  1225. break;
  1226. case QMessageBox::Abort:
  1227. bQuit = true;
  1228. break;
  1229. case QMessageBox::Ok:
  1230. bQuit = true;
  1231. break;
  1232. default:
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. break;
  1238. case Test_Step_KeepPressure:
  1239. {
  1240. emit sigState(m_compartmentid+1, "保压", "");
  1241. // 检测 5分钟都没超限,就是通过了
  1242. float delta = 0;
  1243. float start_value = 0.0;
  1244. float end_value = 0.0;
  1245. bool bPassed = true;
  1246. ret_v = Pressure_Keep(5*60, start_value, end_value);
  1247. if(Ret_Exit == ret_v){
  1248. bQuit = true;
  1249. }else {
  1250. delta = start_value - end_value;
  1251. if(delta <= 0.5){
  1252. bPassed = true;
  1253. }else{
  1254. bPassed = false;
  1255. }
  1256. //QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1257. QString info_str = "初始压力:";
  1258. info_str += QString("%1").arg(start_value, 4,'f',2,QLatin1Char('0'));
  1259. info_str += ", 结束压力:";
  1260. info_str += QString("%1").arg(end_value, 4,'f',2,QLatin1Char('0'));
  1261. info_str += ", Deta:";
  1262. info_str += QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1263. emit sigSelfTestResult(m_compartmentid+1, info_str, bPassed);
  1264. emit sigState(m_compartmentid+1, "结束", "");
  1265. m_step = Test_Step_reliefPressure;
  1266. }
  1267. }
  1268. break;
  1269. case Test_Step_reliefPressure:
  1270. {
  1271. ret_btn = m_msgbox.information(NULL, "提示:", "栓测任务结束,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1272. m_step = Test_Step_End;
  1273. switch (ret_btn)
  1274. {
  1275. case QMessageBox::Yes:
  1276. qDebug()<<"Yes";
  1277. {
  1278. TestExpect expect;
  1279. expect.target = 0.0;
  1280. expect.volume = 0;
  1281. expect.timeout = 30;
  1282. ret_v = Pressure_Relief(expect);
  1283. if(Ret_Exit == ret_v){
  1284. bQuit = true;
  1285. }
  1286. }
  1287. break;
  1288. default:
  1289. break;
  1290. }
  1291. }
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. if(Test_Step_End == m_step){
  1297. break;
  1298. }
  1299. }
  1300. }else{ //negtive
  1301. }
  1302. }
  1303. if(Test_Step_End == m_step){
  1304. ret_btn = QMessageBox::NoButton;
  1305. }else{
  1306. if(true == bQuit){
  1307. if(Ret_Exit == ret_v){ // 主动退出
  1308. ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1309. }else if(Ret_Timeout == ret_v){ // 超时退出
  1310. ret_btn = m_msgbox.information(NULL, "提示:", "程序超时退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1311. }else{//通信错误退出
  1312. ret_btn = m_msgbox.information(NULL, "提示:", "程序通信错误退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1313. }
  1314. }
  1315. }
  1316. switch (ret_btn)
  1317. {
  1318. case QMessageBox::Yes:
  1319. Pressure_Relief();
  1320. break;
  1321. default:
  1322. break;
  1323. }
  1324. if(m_bRunning){
  1325. m_bRunning = false;
  1326. emit sigStop(m_compartmentid+1);
  1327. }
  1328. Valve all_v(&m_SerialUi3, ALL_VALVE);
  1329. all_v.Close();
  1330. qDebug("doTest >>>leave ");
  1331. }