source: flair-src/trunk/tools/FlairGCS/src/file_ui.cpp@ 443

Last change on this file since 443 was 257, checked in by Sanahuja Guillaume, 6 years ago

set file ui in gcs in blocking receaving mode

File size: 14.5 KB
RevLine 
[10]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[10]4// %flair:license}
[9]5#include "file_ui.h"
[234]6#include "communication.h"
7
[9]8#include <stdio.h>
9#include <cstring>
10#include <cstdlib>
11#include <fstream>
12#include <iostream>
13#include <QDir>
14#include <QDate>
15#include <QTextStream>
16#include <QGridLayout>
17#include <io_hdfile.h>
18#include <QtEndian>
19#include <QComboBox>
20#include <QPushButton>
21#include <QTextEdit>
22#include <QDialog>
23#include <QStringList>
24#include <QFormLayout>
[234]25#include <QThread>
[9]26
[234]27#include <unistd.h>
[9]28using namespace std;
29
[234]30file_ui::file_ui(UDTSOCKET socket,QString name): QObject() {
31 this->socket=socket;
32 this->name=name;
33
34 bool blocking = true;
35 if (UDT::setsockopt(socket, 0, UDT_SNDSYN, &blocking, sizeof(bool))!= 0) {
[244]36 fprintf(stderr,"UDT::setsockopt error (UDT_SNDSYN) %s\n",UDT::getlasterror().getErrorMessage());
[234]37 }
[257]38
39 if (UDT::setsockopt(socket, 0, UDT_RCVSYN, &blocking, sizeof(bool)) != 0) {
40 fprintf(stderr,"UDT::setsockopt error (UDT_RCVSYN) %s\n",UDT::getlasterror().getErrorMessage());
41 }
[234]42
43 linger _linger;
44 _linger.l_onoff=1;
45 _linger.l_linger=180;
46
47 if (UDT::setsockopt(socket, 0, UDT_LINGER, &_linger, sizeof(struct linger)) != 0)
[244]48 fprintf(stderr,"UDT::setsockopt error (UDT_LINGER) %s\n",UDT::getlasterror().getErrorMessage());
[234]49
[15]50 dialog = new QDialog();
[248]51
[15]52 dialog->setWindowTitle("log files");
53 QGridLayout *main_layout = new QGridLayout(dialog);
54 ok_button = new QPushButton("Ok", dialog);
55 log_text = new QTextEdit(dialog);
56 log_text->setReadOnly(true);
57 input_text = new QTextEdit(dialog);
58 input_text->setText("add your log comment here");
59 input_cleared = false;
60 ok_button->setEnabled(false);
[9]61
[15]62 main_layout->addWidget(log_text, 0, 0);
63 main_layout->addWidget(input_text, 1, 0);
[9]64
[15]65 QWidget *widget = new QWidget(dialog);
66 QFormLayout *formLayout = new QFormLayout(widget);
67 csv_combo = new QComboBox(widget);
68 formLayout->addRow(tr("save all log with following base time"), csv_combo);
69 csv_combo->addItem("(no base time)");
70 main_layout->addWidget(widget, 2, 0);
71 main_layout->addWidget(ok_button, 3, 0);
[9]72
[234]73 //connect for multithreaded stuffs
74 connect(ok_button, SIGNAL(clicked()), this, SLOT(save()),Qt::QueuedConnection);
75 connect(input_text, SIGNAL(cursorPositionChanged()), this,SLOT(clearInputText()),Qt::DirectConnection);
76 connect(this, SIGNAL(appendToLog(QString)), log_text, SLOT(append(QString)));
[9]77
[15]78 file_names = new QStringList();
[234]79
80 dialog->show();
[9]81}
82
[234]83file_ui::~file_ui() {
84 delete dialog;
85}
[9]86
[234]87void file_ui::receive(void) {
88 char *recv_buf;
89 int bytesRead;
90 bool flag_new_seq = true;
91 QString folder_name;
[244]92//ffprintf(stderr,stderr,"file_ui thread %x\n",thread());
[234]93 while(1) {
94 // receive file info
95 recv_buf = (char *)malloc(1024);
96 bytesRead = UDT::recvmsg(socket, recv_buf, 1024);
97 if (bytesRead <= 0) {
98 free(recv_buf);
99 break;
100 }
[9]101
[234]102 int size;
103 memcpy(&size, &recv_buf[1], sizeof(int));
104 if (recv_buf[0] == FILE_INFO_BIG_ENDIAN)
105 size = qFromBigEndian(size);
106
[244]107 // fprintf(stderr,"file_ui recu %i %x\n",bytesRead,recv_buf[0]);
[234]108 if ((recv_buf[0]==FILE_INFO_LITTLE_ENDIAN || recv_buf[0]==FILE_INFO_BIG_ENDIAN) && size>0) {
109 if (flag_new_seq == true) {
110 // create directory for storage
111 QDateTime dateTime = QDateTime::currentDateTime();
112 folder_name = dateTime.toString("yyyyMMdd_hhmm") + "_" + name;
113 if (QDir().exists(folder_name) == true) {
114 folder_name = dateTime.toString("yyyyMMdd_hhmm_ss") + "_" + name;
115 }
116 QDir().mkdir(folder_name);
117
118 flag_new_seq = false;
119 appendToLog("Creating directory " + folder_name);
120 }
121
122 QString file_name=QString::fromAscii((const char *)&recv_buf[5], bytesRead - 5);
123 QString file_path=folder_name+"/"+file_name;
124 appendToLog(QString("receiving %1 (%2 bytes)").arg(file_name).arg(size));
125 QFile fichier(file_path);
126
127 if (!fichier.open(QIODevice::WriteOnly)) {
128 appendToLog(" could not write to file!");
129 } else {
130 // receive file
131 recv_buf = (char *)realloc((void *)recv_buf, size);
132 bytesRead = UDT::recvmsg(socket, recv_buf, size);
133 if (bytesRead != size) {
[257]134 appendToLog(QString(" error receiving file! (%1/%2): %3").arg(bytesRead).arg(size).arg(UDT::getlasterror().getErrorMessage()));
[234]135 free(recv_buf);
136 break;
137 } else {
138 appendToLog(" ok");
139 }
140
141 QDataStream stream(&fichier);
142 stream.writeRawData(recv_buf, size);
143 fichier.close();
144
145 addFile(file_path);
146 }
147
148 free(recv_buf);
149 } else if (recv_buf[0] == END_SENDING_FILES) {
150 //end ack
151 UDT::sendmsg(socket,&recv_buf[0],1);
152 endOfFiles();
153 UDT::close(socket);
[244]154 fprintf(stderr,"disconnected from log files\n");
[234]155 break;
156 }
157 }
158}
159
[9]160void file_ui::addFile(QString file_path) {
[15]161 // framework sends dbt file then txt file
162 // when we receive txt, we have both files
163 // and we can convert it to .csv
164 if (file_path.endsWith(".dbt") == true) {
165 QString name =
166 file_path.section('/', -1); // remove path for displaying on combobox
167 csv_combo->addItem(name.replace(QString(".dbt"), QString(".csv")));
168 file_names->append(file_path.replace(QString(".dbt"), QString(".csv")));
169 }
[9]170
[15]171 if (file_path.endsWith(".txt") == true) {
172 dbt2csv(file_path.replace(QString(".txt"), QString(".dbt")));
173 }
[9]174}
175
176void file_ui::endOfFiles(void) {
[15]177 ok_button->setEnabled(true);
[9]178
[15]179 qint64 max_file_size = 0;
180 for (int i = 0; i < file_names->count(); i++) {
181 QFileInfo info(file_names->at(i));
182 if (info.size() > max_file_size) {
183 max_file_size = info.size();
[234]184 csv_combo->setCurrentIndex(i+1); // first item of combobox is already taken
[9]185 }
[15]186 }
[9]187}
188
[15]189void file_ui::dbt2csv(QString file_path) {
190 hdfile_t *dbtFile = NULL;
191 char *data;
192 QStringList data_type;
[9]193
[234]194 QString filename =file_path.section('/', -1); // remove path for displaying on logs
[15]195 appendToLog(QString("converting %1 to csv").arg(filename));
[9]196
[15]197 // open csv file
198 QString csv_filename = file_path;
199 csv_filename.replace(QString(".dbt"), QString(".csv"));
200 QFile csv_file(csv_filename);
[9]201
[15]202 if (!csv_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
203 appendToLog(" error opening csv file!");
204 return;
205 }
206 QTextStream out(&csv_file);
[9]207
[15]208 // open txt file
209 QString txt_filename = file_path;
210 txt_filename.replace(QString(".dbt"), QString(".txt"));
211 QFile txt_file(txt_filename);
[9]212
[15]213 if (!txt_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
214 appendToLog(" error opening txt file!");
215 return;
216 }
[9]217
[15]218 // read txt file
219 QTextStream txt_in(&txt_file);
220 txt_in.readLine(); // time us
221 txt_in.readLine(); // time ns
222 while (1) {
223 if (txt_in.atEnd() == true)
224 break;
225 QString txt_line = txt_in.readLine();
[234]226 data_type.append(txt_line.section("(",-1)); // on part de la fin pour trouver la premiere parenthese ouvrante
[244]227 // fprintf(stderr,"type %s\n",txt_line.section("(",-1).toLocal8Bit().constData());
[15]228 }
229 txt_file.close();
[9]230
[15]231 dbtFile = open_hdfile(file_path.toLocal8Bit().data(), READ_MODE);
[9]232
[15]233 if (!dbtFile) {
234 appendToLog(" error opening dbt file!");
235 return;
236 }
237 data = (char *)malloc(dbtFile->h.DataSize);
238 if (data == NULL) {
239 appendToLog(" error malloc!");
240 return;
241 }
[9]242
[15]243 bool dataWritten = false;
244 while (1) {
245 road_time_t time;
246 road_timerange_t tr = 0;
247 int offset = 0;
248 QTextStream csv_line;
[9]249
[15]250 if (read_hdfile(dbtFile, (void *)data, &time, &tr) == 0) {
251 break;
252 }
253 dataWritten = true;
[9]254
[15]255 out << time << "," << tr;
256 for (int i = 0; i < data_type.size(); i++) {
[51]257 if (data_type.at(i) == "double)") {
258 double *value = (double *)(data + offset);
259 offset += sizeof(double);
260 out << "," << *value;
261 } else if (data_type.at(i) == "float)") {
[15]262 float *value = (float *)(data + offset);
263 offset += sizeof(float);
264 out << "," << *value;
265 } else if (data_type.at(i) == "int8_t)") {
266 int8_t *value = (int8_t *)(data + offset);
267 offset += sizeof(int8_t);
268 out << "," << *value;
[51]269 } else if (data_type.at(i) == "uint8_t)") {
270 uint8_t *value = (uint8_t *)(data + offset);
271 offset += sizeof(uint8_t);
272 out << "," << *value;
[214]273 } else if (data_type.at(i) == "int16_t)") {
274 int16_t *value = (int16_t *)(data + offset);
275 offset += sizeof(int16_t);
276 out << "," << *value;
277 } else if (data_type.at(i) == "uint16_t)") {
278 uint16_t *value = (uint16_t *)(data + offset);
279 offset += sizeof(uint16_t);
280 out << "," << *value;
[15]281 } else {
282 appendToLog(QString(" unhandled type: %1").arg(data_type.at(i)));
283 }
[9]284 }
285
[15]286 out << "\n";
287 }
288
289 if (!dataWritten) {
290 // empty file!
291 out << "0,0"; // timr
292 for (int i = 0; i < data_type.size(); i++) {
293 out << ",0";
[9]294 }
[15]295 out << "\n";
296 }
[9]297
[15]298 csv_file.close();
299 close_hdfile(dbtFile);
300 if (data != NULL)
301 free(data);
[9]302
[15]303 appendToLog(" ok");
[9]304}
305
[15]306void file_ui::clearInputText(void) {
307 if (input_cleared == false) {
308 input_cleared = true;
309 input_text->clear();
310 }
[9]311}
312
[15]313void file_ui::save(void) {
314 save_comment();
315 if (csv_combo->currentIndex() != 0) {
316 save_csv();
317 save_txt();
318 }
[9]319
[15]320 emit finished();
[9]321}
322
[15]323void file_ui::save_comment(void) {
324 QString folder_name = file_names->at(0).section('/', 0, -2);
[9]325
[15]326 QString filename = folder_name + "/commentaire.txt";
327 QFile file(filename);
328 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
[244]329 fprintf(stderr,"file_ui::save_comment: erreur ouverture fichier %s\n",
[15]330 filename.toLocal8Bit().constData());
331 QTextStream out(&file);
[9]332
[15]333 out << input_text->toPlainText();
334 file.close();
[9]335}
336
[15]337void file_ui::save_csv(void) {
338 // global csv file
339 QString folder_name = file_names->at(0).section('/', 0, -2);
340 QString filename = folder_name + "/all_logs.csv";
341 QFile global_file(filename);
342 if (!global_file.open(QIODevice::WriteOnly | QIODevice::Text))
[244]343 fprintf(stderr,"file_ui::save_csv: erreur ouverture fichier %s\n",
[15]344 filename.toLocal8Bit().constData());
345 QTextStream out(&global_file);
[9]346
[15]347 // reference csv file
348 filename = file_names->at(csv_combo->currentIndex() - 1);
349 QFile ref_file(filename);
[244]350 // fprintf(stderr,"file_ui::save_csv: ref %s\n",filename.toLocal8Bit().constData());
[15]351 if (!ref_file.open(QIODevice::ReadOnly | QIODevice::Text))
[244]352 fprintf(stderr,"file_ui::save_csv: erreur ouverture ficher %s\n",
[15]353 filename.toLocal8Bit().constData());
[9]354
[15]355 // other csv files
356 int j = 0;
357 QFile m_file[file_names->count() - 1];
358 QTextStream m_in[file_names->count() - 1];
359 for (int i = 0; i < file_names->count(); i++) {
360 if (i == csv_combo->currentIndex() - 1)
361 continue;
362 filename = file_names->at(i);
363 m_file[j].setFileName(filename);
364 if (!m_file[j].open(QIODevice::ReadOnly | QIODevice::Text))
[244]365 fprintf(stderr,"file_ui::save_csv: erreur ouverture ficher %s\n",
[15]366 filename.toLocal8Bit().constData());
367 m_in[j].setDevice(&m_file[j]);
368 j++;
369 }
[9]370
[15]371 // init
372 QTextStream ref_in(&ref_file);
373 QString m_line[file_names->count() - 1];
374 QString m_line_prev[file_names->count() - 1];
375 for (int i = 0; i < file_names->count() - 1; i++) {
376 m_line[i] = m_in[i].readLine();
377 m_line_prev[i] = m_line[i];
378 }
[9]379
[15]380 // organize csv files in one file
381 while (1) {
382 if (ref_in.atEnd() == true)
383 break;
384 QString ref_line = ref_in.readLine();
[9]385
[15]386 qint64 ref_us = ref_line.section(',', 0, 0).toLongLong();
387 int ref_ns = ref_line.section(',', 1, 1).toInt();
[244]388 // fprintf(stderr,"ref %lld %i\n",ref_us,ref_ns);
[9]389
[15]390 for (int i = 0; i < file_names->count() - 1; i++) {
391 qint64 csv_us = m_line[i].section(',', 0, 0).toLongLong();
392 int csv_ns = m_line[i].section(',', 1, 1).toInt();
[244]393 // fprintf(stderr,"m %lld %i\n",csv_us,csv_ns);
[9]394
[15]395 while (is_greater(ref_us, csv_us, ref_ns, csv_ns) == true) {
396 m_line_prev[i] = m_line[i];
397 if (m_in[i].atEnd() == true)
398 break;
399 m_line[i] = m_in[i].readLine();
400 csv_us = m_line[i].section(',', 0, 0).toLongLong();
401 csv_ns = m_line[i].section(',', 1, 1).toInt();
[244]402 // fprintf(stderr,"m %lld %i\n",csv_us,csv_ns);
[15]403 }
404 csv_us = m_line_prev[i].section(',', 0, 0).toLongLong();
405 csv_ns = m_line_prev[i].section(',', 1, 1).toInt();
[244]406 // fprintf(stderr,"m ok %lld %i\n",csv_us,csv_ns);
[9]407
[15]408 ref_line += "," + m_line_prev[i].section(',', 2);
[9]409 }
410
[15]411 out << ref_line << "\n";
412 }
413
414 global_file.close();
415 ref_file.close();
416 for (int i = 0; i < file_names->count() - 1; i++)
417 m_file[i].close();
[9]418}
419
[15]420void file_ui::save_txt(void) {
421 // global txt file
422 QString folder_name = file_names->at(0).section('/', 0, -2);
423 QString filename = folder_name + "/all_logs.txt";
424 QFile global_file(filename);
425 if (!global_file.open(QIODevice::WriteOnly | QIODevice::Text))
[244]426 fprintf(stderr,"file_ui::save_txt: erreur ouverture ficher %s\n",
[15]427 filename.toLocal8Bit().constData());
428 QTextStream out(&global_file);
[9]429
[15]430 // reference txt file
431 filename = file_names->at(csv_combo->currentIndex() - 1);
432 filename.replace(QString(".csv"), QString(".txt"));
433 QFile ref_file(filename);
434 if (!ref_file.open(QIODevice::ReadOnly | QIODevice::Text))
[244]435 fprintf(stderr,"file_ui::save_txt: erreur ouverture ficher %s\n",
[15]436 filename.toLocal8Bit().constData());
[9]437
[15]438 QTextStream ref_in(&ref_file);
439 QString current_line = ref_in.readLine();
440 int nb_lines = 1;
441 while (current_line != NULL) {
442 out << current_line << "\n";
443 ;
444 current_line = ref_in.readLine();
445 nb_lines++;
446 }
[9]447
[15]448 // other txt files
449 for (int i = 0; i < file_names->count(); i++) {
450 if (i == csv_combo->currentIndex() - 1)
451 continue;
452 filename = file_names->at(i);
453 filename.replace(QString(".csv"), QString(".txt"));
454 QFile txt_file(filename);
455 if (!txt_file.open(QIODevice::ReadOnly | QIODevice::Text))
[244]456 fprintf(stderr,"file_ui::save_txt: erreur ouverture ficher %s\n",
[15]457 filename.toLocal8Bit().constData());
458 QTextStream txt_in(&txt_file);
459 txt_in.readLine(); // time us
460 txt_in.readLine(); // time ns
461 current_line = txt_in.readLine();
462 while (current_line != NULL) {
463 out << nb_lines << ":" << current_line.section(':', 1) << "\n";
464 ;
465 current_line = txt_in.readLine();
466 nb_lines++;
[9]467 }
[15]468 txt_file.close();
469 }
470 global_file.close();
471 ref_file.close();
[9]472}
473
[15]474bool file_ui::is_greater(qint64 ref_us, qint64 csv_us, int ref_ns, int csv_ns) {
475 if (ref_us == csv_us) {
476 if (ref_ns > csv_ns) {
477 return true;
478 } else {
479 return false;
[9]480 }
[15]481 }
482 if (ref_us > csv_us) {
483 return true;
484 } else {
485 return false;
486 }
[9]487}
Note: See TracBrowser for help on using the repository browser.