-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestworker.cpp
More file actions
126 lines (113 loc) · 3.49 KB
/
Copy pathtestworker.cpp
File metadata and controls
126 lines (113 loc) · 3.49 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
#include "testworker.h"
#include <QDate>
#include <QDebug>
#include <QDir>
#include <QFile>
#include "libp8020/libp8020.h"
#include "protocol.h"
TestWorker::TestWorker(P8020Device* const aDevice, QObject* const aParent)
: QObject(aParent)
, mDevice(aDevice)
{
}
auto
enquote(const QString& aIn) -> QString
{
// Same as in libp8020: using an established CSV library would be the sensible
// thing to do, but being sensible has no place here.
bool hasQuotationMark = false;
bool hasComma = false;
for (auto it : aIn) {
if (it == '"') {
hasQuotationMark = true;
} else if (it == ',') {
hasComma = true;
}
}
if (!hasQuotationMark && !hasComma) {
return { aIn };
};
QString out = aIn;
if (hasQuotationMark) {
out = out.replace('"', "\"\"");
}
return QString("\"%1\"").arg(out);
}
void
TestWorker::runTest(const QSharedPointer<Protocol> aProtocol,
const TestCallback aCallback,
void* const aCallbackData,
const QString& aSpecimen,
const QString& aSubject,
const QString& aComment)
{
QDir logDir(QDir::homePath() + "/fit_testing/");
if (!logDir.exists()) {
qInfo() << "log directory does not exist, attempting to create it";
// QDir does not offer an API to create the directory it points to, there's
// either mkdir(someSubdir), or this.
if (!logDir.mkpath(logDir.path())) {
// TODO: introduce some kind of user-friendly error handling.
qWarning() << "Unable to create log dir (" << logDir.path() << ")";
return;
}
}
QFile testLog(logDir.path() + "/ftl_log.csv");
QIODeviceBase::OpenMode openOptions = QIODeviceBase::WriteOnly;
if (testLog.exists()) {
openOptions |= QIODeviceBase::Append;
}
if (!testLog.open(openOptions)) {
// TODO: introduce some kind of user-friendly error handling.
qWarning() << "Unable to open log (" << testLog.fileName()
<< "): " << testLog.errorString();
return;
}
QTextStream stream(&testLog);
const TestConfig* testConfig;
switch (aProtocol->tag) {
case Protocol::BUILTIN_CONFIG:
testConfig = aProtocol->builtinConfig;
break;
case Protocol::BUILTIN_CONFIG_ID:
assert(false && "not supported yet - needs to be moved into Protocol");
break;
}
P8020TestResult* result =
p8020_device_run_test(mDevice, testConfig, aCallback, aCallbackData);
emit testCompleted();
if (result == nullptr) {
return;
}
auto date = QDateTime::currentDateTime().toString("yyyy_MM_dd");
auto exerciseCount = p8020_test_result_get_exercise_count(result);
for (size_t i = 0; i < exerciseCount; i += 12) {
// TODO: make this robust against non-ASCII specimen and subjects.
stream << enquote(aSpecimen) << ",";
for (size_t j = i; j < i + 12; j++) {
if (j >= exerciseCount) {
stream << ",";
continue;
}
double ff =
p8020_test_result_get_fit_factor(result, /* device_id */ 0, j);
if (ff >= 10) {
stream << QString::number(ff, 'f', 0);
} else {
stream << QString::number(ff, 'f', 1);
}
stream << ",";
}
stream << enquote(aComment) << "," << enquote(aSubject) << "," << date
<< "," << aProtocol->id() << "\n";
}
testLog.close();
// TODO: log raw data?
// TODO: save to DB?
p8020_test_result_free(result);
}
TestWorker::~TestWorker()
{
// Also ensures that we cleanly disconnect (exit external control mode).
p8020_device_free(mDevice);
}