- training_area_telegrams.cpp: Fixed QJsonObject initializer syntax (Qt6 requires {{}} not ({}))
- training_area_telegrams.cpp: Fixed fromJson() methods to access m_* members directly
- training_area_telegrams.cpp: Fixed brace mismatches in multiple toJson() methods
- training_area_telegrams.h: Changed Header struct to public members for consistency
- tx_mixin.cpp: Fixed getEquipmentNumber() template for direct member access
- lts_coordinate.cpp: Added missing #include <iomanip>
- MIGRATION_STATUS.md: Added migration progress tracking document
C++17 constraint enforced due to RTI Connext DDS 7.7.0 incompatibility with C++20+
Build status: BUILD_RTI=OFF successful (0 errors, warnings only)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
117 lines
2.9 KiB
C++
Executable File
117 lines
2.9 KiB
C++
Executable File
/**
|
|
* Websocket interface to participant positions
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Justus Hertel, SLSE, 2020-07-08
|
|
*/
|
|
|
|
#include "web_socket_handler.h"
|
|
#include <utility>
|
|
|
|
#include <QtWebSockets/QWebSocket>
|
|
#include <QtCore/QJsonObject>
|
|
#include <QtCore/QJsonDocument>
|
|
|
|
#include <ParticipantManagement/ppm.hpp>
|
|
|
|
namespace ParticipantManagement
|
|
{
|
|
|
|
WebSocketHandler::WebSocketHandler(QObject *parent)
|
|
: QObject(parent)
|
|
, m_webSocketServer(new QWebSocketServer(QStringLiteral("ParticipantPositionsSocket"), QWebSocketServer::NonSecureMode, this))
|
|
, m_logger(getLogger(QStringLiteral("WebSocketHandler")))
|
|
{}
|
|
|
|
WebSocketHandler::~WebSocketHandler()
|
|
{
|
|
for (const auto& ws : std::as_const(m_webSockets))
|
|
{
|
|
ws->disconnect(this);
|
|
}
|
|
m_webSockets.clear();
|
|
}
|
|
|
|
bool WebSocketHandler::init(quint16 listenPort)
|
|
{
|
|
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
|
|
{
|
|
WARNING_LOG(m_logger) << "Can not listen on port " << listenPort << " - aborting";
|
|
return false;
|
|
}
|
|
|
|
connect(m_webSocketServer, &QWebSocketServer::newConnection,
|
|
this, &WebSocketHandler::onNewConnection);
|
|
|
|
INFO_LOG(m_logger) << "Listening on port " << m_webSocketServer->serverPort();
|
|
return true;
|
|
}
|
|
|
|
bool WebSocketHandler::init(const QPair<quint16, quint16> &portRange)
|
|
{
|
|
for (quint16 currentPort = portRange.first; currentPort <= portRange.second; ++currentPort)
|
|
{
|
|
if (init(currentPort))
|
|
return true;
|
|
}
|
|
|
|
return init();
|
|
}
|
|
|
|
bool WebSocketHandler::sendTelegramInternal(const ParticipantPosition &pos, int eqNo) const
|
|
{
|
|
QJsonObject obj;
|
|
obj[QStringLiteral("participantId")] = pos.participantId();
|
|
obj[QStringLiteral("equipmentNumber")] = eqNo;
|
|
obj[QStringLiteral("lat")] = pos.pos().lat();
|
|
obj[QStringLiteral("lon")] = pos.pos().lon();
|
|
obj[QStringLiteral("height")] = pos.pos().z();
|
|
|
|
const QString jsonStr = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact));
|
|
|
|
bool ret = false;
|
|
for (const auto &wsw : std::as_const(m_webSockets))
|
|
{
|
|
if (wsw->sendTextMessage(jsonStr))
|
|
ret = true;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void WebSocketHandler::onNewConnection()
|
|
{
|
|
QWebSocket *ws = m_webSocketServer->nextPendingConnection();
|
|
if (!ws)
|
|
return;
|
|
|
|
connect(ws, &QWebSocket::disconnected,
|
|
this, &WebSocketHandler::onWebSocketDisconnected);
|
|
|
|
m_webSockets += ws;
|
|
|
|
INFO_LOG(m_logger) << "New websocket connected";
|
|
}
|
|
|
|
void WebSocketHandler::onWebSocketDisconnected()
|
|
{
|
|
QWebSocket *ws = qobject_cast<QWebSocket *>(sender());
|
|
if (!ws)
|
|
return;
|
|
|
|
m_webSockets.removeAll(ws);
|
|
ws->deleteLater();
|
|
|
|
INFO_LOG(m_logger) << "A websocket disconnected";
|
|
}
|
|
|
|
bool WebSocketHandler::sendPosition(const ParticipantPosition &participantPosition, int equipmentNumber) const
|
|
{
|
|
if (!isConnected() || m_webSockets.isEmpty())
|
|
return false;
|
|
|
|
return sendTelegramInternal(participantPosition, equipmentNumber);
|
|
}
|
|
|
|
}
|