Compare commits
No commits in common. "9e147b41642c66542655d163199a471692125bb1" and "81fd7606bdff47c0dd0797a8fb5bb14d38d727bd" have entirely different histories.
9e147b4164
...
81fd7606bd
@ -67,13 +67,11 @@ contains( CONFIG, DesktopLinux ) {
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
message_handler.cpp \
|
||||
process/command.cpp
|
||||
message_handler.cpp
|
||||
|
||||
HEADERS += \
|
||||
message_handler.h \
|
||||
plugins/interfaces.h \
|
||||
process/command.h
|
||||
plugins/interfaces.h
|
||||
|
||||
OTHER_FILES += \
|
||||
ATBDownloadDCJsonFiles.ini
|
||||
|
24
main.cpp
24
main.cpp
@ -7,8 +7,10 @@
|
||||
#include "plugins/interfaces.h"
|
||||
|
||||
|
||||
//#include <unistd.h>
|
||||
//#include <errno.h>
|
||||
#if defined (__unix__) || defined (__linux__)
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#ifdef PTU5
|
||||
#define SERIAL_PORT "ttymxc2"
|
||||
@ -34,23 +36,5 @@ int main(int argc, char **argv) {
|
||||
setDebugLevel(LOG_NOTICE);
|
||||
}
|
||||
|
||||
int r = std::system("ls -l /proc/[0-9]*/fd/* 2>/dev/null | grep /dev/ttymxc2 > /tmp/slave-or-master.txt");
|
||||
// lrwx------ 1 root root 64 Oct 31 14:55 /proc/884/fd/18 -> /dev/ttymxc2
|
||||
|
||||
//std::ifstream ifs("/tmp/slave-or-master.txt");
|
||||
|
||||
//if (ifs.is_open()) {
|
||||
// std::string line;
|
||||
// if (std::getline(ifs, line)) {
|
||||
// if (line.size() > 0) {
|
||||
// std::cout << "SLAVE " << line << std::endl;
|
||||
// }
|
||||
// } else {
|
||||
// }
|
||||
|
||||
// ifs.close();
|
||||
|
||||
//}
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
@ -1,124 +0,0 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QRegularExpression>
|
||||
#include <QDateTime>
|
||||
|
||||
Command::Command(QString const &command, int start_timeout, int finish_timeout)
|
||||
: m_command(command.trimmed())
|
||||
, m_commandResult("")
|
||||
, m_waitForStartTimeout(start_timeout)
|
||||
, m_waitForFinishTimeout(finish_timeout)
|
||||
, m_exitCode(-1) {
|
||||
}
|
||||
|
||||
QString Command::getCommandResult() const {
|
||||
return m_commandResult;
|
||||
}
|
||||
|
||||
void Command::readyReadStandardOutput() {
|
||||
QProcess *p = (QProcess *)sender();
|
||||
m_commandResult += p->readAllStandardOutput();
|
||||
// qCritical() << m_commandResult;
|
||||
}
|
||||
|
||||
void Command::readyReadStandardError() {
|
||||
QProcess *p = (QProcess *)sender();
|
||||
QByteArray buf = p->readAllStandardError();
|
||||
qCritical() << buf;
|
||||
}
|
||||
|
||||
void Command::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
|
||||
QProcess *p = (QProcess *)sender();
|
||||
// read all remaining data sent to the process, just in case
|
||||
QString d = p->readAllStandardOutput();
|
||||
if (!d.isEmpty()) {
|
||||
m_commandResult += d;
|
||||
}
|
||||
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));
|
||||
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardError()));
|
||||
}
|
||||
|
||||
bool Command::execute(QString workingDirectory, QStringList args) {
|
||||
|
||||
if (!QDir::setCurrent(workingDirectory)) {
|
||||
qCritical() << "SET WORKING_DIRECTORY" << workingDirectory
|
||||
<< "FAILED FOR" << m_command;
|
||||
return false;
|
||||
}
|
||||
|
||||
QScopedPointer<QProcess> p(new QProcess(this));
|
||||
p->setWorkingDirectory(workingDirectory);
|
||||
p->setProcessChannelMode(QProcess::MergedChannels);
|
||||
|
||||
connect(&(*p), SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()));
|
||||
connect(&(*p), SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()));
|
||||
|
||||
if (!args.isEmpty()) {
|
||||
qDebug() << "START COMMAND" << m_command << "WITH ARGS" << args
|
||||
<< "IN" << p->workingDirectory();
|
||||
p->start(m_command, args);
|
||||
} else {
|
||||
qDebug() << "START COMMAND" << m_command
|
||||
<< "IN" << p->workingDirectory();
|
||||
p->start(m_command);
|
||||
}
|
||||
|
||||
qint64 const start = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
|
||||
if (p->waitForStarted(m_waitForStartTimeout)) {
|
||||
qDebug() << "PROCESS" << m_command << "STARTED IN" << p->workingDirectory();
|
||||
if (p->state() == QProcess::ProcessState::Running) {
|
||||
qDebug() << "PROCESS" << m_command << "RUNNING IN" << p->workingDirectory();
|
||||
// wait forever for git/opkg-commands to finish
|
||||
int wait = m_waitForFinishTimeout;
|
||||
if (m_command.trimmed().startsWith("git", Qt::CaseInsensitive) ||
|
||||
m_command.trimmed().startsWith("opkg", Qt::CaseInsensitive)) {
|
||||
wait = -1;
|
||||
}
|
||||
bool const no_timeout = p->waitForFinished(wait);
|
||||
if (no_timeout) {
|
||||
qDebug() << "PROCESS" << m_command << "FINISHED IN" << p->workingDirectory();
|
||||
if (p->exitStatus() == QProcess::NormalExit) {
|
||||
if ((m_exitCode = p->exitCode()) == 0) {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qDebug() << "EXECUTED" << m_command
|
||||
<< QString("(runtime %1ms)").arg(end-start)
|
||||
<< "with code" << m_exitCode
|
||||
<< "IN" << p->workingDirectory();
|
||||
return true;
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "EXECUTED" << m_command
|
||||
<< QString("(runtime %1ms)").arg(end-start)
|
||||
<< "with code" << m_exitCode
|
||||
<< "IN" << p->workingDirectory();
|
||||
}
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "PROCESS" << m_command << "CRASHED with code"
|
||||
<< p->exitCode()
|
||||
<< QString("(after %1ms)").arg(end-start)
|
||||
<< "IN" << p->workingDirectory();
|
||||
}
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "PROCESS" << m_command
|
||||
<< "DID NOT FINISH WITH" << wait
|
||||
<< "MS IN" << p->workingDirectory()
|
||||
<< QString("(runtime %1ms)").arg(end-start);
|
||||
}
|
||||
} else {
|
||||
qCritical() << "WRONG PROCESS STATE" << p->state()
|
||||
<< "IN" << p->workingDirectory();
|
||||
}
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "PROCESS" << m_command << "TIMEOUT AT START"
|
||||
<< QString("(runtime %1ms)").arg(end-start)
|
||||
<< "IN" << p->workingDirectory();
|
||||
}
|
||||
return false;
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
#ifndef COMMAND_H_INCLUDED
|
||||
#define COMMAND_H_INCLUDED
|
||||
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QProcess>
|
||||
|
||||
|
||||
class Command : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
QString m_command;
|
||||
QString m_commandResult;
|
||||
int m_waitForStartTimeout;
|
||||
int m_waitForFinishTimeout;
|
||||
int m_exitCode;
|
||||
public:
|
||||
explicit Command(QString const &command,
|
||||
int start_timeout = 100000,
|
||||
int finish_timeout = 100000);
|
||||
|
||||
QString getCommandResult() const;
|
||||
QString command() const { return m_command; }
|
||||
|
||||
bool execute(QString workingDirectory, QStringList args = QStringList());
|
||||
int exitCode() const { return m_exitCode; }
|
||||
|
||||
private slots:
|
||||
void readyReadStandardOutput();
|
||||
void readyReadStandardError();
|
||||
void finished(int exitCode, QProcess::ExitStatus exitStatus);
|
||||
};
|
||||
|
||||
#endif // COMMAND_H_INCLUDED
|
Loading…
x
Reference in New Issue
Block a user