Adds check for active simulator before commands

Implements a check to ensure a simulator is active before processing commands.

This prevents errors and unexpected behavior when commands are sent without an active simulator. The check is performed at the beginning of the command parsing, and logs an error if no simulator is active, returning an empty JSON object.
This commit is contained in:
2026-02-14 14:32:10 +01:00
parent fe0ee989e9
commit ee185b68e6
3 changed files with 16 additions and 0 deletions
+10
View File
@@ -32,12 +32,22 @@ json Simulators::command_parser(json command)
{ {
case Command::get_UI_element: case Command::get_UI_element:
SPDLOG_INFO("Command {}: {}", type, (size_t)command.at("id")); 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"]["type"] = type;
j["response"]["UI_item"] = j["response"]["UI_item"] =
invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id")); invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
break; break;
case Command::get_UI_elements: case Command::get_UI_elements:
SPDLOG_INFO("Command {}", type); 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"]["type"] = type;
j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"]; j["response"]["UI_items"] = invoke_active_simulator()->get_UI_items()["UI_items"];
break; break;
+5
View File
@@ -151,6 +151,11 @@ std::vector<std::string> Simulators::list_simulators() const
return names; return names;
} }
bool Simulators::is_not_active() const
{
return this->m_current_simulator.empty();
}
Simulator_base *Simulators::invoke_active_simulator() Simulator_base *Simulators::invoke_active_simulator()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
+1
View File
@@ -52,6 +52,7 @@ class Simulators : public QObject
void stop(); void stop();
void switch_simulator(std::string name); void switch_simulator(std::string name);
std::vector<std::string> list_simulators() const; std::vector<std::string> list_simulators() const;
bool is_not_active() const;
Simulator_base *invoke_active_simulator(); Simulator_base *invoke_active_simulator();
json changed_UI_items(); json changed_UI_items();