update websocket
updated to use protobuf
not quite finished with the Websocket message parser
This commit is contained in:
2026-08-20 13:27:32 +02:00
parent cb41ac2aa2
commit 338bd461af
9 changed files with 220 additions and 220 deletions
+109 -114
View File
@@ -3,131 +3,126 @@
#include <magic_enum/magic_enum.hpp>
#include <spdlog/spdlog.h>
QString Simulators::message_parser(QString message)
// TODO: Fix parsing messages
QByteArray Simulators::message_parser(QByteArray message)
{
json j = json::parse(message.toStdString());
if (j.contains("command"))
Event_m event_m;
bool event = m_serializer.deserialize(&event_m, message);
// if (j.contains("command"))
// {
// return QString::fromStdString(command_parser(j["command"]).dump());
// }
/*else*/ if (event)
{
return QString::fromStdString(command_parser(j["command"]).dump());
event_handler(event_m);
}
else if (j.contains("event"))
{
event_handler(j["event"]);
}
return "{}";
return QByteArray();
}
json Simulators::command_parser(json command)
// QByteArray Simulators::command_parser(json command)
// {
// json j = "{}"_json;
// try
// {
// std::string type = command.at("type");
// Command cmd = magic_enum::enum_cast<Command>(type).value_or(Command::end);
// switch (cmd)
// {
// case Command::get_UI_element:
// SPDLOG_INFO("Command {}: {}", type, (size_t)command.at("id"));
// if (is_not_active())
// {
// SPDLOG_ERROR("Command got called but there is no active simulator");
// return j;
// }
// j["response"]["type"] = type;
// j["response"]["UI_item"] =
// invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
// break;
// case Command::get_UI_elements:
// SPDLOG_INFO("Command {}", type);
// if (is_not_active())
// {
// SPDLOG_ERROR("Command got called but there is no active simulator");
// return j;
// }
// j["response"]["type"] = type;
// j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"];
// break;
// case Command::switch_simulator:
// SPDLOG_INFO("Command {} to {}", type, std::string(command.at("name")));
// switch_simulator(command.at("name"));
// j["response"]["type"] = type;
// break;
// case Command::get_active_simulator_name:
// SPDLOG_INFO("Command {}", type);
// j["response"]["type"] = type;
// j["response"]["name"] = active_simulator_name();
// break;
// case Command::get_simulators:
// SPDLOG_INFO("Command {}", type);
// j["response"]["type"] = type;
// j["response"]["simulators"] = list_simulators();
// break;
// default:
// break;
// }
// }
// catch (const json::exception &e)
// {
// SPDLOG_ERROR("{} {}", __FUNCTION__, e.what());
// }
// return j;
// }
void Simulators::event_handler(Event_m event)
{
json j = "{}"_json;
try
if (invoke_active_simulator() == nullptr)
{
std::string type = command.at("type");
Command cmd = magic_enum::enum_cast<Command>(type).value_or(Command::end);
switch (cmd)
{
case Command::get_UI_element:
SPDLOG_INFO("Command {}: {}", type, (size_t)command.at("id"));
if (is_not_active())
{
SPDLOG_ERROR("Command got called but there is no active simulator");
return j;
}
j["response"]["type"] = type;
j["response"]["UI_item"] =
invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
break;
case Command::get_UI_elements:
SPDLOG_INFO("Command {}", type);
if (is_not_active())
{
SPDLOG_ERROR("Command got called but there is no active simulator");
return j;
}
j["response"]["type"] = type;
j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"];
break;
case Command::switch_simulator:
SPDLOG_INFO("Command {} to {}", type, std::string(command.at("name")));
switch_simulator(command.at("name"));
j["response"]["type"] = type;
break;
case Command::get_active_simulator_name:
SPDLOG_INFO("Command {}", type);
j["response"]["type"] = type;
j["response"]["name"] = active_simulator_name();
break;
case Command::get_simulators:
SPDLOG_INFO("Command {}", type);
j["response"]["type"] = type;
j["response"]["simulators"] = list_simulators();
break;
default:
break;
}
}
catch (const json::exception &e)
{
SPDLOG_ERROR("{} {}", __FUNCTION__, e.what());
SPDLOG_ERROR("Event got called but there is no active simulator");
return;
}
return j;
}
QString type; // = QString::fromStdString(magic_enum::enum_cast<Event_m::KindFields>(event.kindField()).value_or(Event_m::KindFields::UninitializedField));
void Simulators::event_handler(json event)
{
try
switch (event.kindField())
{
std::string type = event.at("type");
Event e = magic_enum::enum_cast<Event>(type).value_or(Event::end);
if (invoke_active_simulator() == nullptr)
{
SPDLOG_ERROR("Event got called but there is no active simulator");
return;
}
switch (e)
{
case Event::clicked: {
SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id"));
invoke_active_simulator()->get_UI_item(event.at("id"))->click();
break;
}
case Event::value_changed: {
SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), (double)(event["value"]));
invoke_active_simulator()->get_UI_item(event.at("id"))->set_value((double)event.at("value"));
break;
}
case Event::text_changed: {
SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("text")));
invoke_active_simulator()->get_UI_item(event.at("id"))->set_text(std::string(event.at("text")));
break;
}
case Event::selected: {
SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), std::to_string((size_t)event.at("selected")));
invoke_active_simulator()->get_UI_item(event.at("id"))->set_selected(event.at("selected"));
break;
}
case Event::can_send: {
SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id"));
invoke_active_simulator()
->get_UI_item(event.at("id"))
->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"),
(std::array<uint8_t, 8>)event.at("payload"));
break;
}
case Event::clear: {
SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id"));
invoke_active_simulator()->get_UI_item(event.at("id"))->clear();
break;
}
default: {
break;
}
}
case Event_m::KindFields::Clicked: {
SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.id_proto());
invoke_active_simulator()->get_UI_item(event.id_proto())->click();
break;
}
case Event_m::KindFields::ValueChanged: {
SPDLOG_DEBUG("Event {}: {} {}", type, event.id_proto(), event.valueChanged().value());
invoke_active_simulator()->get_UI_item(event.id_proto())->set_value(event.valueChanged().value());
break;
}
case Event_m::KindFields::TextChanged: {
SPDLOG_DEBUG("Event {}: {} {}", type, event.id_proto(), event.textChanged().text());
invoke_active_simulator()->get_UI_item(event.id_proto())->set_text(event.textChanged().text());
break;
}
case Event_m::KindFields::Selected: {
SPDLOG_DEBUG("Event {}: {} {}", type, event.id_proto(), std::to_string(event.selected().selected()));
invoke_active_simulator()->get_UI_item(event.id_proto())->set_selected(event.selected().selected());
break;
}
case Event_m::KindFields::CanSend: {
SPDLOG_DEBUG("Event {}: {}", type, event.id_proto());
invoke_active_simulator()
->get_UI_item(event.id_proto())
->can_send(event.canSend().frame().sid(), event.canSend().frame().dlc(),
event.canSend().frame().payload());
break;
}
case Event_m::KindFields::Clear: {
SPDLOG_DEBUG("Event {}: {}", type, event.id_proto());
invoke_active_simulator()->get_UI_item(event.id_proto())->clear();
break;
}
default: {
break;
}
catch (const json::exception &e)
{
SPDLOG_ERROR("{} {}", __FUNCTION__, e.what());
}
}