-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadlog.h
More file actions
227 lines (174 loc) · 6.57 KB
/
Copy pathreadlog.h
File metadata and controls
227 lines (174 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <vector>
#include <string>
#include "log.h"
using namespace std;
#define BASE_VISUAL_LIMIT 300 //lmite di dati visualizzabili in una volta nel grafico
const string BLF_NAME_FILE = "TXT\\blf_name.txt";
const string DBC_NAME_FILE = "TXT\\dbc_names.txt";
const string ID_NAME_FILE = "TXT\\id_names.txt";
const string LOG_DATA_FILE = "TXT\\log_datas.txt";
const string OPEN_INFO_FILE = "TXT\\open_info.txt";
// funzione che trasmorma un oggetto LogMsg in una stringa in formato csv
string log_to_string(LogMsg in_log_msg) {
string out_string = "";
return out_string + to_string(in_log_msg.get_timestamp()) + "," + to_string(in_log_msg.get_id()) + "," + to_string(in_log_msg.get_data()) + "," + to_string(in_log_msg.get_channel());
}
//funzione per eseguire script python per leggere i dati da file blf
void run_python_file(string& file_name) {
ifstream open_file(OPEN_INFO_FILE);
string command;
open_file >> command;
command += (" " + file_name);
open_file.close();
system(command.c_str());
}
// funzione per estrarre il nome di un file dato il suo percorso
string getFileName(const string filePath) {
size_t lastSlash = filePath.find_last_of("\\/"); // Trova l'ultimo separatore
if (lastSlash != std::string::npos) {
return filePath.substr(lastSlash + 1); // Estrae il nome del file
}
return filePath; // Se non ci sono separatori, restituisce l'intera stringa
}
//ricerca un messaggio di log in base all'ID (utilizzata in list_foreach_id) e ne restituisce l'indice in list_for_id
int search_msg_by_id(vector<LogSignal>& in_list, int id_to_search) {
int index_id = 0;
for (int i = 0; i < in_list.size(); i++) {
if (in_list[i].first().get_id() == id_to_search)
return index_id;
index_id++;
}
return -1;
}
//legge i dati di ogni messaggio contenuto nel file txt in formato csv e li memorizza in un vettore di oggetti Log
void read_data_from_txt(vector<LogMsg>& L_log)
{
string filename = "read_blf.py";
run_python_file(filename);
//Lettura file txt con dati del file blf
ifstream log_file(LOG_DATA_FILE);
if (log_file.is_open()) {
string str_timestamp, str_datas, str_id, str_channel, str_name;
while (getline(log_file, str_timestamp, ',')) {
getline(log_file, str_datas, ',');
getline(log_file, str_id, ',');
getline(log_file, str_channel, ',');
getline(log_file, str_name);
istringstream tk_tm(str_timestamp);
double tmp_timestamp;
tk_tm >> tmp_timestamp;
istringstream tk_dt(str_datas);
double tmp_datas;
tk_dt >> tmp_datas;
istringstream tk_id(str_id);
int tmp_id;
tk_id >> tmp_id;
istringstream tk_ch(str_channel);
int tmp_ch;
tk_ch >> tmp_ch;
//Creazione di un array di elementi della classe Log
LogMsg tmp_log(tmp_timestamp, tmp_datas, tmp_id, tmp_ch);
L_log.push_back(tmp_log);
}
}
log_file.close();
//cancello il contenuto del file txt usato per passare i dati
ofstream file(LOG_DATA_FILE, ios::trunc);
}
//crea un vettore di vettori di log (di un canale dato) divisi per ID
void list_foreach_id(vector<LogMsg>& log_list, vector<LogSignal>& list_for_id, int in_channel) {
for (int i = 0; i < log_list.size(); i++) {
LogMsg tmp_log = log_list[i];
if (tmp_log.get_channel() != in_channel)
continue;
int id_index = search_msg_by_id(list_for_id, tmp_log.get_id());
if (id_index >= 0) {
list_for_id[id_index].add_log_msg(tmp_log);
}
else {
vector<LogMsg> new_id_vector;
new_id_vector.push_back(tmp_log);
LogSignal new_signal;
new_signal.set_logs(new_id_vector);
list_for_id.push_back(new_signal);
}
}
return;
}
//estraggo da un vettore di log i timestamp e i data, visual limit imposta il numero di dati estratti
void extract_time_data(vector<LogMsg> in_logs, vector<double>& in_time, vector<double>& in_data, int visual_limit) {
for (int i = visual_limit - BASE_VISUAL_LIMIT; i < in_logs.size(); i++) {
if (i > visual_limit)
break;
LogMsg tmp_log = in_logs[i];
in_time.push_back(tmp_log.get_timestamp());
in_data.push_back(tmp_log.get_data());
}
}
// Scrittura file in formato csv (il file txt è presente nella cartella dell'eseguibile)
bool write_csv_file(LogSignal to_export_logs) {
string tmp_log_name = to_export_logs.get_name();
ofstream export_csv_file("CSV\\csv_" + tmp_log_name + ".txt");
for (int i = 0; i < to_export_logs.get_logs().size(); i++) {
export_csv_file << log_to_string(to_export_logs.get_logs()[i]) << endl;
}
export_csv_file.close();
return true;
}
// funzione per scrivere il path della dbc inserita dal textbox nel file dbc_names.txt
void write_dbc_path(wxTextCtrl* dbc_name) {
ofstream dbc_names_file;
dbc_names_file.open(DBC_NAME_FILE, ios_base::app);
wxString tmp_dbc_path = dbc_name->GetValue();
dbc_names_file << tmp_dbc_path << endl;
dbc_names_file.close();
}
// funzione per cancellare il path di una dbc dal file dbc_names.txt
void delete_dbc_path(wxString dbc_name) {
ifstream dbc_names_file(DBC_NAME_FILE);
string tmp_dbc;
vector<string> tmp_dbc_list;
while (dbc_names_file >> tmp_dbc) { //copio i path delle dbc presenti nel file in un vettore temporaneo
if (dbc_name.ToStdString() != getFileName(tmp_dbc)) { //tranne il path di quella da cancellare
tmp_dbc_list.push_back(tmp_dbc);
}
}
dbc_names_file.close();
ofstream dbc_names_out_file(DBC_NAME_FILE); //riscrivo il file con i path rimanenti
for (int i = 0; i < tmp_dbc_list.size(); i++) {
dbc_names_out_file << tmp_dbc_list[i] << endl;
}
}
// funzione per scrivere nel listbox delle dbc il nome di ciascuna dbc presente nel file dbc_names.txt
void write_dbc_list(wxListBox* dbc_list) {
ifstream dbc_names_file(DBC_NAME_FILE);
string str_path;
while (dbc_names_file >> str_path) {
string str_name = getFileName(str_path);
wxString wx_name;
wx_name << str_name;
dbc_list->AppendString(wx_name);
}
dbc_names_file.close();
}
// funzione per assegnare il nome a un messaggio di log corrispondente al suo id
void assign_name_to_id(vector<LogSignal>& log_signals) {
ifstream id_names_file(ID_NAME_FILE);
string str_id, str_name;
while (getline(id_names_file, str_id, ',')) {
getline(id_names_file, str_name);
istringstream tk_id(str_id);
int tmp_id;
tk_id >> tmp_id;
for (int i = 0; i < log_signals.size(); i++) {
if (log_signals[i].first().get_id() == tmp_id) {
log_signals[i].set_name(str_name);
}
}
}
id_names_file.close();
}