/** * @RDE_HEADER@ * ExerciseChooser Widget for ADLER WebsocketSimulator * $Id$ * $HeadURL$ * $Author$ * Created by Patric Karwat, ESZS, 2025-03-03 */ #include "awss_exercise_chooser.h" #include "ui_awss_exercise_chooser.h" #include #include #include #include namespace AdlerWebSocketSimulator { ExerciseChooser::ExerciseChooser(QWidget *parent) : QWidget(parent) , m_ui(new Ui::ExerciseChooser) , m_logger(getLogger("ExerciseChooser")) { m_ui->setupUi(this); m_ui->stackedWidget->setCurrentIndex(0); } ExerciseChooser::~ExerciseChooser() { delete m_ui; } void ExerciseChooser::setExercises(const QString &message) { ERROR_LOG(m_logger) << message; if (!m_ui->vlExercises->isEmpty()) { for (auto ptr : m_ui->Exercise->findChildren()) { m_ui->vlExercises->removeWidget(ptr); ptr->deleteLater(); } //remove all } QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());// = QJsonDocument::fromJson(message); if (!doc.object().contains(QStringLiteral("data"))) return; QJsonValue val = doc.object().value(QStringLiteral("data")); if (!val.isArray()) return; for (const auto &ex: val.toArray()) { // ERROR_LOG(m_logger) << ex; int ppmPort = 0; QJsonObject ob = ex.toObject(); if (!ob.contains(QStringLiteral("exerciseId"))) return; //get current Exercise ID int id = ob.value(QStringLiteral("exerciseId")).toInt(); // now querry services for PPM Port if (ob.contains(QStringLiteral("services"))) { QJsonValue services = ob.value(QStringLiteral("services")); if (!services.isArray()) return; for (const auto &serv :services.toArray()) { if (!serv.toObject().isEmpty() && serv.toObject().contains(QStringLiteral("nameAbbrevation"))) { bool isTab = serv.toObject().value(QStringLiteral("nameAbbrevation")).toString().toLower() == QStringLiteral("tab"); bool isPpm = serv.toObject().value(QStringLiteral("nameAbbrevation")).toString().toLower() == QStringLiteral("ppm"); bool isExm = serv.toObject().value(QStringLiteral("nameAbbrevation")).toString().toLower() == QStringLiteral("exm"); if (!isTab && !isPpm && !isExm) continue; QJsonValue ports = serv.toObject().value(QStringLiteral("ports")); if (!ports.isArray()) return; for (const auto &port: ports.toArray()) { if (!port.toObject().contains(QStringLiteral("typeId")) || !port.toObject().contains(QStringLiteral("number")) ) return; if (isExm && port.toObject().value(QStringLiteral("typeId")).toInt() == 1) { m_exmPort = port.toObject().value(QStringLiteral("number")).toInt() ; continue; } if (isPpm && port.toObject().value(QStringLiteral("typeId")).toInt() == 1) { ppmPort = port.toObject().value(QStringLiteral("number")).toInt() ; } if (isTab && port.toObject().value(QStringLiteral("typeId")).toInt() == 3) { int tabPort = port.toObject().value(QStringLiteral("number")).toInt() ; m_execerciseToProxyPort.insert(id, tabPort ); continue; } } } } } if (ppmPort <= 0 || id <= 0) continue; QPushButton *btn = new QPushButton(tr("Exercise: ")+ QString::number(id)); btn->setProperty("port", ppmPort); btn->setProperty("excerciseId", id); btn->setDisabled(true); btn->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding); connect( btn, &QPushButton::clicked, this, &ExerciseChooser::onExerciseClicked); m_ui->vlExercises->addWidget(btn); } if (m_exmPort > 0) { emit requestExerciseInformation(m_exmPort); } //parse exercises } void ExerciseChooser::setExeciseInfo(const QString &message) { ERROR_LOG(m_logger) << message; if (m_ui->vlExercises->isEmpty()) { return; } QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());// = QJsonDocument::fromJson(message); if (!doc.object().contains(QStringLiteral("data"))) return; QJsonValue val = doc.object().value(QStringLiteral("data")); if (!val.isArray()) return; for (const auto &ex: val.toArray()) { if (ex.toObject().isEmpty()) return; const QJsonObject &exOb = ex.toObject(); if (!exOb.contains(QStringLiteral("status")) || !exOb.contains(QStringLiteral("id"))) return; int id = exOb.value(QStringLiteral("id")).toInt(); int status = exOb.value(QStringLiteral("status")).toInt(); for (auto ptr : m_ui->Exercise->findChildren(QString())) { if (ptr->property("excerciseId").toInt() == id) { if (status > 1 && status < 6) ptr->setEnabled(true); } } } } void ExerciseChooser::setPartyData(const QString &message) { if (!m_ui->vlParty->isEmpty()) { for (auto ptr : m_ui->Party->findChildren()) { m_ui->vlParty->removeWidget(ptr); ptr->deleteLater(); } //remove all } QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());// = QJsonDocument::fromJson(message); if (!doc.object().contains(QStringLiteral("data"))) return; QJsonValue val = doc.object().value(QStringLiteral("data")); if (!val.isArray()) return; for (const auto &partyData: val.toArray()) { // ERROR_LOG(m_logger) << ex; QJsonObject ob = partyData.toObject(); int id = ob.value(QStringLiteral("id")).toInt(); if (id <= 1) // 0 ist pool 1 is OC continue; QString name; QColor color; // now querry services for PPM Port if (!ob.contains(QStringLiteral("name"))) return; name = ob.value(QStringLiteral("name")).toString(); if (!ob.contains(QStringLiteral("color"))) return; color = QColor::fromRgb(QRgb(ob.value(QStringLiteral("color")).toInt())); QPushButton *btn = new QPushButton(tr("Name: ") + name); btn->setStyleSheet(QStringLiteral("background-color: %1").arg(color.name())); btn->setProperty("id", id); btn->setProperty("name", name); btn->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding); connect( btn, &QPushButton::clicked, this, &ExerciseChooser::onPartyClicked); m_ui->vlParty->addWidget(btn); } m_ui->stackedWidget->setCurrentIndex(1); } void ExerciseChooser::onExerciseClicked() { int port = sender()->property("port").toInt(); int excerciseId = sender()->property("excerciseId").toInt(); m_currentExercise = excerciseId; emit connectToExercise(port); } void ExerciseChooser::onPartyClicked() { int partyId = sender()->property("id").toInt(); QString partyName = sender()->property("name").toString(); emit connectToProxy(m_execerciseToProxyPort.value(m_currentExercise), partyId, partyName); } } //namespace AdlerWebSocketSimulator