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

View File

@ -67,7 +67,7 @@ public slots:
private slots: private slots:
void writeValues(); void writeValues();
void updateValues(); void updateValues(const QJsonDocument &document, const QDateTime &time);
private: private:
QFile m_csvFile; 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 * This function is set to public in order to allow the CSV-replay feature to
* work by replacing the data/json input source. * 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()) if (document.object().isEmpty())
return; return;
@ -270,7 +270,7 @@ void Generator::setJsonDocument(const QJsonDocument &document)
if (m_frame.read(m_document.object())) if (m_frame.read(m_document.object()))
emit frameChanged(); 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 // Create new worker thread to read JSON data
QThread *thread = new QThread; QThread *thread = new QThread;
JSONWorker *worker = new JSONWorker(data); JSONWorker *worker = new JSONWorker(data, QDateTime::currentDateTime());
worker->moveToThread(thread); worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process())); connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit())); 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) // 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_data = data;
m_time = time;
} }
void JSONWorker::process() void JSONWorker::process()
@ -383,7 +384,7 @@ void JSONWorker::process()
// No parse error, update UI & reset error counter // No parse error, update UI & reset error counter
if (error.error == QJsonParseError::NoError) if (error.error == QJsonParseError::NoError)
emit jsonReady(document); emit jsonReady(document, m_time);
// Delete object in 500 ms // Delete object in 500 ms
QTimer::singleShot(500, this, SIGNAL(finished())); QTimer::singleShot(500, this, SIGNAL(finished()));

View File

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