mirror of
https://github.com/MoreTore/openpilot.git
synced 2026-08-03 08:41:47 +08:00
qt/util: new function getDongleId (#21810)
* new function getDongleId * return std::optional
This commit is contained in:
@@ -156,10 +156,6 @@ std::string dir_name(std::string const &path) {
|
||||
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() {
|
||||
time_t 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 base_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 *****
|
||||
std::string read_file(const std::string& fn);
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "selfdrive/common/params.h"
|
||||
#include "selfdrive/common/util.h"
|
||||
#include "selfdrive/hardware/hw.h"
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
|
||||
namespace CommaApi {
|
||||
|
||||
@@ -48,9 +49,8 @@ QByteArray rsa_sign(const QByteArray &data) {
|
||||
QString create_jwt(const QJsonObject &payloads, int expiry) {
|
||||
QJsonObject header = {{"alg", "RS256"}};
|
||||
|
||||
QString dongle_id = QString::fromStdString(Params().get("DongleId"));
|
||||
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) {
|
||||
payload.insert(it.key(), it.value());
|
||||
}
|
||||
|
||||
@@ -93,19 +93,18 @@ MapPanel::MapPanel(QWidget* parent) : QWidget(parent) {
|
||||
|
||||
clear();
|
||||
|
||||
std::string dongle_id = params.get("DongleId");
|
||||
if (util::is_valid_dongle_id(dongle_id)) {
|
||||
if (auto dongle_id = getDongleId()) {
|
||||
// Fetch favorite and recent locations
|
||||
{
|
||||
std::string url = "https://api.commadotai.com/v1/navigation/" + dongle_id + "/locations";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_NavDestinations", 30, true);
|
||||
QString url = "https://api.commadotai.com/v1/navigation/" + *dongle_id + "/locations";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &MapPanel::parseResponse);
|
||||
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &MapPanel::failedResponse);
|
||||
}
|
||||
|
||||
// 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);
|
||||
HttpRequest* deleter = new HttpRequest(this);
|
||||
|
||||
|
||||
@@ -95,9 +95,7 @@ TogglesPanel::TogglesPanel(QWidget *parent) : QWidget(parent) {
|
||||
DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) {
|
||||
QVBoxLayout *main_layout = new QVBoxLayout(this);
|
||||
Params params = Params();
|
||||
|
||||
QString dongle = QString::fromStdString(params.get("DongleId", false));
|
||||
main_layout->addWidget(new LabelControl("Dongle ID", dongle));
|
||||
main_layout->addWidget(new LabelControl("Dongle ID", getDongleId().value_or("")));
|
||||
main_layout->addWidget(horizontal_line());
|
||||
|
||||
QString serial = QString::fromStdString(params.get("HardwareSerial", false));
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "selfdrive/common/params.h"
|
||||
#include "selfdrive/common/swaglog.h"
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
|
||||
template <typename T>
|
||||
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
|
||||
tethering_ssid = "weedle";
|
||||
std::string bytes = Params().get("DongleId");
|
||||
if (bytes.length() >= 4) {
|
||||
tethering_ssid += "-" + QString::fromStdString(bytes.substr(0,4));
|
||||
if (auto dongle_id = getDongleId()) {
|
||||
tethering_ssid += "-" + dongle_id->left(4);
|
||||
}
|
||||
|
||||
adapter = getAdapter();
|
||||
|
||||
@@ -16,6 +16,17 @@ QString getBrandVersion() {
|
||||
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) {
|
||||
QFont f(family);
|
||||
f.setPixelSize(size);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QLayout>
|
||||
#include <QMouseEvent>
|
||||
@@ -9,6 +11,7 @@
|
||||
|
||||
QString getBrand();
|
||||
QString getBrandVersion();
|
||||
std::optional<QString> getDongleId();
|
||||
void configFont(QPainter &p, const QString &family, int size, const QString &style);
|
||||
void clearLayout(QLayout* layout);
|
||||
void setQtSurfaceFormat();
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "selfdrive/common/params.h"
|
||||
#include "selfdrive/ui/qt/request_repeater.h"
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
|
||||
const double MILE_TO_KM = 1.60934;
|
||||
|
||||
@@ -46,10 +47,9 @@ DriveStats::DriveStats(QWidget* parent) : QFrame(parent) {
|
||||
main_layout->addStretch();
|
||||
add_stats_layouts("PAST WEEK", week_);
|
||||
|
||||
std::string dongle_id = Params().get("DongleId");
|
||||
if (util::is_valid_dongle_id(dongle_id)) {
|
||||
std::string url = "https://api.commadotai.com/v1.1/devices/" + dongle_id + "/stats";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_DriveStats", 30);
|
||||
if (auto dongleId = getDongleId()) {
|
||||
QString url = "https://api.commadotai.com/v1.1/devices/" + *dongleId + "/stats";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <QrCode.hpp>
|
||||
|
||||
#include "selfdrive/ui/qt/request_repeater.h"
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
|
||||
using qrcodegen::QrCode;
|
||||
|
||||
@@ -109,10 +110,9 @@ PrimeUserWidget::PrimeUserWidget(QWidget* parent) : QWidget(parent) {
|
||||
mainLayout->addStretch();
|
||||
|
||||
// set up API requests
|
||||
std::string dongleId = Params().get("DongleId");
|
||||
if (util::is_valid_dongle_id(dongleId)) {
|
||||
std::string url = "https://api.commadotai.com/v1/devices/" + dongleId + "/owner";
|
||||
RequestRepeater *repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_Owner", 6);
|
||||
if (auto dongleId = getDongleId()) {
|
||||
QString url = "https://api.commadotai.com/v1/devices/" + *dongleId + "/owner";
|
||||
RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_Owner", 6);
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &PrimeUserWidget::replyFinished);
|
||||
}
|
||||
}
|
||||
@@ -255,10 +255,9 @@ SetupWidget::SetupWidget(QWidget* parent) : QFrame(parent) {
|
||||
setSizePolicy(sp_retain);
|
||||
|
||||
// set up API requests
|
||||
std::string dongleId = Params().get("DongleId");
|
||||
if (util::is_valid_dongle_id(dongleId)) {
|
||||
std::string url = "https://api.commadotai.com/v1.1/devices/" + dongleId + "/";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, QString::fromStdString(url), "ApiCache_Device", 5);
|
||||
if (auto dongleId = getDongleId()) {
|
||||
QString url = "https://api.commadotai.com/v1.1/devices/" + *dongleId + "/";
|
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_Device", 5);
|
||||
|
||||
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &SetupWidget::replyFinished);
|
||||
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &SetupWidget::parseError);
|
||||
|
||||
Reference in New Issue
Block a user