Compare commits

..

No commits in common. "6d4c247de7880c588f7ba2b034f92bb1454f2a40" and "18e4811d57672b9a5a9c266d0905328787cc3460" have entirely different histories.

3 changed files with 69 additions and 273 deletions

View File

@ -5,18 +5,14 @@
#include <QFileInfo>
#include "message_handler.h"
#include "interfaces.h"
#include "DCPlugin/include/hwapi.h"
#include <unistd.h>
#include <thread>
#include <memory>
#include <QSharedMemory>
#include <QRunnable>
#include <QThreadPool>
#include "update.h"
#ifdef PTU5
#define SERIAL_PORT "ttymxc2"
@ -24,16 +20,35 @@
#define SERIAL_PORT "ttyUSB0"
#endif
class Work : public QRunnable {
QString m_update_ctrl_file;
public:
explicit Work(QString update_ctrl_file)
: m_update_ctrl_file(update_ctrl_file) {}
void run() {
Update m_update(m_update_ctrl_file);
m_update.doUpdate();
static void updateBinary(std::unique_ptr<hwinf> 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));
}
};
QCoreApplication::quit();
}
static void updatePrinterConf(std::unique_ptr<hwinf> hw, // update printer-file
QVector<int> nrOfTemplate,
QVector<QString> 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[]) {
@ -45,18 +60,46 @@ int main(int argc, char *argv[]) {
//setDebugLevel(QtMsgType::QtDebugMsg);
}
QString update_ctrl_file = "/opt/app/tools/atbupdate/update_log.csv";
if (argc == 2) {
update_ctrl_file = argv[1];
}
qInfo() << "Using" << update_ctrl_file << "as update logfile";
Work work(update_ctrl_file);
work.setAutoDelete(false);
QThreadPool *threadPool = QThreadPool::globalInstance();
threadPool->start(&work);
if (!threadPool->waitForDone()) {
if (argc != 2) {
qCritical() << "Usage: " << argv[0] << "<file to send to dc>";
return -1;
}
return 0;
std::unique_ptr<hwinf> 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<QString> fnames;
QVector<int> 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;
}

201
utils.cpp
View File

@ -1,201 +0,0 @@
#include "utils.h"
#include <QCoreApplication>
#include <QApplication>
#include <QFile>
#include <QTemporaryFile>
#include <QDebug>
#include <QTextStream>
#include "interfaces.h"
#include "DCPlugin/include/hwapi.h"
//#include <unistd.h>
#include <thread>
#include <memory>
#include <QSharedMemory>
#include <QScopedPointer>
#include <QProcess>
#define COLUMN_STATUS (0)
#define COLUMN_NAME (1)
#define COLUMN_DATE_TIME (2)
#define COLUMN_RESULT (3)
Utils::Utils(QString update_ctrl_file,
QObject *parent,
char const *serialInterface,
char const *baudrate)
: QObject(parent)
, m_hw(new hwapi())
, m_serialInterface(serialInterface)
, m_baudrate(baudrate)
, m_update_ctrl_file(update_ctrl_file)
, m_update_ctrl_file_copy(update_ctrl_file + ".copy")
, m_in(&m_update_ctrl_file)
, m_out(&m_update_ctrl_file_copy)
, m_init(true) {
if (!m_update_ctrl_file.exists()) {
qCritical() << "Update-file" << m_update_ctrl_file.fileName()
<< "does not exist";
m_init = false;
}
if (!m_update_ctrl_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCritical() << "can not open " << m_update_ctrl_file.fileName()
<< "for reading";
m_init = false;
}
if (!m_update_ctrl_file_copy.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCritical() << "can not open " << m_update_ctrl_file_copy.fileName()
<< "for writing";
m_init = false;
}
}
Utils::~Utils() {
}
void Utils::updateBinary(char const *fileToSendToDC) {
qDebug() << "file to send to DC ..." << fileToSendToDC;
qDebug() << "baudrate ............." << m_baudrate;
qDebug() << "serial interface ....." << m_serialInterface;
m_hw->dc_updateDC(fileToSendToDC, m_baudrate, m_serialInterface);
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
QCoreApplication::quit();
}
void Utils::updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC) {
QVector<int> printTemplates{ nrOfTemplate };
QVector<QString> filesToSend{ fileToSendToDC };
m_hw->dc_updatePrinterTemplate(hwapi::FileTypeJson::PRINTER,
printTemplates, filesToSend,
QString(m_baudrate),
QString(m_serialInterface));
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
QCoreApplication::quit();
}
QStringList Utils::getOpenLines() {
QStringList openLines;
while (!m_in.atEnd()) {
QString line = m_in.readLine().trimmed();
// QString.split() is defined >= 5.14
if (!line.startsWith("OPEN")) {
m_out << line;
} else {
openLines << line;
}
}
return openLines;
}
bool Utils::doUpdate() {
/*
The file referred to by 'update_data' has the following structure for
each line:
# ======================================================================
# STATUS | NAME | DATE | RESULT
# ======================================================================
# where
#
# STATUS: OPEN or CLOSED
# NAME : If starting with 'opkg' it is an opkg-command to be executed.
# Otherwise its the name of a file which has to be updated.
# DATE : 0000-00-00T00:00:00
# RESULT: SUCCESS or ERROR (possibly with description)
#
*/
if (!m_init) {
return false;
}
QStringList openLines = getOpenLines();
bool res = false;
QList<QString>::const_iterator it;
for (it = openLines.cbegin(); it != openLines.cend(); ++it) {
int start = 0, end;
int column = 0;
QString status, name, datetime, result;
QString line = *it;
while ((end = line.indexOf(QChar(','), start)) != -1) {
QString next = line.mid(start, end).trimmed();
switch (column) {
case COLUMN_STATUS:
status = next;
break;
case COLUMN_NAME:
name = next;
break;
case COLUMN_DATE_TIME:
datetime = next;
break;
case COLUMN_RESULT:
result = next;
break;
}
++column;
start = end + 1;
}
if (!status.contains("OPEN")) {
qCritical() << "Parsing error for" << m_update_ctrl_file.fileName();
return false;
}
if (name.contains("dc2c") && name.endsWith(".bin")) {
updateBinary(name.toStdString().c_str());
res = true;
} else
if (name.contains("DC2C_print") && name.endsWith(".json")) {
int i = name.indexOf("DC2C_print");
int templateIdx = name.mid(i).midRef(10, 2).toInt();
updatePrinterConf(templateIdx, name.toStdString().c_str());
res = true;
} else
if (name.contains("opkg")) {
int i = name.indexOf("opkg ");
QString rest = name.mid(i).trimmed();
QScopedPointer<QProcess> p(new QProcess(this));
p->setProcessChannelMode(QProcess::MergedChannels);
p->start("opkg", QStringList() << rest);
if (p->waitForStarted(1000)) {
if (p->state() == QProcess::ProcessState::Running) {
if (p->waitForFinished(10000)) {
QByteArray output = p->readAllStandardOutput();
qCritical() << output;
res = true;
}
}
}
} else {
// TODO
}
QString resultLine = "CLOSED";
resultLine += ", " + name;
resultLine += ", " + QDateTime::currentDateTime().toString(Qt::ISODate);
resultLine += ", " + (res == true) ? "SUCCESS" : "ERROR";
m_out << resultLine;
} // for (it = openLines.cbegin(); it != openLines.end(); ++it) {
return finishUpdate(openLines.size() > 0);
}
bool Utils::finishUpdate(bool replaceCtrlFile) {
if (replaceCtrlFile) {
if (!m_update_ctrl_file_copy.exists()) {
return false;
}
if (!m_update_ctrl_file.remove()) {
return false;
}
if (!m_update_ctrl_file_copy.rename(m_update_ctrl_file.fileName())) {
return false;
}
}
return true;
}

46
utils.h
View File

@ -1,46 +0,0 @@
#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED
#include <QObject>
#include <QString>
#include <QFile>
#include <memory>
#include "interfaces.h"
#include "DCPlugin/include/hwapi.h"
#ifdef PTU5
#define SERIAL_PORT "ttymxc2"
#else
#define SERIAL_PORT "ttyUSB0"
#endif
class Utils : public QObject {
Q_OBJECT
std::unique_ptr<hwinf> m_hw;
char const *m_serialInterface;
char const *m_baudrate;
QFile m_update_ctrl_file;
QFile m_update_ctrl_file_copy;
QTextStream m_in;
QTextStream m_out;
bool m_init;
void updateBinary(char const *fileToSendToDC);
void updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC);
bool finishUpdate(bool finish);
QStringList getOpenLines();
static constexpr QChar SEPARATOR = QChar(',');
public:
explicit Utils(QString update_ctrl_file,
QObject *parent = nullptr,
char const *serialInterface = SERIAL_PORT,
char const *baudrate = "115200");
virtual ~Utils() override;
bool doUpdate();
};
#endif // UTILS_H_INCLUDED