| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // Read-only Qt 5.15 / Qt 6 example: two concurrent calls on one WebSocket.
- // No protobuf, gRPC client, or hardware commands are required.
- #include <QCoreApplication>
- #include <QDebug>
- #include <QHash>
- #include <QJsonArray>
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QNetworkRequest>
- #include <QStringList>
- #include <QTimer>
- #include <QUrl>
- #include <QUuid>
- #include <QWebSocket>
- int main(int argc, char **argv)
- {
- QCoreApplication app(argc, argv);
- const auto args = app.arguments();
- const QUrl url(args.value(1, QStringLiteral("ws://127.0.0.1:8080/ws")));
- const QString station = args.value(2, QStringLiteral("S1"));
- QWebSocket socket;
- QHash<QString, QString> pending; // request id -> method, never assume reply order
- bool done = false;
- int failures = 0;
- auto finish = [&](int code) {
- if (done) return;
- done = true;
- socket.close();
- app.exit(code);
- };
- auto call = [&](const QString &method, const QJsonObject ¶ms) {
- const QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
- pending.insert(id, method);
- const QJsonObject frame{{"v", 1}, {"id", id}, {"method", method}, {"params", params}};
- socket.sendTextMessage(QString::fromUtf8(QJsonDocument(frame).toJson(QJsonDocument::Compact)));
- };
- QObject::connect(&socket, &QWebSocket::connected, &app, [&] {
- call(QStringLiteral("GetSystemInfo"), {});
- call(QStringLiteral("GetStationControl"), {{"station_id", station}});
- });
- QObject::connect(&socket, &QWebSocket::textMessageReceived, &app, [&](const QString &text) {
- QJsonParseError parse;
- const auto doc = QJsonDocument::fromJson(text.toUtf8(), &parse);
- if (parse.error != QJsonParseError::NoError || !doc.isObject()) {
- qWarning() << "Invalid JSON reply";
- finish(1);
- return;
- }
- const auto frame = doc.object();
- if (frame.value("v").toInt() != 1 || !frame.value("id").isString()
- || !frame.value("ok").isBool()) {
- qWarning() << "Invalid JSON v1 response envelope";
- finish(1);
- return;
- }
- const QString id = frame.value("id").toString();
- if (!pending.contains(id)) return;
- const QString method = pending.take(id);
- if (!frame.value("ok").toBool()) {
- qWarning().noquote() << method << QJsonDocument(frame.value("error").toObject()).toJson(QJsonDocument::Compact);
- ++failures;
- } else {
- const auto result = frame.value("result").toObject();
- if (method == QStringLiteral("GetSystemInfo")) {
- qInfo() << "version" << result.value("version").toString()
- << "commit" << result.value("commit").toString();
- } else {
- const auto firmware = result.value("firmware").toObject();
- const auto mainStage = firmware.value("stage_progress").toObject().value("main").toObject();
- qInfo() << "station" << station << "main stage" << mainStage.value("id").toString()
- << "DIMM slots" << result.value("dimms").toArray().size();
- qInfo().noquote() << "prompt:" << QJsonDocument(firmware.value("console_prompt").toObject()).toJson(QJsonDocument::Compact);
- }
- }
- if (pending.isEmpty()) finish(failures ? 1 : 0);
- });
- QObject::connect(&socket, &QWebSocket::disconnected, &app, [&] {
- if (!done) {
- qWarning() << "Disconnected; requests are not replayed" << pending.keys();
- finish(1);
- }
- });
- auto socketError = [&](QAbstractSocket::SocketError) {
- qWarning().noquote() << socket.errorString();
- finish(1);
- };
- #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
- QObject::connect(&socket, &QWebSocket::errorOccurred, &app, socketError);
- #else
- QObject::connect(&socket, static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), &app, socketError);
- #endif
- QTimer::singleShot(15000, &app, [&] {
- if (!done) { qWarning() << "Reply timeout"; finish(1); }
- });
- QNetworkRequest request(url);
- const auto token = qgetenv("STL_API_TOKEN");
- if (!token.isEmpty()) request.setRawHeader("Authorization", "Bearer " + token);
- // Open after exec() starts: an immediate connection error must be able to
- // exit the event loop, rather than calling exit() before it is running.
- QTimer::singleShot(0, &app, [&socket, request] { socket.open(request); });
- return app.exec();
- }
|