This commit is contained in:
2024-08-29 15:00:27 +02:00
parent 6c6a60e980
commit 21a2c16607
31 changed files with 457 additions and 409 deletions
+3 -4
View File
@@ -1,14 +1,13 @@
#include "UI_button.hpp" #include "UI_button.hpp"
UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, std::function<void(std::string)> on_click) UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color,
std::function<void(std::string)> on_click)
: UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color) : UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color)
{ {
this->m_on_click = on_click; this->m_on_click = on_click;
} }
UI_button::UI_button(const json& j) UI_button::UI_button(const json &j) : UI_item(UI_BUTTON_TYPE), m_on_click(nullptr)
: UI_item(UI_BUTTON_TYPE),
m_on_click(nullptr)
{ {
from_json(j); from_json(j);
} }
+3 -1
View File
@@ -7,13 +7,15 @@
class UI_button : public UI_item class UI_button : public UI_item
{ {
public: public:
UI_button(std::string id, std::string text, Color fg_color, Color bg_color, std::function<void(std::string)> on_click = nullptr); UI_button(std::string id, std::string text, Color fg_color, Color bg_color,
std::function<void(std::string)> on_click = nullptr);
UI_button(const json &j); UI_button(const json &j);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
void click() const override; void click() const override;
private: private:
std::function<void(std::string)> m_on_click = nullptr; std::function<void(std::string)> m_on_click = nullptr;
}; };
+5 -5
View File
@@ -1,11 +1,12 @@
#include "UI_checkbox.hpp" #include "UI_checkbox.hpp"
UI_checkbox::UI_checkbox() UI_checkbox::UI_checkbox() : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
: UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
{ {
} }
UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, std::function<void(std::string, std::vector<std::string>)> on_change) UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options,
std::function<void(std::string, std::vector<std::string>)> on_change)
: UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options), m_selected(), m_on_change(on_change) : UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options), m_selected(), m_on_change(on_change)
{ {
for (size_t i = 0; i < m_options.size(); i++) for (size_t i = 0; i < m_options.size(); i++)
@@ -14,8 +15,7 @@ UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color
} }
} }
UI_checkbox::UI_checkbox(const json& j) : UI_checkbox::UI_checkbox(const json &j) : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
{ {
from_json(j); from_json(j);
} }
+4 -2
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <cstdint>
#include "UI_item.hpp" #include "UI_item.hpp"
#include "UI_x_boxes.hpp" #include "UI_x_boxes.hpp"
#include <cstdint>
#define UI_CHECKBOX_TYPE std::string("UI_checkbox") #define UI_CHECKBOX_TYPE std::string("UI_checkbox")
@@ -9,7 +9,8 @@ class UI_checkbox : public UI_x_boxes
{ {
public: public:
UI_checkbox(); UI_checkbox();
UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, std::function<void(std::string, std::vector<std::string>)> on_change = nullptr); UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
std::function<void(std::string, std::vector<std::string>)> on_change = nullptr);
UI_checkbox(const json &j); UI_checkbox(const json &j);
void toggle_selected(size_t selected); void toggle_selected(size_t selected);
@@ -19,6 +20,7 @@ class UI_checkbox : public UI_x_boxes
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
std::vector<bool> m_selected; std::vector<bool> m_selected;
+5 -5
View File
@@ -1,17 +1,17 @@
#include "UI_combobox.hpp" #include "UI_combobox.hpp"
UI_combobox::UI_combobox() UI_combobox::UI_combobox() : UI_x_boxes(UI_COMBOBOX_TYPE)
: UI_x_boxes(UI_COMBOBOX_TYPE)
{ {
} }
UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected, std::function<void(std::string, std::string)> on_change) UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, size_t selected,
std::function<void(std::string, std::string)> on_change)
: UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options), m_selected(selected), m_on_change(on_change) : UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options), m_selected(selected), m_on_change(on_change)
{ {
} }
UI_combobox::UI_combobox(const json& j) UI_combobox::UI_combobox(const json &j) : UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr)
: UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr)
{ {
from_json(j); from_json(j);
} }
+4 -2
View File
@@ -1,8 +1,8 @@
#pragma once #pragma once
#include <functional>
#include "UI_item.hpp" #include "UI_item.hpp"
#include "UI_x_boxes.hpp" #include "UI_x_boxes.hpp"
#include <functional>
#define UI_COMBOBOX_TYPE std::string("UI_combobox") #define UI_COMBOBOX_TYPE std::string("UI_combobox")
@@ -10,7 +10,8 @@ class UI_combobox : public UI_x_boxes
{ {
public: public:
UI_combobox(); UI_combobox();
UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0, std::function<void(std::string, std::string)> on_change = nullptr); UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
size_t selected = 0, std::function<void(std::string, std::string)> on_change = nullptr);
UI_combobox(const json &j); UI_combobox(const json &j);
void set_selected(size_t selected); void set_selected(size_t selected);
@@ -20,6 +21,7 @@ class UI_combobox : public UI_x_boxes
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
size_t m_selected; size_t m_selected;
+1 -2
View File
@@ -5,8 +5,7 @@ UI_dial::UI_dial(std::string id, std::string text, Color fg_color, Color bg_colo
{ {
} }
UI_dial::UI_dial(const json& j) UI_dial::UI_dial(const json &j) : UI_item(UI_DIAL_TYPE)
: UI_item(UI_DIAL_TYPE)
{ {
from_json(j); from_json(j);
} }
+1
View File
@@ -17,6 +17,7 @@ class UI_dial : public UI_item
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
double m_min; double m_min;
double m_max; double m_max;
+4 -2
View File
@@ -1,12 +1,14 @@
#include "UI_item.hpp" #include "UI_item.hpp"
UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color) UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color)
: m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color), enabled(true), visible(true) : m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color),
enabled(true), visible(true)
{ {
} }
UI_item::UI_item(std::string type) UI_item::UI_item(std::string type)
: m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White), enabled(true), visible(true) : m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White),
enabled(true), visible(true)
{ {
} }
+13 -6
View File
@@ -1,15 +1,16 @@
#pragma once #pragma once
#include <functional> #include "color.hpp"
#include <thread>
#include <cstdint> #include <cstdint>
#include <functional>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
#include "color.hpp" #include <thread>
using json = nlohmann::json; using json = nlohmann::json;
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
class UI_item { class UI_item
{
public: public:
UI_item(std::string type); UI_item(std::string type);
UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color); UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color);
@@ -42,8 +43,14 @@ class UI_item {
virtual json to_json() const; virtual json to_json() const;
virtual void click() const {}; virtual void click() const {};
virtual void set_selected(std::string selected) {UNUSED(selected);}; virtual void set_selected(std::string selected)
virtual void set_value(double value) {UNUSED(value);}; {
UNUSED(selected);
};
virtual void set_value(double value)
{
UNUSED(value);
};
protected: protected:
std::string m_id; std::string m_id;
+1 -2
View File
@@ -5,8 +5,7 @@ UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_co
{ {
} }
UI_label::UI_label(const json& j) UI_label::UI_label(const json &j) : UI_item(UI_LABEL_TYPE)
: UI_item(UI_LABEL_TYPE)
{ {
from_json(j); from_json(j);
} }
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include <string>
#include "UI_item.hpp" #include "UI_item.hpp"
#include <string>
#define UI_LABEL_TYPE std::string("UI_label") #define UI_LABEL_TYPE std::string("UI_label")
+3 -6
View File
@@ -1,14 +1,11 @@
#include "UI_led.hpp" #include "UI_led.hpp"
UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color) : UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color)
UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color), : UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color), m_led_color(led_color)
m_led_color(led_color)
{ {
} }
UI_led::UI_led(const json& j) : UI_led::UI_led(const json &j) : UI_item(UI_LED_TYPE)
UI_item(UI_LED_TYPE)
{ {
from_json(j); from_json(j);
} }
+2 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include <string>
#include "UI_item.hpp" #include "UI_item.hpp"
#include <string>
#define UI_LED_TYPE std::string("UI_led") #define UI_LED_TYPE std::string("UI_led")
@@ -15,6 +15,7 @@ public:
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
Color m_led_color; Color m_led_color;
}; };
+3 -4
View File
@@ -1,14 +1,13 @@
#include "UI_slider.hpp" #include "UI_slider.hpp"
UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, std::function<void(std::string, double)> on_change) UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max,
double value, std::function<void(std::string, double)> on_change)
: UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value) : UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value)
{ {
this->m_on_change = on_change; this->m_on_change = on_change;
} }
UI_slider::UI_slider(const json& j) UI_slider::UI_slider(const json &j) : UI_item(UI_SLIDER_TYPE), m_on_change(nullptr)
: UI_item(UI_SLIDER_TYPE),
m_on_change(nullptr)
{ {
from_json(j); from_json(j);
} }
+3 -1
View File
@@ -6,7 +6,8 @@
class UI_slider : public UI_item class UI_slider : public UI_item
{ {
public: public:
UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, std::function<void(std::string, double)> on_change = nullptr); UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value,
std::function<void(std::string, double)> on_change = nullptr);
UI_slider(const json &j); UI_slider(const json &j);
void set_value(double value) override; void set_value(double value) override;
@@ -17,6 +18,7 @@ class UI_slider : public UI_item
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
double m_min; double m_min;
double m_max; double m_max;
+3 -4
View File
@@ -1,13 +1,12 @@
#include "UI_textbox.hpp" #include "UI_textbox.hpp"
UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function<void(std::string, std::string)> on_change) UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color,
std::function<void(std::string, std::string)> on_change)
: UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color), m_hint(hint), m_on_change(on_change) : UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color), m_hint(hint), m_on_change(on_change)
{ {
} }
UI_textbox::UI_textbox(const json& j) UI_textbox::UI_textbox(const json &j) : UI_item(UI_TEXTBOX_TYPE), m_on_change(nullptr)
: UI_item(UI_TEXTBOX_TYPE),
m_on_change(nullptr)
{ {
from_json(j); from_json(j);
} }
+4 -2
View File
@@ -1,13 +1,14 @@
#pragma once #pragma once
#include <string>
#include "UI_item.hpp" #include "UI_item.hpp"
#include <string>
#define UI_TEXTBOX_TYPE std::string("UI_textbox") #define UI_TEXTBOX_TYPE std::string("UI_textbox")
class UI_textbox : public UI_item class UI_textbox : public UI_item
{ {
public: public:
UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function<void(std::string, std::string)> on_change = nullptr); UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color,
std::function<void(std::string, std::string)> on_change = nullptr);
UI_textbox(const json &j); UI_textbox(const json &j);
void set_text(std::string text) override; void set_text(std::string text) override;
@@ -17,6 +18,7 @@ public:
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
std::string m_hint; std::string m_hint;
std::function<void(std::string, std::string)> m_on_change; std::function<void(std::string, std::string)> m_on_change;
+3 -3
View File
@@ -1,12 +1,12 @@
#include "UI_thermo.hpp" #include "UI_thermo.hpp"
UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value) UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max,
double value)
: UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value) : UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value)
{ {
} }
UI_thermo::UI_thermo(const json& j) UI_thermo::UI_thermo(const json &j) : UI_item(UI_THERMO_TYPE)
: UI_item(UI_THERMO_TYPE)
{ {
from_json(j); from_json(j);
} }
+1
View File
@@ -25,6 +25,7 @@ class UI_thermo : public UI_item
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
private: private:
double m_min; double m_min;
double m_max; double m_max;
+3 -3
View File
@@ -1,11 +1,11 @@
#include "UI_x_boxes.hpp" #include "UI_x_boxes.hpp"
UI_x_boxes::UI_x_boxes(std::string type) UI_x_boxes::UI_x_boxes(std::string type) : UI_item(type), m_options()
: UI_item(type), m_options()
{ {
} }
UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options) UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options)
: UI_item(id, type, text, 12, fg_color, bg_color), m_options(options) : UI_item(id, type, text, 12, fg_color, bg_color), m_options(options)
{ {
} }
+3 -1
View File
@@ -6,10 +6,12 @@ class UI_x_boxes : public UI_item
{ {
public: public:
UI_x_boxes(std::string type); UI_x_boxes(std::string type);
UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options); UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
protected: protected:
std::vector<std::string> m_options; std::vector<std::string> m_options;
}; };
+1 -1
View File
@@ -1,5 +1,5 @@
#include "gtest/gtest.h"
#include "UI.hpp" #include "UI.hpp"
#include "gtest/gtest.h"
#include <iostream> #include <iostream>
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
+14 -7
View File
@@ -3,26 +3,30 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "spdlog/spdlog.h"
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include "message_parser.hpp"
#include "simulators.hpp" #include "simulators.hpp"
#include "websocket.hpp" #include "websocket.hpp"
#include "message_parser.hpp"
class Spdlog_buffer : public std::stringbuf class Spdlog_buffer : public std::stringbuf
{ {
public: public:
Spdlog_buffer(std::function <void(std::string)> log_function) : log_function(log_function) {} Spdlog_buffer(std::function<void(std::string)> log_function) : log_function(log_function)
int sync() { {
}
int sync()
{
std::string str = this->str(); std::string str = this->str();
str.pop_back(); str.pop_back();
log_function(str); log_function(str);
this->str(""); this->str("");
return 0; return 0;
} }
private: private:
std::function<void(std::string)> log_function; std::function<void(std::string)> log_function;
}; };
@@ -55,7 +59,8 @@ void init_logger()
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10, 3); auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10, 3);
std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink}; std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink};
auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(),
spdlog::async_overflow_policy::block);
spdlog::set_default_logger(logger); spdlog::set_default_logger(logger);
spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v");
spdlog::flush_every(std::chrono::seconds(3)); spdlog::flush_every(std::chrono::seconds(3));
@@ -81,7 +86,9 @@ int main(void)
Simulators simulators; Simulators simulators;
g_simulators = &simulators; g_simulators = &simulators;
Websocket web_socket(9002, &g_out, &g_err, [](std::string message) { return message_parser(message); }, []() { return g_simulators->changed_UI_items().dump(); }); Websocket web_socket(
9002, &g_out, &g_err, [](std::string message) { return message_parser(message); },
[]() { return g_simulators->changed_UI_items().dump(); });
g_web_socket = &web_socket; g_web_socket = &web_socket;
// Start the message processor and broadcast processor in separate threads // Start the message processor and broadcast processor in separate threads
+17 -15
View File
@@ -1,6 +1,6 @@
#include "message_parser.hpp" #include "message_parser.hpp"
#include <magic_enum.hpp>
#include <iostream> #include <iostream>
#include <magic_enum.hpp>
extern Simulators *g_simulators; extern Simulators *g_simulators;
@@ -34,7 +34,8 @@ json command_parser(json command)
case Command::get_UI_element: case Command::get_UI_element:
spdlog::info("Command {}: {}", type, std::string(command.at("id"))); spdlog::info("Command {}: {}", type, std::string(command.at("id")));
j["response"]["type"] = type; j["response"]["type"] = type;
j["response"]["UI_item"] = g_simulators->invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(); j["response"]["UI_item"] =
g_simulators->invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
break; break;
case Command::get_UI_elements: case Command::get_UI_elements:
spdlog::info("Command {}", type); spdlog::info("Command {}", type);
@@ -82,32 +83,33 @@ void event_handler(json event)
switch (e) switch (e)
{ {
case Event::clicked: case Event::clicked: {
{
spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); spdlog::debug("Event {}: {}", type, std::string(event.at("id")));
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click(); g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click();
break; break;
} }
case Event::value_changed: case Event::value_changed: {
{
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"]));
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value")); g_simulators->invoke_active_simulator()
->get_UI_item(std::string(event.at("id")))
->set_value((double)event.at("value"));
break; break;
} }
case Event::text_changed: case Event::text_changed: {
{
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text"))); spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text")));
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_text(std::string(event.at("text"))); g_simulators->invoke_active_simulator()
->get_UI_item(std::string(event.at("id")))
->set_text(std::string(event.at("text")));
break; break;
} }
case Event::selected: case Event::selected: {
{
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected"))); spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected")));
g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_selected(std::string(event.at("selected"))); g_simulators->invoke_active_simulator()
->get_UI_item(std::string(event.at("id")))
->set_selected(std::string(event.at("selected")));
break; break;
} }
default: default: {
{
break; break;
} }
} }
+26 -14
View File
@@ -3,18 +3,21 @@
Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval) Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval)
: m_name(name), m_timer([this] { timer(); }), m_interval(interval) : m_name(name), m_timer([this] { timer(); }), m_interval(interval)
{ {
} }
std::string Simulator_base::name() const { std::string Simulator_base::name() const
{
// Get the name of the simulator // Get the name of the simulator
return m_name; return m_name;
} }
bool Simulator_base::is_ui_id_unique(std::string id) const { bool Simulator_base::is_ui_id_unique(std::string id) const
{
// Check if the UI name is unique // Check if the UI name is unique
for (UI_item* item : m_UI_items) { for (UI_item *item : m_UI_items)
if (item->id() == id) { {
if (item->id() == id)
{
return false; return false;
} }
} }
@@ -22,48 +25,57 @@ bool Simulator_base::is_ui_id_unique(std::string id) const {
return true; return true;
} }
void Simulator_base::add_UI_item(UI_item* item) { void Simulator_base::add_UI_item(UI_item *item)
{
// Add a UI item to the simulator // Add a UI item to the simulator
if (is_ui_id_unique(item->id()) == false) { if (is_ui_id_unique(item->id()) == false)
{
throw std::runtime_error("UI item id " + item->id() + " is not unique"); throw std::runtime_error("UI item id " + item->id() + " is not unique");
} }
m_UI_items_map[item->id()] = item; m_UI_items_map[item->id()] = item;
m_UI_items.push_back(item); m_UI_items.push_back(item);
} }
json Simulator_base::get_UI_items() const { json Simulator_base::get_UI_items() const
{
// Get the UI items // Get the UI items
json items; json items;
items["name"] = m_name; items["name"] = m_name;
for (UI_item* item : m_UI_items) { for (UI_item *item : m_UI_items)
{
items["UI_items"].push_back(item->to_json()); items["UI_items"].push_back(item->to_json());
} }
return items; return items;
} }
UI_item* Simulator_base::get_UI_item(std::string id) const { UI_item *Simulator_base::get_UI_item(std::string id) const
{
// Get a UI item by id // Get a UI item by id
return m_UI_items_map.at(id); return m_UI_items_map.at(id);
} }
void Simulator_base::run() { void Simulator_base::run()
{
// Start the timer // Start the timer
m_timer.setInterval(m_interval); m_timer.setInterval(m_interval);
run_at_startup(); run_at_startup();
} }
void Simulator_base::stop() { void Simulator_base::stop()
{
// Stop the simulator // Stop the simulator
m_timer.stop(); m_timer.stop();
} }
void Simulator_base::shutdown() { void Simulator_base::shutdown()
{
// Shutdown the simulator // Shutdown the simulator
system("shutdown -h now"); system("shutdown -h now");
} }
void Simulator_base::reboot() { void Simulator_base::reboot()
{
// Reboot the simulator // Reboot the simulator
system("reboot"); system("reboot");
} }
+4 -2
View File
@@ -1,10 +1,11 @@
#pragma once #pragma once
#include "UI.hpp" #include "UI.hpp"
#include "timer.hpp"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "timer.hpp"
class Simulator_base { class Simulator_base
{
public: public:
/** /**
@@ -81,6 +82,7 @@ class Simulator_base {
void shutdown(); void shutdown();
void reboot(); void reboot();
protected: protected:
std::string m_name; std::string m_name;
std::vector<UI_item *> m_UI_items; std::vector<UI_item *> m_UI_items;
+2 -2
View File
@@ -1,7 +1,7 @@
#include "simulators.hpp" #include "simulators.hpp"
#include <unordered_set>
#include "test_sim.hpp"
#include "can_debugger.hpp" #include "can_debugger.hpp"
#include "test_sim.hpp"
#include <unordered_set>
Simulators::Simulators() Simulators::Simulators()
{ {
+4 -2
View File
@@ -1,13 +1,14 @@
#pragma once #pragma once
#include <memory>
#include <map> #include <map>
#include <memory>
#include <mutex> #include <mutex>
#include "simulator_base.hpp" #include "simulator_base.hpp"
#define INSERT_SIMULATOR(type) m_simulators.insert(std::make_pair(type().name(), std::make_unique<type>())); #define INSERT_SIMULATOR(type) m_simulators.insert(std::make_pair(type().name(), std::make_unique<type>()));
class Simulators { class Simulators
{
public: public:
Simulators(); Simulators();
std::string active_simulator_name() const; std::string active_simulator_name() const;
@@ -17,6 +18,7 @@ class Simulators {
std::vector<std::string> list_simulators() const; std::vector<std::string> list_simulators() const;
Simulator_base *invoke_active_simulator(); Simulator_base *invoke_active_simulator();
json changed_UI_items(); json changed_UI_items();
private: private:
std::map<std::string, std::unique_ptr<Simulator_base>> m_simulators; std::map<std::string, std::unique_ptr<Simulator_base>> m_simulators;
std::string m_current_simulator = ""; std::string m_current_simulator = "";
+13 -14
View File
@@ -2,8 +2,7 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
Websocket::Websocket(uint16_t port, std::ostream *out, std::ostream *err, Websocket::Websocket(uint16_t port, std::ostream *out, std::ostream *err,
std::function<std::string(std::string)> on_message, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update)
std::function<std::string()> on_update)
: m_on_message(on_message), m_on_update(on_update), m_port(port) : m_on_message(on_message), m_on_update(on_update), m_port(port)
{ {
// Set logging settings // Set logging settings
@@ -11,10 +10,8 @@ Websocket::Websocket(uint16_t port, std::ostream* out, std::ostream* err,
m_server.get_elog().set_custom(err != &std::cerr); m_server.get_elog().set_custom(err != &std::cerr);
m_server.get_alog().set_ostream(out); m_server.get_alog().set_ostream(out);
m_server.get_elog().set_ostream(err); m_server.get_elog().set_ostream(err);
m_server.set_access_channels(websocketpp::log::alevel::connect + m_server.set_access_channels(websocketpp::log::alevel::connect + websocketpp::log::alevel::disconnect);
websocketpp::log::alevel::disconnect); m_server.clear_access_channels(websocketpp::log::alevel::frame_payload + websocketpp::log::alevel::frame_header);
m_server.clear_access_channels(websocketpp::log::alevel::frame_payload +
websocketpp::log::alevel::frame_header);
// Initialize Asio // Initialize Asio
m_server.init_asio(); m_server.init_asio();
@@ -35,10 +32,14 @@ void Websocket::run()
m_server.start_accept(); m_server.start_accept();
// Start the ASIO io_service run loop // Start the ASIO io_service run loop
m_server.run(); m_server.run();
} catch (websocketpp::exception const& e) { }
catch (websocketpp::exception const &e)
{
spdlog::critical("{}", e.what()); spdlog::critical("{}", e.what());
exit(1); exit(1);
} catch (...) { }
catch (...)
{
spdlog::critical("other exception"); spdlog::critical("other exception");
exit(1); exit(1);
} }
@@ -94,21 +95,19 @@ void Websocket::process_messages()
lock.unlock(); lock.unlock();
switch (a.type) { switch (a.type)
case SUBSCRIBE:
{ {
case SUBSCRIBE: {
std::lock_guard<std::mutex> guard(m_connection_lock); std::lock_guard<std::mutex> guard(m_connection_lock);
m_connections.insert(a.hdl); m_connections.insert(a.hdl);
} }
break; break;
case UNSUBSCRIBE: case UNSUBSCRIBE: {
{
std::lock_guard<std::mutex> guard(m_connection_lock); std::lock_guard<std::mutex> guard(m_connection_lock);
m_connections.erase(a.hdl); m_connections.erase(a.hdl);
} }
break; break;
case MESSAGE: case MESSAGE: {
{
std::string response = m_on_message(a.msg->get_payload()); std::string response = m_on_message(a.msg->get_payload());
if (response.empty() || response == "{}") if (response.empty() || response == "{}")
{ {
+14 -6
View File
@@ -16,17 +16,22 @@ using websocketpp::lib::placeholders::_2;
* on_message queue send to all channels * on_message queue send to all channels
*/ */
enum action_type { enum action_type
{
SUBSCRIBE, SUBSCRIBE,
UNSUBSCRIBE, UNSUBSCRIBE,
MESSAGE, MESSAGE,
BROADCAST BROADCAST
}; };
struct action { struct action
action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {} {
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h)
: type(t), hdl(h), msg(m) {} {
}
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) : type(t), hdl(h), msg(m)
{
}
action_type type; action_type type;
websocketpp::connection_hdl hdl; websocketpp::connection_hdl hdl;
@@ -36,15 +41,18 @@ struct action {
class Websocket class Websocket
{ {
public: public:
Websocket(uint16_t port, std::ostream* out, std::ostream* err, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update); Websocket(uint16_t port, std::ostream *out, std::ostream *err, std::function<std::string(std::string)> on_message,
std::function<std::string()> on_update);
void run(); void run();
void stop(); void stop();
void process_messages(); void process_messages();
void process_broadcast(); void process_broadcast();
private: private:
void on_open(websocketpp::connection_hdl hdl); void on_open(websocketpp::connection_hdl hdl);
void on_close(websocketpp::connection_hdl hdl); void on_close(websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg); void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg);
private: private:
std::function<std::string(std::string)> m_on_message; std::function<std::string(std::string)> m_on_message;
std::function<std::string()> m_on_update; std::function<std::string()> m_on_update;