From 3a259f8a221254c167597075886dbd47e4b272e8 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 7 Feb 2025 13:32:15 +0100 Subject: [PATCH] First version of ATBUpdateCheck --- Check/ismas_client.cpp | 44 +++++++++----- Check/ismas_client.h | 2 +- Check/main.cpp | 114 +++++++++++++++++++++++++++++++++--- UpdatePTUDevCtrl/worker.cpp | 3 +- 4 files changed, 138 insertions(+), 25 deletions(-) diff --git a/Check/ismas_client.cpp b/Check/ismas_client.cpp index ae34314..1cd4317 100644 --- a/Check/ismas_client.cpp +++ b/Check/ismas_client.cpp @@ -112,19 +112,23 @@ void IsmasClient::printErrorMessage(int port, } std::optional -IsmasClient::sendRequestReceiveResponse(int port, QString const &request) { +IsmasClient::sendRequestReceiveResponse(int port, QString const &request, bool verbose) { - qInfo() << "REQUEST" << request; + if (verbose) { + qInfo() << "REQUEST" << request; + } int sockfd; int r; errno = 0; // socket create and verification if ((sockfd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) { - qCritical().noquote() - << "\n" - << "SEND-REQUEST-RECEIVE-RESPONSE ..." << "\n" - << "SOCKET CREATION FAILED (" << strerror(errno) << ")"; + if (verbose) { + qCritical().noquote() + << "\n" + << "SEND-REQUEST-RECEIVE-RESPONSE ..." << "\n" + << "SOCKET CREATION FAILED (" << strerror(errno) << ")"; + } return std::nullopt; } @@ -137,10 +141,12 @@ IsmasClient::sendRequestReceiveResponse(int port, QString const &request) { servAddr.sin_port = htons(port); // connect the client socket to server socket if ((r = ::connect(sockfd, (struct sockaddr *)(&servAddr), sizeof(servAddr))) != 0) { - qCritical().noquote() - << "\n" - << "SEND-REQUEST-RECEIVE-RESPONSE ..." << "\n" - << "CONNECTION WITH SERVER FAILED (" << strerror(r) << ")"; + if (verbose) { + qCritical().noquote() + << "\n" + << "SEND-REQUEST-RECEIVE-RESPONSE ..." << "\n" + << "CONNECTION WITH SERVER FAILED (" << strerror(r) << ")"; + } ::close(sockfd); return std::nullopt; } @@ -155,7 +161,9 @@ IsmasClient::sendRequestReceiveResponse(int port, QString const &request) { inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, sizeof(clientIP)); unsigned int clientPort = ntohs(clientAddr.sin_port); - printDebugMessage(port, clientIP, clientPort, QString("CONNECTED TO SERVER")); + if (verbose) { + printDebugMessage(port, clientIP, clientPort, QString("CONNECTED TO SERVER")); + } struct timeval tv; tv.tv_sec = 10; /* 10 secs timeout for read and write */ @@ -244,7 +252,9 @@ IsmasClient::sendRequestReceiveResponse(int port, QString const &request) { // QString("CANNOT CLOSE WRITING END (") + strerror(errno) + ")"); // } - printInfoMessage(port, clientIP, clientPort, QString("MESSAGE SENT <<<") + buf + ">>>"); + if (verbose) { + printInfoMessage(port, clientIP, clientPort, QString("MESSAGE SENT <<<") + buf + ">>>"); + } loop = 0; bzero(buf, sizeof(buf)); @@ -325,7 +335,9 @@ IsmasClient::sendRequestReceiveResponse(int port, QString const &request) { response = response.mid(0, idx); } else if (response.contains("RECORD")) { // RECORD SAVED or RECORD WRITE ABORTED - printInfoMessage(port, clientIP, clientPort, QString("IGNORED '") + response + "' RESPONSE"); + if (verbose) { + printInfoMessage(port, clientIP, clientPort, QString("IGNORED '") + response + "' RESPONSE"); + } ::close(sockfd); return std::nullopt; } @@ -334,8 +346,10 @@ IsmasClient::sendRequestReceiveResponse(int port, QString const &request) { QJsonDocument document(QJsonDocument::fromJson(response.toUtf8(), &parseError)); if (parseError.error == QJsonParseError::NoError) { if (document.isObject()) { // done: received valid APISM response - printInfoMessage(port, clientIP, clientPort, - QString("VALID APISM RESPONSE .. \n") + response); + if (verbose) { + printInfoMessage(port, clientIP, clientPort, + QString("VALID APISM RESPONSE .. \n") + response); + } ::close(sockfd); return response; } else { diff --git a/Check/ismas_client.h b/Check/ismas_client.h index 4c38c78..145d826 100644 --- a/Check/ismas_client.h +++ b/Check/ismas_client.h @@ -37,7 +37,7 @@ public: static char const *reason[REASON::ENTRIES]; static std::optional - sendRequestReceiveResponse(int port, QString const &request); + sendRequestReceiveResponse(int port, QString const &request, bool verbose=false); private: static void printDebugMessage(int port, QString const &clientIP, int clientPort, diff --git a/Check/main.cpp b/Check/main.cpp index b670f3b..b1d5f4f 100644 --- a/Check/main.cpp +++ b/Check/main.cpp @@ -9,8 +9,18 @@ #include #include -#include "commandline_parser.h" +#include +#include +#include +#include +#include +#include + +#include + #include "message_handler.h" +#include "ismas_client.h" + int main(int argc, char **argv) { QByteArray const value = qgetenv("LC_ALL"); @@ -18,9 +28,6 @@ int main(int argc, char **argv) { qputenv("LC_ALL", "C"); } - - // qputenv("XDG_RUNTIME_DIR", "/var/run/user/0"); - openlog("ATB-UPDATE_CHECK", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER); QCoreApplication a(argc, argv); @@ -36,15 +43,106 @@ int main(int argc, char **argv) { 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); - if (parser.isSet(ismasConnectOption)) { - qCritical() << parser.isSet(ismasConnectOption); - } else + QString connectionStatus{"not connected"}; + QString updateRequestStatus{"not requested"}; + + if (std::optional 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)) { - qCritical() << parser.isSet(updateRequestedOption); + if (connectionStatus == "connected") { + if (std::optional 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; diff --git a/UpdatePTUDevCtrl/worker.cpp b/UpdatePTUDevCtrl/worker.cpp index aad8cfe..5e25e4e 100644 --- a/UpdatePTUDevCtrl/worker.cpp +++ b/UpdatePTUDevCtrl/worker.cpp @@ -197,7 +197,8 @@ Worker::Worker(int customerNr, // check ISMAS connectivity m_workList.push_back( std::make_unique( - QString("echo CheckIsmasConnectivityCommand") + //QString("echo CheckIsmasConnectivityCommand") + QString("/opt/app/tools/atbupdate/ATBUpdateCheck --ismas-connected") , this)); // check if update activated in ISMAS