mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
Client
This commit is contained in:
+73
-58
@@ -3,33 +3,35 @@
|
||||
#include "git_version.h"
|
||||
#include "ui_base.hpp"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "web_socket_wrapper.hpp"
|
||||
#include "messages_client.grpc.qpb.h"
|
||||
#include <QDirIterator>
|
||||
#include <QLibrary>
|
||||
#include <QMessageBox>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
#include <magic_enum/magic_enum.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <QGrpcHttp2Channel>
|
||||
#include <QGrpcChannelOptions>
|
||||
#include <QSslConfiguration>
|
||||
#include <QSslSocket>
|
||||
|
||||
MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent)
|
||||
typedef UI_base *(*Get_UI)(RemoteUiService::Client *, QWidget *);
|
||||
|
||||
MainWindow::MainWindow(QUrl server_url, QString sim, QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow), m_error(new QMessageBox()),
|
||||
m_web_socket(new Web_socket_wrapper(ws_url))
|
||||
m_remote_ui_client(new RemoteUiService::Client()), m_ping(new QTimer())
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->statusbar->showMessage(QString("Version: %1-%2%3")
|
||||
.arg(Git_version::branch)
|
||||
.arg(Git_version::shortSha1)
|
||||
.arg(Git_version::dirty ? "-dirty" : ""));
|
||||
connect(m_web_socket.get(), &Web_socket_wrapper::log_signal, this,
|
||||
[](const char *filename_in, int line_in, const char *funcname_in, int level, QString msg) {
|
||||
if (spdlog::default_logger_raw()->should_log((spdlog::level::level_enum)level))
|
||||
spdlog::default_logger_raw()->log(spdlog::source_loc{filename_in, line_in, funcname_in},
|
||||
(spdlog::level::level_enum)level, msg.toStdString());
|
||||
});
|
||||
m_ping->setInterval(std::chrono::milliseconds(1000));
|
||||
|
||||
for (const QString &name : UIFactory::instance().keys())
|
||||
{
|
||||
if (auto *w = UIFactory::instance().create(name, m_web_socket.get(), this))
|
||||
if (auto *w = UIFactory::instance().create(name, m_remote_ui_client.get(), this))
|
||||
{
|
||||
w->set_name(name);
|
||||
m_sims.insert(std::make_pair(name, w));
|
||||
@@ -42,9 +44,6 @@ MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent)
|
||||
}
|
||||
}
|
||||
|
||||
// Insert debug sims here
|
||||
// INSERT_SIMULATOR(SIM_NAME);
|
||||
|
||||
setWindowTitle("Unisim cpp client");
|
||||
m_error->setIcon(QMessageBox::Critical);
|
||||
m_error->setWindowTitle("Critical");
|
||||
@@ -52,12 +51,42 @@ MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent)
|
||||
QPushButton *exit_app = m_error->addButton("Exit Application", QMessageBox::AcceptRole);
|
||||
m_error->addButton("Close", QMessageBox::RejectRole);
|
||||
connect(exit_app, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection);
|
||||
ui->connection->setText("Connected to " + ws_url.toString());
|
||||
ui->connection->setText("Connected to " + server_url.toString());
|
||||
// Default sim
|
||||
if (!sim.isEmpty())
|
||||
default_sim(sim);
|
||||
|
||||
QSslConfiguration ssl_config = QSslConfiguration::defaultConfiguration();
|
||||
|
||||
// Required for HTTP/2
|
||||
ssl_config.setAllowedNextProtocols({ "h2" });
|
||||
|
||||
// Disable certificate verification
|
||||
ssl_config.setPeerVerifyMode(QSslSocket::VerifyNone);
|
||||
|
||||
QGrpcChannelOptions options;
|
||||
options.setSslConfiguration(ssl_config);
|
||||
|
||||
server_url.setScheme("https");
|
||||
auto channel = std::make_shared<QGrpcHttp2Channel>(server_url, options);
|
||||
m_remote_ui_client->attachChannel(channel);
|
||||
|
||||
connect(ui->pushButton, &QPushButton::clicked, this, [=, this] { open_sim(ui->comboBox->currentText()); });
|
||||
connect(m_ping, &QTimer::timeout, this, [this]{
|
||||
// TODO: find a way to check if the channel is open
|
||||
});
|
||||
connect(this, &MainWindow::on_show_event, this, [this]{
|
||||
auto reply = m_remote_ui_client->Command_get_simulators(Empty());
|
||||
connect(reply.get(), &QGrpcCallReply::finished, this, [this, reply = reply.get()](const QGrpcStatus &status) {
|
||||
auto response = reply->read<Simulators_m>();
|
||||
if (!response.has_value() || !status.isOk())
|
||||
{
|
||||
SPDLOG_CRITICAL("Command get simulators has got no response");
|
||||
return;
|
||||
}
|
||||
pase_sim_names(response.value());
|
||||
}, Qt::SingleShotConnection);
|
||||
}, Qt::SingleShotConnection);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@@ -68,19 +97,14 @@ MainWindow::~MainWindow()
|
||||
void MainWindow::showEvent(QShowEvent *event)
|
||||
{
|
||||
QWidget::showEvent(event);
|
||||
connect(m_web_socket.get(), &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); });
|
||||
connect(m_web_socket.get(), &Web_socket_wrapper::on_connected, this,
|
||||
[=, this] { m_web_socket->send_command(Web_socket_wrapper::Command::get_simulators); });
|
||||
connect(m_web_socket.get(), &Web_socket_wrapper::on_closed, this, [=, this] {
|
||||
m_error->open();
|
||||
ui->connection->setText("Could not connect to the server");
|
||||
});
|
||||
emit on_show_event();
|
||||
m_ping->start();
|
||||
}
|
||||
|
||||
void MainWindow::hideEvent(QHideEvent *event)
|
||||
{
|
||||
QWidget::hideEvent(event);
|
||||
disconnect(m_web_socket.get(), nullptr, nullptr, nullptr);
|
||||
disconnect(m_remote_ui_client.get(), nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void MainWindow::default_sim(QString name)
|
||||
@@ -89,12 +113,11 @@ void MainWindow::default_sim(QString name)
|
||||
this->m_default_sim = true;
|
||||
}
|
||||
|
||||
void MainWindow::pase_sim_names(json &sims)
|
||||
void MainWindow::pase_sim_names(Simulators_m &sims)
|
||||
{
|
||||
for (json &item : sims)
|
||||
for (auto &item : sims.simulator())
|
||||
{
|
||||
if (m_sims.contains(QString::fromStdString(item)))
|
||||
ui->comboBox->addItem(QString::fromStdString(item));
|
||||
ui->comboBox->addItem(item);
|
||||
}
|
||||
|
||||
QThread::msleep(30);
|
||||
@@ -119,7 +142,18 @@ void MainWindow::open_sim(QString sim_name)
|
||||
return;
|
||||
}
|
||||
|
||||
m_web_socket->send_command(Web_socket_wrapper::Command::get_active_simulator_name);
|
||||
auto cmd = Simulator_m();
|
||||
cmd.setSimuator(sim_name);
|
||||
auto reply = m_remote_ui_client->Command_switch_simulator(cmd);
|
||||
connect(reply.get(), &QGrpcCallReply::finished, this, [this, reply = reply.get()] {
|
||||
auto response = reply->read<Empty>();
|
||||
if (!response.has_value())
|
||||
{
|
||||
qDebug("Switch simulator deserialization failed");
|
||||
return;
|
||||
}
|
||||
open_sim_window();
|
||||
}, Qt::SingleShotConnection);
|
||||
}
|
||||
|
||||
void MainWindow::open_sim_window(void)
|
||||
@@ -130,42 +164,23 @@ void MainWindow::open_sim_window(void)
|
||||
|
||||
void MainWindow::check_active_sim(QString name)
|
||||
{
|
||||
if (name == "")
|
||||
{
|
||||
SPDLOG_DEBUG("Name not set");
|
||||
m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name);
|
||||
return;
|
||||
}
|
||||
|
||||
SPDLOG_DEBUG("Current sim:", name);
|
||||
|
||||
if (name == m_selected_sim_name)
|
||||
{
|
||||
SPDLOG_DEBUG("Name set and the same");
|
||||
open_sim_window();
|
||||
return;
|
||||
}
|
||||
|
||||
SPDLOG_DEBUG("Name set and wrong");
|
||||
m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name);
|
||||
}
|
||||
|
||||
void MainWindow::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_simulators:
|
||||
pase_sim_names(j.at("simulators"));
|
||||
break;
|
||||
case Web_socket_wrapper::Command::get_active_simulator_name:
|
||||
check_active_sim(QString::fromStdString(j.at("name")));
|
||||
break;
|
||||
case Web_socket_wrapper::Command::switch_simulator:
|
||||
|
||||
auto cmd = Simulator_m();
|
||||
cmd.setSimuator(m_selected_sim_name);
|
||||
auto reply = m_remote_ui_client->Command_switch_simulator(cmd);
|
||||
connect(reply.get(), &QGrpcCallReply::finished, this, [this, reply = reply.get()] {
|
||||
auto response = reply->read<Empty>();
|
||||
if (!response.has_value())
|
||||
{
|
||||
qDebug("Switch simulator deserialization failed");
|
||||
return;
|
||||
}
|
||||
open_sim_window();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}, Qt::SingleShotConnection);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user