main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Read-only Qt 5.15 / Qt 6 example: two concurrent calls on one WebSocket.
  2. // No protobuf, gRPC client, or hardware commands are required.
  3. #include <QCoreApplication>
  4. #include <QDebug>
  5. #include <QHash>
  6. #include <QJsonArray>
  7. #include <QJsonDocument>
  8. #include <QJsonObject>
  9. #include <QNetworkRequest>
  10. #include <QStringList>
  11. #include <QTimer>
  12. #include <QUrl>
  13. #include <QUuid>
  14. #include <QWebSocket>
  15. int main(int argc, char **argv)
  16. {
  17. QCoreApplication app(argc, argv);
  18. const auto args = app.arguments();
  19. const QUrl url(args.value(1, QStringLiteral("ws://127.0.0.1:8080/ws")));
  20. const QString station = args.value(2, QStringLiteral("S1"));
  21. QWebSocket socket;
  22. QHash<QString, QString> pending; // request id -> method, never assume reply order
  23. bool done = false;
  24. int failures = 0;
  25. auto finish = [&](int code) {
  26. if (done) return;
  27. done = true;
  28. socket.close();
  29. app.exit(code);
  30. };
  31. auto call = [&](const QString &method, const QJsonObject &params) {
  32. const QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
  33. pending.insert(id, method);
  34. const QJsonObject frame{{"v", 1}, {"id", id}, {"method", method}, {"params", params}};
  35. socket.sendTextMessage(QString::fromUtf8(QJsonDocument(frame).toJson(QJsonDocument::Compact)));
  36. };
  37. QObject::connect(&socket, &QWebSocket::connected, &app, [&] {
  38. call(QStringLiteral("GetSystemInfo"), {});
  39. call(QStringLiteral("GetStationControl"), {{"station_id", station}});
  40. });
  41. QObject::connect(&socket, &QWebSocket::textMessageReceived, &app, [&](const QString &text) {
  42. QJsonParseError parse;
  43. const auto doc = QJsonDocument::fromJson(text.toUtf8(), &parse);
  44. if (parse.error != QJsonParseError::NoError || !doc.isObject()) {
  45. qWarning() << "Invalid JSON reply";
  46. finish(1);
  47. return;
  48. }
  49. const auto frame = doc.object();
  50. if (frame.value("v").toInt() != 1 || !frame.value("id").isString()
  51. || !frame.value("ok").isBool()) {
  52. qWarning() << "Invalid JSON v1 response envelope";
  53. finish(1);
  54. return;
  55. }
  56. const QString id = frame.value("id").toString();
  57. if (!pending.contains(id)) return;
  58. const QString method = pending.take(id);
  59. if (!frame.value("ok").toBool()) {
  60. qWarning().noquote() << method << QJsonDocument(frame.value("error").toObject()).toJson(QJsonDocument::Compact);
  61. ++failures;
  62. } else {
  63. const auto result = frame.value("result").toObject();
  64. if (method == QStringLiteral("GetSystemInfo")) {
  65. qInfo() << "version" << result.value("version").toString()
  66. << "commit" << result.value("commit").toString();
  67. } else {
  68. const auto firmware = result.value("firmware").toObject();
  69. const auto mainStage = firmware.value("stage_progress").toObject().value("main").toObject();
  70. qInfo() << "station" << station << "main stage" << mainStage.value("id").toString()
  71. << "DIMM slots" << result.value("dimms").toArray().size();
  72. qInfo().noquote() << "prompt:" << QJsonDocument(firmware.value("console_prompt").toObject()).toJson(QJsonDocument::Compact);
  73. }
  74. }
  75. if (pending.isEmpty()) finish(failures ? 1 : 0);
  76. });
  77. QObject::connect(&socket, &QWebSocket::disconnected, &app, [&] {
  78. if (!done) {
  79. qWarning() << "Disconnected; requests are not replayed" << pending.keys();
  80. finish(1);
  81. }
  82. });
  83. auto socketError = [&](QAbstractSocket::SocketError) {
  84. qWarning().noquote() << socket.errorString();
  85. finish(1);
  86. };
  87. #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
  88. QObject::connect(&socket, &QWebSocket::errorOccurred, &app, socketError);
  89. #else
  90. QObject::connect(&socket, static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), &app, socketError);
  91. #endif
  92. QTimer::singleShot(15000, &app, [&] {
  93. if (!done) { qWarning() << "Reply timeout"; finish(1); }
  94. });
  95. QNetworkRequest request(url);
  96. const auto token = qgetenv("STL_API_TOKEN");
  97. if (!token.isEmpty()) request.setRawHeader("Authorization", "Bearer " + token);
  98. // Open after exec() starts: an immediate connection error must be able to
  99. // exit the event loop, rather than calling exit() before it is running.
  100. QTimer::singleShot(0, &app, [&socket, request] { socket.open(request); });
  101. return app.exec();
  102. }