Try to establish a connection to backend 50x.

This commit is contained in:
Gerhard Hoffmann 2023-08-09 16:17:28 +02:00
parent e523d3cc2c
commit 042e6dfa38

View File

@ -301,14 +301,14 @@ void Worker::privateUpdate() {
}
bool Worker::backendConnected() {
static int repeat = 0;
if (repeat == 0) {
emit appendText("\nConnecting backend ...");
if (m_withoutIsmasDirectPort) { // useful for testing
return true;
}
if (repeat < 3) {
qInfo() << repeat << "In backendConnected() -> #M=APISM#C=REQ_SELF#J={}";
emit appendText("\nConnecting backend ...");
for (int repeat = 0; repeat < 50; ++repeat) {
qInfo() << "REPEAT" << repeat << "In backendConnected() -> #M=APISM#C=REQ_SELF#J={}";
startProgressLoop();
std::optional<QString> result
= IsmasClient::sendRequestReceiveResponse(
@ -334,7 +334,7 @@ bool Worker::backendConnected() {
return false;
}
setProgress(progress + 10);
setProgress(progress + 1);
QJsonObject obj = document.object();
QStringList keys = obj.keys();
@ -356,11 +356,11 @@ bool Worker::backendConnected() {
return true;
}
}
if (status.startsWith("Connecting") || status.startsWith("Re-Connecting")) {
QThread::sleep(1);
++repeat;
return backendConnected();
}
emit showErrorMessage("Check backend connection",
QString ("REPEAT %1 Broker=<").arg(repeat)
+ status + ">, ISMAS=<" + (ismas ? "true>" : "false>"));
QThread::sleep(6);
continue;
}
}
}
@ -386,125 +386,145 @@ bool Worker::backendConnected() {
}
bool Worker::updateTriggerSet() {
// nmap -Pn 62.141.45.68 -p 8883
// Host is up (0.053s latency).
//
// PORT STATE SERVICE
// 8883/tcp open secure-mqtt
if (m_withoutIsmasDirectPort) { // useful for testing
return true;
}
emit appendText("\nUpdate trigger set ...");
QString triggerValue("");
startProgressLoop();
if (std::optional<QString> result
= IsmasClient::sendRequestReceiveResponse(
IsmasClient::APISM::DIRECT_PORT, "#M=APISM#C=REQ_ISMASPARAMETER#J={}")) {
stopProgressLoop();
int progress = (m_mainWindow->progressValue()/10) + 10;
setProgress(progress);
for (int repeat = 0; repeat < 50; ++repeat) {
startProgressLoop();
if (std::optional<QString> result
= IsmasClient::sendRequestReceiveResponse(
IsmasClient::APISM::DIRECT_PORT, "#M=APISM#C=REQ_ISMASPARAMETER#J={}")) {
stopProgressLoop();
int progress = (m_mainWindow->progressValue()/10) + 10;
setProgress(progress);
QString msg = result.value();
QJsonParseError parseError;
QJsonDocument document(QJsonDocument::fromJson(msg.toUtf8(), &parseError));
if (parseError.error != QJsonParseError::NoError) {
qCritical() << "(2) INVALID JSON MSG: PARSING FAILED (msg=" << msg << "):"
<< parseError.error << parseError.errorString();
emit showErrorMessage("check update trigger",
QString("invalid json ") + msg.mid(0, 20));
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
if (!document.isObject()) {
qCritical() << "FILE IS NOT A JSON OBJECT!";
emit showErrorMessage("check update trigger",
QString("not a json object") + msg);
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
QString msg = result.value();
setProgress(progress + 1);
qInfo() << "REPEAT" << repeat << "APISM RESPONSE" << msg;
QJsonObject obj = document.object();
// sanity check: cust_nr and machine_nr of PSA correct ?
if (obj.contains("Dev_ID")) {
QJsonValue v = obj.value("Dev_ID");
if (v.isObject()) {
QJsonObject obj = v.toObject();
if (obj.contains("Custom_ID") && obj.contains("Device_ID")) {
QJsonValue const c = obj.value("Custom_ID");
QJsonValue const m = obj.value("Device_ID");
int customerNr = c.toInt(-1);
int machineNr = m.toInt(-1);
if (customerNr != m_customerNr) {
m_updateStatus = UpdateStatus(UPDATE_STATUS::ISMAS_WAIT_STATE_CHECK_FAILURE,
QString("CUSTOMER-NR (%1) != LOCAL CUSTOMER-NR (%2)")
.arg(customerNr).arg(m_customerNr));
emit showErrorMessage("check update trigger", m_updateStatus.m_statusDescription);
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") +
m_ismasClient.sanityCheckFailed(IsmasClient::RESULT_CODE::INSTALL_ERROR,
m_updateStatus.m_statusDescription));
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
if (machineNr != m_machineNr) {
setProgress(100);
m_updateStatus = UpdateStatus(UPDATE_STATUS::ISMAS_WAIT_STATE_CHECK_FAILURE,
QString("MACHINE-NR (%1) != LOCAL MACHINE-NR (%2)")
.arg(machineNr).arg(m_machineNr));
emit showErrorMessage("check update trigger", m_updateStatus.m_statusDescription);
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") +
m_ismasClient.sanityCheckFailed(IsmasClient::RESULT_CODE::INSTALL_ERROR,
m_updateStatus.m_statusDescription));
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
}
QJsonParseError parseError;
QJsonDocument document(QJsonDocument::fromJson(msg.toUtf8(), &parseError));
if (parseError.error != QJsonParseError::NoError) {
qCritical() << "(2) INVALID JSON MSG: PARSING FAILED (msg=" << msg << "):"
<< parseError.error << parseError.errorString();
emit showErrorMessage("check update trigger",
QString("invalid json ") + msg.mid(0, 20));
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
if (!document.isObject()) {
qCritical() << "FILE IS NOT A JSON OBJECT!";
emit showErrorMessage("check update trigger",
QString("not a json object") + msg);
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
}
setProgress(progress + 1);
setProgress(progress + 1);
if (obj.contains("Fileupload")) {
QJsonValue v = obj.value("Fileupload");
if (v.isObject()) {
obj = v.toObject();
if (obj.contains("TRG")) {
v = obj.value("TRG");
if (v.isString()) {
triggerValue = v.toString();
if (triggerValue == "WAIT") {
setProgress(100);
m_updateStatus = UpdateStatus(UPDATE_STATUS::UPDATE_TRIGGER_SET,
QString("UPDATE TRIGGER SET. CONTINUE. "));
QJsonObject obj = document.object();
// sanity check: cust_nr and machine_nr of PSA correct ?
// note: this check has to be done here, as the cust_nr and the machine_nr
// of the PSA are sent by ISMAS.
if (obj.contains("Dev_ID")) {
QJsonValue v = obj.value("Dev_ID");
if (v.isObject()) {
QJsonObject obj = v.toObject();
if (obj.contains("Custom_ID") && obj.contains("Device_ID")) {
QJsonValue const c = obj.value("Custom_ID");
QJsonValue const m = obj.value("Device_ID");
int customerNr = c.toInt(-1);
int machineNr = m.toInt(-1);
if (customerNr != m_customerNr) {
m_updateStatus = UpdateStatus(UPDATE_STATUS::ISMAS_WAIT_STATE_CHECK_FAILURE,
QString("CUSTOMER-NR (%1) != LOCAL CUSTOMER-NR (%2)")
.arg(customerNr).arg(m_customerNr));
emit showErrorMessage("check update trigger", m_updateStatus.m_statusDescription);
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") +
m_ismasClient.updateTriggerSet(m_updateStatus.m_statusDescription, ""));
m_ismasClient.sanityCheckFailed(IsmasClient::RESULT_CODE::INSTALL_ERROR,
m_updateStatus.m_statusDescription));
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
if (machineNr != m_machineNr) {
setProgress(100);
m_updateStatus = UpdateStatus(UPDATE_STATUS::ISMAS_WAIT_STATE_CHECK_FAILURE,
QString("MACHINE-NR (%1) != LOCAL MACHINE-NR (%2)")
.arg(machineNr).arg(m_machineNr));
emit showErrorMessage("check update trigger", m_updateStatus.m_statusDescription);
emit replaceLast("Update trigger set ...", UPDATE_STEP_OK);
return true;
} else {
emit showErrorMessage("check update trigger",
QString ("TRG key=<") + triggerValue
+ ">\n(reset download button?)");
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") +
m_ismasClient.sanityCheckFailed(IsmasClient::RESULT_CODE::INSTALL_ERROR,
m_updateStatus.m_statusDescription));
emit replaceLast("Update trigger set ...", UPDATE_STEP_FAIL);
return false;
}
}
} else {
emit showErrorMessage("check update trigger", "TRG key not contained");
}
} else {
emit showErrorMessage("check update trigger", "Fileupload not a json-object");
}
setProgress(progress + 1);
if (obj.contains("Fileupload")) {
QJsonValue v = obj.value("Fileupload");
if (v.isObject()) {
obj = v.toObject();
if (obj.contains("TRG")) {
v = obj.value("TRG");
if (v.isString()) {
triggerValue = v.toString();
qInfo() << "REPEAT" << repeat
<< "In updateTriggerSet() TRG value=<"
<< triggerValue << ">";
if (triggerValue == "WAIT") {
setProgress(100);
m_updateStatus = UpdateStatus(UPDATE_STATUS::UPDATE_TRIGGER_SET,
QString("UPDATE TRIGGER SET. CONTINUE. "));
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") +
m_ismasClient.updateTriggerSet(m_updateStatus.m_statusDescription, ""));
emit replaceLast("Update trigger set ...", UPDATE_STEP_OK);
return true;
} else {
emit showErrorMessage("check update trigger",
QString ("TRG value=<") + triggerValue
+ ">\n(reset download button?)");
QThread::sleep(6);
continue;
}
}
} else {
emit showErrorMessage("check update trigger", "TRG key not contained");
}
} else {
emit showErrorMessage("check update trigger", "Fileupload not a json-object");
}
}
if (obj.contains("error")) {
QString value = obj.value("error").toString();
emit showErrorMessage("check update trigger", QString("REPEAT %1 error=<").arg(repeat) + value + ">");
qInfo() << "REPEAT" << repeat << "In updateTriggerSet() error=<"
<< value << ">";
}
} else {
stopProgressLoop();
int progress = (m_mainWindow->progressValue()/10) + 10;
setProgress(progress);
emit showErrorMessage("check update trigger", "no ISMAS response");
QThread::sleep(6);
}
} else {
stopProgressLoop();
int progress = (m_mainWindow->progressValue()/10) + 10;
setProgress(progress);
emit showErrorMessage("check update trigger", "no ISMAS response");
}
setProgress(100);
@ -672,6 +692,10 @@ bool Worker::updateFiles(quint8 percent) {
m_displayIndex = 1;
emit replaceLast(QString("(") + QString("%1").arg(m_displayIndex).rightJustified(2, ' ') + QString(")")
+ QString(" Update opkg pakets ... "), UPDATE_STEP_DONE);
} else {
m_displayIndex = 1;
emit replaceLast(QString("(") + QString("%1").arg(m_displayIndex).rightJustified(2, ' ') + QString(")")
+ QString(" Update opkg pakets ... "), UPDATE_STEP_FAIL);
}
}
}