Better TX/RX precision on CSV export

This commit is contained in:
Alex Spataru 2021-02-09 23:33:46 -05:00
parent 5bdc1783d7
commit 06bf2823b6
4 changed files with 20 additions and 17 deletions

View File

@ -53,9 +53,9 @@ Export::Export()
auto io = IO::Manager::getInstance();
auto jp = JSON::Generator::getInstance();
auto te = Misc::TimerEvents::getInstance();
connect(te, SIGNAL(timeout1Hz()), this, SLOT(writeValues()));
connect(jp, SIGNAL(jsonChanged()), this, SLOT(updateValues()));
connect(io, SIGNAL(connectedChanged()), this, SLOT(closeFile()));
connect(io, &IO::Manager::connectedChanged, this, &Export::closeFile);
connect(jp, &JSON::Generator::jsonChanged, this, &Export::updateValues);
connect(te, &Misc::TimerEvents::timeout1Hz, this, &Export::writeValues);
LOG_TRACE() << "Class initialized";
}
@ -151,7 +151,7 @@ void Export::closeFile()
*/
void Export::writeValues()
{
for (int k = 0; k < qMin(m_jsonList.count(), 10); ++k)
for (int k = 0; k < m_jsonList.count(); ++k)
{
// Get project title & cell values
auto json = m_jsonList.first().second;
@ -280,7 +280,7 @@ void Export::writeValues()
* Obtains the latest JSON dataframe & appends it to the JSON list
* (which is later read & written to the CSV file).
*/
void Export::updateValues()
void Export::updateValues(const QJsonDocument &document, const QDateTime &time)
{
// Ignore if device is not connected
if (!IO::Manager::getInstance()->connected())
@ -291,11 +291,11 @@ void Export::updateValues()
return;
// Get & validate JSON document
auto json = JSON::Generator::getInstance()->document().object();
auto json = document.object();
if (json.isEmpty())
return;
// Update JSON list
auto pair = qMakePair<QDateTime, QJsonObject>(QDateTime::currentDateTime(), json);
auto pair = qMakePair<QDateTime, QJsonObject>(time, json);
m_jsonList.append(pair);
}

View File

@ -67,7 +67,7 @@ public slots:
private slots:
void writeValues();
void updateValues();
void updateValues(const QJsonDocument &document, const QDateTime &time);
private:
QFile m_csvFile;

View File

@ -259,7 +259,7 @@ void Generator::writeSettings(const QString &path)
* This function is set to public in order to allow the CSV-replay feature to
* work by replacing the data/json input source.
*/
void Generator::setJsonDocument(const QJsonDocument &document)
void Generator::setJsonDocument(const QJsonDocument &document, const QDateTime &time)
{
if (document.object().isEmpty())
return;
@ -270,7 +270,7 @@ void Generator::setJsonDocument(const QJsonDocument &document)
if (m_frame.read(m_document.object()))
emit frameChanged();
emit jsonChanged();
emit jsonChanged(document, time);
}
/**
@ -308,7 +308,7 @@ void Generator::readData(const QByteArray &data)
// Create new worker thread to read JSON data
QThread *thread = new QThread;
JSONWorker *worker = new JSONWorker(data);
JSONWorker *worker = new JSONWorker(data, QDateTime::currentDateTime());
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
@ -322,9 +322,10 @@ void Generator::readData(const QByteArray &data)
// JSON worker object (executed for each frame on a new thread)
//----------------------------------------------------------------------------------------
JSONWorker::JSONWorker(const QByteArray &data)
JSONWorker::JSONWorker(const QByteArray &data, const QDateTime &time)
{
m_data = data;
m_time = time;
}
void JSONWorker::process()
@ -383,7 +384,7 @@ void JSONWorker::process()
// No parse error, update UI & reset error counter
if (error.error == QJsonParseError::NoError)
emit jsonReady(document);
emit jsonReady(document, m_time);
// Delete object in 500 ms
QTimer::singleShot(500, this, SIGNAL(finished()));

View File

@ -44,15 +44,16 @@ class JSONWorker : public QObject
signals:
void finished();
void jsonReady(const QJsonDocument &document);
void jsonReady(const QJsonDocument &document, const QDateTime &time);
public:
JSONWorker(const QByteArray &data);
JSONWorker(const QByteArray &data, const QDateTime &time);
public slots:
void process();
private:
QDateTime m_time;
QByteArray m_data;
};
@ -73,10 +74,10 @@ class Generator : public QObject
// clang-format on
signals:
void jsonChanged();
void frameChanged();
void jsonFileMapChanged();
void operationModeChanged();
void jsonChanged(const QJsonDocument &document, const QDateTime &time);
public:
enum OperationMode
@ -107,7 +108,8 @@ private:
public slots:
void readSettings();
void writeSettings(const QString &path);
void setJsonDocument(const QJsonDocument &document);
void setJsonDocument(const QJsonDocument &document,
const QDateTime &time = QDateTime::currentDateTime());
private slots:
void reset();