message parser

Add magic enum
Add function to parse commands
Add function to parse events
Add function to feed json input to there corresponding parse functions and return there result
This commit is contained in:
2024-08-22 13:57:32 +02:00
parent 45263970fc
commit e12263b8d2
3 changed files with 139 additions and 0 deletions
+8
View File
@@ -1,6 +1,13 @@
add_executable(server) add_executable(server)
include(FetchContent)
FetchContent_Declare(magic_enum URL https://github.com/Neargye/magic_enum/releases/download/v0.9.5/magic_enum-v0.9.5.tar.gz)
FetchContent_MakeAvailable(magic_enum)
target_sources(server PRIVATE target_sources(server PRIVATE
${CMAKE_CURRENT_LIST_DIR}/main.cpp ${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/message_parser.cpp
${CMAKE_CURRENT_LIST_DIR}/simulator_base.cpp ${CMAKE_CURRENT_LIST_DIR}/simulator_base.cpp
${CMAKE_CURRENT_LIST_DIR}/simulators.cpp ${CMAKE_CURRENT_LIST_DIR}/simulators.cpp
${CMAKE_CURRENT_LIST_DIR}/websocket.cpp ${CMAKE_CURRENT_LIST_DIR}/websocket.cpp
@@ -20,4 +27,5 @@ target_include_directories(server
target_link_libraries(server PRIVATE target_link_libraries(server PRIVATE
UI UI
Timer Timer
magic_enum::magic_enum
) )
+102
View File
@@ -0,0 +1,102 @@
#include "message_parser.hpp"
#include <magic_enum.hpp>
extern Simulators g_simulators;
std::string message_parser(std::string message)
{
json j = message;
if (j.contains("command"))
{
return command_parser(j["command"]).dump();
}
else if (j.contains("event"))
{
event_handler(j["event"]);
return "";
}
else
{
return "";
}
}
json command_parser(json command)
{
Command cmd = magic_enum::enum_cast<Command>(command["type"]).value_or(Command::end);
switch (cmd)
{
case Command::get_UI_element:
{
std::string id = command["id"];
return g_simulators.invoke_active_simulator()->get_UI_item(id)->to_json();
}
case Command::get_UI_elements:
{
return g_simulators.invoke_active_simulator()->get_UI_items();
}
case Command::switch_simulator:
{
std::string name = command["name"];
g_simulators.switch_simulator(name);
return "";
}
case Command::get_active_simulator_name:
{
json j;
j["name"] = g_simulators.active_simulator_name();
return j;
}
case Command::get_simulators:
{
json j;
j["simulators"] = g_simulators.list_simulators();
return j;
}
default:
{
return "";
}
}
}
void event_handler(json event)
{
Event e = magic_enum::enum_cast<Event>(event["type"]).value_or(Event::end);
switch(e)
{
case Event::clicked:
{
std::string id = event["id"];
g_simulators.invoke_active_simulator()->get_UI_item(id)->click();
break;
}
case Event::value_changed:
{
std::string id = event["id"];
int value = event["value"];
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_value(value);
break;
}
case Event::text_changed:
{
std::string id = event["id"];
std::string text = event["text"];
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_text(text);
break;
}
case Event::selected:
{
std::string id = event["id"];
std::string selected = event["selected"];
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_selected(selected);
break;
}
default:
{
break;
}
}
}
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include "simulators.hpp"
enum class Command
{
get_UI_element,
get_UI_elements,
switch_simulator,
get_active_simulator_name,
get_simulators,
end,
begin = 0
};
enum class Event
{
clicked,
value_changed,
text_changed,
selected,
ui_changed,
end,
begin = 0
};
std::string message_parser(std::string message);
json command_parser(json command);
void event_handler(json event);