diff --git a/Check/main.cpp b/Check/main.cpp index 7fa0237..b6241e9 100644 --- a/Check/main.cpp +++ b/Check/main.cpp @@ -55,122 +55,125 @@ int main(int argc, char **argv) { QString connectionStatus{internal::ISMAS_NOT_CONNECTED}; QString updateRequestStatus{internal::UPDATE_NOT_REQUESTED}; - QDebug debug = qCritical(); - int tries = 10; + { + QDebug debug = qCritical(); - while (--tries > 0) { - 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 bit = o.find("Broker"); - QJsonObject::const_iterator ismit = o.find("ISMAS"); - if (bit != o.constEnd() && ismit != o.constEnd()) { - // value for "Broker" - QString const &v = bit->toString(); - bool ismas = ismit->toBool(); - if (ismas && v.contains(internal::BROKER_CONNECTED, Qt::CaseInsensitive)) { - connectionStatus = internal::ISMAS_CONNECTED; - break; - } else - if (v.contains(internal::BROKER_NOT_CONNECTED, Qt::CaseInsensitive)) { - connectionStatus = internal::ISMAS_NOT_CONNECTED; - } else - if (v.contains(internal::BROKER_DISCONNECTED, Qt::CaseInsensitive)) { - connectionStatus = internal::ISMAS_NOT_CONNECTED; - } else - if (v.contains(internal::BROKER_CONNECTION_IN_PROGRESS, Qt::CaseInsensitive)) { - connectionStatus = internal::ISMAS_NOT_CONNECTED; - } else - if (v.contains(internal::BROKER_DISCONNECTING, Qt::CaseInsensitive)) { - connectionStatus = internal::ISMAS_NOT_CONNECTED; - } - break; - } - } - } - } + int tries = 10; - if (connectionStatus == internal::ISMAS_CONNECTED) { - break; - } - - QThread::sleep(5); - } - - if (connectionStatus != internal::ISMAS_CONNECTED) { - if (internal::customerRepoExists() == false) { - debug.noquote() << internal::NO_CUSTOMER_REPOSITORY; - } - } - - if (parser.isSet(updateRequestedOption)) { - if (internal::customerRepoExists() == false) { - // if the customer repository does not exists, it does not matter is - // ISMAS is connected or how the setting for the WAIT-button is. - updateRequestStatus = internal::NO_CUSTOMER_REPOSITORY; - } else { - if (connectionStatus == internal::ISMAS_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 &m : d.object().keys()) { // request ack - if (!m.contains("FileUpload", Qt::CaseInsensitive)) continue; - QJsonObject o2 = d.object()[m].toObject(); - - QJsonObject::const_iterator it2 = o2.find("TRG"); - if (it2 == o2.constEnd()) break; - - QString const &v2 = it2->toString(); - if (v2 == "WAIT") { - updateRequestStatus = internal::UPDATE_REQUESTED; - } else { - // the customer-repository does exist, and the ISMAS-trigger is - // *NOT* "WAIT", but from 00:00:00 - 00:03:59 this counts as an - // automatic update - QDateTime const ¤t = QDateTime::currentDateTime(); - if (current.time().hour() < 4) { - updateRequestStatus = internal::UPDATE_NOT_NECESSARY; - } else { - updateRequestStatus = internal::UPDATE_NOT_REQUESTED; - exitCode = -2; - } - } + while (--tries > 0) { + 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 bit = o.find("Broker"); + QJsonObject::const_iterator ismit = o.find("ISMAS"); + if (bit != o.constEnd() && ismit != o.constEnd()) { + // value for "Broker" + QString const &v = bit->toString(); + bool ismas = ismit->toBool(); + if (ismas && v.contains(internal::BROKER_CONNECTED, Qt::CaseInsensitive)) { + connectionStatus = internal::ISMAS_CONNECTED; break; + } else + if (v.contains(internal::BROKER_NOT_CONNECTED, Qt::CaseInsensitive)) { + connectionStatus = internal::ISMAS_NOT_CONNECTED; + } else + if (v.contains(internal::BROKER_DISCONNECTED, Qt::CaseInsensitive)) { + connectionStatus = internal::ISMAS_NOT_CONNECTED; + } else + if (v.contains(internal::BROKER_CONNECTION_IN_PROGRESS, Qt::CaseInsensitive)) { + connectionStatus = internal::ISMAS_NOT_CONNECTED; + } else + if (v.contains(internal::BROKER_DISCONNECTING, Qt::CaseInsensitive)) { + connectionStatus = internal::ISMAS_NOT_CONNECTED; } break; } } } - } else { - // not connected (so its unknown if update has been requested), - // and customer repository exists. Assume 'not requested'. - updateRequestStatus = internal::UPDATE_NOT_REQUESTED; - exitCode = -1; + } + + if (connectionStatus == internal::ISMAS_CONNECTED) { + break; + } + + QThread::sleep(5); + } + + if (connectionStatus != internal::ISMAS_CONNECTED) { + if (internal::customerRepoExists() == false) { + debug.noquote() << internal::NO_CUSTOMER_REPOSITORY; } } - debug.noquote() << updateRequestStatus; - } else - if (parser.isSet(ismasConnectOption)) { - debug.noquote() << connectionStatus; + + if (parser.isSet(updateRequestedOption)) { + if (internal::customerRepoExists() == false) { + // if the customer repository does not exists, it does not matter is + // ISMAS is connected or how the setting for the WAIT-button is. + updateRequestStatus = internal::UPDATE_INITIAL; + } else { + if (connectionStatus == internal::ISMAS_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 &m : d.object().keys()) { // request ack + if (!m.contains("FileUpload", Qt::CaseInsensitive)) continue; + QJsonObject o2 = d.object()[m].toObject(); + + QJsonObject::const_iterator it2 = o2.find("TRG"); + if (it2 == o2.constEnd()) break; + + QString const &v2 = it2->toString(); + if (v2 == "WAIT") { + updateRequestStatus = internal::UPDATE_REQUESTED; + } else { + // the customer-repository does exist, and the ISMAS-trigger is + // *NOT* "WAIT", but from 00:00:00 - 00:03:59 this counts as an + // automatic update + QDateTime const ¤t = QDateTime::currentDateTime(); + if (current.time().hour() < 4) { + updateRequestStatus = internal::UPDATE_NOT_NECESSARY; + } else { + updateRequestStatus = internal::UPDATE_NOT_REQUESTED; + exitCode = -2; + } + } + break; + } + break; + } + } + } + } else { + // not connected (so its unknown if update has been requested), + // and customer repository exists. Assume 'not requested'. + updateRequestStatus = internal::UPDATE_NOT_REQUESTED; + exitCode = -1; + } + } + debug.noquote() << updateRequestStatus; + } else + if (parser.isSet(ismasConnectOption)) { + debug.noquote() << connectionStatus; + } } return exitCode;