150 lines
5.6 KiB
C++
150 lines
5.6 KiB
C++
#include <QtGlobal>
|
|
#include <QCoreApplication>
|
|
#include <QByteArray>
|
|
|
|
#include <QProcess>
|
|
#include <QCommandLineParser>
|
|
#include <QStandardPaths>
|
|
#include <QSettings>
|
|
#include <QDir>
|
|
#include <QDebug>
|
|
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonValue>
|
|
#include <QRegularExpression>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
|
|
#include <optional>
|
|
|
|
#include "message_handler.h"
|
|
#include "ismas_client.h"
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
QByteArray const value = qgetenv("LC_ALL");
|
|
if (value.isEmpty() || value != "C") {
|
|
qputenv("LC_ALL", "C");
|
|
}
|
|
|
|
openlog("ATB-UPDATE_CHECK", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);
|
|
|
|
QCoreApplication a(argc, argv);
|
|
QCoreApplication::setApplicationName("ATBUpdateCheck");
|
|
QCoreApplication::setApplicationVersion(APP_VERSION);
|
|
|
|
if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
|
|
atbInstallMessageHandler(nullptr);
|
|
//atbInstallMessageHandler(atbDebugOutput);
|
|
setDebugLevel(LOG_NOTICE);
|
|
}
|
|
|
|
QCommandLineParser parser;
|
|
QCommandLineOption ismasConnectOption("ismas-connected");
|
|
QCommandLineOption updateRequestedOption("update-requested");
|
|
QCommandLineOption verboseOption("verbose");
|
|
parser.addOption(ismasConnectOption);
|
|
parser.addOption(updateRequestedOption);
|
|
parser.addOption(verboseOption);
|
|
parser.process(a);
|
|
|
|
QString connectionStatus{"not connected"};
|
|
QString updateRequestStatus{"not requested"};
|
|
|
|
if (std::optional<QString> result
|
|
= IsmasClient::sendRequestReceiveResponse(
|
|
IsmasClient::APISM::DIRECT_PORT,
|
|
"#M=APISM#C=REQ_SELF#J={}")) {
|
|
QJsonDocument d = QJsonDocument::fromJson(result.value().toUtf8());
|
|
for (QString const &k : d.object().keys()) {
|
|
if (k.contains("CMD_GET_APISMSTATUS_RESPONSE")) {
|
|
QJsonObject o = d.object()[k].toObject();
|
|
QJsonObject::const_iterator it = o.find("Broker");
|
|
if (it != o.constEnd()) {
|
|
// value for "Broker"
|
|
QString const &v = it->toString();
|
|
if (v.contains("connected", Qt::CaseInsensitive)) {
|
|
connectionStatus = "connected";
|
|
} else
|
|
if (v.contains("not connected", Qt::CaseInsensitive)) {
|
|
connectionStatus = "not connected";
|
|
} else
|
|
if (v.contains("disconnected", Qt::CaseInsensitive)) {
|
|
connectionStatus = "not connected";
|
|
} else
|
|
if (v.contains("connecting", Qt::CaseInsensitive)) {
|
|
connectionStatus = "not connected";
|
|
} else
|
|
if (v.contains("disconnecting", Qt::CaseInsensitive)) {
|
|
connectionStatus = "not connected";
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (parser.isSet(updateRequestedOption)) {
|
|
if (connectionStatus == "connected") {
|
|
if (std::optional<QString> result
|
|
= IsmasClient::sendRequestReceiveResponse(
|
|
IsmasClient::APISM::DIRECT_PORT,
|
|
"#M=APISM#C=REQ_ISMASPARAMETER#J={}")) {
|
|
|
|
QJsonDocument d = QJsonDocument::fromJson(result.value().toUtf8());
|
|
for (QString const &k : d.object().keys()) {
|
|
if (k.contains("REQ_ISMASPARAMETER")) {
|
|
QJsonObject o = d.object()[k].toObject();
|
|
|
|
QJsonObject::const_iterator it = o.find("Aknoledge");
|
|
if (it == o.constEnd()) continue;
|
|
|
|
QString const &v = it->toString();
|
|
if (v != "OK") break;
|
|
|
|
for (QString const &k : d.object().keys()) { // request ack
|
|
if (!k.contains("FileUpload", Qt::CaseInsensitive)) continue;
|
|
QJsonObject o = d.object()[k].toObject();
|
|
|
|
QJsonObject::const_iterator it = o.find("TRG");
|
|
if (it == o.constEnd()) break;
|
|
|
|
QString const &v = it->toString();
|
|
if (v == "WAIT") {
|
|
updateRequestStatus = "requested";
|
|
} else
|
|
if (v.isEmpty()) {
|
|
QFile f("/mnt/system_data/cust_nr");
|
|
if (!f.exists()) break;
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) break;
|
|
|
|
QTextStream in(&f);
|
|
in.setCodec("UTF-8");
|
|
if(!in.atEnd()) {
|
|
unsigned custNr = in.readLine().toInt();
|
|
if (custNr > 0) {
|
|
QString custRepo = QString("/opt/app/tools/atbupdate/customer_%1").arg(custNr);
|
|
if (!QDir(custRepo).exists()) {
|
|
updateRequestStatus = "not necessary";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
qCritical() << updateRequestStatus;
|
|
}
|
|
|
|
if (parser.isSet(ismasConnectOption)) {
|
|
qCritical() << connectionStatus;
|
|
}
|
|
|
|
return 0;
|
|
}
|