2021-06-09 03:09:30 +08:00
|
|
|
#include "selfdrive/ui/qt/request_repeater.h"
|
2021-04-24 00:59:09 -07:00
|
|
|
|
2021-05-01 14:58:12 +08:00
|
|
|
RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey,
|
2021-07-26 06:36:04 +08:00
|
|
|
int period, bool while_onroad) : HttpRequest(parent) {
|
2021-04-29 11:18:59 -07:00
|
|
|
timer = new QTimer(this);
|
|
|
|
|
timer->setTimerType(Qt::VeryCoarseTimer);
|
2021-06-11 16:17:52 +08:00
|
|
|
QObject::connect(timer, &QTimer::timeout, [=]() {
|
2021-12-15 14:28:12 +08:00
|
|
|
if ((!uiState()->scene.started || while_onroad) && uiState()->awake && !active()) {
|
2021-04-24 00:59:09 -07:00
|
|
|
sendRequest(requestURL);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-06-19 14:47:23 +08:00
|
|
|
|
2021-04-29 11:18:59 -07:00
|
|
|
timer->start(period * 1000);
|
2021-06-19 14:47:23 +08:00
|
|
|
|
|
|
|
|
if (!cacheKey.isEmpty()) {
|
|
|
|
|
prevResp = QString::fromStdString(params.get(cacheKey.toStdString()));
|
|
|
|
|
if (!prevResp.isEmpty()) {
|
2021-11-29 18:19:08 +08:00
|
|
|
QTimer::singleShot(500, [=]() { emit requestDone(prevResp, true); });
|
2021-06-19 14:47:23 +08:00
|
|
|
}
|
2021-11-29 18:19:08 +08:00
|
|
|
QObject::connect(this, &HttpRequest::requestDone, [=](const QString &resp, bool success) {
|
|
|
|
|
if (success && resp != prevResp) {
|
2021-06-19 14:47:23 +08:00
|
|
|
params.put(cacheKey.toStdString(), resp.toStdString());
|
|
|
|
|
prevResp = resp;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-04-24 00:59:09 -07:00
|
|
|
}
|