From 98bebb6c46bace93923552b1ea11dfd36c4a83f7 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 25 Aug 2024 17:02:31 +0200 Subject: [PATCH] Update sim base and the sims --- Qt/sim-can_debugger.cpp | 4 +- Qt/sim-can_debugger.h | 2 +- Qt/simulatorbase.cpp | 220 +++++++++++++++------------------------- Qt/simulatorbase.h | 32 +++--- 4 files changed, 101 insertions(+), 157 deletions(-) diff --git a/Qt/sim-can_debugger.cpp b/Qt/sim-can_debugger.cpp index 9e1946c..1cbace6 100644 --- a/Qt/sim-can_debugger.cpp +++ b/Qt/sim-can_debugger.cpp @@ -1,8 +1,8 @@ #include "sim-can_debugger.h" #include "ui_sim-can_debugger.h" -Can_Debugger::Can_Debugger(OpenAPI::OAICANApiApi* api, QWidget *parent) : - SimulatorBase(api, parent), +Can_Debugger::Can_Debugger(Web_socket_wrapper* web_socket, QWidget *parent) : + SimulatorBase(web_socket, parent), ui(new Ui::Can_Debugger) { ui->setupUi(this); this->setWindowTitle("CAN Debugger"); diff --git a/Qt/sim-can_debugger.h b/Qt/sim-can_debugger.h index 9aa28a4..808061d 100644 --- a/Qt/sim-can_debugger.h +++ b/Qt/sim-can_debugger.h @@ -12,7 +12,7 @@ class Can_Debugger : public SimulatorBase { Q_OBJECT public: - explicit Can_Debugger(OpenAPI::OAICANApiApi* api, QWidget *parent = nullptr); + explicit Can_Debugger(Web_socket_wrapper* web_socket, QWidget *parent = nullptr); ~Can_Debugger(); private: diff --git a/Qt/simulatorbase.cpp b/Qt/simulatorbase.cpp index 0a11eef..2f6e8e9 100644 --- a/Qt/simulatorbase.cpp +++ b/Qt/simulatorbase.cpp @@ -1,24 +1,16 @@ #include "simulatorbase.h" -SimulatorBase::SimulatorBase(OpenAPI::OAICANApiApi* api, QWidget* parent) : +SimulatorBase::SimulatorBase(Web_socket_wrapper* web_socket, QWidget* parent) : QMainWindow{parent}, - error_dialog(new NetworkError(this)) + m_error_dialog(new NetworkError(this)) { - this->api = api; - timer_update = new QTimer(this); - timer_update->connect(timer_update, &QTimer::timeout, this, [=]{sim_update();}); - refresh_rate = 100; -} - -void SimulatorBase::sim_update(void) -{ - api->apiCanapiCurrentsimGet(); + this->m_web_socket = web_socket; } void SimulatorBase::showEvent( QShowEvent* event ) { QWidget::showEvent( event ); - QD << "Starting refresh timer"; + QD << "Connecting callbacks"; setup_cb(); this->timer_update->start(refresh_rate); closed = false; @@ -39,95 +31,71 @@ void SimulatorBase::closeEvent(QCloseEvent* event) void SimulatorBase::setup_cb(void) { - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsPushbuttonPostSignal, this, [this](bool output){apiCanapiUielements_bool_cb(output);}); - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsPushbuttonPostSignalError, this, [this](bool output, QNetworkReply::NetworkError error_type, QString error_str){apiCanapiUielements_bool_cb(false, false, error_type, error_str);}); - - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsPushbuttonarrayPostSignal, this, [this](bool output){apiCanapiUielements_bool_cb(output);}); - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsPushbuttonarrayPostSignalError, this, [this](bool output, QNetworkReply::NetworkError error_type, QString error_str){apiCanapiUielements_bool_cb(false, false, error_type, error_str);}); - - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsTextboxPostSignal, this, [this](bool output){apiCanapiUielements_bool_cb(output);}); - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsTextboxPostSignalError, this, [this](bool output, QNetworkReply::NetworkError error_type, QString error_str){apiCanapiUielements_bool_cb(false, false, error_type, error_str);}); - - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsSliderPostSignal, this, [this](bool output){apiCanapiUielements_bool_cb(output);}); - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsSliderPostSignalError, this, [this](bool output, QNetworkReply::NetworkError error_type, QString error_str){apiCanapiUielements_bool_cb(false, false, error_type, error_str);}); - - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsTogglearrayPostSignal, this, [this](bool output){apiCanapiUielements_bool_cb(output);}); - connect(api, &OpenAPI::OAICANApiApi::apiCanapiUielementsTogglearrayPostSignalError, this, [this](bool output, QNetworkReply::NetworkError error_type, QString error_str){apiCanapiUielements_bool_cb(false, false, error_type, error_str);}); - - connect(api, &OpenAPI::OAICANApiApi::apiCanapiCurrentsimGetSignal, this, [this](OpenAPI::OAISimulatorInfo summary){apiCanapiCurrentsimGet_cb(summary);}); - connect(api, &OpenAPI::OAICANApiApi::apiCanapiCurrentsimGetSignalError, this, [this](OpenAPI::OAISimulatorInfo summary, QNetworkReply::NetworkError error_type, QString error_str){apiCanapiCurrentsimGet_cb(OpenAPI::OAISimulatorInfo(), error_type, error_str);}); + connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=](json& j){on_cmd_cb(j);}); + connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=](json& j){on_event_cb(j);}); } -void SimulatorBase::apiCanapiUielements_bool_cb(bool output, bool currentsim, QNetworkReply::NetworkError error_type, QString error_str) +void SimulatorBase::on_cmd_cb(json& j) { - if (error_type) { - QD << "( " << Qt::hex << output << "," << error_type << "," << error_str << ")"; - error_dialog->set_error(error_str + "\n" + network_reply_to_fix(error_type)); - error_dialog->open(); - return; - } if (!output) { - QD << " got called with invalid data and no error!"; - error_dialog->set_error(QString(__FUNCTION__) + " got called with invalid data and no error!"); - error_dialog->open(); - return; - } -} - -void SimulatorBase::apiCanapiCurrentsimGet_cb(OpenAPI::OAISimulatorInfo summary, QNetworkReply::NetworkError error_type, QString error_str) -{ - if (error_type) { - QD << "( " << summary.asJson() << "," << error_type << "," << error_str << ")"; - error_dialog->set_error(error_str + "\n" + network_reply_to_fix(error_type)); - error_dialog->open(); - return; - } if (!summary.isValid()) { - QD << " got called with invalid data and no error!"; - error_dialog->set_error(QString(__FUNCTION__) + " got called with invalid data and no error!"); - error_dialog->open(); - return; - } - OAISimulatorInfo_parser(summary); -} - -void SimulatorBase::OAISimulatorInfo_parser(OpenAPI::OAISimulatorInfo input) -{ - const OpenAPI::OAIClientUIItem* uiItem; - - for (size_t i = 0; i < input.getUiItems().count(); i++) + auto response = magic_enum::enum_cast(std::string(j.at("type"))).value_or(Web_socket_wrapper::Command::end); + switch (response) { - uiItem = &(input.getUiItems().at(i)); + case Web_socket_wrapper::Command::get_UI_elements: + UI_item_parser(j); + break; + default: + break; + } +} - if (uiItem->getType() == "UIButtonArray") +void SimulatorBase::on_event_cb(json& j) +{ + auto response = magic_enum::enum_cast(std::string(j.at("type"))).value_or(Web_socket_wrapper::Event::end); + switch (response) + { + case Web_socket_wrapper::Event::ui_changed: + UI_item_parser(j); + break; + default: + break; + } +} + +void SimulatorBase::UI_item_parser(json& input) +{ + for (auto& uiItem : input["UI_items"]) + { + if (uiItem["type"] == "UI_button") { - process_ui_button_array(uiItem); + process_ui_button(uiItem); continue; } - if (uiItem->getType() == "UILedArray") + if (uiItem["type"] == "UI_led") { - process_ui_led_array(uiItem); + process_ui_led(uiItem); continue; } - if (uiItem->getType() == "UISlider") + if (uiItem["type"] == "UI_slider") { process_ui_slider(uiItem); continue; } - if (uiItem->getType() == "UITextbox") + if (uiItem["type"] == "UI_textbox") { process_ui_textbox(uiItem); continue; } - if (uiItem->getType() == "UILabel") + if (uiItem["type"] == "UI_label") { process_ui_label(uiItem); continue; } - if (uiItem->getType() == "UICombobox") + if (uiItem["type"] == "UI_combobox") { process_ui_combobox(uiItem); continue; } - if (uiItem->getType() == "UIRadioButton") + if (uiItem["type"] == "UIRadioButton") { process_ui_radiobutton(uiItem); continue; @@ -135,36 +103,16 @@ void SimulatorBase::OAISimulatorInfo_parser(OpenAPI::OAISimulatorInfo input) } } -void SimulatorBase::process_ui_button_array(const OpenAPI::OAIClientUIItem* uiItem) +void SimulatorBase::process_ui_label(json& uiItem) { - QJsonObject state = uiItem->getState().asJsonObject(); - QJsonArray state_array = state["buttonArray"].toArray(); - for (size_t i = 0; i < state_array.count(); i++) - { - process_ui_button_array_element(state_array[i].toObject()); - } -} - -void SimulatorBase::process_ui_led_array(const OpenAPI::OAIClientUIItem* uiItem) -{ - QJsonObject state = uiItem->getState().asJsonObject(); - QJsonArray state_array = state["ledArray"].toArray(); - for (size_t i = 0; i < state_array.count(); i++) - { - process_ui_led_array_element(state_array[i].toObject()); - } -} - -void SimulatorBase::process_ui_label(const OpenAPI::OAIClientUIItem* uiItem) -{ - QLabel* label = id_to_label(uiItem->getName()); + QLabel* label = id_to_label(QString::fromStdString(uiItem["id"])); if (label == nullptr) { - QD << "id_to_label returned null on " << uiItem->getName(); + QD << "id_to_label returned null on " << QString::fromStdString(uiItem["id"]); return; } - QString value = uiItem->getState().asJsonObject()["textString"].toString(); + QString value = QString::fromStdString(uiItem["text"]); if (label->text() != value) { @@ -172,27 +120,27 @@ void SimulatorBase::process_ui_label(const OpenAPI::OAIClientUIItem* uiItem) } } -void SimulatorBase::process_ui_slider(const OpenAPI::OAIClientUIItem* uiItem) +void SimulatorBase::process_ui_slider(json& uiItem) { - if (uiItem->getName().startsWith('t')) + if (QString::fromStdString(uiItem["id"]).startsWith('t')) { - QwtSlider* slider = id_to_slider(uiItem->getName()); - QLabel* label = id_to_slider_label(uiItem->getName()); + QwtSlider* slider = id_to_slider(QString::fromStdString(uiItem["id"])); + QLabel* label = id_to_slider_label(QString::fromStdString(uiItem["id"])); if (slider == nullptr) { - QD << "id_to_slider returned null on " << uiItem->getName(); + QD << "id_to_slider returned null on " << QString::fromStdString(uiItem["id"]); return; } - double value = uiItem->getState().asJsonObject()["currentValue"].toDouble(); - bool enable = uiItem->getState().asJsonObject()["enable"].toBool(); - bool visible = uiItem->getState().asJsonObject()["visible"].toBool(); + double value = uiItem["value"]; + bool enable = uiItem["enabled"]; + bool visible = uiItem["visible"]; if (slider->value() != value) { if (label != nullptr) { - label->setText(format_slider_value(uiItem->getName(), value)); + label->setText(format_slider_value(QString::fromStdString(uiItem["id"]), value)); } slider->setValue(value); } @@ -210,24 +158,24 @@ void SimulatorBase::process_ui_slider(const OpenAPI::OAIClientUIItem* uiItem) return; } - if (uiItem->getName().startsWith('d')) + if (QString::fromStdString(uiItem["id"]).startsWith('d')) { - QwtDial* dial = id_to_dial(uiItem->getName()); - QLabel* label = id_to_dial_label(uiItem->getName()); + QwtDial* dial = id_to_dial(QString::fromStdString(uiItem["id"])); + QLabel* label = id_to_dial_label(QString::fromStdString(uiItem["id"])); if (dial == nullptr) { - QD << "id_to_dial returned null on " << uiItem->getName(); + QD << "id_to_dial returned null on " << QString::fromStdString(uiItem["id"]); return; } - double value = uiItem->getState().asJsonObject()["currentValue"].toDouble(); - bool visible = uiItem->getState().asJsonObject()["visible"].toBool(); + double value = uiItem["value"]; + bool visible = uiItem["visible"]; if (dial->value() != value) { if (label != nullptr) { - label->setText(format_dial_value(uiItem->getName(), value)); + label->setText(format_dial_value(QString::fromStdString(uiItem["id"]), value)); } dial->setValue(value); } @@ -240,31 +188,27 @@ void SimulatorBase::process_ui_slider(const OpenAPI::OAIClientUIItem* uiItem) } } -void SimulatorBase::process_ui_textbox(const OpenAPI::OAIClientUIItem* uiItem) +void SimulatorBase::process_ui_textbox(json& uiItem) { QD << "Not implemented"; emit quit(); } -void SimulatorBase::process_ui_combobox(const OpenAPI::OAIClientUIItem* uiItem) +void SimulatorBase::process_ui_combobox(json& uiItem) { - QComboBox* combobox = id_to_combobox(uiItem->getName()); + QComboBox* combobox = id_to_combobox(QString::fromStdString(uiItem["id"])); if (combobox == nullptr) { - QD << "id_to_combobox returned null on " << uiItem->getName(); + QD << "id_to_combobox returned null on " << QString::fromStdString(uiItem["id"]); return; } - QJsonObject state = uiItem->getState().asJsonObject(); - QString selected = state["selected"].toString(); - QJsonArray toggleArray = state["toggleArray"].toArray(); + QString selected = QString::fromStdString(uiItem["selected"]); - - for (size_t i = 0; i < toggleArray.count(); i++) + for (json& item : uiItem["options"]) { - QString item = toggleArray[i].toObject()["id"].toString(); - if (combobox->findText(item) == -1) - combobox->addItem(item); + if (combobox->findText(QString::fromStdString(item)) == -1) + combobox->addItem(QString::fromStdString(item)); } if (combobox->currentText() != selected) @@ -273,26 +217,26 @@ void SimulatorBase::process_ui_combobox(const OpenAPI::OAIClientUIItem* uiItem) } } -void SimulatorBase::process_ui_radiobutton(const OpenAPI::OAIClientUIItem* uiItem) +void SimulatorBase::process_ui_radiobutton(json& uiItem) { QD << "Not implemented"; emit quit(); } -void SimulatorBase::process_ui_button_array_element(QJsonObject jsonObject) +void SimulatorBase::process_ui_button(json& uiItem) { //TODO: fix bgcolor and color - QPushButton* button = id_to_button(jsonObject["id"].toString()); + QPushButton* button = id_to_button(QString::fromStdString(uiItem["id"])); if (button == nullptr) { - QD << "id_to_button returned null on " << jsonObject["id"].toString(); + QD << "id_to_button returned null on " << QString::fromStdString(uiItem["id"]); return; } - QColor bg_color = QColor(jsonObject["color"].toString()); - QString text = jsonObject["title"].toString(); - bool enabled = jsonObject["enable"].toBool(); + QColor bg_color = QColor(QString::fromStdString(uiItem["color"])); + QString text = QString::fromStdString(uiItem["text"]); + bool enabled = uiItem["enabled"]; if (button->text() != text) { @@ -334,18 +278,18 @@ void SimulatorBase::process_ui_button_array_element(QJsonObject jsonObject) } } -void SimulatorBase::process_ui_led_array_element(QJsonObject jsonObject) +void SimulatorBase::process_ui_led(json& uiItem) { - QPushButton* led = id_to_led(jsonObject["id"].toString()); + QPushButton* led = id_to_led(QString::fromStdString(uiItem["id"])); if (led == nullptr) { - QD << "id_to_led returned null on " << jsonObject["id"].toString(); + QD << "id_to_led returned null on " << QString::fromStdString(uiItem["id"]); return; } - QColor bg_color = QColor(jsonObject["state"].toString()); + QColor bg_color = QColor(QString::fromStdString(uiItem["led_color"])); QColor color = QColor(~bg_color.red(), ~bg_color.green(), ~bg_color.blue()); - QString text = jsonObject["title"].toString(); + QString text = QString::fromStdString(uiItem["text"]); if (false == is_read_only(led)) { diff --git a/Qt/simulatorbase.h b/Qt/simulatorbase.h index ecbc8b9..fbfa6a7 100644 --- a/Qt/simulatorbase.h +++ b/Qt/simulatorbase.h @@ -3,18 +3,21 @@ #include #include -#include "OAICANApiApi.h" +#include "web_socket_wrapper.h" #include "networkerror.h" #include "helpers.h" #include #include "IterableEnum.hpp" +#include +using json = nlohmann::json; + #define QD qDebug() << "Debug:" << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << ":" class SimulatorBase : public QMainWindow { Q_OBJECT public: - explicit SimulatorBase(OpenAPI::OAICANApiApi* api, QWidget* parent = nullptr); + explicit SimulatorBase(Web_socket_wrapper* api, QWidget* parent = nullptr); void setup_cb(void); void sim_update(void); virtual QLabel* id_to_label(QString name) {return nullptr;} @@ -32,24 +35,21 @@ class SimulatorBase : public QMainWindow { signals: void quit(); protected: - void apiCanapiUielements_bool_cb(bool ret, bool currentsim = false, QNetworkReply::NetworkError error_type = QNetworkReply::NetworkError::NoError, QString error_str = QString()); - void apiCanapiCurrentsimGet_cb(OpenAPI::OAISimulatorInfo output, QNetworkReply::NetworkError error_type = QNetworkReply::NetworkError::NoError, QString error_str = QString()); - - void OAISimulatorInfo_parser(OpenAPI::OAISimulatorInfo input); - void process_ui_button_array(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_led_array(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_label(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_slider(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_textbox(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_combobox(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_radiobutton(const OpenAPI::OAIClientUIItem* uiItem); - void process_ui_button_array_element(QJsonObject jsonObject); - void process_ui_led_array_element(QJsonObject jsonObject); + void on_event_cb(json& j); + void on_cmd_cb(json& j); + void UI_item_parser(json& input); + virtual void process_ui_label(json& uiItem); + virtual void process_ui_slider(json& uiItem); + virtual void process_ui_textbox(json& uiItem); + virtual void process_ui_combobox(json& uiItem); + virtual void process_ui_radiobutton(json& uiItem); + virtual void process_ui_button(json& uiItem); + virtual void process_ui_led(json& uiItem); void showEvent(QShowEvent* event); void closeEvent(QCloseEvent* event); - OpenAPI::OAICANApiApi* api; + Web_socket_wrapper* m_web_socket; QTimer* timer_update; uint32_t refresh_rate;