Files
remote-ui/tools/simulator_base.cpp
T
2024-12-30 12:08:13 +01:00

70 lines
1.4 KiB
C++

#include "simulator_base.hpp"
Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval, QObject *parent)
: QObject(parent), m_name(name), m_timer(new QTimer(this)), m_interval(interval)
{
connect(m_timer, &QTimer::timeout, this, [this] { timer(); });
m_UI_items.reserve(50);
}
std::string Simulator_base::name() const
{
// Get the name of the simulator
return m_name;
}
void Simulator_base::add_UI_item(UI_item *item)
{
// Add a UI item to the simulator
connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); });
m_UI_items.push_back(item);
}
json Simulator_base::get_UI_items() const
{
// Get the UI items
json items;
items["name"] = m_name;
for (size_t i = 0; i < m_UI_items.size(); i++)
{
items["UI_items"].push_back(m_UI_items[i]->to_json(i));
}
return items;
}
UI_item *Simulator_base::get_UI_item(size_t id) const
{
// Get a UI item by id using the list
if (id < m_UI_items.size())
{
return m_UI_items[id];
}
return nullptr;
}
void Simulator_base::run()
{
// Start the timer
m_timer->start(m_interval);
run_at_startup();
}
void Simulator_base::stop()
{
// Stop the simulator
m_timer->stop();
}
void Simulator_base::shutdown()
{
// Shutdown the simulator
system("shutdown -h now");
}
void Simulator_base::reboot()
{
// Reboot the simulator
system("reboot");
}