Files
remote-ui/client/simulator_base.cpp
T
2026-02-14 14:31:01 +01:00

677 lines
17 KiB
C++

#include "simulator_base.hpp"
#include "can_transceive.hpp"
#include "plot_wrapper.hpp"
Simulator_base::Simulator_base(QString sim_name, Web_socket_wrapper *web_socket, QWidget *parent)
: QMainWindow{parent}, m_error_dialog(new NetworkError(this)), m_timer_update(new QTimer()),
m_web_socket(web_socket), m_sim_name(sim_name)
{
m_error_dialog->set_error("Connection timed out\nCheck if the server is running");
m_ui_lookup.reserve(40);
}
QWidget *Simulator_base::id_to_ui(size_t id)
{
return m_ui_lookup.at(id);
}
QString Simulator_base::sim_name() const
{
return m_sim_name;
}
void Simulator_base::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
SPDLOG_DEBUG("Connecting callbacks");
connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); });
connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=, this](json &j) { on_event_cb(j); });
connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this] { m_error_dialog->open(); });
m_web_socket->send_command(Web_socket_wrapper::Command::get_UI_elements);
this->m_timer_update->start(m_refresh_rate);
m_open = true;
}
void Simulator_base::closeEvent(QCloseEvent *event)
{
if (m_open == true)
{
SPDLOG_DEBUG("Disconnecting callbacks");
QWidget::closeEvent(event);
m_timer_update->stop();
disconnect(m_web_socket, nullptr, nullptr, nullptr);
m_open = false;
this->parentWidget()->show();
}
}
void Simulator_base::on_cmd_cb(json &j)
{
auto response = magic_enum::enum_cast<Web_socket_wrapper::Command>(std::string(j.at("type")))
.value_or(Web_socket_wrapper::Command::end);
switch (response)
{
case Web_socket_wrapper::Command::get_UI_elements:
case Web_socket_wrapper::Command::get_UI_element:
UI_item_parser(j);
break;
default:
break;
}
}
void Simulator_base::on_event_cb(json &j)
{
auto response = magic_enum::enum_cast<Web_socket_wrapper::Event>(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 Simulator_base::push_ui_item(QWidget *item)
{
m_ui_lookup.emplace_back(item);
setup_ui_item(item, m_ui_lookup.size() - 1);
}
void Simulator_base::UI_item_parser(json &input)
{
for (auto &uiItem : input["UI_items"])
{
if (uiItem["type"] == "UI_button")
{
process_ui_button(uiItem);
continue;
}
if (uiItem["type"] == "UI_led")
{
process_ui_led(uiItem);
continue;
}
if (uiItem["type"] == "UI_slider")
{
process_ui_slider(uiItem);
continue;
}
if (uiItem["type"] == "UI_dial")
{
process_ui_dial(uiItem);
continue;
}
if (uiItem["type"] == "UI_thermo")
{
process_ui_thermo(uiItem);
continue;
}
if (uiItem["type"] == "UI_textbox")
{
process_ui_textbox(uiItem);
continue;
}
if (uiItem["type"] == "UI_label")
{
process_ui_label(uiItem);
continue;
}
if (uiItem["type"] == "UI_combobox")
{
process_ui_combobox(uiItem);
continue;
}
if (uiItem["type"] == "UI_RadioButton")
{
process_ui_radiobutton(uiItem);
continue;
}
if (uiItem["type"] == "UI_checkbox")
{
process_ui_checkbox(uiItem);
continue;
}
if (uiItem["type"] == "UI_plot")
{
process_ui_plot(uiItem);
continue;
}
if (uiItem["type"] == "UI_can")
{
process_ui_can(uiItem);
continue;
}
}
}
void Simulator_base::process_ui_label(json &uiItem)
{
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto label = qobject_cast<QLabel *>(widget);
if (label == nullptr)
{
SPDLOG_WARN("widget is not of type QLabel");
return;
}
QString value = QString::fromStdString(uiItem["text"]);
if (label->text() != value)
{
label->setText(value);
}
}
void Simulator_base::process_ui_slider(json &uiItem)
{
QLabel *label = id_to_label(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto slider = qobject_cast<QwtSlider *>(widget);
if (slider == nullptr)
{
SPDLOG_WARN("widget is not of type QwtSlider");
return;
}
double value = uiItem["value"];
bool enable = uiItem["enabled"];
bool visible = uiItem["visible"];
QString unit = QString::fromStdString(uiItem.at("unit"));
if (label != nullptr)
{
label->setText(QString("%0 %1").arg(value).arg(unit));
}
if (slider->value() != value)
{
slider->setValue(value);
}
if (slider->isReadOnly() != !enable)
{
slider->setReadOnly(!enable);
}
if (slider->isVisible() != visible)
{
slider->setVisible(visible);
}
// TODO: Add color for scale
return;
}
void Simulator_base::process_ui_dial(json &uiItem)
{
QLabel *label = id_to_label(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto dial = qobject_cast<QwtDial *>(widget);
if (dial == nullptr)
{
SPDLOG_WARN("widget is not of type QwtDial");
return;
}
double value = uiItem["value"];
bool visible = uiItem["visible"];
QString unit = QString::fromStdString(uiItem.at("unit"));
if (label != nullptr)
{
label->setText(QString("%0 %1").arg(value).arg(unit));
}
if (dial->value() != value)
{
dial->setValue(value);
}
if (dial->isVisible() != visible)
{
dial->setVisible(visible);
}
return;
}
void Simulator_base::process_ui_thermo(json &uiItem)
{
QLabel *label = id_to_label(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto thermo = qobject_cast<QwtThermo *>(widget);
if (thermo == nullptr)
{
SPDLOG_WARN("widget is not of type QwtThermo");
return;
}
double value = uiItem["value"];
bool visible = uiItem["visible"];
QString unit = QString::fromStdString(uiItem.at("unit"));
if (label != nullptr)
{
label->setText(QString("%0 %1").arg(value).arg(unit));
}
if (thermo->value() != value)
{
thermo->setValue(value);
}
if (thermo->isVisible() != visible)
{
thermo->setVisible(visible);
}
return;
}
void Simulator_base::process_ui_textbox(json &uiItem)
{
SPDLOG_CRITICAL("Not implemented");
}
void Simulator_base::process_ui_combobox(json &uiItem)
{
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto combobox = qobject_cast<QComboBox *>(widget);
if (combobox == nullptr)
{
SPDLOG_WARN("widget is not of type QComboBox");
return;
}
QString selected = QString::fromStdString(uiItem["selected"]);
for (json &item : uiItem["options"])
{
if (combobox->findText(QString::fromStdString(item)) == -1)
combobox->addItem(QString::fromStdString(item));
}
if (combobox->currentText() != selected)
{
combobox->setCurrentText(selected);
}
}
void Simulator_base::process_ui_radiobutton(json &uiItem)
{
SPDLOG_CRITICAL("Not implemented");
}
void Simulator_base::process_ui_checkbox(json &uiItem)
{
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto checkbox = qobject_cast<QCheckBox *>(widget);
if (checkbox == nullptr)
{
SPDLOG_WARN("widget is not of type QCheckbox");
return;
}
QColor bg_color = QColor(QString::fromStdString(uiItem["bg_color"]));
QColor fg_color = QColor(QString::fromStdString(uiItem["fg_color"]));
QString text = QString::fromStdString(uiItem["text"]);
bool enabled = uiItem["enabled"];
bool checked = uiItem["checked"];
if (checkbox->isChecked() != checked)
{
checkbox->setChecked(checked);
}
if (checkbox->text() != text)
{
checkbox->setText(text);
}
if (checkbox->isEnabled() != enabled)
{
checkbox->setEnabled(enabled);
}
if (bg_color == QColor(0x83, 0x91, 0x92))
{
reset_widget_color(checkbox);
return;
}
if (bg_color == QColor(0xFF, 0x00, 0x00))
{
set_widget_color(checkbox, QString("black"), QString("darkred"));
return;
}
if (bg_color == QColor(0x00, 0xFF, 0x00))
{
set_widget_color(checkbox, QString("white"), QString("darkgreen"));
return;
}
if (bg_color == QColor(0xFF8800))
{
set_widget_color(checkbox, QString("darkred"), QString("darkorange"));
return;
}
if (bg_color != widget_bg_color(checkbox) || fg_color != widget_color(checkbox))
{
set_widget_color(checkbox, fg_color, bg_color);
}
}
void Simulator_base::process_ui_button(json &uiItem)
{
// TODO: fix bgcolor and color
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto button = qobject_cast<QPushButton *>(widget);
if (button == nullptr)
{
SPDLOG_WARN("widget is not of type QPushButton");
return;
}
QColor bg_color = QColor(QString::fromStdString(uiItem["bg_color"]));
QColor fg_color = QColor(QString::fromStdString(uiItem["fg_color"]));
QString text = QString::fromStdString(uiItem["text"]);
bool enabled = uiItem["enabled"];
if (button->text() != text)
{
button->setText(text);
}
if (button->isEnabled() != enabled)
{
button->setEnabled(enabled);
}
if (bg_color == QColor(0x83, 0x91, 0x92))
{
reset_widget_color(button);
return;
}
if (bg_color == QColor(0xFF, 0x00, 0x00))
{
set_widget_color(button, QString("black"), QString("darkred"));
return;
}
if (bg_color == QColor(0x00, 0xFF, 0x00))
{
set_widget_color(button, QString("white"), QString("darkgreen"));
return;
}
if (bg_color == QColor(0xFF8800))
{
set_widget_color(button, QString("darkred"), QString("darkorange"));
return;
}
if (bg_color != widget_bg_color(button) || fg_color != widget_color(button))
{
set_widget_color(button, fg_color, bg_color);
}
}
void Simulator_base::process_ui_led(json &uiItem)
{
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto led = qobject_cast<Led *>(widget);
if (led == nullptr)
{
SPDLOG_WARN("widget is not of type led");
return;
}
QColor bg_color = QColor(QString::fromStdString(uiItem["bg_color"]));
QColor color = QColor(QString::fromStdString(uiItem["fg_color"]));
QString text = QString::fromStdString(uiItem["text"]);
if (text != led->text())
{
led->setText(text);
}
if (bg_color == QColor(0x83, 0x91, 0x92))
{
reset_widget_color(led);
return;
}
if (bg_color == QColor(0xFF, 0x00, 0x00))
{
set_widget_color(led, QString("black"), QString("darkred"));
return;
}
if (bg_color == QColor(0x00, 0xFF, 0x00))
{
set_widget_color(led, QString("white"), QString("darkgreen"));
return;
}
if (bg_color == QColor(0xFF8800))
{
set_widget_color(led, QString("darkred"), QString("darkorange"));
return;
}
if (bg_color != widget_bg_color(led) || color != widget_color(led))
{
set_widget_color(led, color, bg_color);
}
}
void Simulator_base::process_ui_plot(json &uiItem)
{
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto plot = qobject_cast<Plot_wrapper *>(widget);
if (plot == nullptr)
{
SPDLOG_WARN("widget is not of type QwtPlot");
return;
}
QString text = QString::fromStdString(uiItem["text"]);
QString x_label = QString::fromStdString(uiItem["x_label"]);
QString y_label = QString::fromStdString(uiItem["y_label"]);
std::vector<double> x_vals = (std::vector<double>)uiItem.at("x_vals");
std::vector<double> y_vals = (std::vector<double>)uiItem.at("x_vals");
if (text != plot->title().text())
{
plot->setTitle(text);
}
if (x_label != plot->axisTitle(QwtAxis::XBottom).text())
{
plot->setAxisTitle(QwtAxis::XBottom, x_label);
}
if (y_label != plot->axisTitle(QwtAxis::YLeft).text())
{
plot->setAxisTitle(QwtAxis::YLeft, y_label);
}
plot->curve()->setRawSamples(
x_vals.data(),
y_vals.data(),
x_vals.capacity()
);
plot->setAxisScale( QwtAxis::XBottom,
x_vals[x_vals.capacity() - 1], x_vals[0] );
// plot_time[0], plot_time[api.get_plot_size() - 1] );
plot->replot();
}
void Simulator_base::process_ui_can(json &uiItem)
{
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto can_ui = qobject_cast<Can_Transceive *>(widget);
if (can_ui == nullptr)
{
SPDLOG_WARN("widget is not of type led");
return;
}
can_ui->clear();
for (auto &send_item : uiItem["send_msgs"])
{
can_ui->add_send_item(send_item["id"], send_item["dlc"], send_item["payload"]);
}
for (auto &recvd_item : uiItem["rcvd_msgs"])
{
can_ui->add_receive_item(recvd_item["id"], recvd_item["dlc"], recvd_item["payload"]);
}
}
void Simulator_base::setup_ui_item(QWidget *item, size_t index)
{
setup_button(item, index);
setup_combobox(item, index);
setup_checkbox(item, index);
setup_dial(item, index);
setup_slider(item, index);
setup_qwtplot(item, index);
setup_can_ui(item, index);
}
void Simulator_base::setup_button(QWidget *item, size_t index)
{
QPushButton *button = qobject_cast<QPushButton *>(item);
if (button == nullptr)
return;
connect(button, &QPushButton::clicked, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::clicked, index); });
}
void Simulator_base::setup_combobox(QWidget *item, size_t index)
{
QComboBox *combobox = qobject_cast<QComboBox *>(item);
if (combobox == nullptr)
return;
connect(combobox, &QComboBox::currentIndexChanged, this, [=, this] {
m_web_socket->send_event(Web_socket_wrapper::Event::selected, index, combobox->currentText());
});
}
void Simulator_base::setup_checkbox(QWidget *item, size_t index)
{
QCheckBox *checkbox = qobject_cast<QCheckBox *>(item);
if (checkbox == nullptr)
return;
connect(checkbox, &QCheckBox::clicked, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::clicked, index); });
}
void Simulator_base::setup_dial(QWidget *item, size_t index)
{
QwtDial *dial = qobject_cast<QwtDial *>(item);
if (dial == nullptr)
return;
create_dial_needle(dial);
}
void Simulator_base::setup_slider(QWidget *item, size_t index)
{
QwtSlider *slider = qobject_cast<QwtSlider *>(item);
if (slider == nullptr)
return;
connect(slider, &QwtSlider::sliderMoved, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, slider->value()); });
}
void Simulator_base::setup_qwtplot(QWidget *item, size_t index)
{
auto *plot = qobject_cast<QwtPlot *>(item);
if (plot == nullptr)
return;
}
void Simulator_base::setup_can_ui(QWidget *item, size_t index)
{
Can_Transceive *can = qobject_cast<Can_Transceive *>(item);
if (can == nullptr)
return;
connect(can, &Can_Transceive::send_can_message, this,
[=, this](uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload) {
m_web_socket->send_event(Web_socket_wrapper::Event::can_send, index, id, dlc, payload);
});
connect(can, &Can_Transceive::can_clear, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::can_clear, index); });
}