123 lines
3.5 KiB
C++
123 lines
3.5 KiB
C++
#include "beerservice.h"
|
|
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QStandardPaths>
|
|
#include <QFile>
|
|
#include <QDebug>
|
|
|
|
namespace {
|
|
|
|
constexpr auto GuestUserId = "2641ffe8cd4311eda27f0242ac120002";
|
|
|
|
}
|
|
|
|
BeerService::BeerService(QObject *parent)
|
|
: QObject{parent}
|
|
{
|
|
restoreStash();
|
|
|
|
connect(&m_socket, &QWebSocket::textMessageReceived, this, [this](QString message) {
|
|
QJsonParseError err;
|
|
QJsonDocument doc = QJsonDocument::fromJson(message.toLocal8Bit(), &err);
|
|
if (err.error != QJsonParseError::NoError) {
|
|
qWarning() << "Json parse error:" << err.errorString() << message;
|
|
return;
|
|
}
|
|
|
|
QString entity = doc.object().value("entity").toString();
|
|
const QList<QObject *> listeners = m_listeners.values(entity);
|
|
for (QObject *listener : listeners) {
|
|
QString event = doc.object().value("event").toString();
|
|
QVariant data = doc.object().value("data").toVariant();
|
|
QMetaObject::invokeMethod(listener, event.toLatin1(), Qt::QueuedConnection, Q_ARG(QVariant, data));
|
|
}
|
|
});
|
|
|
|
connect(&m_socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, [this](QAbstractSocket::SocketError error) {
|
|
qInfo() << error << m_socket.errorString();
|
|
});
|
|
|
|
connect(&m_socket, &QWebSocket::connected, this, [this]() {
|
|
while (!m_commandStash.isEmpty()) {
|
|
sendCommand(m_commandStash.takeFirst().toMap());
|
|
}
|
|
});
|
|
}
|
|
|
|
BeerService::~BeerService()
|
|
{
|
|
saveStash();
|
|
m_socket.close();
|
|
}
|
|
|
|
void BeerService::connectSrv(const QString &userId)
|
|
{
|
|
if (QAbstractSocket::ConnectedState == m_socket.state()) {
|
|
m_socket.close();
|
|
}
|
|
|
|
QNetworkRequest request(QUrl("ws://195.133.196.161:8000"));
|
|
QString authString = QString("%1:pass").arg(userId.isEmpty() ? GuestUserId : userId);
|
|
request.setRawHeader("Authorization", "Basic " + authString.toLatin1().toBase64());
|
|
m_socket.open(request);
|
|
}
|
|
|
|
void BeerService::sendCommand(const QString &entity, const QString &action, const QVariantMap &data)
|
|
{
|
|
sendCommand(QVariantMap {
|
|
{ "entity", entity },
|
|
{ "action", action },
|
|
{ "data", data }
|
|
});
|
|
}
|
|
|
|
void BeerService::connectListener(QObject *listener)
|
|
{
|
|
QString entity = listener->property("entity").toString();
|
|
m_listeners.insert(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();
|
|
} else {
|
|
qWarning() << stash.errorString();
|
|
}
|
|
}
|
|
|
|
void BeerService::sendCommand(const QVariantMap &command)
|
|
{
|
|
if (QAbstractSocket::ConnectedState == m_socket.state()) {
|
|
QJsonDocument doc = QJsonDocument::fromVariant(command);
|
|
m_socket.sendTextMessage(doc.toJson(QJsonDocument::Compact));
|
|
} else {
|
|
m_commandStash << command;
|
|
}
|
|
}
|