1
0
forked from BRT/arc
arc/services/net/NetworkService.h

140 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <qhttpmultipart.h>
#include <QJsonArray>
#include <QNetworkAccessManager>
#include <QObject>
#include <QMap>
class QWidget;
class NetworkService final : public QObject {
Q_OBJECT
public:
explicit NetworkService(QObject *parent = nullptr);
~NetworkService() override;
void setPayload(const QByteArray &json) { m_json = json; }
void setDeviceId(const QString &deviceId) {
m_deviceId = deviceId;
}
void addExtra(const QString &key, const QVariant &value);
// Подвешивает QMessageBox с текстом ошибки от backend (HTTP 409 — "материал
// заблокирован" и подобные) к ближайшему окну. Вызывать после `new NetworkService`
// в UI-коде, до запуска треда.
static void connectErrorDialog(QWidget *parent, NetworkService *net);
public slots:
void postAccountsData();
void postBankProfile();
void patchBankProfile();
void toggleBankProfile();
void postMaterial();
void patchMaterial();
void toggleMaterial();
void postDeviceScreenshot();
void getPayments();
void postAppStatus();
void postEventStatus();
void insertTransactionData();
void updateTransactionData();
void loginAndSetup();
void syncCurrentProfile();
void createDevice();
void updateDevice();
void getTasks(const QStringList &materialIds = {});
void postTaskResult();
void postMonitoringLog(const QString &type, const QString &event,
const QString &message, const QByteArray &image);
void postMonitoringDump(const QString &text, const QByteArray &archive,
const QByteArray &image,
const QString &archiveFilename = QStringLiteral("task_dump.zip"));
void start();
void stop();
void disableAllProfilesOnServer();
void deleteDeviceFromServer();
signals:
void finished();
void finishedWithResult(int value);
void tokenRefreshFailed();
void tasksReceived(QJsonArray tasks);
// Эмитится при HTTP-ошибке PATCH-запроса (например, 409 'материал заблокирован').
// UI подписывается и показывает QMessageBox.
void apiPatchError(int httpStatus, QString message);
private:
QByteArray m_json;
QString m_deviceId;
QString m_token;
QString m_accessToken;
QString m_apiBase;
QMap<QString, QVariant> m_extraData = {};
bool m_running = true;
bool m_tokenErrorShown = false;
QString m_urlAccountUpdate;
QString m_urlPaymentsGet;
QString m_urlTransactionUpdate;
QString m_urlAppStatusUpdate;
QString m_urlTransactionInsert;
QString m_urlEventStatusUpdate;
QString m_pathAuthLogin;
QString m_pathAuthRefresh;
QString m_pathDesktopCreate;
QString m_pathDevice;
QString m_pathDeviceScreenshot;
QString m_pathBankProfile;
QString m_pathMaterial;
QString m_pathGetTasks;
QString m_pathTaskResult;
QString m_pathMonitoringLog;
QString m_pathMonitoringDump;
QString m_monitoringBase;
QString m_monitoringToken;
QString m_pathCurrentProfile;
QString m_pathProfileGetInfo;
bool m_fetchingTasks = false;
QNetworkAccessManager *m_manager;
QJsonValue post(QHttpMultiPart *multiPart, const QString &path);
QJsonArray get(const QString &path, const QMap<QString, QString> &params);
QJsonValue getJson(const QString &path, bool withAuth = false);
QJsonValue postJson(const QString &path, const QJsonObject &body, bool withAuth = false);
QJsonValue patchJson(const QString &path, const QJsonObject &body, bool withAuth = false);
QJsonValue deleteJson(const QString &path, bool withAuth = false);
bool refreshToken();
bool ensureValidToken();
void scheduleNextRefresh();
void scheduleNextStatusSync();
void syncDevicesFromServer(const QString &desktopId);
void syncBankProfilesFromServer();
void syncBanksIfEmpty();
};