Added updateDC() and updatePrinterTemplate(). Prepared for live testing.
This commit is contained in:
		
							
								
								
									
										160
									
								
								update.cpp
									
									
									
									
									
								
							
							
						
						
									
										160
									
								
								update.cpp
									
									
									
									
									
								
							@@ -17,12 +17,18 @@
 | 
				
			|||||||
#include <QThread>
 | 
					#include <QThread>
 | 
				
			||||||
#include <QDateTime>
 | 
					#include <QDateTime>
 | 
				
			||||||
#include <QPluginLoader>
 | 
					#include <QPluginLoader>
 | 
				
			||||||
 | 
					#include <QMap>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define COLUMN_REQUEST      (0)
 | 
					#define COLUMN_REQUEST      (0)
 | 
				
			||||||
#define COLUMN_NAME         (1)
 | 
					#define COLUMN_NAME         (1)
 | 
				
			||||||
#define COLUMN_DATE_TIME    (2)
 | 
					#define COLUMN_DATE_TIME    (2)
 | 
				
			||||||
#define COLUMN_RESULT       (3)
 | 
					#define COLUMN_RESULT       (3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const QMap<QString, int> baudrateMap = {
 | 
				
			||||||
 | 
					  {"1200"   ,   0}, {"9600"   ,   1}, {"19200"  ,   2}, {"38400"  ,   3},
 | 
				
			||||||
 | 
					  {"57600"  ,   4}, {"115200" ,   5}
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
hwinf *Update::loadDCPlugin(QDir const &plugInDir, QString const &fname) {
 | 
					hwinf *Update::loadDCPlugin(QDir const &plugInDir, QString const &fname) {
 | 
				
			||||||
    hwinf *hw = nullptr;
 | 
					    hwinf *hw = nullptr;
 | 
				
			||||||
    if (plugInDir.exists()) {
 | 
					    if (plugInDir.exists()) {
 | 
				
			||||||
@@ -78,12 +84,6 @@ Update::Update(hwinf *hw,
 | 
				
			|||||||
    , m_workingDir(workingDir)
 | 
					    , m_workingDir(workingDir)
 | 
				
			||||||
    , m_init(true) {
 | 
					    , m_init(true) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // qCritical() << "workingDir" << m_workingDir;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // m_hw->dc_autoRequest(false);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    execUpdateScript();
 | 
					    execUpdateScript();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!m_update_ctrl_file.exists()) {
 | 
					    if (!m_update_ctrl_file.exists()) {
 | 
				
			||||||
@@ -106,8 +106,6 @@ Update::Update(hwinf *hw,
 | 
				
			|||||||
        m_init = false;
 | 
					        m_init = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    qDebug() << "Opened" << m_update_ctrl_file_copy.fileName();
 | 
					    qDebug() << "Opened" << m_update_ctrl_file_copy.fileName();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    //QApplication::processEvents();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Update::~Update() {
 | 
					Update::~Update() {
 | 
				
			||||||
@@ -373,26 +371,116 @@ bool Update::updateBinary(char const *fileToSendToDC) {
 | 
				
			|||||||
    bool r;
 | 
					    bool r;
 | 
				
			||||||
    if ((r = fn.exists()) == true) {
 | 
					    if ((r = fn.exists()) == true) {
 | 
				
			||||||
        QString const linkTarget = fn.symLinkTarget();
 | 
					        QString const linkTarget = fn.symLinkTarget();
 | 
				
			||||||
        qCritical() << "updating binary (dc): link target" << linkTarget;
 | 
					        qInfo() << "updating binary (dc)" << linkTarget << "...";
 | 
				
			||||||
        // debug
 | 
					        if ((r = updateDC(linkTarget, m_baudrate, m_serialInterface)) == true) {
 | 
				
			||||||
        //r = m_hw->dc_updateDC(linkTarget, m_baudrate, m_serialInterface);
 | 
					            qInfo() << "updating binary (dc)" << linkTarget << "... done";
 | 
				
			||||||
        qCritical() << "updating binary (dc): "
 | 
					        } else {
 | 
				
			||||||
                    << linkTarget << ((r == true) ? "OK" : "ERROR");
 | 
					            qCritical() << "updating binary (dc)" << linkTarget << "... FAILED";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        qCritical() << "symlink" << fileToSendToDC << "does not exist";
 | 
					        qCritical() << "symlink" << fileToSendToDC << "does not exist";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return r;
 | 
					    return r;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool Update::updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC) {
 | 
					bool Update::updateDC(QString bFile, QString br, QString serial) const {
 | 
				
			||||||
 | 
					    if (!baudrateMap.contains(br)) { // sanity check
 | 
				
			||||||
 | 
					        qCritical() << "passed wrong baudrate" << br;
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    m_hw->dc_autoRequest(false);
 | 
				
			||||||
 | 
					    qDebug() << "updating dc: " << bFile << br << serial << "...";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!openSerial(baudrateMap.value(br), br, serial)) {
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!resetDeviceController()) {
 | 
				
			||||||
 | 
					        closeSerial();
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!startBootloader()) {
 | 
				
			||||||
 | 
					        closeSerial();
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!downloadBinaryToDC(bFile)) {
 | 
				
			||||||
 | 
					        stopBootloader();
 | 
				
			||||||
 | 
					        closeSerial();
 | 
				
			||||||
 | 
					        qCritical() << "updating dc: " << bFile << br << serial << "...FAILED";
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    qInfo() << "updating dc: " << bFile << br << serial << "...OK";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    stopBootloader();
 | 
				
			||||||
 | 
					    QThread::sleep(3);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    closeSerial();
 | 
				
			||||||
 | 
					    return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Update::updatePrinterTemplate(enum FileTypeJson type,
 | 
				
			||||||
 | 
					                                   int templateIdx,
 | 
				
			||||||
 | 
					                                   QString fname,
 | 
				
			||||||
 | 
					                                   QString br,
 | 
				
			||||||
 | 
					                                   QString serial) const {
 | 
				
			||||||
 | 
					    // sanity checks
 | 
				
			||||||
 | 
					    if (!baudrateMap.contains(br)) {
 | 
				
			||||||
 | 
					        qCritical() << "passed wrong baudrate" << br;
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (type != FileTypeJson::PRINTER) {
 | 
				
			||||||
 | 
					        qCritical() << "wrong file type" << (uint8_t)type;
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    qDebug() << "updating: " << fname << br << serial << "...";
 | 
				
			||||||
 | 
					    if (!serial.isNull()) {
 | 
				
			||||||
 | 
					        if (!openSerial(baudrateMap.value(br), br, serial)) {
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    int nTry = 50;
 | 
				
			||||||
 | 
					    while (!m_hw->sys_ready4sending()) { // wait max. 5 seconds
 | 
				
			||||||
 | 
					        std::this_thread::sleep_for(std::chrono::milliseconds(100));
 | 
				
			||||||
 | 
					        if (--nTry <= 0) {
 | 
				
			||||||
 | 
					            qCritical() << "sys not ready for sending";
 | 
				
			||||||
 | 
					            if (!serial.isNull()) {
 | 
				
			||||||
 | 
					                closeSerial();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    bool ret = true;
 | 
				
			||||||
 | 
					    QFile file(fname);
 | 
				
			||||||
 | 
					    if (file.exists() && file.open(QIODevice::ReadOnly)) {
 | 
				
			||||||
 | 
					        QByteArray ba = file.readAll();
 | 
				
			||||||
 | 
					        if (ba.size() <= 800) { // max. size is 800 bytes
 | 
				
			||||||
 | 
					            if (m_hw->sys_sendJsonFileToDc((uint8_t)(type),
 | 
				
			||||||
 | 
					                                           templateIdx,
 | 
				
			||||||
 | 
					                                           (uint8_t *)ba.data())) {
 | 
				
			||||||
 | 
					                std::this_thread::sleep_for(std::chrono::seconds(1));
 | 
				
			||||||
 | 
					                qInfo() << "sent file" << fname << "to dc";
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        qCritical() << fname << "!!! does not exist!!!";
 | 
				
			||||||
 | 
					        ret = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!serial.isNull()) {
 | 
				
			||||||
 | 
					        closeSerial();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Update::updatePrinterConf(int templateIdx, QString fileToSendToDC) {
 | 
				
			||||||
    qCritical() << "updating printer template: " << fileToSendToDC;
 | 
					    qCritical() << "updating printer template: " << fileToSendToDC;
 | 
				
			||||||
    return true; // debug
 | 
					    return updatePrinterTemplate(FileTypeJson::PRINTER,
 | 
				
			||||||
    QVector<int> printTemplates{ nrOfTemplate };
 | 
					                                 templateIdx,
 | 
				
			||||||
    QVector<QString> filesToSend{ fileToSendToDC };
 | 
					                                 fileToSendToDC,
 | 
				
			||||||
    //return m_hw->dc_updatePrinterTemplate(hwapi::FileTypeJson::PRINTER,
 | 
					                                 QString(m_baudrate),
 | 
				
			||||||
    //                                      printTemplates, filesToSend,
 | 
					                                 QString(m_serialInterface));
 | 
				
			||||||
    //                                      QString(m_baudrate),
 | 
					 | 
				
			||||||
    //                                      QString(m_serialInterface));
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QStringList Update::getOpenLines() {
 | 
					QStringList Update::getOpenLines() {
 | 
				
			||||||
@@ -451,6 +539,15 @@ bool Update::doUpdate() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QStringList openLines = getOpenLines();
 | 
					    QStringList openLines = getOpenLines();
 | 
				
			||||||
 | 
					    if (openLines.size() == 0) {
 | 
				
			||||||
 | 
					        qCritical() << "No lines to handle in" << m_update_ctrl_file.fileName();
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    qDebug() << "open lines...";
 | 
				
			||||||
 | 
					    for (int i=0; i< openLines.size(); ++i) {
 | 
				
			||||||
 | 
					        qDebug() << "line" << i << ":" << openLines.at(i).trimmed();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QList<QString>::const_iterator it;
 | 
					    QList<QString>::const_iterator it;
 | 
				
			||||||
    for (it = openLines.cbegin(); it != openLines.cend(); ++it) {
 | 
					    for (it = openLines.cbegin(); it != openLines.cend(); ++it) {
 | 
				
			||||||
@@ -474,6 +571,7 @@ bool Update::doUpdate() {
 | 
				
			|||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (name.contains("dc2c") && name.endsWith(".bin")) {
 | 
					        if (name.contains("dc2c") && name.endsWith(".bin")) {
 | 
				
			||||||
 | 
					            qInfo() << "downloading" << name.trimmed() << "to DC";
 | 
				
			||||||
            if ((res = updateBinary(name.toStdString().c_str())) == true) {
 | 
					            if ((res = updateBinary(name.toStdString().c_str())) == true) {
 | 
				
			||||||
                qInfo() << "downloaded binary" << name;
 | 
					                qInfo() << "downloaded binary" << name;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -481,22 +579,28 @@ bool Update::doUpdate() {
 | 
				
			|||||||
        if (name.contains("DC2C_print") && name.endsWith(".json")) {
 | 
					        if (name.contains("DC2C_print") && name.endsWith(".json")) {
 | 
				
			||||||
            int i = name.indexOf("DC2C_print");
 | 
					            int i = name.indexOf("DC2C_print");
 | 
				
			||||||
            int templateIdx = name.mid(i).midRef(10, 2).toInt();
 | 
					            int templateIdx = name.mid(i).midRef(10, 2).toInt();
 | 
				
			||||||
            if ((res = updatePrinterConf(templateIdx, name.toStdString().c_str())) == true) {
 | 
					            if ((templateIdx < 1) || (templateIdx > 32)) {
 | 
				
			||||||
                qInfo() << "downloaded printer template" << name;
 | 
					                qCritical() << "wrong template index";
 | 
				
			||||||
 | 
					                res = false;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if ((res = updatePrinterConf(templateIdx, name)) == true) {
 | 
				
			||||||
 | 
					                    qInfo() << "downloaded printer template" << name;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else
 | 
					        } else
 | 
				
			||||||
        if (name.contains("opkg")) {
 | 
					        if (name.contains("opkg")) {
 | 
				
			||||||
            int i = name.indexOf("opkg ");
 | 
					            qInfo() << "starting" << name.trimmed();
 | 
				
			||||||
            QString rest = name.mid(i+5).trimmed();
 | 
					 | 
				
			||||||
            QScopedPointer<QProcess> p(new QProcess(this));
 | 
					            QScopedPointer<QProcess> p(new QProcess(this));
 | 
				
			||||||
            p->setProcessChannelMode(QProcess::MergedChannels);
 | 
					            p->setProcessChannelMode(QProcess::MergedChannels);
 | 
				
			||||||
            p->start("opkg", QStringList() << rest);
 | 
					            p->start(name.trimmed());
 | 
				
			||||||
            if (p->waitForStarted(1000)) {
 | 
					            if (p->waitForStarted(1000)) {
 | 
				
			||||||
                if (p->state() == QProcess::ProcessState::Running) {
 | 
					                if (p->state() == QProcess::ProcessState::Running) {
 | 
				
			||||||
                    if (p->waitForFinished(10000)) {
 | 
					                    if (p->waitForFinished(100000)) {
 | 
				
			||||||
                        QString output = p->readAllStandardOutput();
 | 
					                        QString output = p->readAllStandardOutput();
 | 
				
			||||||
                        QStringList outputLst = split(output, QChar('\n'));
 | 
					                        QStringList outputLst = split(output, QChar('\n'));
 | 
				
			||||||
                        qDebug() << outputLst;
 | 
					                        for (int line=0; line < outputLst.size(); ++line) {
 | 
				
			||||||
 | 
					                            qDebug() << outputLst[line];
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
                        res = true;
 | 
					                        res = true;
 | 
				
			||||||
                        qInfo() << "EXECUTED" << name;
 | 
					                        qInfo() << "EXECUTED" << name;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user