DCLibraries/src/reporting_thread.cpp

156 lines
5.3 KiB
C++
Raw Normal View History

2023-12-01 14:28:07 +01:00
#include "reporting_thread.h"
#include "shared_mem_buffer.h"
#include "hwapi.h"
2023-12-06 14:53:52 +01:00
#include <QDateTime>
2023-12-06 16:35:17 +01:00
#include <QDebug>
2023-12-06 14:53:52 +01:00
ReportingThread::ReportingThread(hwapi *hw)
2023-12-01 14:28:07 +01:00
: m_hw(hw)
2023-12-02 09:44:59 +01:00
, m_fileToDownload(m_hw->dcDownloadFileName()) {
2023-12-01 14:28:07 +01:00
}
ReportingThread::~ReportingThread() {
}
2023-12-06 14:53:52 +01:00
// download thread running in ca-slave sends reports of download process to
// each component which has connects for the corresponding signals.
2023-12-01 14:28:07 +01:00
void ReportingThread::run() {
2023-12-06 16:35:17 +01:00
qCritical() << QDateTime::currentDateTime() << "START DOWNLOAD THREAD";
2023-12-06 16:35:17 +01:00
static QString report("");
2023-12-03 19:55:23 +01:00
2023-12-06 14:53:52 +01:00
int cnt = 5;
while (!m_hw->dcDownloadGetRunning()) {
2023-12-01 14:28:07 +01:00
if (--cnt > 0) {
report = QString("%1 waiting for download to start %2")
2023-12-06 14:53:52 +01:00
.arg(QDateTime::currentDateTime().toString(Qt::ISODate))
.arg(cnt);
qCritical() << __LINE__ << "STATUS" << report;
emit m_hw->hwapi_reportDCDownloadStatus(report);
2023-12-01 14:28:07 +01:00
QThread::sleep(1);
2023-12-06 14:53:52 +01:00
} else break;
2023-12-01 14:28:07 +01:00
}
2023-12-06 14:53:52 +01:00
#if 0
2023-12-01 14:28:07 +01:00
if (cnt == 0) {
2023-12-06 14:53:52 +01:00
m_hw->dcDownloadResetRequest();
status = QString("%1 reset download request")
.arg(QDateTime::currentDateTime().toString(Qt::ISODate));
qCritical() << __LINE__ << "STATUS" << status;
emit m_hw->hwapi_reportDCDownloadStatus(status);
cnt = 5;
while (!m_hw->dcDownloadRunning()) {
if (--cnt > 0) {
QThread::sleep(1);
} else break;
}
if (cnt == 0) {
status = QString("%1 download request failure")
.arg(QDateTime::currentDateTime().toString(Qt::ISODate));
qCritical() << __LINE__ << "STATUS" << status;
emit m_hw->hwapi_reportDCDownloadFailure(status);
return;
}
2023-12-01 14:28:07 +01:00
}
2023-12-06 16:35:17 +01:00
uint16_t const tnr = 1750;
uint16_t cnr = 0;
while (cnr <= tnr) {
QThread::msleep(100);
QString report("");
if (cnr > 0) {
double percent = ((double)cnr / (double)tnr) * 100.0;
report = QString(": total blocks %1, current block %2 [%3]")
2023-12-06 16:35:17 +01:00
.arg(tnr).arg(cnr).arg(percent, 0, 'f', 2);
} else {
report = QString(": total blocks %1, current block %2 [0]")
2023-12-06 16:35:17 +01:00
.arg(tnr).arg(cnr);
}
status = QDateTime::currentDateTime().toString(Qt::ISODate) + report;
qCritical() << "STATUS" << status;
emit m_hw->hwapi_reportDCDownloadStatus(status);
cnr += 1;
}
if (tnr == cnr) {
m_hw->hwapi_reportDCDownloadSuccess(
QString("SUCCESS DOWNLOADING") + m_fileToDownload);
} else {
m_hw->hwapi_reportDCDownloadFailure(
QString("ERROR DOWNLOADING %1 (total blocks=%2, sent blocks=%3)")
.arg(m_fileToDownload).arg(tnr).arg(cnr));
}
2023-12-01 14:28:07 +01:00
#endif
2023-12-06 14:53:52 +01:00
uint16_t const totalBlocks = m_hw->dcDownloadGetTotalBlockNumber();
2023-12-01 14:28:07 +01:00
qCritical() << QDateTime::currentDateTime() << "TOTAL BLOCKS" << totalBlocks;
2023-12-01 14:28:07 +01:00
if (totalBlocks) {
qint64 const start = QDateTime::currentMSecsSinceEpoch();
double durationMillis = 0;
uint16_t currentBlockNumber = 0;
while (m_hw->dcDownloadGetRunning()) {
currentBlockNumber = m_hw->dcDownloadGetCurrentBlockNumber();
durationMillis += QDateTime::currentMSecsSinceEpoch() - start;
double const timeAveragePerBlock = (currentBlockNumber > 0) ? (durationMillis / currentBlockNumber) : durationMillis;
double const estimatedSecondsLeft = (timeAveragePerBlock * (totalBlocks - currentBlockNumber)) / 1000.0;
double percent = ((double)currentBlockNumber / (double)totalBlocks) * 100.0;
report = QString(": total blocks %1, current block %2 [%3] (est. time left: %4s)")
.arg(totalBlocks)
.arg(currentBlockNumber)
.arg(percent, 0, 'f', 2)
.arg(estimatedSecondsLeft, 0, 'f', 2);
qCritical() << "RT report" << report;
emit m_hw->hwapi_reportDCDownloadStatus(report);
QThread::msleep(100);
2023-12-01 14:28:07 +01:00
}
QThread::msleep(100);
if (totalBlocks == currentBlockNumber) {
m_hw->hwapi_reportDCDownloadSuccess(
QString("SUCCESS DOWNLOADING") + m_fileToDownload);
} else {
m_hw->hwapi_reportDCDownloadFailure(
QString("ERROR DOWNLOADING %1 (total blocks=%2, sent blocks=%3)")
.arg(m_fileToDownload).arg(totalBlocks).arg(currentBlockNumber));
}
2023-12-01 14:28:07 +01:00
}
qCritical() << QDateTime::currentDateTime() << __PRETTY_FUNCTION__
<< QString("line=%1 REPORT THREAD ABOUT TO FINISH").arg(__LINE__);
2023-12-01 14:28:07 +01:00
cnt = 10;
bool running = m_hw->dcDownloadGetRunning();
bool finished = m_hw->dcDownloadGetFinished();
while (--cnt > 0 && (running && !finished)) {
qCritical() << QDateTime::currentDateTime() << __PRETTY_FUNCTION__
<< QString("line=%1 REPORT THREAD: WAIT FOR END OF DOWNLOAD THREAD %2 %3 (%4)")
.arg(__LINE__).arg(running).arg(finished).arg(cnt);
QThread::sleep(1);
running = m_hw->dcDownloadGetRunning();
finished = m_hw->dcDownloadGetFinished();
2023-12-01 14:28:07 +01:00
}
2023-12-06 16:35:17 +01:00
qCritical() << QDateTime::currentDateTime() << __PRETTY_FUNCTION__
<< QString("line=%1 FINISH REPORT THREAD").arg(__LINE__);
2023-12-01 14:28:07 +01:00
}