From 06bf2823b6379bf4e793e69f61a21f71e60b743e Mon Sep 17 00:00:00 2001 From: Alex Spataru Date: Tue, 9 Feb 2021 23:33:46 -0500 Subject: [PATCH] Better TX/RX precision on CSV export --- src/CSV/Export.cpp | 14 +++++++------- src/CSV/Export.h | 2 +- src/JSON/Generator.cpp | 11 ++++++----- src/JSON/Generator.h | 10 ++++++---- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/CSV/Export.cpp b/src/CSV/Export.cpp index 75c049d1..88e0c101 100644 --- a/src/CSV/Export.cpp +++ b/src/CSV/Export.cpp @@ -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::currentDateTime(), json); + auto pair = qMakePair(time, json); m_jsonList.append(pair); } diff --git a/src/CSV/Export.h b/src/CSV/Export.h index 4f358658..bafdbfec 100644 --- a/src/CSV/Export.h +++ b/src/CSV/Export.h @@ -67,7 +67,7 @@ public slots: private slots: void writeValues(); - void updateValues(); + void updateValues(const QJsonDocument &document, const QDateTime &time); private: QFile m_csvFile; diff --git a/src/JSON/Generator.cpp b/src/JSON/Generator.cpp index ab4c16cb..d5930c89 100644 --- a/src/JSON/Generator.cpp +++ b/src/JSON/Generator.cpp @@ -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())); diff --git a/src/JSON/Generator.h b/src/JSON/Generator.h index 905ae797..bd5abe01 100644 --- a/src/JSON/Generator.h +++ b/src/JSON/Generator.h @@ -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();