ui/map: singleton navigation requests (#28862)

pull/28885/head
Dean Lee 2 years ago committed by GitHub
parent d34138e275
commit 8149c07fac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 75
      selfdrive/ui/qt/maps/map_settings.cc
  2. 16
      selfdrive/ui/qt/maps/map_settings.h

@ -1,5 +1,6 @@
#include "map_settings.h"
#include <QApplication>
#include <QDebug>
#include <vector>
@ -84,38 +85,8 @@ MapSettings::MapSettings(bool closeable, QWidget *parent)
setStyleSheet("MapSettings { background-color: #333333; }");
if (auto dongle_id = getDongleId()) {
// Fetch favorite and recent locations
{
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations";
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true);
QObject::connect(repeater, &RequestRepeater::requestDone, this, &MapSettings::parseResponse);
}
// Destination set while offline
{
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/next";
RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true);
HttpRequest* deleter = new HttpRequest(this);
QObject::connect(repeater, &RequestRepeater::requestDone, [=](const QString &resp, bool success) {
if (success && resp != "null") {
if (params.get("NavDestination").empty()) {
qWarning() << "Setting NavDestination from /next" << resp;
params.put("NavDestination", resp.toStdString());
} else {
qWarning() << "Got location from /next, but NavDestination already set";
}
// Send DELETE to clear destination server side
deleter->sendRequest(url, HttpRequest::Method::DELETE);
}
// Update UI (athena can set destination at any time)
updateCurrentRoute();
});
}
}
QObject::connect(NavigationRequest::instance(), &NavigationRequest::locationsUpdated, this, &MapSettings::parseResponse);
QObject::connect(NavigationRequest::instance(), &NavigationRequest::nextDestinationUpdated, this, &MapSettings::updateCurrentRoute);
}
void MapSettings::mousePressEvent(QMouseEvent *ev) {
@ -347,3 +318,43 @@ void DestinationWidget::unset(const QString &label, bool current) {
setStyleSheet(styleSheet());
}
// singleton NavigationRequest
NavigationRequest *NavigationRequest::instance() {
static NavigationRequest *request = new NavigationRequest(qApp);
return request;
}
NavigationRequest::NavigationRequest(QObject *parent) : QObject(parent) {
if (auto dongle_id = getDongleId()) {
{
// Fetch favorite and recent locations
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations";
RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true);
QObject::connect(repeater, &RequestRepeater::requestDone, this, &NavigationRequest::locationsUpdated);
}
{
// Destination set while offline
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/next";
HttpRequest *deleter = new HttpRequest(this);
RequestRepeater *repeater = new RequestRepeater(this, url, "", 10, true);
QObject::connect(repeater, &RequestRepeater::requestDone, [=](const QString &resp, bool success) {
if (success && resp != "null") {
if (params.get("NavDestination").empty()) {
qWarning() << "Setting NavDestination from /next" << resp;
params.put("NavDestination", resp.toStdString());
} else {
qWarning() << "Got location from /next, but NavDestination already set";
}
// Send DELETE to clear destination server side
deleter->sendRequest(url, HttpRequest::Method::DELETE);
}
// Update UI (athena can set destination at any time)
emit nextDestinationUpdated(resp, success);
});
}
}
}

@ -23,6 +23,22 @@ const QString NAV_FAVORITE_LABEL_WORK = "work";
class NavDestination;
class DestinationWidget;
class NavigationRequest : public QObject {
Q_OBJECT
public:
static NavigationRequest *instance();
signals:
void locationsUpdated(const QString &response, bool success);
void nextDestinationUpdated(const QString &response, bool success);
private:
NavigationRequest(QObject *parent);
Params params;
};
class MapSettings : public QFrame {
Q_OBJECT
public:

Loading…
Cancel
Save