-
Notifications
You must be signed in to change notification settings - Fork 30
/
SessionManager.cpp
206 lines (158 loc) · 6.14 KB
/
SessionManager.cpp
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
#include "SessionManager.h"
SessionManager::SessionManager(const QSharedPointer<QSettings> &settings, QObject *parent) : QObject(parent), _settings(settings)
{
//Database
_db = new DataBase(settings, this);
_db->connectToDataBase();
// _db->createTable();
auto sep = _settings->value("Report/csv_separator").toByteArray();
if(sep == "")
_csv_separator = ";";
else
_csv_separator = sep;
_runningNumber = _settings->value("Report/runningNumber").toInt();
}
SessionManager::~SessionManager()
{
}
void SessionManager::logDutInfo(Dut dut)
{
_runningNumber++;
_settings->setValue("Report/runningNumber", _runningNumber);
DutRecord record;
QString zeroFields;
if(_runningNumber < 10)
zeroFields = "000";
else if(_runningNumber < 100)
zeroFields = "00";
else if(_runningNumber < 1000)
zeroFields = "0";
record.runningNumber = zeroFields + QString().setNum(_runningNumber);
record.id = dut["id"].toString();
record.no = dut["no"].toString();
record.error = dut["error"].toString().remove('\n').remove('\r').remove(';').remove(',');
record.batchNumber = _batchNumber;
record.method = _method;
record.operatorName = _operatorName;
record.timeStamp = _startTime;
if(dut["state"].toInt() != DutState::tested)
{
_failedCount++;
record.state = "FAILED";
}
else
{
_successCount++;
record.state = "PASSED";
}
_records.push_back(record);
emit sessionStatsChanged();
}
void SessionManager::clear()
{
_operatorName = "";
_startTime = "";
_batchNumber = "";
_batchInfo = "";
_method = "";
_successCount = 0;
_failedCount = 0;
_testCyclesCount = 0;
emit sessionStatsChanged();
}
void SessionManager::writeDutRecordsToDatabase()
{
if(_records.isEmpty())
return;
for(auto & record : _records)
{
record.cycleNo = QString().setNum(_testCyclesCount);
_db->insertIntoTable(record);
if(record.state == "PASSED" && record.no.size())
{
emit printLabel(record);
}
}
//Text log file
// QFile file(_settings->value("workDirectory").toString() + "/reports/log.txt");
// file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
// for(auto & record : _records)
// {
// file.write( record.runningNumber.toLocal8Bit() + "; "
// + "ID: " + record.id.toLocal8Bit() + "; "
// + "SOCKET: " + record.no.toLocal8Bit() + "; "
// + "TEST CYCLE: " + record.cycleNo.toLocal8Bit() + "; "
// + "BATCH NUMBER: " + record.batchNumber.toLocal8Bit() + "; "
// + "TEST METHOD: " + record.method.toLocal8Bit() + "; "
// + "OPERATOR: " + record.operatorName.toLocal8Bit() + "; "
// + "DATE: " + record.timeStamp.toLocal8Bit() + "; "
// + "RESULT: " + record.state.toLocal8Bit());
// if(record.error.size())
// {
// file.write(" ERROR DESC: " + record.error.toLocal8Bit() + "\n");
// }
// else
// {
// file.write("\n");
// }
// }
// file.close();
//CSV log file
QFile csv_file(_settings->value("workDirectory").toString() + "/reports/" + "ALL_" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".csv");
csv_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
for(auto & record : _records)
{
csv_file.write( record.runningNumber.toLocal8Bit() + _csv_separator
+ record.id.toLocal8Bit() + _csv_separator
+ record.no.toLocal8Bit() + _csv_separator
+ record.cycleNo.toLocal8Bit() + _csv_separator
+ record.batchNumber.toLocal8Bit() + _csv_separator
+ record.method.toLocal8Bit() + _csv_separator
+ record.operatorName.toLocal8Bit() + _csv_separator
+ record.timeStamp.toLocal8Bit() + _csv_separator
+ record.state.toLocal8Bit());
if(record.error.size())
{
csv_file.write(_csv_separator + record.error.toLocal8Bit());
}
csv_file.write(_csv_separator + "\n");
}
csv_file.close();
//Brief CSV log file
QFile brief_csv_file(_settings->value("workDirectory").toString() + "/reports/" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".csv");
brief_csv_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
for(auto & record : _records)
{
brief_csv_file.write( record.runningNumber.toLocal8Bit() + _csv_separator
+ record.cycleNo.toLocal8Bit() + _csv_separator
+ record.no.toLocal8Bit() + _csv_separator
+ record.id.toLocal8Bit());
if(record.state == "FAILED")
{
brief_csv_file.write(_csv_separator + "FALIED");
}
brief_csv_file.write(_csv_separator + "\n");
}
brief_csv_file.close();
//CSV printer log file
if(_method.size())
{
QFile printer_csv_file(_settings->value("workDirectory").toString() + "/reports/" + "PRINTER_" + QDateTime::currentDateTime().toString("yyyy-MM-dd") + " " + _method + ".csv");
printer_csv_file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
for(auto & record : _records)
{
printer_csv_file.write( record.runningNumber.toLocal8Bit() + _csv_separator
+ record.cycleNo.toLocal8Bit() + "."
+ record.no.toLocal8Bit() + _csv_separator
+ record.id.toLocal8Bit() + _csv_separator
+ record.batchNumber.toLocal8Bit());
if(record.state == "FAILED")
{
printer_csv_file.write(_csv_separator + "FAILED");
}
printer_csv_file.write(_csv_separator + "\n");
}
printer_csv_file.close();
}
_records.clear();
}