mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-19 08:12:07 +08:00
e333e4f189
* cleanup include path * continue * format includes * fix testraw.cc * remove include path from SConstruct * regroup * rebase master * almost done * apply review * rename FileReader.xx to filereader.xx * rename Unlogger.x->unlogger.x * rename FrameReader.xx -> framereader.xx * apply reviews * ui.h * continue * fix framebuffer.cc build error:mv util.h up * full path to msm_media_info * fix qcom2 camerad Co-authored-by: Comma Device <device@comma.ai> old-commit-hash: 7222d0f20dc8edfe0d2f3417d1ce7c84fbd32805
71 lines
2.6 KiB
C++
71 lines
2.6 KiB
C++
#include "drive_stats.h"
|
|
|
|
#include <QDebug>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QVBoxLayout>
|
|
|
|
#include "selfdrive/common/params.h"
|
|
#include "selfdrive/ui/qt/request_repeater.h"
|
|
|
|
const double MILE_TO_KM = 1.60934;
|
|
|
|
static QLayout* build_stat_layout(QLabel** metric, const QString& name) {
|
|
QVBoxLayout* layout = new QVBoxLayout;
|
|
layout->setMargin(0);
|
|
*metric = new QLabel("0");
|
|
(*metric)->setStyleSheet("font-size: 80px; font-weight: 600;");
|
|
layout->addWidget(*metric, 0, Qt::AlignLeft);
|
|
|
|
QLabel* label = new QLabel(name);
|
|
label->setStyleSheet("font-size: 45px; font-weight: 500;");
|
|
layout->addWidget(label, 0, Qt::AlignLeft);
|
|
return layout;
|
|
}
|
|
|
|
void DriveStats::parseResponse(const QString& response) {
|
|
QJsonDocument doc = QJsonDocument::fromJson(response.trimmed().toUtf8());
|
|
if (doc.isNull()) {
|
|
qDebug() << "JSON Parse failed on getting past drives statistics";
|
|
return;
|
|
}
|
|
|
|
auto update = [](const QJsonObject &obj, StatsLabels& labels, bool metric) {
|
|
labels.routes->setText(QString::number((int)obj["routes"].toDouble()));
|
|
labels.distance->setText(QString::number(int(obj["distance"].toDouble() * (metric ? MILE_TO_KM : 1))));
|
|
labels.hours->setText(QString::number((int)(obj["minutes"].toDouble() / 60)));
|
|
};
|
|
|
|
QJsonObject json = doc.object();
|
|
update(json["all"].toObject(), all_, metric);
|
|
update(json["week"].toObject(), week_, metric);
|
|
}
|
|
|
|
DriveStats::DriveStats(QWidget* parent) : QWidget(parent) {
|
|
metric = Params().getBool("IsMetric");
|
|
QString distance_unit = metric ? "KM" : "MILES";
|
|
|
|
auto add_stats_layouts = [&](QGridLayout* gl, StatsLabels& labels, int row, const QString &distance_unit) {
|
|
gl->addLayout(build_stat_layout(&labels.routes, "DRIVES"), row, 0, 3, 1);
|
|
gl->addLayout(build_stat_layout(&labels.distance, distance_unit), row, 1, 3, 1);
|
|
gl->addLayout(build_stat_layout(&labels.hours, "HOURS"), row, 2, 3, 1);
|
|
};
|
|
|
|
QGridLayout* gl = new QGridLayout(this);
|
|
gl->setMargin(0);
|
|
|
|
gl->addWidget(new QLabel("ALL TIME"), 0, 0, 1, 3);
|
|
add_stats_layouts(gl, all_, 1, distance_unit);
|
|
|
|
gl->addWidget(new QLabel("PAST WEEK"), 6, 0, 1, 3);
|
|
add_stats_layouts(gl, week_, 7, distance_unit);
|
|
|
|
QString dongleId = QString::fromStdString(Params().get("DongleId"));
|
|
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);
|
|
|
|
setLayout(gl);
|
|
setStyleSheet(R"(QLabel {font-size: 48px; font-weight: 500;})");
|
|
}
|