Dump service was added
This commit is contained in:
@@ -2,10 +2,6 @@
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QStandardPaths>
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
|
||||
#include "settingsservice.h"
|
||||
|
||||
@@ -19,6 +15,7 @@ BeerService::BeerService()
|
||||
{ ActionModify, "mod" }
|
||||
};
|
||||
|
||||
m_dumpService.setEntityName("command");
|
||||
restoreStash();
|
||||
|
||||
connect(&m_socket, &QWebSocket::textMessageReceived, this, [this](QString message) {
|
||||
@@ -58,7 +55,7 @@ BeerService::BeerService()
|
||||
|
||||
BeerService::~BeerService()
|
||||
{
|
||||
saveStash();
|
||||
m_dumpService.dump(m_commandStash);
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
@@ -90,37 +87,10 @@ void BeerService::removeListener(QObject *listener)
|
||||
m_listeners.remove(entity, listener);
|
||||
}
|
||||
|
||||
QString BeerService::stashFileName() const
|
||||
{
|
||||
return QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/command.stash";
|
||||
}
|
||||
|
||||
void BeerService::saveStash() const
|
||||
{
|
||||
if (m_commandStash.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QFile stash(stashFileName());
|
||||
if (stash.open(QIODevice::WriteOnly)) {
|
||||
stash.write(QJsonDocument::fromVariant(m_commandStash).toJson(QJsonDocument::Compact));
|
||||
stash.close();
|
||||
} else {
|
||||
qWarning() << stash.errorString();
|
||||
}
|
||||
}
|
||||
|
||||
void BeerService::restoreStash()
|
||||
{
|
||||
QFile stash(stashFileName());
|
||||
if (stash.open(QIODevice::ReadOnly)) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(stash.readAll());
|
||||
m_commandStash = doc.array().toVariantList();
|
||||
stash.close();
|
||||
stash.remove();
|
||||
} else {
|
||||
qWarning() << stash.errorString();
|
||||
}
|
||||
m_commandStash = m_dumpService.loadList();
|
||||
m_dumpService.clear();
|
||||
}
|
||||
|
||||
void BeerService::reconnect()
|
||||
@@ -135,7 +105,6 @@ void BeerService::reconnect()
|
||||
QNetworkRequest request(serverUrl);
|
||||
request.setRawHeader("Authorization", "Basic " + QString("%1:pass").arg(userId).toLatin1().toBase64());
|
||||
m_socket.open(request);
|
||||
|
||||
}
|
||||
|
||||
void BeerService::sendCommand(const QVariantMap &command)
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <QObject>
|
||||
#include <QtWebSockets/QWebSocket>
|
||||
|
||||
#include "services/dumpservice.h"
|
||||
|
||||
class SettingsService;
|
||||
class BeerService : public QObject
|
||||
{
|
||||
@@ -40,8 +42,6 @@ private:
|
||||
|
||||
SettingsService *settings() const;
|
||||
|
||||
QString stashFileName() const;
|
||||
void saveStash() const;
|
||||
void restoreStash();
|
||||
|
||||
void reconnect();
|
||||
@@ -50,6 +50,7 @@ private:
|
||||
|
||||
QMultiMap<QString, QObject *> m_listeners;
|
||||
|
||||
DumpService m_dumpService;
|
||||
QWebSocket m_socket;
|
||||
QVariantList m_commandStash;
|
||||
QMap<Action, QString> m_actions;
|
||||
|
||||
62
services/dumpservice.cpp
Normal file
62
services/dumpservice.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "dumpservice.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QStandardPaths>
|
||||
|
||||
void DumpService::setEntityName(const QString &name)
|
||||
{
|
||||
m_entityName = name;
|
||||
}
|
||||
|
||||
void DumpService::dump(const QVariant &data) const
|
||||
{
|
||||
QFile stash(dumpFileName());
|
||||
if (stash.open(QIODevice::WriteOnly)) {
|
||||
stash.write(QJsonDocument::fromVariant(data).toJson(QJsonDocument::Compact));
|
||||
stash.close();
|
||||
} else {
|
||||
qWarning() << stash.errorString();
|
||||
}
|
||||
}
|
||||
|
||||
QVariantList DumpService::loadList() const
|
||||
{
|
||||
QJsonDocument dump = readFile();
|
||||
return dump.array().toVariantList();
|
||||
}
|
||||
|
||||
QVariantMap DumpService::loadMap() const
|
||||
{
|
||||
QJsonDocument dump = readFile();
|
||||
return dump.object().toVariantMap();
|
||||
}
|
||||
|
||||
void DumpService::clear() const
|
||||
{
|
||||
QFile::remove(dumpFileName());
|
||||
}
|
||||
|
||||
QString DumpService::dumpFileName() const
|
||||
{
|
||||
Q_ASSERT(!m_entityName.isEmpty());
|
||||
|
||||
return QString("%1/%2.dump").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), m_entityName);
|
||||
}
|
||||
|
||||
QJsonDocument DumpService::readFile() const
|
||||
{
|
||||
QJsonDocument res;
|
||||
|
||||
QFile dump(dumpFileName());
|
||||
if (dump.open(QIODevice::ReadOnly)) {
|
||||
res = QJsonDocument::fromJson(dump.readAll());
|
||||
dump.close();
|
||||
} else {
|
||||
qWarning() << dump.errorString();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
24
services/dumpservice.h
Normal file
24
services/dumpservice.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef DUMPSERVICE_H
|
||||
#define DUMPSERVICE_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
class QJsonDocument;
|
||||
class DumpService
|
||||
{
|
||||
public:
|
||||
void setEntityName(const QString &name);
|
||||
|
||||
void dump(const QVariant &data) const;
|
||||
QVariantList loadList() const;
|
||||
QVariantMap loadMap() const;
|
||||
void clear() const;
|
||||
|
||||
private:
|
||||
QString dumpFileName() const;
|
||||
QJsonDocument readFile() const;
|
||||
|
||||
QString m_entityName;
|
||||
};
|
||||
|
||||
#endif // DUMPSERVICE_H
|
||||
Reference in New Issue
Block a user