Refactor websockets
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "senttxstore.h"
|
||||
#include "turnstile.h"
|
||||
#include "version.h"
|
||||
#include "websockets.h"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
@@ -712,9 +713,11 @@ void RPC::refreshBalances() {
|
||||
|
||||
// 1. Get the Balances
|
||||
getBalance([=] (json reply) {
|
||||
balT = QString::fromStdString(reply["transparent"]).toDouble();
|
||||
balZ = QString::fromStdString(reply["private"]).toDouble();
|
||||
balTotal = QString::fromStdString(reply["total"]).toDouble();
|
||||
auto balT = QString::fromStdString(reply["transparent"]).toDouble();
|
||||
auto balZ = QString::fromStdString(reply["private"]).toDouble();
|
||||
auto balTotal = QString::fromStdString(reply["total"]).toDouble();
|
||||
|
||||
AppDataModel::getInstance()->setBalances(balT, balZ);
|
||||
|
||||
ui->balSheilded ->setText(Settings::getZECDisplayFormat(balZ));
|
||||
ui->balTransparent->setText(Settings::getZECDisplayFormat(balT));
|
||||
|
||||
@@ -71,10 +71,6 @@ public:
|
||||
Turnstile* getTurnstile() { return turnstile; }
|
||||
Connection* getConnection() { return conn; }
|
||||
|
||||
double balT;
|
||||
double balZ;
|
||||
double balTotal;
|
||||
|
||||
private:
|
||||
void refreshBalances();
|
||||
|
||||
|
||||
@@ -186,14 +186,26 @@ void TxTableModel::updateAllData() {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QString TxTableModel::getTxId(int row) {
|
||||
QString TxTableModel::getTxId(int row) const {
|
||||
return modeldata->at(row).txid;
|
||||
}
|
||||
|
||||
QString TxTableModel::getMemo(int row) {
|
||||
QString TxTableModel::getMemo(int row) const {
|
||||
return modeldata->at(row).memo;
|
||||
}
|
||||
|
||||
QString TxTableModel::getAddr(int row) {
|
||||
QString TxTableModel::getAddr(int row) const {
|
||||
return modeldata->at(row).address.trimmed();
|
||||
}
|
||||
|
||||
qint64 TxTableModel::getDate(int row) const {
|
||||
return modeldata->at(row).datetime;
|
||||
}
|
||||
|
||||
QString TxTableModel::getType(int row) const {
|
||||
return modeldata->at(row).type;
|
||||
}
|
||||
|
||||
QString TxTableModel::getAmt(int row) const {
|
||||
return Settings::getDecimalString(modeldata->at(row).amount);
|
||||
}
|
||||
@@ -15,9 +15,12 @@ public:
|
||||
void addZSentData(const QList<TransactionItem>& data);
|
||||
void addZRecvData(const QList<TransactionItem>& data);
|
||||
|
||||
QString getTxId(int row);
|
||||
QString getMemo(int row);
|
||||
QString getAddr(int row);
|
||||
QString getTxId(int row) const;
|
||||
QString getMemo(int row) const;
|
||||
QString getAddr(int row) const;
|
||||
qint64 getDate(int row) const;
|
||||
QString getType(int row) const;
|
||||
QString getAmt (int row) const;
|
||||
|
||||
bool exportToCsv(QString fileName) const;
|
||||
|
||||
|
||||
@@ -43,12 +43,7 @@ void WSServer::processTextMessage(QString message)
|
||||
if (m_debug)
|
||||
qDebug() << "Message received:" << message;
|
||||
if (pClient) {
|
||||
QJsonDocument json(QJsonObject {
|
||||
{"saplingAddress", m_mainWindow->getRPC()->getDefaultSaplingAddress()},
|
||||
{"balance", m_mainWindow->getRPC()->balTotal},
|
||||
{"zecprice", Settings::getInstance()->getZECPrice()}
|
||||
});
|
||||
|
||||
auto json = AppDataServer::processMessage(message, m_mainWindow);
|
||||
pClient->sendTextMessage(json.toJson());
|
||||
}
|
||||
}
|
||||
@@ -72,4 +67,59 @@ void WSServer::socketDisconnected()
|
||||
m_clients.removeAll(pClient);
|
||||
pClient->deleteLater();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// AppDataServer
|
||||
// ==============================
|
||||
QJsonDocument AppDataServer::processMessage(QString message, MainWindow* mainWindow) {
|
||||
// First, extract the command from the message
|
||||
auto msg = QJsonDocument::fromJson(message.toUtf8());
|
||||
if (!msg.object().contains("command")) {
|
||||
return QJsonDocument(QJsonObject{
|
||||
{"errorCode", -1},
|
||||
{"errorMessage", "Unknown JSON format"}
|
||||
});
|
||||
}
|
||||
|
||||
if (msg["command"] == "getInfo") {
|
||||
return processGetInfo(mainWindow);
|
||||
}
|
||||
else if (msg["command"] == "getTransactions") {
|
||||
return processGetTransactions(mainWindow);
|
||||
}
|
||||
else {
|
||||
return QJsonDocument(QJsonObject{
|
||||
{"errorCode", -1},
|
||||
{"errorMessage", "Command not found:" + msg["command"].toString()}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
QJsonDocument AppDataServer::processGetInfo(MainWindow* mainWindow) {
|
||||
return QJsonDocument(QJsonObject{
|
||||
{"version", 1.0},
|
||||
{"saplingAddress", mainWindow->getRPC()->getDefaultSaplingAddress()},
|
||||
{"balance", AppDataModel::getInstance()->getTotalBalance()},
|
||||
{"zecprice", Settings::getInstance()->getZECPrice()}
|
||||
});
|
||||
}
|
||||
|
||||
QJsonDocument AppDataServer::processGetTransactions(MainWindow* mainWindow) {
|
||||
QJsonArray txns;
|
||||
auto model = mainWindow->getRPC()->getTransactionsModel();
|
||||
for (int i = 0; i < model->rowCount(QModelIndex()); i++) {
|
||||
txns.append(QJsonObject{
|
||||
{"type", model->getType(i)},
|
||||
{"datetime", model->getDate(i)},
|
||||
{"amount", model->getAmt(i)}
|
||||
});
|
||||
}
|
||||
|
||||
return QJsonDocument(txns);
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// AppDataModel
|
||||
// ==============================
|
||||
AppDataModel* AppDataModel::instance = NULL;
|
||||
@@ -31,4 +31,43 @@ private:
|
||||
bool m_debug;
|
||||
};
|
||||
|
||||
class AppDataServer {
|
||||
public:
|
||||
static QJsonDocument processMessage(QString message, MainWindow* mainWindow);
|
||||
static QJsonDocument processGetInfo(MainWindow* mainWindow);
|
||||
static QJsonDocument processGetTransactions(MainWindow* mainWindow);
|
||||
};
|
||||
|
||||
class AppDataModel {
|
||||
public:
|
||||
static AppDataModel* getInstance() {
|
||||
if (instance == NULL)
|
||||
instance = new AppDataModel();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
double getTBalance() { return balTransparent; }
|
||||
double getZBalance() { return balShielded; }
|
||||
double getTotalBalance() { return balTotal; }
|
||||
|
||||
void setBalances(double transparent, double shielded) {
|
||||
balTransparent = transparent;
|
||||
balShielded = shielded;
|
||||
balTotal = balTransparent + balShielded;
|
||||
}
|
||||
|
||||
private:
|
||||
AppDataModel() = default; // Private, for singleton
|
||||
|
||||
double balTransparent;
|
||||
double balShielded;
|
||||
double balTotal;
|
||||
double balMaxSingle;
|
||||
|
||||
QString saplingAddress;
|
||||
|
||||
static AppDataModel* instance;
|
||||
};
|
||||
|
||||
#endif // WEBSOCKETS_H
|
||||
Reference in New Issue
Block a user