provide for update logging using a file watcher

This commit is contained in:
Gerhard Hoffmann 2025-04-11 12:30:08 +02:00
parent cf3e1d61dd
commit d36c7c1f72

View File

@ -19,6 +19,7 @@
#include <QScopedPointer>
#include <QRegularExpression>
#include <QJsonArray>
#include <QProgressBar>
#include <memory>
@ -29,6 +30,7 @@
#include "mainwindow.h"
#include "utils.h" // deprecated
#include "utils_internal.h"
#include "log_line_entry.h"
#include "process/command.h"
#include "process/update_command.h"
#include "process/check_ismas_connectivity_command.h"
@ -193,7 +195,28 @@ Worker::Worker(int customerNr,
, m_dcDownloadJsonFiles(new Command(
QString("/opt/app/tools/atbupdate/ATBDownloadDCJsonFiles --set-ppid %1").arg(QCoreApplication::applicationPid())))
//, m_withoutIsmasDirectPort(true) /* useful for testing */ {
, m_withoutIsmasDirectPort(false) /* useful for testing */ {
, m_withoutIsmasDirectPort(false) /* useful for testing */
, m_updateLog("/opt/app/tools/atbupdate/update.log") // TODO: in ini-file eintragen
, m_updateLogBackup("/opt/app/tools/atbupdate/updateBackup.log") { // TODO: in ini-file eintragen
if (!m_updateLog.exists()) {
qCritical() << "ERROR" << m_updateLog.fileName() << "does not exist";
} else {
if (!m_updateLog.open(QIODevice::ReadWrite | QIODevice::Unbuffered)) {
qCritical() << "ERROR can not open" << m_updateLog.fileName();
} else {
m_updateLog.resize(0);
m_fileWatcher.addPath(QFileInfo(m_updateLog).absoluteFilePath());
connect(&m_fileWatcher, SIGNAL(fileChanged(QString const&)), this, SLOT(onFileChanged(QString const&)));
}
}
if (!m_updateLogBackup.open(QIODevice::ReadWrite | QIODevice::Unbuffered)) {
qCritical() << "ERROR can not open" << m_updateLogBackup.fileName();
} else {
m_updateLogBackup.resize(0);
m_updateLogBackup.close();
}
// *** check ISMAS connectivity ***
// NOTE: if the customer repository does not exist, then it does not matter
@ -202,8 +225,8 @@ Worker::Worker(int customerNr,
int next = 1;
m_workList.push_back(
std::make_unique<CheckIsmasConnectivityCommand>(
QString("echo ATBUpdateCheck --ismas-connected")
//QString("/opt/app/tools/atbupdate/ATBUpdateCheck --ismas-connected")
//QString("echo ATBUpdateCheck --ismas-connected")
QString("/opt/app/tools/atbupdate/ATBUpdateCheck --ismas-connected")
, this, ++next));
// *** check if update activated in ISMAS ***
@ -211,8 +234,8 @@ Worker::Worker(int customerNr,
// if the update has been activated via ISMAS.
m_workList.push_back(
std::make_unique<CheckUpdateActivationCommand>(
QString("echo ATBUpdateCheck --update-requested")
//QString("/opt/app/tools/atbupdate/ATBUpdateCheck --update-requested")
// QString("echo ATBUpdateCheck --update-requested")
QString("/opt/app/tools/atbupdate/ATBUpdateCheck --update-requested")
, this, ++next));
// *** check and fetch git-customer repository ***
@ -257,8 +280,8 @@ Worker::Worker(int customerNr,
// send device-controller firmware down to device-controller-hardware
m_workList.push_back(
std::make_unique<UpdateDCCommand>(
// QString("echo ATBUpdateDC")
QString("/opt/app/tools/atbupdate/ATBUpdateDC")
QString("echo ATBUpdateDC")
// QString("/opt/app/tools/atbupdate/ATBUpdateDC")
, this, ++next));
// show/send software-status
@ -301,10 +324,91 @@ Worker::~Worker() {
}
}
#if 0
struct LogLineEntry {
char receiver; // 1 receiver can be: ISMAS
char reason[5]; // 6 SW_UP
char timestamp[19]; // 25 ISO-format: 1900-xx-xxT00:00:00
char eventId; // 26
char event[5]; // 31
/*
Note:
! After U0002 immer ein CMD_SENDVERSION
! Only U0002 and U0003 finish the Update process.
! U0001: Update finished but not activated
! U0002: Update finished and activated
! U0003: Update finished but FAILed.
*/
// #define _ISMAS_DONE "U0001" // 100%, Check: Resultcode: 0
// #define _ISMAS_SET_WAIT_OK "U0002" // empty WAIT-button (""), ResultCode: 0
// #define _ISMAS_NO_UPDATE_NECESSARY "M0100" // empty WAIT-button (""), ResultCode: 0
// #define _ISMAS_FAILURE "U0003" // FAIL
// #define _ISMAS_CONTINUE "U0010" // %-values: Update laeuft, Resultcodes entsprechend laufender Schritt
// #define _ISMAS_RESET_WAIT "ISMAS" // reset WAIT-button to "WAIT"
// #define _ISMAS_TEST_TRIGGER "U0099" // check the WAIT-button
char eventState; // 32
char percent; // 33 percent in progressbar of update-tool
char resultCode; // 34
char step[40]; // 74 step executed
char stepResult[40];// 114 result for step
char version[14]; // 128
};
#endif
void Worker::onFileChanged(QString const& fname) {
QFile f(fname);
//if (m_updateLog.fileName().contains(fname)) {
// if (f.exists() && m_updateLogBackup.exists()) {
if (!m_updateLogBackup.open(QIODevice::ReadWrite | QIODevice::Unbuffered)) {
qCritical() << "ERROR can not open" << m_updateLogBackup.fileName();
return;
}
if (f.open(QIODevice::ReadOnly)) {
QByteArray const &backup = m_updateLogBackup.readAll();
QByteArray const &data = f.readAll();
int const diff = data.size() - backup.size();
if (diff > 0) {
QByteArray const &newLines = data.mid(backup.size());
int const size = newLines.size();
if (size > 0) {
LogLineEntry logLine;
int pos = 0;
while (pos < size) {
QByteArray const &a = newLines.mid(pos, sizeof(logLine));
if (a.size() == sizeof(logLine)) {
memcpy(&logLine, a.data(), sizeof(logLine));
qCritical() << " reason:" << QString(QByteArray(logLine.reason, sizeof(logLine.reason)));
qCritical() << " timestamp:" << QString(QByteArray(logLine.timestamp, sizeof(logLine.timestamp)));
qCritical() << " event:" << QString(QByteArray(logLine.event, sizeof(logLine.event)));
qCritical() << " step:" << QString(QByteArray(logLine.step, sizeof(logLine.step)));
qCritical() << "stepResult:" << QString(QByteArray(logLine.stepResult, sizeof(logLine.stepResult)));
qCritical() << " percent:" << (int)logLine.percent;
displayProgressInMainWindow(logLine.percent);
}
pos += sizeof(logLine);
}
m_updateLogBackup.write(newLines);
}
}
f.close();
}
//}
m_updateLogBackup.close();
// TODO: daten an ISMAS senden
}
void Worker::displayProgressInMainWindow(int progress) {
if (m_mainWindow) {
QApplication::postEvent(m_mainWindow,
new ProgressEvent(this, progress));
QProgressBar *progressBar = m_mainWindow->progressBar();
if (progressBar) {
progressBar->setValue(progress);
}
//QApplication::postEvent(m_mainWindow,
// new ProgressEvent(this, progress));
}
}