ui: consolidate prime state management into `PrimeState` Class (#33473)
new class PrimeStatepull/33517/head
parent
592778ab29
commit
343de54030
14 changed files with 118 additions and 105 deletions
@ -0,0 +1,48 @@ |
||||
#include "selfdrive/ui/qt/prime_state.h" |
||||
|
||||
#include <QJsonDocument> |
||||
|
||||
#include "selfdrive/ui/qt/api.h" |
||||
#include "selfdrive/ui/qt/request_repeater.h" |
||||
#include "selfdrive/ui/qt/util.h" |
||||
|
||||
PrimeState::PrimeState(QObject* parent) : QObject(parent) { |
||||
const char *env_prime_type = std::getenv("PRIME_TYPE"); |
||||
auto type = env_prime_type ? env_prime_type : Params().get("PrimeType"); |
||||
|
||||
if (!type.empty()) { |
||||
prime_type = static_cast<PrimeState::Type>(std::atoi(type.c_str())); |
||||
} |
||||
|
||||
if (auto dongleId = getDongleId()) { |
||||
QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/"; |
||||
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_Device", 5); |
||||
QObject::connect(repeater, &RequestRepeater::requestDone, this, &PrimeState::handleReply); |
||||
} |
||||
|
||||
// Emit the initial state change
|
||||
QTimer::singleShot(1, [this]() { emit changed(prime_type); }); |
||||
} |
||||
|
||||
void PrimeState::handleReply(const QString& response, bool success) { |
||||
if (!success) return; |
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8()); |
||||
if (doc.isNull()) { |
||||
qDebug() << "JSON Parse failed on getting pairing and PrimeState status"; |
||||
return; |
||||
} |
||||
|
||||
QJsonObject json = doc.object(); |
||||
bool is_paired = json["is_paired"].toBool(); |
||||
auto type = static_cast<PrimeState::Type>(json["prime_type"].toInt()); |
||||
setType(is_paired ? type : PrimeState::PRIME_TYPE_UNPAIRED); |
||||
} |
||||
|
||||
void PrimeState::setType(PrimeState::Type type) { |
||||
if (type != prime_type) { |
||||
prime_type = type; |
||||
Params().put("PrimeType", std::to_string(prime_type)); |
||||
emit changed(prime_type); |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
#pragma once |
||||
|
||||
#include <QObject> |
||||
|
||||
class PrimeState : public QObject { |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
|
||||
enum Type { |
||||
PRIME_TYPE_UNKNOWN = -2, |
||||
PRIME_TYPE_UNPAIRED = -1, |
||||
PRIME_TYPE_NONE = 0, |
||||
PRIME_TYPE_MAGENTA = 1, |
||||
PRIME_TYPE_LITE = 2, |
||||
PRIME_TYPE_BLUE = 3, |
||||
PRIME_TYPE_MAGENTA_NEW = 4, |
||||
PRIME_TYPE_PURPLE = 5, |
||||
}; |
||||
|
||||
PrimeState(QObject *parent); |
||||
void setType(PrimeState::Type type); |
||||
inline PrimeState::Type currentType() const { return prime_type; } |
||||
inline bool isSubscribed() const { return prime_type > PrimeState::PRIME_TYPE_NONE; } |
||||
|
||||
signals: |
||||
void changed(PrimeState::Type prime_type); |
||||
|
||||
private: |
||||
void handleReply(const QString &response, bool success); |
||||
|
||||
PrimeState::Type prime_type = PrimeState::PRIME_TYPE_UNKNOWN; |
||||
}; |
Loading…
Reference in new issue