diff --git a/selfdrive/ui/qt/api.cc b/selfdrive/ui/qt/api.cc index 3d4b38d032..e604a5c09b 100644 --- a/selfdrive/ui/qt/api.cc +++ b/selfdrive/ui/qt/api.cc @@ -67,7 +67,7 @@ QString create_jwt(const QJsonObject &payloads, int expiry) { } // namespace CommaApi -HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, const QString &cache_key, bool create_jwt_) : cache_key(cache_key), create_jwt(create_jwt_), QObject(parent) { +HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, bool create_jwt_) : create_jwt(create_jwt_), QObject(parent) { networkAccessManager = new QNetworkAccessManager(this); reply = NULL; @@ -77,12 +77,6 @@ HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, const QStri connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout); sendRequest(requestURL); - - if (!cache_key.isEmpty()) { - if (std::string cached_resp = Params().get(cache_key.toStdString()); !cached_resp.empty()) { - QTimer::singleShot(0, [=]() { emit receivedResponse(QString::fromStdString(cached_resp)); }); - } - } } void HttpRequest::sendRequest(const QString &requestURL) { @@ -116,15 +110,8 @@ void HttpRequest::requestFinished() { QString response = reply->readAll(); if (reply->error() == QNetworkReply::NoError) { - // save to cache - if (!cache_key.isEmpty()) { - Params().put(cache_key.toStdString(), response.toStdString()); - } emit receivedResponse(response); } else { - if (!cache_key.isEmpty()) { - Params().remove(cache_key.toStdString()); - } qDebug() << reply->errorString(); emit failedResponse(reply->errorString()); } diff --git a/selfdrive/ui/qt/api.h b/selfdrive/ui/qt/api.h index 9dccc08f7e..8aa01ae2b5 100644 --- a/selfdrive/ui/qt/api.h +++ b/selfdrive/ui/qt/api.h @@ -20,14 +20,15 @@ class HttpRequest : public QObject { Q_OBJECT public: - explicit HttpRequest(QObject* parent, const QString &requestURL, const QString &cache_key = "", bool create_jwt_ = true); - QNetworkReply *reply; + explicit HttpRequest(QObject* parent, const QString &requestURL, bool create_jwt_ = true); void sendRequest(const QString &requestURL); +protected: + QNetworkReply *reply; + private: QNetworkAccessManager *networkAccessManager; QTimer *networkTimer; - QString cache_key; bool create_jwt; private slots: diff --git a/selfdrive/ui/qt/request_repeater.cc b/selfdrive/ui/qt/request_repeater.cc index 3d9561a307..7db708d400 100644 --- a/selfdrive/ui/qt/request_repeater.cc +++ b/selfdrive/ui/qt/request_repeater.cc @@ -1,7 +1,7 @@ #include "selfdrive/ui/qt/request_repeater.h" RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey, - int period) : HttpRequest(parent, requestURL, cacheKey) { + int period) : HttpRequest(parent, requestURL) { timer = new QTimer(this); timer->setTimerType(Qt::VeryCoarseTimer); QObject::connect(timer, &QTimer::timeout, [=]() { @@ -9,5 +9,25 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con sendRequest(requestURL); } }); + timer->start(period * 1000); + + if (!cacheKey.isEmpty()) { + prevResp = QString::fromStdString(params.get(cacheKey.toStdString())); + if (!prevResp.isEmpty()) { + QTimer::singleShot(0, [=]() { emit receivedResponse(prevResp); }); + } + QObject::connect(this, &HttpRequest::receivedResponse, [=](const QString &resp) { + if (resp != prevResp) { + params.put(cacheKey.toStdString(), resp.toStdString()); + prevResp = resp; + } + }); + QObject::connect(this, &HttpRequest::failedResponse, [=](const QString &err) { + if (!prevResp.isEmpty()) { + params.remove(cacheKey.toStdString()); + prevResp = ""; + } + }); + } } diff --git a/selfdrive/ui/qt/request_repeater.h b/selfdrive/ui/qt/request_repeater.h index c23bbbcf19..1574d19305 100644 --- a/selfdrive/ui/qt/request_repeater.h +++ b/selfdrive/ui/qt/request_repeater.h @@ -1,5 +1,6 @@ #pragma once +#include "selfdrive/common/util.h" #include "selfdrive/ui/qt/api.h" #include "selfdrive/ui/ui.h" @@ -8,5 +9,7 @@ public: RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey = "", int period = 0); private: + Params params; QTimer *timer; + QString prevResp; }; diff --git a/selfdrive/ui/qt/widgets/ssh_keys.cc b/selfdrive/ui/qt/widgets/ssh_keys.cc index 2dfcec0df8..a56dd62adc 100644 --- a/selfdrive/ui/qt/widgets/ssh_keys.cc +++ b/selfdrive/ui/qt/widgets/ssh_keys.cc @@ -40,7 +40,7 @@ void SshControl::refresh() { } void SshControl::getUserKeys(const QString &username) { - HttpRequest *request = new HttpRequest(this, "https://github.com/" + username + ".keys", "", false); + HttpRequest *request = new HttpRequest(this, "https://github.com/" + username + ".keys", false); QObject::connect(request, &HttpRequest::receivedResponse, [=](const QString &resp) { if (!resp.isEmpty()) { params.put("GithubUsername", username.toStdString()); diff --git a/selfdrive/ui/replay/replay.cc b/selfdrive/ui/replay/replay.cc index 62be37d410..23f168ba94 100644 --- a/selfdrive/ui/replay/replay.cc +++ b/selfdrive/ui/replay/replay.cc @@ -46,7 +46,7 @@ Replay::Replay(QString route, SubMaster *sm_, QObject *parent) : sm(sm_), QObjec } const QString url = "https://api.commadotai.com/v1/route/" + route + "/files"; - http = new HttpRequest(this, url, "", !Hardware::PC()); + http = new HttpRequest(this, url, !Hardware::PC()); QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse); }