From 892fae92ad8f738fbf6b1a426b2e981b1f2c8d3c Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 19 May 2023 15:33:40 +0200 Subject: [PATCH] Using Utils-class and QRunnable.w --- main.cpp | 90 +++++++++++++------------------------------------------- 1 file changed, 21 insertions(+), 69 deletions(-) diff --git a/main.cpp b/main.cpp index a925bc3..4da5633 100644 --- a/main.cpp +++ b/main.cpp @@ -5,14 +5,18 @@ #include #include "message_handler.h" -#include "interfaces.h" +#include "interfaces.h" #include "DCPlugin/include/hwapi.h" #include #include #include #include +#include +#include + +#include "utils.h" #ifdef PTU5 #define SERIAL_PORT "ttymxc2" @@ -20,35 +24,14 @@ #define SERIAL_PORT "ttyUSB0" #endif -static void updateBinary(std::unique_ptr hw, // update d2dc*.bin - char const *fileToSendToDC, - char const *baudrate, - char const *serialInterface) { - for (int i=0; i < 1;++i) { - qDebug() << "file to send to DC ..." << fileToSendToDC; - qDebug() << "baudrate ............." << baudrate; - qDebug() << "serial interface ....." << serialInterface; - hw->dc_updateDC(fileToSendToDC, baudrate, serialInterface); - std::this_thread::sleep_for(std::chrono::milliseconds(3000)); +class Work : public QRunnable { + Utils m_utils; +public: + explicit Work(QString update_ctrl_file) : m_utils(update_ctrl_file) {} + void run() { + m_utils.doUpdate(); } - QCoreApplication::quit(); -} - -static void updatePrinterConf(std::unique_ptr hw, // update printer-file - QVector nrOfTemplate, - QVector fileToSendToDC, - QString br, QString serial) { - // for (int i = 0; i < 1; ++i) { - if (nrOfTemplate.size() == 1 && fileToSendToDC.size() == 1) { - hw->dc_updatePrinterTemplate(hwapi::FileTypeJson::PRINTER, - nrOfTemplate, fileToSendToDC, - br, serial); - - // hw->dc_printTemplate(hwapi::FileTypeJson::PRINTER, - // nrOfTemplate, br, serial); - } - QCoreApplication::quit(); -} +}; // argv[1]: file to send to dc int main(int argc, char *argv[]) { @@ -60,46 +43,15 @@ int main(int argc, char *argv[]) { //setDebugLevel(QtMsgType::QtDebugMsg); } - if (argc != 2) { - qCritical() << "Usage: " << argv[0] << ""; - return -1; + QString update_ctrl_file = "/opt/app/tools/atbupdate/update_log.csv"; + if (argc == 2) { + update_ctrl_file = argv[1]; } + qCritical() << "Using" << update_ctrl_file << "as update logfile"; - std::unique_ptr hw(new hwapi()); - QFileInfo fileInfo(argv[1]); - QString fname(fileInfo.fileName()); - int ret = 0; - - if (fname.startsWith("dc") && fname.endsWith(".bin")) { - std::thread t(updateBinary, std::move(hw), - fname.toStdString().c_str(), "115200", SERIAL_PORT); - ret = a.exec(); - t.join(); - } else { - if (fname.startsWith("DC2C_print") && fname.endsWith(".json")) { - qDebug() << "downloading" << fname << "to dc..."; - - QVector fnames; - QVector templateIdx; - - int const nrOfTemplate = fname.midRef(10, 2).toInt(); - if (nrOfTemplate <= 0 && nrOfTemplate > 32) { - qCritical() << "wrong template number" << nrOfTemplate; - return -1; - } - - fnames.append(fname); - templateIdx.append(nrOfTemplate); - - std::thread t(updatePrinterConf, std::move(hw), - templateIdx, - fnames, - "115200", SERIAL_PORT); - - ret = a.exec(); - t.join(); - } - } - - return ret; + Work work(update_ctrl_file); + work.setAutoDelete(false); + QThreadPool *threadPool = QThreadPool::globalInstance(); + threadPool->start(&work); + return threadPool->waitForDone(); }