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 <adeebshihadeh@gmail.com>
old-commit-hash: b4f7e76531
beeps
Dean Lee 2 years ago committed by GitHub
parent 645479775d
commit 80e7b739b4
  1. 4
      selfdrive/ui/qt/offroad/settings.h
  2. 12
      selfdrive/ui/qt/offroad/software_settings.cc
  3. 26
      selfdrive/ui/qt/util.cc
  4. 20
      selfdrive/ui/qt/util.h

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <QButtonGroup> #include <QButtonGroup>
#include <QFileSystemWatcher>
#include <QFrame> #include <QFrame>
#include <QLabel> #include <QLabel>
#include <QPushButton> #include <QPushButton>
@ -9,6 +8,7 @@
#include <QWidget> #include <QWidget>
#include "selfdrive/ui/qt/util.h"
#include "selfdrive/ui/qt/widgets/controls.h" #include "selfdrive/ui/qt/widgets/controls.h"
// ********** settings window + top-level panels ********** // ********** settings window + top-level panels **********
@ -88,5 +88,5 @@ private:
ButtonControl *targetBranchBtn; ButtonControl *targetBranchBtn;
Params params; Params params;
QFileSystemWatcher *fs_watch; ParamWatcher *fs_watch;
}; };

@ -83,8 +83,8 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) {
}); });
addItem(uninstallBtn); addItem(uninstallBtn);
fs_watch = new QFileSystemWatcher(this); fs_watch = new ParamWatcher(this);
QObject::connect(fs_watch, &QFileSystemWatcher::fileChanged, [=](const QString path) { QObject::connect(fs_watch, &ParamWatcher::paramChanged, [=](const QString &param_name, const QString &param_value) {
updateLabels(); updateLabels();
}); });
@ -105,10 +105,10 @@ void SoftwarePanel::showEvent(QShowEvent *event) {
void SoftwarePanel::updateLabels() { void SoftwarePanel::updateLabels() {
// add these back in case the files got removed // add these back in case the files got removed
fs_watch->addPath(QString::fromStdString(params.getParamPath("LastUpdateTime"))); fs_watch->addParam("LastUpdateTime");
fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdateFailedCount"))); fs_watch->addParam("UpdateFailedCount");
fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdaterState"))); fs_watch->addParam("UpdaterState");
fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdateAvailable"))); fs_watch->addParam("UpdateAvailable");
if (!isVisible()) { if (!isVisible()) {
return; return;

@ -2,6 +2,7 @@
#include <QApplication> #include <QApplication>
#include <QFile> #include <QFile>
#include <QFileInfo>
#include <QHash> #include <QHash>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
@ -11,7 +12,6 @@
#include <QTextStream> #include <QTextStream>
#include <QtXml/QDomDocument> #include <QtXml/QDomDocument>
#include "common/params.h"
#include "common/swaglog.h" #include "common/swaglog.h"
#include "system/hardware/hw.h" #include "system/hardware/hw.h"
@ -252,3 +252,27 @@ bool hasLongitudinalControl(const cereal::CarParams::Reader &car_params) {
? Params().getBool("ExperimentalLongitudinalEnabled") ? Params().getBool("ExperimentalLongitudinalEnabled")
: car_params.getOpenpilotLongitudinalControl(); : 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 &param_name) {
watcher->addPath(QString::fromStdString(params.getParamPath(param_name.toStdString())));
}

@ -3,6 +3,7 @@
#include <optional> #include <optional>
#include <QDateTime> #include <QDateTime>
#include <QFileSystemWatcher>
#include <QLayout> #include <QLayout>
#include <QPainter> #include <QPainter>
#include <QPixmap> #include <QPixmap>
@ -10,6 +11,7 @@
#include <QWidget> #include <QWidget>
#include "cereal/gen/cpp/car.capnp.h" #include "cereal/gen/cpp/car.capnp.h"
#include "common/params.h"
QString getVersion(); QString getVersion();
QString getBrand(); QString getBrand();
@ -36,3 +38,21 @@ struct InterFont : public QFont {
setWeight(weight); setWeight(weight);
} }
}; };
class ParamWatcher : public QObject {
Q_OBJECT
public:
ParamWatcher(QObject *parent);
void addParam(const QString &param_name);
signals:
void paramChanged(const QString &param_name, const QString &param_value);
private:
void fileChanged(const QString &path);
QFileSystemWatcher *watcher;
QHash<QString, QString> params_hash;
Params params;
};

Loading…
Cancel
Save