From d69ab590c71fabc0135b2b07a8859a80e556cdf5 Mon Sep 17 00:00:00 2001 From: Arseniy-Movshev Date: Fri, 21 Jul 2023 11:51:27 +0100 Subject: 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 --- common.cpp | 5 +++++ common.h | 1 + qmlplugin/hrDataLoader.cpp | 11 ++++++++++- qmlplugin/hrDataLoader.h | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/common.cpp b/common.cpp index 4c0f252..10c18d4 100644 --- a/common.cpp +++ b/common.cpp @@ -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)); diff --git a/common.h b/common.h index a6b3197..8240d06 100644 --- a/common.h +++ b/common.h @@ -15,6 +15,7 @@ #include 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 #include #include +#include #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 HrDataLoader::getRawDataForDate(QDate date) { QList 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 #include #include +#include class HrDataLoader : public QObject { @@ -27,8 +28,11 @@ public: Q_INVOKABLE void triggerDaemonRecording(); Q_INVOKABLE QVariant getDataFromTo(QDate date1, QDate date2); QList getRawDataForDate(QDate date); +signals: + void dataChanged(); private: QDBusInterface *m_iface; + QFileSystemWatcher *m_fileWatcher; }; #endif // HRDATALOADER_H -- cgit v1.2.3-54-g00ecf