This commit is contained in:
2024-09-12 20:43:12 +02:00
parent 67bcf10b16
commit 756f69e9c7
30 changed files with 98 additions and 95 deletions
+2 -3
View File
@@ -1,12 +1,11 @@
#include "UI_button.hpp" #include "UI_button.hpp"
UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject* parrent) UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent)
: UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color, parrent) : UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color, parrent)
{ {
} }
UI_button::UI_button(const json &j, QObject* parrent) : UI_item(UI_BUTTON_TYPE, parrent) UI_button::UI_button(const json &j, QObject *parrent) : UI_item(UI_BUTTON_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+2 -2
View File
@@ -8,8 +8,8 @@ class UI_button : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject* parrent = nullptr); UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent = nullptr);
UI_button(const json &j, QObject* parrent = nullptr); UI_button(const json &j, QObject *parrent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
+3 -3
View File
@@ -1,11 +1,11 @@
#include "UI_checkbox.hpp" #include "UI_checkbox.hpp"
UI_checkbox::UI_checkbox(QObject* parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parrent), m_selected() UI_checkbox::UI_checkbox(QObject *parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parrent), m_selected()
{ {
} }
UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, QObject* parrent) std::vector<std::string> options, QObject *parrent)
: UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected() : UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected()
{ {
for (size_t i = 0; i < m_options.size(); i++) for (size_t i = 0; i < m_options.size(); i++)
@@ -14,7 +14,7 @@ UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color
} }
} }
UI_checkbox::UI_checkbox(const json &j, QObject* parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parrent), m_selected() UI_checkbox::UI_checkbox(const json &j, QObject *parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parrent), m_selected()
{ {
from_json(j); from_json(j);
} }
+4 -3
View File
@@ -9,9 +9,10 @@ class UI_checkbox : public UI_x_boxes
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_checkbox(QObject* parrent = nullptr); UI_checkbox(QObject *parrent = nullptr);
UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, QObject* parrent = nullptr); UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
UI_checkbox(const json &j, QObject* parrent = nullptr); QObject *parrent = nullptr);
UI_checkbox(const json &j, QObject *parrent = nullptr);
void toggle_selected(size_t selected); void toggle_selected(size_t selected);
void toggle_selected(std::string selected); void toggle_selected(std::string selected);
+3 -3
View File
@@ -1,16 +1,16 @@
#include "UI_combobox.hpp" #include "UI_combobox.hpp"
UI_combobox::UI_combobox(QObject* parrent) : UI_x_boxes(UI_COMBOBOX_TYPE, parrent) UI_combobox::UI_combobox(QObject *parrent) : UI_x_boxes(UI_COMBOBOX_TYPE, parrent)
{ {
} }
UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, size_t selected, QObject* parrent) std::vector<std::string> options, size_t selected, QObject *parrent)
: UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected(selected) : UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected(selected)
{ {
} }
UI_combobox::UI_combobox(const json &j, QObject* parrent) : UI_x_boxes(UI_COMBOBOX_TYPE, parrent) UI_combobox::UI_combobox(const json &j, QObject *parrent) : UI_x_boxes(UI_COMBOBOX_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+4 -3
View File
@@ -10,10 +10,10 @@ class UI_combobox : public UI_x_boxes
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_combobox(QObject* parrent = nullptr); UI_combobox(QObject *parrent = nullptr);
UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
size_t selected = 0, QObject* parrent = nullptr); size_t selected = 0, QObject *parrent = nullptr);
UI_combobox(const json &j, QObject* parrent = nullptr); UI_combobox(const json &j, QObject *parrent = nullptr);
void set_selected(size_t selected); void set_selected(size_t selected);
void set_selected(std::string selected) override; void set_selected(std::string selected) override;
@@ -24,6 +24,7 @@ class UI_combobox : public UI_x_boxes
json to_json() const override; json to_json() const override;
signals: signals:
void on_change(std::string id, std::string selected); void on_change(std::string id, std::string selected);
private: private:
size_t m_selected; size_t m_selected;
}; };
+3 -2
View File
@@ -1,11 +1,12 @@
#include "UI_dial.hpp" #include "UI_dial.hpp"
UI_dial::UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject* parrent) UI_dial::UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parrent)
: UI_item(id, UI_DIAL_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) : UI_item(id, UI_DIAL_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value)
{ {
} }
UI_dial::UI_dial(const json &j, QObject* parrent) : UI_item(UI_DIAL_TYPE, parrent) UI_dial::UI_dial(const json &j, QObject *parrent) : UI_item(UI_DIAL_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+3 -2
View File
@@ -7,8 +7,9 @@ class UI_dial : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject* parrent = nullptr); UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value,
UI_dial(const json &j, QObject* parrent = nullptr); QObject *parrent = nullptr);
UI_dial(const json &j, QObject *parrent = nullptr);
void set_value(double value) override; void set_value(double value) override;
double value() const; double value() const;
+8 -10
View File
@@ -1,21 +1,19 @@
#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, QObject* parrent) UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color,
: QObject(parrent), QObject *parrent)
m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color), : QObject(parrent), m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color),
enabled(true), visible(true) m_bg_color(bg_color), enabled(true), visible(true)
{ {
} }
UI_item::UI_item(std::string type, QObject* parrent) UI_item::UI_item(std::string type, QObject *parrent)
: QObject(parrent), : QObject(parrent), m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black),
m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White), m_bg_color(Color::White), enabled(true), visible(true)
enabled(true), visible(true)
{ {
} }
UI_item::UI_item(const json &j, QObject* parrent) : UI_item::UI_item(const json &j, QObject *parrent) : QObject(parrent)
QObject(parrent)
{ {
from_json(j); from_json(j);
} }
+4 -3
View File
@@ -15,9 +15,10 @@ class UI_item : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit UI_item(std::string type, QObject* parrent = nullptr); explicit UI_item(std::string type, QObject *parrent = nullptr);
explicit UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, QObject* parrent = nullptr); explicit UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color,
explicit UI_item(const json &j, QObject* parrent = nullptr); Color bg_color, QObject *parrent = nullptr);
explicit UI_item(const json &j, QObject *parrent = nullptr);
~UI_item(); ~UI_item();
std::string id() const; std::string id() const;
+2 -2
View File
@@ -1,11 +1,11 @@
#include "UI_label.hpp" #include "UI_label.hpp"
UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject* parrent) UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent)
: UI_item(id, UI_LABEL_TYPE, text, 12, fg_color, bg_color, parrent) : UI_item(id, UI_LABEL_TYPE, text, 12, fg_color, bg_color, parrent)
{ {
} }
UI_label::UI_label(const json &j, QObject* parrent) : UI_item(UI_LABEL_TYPE, parrent) UI_label::UI_label(const json &j, QObject *parrent) : UI_item(UI_LABEL_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+2 -2
View File
@@ -8,8 +8,8 @@ class UI_label : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject* parrent = nullptr); UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent = nullptr);
UI_label(const json &j, QObject* parrent = nullptr); UI_label(const json &j, QObject *parrent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
+2 -2
View File
@@ -1,11 +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, QObject* parrent) UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color, QObject *parrent)
: UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color, parrent), m_led_color(led_color) : UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color, parrent), m_led_color(led_color)
{ {
} }
UI_led::UI_led(const json &j, QObject* parrent) : UI_item(UI_LED_TYPE, parrent) UI_led::UI_led(const json &j, QObject *parrent) : UI_item(UI_LED_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+3 -2
View File
@@ -8,8 +8,9 @@ class UI_led : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color, QObject* parrent = nullptr); UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color,
UI_led(const json &j, QObject* parrent = nullptr); QObject *parrent = nullptr);
UI_led(const json &j, QObject *parrent = nullptr);
Color led_color() const; Color led_color() const;
void set_led_color(Color led_color); void set_led_color(Color led_color);
+2 -3
View File
@@ -1,13 +1,12 @@
#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, UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max,
double value, QObject* parrent) double value, QObject *parrent)
: UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) : UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value)
{ {
} }
UI_slider::UI_slider(const json &j, QObject* parrent) : UI_item(UI_SLIDER_TYPE, parrent) UI_slider::UI_slider(const json &j, QObject *parrent) : UI_item(UI_SLIDER_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+3 -2
View File
@@ -7,8 +7,9 @@ class UI_slider : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject* parrent = nullptr); UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value,
UI_slider(const json &j, QObject* parrent = nullptr); QObject *parrent = nullptr);
UI_slider(const json &j, QObject *parrent = nullptr);
void set_value(double value) override; void set_value(double value) override;
double value() const; double value() const;
+2 -2
View File
@@ -1,11 +1,11 @@
#include "UI_textbox.hpp" #include "UI_textbox.hpp"
UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject* parrent) UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject *parrent)
: UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color, parrent), m_hint(hint) : UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color, parrent), m_hint(hint)
{ {
} }
UI_textbox::UI_textbox(const json &j, QObject* parrent) : UI_item(UI_TEXTBOX_TYPE, parrent) UI_textbox::UI_textbox(const json &j, QObject *parrent) : UI_item(UI_TEXTBOX_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+2 -2
View File
@@ -8,8 +8,8 @@ class UI_textbox : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject* parrent = nullptr); UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject *parrent = nullptr);
UI_textbox(const json &j, QObject* parrent = nullptr); UI_textbox(const json &j, QObject *parrent = nullptr);
void set_text(std::string text) override; void set_text(std::string text) override;
+2 -2
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, UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max,
double value, QObject* parrent) double value, QObject *parrent)
: UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) : UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value)
{ {
} }
UI_thermo::UI_thermo(const json &j, QObject* parrent) : UI_item(UI_THERMO_TYPE, parrent) UI_thermo::UI_thermo(const json &j, QObject *parrent) : UI_item(UI_THERMO_TYPE, parrent)
{ {
from_json(j); from_json(j);
} }
+3 -2
View File
@@ -8,8 +8,9 @@ class UI_thermo : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject* parrent = nullptr); UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value,
UI_thermo(const json &j, QObject* parrent = nullptr); QObject *parrent = nullptr);
UI_thermo(const json &j, QObject *parrent = nullptr);
void set_value(double value) override; void set_value(double value) override;
double value() const; double value() const;
+2 -2
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, QObject* parrent) : UI_item(type, parrent), m_options() UI_x_boxes::UI_x_boxes(std::string type, QObject *parrent) : UI_item(type, parrent), m_options()
{ {
} }
UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, 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, QObject* parrent) std::vector<std::string> options, QObject *parrent)
: UI_item(id, type, text, 12, fg_color, bg_color, parrent), m_options(options) : UI_item(id, type, text, 12, fg_color, bg_color, parrent), m_options(options)
{ {
} }
+2 -2
View File
@@ -6,9 +6,9 @@ class UI_x_boxes : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_x_boxes(std::string type, QObject* parrent = nullptr); UI_x_boxes(std::string type, QObject *parrent = nullptr);
UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, QObject* parrent = nullptr); std::vector<std::string> options, QObject *parrent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json() const override;
+4 -3
View File
@@ -1,6 +1,6 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption> #include <QtCore/QCommandLineOption>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCoreApplication>
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h"
@@ -32,7 +32,8 @@ int main(int argc, char *argv[])
parser.setApplicationDescription("QtWebSockets example: echoserver"); parser.setApplicationDescription("QtWebSockets example: echoserver");
parser.addHelpOption(); parser.addHelpOption();
QCommandLineOption portOption(QStringList() << "p" << "port", QCommandLineOption portOption(QStringList() << "p"
<< "port",
QCoreApplication::translate("main", "Port for echoserver [default: 9002]."), QCoreApplication::translate("main", "Port for echoserver [default: 9002]."),
QCoreApplication::translate("main", "port"), QLatin1String("9002")); QCoreApplication::translate("main", "port"), QLatin1String("9002"));
parser.addOption(portOption); parser.addOption(portOption);
+2 -5
View File
@@ -32,8 +32,7 @@ json Simulators::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"] = j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
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);
@@ -88,9 +87,7 @@ void Simulators::event_handler(json event)
} }
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"]));
invoke_active_simulator() invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value"));
->get_UI_item(std::string(event.at("id")))
->set_value((double)event.at("value"));
break; break;
} }
case Event::text_changed: { case Event::text_changed: {
+3 -3
View File
@@ -1,9 +1,9 @@
#include "simulator_base.hpp" #include "simulator_base.hpp"
Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval, QObject* parrent) Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval, QObject *parrent)
: QObject(parrent), m_name(name), m_timer(new QTimer(this)), m_interval(interval) : QObject(parrent), m_name(name), m_timer(new QTimer(this)), m_interval(interval)
{ {
connect(m_timer, &QTimer::timeout, this, [this]{timer();}); connect(m_timer, &QTimer::timeout, this, [this] { timer(); });
} }
std::string Simulator_base::name() const std::string Simulator_base::name() const
@@ -33,7 +33,7 @@ void Simulator_base::add_UI_item(UI_item *item)
{ {
throw std::runtime_error("UI item id " + item->id() + " is not unique"); throw std::runtime_error("UI item id " + item->id() + " is not unique");
} }
connect(item, &UI_item::value_changed, this, [this]{emit sim_changed();}); connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); });
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);
} }
+3 -2
View File
@@ -14,7 +14,8 @@ class Simulator_base : public QObject
* @param name The name of the simulator * @param name The name of the simulator
* @param interval The interval of the timer (default is 100ms) * @param interval The interval of the timer (default is 100ms)
*/ */
Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100), QObject* parrent = nullptr); Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100),
QObject *parrent = nullptr);
/** /**
* @brief Returns the name of the simulator * @brief Returns the name of the simulator
@@ -89,6 +90,6 @@ class Simulator_base : public QObject
std::string m_name; std::string m_name;
std::vector<UI_item *> m_UI_items; std::vector<UI_item *> m_UI_items;
std::map<std::string, UI_item *> m_UI_items_map; std::map<std::string, UI_item *> m_UI_items_map;
QTimer* m_timer; QTimer *m_timer;
std::chrono::milliseconds m_interval; std::chrono::milliseconds m_interval;
}; };
+5 -6
View File
@@ -1,12 +1,10 @@
#include "simulators.hpp" #include "simulators.hpp"
#include "can_debugger.hpp" #include "can_debugger.hpp"
#include "test_sim.hpp" #include "test_sim.hpp"
#include <unordered_set>
#include <QDebug> #include <QDebug>
#include <unordered_set>
Simulators::Simulators(uint16_t port, QObject* parrent) : Simulators::Simulators(uint16_t port, QObject *parrent) : QObject(parrent), m_server(new Websocket(port, this))
QObject(parrent),
m_server(new Websocket(port, this))
{ {
qDebug() << port; qDebug() << port;
INSERT_SIMULATOR(Test_Sim); INSERT_SIMULATOR(Test_Sim);
@@ -16,7 +14,7 @@ Simulators::Simulators(uint16_t port, QObject* parrent) :
void Simulators::setup_connections() void Simulators::setup_connections()
{ {
connect(m_server, &Websocket::on_message, this, [this](QWebSocket* conn, QString message){ connect(m_server, &Websocket::on_message, this, [this](QWebSocket *conn, QString message) {
QString response = message_parser(message); QString response = message_parser(message);
if (response.isEmpty() || response == "{}") if (response.isEmpty() || response == "{}")
return; return;
@@ -80,7 +78,8 @@ void Simulators::switch_simulator(std::string name)
disconnect(m_simulators.at(m_current_simulator).get()); disconnect(m_simulators.at(m_current_simulator).get());
} }
this->m_current_simulator = name; this->m_current_simulator = name;
connect(m_simulators.at(m_current_simulator).get(), &Simulator_base::sim_changed, this, [this]{m_server->broadcast(QString::fromStdString(changed_UI_items().dump()));}); connect(m_simulators.at(m_current_simulator).get(), &Simulator_base::sim_changed, this,
[this] { m_server->broadcast(QString::fromStdString(changed_UI_items().dump())); });
this->run(); this->run();
} }
+2 -2
View File
@@ -3,8 +3,8 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include "websocket.hpp"
#include "simulator_base.hpp" #include "simulator_base.hpp"
#include "websocket.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>()));
@@ -44,7 +44,7 @@ class Simulators : public QObject
Simulator_base *invoke_active_simulator(); Simulator_base *invoke_active_simulator();
json changed_UI_items(); json changed_UI_items();
//parsing of websocket messages // parsing of websocket messages
QString message_parser(QString message); QString message_parser(QString message);
json command_parser(json command); json command_parser(json command);
void event_handler(json event); void event_handler(json event);
+12 -11
View File
@@ -1,17 +1,16 @@
#include "websocket.hpp" #include "websocket.hpp"
#include "QtWebSockets/qwebsocketserver.h"
#include "QtWebSockets/qwebsocket.h" #include "QtWebSockets/qwebsocket.h"
#include "QtWebSockets/qwebsocketserver.h"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
Websocket::Websocket(uint16_t port, QObject *parent) : Websocket::Websocket(uint16_t port, QObject *parent)
QObject(parent), : QObject(parent),
m_pWebSocketServer(new QWebSocketServer(QStringLiteral("sim_us Server"), m_pWebSocketServer(new QWebSocketServer(QStringLiteral("sim_us Server"), QWebSocketServer::NonSecureMode, this))
QWebSocketServer::NonSecureMode, this))
{ {
if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
spdlog::debug("Echoserver listening on port {}", port); spdlog::debug("Echoserver listening on port {}", port);
connect(m_pWebSocketServer, &QWebSocketServer::newConnection, connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &Websocket::onNewConnection);
this, &Websocket::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &Websocket::closed); connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &Websocket::closed);
} }
} }
@@ -24,12 +23,13 @@ Websocket::~Websocket()
void Websocket::broadcast(QString message) void Websocket::broadcast(QString message)
{ {
foreach (QWebSocket* conn, m_clients) { foreach (QWebSocket *conn, m_clients)
{
conn->sendTextMessage(message); conn->sendTextMessage(message);
} }
} }
void Websocket::send(QWebSocket* conn, QString message) void Websocket::send(QWebSocket *conn, QString message)
{ {
conn->sendTextMessage(message); conn->sendTextMessage(message);
spdlog::trace("sending message: {}", message.toStdString()); spdlog::trace("sending message: {}", message.toStdString());
@@ -66,7 +66,8 @@ void Websocket::socketDisconnected()
{ {
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
spdlog::debug("socket Disconnected"); spdlog::debug("socket Disconnected");
if (pClient) { if (pClient)
{
m_clients.removeAll(pClient); m_clients.removeAll(pClient);
pClient->deleteLater(); pClient->deleteLater();
} }
+4 -4
View File
@@ -1,8 +1,8 @@
#pragma once #pragma once
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QObject>
QT_FORWARD_DECLARE_CLASS(QWebSocketServer) QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket) QT_FORWARD_DECLARE_CLASS(QWebSocket)
@@ -16,9 +16,9 @@ class Websocket : public QObject
~Websocket(); ~Websocket();
void broadcast(QString message); void broadcast(QString message);
void send(QWebSocket* conn, QString message); void send(QWebSocket *conn, QString message);
signals: signals:
void on_message(QWebSocket* conn, QString message); void on_message(QWebSocket *conn, QString message);
void closed(); void closed();
private slots: private slots: