Add handling of update-script

This commit is contained in:
Gerhard Hoffmann 2023-05-26 13:03:38 +02:00
parent 71a6b82d58
commit 35d40e3b3c
2 changed files with 91 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QDebug> #include <QDebug>
#include <QTextStream> #include <QTextStream>
#include <QRegularExpression>
#include "interfaces.h" #include "interfaces.h"
#include "DCPlugin/include/hwapi.h" #include "DCPlugin/include/hwapi.h"
@ -13,6 +14,7 @@
#include <QSharedMemory> #include <QSharedMemory>
#include <QScopedPointer> #include <QScopedPointer>
#include <QProcess> #include <QProcess>
#include <QDir>
#define COLUMN_REQUEST (0) #define COLUMN_REQUEST (0)
#define COLUMN_NAME (1) #define COLUMN_NAME (1)
@ -26,6 +28,7 @@ void ScopedPointerCustomDeleter::cleanup(Update *update) {
} }
Update::Update(QString update_ctrl_file, Update::Update(QString update_ctrl_file,
QString workingDir,
QObject *parent, QObject *parent,
hwinf *hw, hwinf *hw,
char const *serialInterface, char const *serialInterface,
@ -36,9 +39,12 @@ Update::Update(QString update_ctrl_file,
, m_baudrate(baudrate) , m_baudrate(baudrate)
, m_update_ctrl_file(update_ctrl_file) , m_update_ctrl_file(update_ctrl_file)
, m_update_ctrl_file_copy(update_ctrl_file + ".copy") , m_update_ctrl_file_copy(update_ctrl_file + ".copy")
, m_workingDir(workingDir)
, m_init(true) , m_init(true)
, m_delete(hw == nullptr) { , m_delete(hw == nullptr) {
execUpdateScript();
if (!m_update_ctrl_file.exists()) { if (!m_update_ctrl_file.exists()) {
qCritical() << "Update-file" << m_update_ctrl_file.fileName() qCritical() << "Update-file" << m_update_ctrl_file.fileName()
<< "does not exist"; << "does not exist";
@ -64,6 +70,34 @@ Update::Update(QString update_ctrl_file,
Update::~Update() { Update::~Update() {
} }
bool Update::execUpdateScript() {
// path of update-script 'update_psa'
QString update_psa("/opt/app/tools/atbupdate/update_psa --wdir ");
update_psa += m_workingDir;
//QStringList const params(QStringList() << "-c" << update_psa);
QScopedPointer<QProcess> p(new QProcess(this));
p->setProcessChannelMode(QProcess::MergedChannels);
p->start(update_psa);
if (p->waitForStarted(1000)) {
if (p->state() == QProcess::ProcessState::Running) {
if (p->waitForFinished(60000)) {
QString output = p->readAllStandardOutput().toStdString().c_str();
QStringList lst = output.split('\n');
for (int i = 0; i < lst.size(); ++i) {
qDebug() << lst[i];
}
qInfo() << "EXECUTED" << update_psa;
return ((p->exitStatus() == QProcess::NormalExit)
&& (p->exitCode() == 0));
}
}
}
return false;
}
bool Update::updateBinary(char const *fileToSendToDC) { bool Update::updateBinary(char const *fileToSendToDC) {
return m_hw->dc_updateDC(fileToSendToDC, m_baudrate, m_serialInterface); return m_hw->dc_updateDC(fileToSendToDC, m_baudrate, m_serialInterface);
} }

57
update.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef UPDATE_H_INCLUDED
#define UPDATE_H_INCLUDED
#include <QObject>
#include <QString>
#include <QFile>
#include "interfaces.h"
#include "DCPlugin/include/hwapi.h"
#ifdef PTU5
#define SERIAL_PORT "ttymxc2"
#else
#define SERIAL_PORT "ttyUSB0"
#endif
class Update;
struct ScopedPointerCustomDeleter {
static void cleanup(Update *pointer);
};
// TODO: check hardware compatibility
// TODO: opkg commandos
class Update : public QObject {
Q_OBJECT
QScopedPointer<hwinf> m_hw;
char const *m_serialInterface;
char const *m_baudrate;
QFile m_update_ctrl_file;
QFile m_update_ctrl_file_copy;
QString m_workingDir;
bool m_init;
bool updateBinary(char const *fileToSendToDC);
bool updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC);
bool finishUpdate(bool finish);
QStringList getOpenLines();
QStringList split(QString line);
static constexpr QChar SEPARATOR = QChar(',');
bool execUpdateScript();
public:
explicit Update(QString update_ctrl_file,
QString workingDir = ".",
QObject *parent = nullptr,
hwinf *hw = nullptr,
char const *serialInterface = SERIAL_PORT,
char const *baudrate = "115200");
virtual ~Update() override;
bool doUpdate();
bool const m_delete;
};
#endif // UPDATE_H_INCLUDED