UpdatePTUDevCtrl/worker.cpp

102 lines
2.8 KiB
C++

#include "worker.h"
#include "update.h"
#include <QCoreApplication>
#include <QApplication>
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
#include <QDir>
#include <QThread>
#include "message_handler.h"
#include "plugins/interfaces.h"
#include "ismas/ismas_client.h"
#include "apism/apism_client.h"
Worker::Worker(hwinf *hw, QString update_ctrl_file, QString workingDir,
bool maintenanceMode, bool testMode, bool executeScriptOnly,
bool dryRun, QObject *parent, char const *serialInterface,
char const *baudrate)
: m_workerThread("workerThread")
, m_apismClient(0, 0, 0, this)
, m_gc("/opt/app/tools/atbupdate/customer_999", "zg1/zone1", this) {
this->moveToThread(&m_workerThread);
//m_apismClient.moveToThread(&m_workerThread);
m_workerThread.start();
int cnt = 0;
while (!m_workerThread.isRunning()) {
if (++cnt > 5) {
qCritical() << "starting worker thread FAILED";
return;
}
QThread::sleep(1);
}
m_update = new Update(hw, update_ctrl_file, workingDir,
maintenanceMode, testMode, executeScriptOnly, dryRun,
parent, serialInterface, baudrate);
connect(&m_apismClient, SIGNAL(ismasResponseAvailable(QJsonObject)), this,
SLOT(onIsmasResponseReceived(QJsonObject)));
connect(this, SIGNAL(executeOpkgCommands()), this,
SLOT(onExecuteOpkgCommands()), Qt::QueuedConnection);
//connect(this, SIGNAL(workNow()), this, SLOT(work()), Qt::QueuedConnection);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(runUpdate()), Qt::QueuedConnection);
m_timer.setSingleShot(true);
m_timer.start(1000);
}
Worker::~Worker() {
int cnt = 0;
m_workerThread.quit();
while (!m_workerThread.isFinished()) {
if (!m_workerThread.wait(1000)) {
if (++cnt > 5) {
qCritical() << "stopping worker thread FAILED";
return;
}
}
}
if (m_update) {
delete m_update;
}
}
void Worker::onExecuteOpkgCommands() {
qCritical() << "ON EXECUTE OPKG COMMANDS";
}
// sollte ParameterResponse heissen
void Worker::onIsmasResponseReceived(QJsonObject ismasResponse) {
QJsonValue v = ismasResponse.value("TRG");
if (v.type() == QJsonValue::String) {
QString s = v.toString();
if (s == "WAIT") {
// updates available
// git anwerfen
qCritical() << "GIT ANWERFEN";
emit m_gc.ismasUpdatesAvailable();
}
}
//m_workerThread.quit();
//QApplication::quit();
}
void Worker::runUpdate() {
QThread::sleep(2);
IsmasClient is;
QString data = is.setUpdatesAvailable();
m_apismClient.sendUpdateInfoToIsmas(data);
m_apismClient.requestAvailableIsmasUpdates();
}