From 6d43cf4c9fb9b392b43df3a6e1637d3bebf9fd7f Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Mon, 7 Aug 2023 13:56:51 +0200 Subject: [PATCH] Send custom events to the progress bar according to the state of the update process. Changed the handling of messages for the text edit. --- worker.cpp | 244 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 169 insertions(+), 75 deletions(-) diff --git a/worker.cpp b/worker.cpp index 53a409f..f307e0d 100644 --- a/worker.cpp +++ b/worker.cpp @@ -149,8 +149,14 @@ void Worker::privateUpdate() { m_returnCode = -1; QDir customerRepository(m_customerRepository); if (!customerRepository.exists()) { + emit appendText("\nInitializing customer environment ..."); + startProgressLoop(); if (m_gc.gitCloneAndCheckoutBranch()) { - emit appendText("\nInitializing customer environment", UPDATE_STEP_DONE); + stopProgressLoop(); + emit replaceLast("Initializing customer environment", UPDATE_STEP_DONE); + + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); m_updateStatus = UpdateStatus(UPDATE_STATUS::UPDATE_PROCESS_SUCCESS, QString("CLONED AND CHECKED OUT: ") + m_customerRepository); @@ -160,112 +166,110 @@ void Worker::privateUpdate() { m_ismasClient.cloneAndCheckoutCustomerRepository( m_updateStatus.m_statusDescription)); + setProgress(progress + 10); + IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.updateOfPSASucceeded("")); - emit appendText(QString(""), UPDATE_STEP_SUCCESS); - setProgress(100); m_ismasClient.setProgressInPercent(100); IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.updateOfPSAActivated()); m_returnCode = 0; + } else { + stopProgressLoop(); + + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); + + emit replaceLast("Initializing customer environment", UPDATE_STEP_FAIL); + + m_updateStatus = UpdateStatus(UPDATE_STATUS::UPDATE_PROCESS_FAILURE, + QString("CLONE OR CHECKOUT FAILED: ") + m_customerRepository); + IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, + QString("#M=APISM#C=CMD_EVENT#J=") + + m_ismasClient.errorGitClone(100, m_updateStatus.m_statusDescription)); + + m_returnCode = -3; } } else { - qCritical() << "CHECKOUT BRANCH..."; - // checkout branch + qInfo() << "CHECKOUT BRANCH..."; + emit appendText("\nInitializing customer environment ..."); + startProgressLoop(); if (m_gc.gitCheckoutBranch()) { - int progress = 10; - m_ismasClient.setProgressInPercent(progress); + stopProgressLoop(); + emit replaceLast("Initializing customer environment ...", UPDATE_STEP_DONE); + setProgress(100); + + m_ismasClient.setProgressInPercent(10); m_updateStatus = UpdateStatus(UPDATE_STATUS::GIT_CHECKOUT_BRANCH, QString("CHECKED OUT BRANCH: ") + m_gc.branchName()); IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.checkoutBranch( m_updateStatus.m_statusDescription, "")); - setProgress(progress); qCritical() << "CHECKED OUT BRANCH"; if (backendConnected()) { qCritical() << "BACKEND CONNECTED"; - progress = 20; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(20); if (updateTriggerSet()) { qCritical() << "UPDATE TRIGGER SET"; - progress = 30; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(30); if (customerEnvironment()) { qCritical() << "CUSTOMER ENVIRONMENT"; - progress = 40; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(40); if (filesToUpdate()) { qCritical() << "FILES TO UPDATE"; - progress = 50; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); - if (updateFiles(progress)) { qCritical() << "UPDATE FILES"; - progress = 60; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(50); + if (updateFiles(50)) { qCritical() << "UPDATE FILES"; + m_ismasClient.setProgressInPercent(60); if (syncCustomerRepositoryAndFS()) { qCritical() << "SYNC REPOSITORY"; - progress = 70; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(70); if (sendIsmasLastVersionNotification()) { qCritical() << "SEND LAST NOTIFICATION"; - progress = 80; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(80); sentIsmasLastVersionNotification = true; if (saveLogFile()) { qCritical() << "SAVE LOG FILE"; - progress = 90; - setProgress(progress); - m_ismasClient.setProgressInPercent(progress); - emit appendText(QString("Update process "), UPDATE_STEP_SUCCESS); + m_ismasClient.setProgressInPercent(90); IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.updateOfPSASucceeded("")); // mark update as activated -> this resets the WAIT button - progress = 100; - setProgress(progress); - - m_ismasClient.setProgressInPercent(progress); + m_ismasClient.setProgressInPercent(95); IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.updateOfPSAActivated()); + m_returnCode = 0; } else { - m_returnCode = -9; + m_returnCode = -12; } } else { - m_returnCode = -8; + m_returnCode = -11; } } else { - m_returnCode = -7; + m_returnCode = -10; } } else { - m_returnCode = -6; + m_returnCode = -9; } } else { - m_returnCode = -5; + m_returnCode = -8; } } else { - m_returnCode = -4; + m_returnCode = -7; } } else { - m_returnCode = -3; + m_returnCode = -6; } } else { - m_returnCode = -2; + m_returnCode = -5; } } else { - m_returnCode = -1; + m_returnCode = -4; } } - setProgress(100); m_ismasClient.setProgressInPercent(100); if (!sentIsmasLastVersionNotification) { @@ -274,9 +278,13 @@ void Worker::privateUpdate() { } if (m_returnCode != 0) { - emit appendText(QString("Update process "), UPDATE_STEP_FAIL); + stopProgressLoop(); + emit appendText(QString("UPDATE "), UPDATE_STEP_FAIL); + } else { + emit appendText(QString("UPDATE "), UPDATE_STEP_SUCCESS); } + setProgress(100); m_updateProcessRunning = false; emit enableExit(); emit restartExitTimer(); @@ -285,26 +293,39 @@ void Worker::privateUpdate() { bool Worker::backendConnected() { static int repeat = 0; + if (repeat == 0) { + emit appendText("\nConnecting backend ..."); + } + if (repeat < 3) { - qCritical() << "In backendConnected() -> #M=APISM#C=REQ_SELF#J={}"; + qInfo() << repeat << "In backendConnected() -> #M=APISM#C=REQ_SELF#J={}"; + startProgressLoop(); std::optional result = IsmasClient::sendRequestReceiveResponse( IsmasClient::APISM::DIRECT_PORT, "#M=APISM#C=REQ_SELF#J={}"); if (result) { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); + QString msg = result.value(); - qCritical() << "In backendConnected() -> APISM response" << msg; + qInfo() << "In backendConnected() -> APISM response" << msg; QJsonParseError parseError; QJsonDocument document(QJsonDocument::fromJson(msg.toUtf8(), &parseError)); if (parseError.error != QJsonParseError::NoError) { qCritical() << "(2) INVALID JSON MSG: PARSING FAILED (msg=" << msg << "):" << parseError.error << parseError.errorString(); + emit replaceLast("Connecting backend ...", UPDATE_STEP_FAIL); return false; } if (!document.isObject()) { qCritical() << "FILE IS NOT A JSON OBJECT!"; + emit replaceLast("Connecting backend ...", UPDATE_STEP_FAIL); return false; } + setProgress(progress + 10); + QJsonObject obj = document.object(); QStringList keys = obj.keys(); for (QString const& key : keys ) { @@ -314,14 +335,13 @@ bool Worker::backendConnected() { obj = v.toObject(); bool ismas = obj.value("ISMAS").toBool(); QString status = obj.value("Broker").toString(); - - qCritical() << "In backendConnected() STATUS" << status; - + qInfo() << "In backendConnected() STATUS" << status; if (ismas) { if (status == "Connected") { // do not send, as this would result in a corrupted wait button // but update the user-interface - emit appendText("\nBackend connected", UPDATE_STEP_OK); + setProgress(100); + emit replaceLast("Connecting backend ...", UPDATE_STEP_OK); return true; } } @@ -330,14 +350,20 @@ bool Worker::backendConnected() { ++repeat; return backendConnected(); } - emit appendText("\nBackend connected", UPDATE_STEP_FAIL); - emit showErrorMessage("Error", "Backend not available"); } } } + } else { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); } } - qCritical() << "In backendConnected() ERROR"; + + setProgress(100); + + emit replaceLast("Connecting backend", UPDATE_STEP_FAIL); + emit showErrorMessage("Error", "Backend not available"); m_updateStatus = UpdateStatus(UPDATE_STATUS::BACKEND_NOT_CONNECTED, QString("NO BACKEND CONNECTION")); @@ -355,11 +381,17 @@ bool Worker::updateTriggerSet() { // PORT STATE SERVICE // 8883/tcp open secure-mqtt + emit appendText("\nUpdate trigger set ..."); QString triggerValue(""); + startProgressLoop(); if (std::optional result = IsmasClient::sendRequestReceiveResponse( IsmasClient::APISM::DIRECT_PORT, "#M=APISM#C=REQ_ISMASPARAMETER#J={}")) { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); + QString msg = result.value(); QJsonParseError parseError; QJsonDocument document(QJsonDocument::fromJson(msg.toUtf8(), &parseError)); @@ -368,15 +400,19 @@ bool Worker::updateTriggerSet() { << parseError.error << parseError.errorString(); emit showErrorMessage("check update trigger", QString("invalid json ") + msg.mid(0, 20)); + emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL); return false; } if (!document.isObject()) { qCritical() << "FILE IS NOT A JSON OBJECT!"; emit showErrorMessage("check update trigger", QString("not a json object") + msg); + emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL); return false; } + setProgress(progress + 1); + QJsonObject obj = document.object(); // sanity check: cust_nr and machine_nr of PSA correct ? if (obj.contains("Dev_ID")) { @@ -398,9 +434,11 @@ bool Worker::updateTriggerSet() { QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.sanityCheckFailed(IsmasClient::RESULT_CODE::INSTALL_ERROR, m_updateStatus.m_statusDescription)); + emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL); return false; } if (machineNr != m_machineNr) { + setProgress(100); m_updateStatus = UpdateStatus(UPDATE_STATUS::ISMAS_WAIT_STATE_CHECK_FAILURE, QString("MACHINE-NR (%1) != LOCAL MACHINE-NR (%2)") .arg(machineNr).arg(m_machineNr)); @@ -410,11 +448,15 @@ bool Worker::updateTriggerSet() { QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.sanityCheckFailed(IsmasClient::RESULT_CODE::INSTALL_ERROR, m_updateStatus.m_statusDescription)); + emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL); return false; } } } } + + setProgress(progress + 1); + if (obj.contains("Fileupload")) { QJsonValue v = obj.value("Fileupload"); if (v.isObject()) { @@ -424,8 +466,7 @@ bool Worker::updateTriggerSet() { if (v.isString()) { triggerValue = v.toString(); if (triggerValue == "WAIT") { - emit appendText("\nUpdate trigger set", UPDATE_STEP_OK); - + setProgress(100); m_updateStatus = UpdateStatus(UPDATE_STATUS::UPDATE_TRIGGER_SET, QString("UPDATE TRIGGER SET. CONTINUE. ")); @@ -433,6 +474,7 @@ bool Worker::updateTriggerSet() { QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.updateTriggerSet(m_updateStatus.m_statusDescription, "")); + emit replaceLast("Update trigger set ...", UPDATE_STEP_OK); return true; } else { emit showErrorMessage("check update trigger", @@ -448,72 +490,111 @@ bool Worker::updateTriggerSet() { } } } else { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); emit showErrorMessage("check update trigger", "no ISMAS response"); } + setProgress(100); m_updateStatus = UpdateStatus(UPDATE_STATUS::UPDATE_TRIGGER_NOT_SET_OR_WRONG, QString("UPDATE-TRIGGER-NOT-SET-OR-WRONG: VALUE=(") + triggerValue + ")"); IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.errorUpdateTrigger(m_updateStatus.m_statusDescription, "")); + + emit replaceLast("Update trigger set ...", UPDATE_STEP_OK); return false; } bool Worker::customerEnvironment() { + emit appendText("\nPrepare customer environment ..."); if (QDir(m_customerRepository).exists()) { + startProgressLoop(); if (m_gc.gitCheckoutBranch()) { - emit appendText("\nPrepare customer environment", UPDATE_STEP_DONE); + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); + m_updateStatus = UpdateStatus(UPDATE_STATUS::GIT_CHECKOUT_BRANCH, QString("CHECKED-OUT BRANCH ") + m_gc.branchName()); IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.checkoutBranch(m_updateStatus.m_statusDescription, "")); + + setProgress(100); + emit replaceLast("Prepare customer environment ...", UPDATE_STEP_DONE); return true; } else { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); emit showErrorMessage("cust-env", QString("Checkout ") + m_customerRepository + " failed"); } } else { emit showErrorMessage("cust-env", m_customerRepository + " does not exist"); } + + setProgress(100); + emit replaceLast("Prepare customer environment ...", UPDATE_STEP_FAIL); return false; } bool Worker::filesToUpdate() { + emit appendText("\nFetch changes files ..."); + startProgressLoop(); if (std::optional changes = m_gc.gitFetch()) { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress); + m_updateStatus = UpdateStatus(UPDATE_STATUS::GIT_FETCH_UPDATES, QString("FETCHING OF ") + m_customerRepositoryPath + QString(" INTO ") + m_customerRepository); + setProgress(progress + 10); if (std::optional changedFileNames = m_gc.gitDiff(changes.value())) { - m_filesToUpdate = changedFileNames.value(); - int const size = m_filesToUpdate.size(); - if (size > 1) { - emit appendText(QString("\nFound %1 files to update ").arg(size), UPDATE_STEP_DONE); - } else { - emit appendText(QString("\nFound 1 file to update "), UPDATE_STEP_DONE); - } + setProgress(progress + 20); if (m_gc.gitPull()) { - emit appendText(QString("\nFetch changes files "), UPDATE_STEP_DONE); + emit replaceLast(QString("Fetch changes files ..."), UPDATE_STEP_DONE); + m_filesToUpdate = changedFileNames.value(); + int const size = m_filesToUpdate.size(); + if (size > 1) { + emit appendText(QString("Found %1 files to update ").arg(size), UPDATE_STEP_DONE); + } else { + emit appendText("Found 1 file to update ", UPDATE_STEP_DONE); + } return true; } emit showErrorMessage("files to update", "pulling files failed"); } else { emit showErrorMessage("files to update", "no files to update (checked-in any files?)"); } + setProgress(progress + 30); } else { + stopProgressLoop(); + int progress = (m_mainWindow->progressValue()/10) + 10; + setProgress(progress + 30); + emit showErrorMessage("files to update", QString("no changes in ") + m_customerRepository + " (checked-in any files?)"); } + + emit replaceLast(QString("Fetch changes files ..."), UPDATE_STEP_FAIL); + setProgress(100); return false; } bool Worker::updateFiles(quint8 percent) { + emit appendText("\n( ) Update opkg pakets ..."); QStringList filesToDownload; m_displayIndex = 0; + + startProgressLoop(); for (int i = 0; i < m_filesToUpdate.size(); ++i) { QString fName = m_filesToUpdate.at(i); if (fName.contains("opkg_commands", Qt::CaseInsensitive)) { @@ -544,8 +625,8 @@ bool Worker::updateFiles(quint8 percent) { f.close(); if (cmdCount > 0) { m_displayIndex = 1; - emit appendText(QString("\n(") + QString("%1").arg(m_displayIndex).rightJustified(2, ' ') + QString(")") - + QString(" Update opkg pakets "), UPDATE_STEP_DONE); + emit replaceLast(QString("(") + QString("%1").arg(m_displayIndex).rightJustified(2, ' ') + QString(")") + + QString(" Update opkg pakets ... "), UPDATE_STEP_DONE); } } } @@ -563,12 +644,19 @@ bool Worker::updateFiles(quint8 percent) { qCritical() << "FILES_TO_WORK_ON" << filesToDownload; } - return m_update->doUpdate(m_displayIndex, filesToDownload); + bool const ret = m_update->doUpdate(m_displayIndex, filesToDownload); + stopProgressLoop(); + setProgress(100); + return ret; } bool Worker::syncCustomerRepositoryAndFS() { + setProgress(0); + emit appendText("\nSync customer environment with filesystem ..."); if (QDir(m_customerRepository).exists()) { if (QDir::setCurrent(m_customerRepository)) { + int progress = 10; + setProgress(progress); QString const params("-vv " "--recursive " "--progress " @@ -585,6 +673,8 @@ bool Worker::syncCustomerRepositoryAndFS() { QString cmd; bool error = false; foreach (cmd, cmds) { + progress += 5; + setProgress(progress); if (!error) { Command c("bash"); qInfo() << "EXECUTING CMD..." << cmd; @@ -601,13 +691,17 @@ bool Worker::syncCustomerRepositoryAndFS() { } } } + progress += 5; + setProgress(progress); if (!error) { - emit appendText(QString("\nSync customer environment with filesystem "), - UPDATE_STEP_DONE); + setProgress(100); + emit replaceLast(QString("Sync customer environment with filesystem ..."), UPDATE_STEP_DONE); return true; } } } + setProgress(100); + emit replaceLast(QString("Sync customer environment with filesystem ..."), UPDATE_STEP_FAIL); return false; } @@ -615,7 +709,7 @@ bool Worker::sendIsmasLastVersionNotification() { IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, QString("#M=APISM#C=CMD_SENDVERSION#J=") + m_ismasClient.updateOfPSASendVersion(getPSAInstalled())); - emit appendText(QString("\nSend last version info "), UPDATE_STEP_DONE); + emit appendText(QString("Send last version info "), UPDATE_STEP_DONE); return true; }