qt/util: new function getDongleId (#21810)

* new function getDongleId

* return std::optional
pull/21814/head
Dean Lee 4 years ago committed by GitHub
parent fecdf16f6b
commit 073b96e5d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      selfdrive/common/util.cc
  2. 1
      selfdrive/common/util.h
  3. 4
      selfdrive/ui/qt/api.cc
  4. 9
      selfdrive/ui/qt/maps/map_settings.cc
  5. 4
      selfdrive/ui/qt/offroad/settings.cc
  6. 6
      selfdrive/ui/qt/offroad/wifiManager.cc
  7. 11
      selfdrive/ui/qt/util.cc
  8. 3
      selfdrive/ui/qt/util.h
  9. 8
      selfdrive/ui/qt/widgets/drive_stats.cc
  10. 15
      selfdrive/ui/qt/widgets/prime.cc

@ -156,10 +156,6 @@ std::string dir_name(std::string const &path) {
return path.substr(0, pos); return path.substr(0, pos);
} }
bool is_valid_dongle_id(std::string const& dongle_id) {
return !dongle_id.empty() && dongle_id != "UnregisteredDevice";
}
struct tm get_time() { struct tm get_time() {
time_t rawtime; time_t rawtime;
time(&rawtime); time(&rawtime);

@ -58,7 +58,6 @@ std::string tohex(const uint8_t* buf, size_t buf_size);
std::string hexdump(const std::string& in); std::string hexdump(const std::string& in);
std::string base_name(std::string const& path); std::string base_name(std::string const& path);
std::string dir_name(std::string const& path); std::string dir_name(std::string const& path);
bool is_valid_dongle_id(std::string const& dongle_id);
// **** file fhelpers ***** // **** file fhelpers *****
std::string read_file(const std::string& fn); std::string read_file(const std::string& fn);

@ -14,6 +14,7 @@
#include "selfdrive/common/params.h" #include "selfdrive/common/params.h"
#include "selfdrive/common/util.h" #include "selfdrive/common/util.h"
#include "selfdrive/hardware/hw.h" #include "selfdrive/hardware/hw.h"
#include "selfdrive/ui/qt/util.h"
namespace CommaApi { namespace CommaApi {
@ -48,9 +49,8 @@ QByteArray rsa_sign(const QByteArray &data) {
QString create_jwt(const QJsonObject &payloads, int expiry) { QString create_jwt(const QJsonObject &payloads, int expiry) {
QJsonObject header = {{"alg", "RS256"}}; QJsonObject header = {{"alg", "RS256"}};
QString dongle_id = QString::fromStdString(Params().get("DongleId"));
auto t = QDateTime::currentSecsSinceEpoch(); auto t = QDateTime::currentSecsSinceEpoch();
QJsonObject payload = {{"identity", dongle_id}, {"nbf", t}, {"iat", t}, {"exp", t + expiry}}; QJsonObject payload = {{"identity", getDongleId().value_or("")}, {"nbf", t}, {"iat", t}, {"exp", t + expiry}};
for (auto it = payloads.begin(); it != payloads.end(); ++it) { for (auto it = payloads.begin(); it != payloads.end(); ++it) {
payload.insert(it.key(), it.value()); payload.insert(it.key(), it.value());
} }

@ -93,19 +93,18 @@ MapPanel::MapPanel(QWidget* parent) : QWidget(parent) {
clear(); clear();
std::string dongle_id = params.get("DongleId"); if (auto dongle_id = getDongleId()) {
if (util::is_valid_dongle_id(dongle_id)) {
// Fetch favorite and recent locations // Fetch favorite and recent locations
{ {
std::string url = "https://api.commadotai.com/v1/navigation/" + dongle_id + "/locations"; QString url = "https://api.commadotai.com/v1/navigation/" + *dongle_id + "/locations";
RequestRepeater* repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_NavDestinations", 30, true); RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &MapPanel::parseResponse); QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &MapPanel::parseResponse);
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &MapPanel::failedResponse); QObject::connect(repeater, &RequestRepeater::failedResponse, this, &MapPanel::failedResponse);
} }
// Destination set while offline // Destination set while offline
{ {
QString url = QString::fromStdString("https://api.commadotai.com/v1/navigation/" + dongle_id + "/next"); QString url = "https://api.commadotai.com/v1/navigation/" + *dongle_id + "/next";
RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true); RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true);
HttpRequest* deleter = new HttpRequest(this); HttpRequest* deleter = new HttpRequest(this);

@ -95,9 +95,7 @@ TogglesPanel::TogglesPanel(QWidget *parent) : QWidget(parent) {
DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) { DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) {
QVBoxLayout *main_layout = new QVBoxLayout(this); QVBoxLayout *main_layout = new QVBoxLayout(this);
Params params = Params(); Params params = Params();
main_layout->addWidget(new LabelControl("Dongle ID", getDongleId().value_or("")));
QString dongle = QString::fromStdString(params.get("DongleId", false));
main_layout->addWidget(new LabelControl("Dongle ID", dongle));
main_layout->addWidget(horizontal_line()); main_layout->addWidget(horizontal_line());
QString serial = QString::fromStdString(params.get("HardwareSerial", false)); QString serial = QString::fromStdString(params.get("HardwareSerial", false));

@ -6,6 +6,7 @@
#include "selfdrive/common/params.h" #include "selfdrive/common/params.h"
#include "selfdrive/common/swaglog.h" #include "selfdrive/common/swaglog.h"
#include "selfdrive/ui/qt/util.h"
template <typename T> template <typename T>
T get_response(QDBusMessage response) { T get_response(QDBusMessage response) {
@ -35,9 +36,8 @@ WifiManager::WifiManager(QWidget* parent) : QWidget(parent) {
// Set tethering ssid as "weedle" + first 4 characters of a dongle id // Set tethering ssid as "weedle" + first 4 characters of a dongle id
tethering_ssid = "weedle"; tethering_ssid = "weedle";
std::string bytes = Params().get("DongleId"); if (auto dongle_id = getDongleId()) {
if (bytes.length() >= 4) { tethering_ssid += "-" + dongle_id->left(4);
tethering_ssid += "-" + QString::fromStdString(bytes.substr(0,4));
} }
adapter = getAdapter(); adapter = getAdapter();

@ -16,6 +16,17 @@ QString getBrandVersion() {
return getBrand() + " v" + QString::fromStdString(Params().get("Version")).left(14).trimmed(); return getBrand() + " v" + QString::fromStdString(Params().get("Version")).left(14).trimmed();
} }
std::optional<QString> getDongleId() {
static QString dongleId;
if (dongleId.isEmpty()) {
std::string id = Params().get("DongleId");
if (!id.empty() && id != "UnregisteredDevice") {
dongleId = QString::fromStdString(id);
}
}
return !dongleId.isEmpty() ? std::make_optional(dongleId) : std::nullopt;
}
void configFont(QPainter &p, const QString &family, int size, const QString &style) { void configFont(QPainter &p, const QString &family, int size, const QString &style) {
QFont f(family); QFont f(family);
f.setPixelSize(size); f.setPixelSize(size);

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <optional>
#include <QDateTime> #include <QDateTime>
#include <QLayout> #include <QLayout>
#include <QMouseEvent> #include <QMouseEvent>
@ -9,6 +11,7 @@
QString getBrand(); QString getBrand();
QString getBrandVersion(); QString getBrandVersion();
std::optional<QString> getDongleId();
void configFont(QPainter &p, const QString &family, int size, const QString &style); void configFont(QPainter &p, const QString &family, int size, const QString &style);
void clearLayout(QLayout* layout); void clearLayout(QLayout* layout);
void setQtSurfaceFormat(); void setQtSurfaceFormat();

@ -7,6 +7,7 @@
#include "selfdrive/common/params.h" #include "selfdrive/common/params.h"
#include "selfdrive/ui/qt/request_repeater.h" #include "selfdrive/ui/qt/request_repeater.h"
#include "selfdrive/ui/qt/util.h"
const double MILE_TO_KM = 1.60934; const double MILE_TO_KM = 1.60934;
@ -46,10 +47,9 @@ DriveStats::DriveStats(QWidget* parent) : QFrame(parent) {
main_layout->addStretch(); main_layout->addStretch();
add_stats_layouts("PAST WEEK", week_); add_stats_layouts("PAST WEEK", week_);
std::string dongle_id = Params().get("DongleId"); if (auto dongleId = getDongleId()) {
if (util::is_valid_dongle_id(dongle_id)) { QString url = "https://api.commadotai.com/v1.1/devices/" + *dongleId + "/stats";
std::string url = "https://api.commadotai.com/v1.1/devices/" + dongle_id + "/stats"; RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30);
RequestRepeater* repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_DriveStats", 30);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse); QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse);
} }

@ -11,6 +11,7 @@
#include <QrCode.hpp> #include <QrCode.hpp>
#include "selfdrive/ui/qt/request_repeater.h" #include "selfdrive/ui/qt/request_repeater.h"
#include "selfdrive/ui/qt/util.h"
using qrcodegen::QrCode; using qrcodegen::QrCode;
@ -109,10 +110,9 @@ PrimeUserWidget::PrimeUserWidget(QWidget* parent) : QWidget(parent) {
mainLayout->addStretch(); mainLayout->addStretch();
// set up API requests // set up API requests
std::string dongleId = Params().get("DongleId"); if (auto dongleId = getDongleId()) {
if (util::is_valid_dongle_id(dongleId)) { QString url = "https://api.commadotai.com/v1/devices/" + *dongleId + "/owner";
std::string url = "https://api.commadotai.com/v1/devices/" + dongleId + "/owner"; RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_Owner", 6);
RequestRepeater *repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_Owner", 6);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &PrimeUserWidget::replyFinished); QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &PrimeUserWidget::replyFinished);
} }
} }
@ -255,10 +255,9 @@ SetupWidget::SetupWidget(QWidget* parent) : QFrame(parent) {
setSizePolicy(sp_retain); setSizePolicy(sp_retain);
// set up API requests // set up API requests
std::string dongleId = Params().get("DongleId"); if (auto dongleId = getDongleId()) {
if (util::is_valid_dongle_id(dongleId)) { QString url = "https://api.commadotai.com/v1.1/devices/" + *dongleId + "/";
std::string url = "https://api.commadotai.com/v1.1/devices/" + dongleId + "/"; RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_Device", 5);
RequestRepeater* repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_Device", 5);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &SetupWidget::replyFinished); QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &SetupWidget::replyFinished);
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &SetupWidget::parseError); QObject::connect(repeater, &RequestRepeater::failedResponse, this, &SetupWidget::parseError);

Loading…
Cancel
Save