mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-06-23 15:02:06 +08:00
HttpRequest: don't send request in ctor (#21665)
This commit is contained in:
@@ -67,24 +67,24 @@ QString create_jwt(const QJsonObject &payloads, int expiry) {
|
||||
|
||||
} // namespace CommaApi
|
||||
|
||||
HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, bool create_jwt_,
|
||||
int timeout) : create_jwt(create_jwt_), QObject(parent) {
|
||||
HttpRequest::HttpRequest(QObject *parent, bool create_jwt, int timeout) : create_jwt(create_jwt), QObject(parent) {
|
||||
networkAccessManager = new QNetworkAccessManager(this);
|
||||
reply = NULL;
|
||||
|
||||
networkTimer = new QTimer(this);
|
||||
networkTimer->setSingleShot(true);
|
||||
networkTimer->setInterval(timeout);
|
||||
connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout);
|
||||
|
||||
sendRequest(requestURL);
|
||||
}
|
||||
|
||||
bool HttpRequest::active() {
|
||||
return reply != NULL;
|
||||
return reply != nullptr;
|
||||
}
|
||||
|
||||
void HttpRequest::sendRequest(const QString &requestURL) {
|
||||
if (active()) {
|
||||
qDebug() << "HttpRequest is active";
|
||||
return;
|
||||
}
|
||||
QString token;
|
||||
if(create_jwt) {
|
||||
token = CommaApi::create_jwt();
|
||||
@@ -129,5 +129,5 @@ void HttpRequest::requestFinished() {
|
||||
}
|
||||
emit requestDone(success);
|
||||
reply->deleteLater();
|
||||
reply = NULL;
|
||||
reply = nullptr;
|
||||
}
|
||||
|
||||
@@ -20,16 +20,16 @@ class HttpRequest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HttpRequest(QObject* parent, const QString &requestURL, bool create_jwt_ = true, int timeout = 20000);
|
||||
explicit HttpRequest(QObject* parent, bool create_jwt = true, int timeout = 20000);
|
||||
void sendRequest(const QString &requestURL);
|
||||
bool active();
|
||||
|
||||
protected:
|
||||
QNetworkReply *reply;
|
||||
QNetworkReply *reply = nullptr;
|
||||
|
||||
private:
|
||||
QNetworkAccessManager *networkAccessManager;
|
||||
QTimer *networkTimer;
|
||||
QNetworkAccessManager *networkAccessManager = nullptr;
|
||||
QTimer *networkTimer = nullptr;
|
||||
bool create_jwt;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "selfdrive/ui/qt/request_repeater.h"
|
||||
|
||||
RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey,
|
||||
int period, bool while_onroad) : HttpRequest(parent, requestURL) {
|
||||
int period, bool while_onroad) : HttpRequest(parent) {
|
||||
timer = new QTimer(this);
|
||||
timer->setTimerType(Qt::VeryCoarseTimer);
|
||||
QObject::connect(timer, &QTimer::timeout, [=]() {
|
||||
if ((!QUIState::ui_state.scene.started || while_onroad) && QUIState::ui_state.awake && reply == NULL) {
|
||||
if ((!QUIState::ui_state.scene.started || while_onroad) && QUIState::ui_state.awake && !active()) {
|
||||
sendRequest(requestURL);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -117,12 +117,13 @@ QWidget * Setup::network_setup() {
|
||||
blayout->addWidget(cont);
|
||||
|
||||
// setup timer for testing internet connection
|
||||
HttpRequest *request = new HttpRequest(this, DASHCAM_URL, false, 2500);
|
||||
HttpRequest *request = new HttpRequest(this, false, 2500);
|
||||
QObject::connect(request, &HttpRequest::requestDone, [=](bool success) {
|
||||
cont->setEnabled(success);
|
||||
cont->setText(success ? "Continue" : "Waiting for internet");
|
||||
repaint();
|
||||
});
|
||||
request->sendRequest(DASHCAM_URL);
|
||||
QTimer *timer = new QTimer(this);
|
||||
QObject::connect(timer, &QTimer::timeout, [=]() {
|
||||
if (!request->active() && cont->isVisible()) {
|
||||
|
||||
@@ -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, false);
|
||||
QObject::connect(request, &HttpRequest::receivedResponse, [=](const QString &resp) {
|
||||
if (!resp.isEmpty()) {
|
||||
params.put("GithubUsername", username.toStdString());
|
||||
@@ -61,4 +61,6 @@ void SshControl::getUserKeys(const QString &username) {
|
||||
refresh();
|
||||
request->deleteLater();
|
||||
});
|
||||
|
||||
request->sendRequest("https://github.com/" + username + ".keys");
|
||||
}
|
||||
|
||||
@@ -46,8 +46,9 @@ 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, !Hardware::PC());
|
||||
QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse);
|
||||
http->sendRequest(url);
|
||||
}
|
||||
|
||||
void Replay::parseResponse(const QString &response) {
|
||||
|
||||
Reference in New Issue
Block a user