From 80e7b739b4a31fb2578e1d37de11179a11c55b6c Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Mon, 17 Jul 2023 04:17:16 +0800 Subject: [PATCH] ui/util: add a helper class ParamWatcher (#28978) * new class ParamWatcher * Update selfdrive/ui/qt/util.cc * Update selfdrive/ui/qt/util.h * Update selfdrive/ui/qt/util.cc * Update selfdrive/ui/qt/util.cc --------- Co-authored-by: Adeeb Shihadeh old-commit-hash: b4f7e76531cfa378ff231b399b3910b4fb996363 --- selfdrive/ui/qt/offroad/settings.h | 4 +-- selfdrive/ui/qt/offroad/software_settings.cc | 12 ++++----- selfdrive/ui/qt/util.cc | 26 +++++++++++++++++++- selfdrive/ui/qt/util.h | 20 +++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/selfdrive/ui/qt/offroad/settings.h b/selfdrive/ui/qt/offroad/settings.h index e35811fdc8..edba5be800 100644 --- a/selfdrive/ui/qt/offroad/settings.h +++ b/selfdrive/ui/qt/offroad/settings.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include @@ -9,6 +8,7 @@ #include +#include "selfdrive/ui/qt/util.h" #include "selfdrive/ui/qt/widgets/controls.h" // ********** settings window + top-level panels ********** @@ -88,5 +88,5 @@ private: ButtonControl *targetBranchBtn; Params params; - QFileSystemWatcher *fs_watch; + ParamWatcher *fs_watch; }; diff --git a/selfdrive/ui/qt/offroad/software_settings.cc b/selfdrive/ui/qt/offroad/software_settings.cc index d25c8490f3..15c022db9a 100644 --- a/selfdrive/ui/qt/offroad/software_settings.cc +++ b/selfdrive/ui/qt/offroad/software_settings.cc @@ -83,8 +83,8 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) { }); addItem(uninstallBtn); - fs_watch = new QFileSystemWatcher(this); - QObject::connect(fs_watch, &QFileSystemWatcher::fileChanged, [=](const QString path) { + fs_watch = new ParamWatcher(this); + QObject::connect(fs_watch, &ParamWatcher::paramChanged, [=](const QString ¶m_name, const QString ¶m_value) { updateLabels(); }); @@ -105,10 +105,10 @@ void SoftwarePanel::showEvent(QShowEvent *event) { void SoftwarePanel::updateLabels() { // add these back in case the files got removed - fs_watch->addPath(QString::fromStdString(params.getParamPath("LastUpdateTime"))); - fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdateFailedCount"))); - fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdaterState"))); - fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdateAvailable"))); + fs_watch->addParam("LastUpdateTime"); + fs_watch->addParam("UpdateFailedCount"); + fs_watch->addParam("UpdaterState"); + fs_watch->addParam("UpdateAvailable"); if (!isVisible()) { return; diff --git a/selfdrive/ui/qt/util.cc b/selfdrive/ui/qt/util.cc index 458d0be2fc..a2926548ed 100644 --- a/selfdrive/ui/qt/util.cc +++ b/selfdrive/ui/qt/util.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -11,7 +12,6 @@ #include #include -#include "common/params.h" #include "common/swaglog.h" #include "system/hardware/hw.h" @@ -252,3 +252,27 @@ bool hasLongitudinalControl(const cereal::CarParams::Reader &car_params) { ? Params().getBool("ExperimentalLongitudinalEnabled") : car_params.getOpenpilotLongitudinalControl(); } + +// ParamWatcher + +ParamWatcher::ParamWatcher(QObject *parent) : QObject(parent) { + watcher = new QFileSystemWatcher(this); + QObject::connect(watcher, &QFileSystemWatcher::fileChanged, this, &ParamWatcher::fileChanged); +} + +void ParamWatcher::fileChanged(const QString &path) { + auto param_name = QFileInfo(path).fileName(); + auto param_value = QString::fromStdString(params.get(param_name.toStdString())); + + auto it = params_hash.find(param_name); + bool content_changed = (it == params_hash.end()) || (it.value() != param_value); + params_hash[param_name] = param_value; + // emit signal when the content changes. + if (content_changed) { + emit paramChanged(param_name, param_value); + } +} + +void ParamWatcher::addParam(const QString ¶m_name) { + watcher->addPath(QString::fromStdString(params.getParamPath(param_name.toStdString()))); +} diff --git a/selfdrive/ui/qt/util.h b/selfdrive/ui/qt/util.h index 34804f6cf9..68c7bd2712 100644 --- a/selfdrive/ui/qt/util.h +++ b/selfdrive/ui/qt/util.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -10,6 +11,7 @@ #include #include "cereal/gen/cpp/car.capnp.h" +#include "common/params.h" QString getVersion(); QString getBrand(); @@ -36,3 +38,21 @@ struct InterFont : public QFont { setWeight(weight); } }; + +class ParamWatcher : public QObject { + Q_OBJECT + +public: + ParamWatcher(QObject *parent); + void addParam(const QString ¶m_name); + +signals: + void paramChanged(const QString ¶m_name, const QString ¶m_value); + +private: + void fileChanged(const QString &path); + + QFileSystemWatcher *watcher; + QHash params_hash; + Params params; +};