您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

1054 行
29 KiB

  1. /*******************************************************************************
  2. * *
  3. * Société de Transports de Montréal. *
  4. * 2012 - 2013 *
  5. * *
  6. * Projet Zones Tests *
  7. * *
  8. * *
  9. * *
  10. *******************************************************************************/
  11. /*
  12. Description:
  13. Cette classe est responsable de la sauvegarde et de l'ouverture des fichiers
  14. log de passage des trains.
  15. */
  16. /* ************************************************************************** */
  17. /* Revision:
  18. ### YYYMMDD JFM
  19. Verision d'origine.
  20. ### YYYYMMDD Description du besoin ou du bug
  21. Description du changement.
  22. */
  23. /* ************************************************************************** */
  24. #include "TrainLogFileMgr.h"
  25. #include <QFile>
  26. #include <QTextStream>
  27. #include "LogMgr.h"
  28. CTrainLogFileMgr CTrainLogFileMgr::mSingleton;
  29. CTrainLogFileMgr::CTrainLogFileMgr()
  30. {
  31. }
  32. //unsigned int CTrainLogFileMgr::SaveTrainLog(QString LogFilePathName, CZT1Log *ZT1Log, QList<CZTDetectionData *> *ZT1DetectionsLog,QString StationName)
  33. unsigned int CTrainLogFileMgr::SaveTrainLog(QString LogFilePathName, CZT1Log *ZT1Log, QVector<CZTDetectionData *> *ZT1DetectionsLog,QString StationName)
  34. {
  35. QFile* BinaryLogFile = new QFile(LogFilePathName);
  36. if(BinaryLogFile)
  37. {
  38. if(BinaryLogFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == false)
  39. {
  40. qDebug("Could not create log file : %s",LogFilePathName.toLatin1().data());
  41. delete BinaryLogFile;
  42. return RET_ERROR;
  43. }
  44. }
  45. else
  46. return RET_ERROR;
  47. QDataStream * OutputStream = new QDataStream(BinaryLogFile);
  48. OutputStream->setVersion(QDataStream::Qt_4_8);
  49. quint32 MagicNbr = 0xDEADBEEF;
  50. quint32 NbLogEntry = 0,NbDetections = 0;
  51. quint32 LogType = ZT1_LOG_TYPE;
  52. NbLogEntry = ZT1Log->mZT1LogData.size();
  53. NbDetections = ZT1DetectionsLog->size();
  54. quint32 TrainType = 0;
  55. quint32 NbElements = 0;
  56. quint64 ThreadDataStartTime = 0, ThreadDataEndTime = 0;
  57. qreal MeanSpeed = 0;
  58. QDateTime DateTime;
  59. //Compute some stats
  60. int SpeedSampleCount = 0;
  61. bool TrainTypeFound = false;
  62. unsigned int NbBogie = 0;
  63. bool ThreadStartTimeFound = false, ThreadEndTimeFound = false;
  64. for(unsigned int i = 0; i < NbLogEntry; i++)
  65. {
  66. if(ZT1Log->mZT1LogData.at(i)->mZT1ThreadData != 0)
  67. {
  68. //get the train type
  69. if(!TrainTypeFound)
  70. {
  71. if(ZT1Log->mZT1LogData.at(i)->mZT1ThreadData->mTrainType != TRAIN_TYPE_UNKNOWN)
  72. {
  73. TrainType = ZT1Log->mZT1LogData.at(i)->mZT1ThreadData->mTrainType;
  74. TrainTypeFound = true;
  75. }
  76. }
  77. //Get the rank count for the train
  78. if(ZT1Log->mZT1LogData.at(i)->mZT1ThreadData->mRank > NbBogie)
  79. NbBogie = ZT1Log->mZT1LogData.at(i)->mZT1ThreadData->mBogie;
  80. //Find the time limits
  81. if(ThreadStartTimeFound == false)
  82. {
  83. ThreadStartTimeFound = true;
  84. ThreadDataStartTime = ZT1Log->mZT1LogData.at(i)->mTimestamp;
  85. }
  86. //Compute the mean speed
  87. if(ZT1Log->mZT1LogData.at(i)->mZT1ThreadData->mTrainSpeed != 0)
  88. {
  89. MeanSpeed += ZT1Log->mZT1LogData.at(i)->mZT1ThreadData->mTrainSpeed;
  90. SpeedSampleCount++;
  91. }
  92. }
  93. else
  94. {
  95. //Find the time limits
  96. if(ThreadStartTimeFound == true && ThreadEndTimeFound == false)
  97. {
  98. ThreadEndTimeFound = true;
  99. ThreadDataEndTime = ZT1Log->mZT1LogData.at(i-1)->mTimestamp;
  100. }
  101. }
  102. }
  103. MeanSpeed /= SpeedSampleCount;
  104. NbElements = NbBogie/6;
  105. DateTime = ZT1Log->mZT1LogData.first()->mDateTime;
  106. *OutputStream << MagicNbr << LogType << NbLogEntry << NbDetections;
  107. *OutputStream << StationName;
  108. *OutputStream << ZT1Log->mZT1Flags;
  109. *OutputStream << TrainType << NbElements << ThreadDataStartTime << ThreadDataEndTime << MeanSpeed << DateTime;
  110. //write detections
  111. for(unsigned int i =0; i < NbDetections; i++)
  112. {
  113. *OutputStream << *ZT1DetectionsLog->at(i);
  114. }
  115. //write train passage log
  116. for(unsigned int i = 0; i < NbLogEntry; i++)
  117. {
  118. *OutputStream << *ZT1Log->mZT1LogData.at(i);
  119. }
  120. #ifdef USE_ANALOG_ACQUISITION
  121. //write analog data if present.
  122. if(ZT1Log->mZT1Flags.mAnalogTracePresent == 1)
  123. {
  124. for(unsigned int i = 0; i < NbLogEntry; i++)
  125. {
  126. *OutputStream << ZT1Log->mZT1LogData.at(i)->mAnalogData;
  127. }
  128. }
  129. #endif
  130. BinaryLogFile->close();
  131. delete BinaryLogFile;
  132. delete OutputStream;
  133. return RET_OK;
  134. }
  135. unsigned int CTrainLogFileMgr::SaveTrainLog(QString LogFilePathName, QVector<CZT2LogData *> *ZT2Log, QVector<CZTDetectionData *> *ZT2DetectionsLog, QString StationName)
  136. {
  137. QFile* BinaryLogFile = new QFile(LogFilePathName);
  138. if(BinaryLogFile)
  139. {
  140. if(BinaryLogFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered) == false)
  141. {
  142. qDebug("Could not create log file : %s",LogFilePathName.toLatin1().data());
  143. delete BinaryLogFile;
  144. return RET_ERROR;
  145. }
  146. }
  147. else
  148. return RET_ERROR;
  149. QDataStream * OutputStream = new QDataStream(BinaryLogFile);
  150. OutputStream->setVersion(QDataStream::Qt_4_8);
  151. quint32 MagicNbr = 0xDEADBEEF;
  152. quint32 NbLogEntry,NbDetections;
  153. quint32 LogType = ZT2_LOG_TYPE;
  154. NbLogEntry = ZT2Log->size();
  155. NbDetections = ZT2DetectionsLog->size();
  156. quint32 NbElements = 0;
  157. QDateTime DateTime = ZT2Log->at(0)->mDateTime;
  158. //Get some stats
  159. for(int j = 0; j < ZT2Log->size(); j++)
  160. {
  161. if(ZT2Log->at(j)->mZT2ThreadData != 0)
  162. {
  163. //Get the rank count for the train
  164. if(ZT2Log->at(j)->mZT2ThreadData->mRank > NbElements)
  165. NbElements = ZT2Log->at(j)->mZT2ThreadData->mBogie;
  166. }
  167. }
  168. NbElements /= 6; //6 bogies per element
  169. *OutputStream << MagicNbr << LogType << NbLogEntry << NbDetections;
  170. *OutputStream << StationName;
  171. *OutputStream << NbElements << DateTime;
  172. //write detections
  173. for(unsigned int i =0; i < NbDetections; i++)
  174. {
  175. *OutputStream << *ZT2DetectionsLog->at(i);
  176. }
  177. //write train passage log
  178. for(unsigned int i = 0; i < NbLogEntry; i++)
  179. {
  180. *OutputStream << *ZT2Log->at(i);
  181. }
  182. BinaryLogFile->close();
  183. delete BinaryLogFile;
  184. delete OutputStream;
  185. return RET_OK;
  186. }
  187. //It is the responsibility of the caller to make shure TargetElement is empty
  188. CLogElement* CTrainLogFileMgr::OpenTrainLog(QString LogFilePathName,unsigned int &Retvalue,CLogElement* TargetElement,bool LoadData)
  189. {
  190. // if(!ZT1Log->isEmpty() || !ZT1DetectionsLog->isEmpty())
  191. // return RET_ERROR;
  192. int LogFileVersion;
  193. bool FileProtected = false;
  194. if(QFileInfo(LogFilePathName).suffix() != "bin")
  195. {
  196. Retvalue = RET_ERROR;
  197. return 0;
  198. }
  199. QFile* BinaryLogFile = new QFile(LogFilePathName);
  200. if(BinaryLogFile)
  201. {
  202. if(BinaryLogFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered) == false)
  203. {
  204. Retvalue = RET_ERROR;
  205. delete BinaryLogFile;
  206. return 0;
  207. }
  208. }
  209. else
  210. {
  211. Retvalue = RET_ERROR;
  212. return 0;
  213. }
  214. QDataStream * InputStream = new QDataStream(BinaryLogFile);
  215. InputStream->setVersion(QDataStream::Qt_4_8);
  216. quint32 MagicNbr,NbLogEntry,NbDetections,LogType;
  217. bool IsDetectFunctionFlagsValid = false;
  218. *InputStream >> MagicNbr;
  219. if(MagicNbr == 0xDEADBEEF)
  220. {
  221. LogFileVersion = 1;
  222. FileProtected = false;
  223. }
  224. else if(MagicNbr == 0xDEADBEEF+1)
  225. {
  226. LogFileVersion = 2;
  227. FileProtected = false;
  228. }
  229. else if(MagicNbr == 0xDEADBEEF+2)
  230. {
  231. LogFileVersion = 1;
  232. FileProtected = true;
  233. }
  234. else if(MagicNbr == 0xDEADBEEF+3)
  235. {
  236. LogFileVersion = 2;
  237. FileProtected = true;
  238. }
  239. else if(MagicNbr >= 0xDEADBEEF+15)
  240. {
  241. LogFileVersion = 3;
  242. qint32 OutilZTFlags[10];
  243. for(int i = 0; i < 10; i++)
  244. {
  245. *InputStream >> OutilZTFlags[i];
  246. }
  247. if(OutilZTFlags[OUTILZT_FILE_PROTECTED_FLAG] == 1)
  248. {
  249. FileProtected = true;
  250. }
  251. else
  252. {
  253. FileProtected = false;
  254. }
  255. if(MagicNbr == 0xDEADBEEF+16)
  256. {
  257. IsDetectFunctionFlagsValid = true;
  258. }
  259. }
  260. else
  261. {
  262. qDebug(qPrintable(QString().sprintf("Fichier de passage invalide (Magic number) %s",qPrintable(LogFilePathName))));
  263. BinaryLogFile->close();
  264. delete BinaryLogFile;
  265. delete InputStream;
  266. Retvalue = RET_ERROR;
  267. return 0;
  268. }
  269. *InputStream >> LogType;
  270. if(LogType == ZT1_LOG_TYPE)
  271. {
  272. CZT1LogElement *PassageLog;
  273. if(TargetElement == 0)
  274. {
  275. PassageLog = new CZT1LogElement();
  276. }
  277. else
  278. {
  279. PassageLog = (CZT1LogElement*)TargetElement; //It is the responsibility of the caller to make shure TargetElement is empty
  280. }
  281. PassageLog->mDetectionFlagsValid = IsDetectFunctionFlagsValid;
  282. PassageLog->mLogFileName = LogFilePathName;
  283. PassageLog->mFileProtected = FileProtected;
  284. *InputStream >> NbLogEntry;
  285. *InputStream >> NbDetections;
  286. *InputStream >> PassageLog->mStationName;
  287. if(LogFileVersion == 1)
  288. {
  289. *InputStream >> PassageLog->mFlags.mExtPGOffset
  290. >> PassageLog->mFlags.mIntPGOffset
  291. >> PassageLog->mFlags.mPGCalibrationON
  292. >> PassageLog->mFlags.mPGTresholdValue
  293. >> PassageLog->mFlags.mAnalogTracePresent;
  294. PassageLog->mFlags.mTrainCompo1 = 0;
  295. PassageLog->mFlags.mTrainCompo2 = 0;
  296. PassageLog->mFlags.mTrainCompo3 = 0;
  297. PassageLog->mFlags.mModbusTrainType = MODBUS_CC_TRAIN_TYPE_INVALID_NOT_UPDATED;
  298. PassageLog->mFlags.mZT1ITI = ZT_PRIMARY_ITI;
  299. }
  300. else if(LogFileVersion == 2)
  301. {
  302. *InputStream >> PassageLog->mFlags.mExtPGOffset
  303. >> PassageLog->mFlags.mIntPGOffset
  304. >> PassageLog->mFlags.mPGCalibrationON
  305. >> PassageLog->mFlags.mPGTresholdValue
  306. >> PassageLog->mFlags.mAnalogTracePresent
  307. >> PassageLog->mFlags.mIsProblematicPassage;
  308. PassageLog->mFlags.mTrainCompo1 = 0;
  309. PassageLog->mFlags.mTrainCompo2 = 0;
  310. PassageLog->mFlags.mTrainCompo3 = 0;
  311. PassageLog->mFlags.mModbusTrainType = MODBUS_CC_TRAIN_TYPE_INVALID_NOT_UPDATED;
  312. PassageLog->mFlags.mZT1ITI = ZT_PRIMARY_ITI;
  313. }
  314. else if(LogFileVersion == 3)
  315. {
  316. *InputStream >> PassageLog->mFlags;
  317. }
  318. *InputStream >> PassageLog->mTrainType;
  319. *InputStream >> PassageLog->mNbElements;
  320. *InputStream >> PassageLog->mThreadDataStartTime;
  321. *InputStream >> PassageLog->mThreadDataEndTime;
  322. *InputStream >> PassageLog->mMeanSpeed;
  323. *InputStream >> PassageLog->mPassageDateTime;
  324. for(unsigned int i = 0; i < NbDetections; i++)
  325. {
  326. CZTDetectionData *NewDetection = new CZTDetectionData();
  327. *InputStream >> *NewDetection;
  328. PassageLog->mZTDetections.append(NewDetection);
  329. }
  330. if(LoadData == true)
  331. {
  332. for(unsigned int i = 0; i < NbLogEntry; i++)
  333. {
  334. CZT1LogData *NewLogChunk = new CZT1LogData();
  335. *InputStream >> *NewLogChunk;
  336. PassageLog->mZTLogData.append(NewLogChunk);
  337. }
  338. #ifdef USE_ANALOG_ACQUISITION
  339. if(PassageLog->mFlags.mAnalogTracePresent == 1)
  340. {
  341. for(int i = 0; i < PassageLog->mZTLogData.size(); i++)
  342. {
  343. *InputStream >> PassageLog->mZTLogData.at(i)->mAnalogData;
  344. }
  345. }
  346. #endif
  347. }
  348. BinaryLogFile->close();
  349. delete BinaryLogFile;
  350. delete InputStream;
  351. Retvalue = RET_OK;
  352. return (CZT1LogElement*) PassageLog;
  353. }
  354. else if(LogType == ZT2_LOG_TYPE)
  355. {
  356. quint32 NbElements;
  357. QDateTime DateTime;
  358. CZT2LogElement *PassageLog;
  359. if(TargetElement == 0)
  360. {
  361. PassageLog = new CZT2LogElement();
  362. }
  363. else
  364. {
  365. PassageLog = (CZT2LogElement*) TargetElement; //It is the responsibility of the caller to make shure TargetElement is empty
  366. }
  367. PassageLog->mDetectionFlagsValid = IsDetectFunctionFlagsValid;
  368. *InputStream >> NbLogEntry;
  369. *InputStream >> NbDetections;
  370. *InputStream >> PassageLog->mStationName;
  371. if(LogFileVersion == 2)
  372. {
  373. *InputStream >> PassageLog->mFlags.mIsProblematicPassage;
  374. PassageLog->mFlags.mModbusTrainType = MODBUS_CC_TRAIN_TYPE_INVALID_NOT_UPDATED;
  375. PassageLog->mFlags.mTrainCompo1 = 0;
  376. PassageLog->mFlags.mTrainCompo2 = 0;
  377. PassageLog->mFlags.mTrainCompo3 = 0;
  378. }
  379. else if(LogFileVersion == 3)
  380. {
  381. *InputStream >> PassageLog->mFlags;
  382. }
  383. else
  384. {
  385. PassageLog->mFlags.mIsProblematicPassage = 2;
  386. PassageLog->mFlags.mModbusTrainType = MODBUS_CC_TRAIN_TYPE_INVALID_NOT_UPDATED;
  387. PassageLog->mFlags.mTrainCompo1 = 0;
  388. PassageLog->mFlags.mTrainCompo2 = 0;
  389. PassageLog->mFlags.mTrainCompo3 = 0;
  390. }
  391. *InputStream >> NbElements;
  392. *InputStream >> DateTime;
  393. PassageLog->mNbElements = NbElements;
  394. PassageLog->mLogFileName = LogFilePathName;
  395. PassageLog->mPassageDateTime = DateTime;
  396. PassageLog->mFileProtected = FileProtected;
  397. for(unsigned int i = 0; i < NbDetections; i++)
  398. {
  399. CZTDetectionData *NewDetection = new CZTDetectionData();
  400. *InputStream >> *NewDetection;
  401. PassageLog->mZTDetections.append(NewDetection);
  402. }
  403. if(LoadData == true)
  404. {
  405. for(unsigned int i = 0; i < NbLogEntry; i++)
  406. {
  407. CZT2LogData *NewLogChunk = new CZT2LogData();
  408. *InputStream >> *NewLogChunk;
  409. PassageLog->mZTLogData.append(NewLogChunk);
  410. }
  411. }
  412. BinaryLogFile->close();
  413. delete BinaryLogFile;
  414. delete InputStream;
  415. Retvalue = RET_OK;
  416. return PassageLog;
  417. }
  418. else
  419. {
  420. qDebug("Invalid log type in file %s",LogFilePathName.toLatin1().data());
  421. Retvalue = RET_ERROR;
  422. return 0;
  423. }
  424. }
  425. unsigned int CTrainLogFileMgr::SetTrainLogProtected(bool IsProtected, QString LogFilePathName)
  426. {
  427. QFile* BinaryLogFile = new QFile(LogFilePathName);
  428. if(BinaryLogFile)
  429. {
  430. if(BinaryLogFile->open(QIODevice::ReadWrite | QIODevice::Unbuffered) == false)
  431. {
  432. qDebug("Could not Open log file to set protection : %s",LogFilePathName.toLatin1().data());
  433. delete BinaryLogFile;
  434. return RET_ERROR;
  435. }
  436. }
  437. else
  438. return RET_ERROR;
  439. QDataStream * Stream = new QDataStream(BinaryLogFile);
  440. Stream->setVersion(QDataStream::Qt_4_8);
  441. quint32 MagicNbr;
  442. *Stream >> MagicNbr;
  443. if(IsProtected == true)
  444. {
  445. if(MagicNbr == 0xDEADBEEF + 2 || MagicNbr == 0xDEADBEEF + 3)
  446. {
  447. //file is already protected
  448. }
  449. else if(MagicNbr == 0xDEADBEEF || MagicNbr == 0xDEADBEEF + 1)
  450. {
  451. MagicNbr += 2;
  452. BinaryLogFile->seek(0);
  453. *Stream << MagicNbr;
  454. }
  455. else if(MagicNbr == 0xDEADBEEF + 15)
  456. {
  457. //Use the reserved registers inside the file...
  458. qint32 Flags[10];
  459. for(int i = 0; i < 10; i++)
  460. {
  461. *Stream >> Flags[i];
  462. }
  463. Flags[OUTILZT_FILE_PROTECTED_FLAG] = 1;
  464. BinaryLogFile->seek(0);
  465. *Stream >> MagicNbr; //Dummy read;
  466. for(int i = 0; i < 10; i++)
  467. {
  468. *Stream << Flags[i];
  469. }
  470. }
  471. else
  472. {
  473. //Log File Error
  474. BinaryLogFile->close();
  475. qDebug("Invalid log file magic number to set protection : %s",LogFilePathName.toLatin1().data());
  476. delete BinaryLogFile;
  477. }
  478. }
  479. else
  480. {
  481. if(MagicNbr == 0xDEADBEEF + 2 || MagicNbr == 0xDEADBEEF + 3)
  482. {
  483. //file is already protected
  484. MagicNbr -= 2;
  485. BinaryLogFile->seek(0);
  486. *Stream << MagicNbr;
  487. }
  488. else if(MagicNbr == 0xDEADBEEF || MagicNbr == 0xDEADBEEF + 1)
  489. {
  490. //file is already unprotected
  491. }
  492. else if(MagicNbr == 0xDEADBEEF + 15)
  493. {
  494. //Use the reserved registers inside the file...
  495. qint32 Flags[10];
  496. for(int i = 0; i < 10; i++)
  497. {
  498. *Stream >> Flags[i];
  499. }
  500. Flags[OUTILZT_FILE_PROTECTED_FLAG] = 0;
  501. BinaryLogFile->seek(0);
  502. *Stream >> MagicNbr; //Dummy read;
  503. for(int i = 0; i < 10; i++)
  504. {
  505. *Stream << Flags[i];
  506. }
  507. }
  508. else
  509. {
  510. //Log File Error
  511. BinaryLogFile->close();
  512. qDebug("Invalid log file magic number to set protection : %s",LogFilePathName.toLatin1().data());
  513. delete BinaryLogFile;
  514. }
  515. }
  516. BinaryLogFile->close();
  517. qDebug("Invalid log file magic number to set protection : %s",LogFilePathName.toLatin1().data());
  518. delete BinaryLogFile;
  519. return RET_OK;
  520. }
  521. //unsigned int CTrainLogFileMgr::SaveCSVFile(QString CSVFilePathName, CZT1Log *ZT1Log, QList<CZTDetectionData *> *ZT1DetectionsLog, QString StationName)
  522. unsigned int CTrainLogFileMgr::SaveCSVFile(QString CSVFilePathName, CZT1Log *ZT1Log, QVector<CZTDetectionData *> *ZT1DetectionsLog, QString StationName)
  523. {
  524. return SaveCSVFile(CSVFilePathName, &ZT1Log->mZT1LogData, &ZT1Log->mZT1Flags, ZT1DetectionsLog,StationName);
  525. }
  526. //unsigned int CTrainLogFileMgr::SaveCSVFile(QString CSVFilePathName, QList<CZT1LogData*> *ZT1Log, CZT1FlagsData *ZT1Flags, QList<CZTDetectionData *> *ZT1DetectionsLog, QString StationName)
  527. unsigned int CTrainLogFileMgr::SaveCSVFile(QString CSVFilePathName, QVector<CZT1LogData*> *ZT1Log, CZT1FlagsData *ZT1Flags, QVector<CZTDetectionData *> *ZT1DetectionsLog, QString StationName)
  528. {
  529. QFile CSVFile(CSVFilePathName);
  530. if(!CSVFile.open(QIODevice::QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate))
  531. return RET_ERROR;
  532. QTextStream CSVStream(&CSVFile);
  533. CSVStream << "Log de passage du train dans la ZT1\n";
  534. CSVStream << QString::fromUtf8("Station : ") << StationName << "\n";
  535. CSVStream << QString::fromUtf8("Valeur seuil Pneu de guidage : ") << ZT1Flags->mPGTresholdValue << "\n";
  536. CSVStream << QString::fromUtf8("Valeur zérotage PG Extérieur : ") << ZT1Flags->mExtPGOffset << "\n";
  537. CSVStream << QString::fromUtf8("Valeur zérotage PG Intérieur : ") << ZT1Flags->mIntPGOffset << "\n";
  538. if(ZT1Flags->mPGCalibrationON == 1)
  539. {
  540. CSVStream << "Calibration PG en cours lors du passage: OUI\n";
  541. }
  542. else
  543. CSVStream << "Calibration PG en cours lors du passage: NON\n";
  544. CSVStream << QString::fromUtf8("Nombre de déclenchements : ") << ZT1DetectionsLog->size() << "\n";
  545. if(ZT1DetectionsLog->size() > 0)
  546. {
  547. for(int i = 0; i < ZT1DetectionsLog->size(); i++)
  548. {
  549. CSVStream << QString::fromUtf8("Déclenchement ") << i+1 << ": " << ZT1DetectionsLog->at(i)->mTimeStamp << " - " << QString::fromUtf8(CZTData::GetErrorString(ZT1DetectionsLog->at(i)->mDetectionID)) << " au rang " << ZT1DetectionsLog->at(i)->mRank << "\n";
  550. }
  551. }
  552. CSVStream << "\n";
  553. CSVStream << "Date,Heure,Timestamp,CI,CDV Approche,CDV ZT1,S1,S2,FN,PPI,PPE,PG,Vitesse,Bogie,Rang,Laser Ext,Laser Int,Train";
  554. if(ZT1Flags->mAnalogTracePresent == 1)
  555. {
  556. CSVStream << ",4-20mA SDF";
  557. }
  558. CSVStream << "\n";
  559. for(int i = 0; i < ZT1Log->size(); i++)
  560. {
  561. CSVStream << ZT1Log->at(i)->mDateTime.toString("yyyy-MM-dd, hh:mm:ss:zzz") << ",";
  562. CSVStream << ZT1Log->at(i)->mTimestamp << ",";
  563. CSVStream << ZT1Log->at(i)->mCIZT1 << ",";
  564. CSVStream << ZT1Log->at(i)->mCDVApproach_ZT1 << ",";
  565. CSVStream << ZT1Log->at(i)->mCDVARM_ZT1 << ",";
  566. if(ZT1Log->at(i)->mZT1ThreadData != 0)
  567. {
  568. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mS1 << ",";
  569. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mS2 << ",";
  570. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mFN << ",";
  571. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mPInt << ",";
  572. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mPExt << ",";
  573. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mPG << ",";
  574. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mTrainSpeed << ",";
  575. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mBogie << ",";
  576. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mRank << ",";
  577. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mPGExtValue << ",";
  578. CSVStream << ZT1Log->at(i)->mZT1ThreadData->mPGIntValue << ",";
  579. CSVStream << CZTData::GetTrainTypeString(ZT1Log->at(i)->mZT1ThreadData->mTrainType);
  580. }
  581. else
  582. {
  583. CSVStream << "0,0,0,0,0,0,0,0,0,0,0,0";
  584. //CSVStream <<"\n";
  585. }
  586. if(ZT1Flags->mAnalogTracePresent == 1)
  587. {
  588. CSVStream << "," << ZT1Log->at(i)->mAnalogData ;
  589. }
  590. CSVStream << "\n";
  591. }
  592. CSVFile.flush();
  593. CSVFile.close();
  594. return RET_OK;
  595. }
  596. unsigned int CTrainLogFileMgr::SaveCSVFile(QString CSVFilePathName, QVector<CZT2LogData *> *ZT2Log, QVector<CZTDetectionData *> *ZT2DetectionsLog, QString StationName)
  597. //unsigned int CTrainLogFileMgr::SaveCSVFile(QString CSVFilePathName, QVector<CZT2LogData *> *ZT2Log, QVector<CZTDetectionData *> *ZT2DetectionsLog, QString StationName)
  598. {
  599. QFile CSVFile(CSVFilePathName);
  600. if(!CSVFile.open(QIODevice::QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate))
  601. return RET_ERROR;
  602. QTextStream CSVStream(&CSVFile);
  603. CSVStream << "Log de passage du train dans la ZT2\n";
  604. CSVStream << QString::fromUtf8("Station : ") << StationName << "\n";
  605. CSVStream << QString::fromUtf8("Nombre de déclenchements : ") << ZT2DetectionsLog->size() << "\n";
  606. if(ZT2DetectionsLog->size() > 0)
  607. {
  608. for(int i = 0; i < ZT2DetectionsLog->size(); i++)
  609. {
  610. CSVStream << QString::fromUtf8("Déclenchement ") << i+1 << ": " << ZT2DetectionsLog->at(i)->mTimeStamp << " - " << QString::fromUtf8(CZTData::GetErrorString(ZT2DetectionsLog->at(i)->mDetectionID)) << " au rang " << ZT2DetectionsLog->at(i)->mRank << "\n";
  611. }
  612. CSVStream << "\n";
  613. }
  614. CSVStream << "Date,Heure,Timestamp,CI,CDV ZT2,S1,PPI,PPE,Bogie,Rang\n";
  615. for(int i = 0; i < ZT2Log->size(); i++)
  616. {
  617. CSVStream << ZT2Log->at(i)->mDateTime.toString("yyyy-MM-dd, hh:mm:ss:zzz") << ",";
  618. CSVStream << ZT2Log->at(i)->mTimestamp << ",";
  619. CSVStream << ZT2Log->at(i)->mCIZT2 << ",";
  620. CSVStream << ZT2Log->at(i)->mCDVARM_ZT2 << ",";
  621. if(ZT2Log->at(i)->mZT2ThreadData != 0)
  622. {
  623. CSVStream << ZT2Log->at(i)->mZT2ThreadData->mS1 << ",";
  624. CSVStream << ZT2Log->at(i)->mZT2ThreadData->mPPInt << ",";
  625. CSVStream << ZT2Log->at(i)->mZT2ThreadData->mPPExt << ",";
  626. CSVStream << ZT2Log->at(i)->mZT2ThreadData->mBogie << ",";
  627. CSVStream << ZT2Log->at(i)->mZT2ThreadData->mRank << ",";
  628. }
  629. else
  630. {
  631. CSVStream << "0,0,0,0,0";
  632. CSVStream <<"\n";
  633. }
  634. }
  635. CSVFile.flush();
  636. CSVFile.close();
  637. return RET_OK;
  638. }
  639. //Cette fonction a été écrite pour des fins de tests et n'est PAS du tout robuste.
  640. //utiliser avec précaution.
  641. unsigned int CTrainLogFileMgr::SaveBINFromCSV(QString CSVFilePathName)
  642. {
  643. QFile CSVFile(CSVFilePathName);
  644. if(!CSVFile.open(QIODevice::ReadOnly|QIODevice::Text))
  645. return RET_ERROR;
  646. QTextStream CSVStream(&CSVFile);
  647. bool IsZT1 = true;
  648. QString Line;
  649. QStringList LineElements;
  650. QString StationName;
  651. Line.clear();
  652. LineElements.clear();
  653. Line = CSVStream.readLine(); //Log de passage du train dans la ZT1
  654. if(Line.contains("ZT1") == true)
  655. {
  656. IsZT1 = true;
  657. }
  658. else if(Line.contains("ZT2") == true)
  659. {
  660. IsZT1 = false;
  661. }
  662. else
  663. {
  664. //invalid file
  665. CSVFile.close();
  666. return RET_ERROR;
  667. }
  668. Line = CSVStream.readLine(); //Station : Snowdon
  669. LineElements = Line.split(":");
  670. if(LineElements.isEmpty())
  671. {
  672. CSVFile.close();
  673. return RET_ERROR;
  674. }
  675. StationName = LineElements.at(1);
  676. Line.clear();
  677. if(IsZT1 == true)
  678. {
  679. CZT1Log *ZT1LOG = new CZT1Log();
  680. Line = CSVStream.readLine(); //Valeur seuil Pneu de guidage : 15625
  681. LineElements = Line.split(":");
  682. if(LineElements.isEmpty())
  683. {
  684. CSVFile.close();
  685. return RET_ERROR;
  686. }
  687. ZT1LOG->mZT1Flags.mPGTresholdValue = LineElements.at(1).toInt();
  688. Line.clear();
  689. Line = CSVStream.readLine(); //Valeur zérotage PG Extérieur : 128457
  690. LineElements = Line.split(":");
  691. if(LineElements.isEmpty())
  692. {
  693. CSVFile.close();
  694. return RET_ERROR;
  695. }
  696. ZT1LOG->mZT1Flags.mExtPGOffset = LineElements.at(1).toInt();
  697. Line.clear();
  698. Line = CSVStream.readLine(); //Valeur zérotage PG Intérieur : 1300637
  699. LineElements = Line.split(":");
  700. if(LineElements.isEmpty())
  701. {
  702. CSVFile.close();
  703. return RET_ERROR;
  704. }
  705. ZT1LOG->mZT1Flags.mIntPGOffset = LineElements.at(1).toInt();
  706. Line.clear();
  707. Line = CSVStream.readLine(); //Calibration PG en cours lors du passage: NON
  708. if(Line.contains("OUI") == true)
  709. {
  710. ZT1LOG->mZT1Flags.mPGCalibrationON = true;
  711. }
  712. else if(Line.contains("NON") == true)
  713. {
  714. ZT1LOG->mZT1Flags.mPGCalibrationON = false;
  715. }
  716. else
  717. {
  718. CSVFile.close();
  719. return RET_ERROR;
  720. }
  721. Line.clear();
  722. int NbDetections;
  723. Line = CSVStream.readLine(); //Nombre de déclenchements : 12
  724. LineElements = Line.split(":");
  725. if(LineElements.isEmpty())
  726. {
  727. CSVFile.close();
  728. return RET_ERROR;
  729. }
  730. NbDetections = LineElements.at(1).toInt();
  731. Line.clear();
  732. //Ignore detections...
  733. for(int i = 0; i < NbDetections; i++)
  734. {
  735. Line = CSVStream.readLine();
  736. Line.clear();
  737. }
  738. //Skip the empty line after the detections.
  739. Line = CSVStream.readLine();
  740. Line.clear();
  741. //Skip the table header
  742. Line = CSVStream.readLine();
  743. Line.clear();
  744. quint32 LastS1 = 0, LastS2 = 0, LastFN = 0;
  745. quint32 S1Count = 0;
  746. quint32 S2Count = 0;
  747. quint32 FNCount = 0;
  748. while(CSVStream.atEnd() == false)
  749. {
  750. Line.clear();
  751. LineElements.clear();
  752. Line = CSVStream.readLine(); //Date Heure Timestamp CI CDV Approche CDV ZT1 S1 S2 FN PPI PPE PG Vitesse Bogie Rang Sonde Ext Sonde Int Train
  753. LineElements = Line.split(",");
  754. if(LineElements.count() != 18)
  755. {
  756. CSVFile.close();
  757. return RET_ERROR;
  758. }
  759. QDate date = QDate::fromString(LineElements.at(0),"yyyy-MM-dd");
  760. if(date.isValid() == false)
  761. {
  762. CSVFile.close();
  763. return RET_ERROR;
  764. }
  765. QTime time = QTime::fromString(LineElements.at(1).trimmed(),"hh:mm:ss:zzz");
  766. if(time.isValid() == false)
  767. {
  768. CSVFile.close();
  769. return RET_ERROR;
  770. }
  771. qint64 timestamp = LineElements.at(2).toLongLong();
  772. quint32 CIZT1 = LineElements.at(3).toUInt();
  773. quint32 CDVApproach_ZT1 = LineElements.at(4).toUInt();
  774. quint32 CDVARM_ZT1 = LineElements.at(5).toUInt();
  775. CZT1LogData *LogData;
  776. CZT1ThreadData *ThreadData;
  777. if(LineElements.at(17) == "0") //Check if ThreadData is valid
  778. {
  779. //Thread data not valid...
  780. LogData = new CZT1LogData();
  781. }
  782. else
  783. {
  784. ThreadData = new CZT1ThreadData();
  785. LogData = new CZT1LogData(ThreadData);
  786. quint32 S1 = LineElements.at(6).toUInt();
  787. quint32 S2 = LineElements.at(7).toUInt();
  788. quint32 FN = LineElements.at(8).toUInt();
  789. quint32 PInt = LineElements.at(9).toUInt();
  790. quint32 PExt = LineElements.at(10).toUInt();
  791. quint32 PG = LineElements.at(11).toUInt();
  792. qreal TrainSpeed = LineElements.at(12).toFloat();
  793. quint32 Bogie = LineElements.at(13).toUInt();
  794. quint32 Rank = LineElements.at(14).toUInt();
  795. qint32 PGIntValue = LineElements.at(15).toInt();
  796. qint32 PGExtValue = LineElements.at(16).toInt();
  797. quint32 TrainType;
  798. if(LineElements.at(17) == "Inconnu")
  799. {
  800. TrainType = TRAIN_TYPE_UNKNOWN;
  801. }
  802. else if(LineElements.at(17) == "MR63/73")
  803. {
  804. TrainType = TRAIN_TYPE_MR63_MR73;
  805. }
  806. else if(LineElements.at(17) == "MPM10")
  807. {
  808. TrainType = TRAIN_TYPE_MPM10;
  809. }
  810. else
  811. {
  812. CSVFile.close();
  813. return RET_ERROR;
  814. }
  815. if(S1 == 1 && LastS1 == 0)
  816. S1Count++;
  817. if(S2 == 1 && LastS2 == 0)
  818. S2Count++;
  819. if(FN == 1 && LastFN == 0)
  820. FNCount++;
  821. LastS1 = S1;
  822. LastS2 = S2;
  823. LastFN = FN;
  824. ThreadData->mTimeStamp = timestamp; //nanosecs
  825. ThreadData->mDateTime.setDate(date);
  826. ThreadData->mDateTime.setTime(time);
  827. ThreadData->mS1 = S1;
  828. ThreadData->mS2 = S2;
  829. ThreadData->mFN = FN;
  830. ThreadData->mPInt = PInt;
  831. ThreadData->mPExt = PExt;
  832. ThreadData->mPG = PG;
  833. ThreadData->mTrainSpeed = TrainSpeed;
  834. ThreadData->mBogie = Bogie;
  835. ThreadData->mRank = Rank;
  836. ThreadData->mPGIntValue = PGIntValue;
  837. ThreadData->mPGExtValue = PGExtValue;
  838. ThreadData->mS1Count = S1Count;
  839. ThreadData->mS2Count = S2Count;
  840. ThreadData->mFNCount = FNCount;
  841. ThreadData->mTrainType = TrainType;
  842. }
  843. LogData->mTimestamp = timestamp;
  844. LogData->mCIZT1 = CIZT1;
  845. LogData->mCDVApproach_ZT1 = CDVApproach_ZT1;
  846. LogData->mCDVARM_ZT1 = CDVARM_ZT1;
  847. LogData->mDateTime.setDate(date);
  848. LogData->mDateTime.setTime(time);
  849. ZT1LOG->mZT1LogData.append(LogData);
  850. }
  851. // QList<CZTDetectionData*> Dummy; //JFM
  852. QVector<CZTDetectionData*> Dummy;
  853. Dummy.clear();
  854. SaveTrainLog(CSVFilePathName.replace("csv","bin"),ZT1LOG,&Dummy,StationName);
  855. for(int i = 0; i < ZT1LOG->mZT1LogData.size(); i++)
  856. {
  857. delete ZT1LOG->mZT1LogData.at(i);
  858. }
  859. ZT1LOG->mZT1LogData.clear();
  860. delete ZT1LOG;
  861. }
  862. else //ZT1
  863. {
  864. }
  865. return RET_OK;
  866. }