Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Proxies/WirelessLocationSystem/web_socket_handler.cpp
T
torstenandCopilot b774f4035c Qt6 Migration: DDS-Stubs, Logger-Fixes, Export-Header Korrekturen
- NGL_ADD_LIBRARY2: 87 CMakeLists.txt konvertiert für Export-Header Generierung
- 44 Export-Header Include-Pfade korrigiert (_export.h)
- 81 DDS-Stub-Header generiert (*_dtc.h, *_fts.h, *_dts.h, *_ftc.h)
- INGLLogger: Duplikat gelöscht, m_logger Member entfernt, variadische Makros
- Q_FUNC_INFO Redefinition behoben (Qt6 definiert bereits)
- FATAL_LOG Makro hinzugefügt
- InfrastructureManagement DDS-Stubs (ism.hpp, ism_enums.hpp, ism_ftc.h)
- FilterManagement DDS-Stubs (flt_ftc.h, flt_dts.h)
- AreaEffectSimulation DDS-Stubs (aes.hpp, aes_enums.hpp)
- SymbolAppearance Klasse vollständig definiert mit content() Methode
- BuildingComponentFilter Klasse definiert mit Copy-Konstruktor
- Ftc Factory: nullptr Return (abstract class)
- AesFilter Klasse in aes.hpp hinzugefügt
- Dtc Klasse in ppm.hpp mit participantChanged Signal
- AesType Enum: alle 15 At* Werte definiert
- AesEffectState_def: EsReceived hinzugefügt
- DataModification Enum: eAdded/eModified/eRemoved
- function_reply_object.h → core_types.h Include-Pfad (72 Dateien)
- Enum-Class-Streaming Template-Operator in INGLLogger.h
- ism_mounting_point.h: QImage → QtGui/QImage, QGuiApplication/QScreen
- ism_data_plugin_interface.h: QSet Include, Include-Reihenfolge
- QRegExp → QRegularExpression in tpm_js_parser.cpp
- .underlying() Aufrufe zu direktem Enum-Vergleich korrigiert
- Include-Pfade: aes.hpp in symbol_appearance_settings_impl.h und aes_display_filter_impl.h

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-19 06:06:24 +02:00

304 lines
9.1 KiB
C++
Executable File

#include "web_socket_handler.h"
#include <utility>
#include "ism_handler.h"
#include "main_handler.h"
#include "settings.h"
#include "wls_handler.h"
#include "wls_participant_handler.h"
#include <LtsUtilsCore/dds_vector.h>
#include <QJsonDocument>
#include <QPointF>
#include <QWebSocket>
namespace WirelessLocationSystem
{
const char *exerciseIdProperty = "exerciseId";
WebSocketHandler::WebSocketHandler(QObject *parent)
: QObject(parent)
, m_webSocketServer(new QWebSocketServer(QStringLiteral("WLS"), QWebSocketServer::NonSecureMode, this))
, m_basePoint(LtsCoordTrans::UtmCoordinate::fromMgrs(Settings::wlsMgrsBasePoint()))
{}
WebSocketHandler::~WebSocketHandler()
{}
bool WebSocketHandler::init(std::shared_ptr<WlsHandler> wlsHandler,
std::shared_ptr<WlsParticipantHandler> participantHandler,
std::shared_ptr<IsmHandler> ismHandler,
quint16 listenPort)
{
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
{
WARNING_LOG() << "Can not listen on port " << listenPort << " - aborting" << m_webSocketServer->errorString();
return false;
}
connect(m_webSocketServer, &QWebSocketServer::newConnection,
this, &WebSocketHandler::onNewConnection);
m_wlsHandler = wlsHandler;
m_participantHandler = participantHandler;
m_ismHandler = ismHandler;
return true;
}
void WebSocketHandler::onNewConnection()
{
QWebSocket *ws = m_webSocketServer->nextPendingConnection();
if (ws == nullptr)
return;
DEBUG_LOG() << "New websocket connection from " << ws->peerAddress().toString() << ":" << ws->peerPort();
connect(ws, &QWebSocket::textMessageReceived,
this, &WebSocketHandler::onTextMessageReceived);
connect(ws, &QWebSocket::disconnected,
ws, &QWebSocket::deleteLater);
connect(this, &WebSocketHandler::signalBroadcastMessage,
ws, [this, ws](const QString &msg, int exerciseId)
{
DEBUG_LOG() << "broadcast: exercise property is valid? " << ws->property(exerciseIdProperty).isValid() << " for " << ws->peerPort();
bool exerciseIdAvailable = false;
int wsExerciseId = ws->property(exerciseIdProperty).toInt(&exerciseIdAvailable);
if (exerciseIdAvailable && wsExerciseId != exerciseId)
{
DEBUG_LOG() << "broadcast: websocket has " << wsExerciseId << ", but " << exerciseId << " was requested";
return;
}
ws->sendTextMessage(msg);
});
TrainingAreaBase::TetraIcd::ProxyConnectionState connState;
connState.connectedToSystem = m_wlsHandler->isConnected();
sendTelegram(&connState, 0);
for (const WlsParticipant *participant : std::as_const(m_participantHandler->participants()))
sendDataCommunicationState(participant);
}
void WebSocketHandler::sendTelegramInternal(const TrainingAreaBase::TetraIcd::TrainingAreaTelegram *data,
const QString &type, int exerciseId)
{
QJsonObject obj;
obj[QStringLiteral("data")] = data->toJson();
obj[QStringLiteral("type")] = type;
obj[QStringLiteral("typeId")] = data->id();
const QString jsonStr(QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact)));
DEBUG_LOG() << "jsonStr: " << jsonStr;
emit signalBroadcastMessage(jsonStr, exerciseId);
}
void WebSocketHandler::sendParticipantConnectionStates(int exerciseId)
{
TrainingAreaBase::TetraIcd::WlsParticipantsConnectionState partConnState;
for (const WlsParticipant *p : m_participantHandler->participants())
{
if (p->equipmentNumber() == 0 ||
p->exerciseId() != exerciseId ||
p->tagId().isNull())
continue;
partConnState.equipmentNumber = p->equipmentNumber();
partConnState.dataCommunicationPossible = p->isDataCommunicationPossible();
sendTelegram(&partConnState, exerciseId);
}
}
bool WebSocketHandler::sendWlsStateReport(int exerciseId)
{
TrainingAreaBase::TetraIcd::WlsParticipantsStateReport stateReport;
stateReport.participantStates.reserve(m_participantHandler->participants().size());
for (const WlsParticipant *p : m_participantHandler->participants())
stateReport.participantStates.push_back(p->wlsParticipantState());
sendTelegram(&stateReport, exerciseId);
return true;
}
void WebSocketHandler::onTextMessageReceived(const QString &baJson)
{
using namespace TrainingAreaBase::TetraIcd;
TRACE_LOG() << "WebSocketHandler::onTextMessageReceived: " << baJson;
QWebSocket *ws = qobject_cast<QWebSocket*>(sender());
if (ws == nullptr)
return;
if (baJson.isEmpty())
return;
QJsonParseError err;
QJsonDocument doc(QJsonDocument::fromJson(baJson.toUtf8(), &err));
if (err.error != QJsonParseError::NoError)
{
ERROR_LOG() << "received invalid json from websocket at " << err.offset << ": " << err.errorString();
return;
}
const QJsonObject obj(doc.object());
const QJsonValue typeId(obj.value(QStringLiteral("typeId")));
if (!typeId.isDouble())
{
ERROR_LOG() << "Can not read type id: " << baJson;
return;
}
const QJsonObject data(obj.value(QStringLiteral("data")).toObject());
try
{
switch (typeId.toInt())
{
case Constraints<WlsParticipantsStateRequest>::id():
{
sendWlsStateReport(-1);
break;
}
case Constraints<ExerciseStateData>::id():
{
int exerciseId = ExerciseStateData::fromJson(data).exerciseId;
ws->setProperty(exerciseIdProperty, exerciseId);
DEBUG_LOG() << "set exercise id " << exerciseId << " for ws with peer port " << ws->peerPort();
sendParticipantConnectionStates(exerciseId);
break;
}
case Constraints<ExerciseEquipmentAssignmentComplete>::id():
m_participantHandler->exerciseEquipmentAssignmentChanged(ExerciseEquipmentAssignmentComplete::fromJson(data).exerciseEquipmentAssignments);
break;
case Constraints<WlsBinarySfmData>::id():
m_participantHandler->handleWlsBinarySfmData(WlsBinarySfmData::fromJson(data));
break;
default:
WARNING_LOG() << "Handler for telegram Id " << typeId.toInt()
<< " not yet implemented (" << baJson << ")";
return;
}
}
catch (const std::exception &e)
{
WARNING_LOG() << "Could not parse json: " << e.what() << " (" << baJson << ")";
return;
}
}
void WebSocketHandler::sendBinarySfmData(const WlsParticipant *participant)
{
if (!participant->isSfmRxDataAvailable())
return;
if (participant->equipmentNumber() == 0 || participant->tagId().isNull())
return;
TrainingAreaBase::TetraIcd::WlsBinarySfmData tele;
tele.header.equipmentNumber = participant->equipmentNumber();
tele.header.timeStamp = participant->lastSeenWlsTelegramTime();
LtsUtils::convert(participant->sfmRxData(), tele.payload);
sendTelegram(&tele, participant->exerciseId());
}
void WebSocketHandler::sendDataCommunicationState(const WlsParticipant *participant)
{
if (participant->equipmentNumber() == 0 || participant->tagId().isNull())
return;
TrainingAreaBase::TetraIcd::WlsParticipantsConnectionState tele;
tele.equipmentNumber = participant->equipmentNumber();
tele.dataCommunicationPossible = participant->isDataCommunicationPossible();
sendTelegram(&tele, participant->exerciseId());
}
void WebSocketHandler::sendGpsPositionUsage(const WlsParticipant *participant)
{
if (!participant->isGpsMode())
return;
forceSendPosition(participant, -2, -2);
}
template <typename T>
void WebSocketHandler::setPosition(T &tele, const WlsParticipant *participant)
{
tele.header.equipmentNumber = participant->equipmentNumber();
tele.header.timeStamp = participant->positionTimestamp();
const WlsParticipant::RelativePosition &pos = participant->position();
const QPointF lonLat(m_basePoint.geographic(pos.x, pos.y));
tele.geoPosition.lon = lonLat.x();
tele.geoPosition.lat = lonLat.y();
tele.geoPosition.z = pos.z;
const int moutObjectId =
m_ismHandler->buildingIdFromExternalWlsId(pos.wlsBuildingId);
const int moutRoomId =
m_ismHandler->roomIdFromRoomNumber(moutObjectId, pos.wlsRoomNumber);
tele.opsPosition.moutObjectId = moutObjectId;
tele.opsPosition.moutRoomId = moutRoomId;
}
void WebSocketHandler::sendPosition(const WlsParticipant *participant)
{
INFO_LOG() << "sendPosition equipmentNumber=" << participant->equipmentNumber();
if (!participant || !participant->positionIsValid() || participant->equipmentNumber() == 0)
{
INFO_LOG() << "not send - participant is not yet fully initialized";
return;
}
if (participant->isGpsMode())
{
INFO_LOG() << "not send - participant is in gps mode";
return;
}
TrainingAreaBase::TetraIcd::PositionReport posValue;
setPosition(posValue, participant);
sendTelegram(&posValue, participant->exerciseId());
}
void WebSocketHandler::forceSendPosition(const WlsParticipant *participant,
int moutObjectId, int moutRoomId)
{
TrainingAreaBase::TetraIcd::PositionReport posValue;
setPosition(posValue, participant);
posValue.opsPosition.moutObjectId = moutObjectId;
posValue.opsPosition.moutRoomId = moutRoomId;
sendTelegram(&posValue, participant->exerciseId());
}
}