fix OTA
This commit is contained in:
@@ -13,6 +13,62 @@
|
||||
#include <QFileInfo>
|
||||
#include <QPointer>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace {
|
||||
|
||||
QString installRoot()
|
||||
{
|
||||
const QString env = qEnvironmentVariable("BUTTONTASK_ROOT");
|
||||
return env.isEmpty() ? QStringLiteral("/opt/buttontask") : env;
|
||||
}
|
||||
|
||||
void appendButtonLog(const QJsonObject &entry)
|
||||
{
|
||||
const QString runDir = installRoot() + QStringLiteral("/run");
|
||||
const QString path = runDir + QStringLiteral("/button-log.jsonl");
|
||||
QDir().mkpath(runDir);
|
||||
|
||||
QFile f(path);
|
||||
if (f.exists() && f.size() > 200000 && f.open(QIODevice::ReadOnly)) {
|
||||
QList<QByteArray> lines = f.readAll().split('\n');
|
||||
f.close();
|
||||
while (lines.size() > 150)
|
||||
lines.removeFirst();
|
||||
if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
f.write(lines.join('\n'));
|
||||
if (!lines.isEmpty())
|
||||
f.write("\n");
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
if (!f.open(QIODevice::Append | QIODevice::Text))
|
||||
return;
|
||||
f.write(QJsonDocument(entry).toJson(QJsonDocument::Compact));
|
||||
f.write("\n");
|
||||
}
|
||||
|
||||
void logButtonResult(const QString &label, const QString &method,
|
||||
const QString &url, bool ok, int httpStatus,
|
||||
qint64 elapsedMs, const QString &message)
|
||||
{
|
||||
QJsonObject log;
|
||||
log.insert(QStringLiteral("ts"),
|
||||
QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss")));
|
||||
log.insert(QStringLiteral("label"), label);
|
||||
log.insert(QStringLiteral("method"), method);
|
||||
log.insert(QStringLiteral("url"), url);
|
||||
log.insert(QStringLiteral("ok"), ok);
|
||||
if (httpStatus > 0)
|
||||
log.insert(QStringLiteral("http"), httpStatus);
|
||||
log.insert(QStringLiteral("ms"), elapsedMs);
|
||||
log.insert(QStringLiteral("message"), message);
|
||||
appendButtonLog(log);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ButtonController::ButtonController(ConfigManager *config, ButtonsModel *model, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -52,10 +108,15 @@ void ButtonController::invokeButton(const QString &id)
|
||||
bool acceptAnyResponse = action.value("acceptAnyResponse").toBool(false);
|
||||
QString successText = feedback.value("successText").toString(btn.value("label").toString());
|
||||
QString errorText = feedback.value("errorText").toString(QStringLiteral("Ошибка"));
|
||||
QString label = btn.value("label").toString();
|
||||
QString httpMethod = (type == QLatin1String("http_post")) ? QStringLiteral("POST")
|
||||
: QStringLiteral("GET");
|
||||
const qint64 startedMs = QDateTime::currentMSecsSinceEpoch();
|
||||
|
||||
if (url.isEmpty()) {
|
||||
qWarning() << "invokeButton: empty url for" << id;
|
||||
m_model->setStatus(id, 2, errorText);
|
||||
logButtonResult(label, httpMethod, url, false, 0, 0, errorText);
|
||||
emit buttonInvoked(id, false, errorText);
|
||||
return;
|
||||
}
|
||||
@@ -88,7 +149,8 @@ void ButtonController::invokeButton(const QString &id)
|
||||
});
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this,
|
||||
[this, reply, id, successText, errorText, fadeMs, acceptAnyResponse]() {
|
||||
[this, reply, id, label, httpMethod, url, successText, errorText,
|
||||
fadeMs, acceptAnyResponse, startedMs]() {
|
||||
const int httpStatus =
|
||||
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
bool ok;
|
||||
@@ -100,6 +162,9 @@ void ButtonController::invokeButton(const QString &id)
|
||||
ok = (reply->error() == QNetworkReply::NoError);
|
||||
}
|
||||
QString msg = ok ? successText : errorText;
|
||||
const qint64 elapsedMs = qMax<qint64>(
|
||||
0, QDateTime::currentMSecsSinceEpoch() - startedMs);
|
||||
logButtonResult(label, httpMethod, url, ok, httpStatus, elapsedMs, msg);
|
||||
m_model->setStatus(id, ok ? 1 : 2, msg);
|
||||
emit buttonInvoked(id, ok, msg);
|
||||
QString idCopy = id;
|
||||
|
||||
Reference in New Issue
Block a user