diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-16 15:25:02 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-03-16 15:25:02 +0100 |
| commit | df2be9eb678f12b6107d582d8ebd2ca2b4b7e92b (patch) | |
| tree | 71c49e292e7bd000e2363b40ac6e83cd17a31a1e /src/Xpl2Client.cpp | |
| parent | 50034b142dc851da7118032f7218cb1d25ea98e4 (diff) | |
| download | QtXpl2-df2be9eb678f12b6107d582d8ebd2ca2b4b7e92b.tar.gz QtXpl2-df2be9eb678f12b6107d582d8ebd2ca2b4b7e92b.zip | |
Refactor CN_ response handlers into data-driven dispatch table
Diffstat (limited to 'src/Xpl2Client.cpp')
| -rw-r--r-- | src/Xpl2Client.cpp | 380 |
1 files changed, 119 insertions, 261 deletions
diff --git a/src/Xpl2Client.cpp b/src/Xpl2Client.cpp index 8276577..45963fa 100644 --- a/src/Xpl2Client.cpp +++ b/src/Xpl2Client.cpp @@ -8,6 +8,67 @@ bool Xpl2Client::s_wireDebug = false; +/* ------------------------------------------------------------------ */ +/* Response dispatch table */ +/* ------------------------------------------------------------------ */ + +// clang-format off +const QHash<QByteArray, Xpl2Client::ResponseEntry> +Xpl2Client::s_responseTable = { + /* CN_ JC success shape: controllerId, successFlag */ + { "CN_JETTING_ALL_ON", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jettingAllOnResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JETTING_ON", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jettingOnResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JETTING_OFF", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jettingOffResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JC_ID_LED_ON", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jcIdLedOnResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JC_ID_LED_OFF", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jcIdLedOffResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JC_CALIBRATION", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jcCalibrationResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JC_RESET_FAULT_CODES", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jcResetFaultCodesResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_JC_STATUS_MESSAGING_STOP", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->jcStatusMessagingStopResult (p[0].toInt (), p[1].toInt () == 1); } } }, + { "CN_PH_STATUS_MESSAGING_STOP", { ResponseShape::JcSuccess, 2, + [](auto *s, const auto &p) { emit s->phStatusMessagingStopResult (p[0].toInt (), p[1].toInt () == 1); } } }, + + /* CN_ PH success shape: controllerId, printheadId, successFlag */ + { "CN_PH_JETTING_ON", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phJettingOnResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + { "CN_PH_JETTING_OFF", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phJettingOffResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + { "CN_PH_ID_LED_ON", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phIdLedOnResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + { "CN_PH_ID_LED_OFF", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phIdLedOffResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + { "CN_PH_CALIBRATION", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phCalibrationResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + { "CN_PH_RESET_FAULT_CODES", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phResetFaultCodesResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + { "CN_PH_NOZZLES_DISABLED", { ResponseShape::PhSuccess, 3, + [](auto *s, const auto &p) { emit s->phNozzlesDisabledResult (p[0].toInt (), p[1].toInt (), p[2].toInt () == 1); } } }, + + /* CN_ calibration data: controllerId, phId, 48 floats */ + { "CN_PH_CALIBRATION_DATA", { ResponseShape::CalData, 50, + [](auto *s, const auto &p) { emit s->phCalibrationDataReceived (p[0].toInt (), p[1].toInt (), p.mid (2, 48)); } } }, + { "CN_PH_CALIBRATION_RAW_DATA", { ResponseShape::CalData, 50, + [](auto *s, const auto &p) { emit s->phCalibrationRawDataReceived (p[0].toInt (), p[1].toInt (), p.mid (2, 48)); } } }, + + /* CN_ base frequency: controllerId, phId, baseFreq, activeBaseFreq */ + { "CN_PH_CALIBRATED_BASE_FREQUENCY", { ResponseShape::BaseFreq, 4, + [](auto *s, const auto &p) { emit s->phCalibratedBaseFrequencyReceived (p[0].toInt (), p[1].toInt (), p[2].toDouble (), p[3].toDouble ()); } } }, + + /* CN_ status messaging start: controllerId, level, interval, successFlag */ + { "CN_JC_STATUS_MESSAGING_START", { ResponseShape::StatusStart, 4, + [](auto *s, const auto &p) { emit s->jcStatusMessagingStartResult (p[0].toInt (), p[1].toInt (), p[2].toInt (), p[3].toInt () == 1); } } }, + { "CN_PH_STATUS_MESSAGING_START", { ResponseShape::StatusStart, 4, + [](auto *s, const auto &p) { emit s->phStatusMessagingStartResult (p[0].toInt (), p[1].toInt (), p[2].toInt (), p[3].toInt () == 1); } } }, +}; +// clang-format on + Xpl2Client::Xpl2Client (QObject *parent) : QObject (parent) { for (auto *socket : { &m_commandSocket, &m_imagingSocket, &m_statusSocket }) @@ -284,31 +345,7 @@ Xpl2Client::dispatchCommandMessage (const Xpl2Protocol::ParsedMessage &msg) handleGsJcVersion (msg.params); else if (msg.command == "GS_PH_VERSION") handleGsPhVersion (msg.params); - /* CN_ commands — JC success shape (controllerId, success) */ - else if (msg.command == "CN_JETTING_ALL_ON" || msg.command == "CN_JETTING_ON" - || msg.command == "CN_JETTING_OFF" - || msg.command == "CN_JC_ID_LED_ON" - || msg.command == "CN_JC_ID_LED_OFF" - || msg.command == "CN_JC_CALIBRATION" - || msg.command == "CN_JC_RESET_FAULT_CODES") - handleJcSuccessResponse (msg.command, msg.params); - /* CN_ commands — PH success shape (controllerId, phId, success) */ - else if (msg.command == "CN_PH_JETTING_ON" - || msg.command == "CN_PH_JETTING_OFF" - || msg.command == "CN_PH_ID_LED_ON" - || msg.command == "CN_PH_ID_LED_OFF" - || msg.command == "CN_PH_CALIBRATION" - || msg.command == "CN_PH_RESET_FAULT_CODES" - || msg.command == "CN_PH_NOZZLES_DISABLED") - handlePhSuccessResponse (msg.command, msg.params); - /* CN_ commands — custom shapes */ - else if (msg.command == "CN_PH_CALIBRATION_DATA") - handleCnPhCalibrationData (msg.params); - else if (msg.command == "CN_PH_CALIBRATION_RAW_DATA") - handleCnPhCalibrationRawData (msg.params); - else if (msg.command == "CN_PH_CALIBRATED_BASE_FREQUENCY") - handleCnPhCalibratedBaseFrequency (msg.params); - else + else if (!dispatchResponse (msg.command, msg.params, &m_commandSocket)) qWarning ("%s Unknown command: %s", qPrintable (logTag (&m_commandSocket)), msg.command.constData ()); } @@ -328,15 +365,7 @@ Xpl2Client::dispatchStatusMessage (const Xpl2Protocol::ParsedMessage &msg) { if (msg.command == "KA_PING") handleKaPing (m_statusSocket); - else if (msg.command == "CN_JC_STATUS_MESSAGING_START") - handleCnJcStatusMessagingStart (msg.params); - else if (msg.command == "CN_JC_STATUS_MESSAGING_STOP") - handleCnJcStatusMessagingStop (msg.params); - else if (msg.command == "CN_PH_STATUS_MESSAGING_START") - handleCnPhStatusMessagingStart (msg.params); - else if (msg.command == "CN_PH_STATUS_MESSAGING_STOP") - handleCnPhStatusMessagingStop (msg.params); - else + else if (!dispatchResponse (msg.command, msg.params, &m_statusSocket)) qWarning ("%s Unknown command: %s", qPrintable (logTag (&m_statusSocket)), msg.command.constData ()); } @@ -406,243 +435,72 @@ Xpl2Client::handleGsPhVersion (const QVariantList ¶ms) bootVer); } -void -Xpl2Client::handleJcSuccessResponse (const QByteArray &command, - const QVariantList ¶ms) -{ - if (params.size () < 2) - { - qWarning () << command << ": expected 2 params, got" << params.size (); - return; - } - int cid = params[0].toInt (); - bool ok = params[1].toInt () == 1; - - qDebug ("%s %s controller=%d success=%d", - qPrintable (logTag (&m_commandSocket)), command.constData (), cid, - ok); - emit statusMessage (QStringLiteral ("RX %1: controller=%2 success=%3") - .arg (QString::fromUtf8 (command)) - .arg (cid) - .arg (ok)); - - if (command == "CN_JETTING_ALL_ON") - emit jettingAllOnResult (cid, ok); - else if (command == "CN_JETTING_ON") - emit jettingOnResult (cid, ok); - else if (command == "CN_JETTING_OFF") - emit jettingOffResult (cid, ok); - else if (command == "CN_JC_ID_LED_ON") - emit jcIdLedOnResult (cid, ok); - else if (command == "CN_JC_ID_LED_OFF") - emit jcIdLedOffResult (cid, ok); - else if (command == "CN_JC_CALIBRATION") - emit jcCalibrationResult (cid, ok); - else if (command == "CN_JC_RESET_FAULT_CODES") - emit jcResetFaultCodesResult (cid, ok); -} - -void -Xpl2Client::handlePhSuccessResponse (const QByteArray &command, - const QVariantList ¶ms) -{ - if (params.size () < 3) - { - qWarning () << command << ": expected 3 params, got" << params.size (); - return; - } - int cid = params[0].toInt (); - int phId = params[1].toInt (); - bool ok = params[2].toInt () == 1; - - qDebug ("%s %s controller=%d ph=%d success=%d", - qPrintable (logTag (&m_commandSocket)), command.constData (), cid, - phId, ok); - emit statusMessage (QStringLiteral ("RX %1: controller=%2 ph=%3 success=%4") - .arg (QString::fromUtf8 (command)) - .arg (cid) - .arg (phId) - .arg (ok)); - - if (command == "CN_PH_JETTING_ON") - emit phJettingOnResult (cid, phId, ok); - else if (command == "CN_PH_JETTING_OFF") - emit phJettingOffResult (cid, phId, ok); - else if (command == "CN_PH_ID_LED_ON") - emit phIdLedOnResult (cid, phId, ok); - else if (command == "CN_PH_ID_LED_OFF") - emit phIdLedOffResult (cid, phId, ok); - else if (command == "CN_PH_CALIBRATION") - emit phCalibrationResult (cid, phId, ok); - else if (command == "CN_PH_RESET_FAULT_CODES") - emit phResetFaultCodesResult (cid, phId, ok); - else if (command == "CN_PH_NOZZLES_DISABLED") - emit phNozzlesDisabledResult (cid, phId, ok); -} - -void -Xpl2Client::handleCnPhCalibrationData (const QVariantList ¶ms) -{ - if (params.size () < 50) - { - qWarning () << "CN_PH_CALIBRATION_DATA: expected 50 params, got" - << params.size (); - return; - } - int cid = params[0].toInt (); - int phId = params[1].toInt (); - QVariantList frequencies = params.mid (2, 48); - qDebug ("%s CN_PH_CALIBRATION_DATA controller=%d ph=%d (%lld freqs)", - qPrintable (logTag (&m_commandSocket)), cid, phId, - static_cast<long long> (frequencies.size ())); - emit statusMessage ( - QStringLiteral ("RX CN_PH_CALIBRATION_DATA: controller=%1 ph=%2") - .arg (cid) - .arg (phId)); - emit phCalibrationDataReceived (cid, phId, frequencies); -} - -void -Xpl2Client::handleCnPhCalibrationRawData (const QVariantList ¶ms) -{ - if (params.size () < 50) - { - qWarning () << "CN_PH_CALIBRATION_RAW_DATA: expected 50 params, got" - << params.size (); - return; - } - int cid = params[0].toInt (); - int phId = params[1].toInt (); - QVariantList frequencies = params.mid (2, 48); - qDebug ("%s CN_PH_CALIBRATION_RAW_DATA controller=%d ph=%d (%lld freqs)", - qPrintable (logTag (&m_commandSocket)), cid, phId, - static_cast<long long> (frequencies.size ())); - emit statusMessage ( - QStringLiteral ("RX CN_PH_CALIBRATION_RAW_DATA: controller=%1 ph=%2") - .arg (cid) - .arg (phId)); - emit phCalibrationRawDataReceived (cid, phId, frequencies); -} - -void -Xpl2Client::handleCnPhCalibratedBaseFrequency (const QVariantList ¶ms) +bool +Xpl2Client::dispatchResponse (const QByteArray &command, + const QVariantList ¶ms, + const QTcpSocket *socket) { - if (params.size () < 4) - { - qWarning () << "CN_PH_CALIBRATED_BASE_FREQUENCY: expected 4 params, got" - << params.size (); - return; - } - int cid = params[0].toInt (); - int phId = params[1].toInt (); - double baseFreq = params[2].toDouble (); - double activeBaseFreq = params[3].toDouble (); - qDebug ("%s CN_PH_CALIBRATED_BASE_FREQUENCY controller=%d ph=%d " - "base=%.2f active=%.2f", - qPrintable (logTag (&m_commandSocket)), cid, phId, baseFreq, - activeBaseFreq); - emit statusMessage ( - QStringLiteral ("RX CN_PH_CALIBRATED_BASE_FREQUENCY: controller=%1 " - "ph=%2 base=%3 active=%4") - .arg (cid) - .arg (phId) - .arg (baseFreq) - .arg (activeBaseFreq)); - emit phCalibratedBaseFrequencyReceived (cid, phId, baseFreq, activeBaseFreq); -} + auto it = s_responseTable.find (command); + if (it == s_responseTable.end ()) + return false; -void -Xpl2Client::handleCnJcStatusMessagingStart (const QVariantList ¶ms) -{ - if (params.size () < 4) + const ResponseEntry &entry = *it; + if (params.size () < entry.minParams) { - qWarning () << "CN_JC_STATUS_MESSAGING_START: expected 4 params, got" - << params.size (); - return; + qWarning ("%s %s: expected %d params, got %lld", + qPrintable (logTag (socket)), command.constData (), + entry.minParams, static_cast<long long> (params.size ())); + return true; } - int cid = params[0].toInt (); - int level = params[1].toInt (); - int interval = params[2].toInt (); - bool ok = params[3].toInt () == 1; - qDebug ("%s CN_JC_STATUS_MESSAGING_START controller=%d level=%d " - "interval=%d success=%d", - qPrintable (logTag (&m_statusSocket)), cid, level, interval, ok); - emit statusMessage ( - QStringLiteral ("RX CN_JC_STATUS_MESSAGING_START: controller=%1 " - "level=%2 interval=%3 success=%4") - .arg (cid) - .arg (level) - .arg (interval) - .arg (ok)); - emit jcStatusMessagingStartResult (cid, level, interval, ok); -} -void -Xpl2Client::handleCnJcStatusMessagingStop (const QVariantList ¶ms) -{ - if (params.size () < 2) - { - qWarning () << "CN_JC_STATUS_MESSAGING_STOP: expected 2 params, got" - << params.size (); - return; - } int cid = params[0].toInt (); - bool ok = params[1].toInt () == 1; - qDebug ("%s CN_JC_STATUS_MESSAGING_STOP controller=%d success=%d", - qPrintable (logTag (&m_statusSocket)), cid, ok); - emit statusMessage ( - QStringLiteral ("RX CN_JC_STATUS_MESSAGING_STOP: controller=%1 " - "success=%2") - .arg (cid) - .arg (ok)); - emit jcStatusMessagingStopResult (cid, ok); -} + QString cmd = QString::fromUtf8 (command); + QString logStr; -void -Xpl2Client::handleCnPhStatusMessagingStart (const QVariantList ¶ms) -{ - if (params.size () < 4) + switch (entry.shape) { - qWarning () << "CN_PH_STATUS_MESSAGING_START: expected 4 params, got" - << params.size (); - return; + case ResponseShape::JcSuccess: + logStr = QStringLiteral ("RX %1: controller=%2 success=%3") + .arg (cmd) + .arg (cid) + .arg (params[1].toInt () == 1); + break; + case ResponseShape::PhSuccess: + logStr = QStringLiteral ("RX %1: controller=%2 ph=%3 success=%4") + .arg (cmd) + .arg (cid) + .arg (params[1].toInt ()) + .arg (params[2].toInt () == 1); + break; + case ResponseShape::CalData: + logStr = QStringLiteral ("RX %1: controller=%2 ph=%3") + .arg (cmd) + .arg (cid) + .arg (params[1].toInt ()); + break; + case ResponseShape::BaseFreq: + logStr = QStringLiteral ("RX %1: controller=%2 ph=%3 base=%4 active=%5") + .arg (cmd) + .arg (cid) + .arg (params[1].toInt ()) + .arg (params[2].toDouble ()) + .arg (params[3].toDouble ()); + break; + case ResponseShape::StatusStart: + logStr = QStringLiteral ("RX %1: controller=%2 level=%3 interval=%4 " + "success=%5") + .arg (cmd) + .arg (cid) + .arg (params[1].toInt ()) + .arg (params[2].toInt ()) + .arg (params[3].toInt () == 1); + break; } - int cid = params[0].toInt (); - int level = params[1].toInt (); - int interval = params[2].toInt (); - bool ok = params[3].toInt () == 1; - qDebug ("%s CN_PH_STATUS_MESSAGING_START controller=%d level=%d " - "interval=%d success=%d", - qPrintable (logTag (&m_statusSocket)), cid, level, interval, ok); - emit statusMessage ( - QStringLiteral ("RX CN_PH_STATUS_MESSAGING_START: controller=%1 " - "level=%2 interval=%3 success=%4") - .arg (cid) - .arg (level) - .arg (interval) - .arg (ok)); - emit phStatusMessagingStartResult (cid, level, interval, ok); -} -void -Xpl2Client::handleCnPhStatusMessagingStop (const QVariantList ¶ms) -{ - if (params.size () < 2) - { - qWarning () << "CN_PH_STATUS_MESSAGING_STOP: expected 2 params, got" - << params.size (); - return; - } - int cid = params[0].toInt (); - bool ok = params[1].toInt () == 1; - qDebug ("%s CN_PH_STATUS_MESSAGING_STOP controller=%d success=%d", - qPrintable (logTag (&m_statusSocket)), cid, ok); - emit statusMessage ( - QStringLiteral ("RX CN_PH_STATUS_MESSAGING_STOP: controller=%1 " - "success=%2") - .arg (cid) - .arg (ok)); - emit phStatusMessagingStopResult (cid, ok); + qDebug ("%s %s", qPrintable (logTag (socket)), qPrintable (logStr)); + emit statusMessage (logStr); + entry.emitter (this, params); + return true; } /* ------------------------------------------------------------------ */ |
