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
+4 -3
View File
@@ -1,6 +1,6 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCoreApplication>
#include "spdlog/async.h"
#include "spdlog/sinks/rotating_file_sink.h"
@@ -32,7 +32,8 @@ int main(int argc, char *argv[])
parser.setApplicationDescription("QtWebSockets example: echoserver");
parser.addHelpOption();
QCommandLineOption portOption(QStringList() << "p" << "port",
QCommandLineOption portOption(QStringList() << "p"
<< "port",
QCoreApplication::translate("main", "Port for echoserver [default: 9002]."),
QCoreApplication::translate("main", "port"), QLatin1String("9002"));
parser.addOption(portOption);
+2 -5
View File
@@ -32,8 +32,7 @@ json Simulators::command_parser(json command)
case Command::get_UI_element:
spdlog::info("Command {}: {}", type, std::string(command.at("id")));
j["response"]["type"] = type;
j["response"]["UI_item"] =
invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
break;
case Command::get_UI_elements:
spdlog::info("Command {}", type);
@@ -88,9 +87,7 @@ void Simulators::event_handler(json event)
}
case Event::value_changed: {
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"]));
invoke_active_simulator()
->get_UI_item(std::string(event.at("id")))
->set_value((double)event.at("value"));
invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value"));
break;
}
case Event::text_changed: {
+3 -3
View File
@@ -1,9 +1,9 @@
#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)
{
connect(m_timer, &QTimer::timeout, this, [this]{timer();});
connect(m_timer, &QTimer::timeout, this, [this] { timer(); });
}
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");
}
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.push_back(item);
}
+3 -2
View File
@@ -14,7 +14,8 @@ class Simulator_base : public QObject
* @param name The name of the simulator
* @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
@@ -89,6 +90,6 @@ class Simulator_base : public QObject
std::string m_name;
std::vector<UI_item *> m_UI_items;
std::map<std::string, UI_item *> m_UI_items_map;
QTimer* m_timer;
QTimer *m_timer;
std::chrono::milliseconds m_interval;
};
+5 -6
View File
@@ -1,12 +1,10 @@
#include "simulators.hpp"
#include "can_debugger.hpp"
#include "test_sim.hpp"
#include <unordered_set>
#include <QDebug>
#include <unordered_set>
Simulators::Simulators(uint16_t port, QObject* parrent) :
QObject(parrent),
m_server(new Websocket(port, this))
Simulators::Simulators(uint16_t port, QObject *parrent) : QObject(parrent), m_server(new Websocket(port, this))
{
qDebug() << port;
INSERT_SIMULATOR(Test_Sim);
@@ -16,7 +14,7 @@ Simulators::Simulators(uint16_t port, QObject* parrent) :
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);
if (response.isEmpty() || response == "{}")
return;
@@ -80,7 +78,8 @@ void Simulators::switch_simulator(std::string name)
disconnect(m_simulators.at(m_current_simulator).get());
}
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();
}
+2 -2
View File
@@ -3,8 +3,8 @@
#include <memory>
#include <mutex>
#include "websocket.hpp"
#include "simulator_base.hpp"
#include "websocket.hpp"
#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();
json changed_UI_items();
//parsing of websocket messages
// parsing of websocket messages
QString message_parser(QString message);
json command_parser(json command);
void event_handler(json event);
+12 -11
View File
@@ -1,17 +1,16 @@
#include "websocket.hpp"
#include "QtWebSockets/qwebsocketserver.h"
#include "QtWebSockets/qwebsocket.h"
#include "QtWebSockets/qwebsocketserver.h"
#include <spdlog/spdlog.h>
Websocket::Websocket(uint16_t port, QObject *parent) :
QObject(parent),
m_pWebSocketServer(new QWebSocketServer(QStringLiteral("sim_us Server"),
QWebSocketServer::NonSecureMode, this))
Websocket::Websocket(uint16_t port, QObject *parent)
: QObject(parent),
m_pWebSocketServer(new QWebSocketServer(QStringLiteral("sim_us Server"), QWebSocketServer::NonSecureMode, this))
{
if (m_pWebSocketServer->listen(QHostAddress::Any, port)) {
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
spdlog::debug("Echoserver listening on port {}", port);
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &Websocket::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &Websocket::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &Websocket::closed);
}
}
@@ -24,12 +23,13 @@ Websocket::~Websocket()
void Websocket::broadcast(QString message)
{
foreach (QWebSocket* conn, m_clients) {
foreach (QWebSocket *conn, m_clients)
{
conn->sendTextMessage(message);
}
}
void Websocket::send(QWebSocket* conn, QString message)
void Websocket::send(QWebSocket *conn, QString message)
{
conn->sendTextMessage(message);
spdlog::trace("sending message: {}", message.toStdString());
@@ -66,7 +66,8 @@ void Websocket::socketDisconnected()
{
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
spdlog::debug("socket Disconnected");
if (pClient) {
if (pClient)
{
m_clients.removeAll(pClient);
pClient->deleteLater();
}
+4 -4
View File
@@ -1,8 +1,8 @@
#pragma once
#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QObject>
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket)
@@ -16,9 +16,9 @@ class Websocket : public QObject
~Websocket();
void broadcast(QString message);
void send(QWebSocket* conn, QString message);
void send(QWebSocket *conn, QString message);
signals:
void on_message(QWebSocket* conn, QString message);
void on_message(QWebSocket *conn, QString message);
void closed();
private slots: