Skip to content

Commit

Permalink
datamonitor: data logging WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Ionut Muthi <[email protected]>
  • Loading branch information
IonutMuthi committed Apr 3, 2024
1 parent 76a8cae commit 4bb2a2c
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef DATALOGGINGMENU_HPP
#define DATALOGGINGMENU_HPP

#include <QWidget>
#include <menuonoffswitch.h>
#include <progresslineedit.h>
#include "scopy-datamonitorplugin_export.h"

namespace scopy {
namespace datamonitor {

enum ProgressBarState
{
SUCCESS,
ERROR,
BUSSY
};

class SCOPY_DATAMONITORPLUGIN_EXPORT DataLoggingMenu : public QWidget
{
friend class DataMonitorStyleHelper;
Q_OBJECT
public:
explicit DataLoggingMenu(QWidget *parent = nullptr);

bool liveDataLogging() const;
bool isLoadDataOverrideOn();

public Q_SLOTS:
void updateDataLoggingStatus(ProgressBarState status);

Q_SIGNALS:
void pathChanged(QString path);
void requestLiveDataLogging(QString path);
void requestDataLogging(QString path);
void requestDataLoading(QString path, bool override);

private:
QString filename;
ProgressLineEdit *dataLoggingFilePath;
QPushButton *dataLoggingBrowseBtn;
QPushButton *dataLoggingBtn;
QPushButton *dataLoadingBtn;
MenuOnOffSwitch *liveDataLoggingButton;
QCheckBox *loadDataOverride;
bool m_liveDataLogging = false;
void chooseFile();
void toggleButtonsEnabled(bool en);
};
} // namespace datamonitor
} // namespace scopy
#endif // DATALOGGINGMENU_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SCOPY_DATAMONITORPLUGIN_EXPORT DataMonitorModel : public QObject

QPair<double, double> getLastReadValue() const;
double getValueAtTime(double time);
void setValueAtTime(double time, double value);
void updateValue(double time, double value);
void resetMinMax();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class LogDataToFile : public QObject
public:
explicit LogDataToFile(DataAcquisitionManager *dataAcquisitionManager, QObject *parent = nullptr);

void continuousLogData(QString path);
void logData(QString path);
void loadData(QString path);

Expand All @@ -23,6 +24,7 @@ class LogDataToFile : public QObject

private:
DataAcquisitionManager *m_dataAcquisitionManager;
QString *currentFileHeader;
};
} // namespace datamonitor
} // namespace scopy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class TimeManager : public QObject
bool isRunning() const;
void setIsRunning(bool newIsRunning);

QDateTime lastReadValue() const;

Q_SIGNALS:
void timeout();

Expand All @@ -41,6 +43,8 @@ class TimeManager : public QObject
bool m_realTime = false;
QTimer *m_timer;
bool m_isRunning = false;

QDateTime m_lastReadValue;
};
} // namespace datamonitor
} // namespace scopy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ double DataMonitorModel::getValueAtTime(double time)

return -Q_INFINITY;
}

void DataMonitorModel::setValueAtTime(double time, double value)
{
if(xdata.contains(time)) {
ydata.replace(xdata.indexOf(time), value);
} else {
updateValue(time, value);
}
}

QList<QPair<double, double>> *DataMonitorModel::getValues() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QDate>
#include <QDebug>
#include <QwtDate>
#include <timemanager.hpp>

using namespace scopy;
using namespace datamonitor;
Expand Down Expand Up @@ -34,7 +35,10 @@ void DMMReadStrategy::read()
double result = (raw + m_offset) * scale * m_umScale;
qDebug() << "dmm read success ";

double currentTime = QwtDate::toDouble(QDateTime::currentDateTime());
// double currentTime = QwtDate::toDouble(QDateTime::currentDateTime());

auto &&timeTracker = TimeManager::GetInstance();
double currentTime = QwtDate::toDouble(timeTracker->lastReadValue());

Q_EMIT readDone(currentTime, result);
}
Expand Down
1 change: 1 addition & 0 deletions plugins/datamonitorPlugin/src/datamonitorplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ bool DataMonitorPlugin::onConnect()
QObject::connect(p, &Preferences::preferenceChanged, this, [=, this](QString id, QVariant var) {
if(id.contains("datamonitorplugin_read_interval")) {
timeTracker->setTimerInterval(p->get("datamonitorplugin_read_interval").toDouble());
/// ?? TODO also change time format ??
}
});

Expand Down
5 changes: 5 additions & 0 deletions plugins/datamonitorPlugin/src/datamonitortool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sevensegmentdisplay.hpp>
#include <timemanager.hpp>
#include "datamonitorstylehelper.hpp"
#include <iioutil/connection.h>

using namespace scopy::datamonitor;

Expand Down Expand Up @@ -184,6 +185,10 @@ DatamonitorTool::DatamonitorTool(DataAcquisitionManager *dataAcquisitionManager,

/// log data
LogDataToFile *logDataToFile = new LogDataToFile(dataAcquisitionManager, this);

connect(m_dataMonitorSettings->getDataLoggingMenu(), &DataLoggingMenu::requestLiveDataLogging, logDataToFile,
&LogDataToFile::continuousLogData);

connect(m_dataMonitorSettings->getDataLoggingMenu(), &DataLoggingMenu::requestDataLogging, logDataToFile,
&LogDataToFile::logData);

Expand Down
50 changes: 40 additions & 10 deletions plugins/datamonitorPlugin/src/menus/dataloggingmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <datamonitorstylehelper.hpp>
#include <menucollapsesection.h>
#include <menusectionwidget.h>
#include <timemanager.hpp>

using namespace scopy;
using namespace datamonitor;
Expand All @@ -24,16 +25,6 @@ DataLoggingMenu::DataLoggingMenu(QWidget *parent)
dataLoggingFilePath = new ProgressLineEdit(logDataSection);
dataLoggingFilePath->getLineEdit()->setReadOnly(true);

connect(dataLoggingFilePath->getLineEdit(), &QLineEdit::textChanged, this, [=, this](QString path) {
if(filename.isEmpty() && dataLoggingFilePath->getLineEdit()->isEnabled()) {
dataLoggingFilePath->getLineEdit()->setText(tr("No file selected"));
dataLoggingFilePath->getLineEdit()->setStyleSheet("color:red");
} else {
dataLoggingFilePath->getLineEdit()->setStyleSheet("color:white");
Q_EMIT pathChanged(path);
}
});

dataLoggingBrowseBtn = new QPushButton("Browse", logDataSection);
connect(dataLoggingBrowseBtn, &QPushButton::clicked, this, &DataLoggingMenu::chooseFile);

Expand All @@ -49,9 +40,46 @@ DataLoggingMenu::DataLoggingMenu(QWidget *parent)
Q_EMIT requestDataLoading(dataLoggingFilePath->getLineEdit()->text());
});

MenuOnOffSwitch *liveDataLogging = new MenuOnOffSwitch("Live data logging", logDataSection);
connect(liveDataLogging->onOffswitch(), &QAbstractButton::toggled, this, [=, this](bool toggled) {
m_liveDataLogging = toggled;
dataLoggingBtn->setEnabled(!toggled);
dataLoadingBtn->setEnabled(!toggled);
});

dataLoggingBtn->setEnabled(false);
dataLoadingBtn->setEnabled(false);
liveDataLogging->setEnabled(false);

///// time manager
/// ??? is this the best solution
auto &&timeTracker = TimeManager::GetInstance();
connect(timeTracker, &TimeManager::timeout, this, [=, this]() {
if(liveDataLogging->onOffswitch()->isChecked()) {
Q_EMIT requestLiveDataLogging(dataLoggingFilePath->getLineEdit()->text());
}
});

connect(dataLoggingFilePath->getLineEdit(), &QLineEdit::textChanged, this, [=, this](QString path) {
if(filename.isEmpty() && dataLoggingFilePath->getLineEdit()->isEnabled()) {
dataLoggingFilePath->getLineEdit()->setText(tr("No file selected"));
dataLoggingFilePath->getLineEdit()->setStyleSheet("color:red");

} else {
dataLoggingFilePath->getLineEdit()->setStyleSheet("color:white");

dataLoggingBtn->setEnabled(true);
dataLoadingBtn->setEnabled(true);
liveDataLogging->setEnabled(true);

Q_EMIT pathChanged(path);
}
});

logDataSection->contentLayout()->addWidget(new QLabel("Choose file"));
logDataSection->contentLayout()->addWidget(dataLoggingFilePath);
logDataSection->contentLayout()->addWidget(dataLoggingBrowseBtn);
logDataSection->contentLayout()->addWidget(liveDataLogging);
logDataSection->contentLayout()->addWidget(dataLoggingBtn);
logDataSection->contentLayout()->addWidget(dataLoadingBtn);

Expand All @@ -76,6 +104,8 @@ void DataLoggingMenu::updateDataLoggingStatus(ProgressBarState status)
}
}

bool DataLoggingMenu::liveDataLogging() const { return m_liveDataLogging; }

void DataLoggingMenu::chooseFile()
{
QString selectedFilter;
Expand Down
115 changes: 106 additions & 9 deletions plugins/datamonitorPlugin/src/menus/logdatatofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,67 @@ LogDataToFile::LogDataToFile(DataAcquisitionManager *dataAcquisitionManager, QOb
: QObject{parent}
{
m_dataAcquisitionManager = dataAcquisitionManager;
currentFileHeader = new QString("");
}

void LogDataToFile::continuousLogData(QString path)
{
QString fileHeader = "Time";
foreach(QString monitor, m_dataAcquisitionManager->getActiveMonitors()) {
fileHeader += "," + monitor;
}

// if a channels is added or removed we need to recreate the file
// ?? use log data for recreating path
if(currentFileHeader != fileHeader) {

logData(path);

currentFileHeader = new QString(fileHeader);
} else {
QFile file(path);
QTextStream exportStream(&file);
if(!file.isOpen()) {
file.open(QIODevice::Append);

// get last time value
// the time of the last read value will be the same for all active monitors
auto lastReadValueTime = m_dataAcquisitionManager->getDataMonitorMap()
->value(m_dataAcquisitionManager->getActiveMonitors().first())
->getXdata()
->last();

QDateTime auxTime = QDateTime::fromMSecsSinceEpoch(lastReadValueTime);
QString time = QString(auxTime.toString(DataMonitorUtils::getDateTimeFormat()));

QString values = time;

auto monitors = currentFileHeader->split(",");
for(int i = 1; i < monitors.length(); i++) {

QString monitor = monitors[i];

auto auxVal =
m_dataAcquisitionManager->getDataMonitorMap()->value(monitor)->getValueAtTime(
lastReadValueTime);

QString val = ", ";
if(lastReadValueTime != -Q_INFINITY) {
val += QString::number(auxVal);
}

values += val;
}

exportStream << time << values << "\n";

} else {
qDebug() << "File already opened! ";
}

if(file.isOpen())
file.close();
}
}

void LogDataToFile::logData(QString path)
Expand All @@ -28,22 +89,59 @@ void LogDataToFile::logData(QString path)

QString tableHead = "Time";
QMap<QString, QString> values;
// foreach(QString monitor, m_dataAcquisitionManager->getActiveMonitors()) {
// tableHead += ", " + monitor;
// auto xData =
//m_dataAcquisitionManager->getDataMonitorMap()->value(monitor)->getXdata();

// for(int i = 0; i < xData->length(); i++) {

// auto val =
// m_dataAcquisitionManager->getDataMonitorMap()->value(monitor)->getValueAtTime(
// xData->at(i));
// QDateTime auxTime = QDateTime::fromMSecsSinceEpoch(xData->at(i));
// QString time =
//QString(auxTime.toString(DataMonitorUtils::getDateTimeFormat()));

// if(values.contains(time)) {
// values[time] += QString(", " + QString::number(val));
// } else {
// values.insert(time, "," + QString::number(val));
// }
// }
// }

QVector<double> *timeValues = new QVector<double>();
foreach(QString monitor, m_dataAcquisitionManager->getActiveMonitors()) {
tableHead += ", " + monitor;

auto xData = m_dataAcquisitionManager->getDataMonitorMap()->value(monitor)->getXdata();

for(int i = 0; i < xData->length(); i++) {
if(xData->length() > timeValues->length()) {
timeValues = xData;
}
}

for(int i = 0; i < timeValues->length(); i++) {

QDateTime auxTime = QDateTime::fromMSecsSinceEpoch(timeValues->at(i));
QString time = QString(auxTime.toString(DataMonitorUtils::getDateTimeFormat()));

auto val =
foreach(QString monitor, m_dataAcquisitionManager->getActiveMonitors()) {

auto auxVal =
m_dataAcquisitionManager->getDataMonitorMap()->value(monitor)->getValueAtTime(
xData->at(i));
QDateTime auxTime = QDateTime::fromMSecsSinceEpoch(xData->at(i));
QString time = QString(auxTime.toString(DataMonitorUtils::getDateTimeFormat()));
timeValues->at(i));

QString val = ", ";
if(auxVal != -Q_INFINITY) {
val += QString::number(auxVal);
}

if(values.contains(time)) {
values[time] += QString(", " + QString::number(val));
values[time] += val;
} else {
values.insert(time, "," + QString::number(val));
values.insert(time, val);
}
}
}
Expand All @@ -53,7 +151,6 @@ void LogDataToFile::logData(QString path)
auto iterator = values.begin();

while(iterator != values.end()) {

exportStream << iterator.key() << iterator.value() << "\n";
iterator++;
}
Expand Down Expand Up @@ -89,7 +186,7 @@ void LogDataToFile::loadData(QString path)
QString ch = channels[i].simplified();
ch.replace(" ", "");
if(m_dataAcquisitionManager->getDataMonitorMap()->contains(ch)) {
m_dataAcquisitionManager->getDataMonitorMap()->value(ch)->updateValue(
m_dataAcquisitionManager->getDataMonitorMap()->value(ch)->setValueAtTime(
QwtDate::toDouble(dateTime), values[i].toDouble());
}
}
Expand Down
Loading

0 comments on commit 4bb2a2c

Please sign in to comment.