diff options
author | Arseniy-Movshev <dodoradio@outlook.com> | 2023-07-21 11:51:27 +0100 |
---|---|---|
committer | Arseniy-Movshev <dodoradio@outlook.com> | 2023-07-21 13:44:08 +0100 |
commit | d69ab590c71fabc0135b2b07a8859a80e556cdf5 (patch) | |
tree | e1e7d9dd5a7f869628cad5d17576c13fb3ce2037 | |
parent | 439c082257448309073c312f008888568628323b (diff) |
Add filesystem watchers to hrdataloader to update when new data comes in
When data for a specific file is requested, that file is added to the watch list. Currently these files aren't ever removed from the watch list, hopefully we won't be loading enough files to cause problems, at least for now
-rw-r--r-- | common.cpp | 5 | ||||
-rw-r--r-- | common.h | 1 | ||||
-rw-r--r-- | qmlplugin/hrDataLoader.cpp | 11 | ||||
-rw-r--r-- | qmlplugin/hrDataLoader.h | 4 |
4 files changed, 20 insertions, 1 deletions
@@ -19,6 +19,11 @@ QString fileNameForDate(QDate date, QString prefix) { return settings.value("loggerRootPath", QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.asteroid-sensorlogd/").toString() + prefix + "/" + date.toString("yyyy-MM-dd.log"); } +QString sensorDirPath(QString prefix) { + QSettings settings("asteroid", "sensorlogd"); // this should be moved out of here at some point TODO + return settings.value("loggerRootPath", QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.asteroid-sensorlogd/").toString() + prefix + "/"; +} + void fileAddRecord(QString sensorPrefix, QString logdata, QDateTime recordTime) { //adds a record to today's log file for the given sensor qDebug() << fileNameForDate(recordTime.date(), sensorPrefix); QFile file(fileNameForDate(recordTime.date(), sensorPrefix)); @@ -15,6 +15,7 @@ #include <QString> QString fileNameForDate(QDate date, QString prefix); +QString sensorDirPath(QString prefix); void fileAddRecord(QString sensorPrefix, QString logdata, QDateTime recordTime = QDateTime::currentDateTime()); //adds a record to today's log file for the given sensor #endif //SENSORLOGD_COMMON_H diff --git a/qmlplugin/hrDataLoader.cpp b/qmlplugin/hrDataLoader.cpp index 23f2ef0..16845ad 100644 --- a/qmlplugin/hrDataLoader.cpp +++ b/qmlplugin/hrDataLoader.cpp @@ -15,6 +15,7 @@ #include <QSettings> #include <QDBusInterface> #include <QPointF> +#include <QFileSystemWatcher> #include "hrDataLoader.h" #include "../common.h" @@ -27,6 +28,10 @@ HrDataLoader::HrDataLoader() : QObject() } else { qDebug()<<"interface is valid"; } + m_fileWatcher = new QFileSystemWatcher(); + m_fileWatcher->addPath(sensorDirPath("heartrateMonitor")); + QObject::connect(m_fileWatcher,SIGNAL(directoryChanged(const QString)),this,SIGNAL(dataChanged())); + QObject::connect(m_fileWatcher,SIGNAL(fileChanged(const QString)),this,SIGNAL(dataChanged())); } QVariant HrDataLoader::getTodayData() { @@ -39,11 +44,15 @@ QVariant HrDataLoader::getDataForDate(QDate date) { QList<QPointF> HrDataLoader::getRawDataForDate(QDate date) { QList<QPointF> m_filedata; - QFile file(fileNameForDate(date, "heartrateMonitor")); + QString path = fileNameForDate(date, "heartrateMonitor"); + QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "failed to open file"; return m_filedata; } + if (!m_fileWatcher->files().contains(path)) { + m_fileWatcher->addPath(path); + } QTextStream inStream(&file); QString line; while (!inStream.atEnd()) { diff --git a/qmlplugin/hrDataLoader.h b/qmlplugin/hrDataLoader.h index 78ac571..f14eacf 100644 --- a/qmlplugin/hrDataLoader.h +++ b/qmlplugin/hrDataLoader.h @@ -15,6 +15,7 @@ #include <QDBusInterface> #include <QPointF> #include <QDate> +#include <QFileSystemWatcher> class HrDataLoader : public QObject { @@ -27,8 +28,11 @@ public: Q_INVOKABLE void triggerDaemonRecording(); Q_INVOKABLE QVariant getDataFromTo(QDate date1, QDate date2); QList<QPointF> getRawDataForDate(QDate date); +signals: + void dataChanged(); private: QDBusInterface *m_iface; + QFileSystemWatcher *m_fileWatcher; }; #endif // HRDATALOADER_H |