mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
b message parser
fix bug that magic enum expects a string and crashes when you give it a json object add stout messages on command and events
This commit is contained in:
+25
-33
@@ -1,11 +1,12 @@
|
|||||||
#include "message_parser.hpp"
|
#include "message_parser.hpp"
|
||||||
#include <magic_enum.hpp>
|
#include <magic_enum.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
extern Simulators g_simulators;
|
extern Simulators g_simulators;
|
||||||
|
|
||||||
std::string message_parser(std::string message)
|
std::string message_parser(std::string message)
|
||||||
{
|
{
|
||||||
json j = message;
|
json j = json::parse(message);
|
||||||
if (j.contains("command"))
|
if (j.contains("command"))
|
||||||
{
|
{
|
||||||
return command_parser(j["command"]).dump();
|
return command_parser(j["command"]).dump();
|
||||||
@@ -23,82 +24,73 @@ std::string message_parser(std::string message)
|
|||||||
|
|
||||||
json command_parser(json command)
|
json command_parser(json command)
|
||||||
{
|
{
|
||||||
Command cmd = magic_enum::enum_cast<Command>(command["type"]).value_or(Command::end);
|
std::string type = command["type"];
|
||||||
|
Command cmd = magic_enum::enum_cast<Command>(type).value_or(Command::end);
|
||||||
|
json j;
|
||||||
switch (cmd)
|
switch (cmd)
|
||||||
{
|
{
|
||||||
case Command::get_UI_element:
|
case Command::get_UI_element:
|
||||||
{
|
std::cout << "get_UI_element: " << command["id"] << std::endl;
|
||||||
std::string id = command["id"];
|
|
||||||
json j;
|
|
||||||
j["response"] = "get_UI_element";
|
j["response"] = "get_UI_element";
|
||||||
j["UI_item"] = g_simulators.invoke_active_simulator()->get_UI_item(id)->to_json();
|
j["UI_item"] = g_simulators.invoke_active_simulator()->get_UI_item(command["id"])->to_json();
|
||||||
return j;
|
break;
|
||||||
}
|
|
||||||
case Command::get_UI_elements:
|
case Command::get_UI_elements:
|
||||||
{
|
std::cout << "get_UI_elements" << std::endl;
|
||||||
json j;
|
|
||||||
j["response"] = "get_UI_elements";
|
j["response"] = "get_UI_elements";
|
||||||
j["UI_items"] = g_simulators.invoke_active_simulator()->get_UI_items()["UI_items"];
|
j["UI_items"] = g_simulators.invoke_active_simulator()->get_UI_items()["UI_items"];
|
||||||
return j;
|
break;
|
||||||
}
|
|
||||||
case Command::switch_simulator:
|
case Command::switch_simulator:
|
||||||
{
|
std::cout << "switch_simulator to " << command["name"] << std::endl;
|
||||||
g_simulators.switch_simulator(command["name"]);
|
g_simulators.switch_simulator(command["name"]);
|
||||||
json j;
|
|
||||||
j["response"] = "switch_simulator";
|
j["response"] = "switch_simulator";
|
||||||
return j;
|
break;
|
||||||
}
|
|
||||||
case Command::get_active_simulator_name:
|
case Command::get_active_simulator_name:
|
||||||
{
|
std::cout << "get_active_simulator_name" << std::endl;
|
||||||
json j;
|
|
||||||
j["response"] = "get_active_simulator_name";
|
j["response"] = "get_active_simulator_name";
|
||||||
j["name"] = g_simulators.active_simulator_name();
|
j["name"] = g_simulators.active_simulator_name();
|
||||||
return j;
|
break;
|
||||||
}
|
|
||||||
case Command::get_simulators:
|
case Command::get_simulators:
|
||||||
{
|
std::cout << "get_simulators" << std::endl;
|
||||||
json j;
|
|
||||||
j["response"] = "get_simulators";
|
j["response"] = "get_simulators";
|
||||||
j["simulators"] = g_simulators.list_simulators();
|
j["simulators"] = g_simulators.list_simulators();
|
||||||
return j;
|
break;
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
break;
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
void event_handler(json event)
|
void event_handler(json event)
|
||||||
{
|
{
|
||||||
Event e = magic_enum::enum_cast<Event>(event["type"]).value_or(Event::end);
|
std::string type = event["type"];
|
||||||
|
Event e = magic_enum::enum_cast<Event>(type).value_or(Event::end);
|
||||||
|
std::string id;
|
||||||
|
|
||||||
switch(e)
|
switch(e)
|
||||||
{
|
{
|
||||||
case Event::clicked:
|
case Event::clicked:
|
||||||
{
|
{
|
||||||
std::string id = event["id"];
|
id = event["id"];
|
||||||
g_simulators.invoke_active_simulator()->get_UI_item(id)->click();
|
g_simulators.invoke_active_simulator()->get_UI_item(id)->click();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Event::value_changed:
|
case Event::value_changed:
|
||||||
{
|
{
|
||||||
std::string id = event["id"];
|
id = event["id"];
|
||||||
int value = event["value"];
|
int value = event["value"];
|
||||||
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_value(value);
|
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_value(value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Event::text_changed:
|
case Event::text_changed:
|
||||||
{
|
{
|
||||||
std::string id = event["id"];
|
id = event["id"];
|
||||||
std::string text = event["text"];
|
std::string text = event["text"];
|
||||||
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_text(text);
|
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_text(text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Event::selected:
|
case Event::selected:
|
||||||
{
|
{
|
||||||
std::string id = event["id"];
|
id = event["id"];
|
||||||
std::string selected = event["selected"];
|
std::string selected = event["selected"];
|
||||||
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_selected(selected);
|
g_simulators.invoke_active_simulator()->get_UI_item(id)->set_selected(selected);
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user