- 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>
216 lines
5.3 KiB
C++
Executable File
216 lines
5.3 KiB
C++
Executable File
|
|
|
|
|
|
#include "web_socket_handler.h"
|
|
#include <utility>
|
|
#include "building_process.h"
|
|
|
|
#include <QWebSocket>
|
|
#include <QJsonObject>
|
|
#include <QJsonDocument>
|
|
#include <QJsonArray>
|
|
|
|
|
|
|
|
namespace Buildings
|
|
{
|
|
|
|
WebSocketHandler::WebSocketHandler(const std::shared_ptr<BuildingControlHandler> &buildingControlHandler, QObject *parent)
|
|
: QObject(parent)
|
|
, m_buildingControlHandler(buildingControlHandler)
|
|
, m_webSocketServer(new QWebSocketServer(QStringLiteral("BuildingsSocket"), 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)
|
|
{
|
|
DEBUG_LOG() << "init";
|
|
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
|
|
{
|
|
WARNING_LOG() << "Can not listen on port " << listenPort << " - aborting";
|
|
return false;
|
|
}
|
|
|
|
connect(m_webSocketServer, &QWebSocketServer::newConnection,
|
|
this, &WebSocketHandler::onNewConnection);
|
|
|
|
|
|
INFO_LOG() << "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
|
|
{
|
|
DEBUG_LOG() << "sendTelegramInternal";
|
|
QJsonObject obj;
|
|
obj[QStringLiteral("participantId")] = 1;
|
|
obj[QStringLiteral("equipmentNumber")] = 2;
|
|
obj[QStringLiteral("lat")] = 3.0;
|
|
obj[QStringLiteral("lon")] = 4.0;
|
|
obj[QStringLiteral("height")] = 5.0;
|
|
|
|
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;
|
|
}
|
|
|
|
bool WebSocketHandler::sendbuildingInfos() const
|
|
{
|
|
DEBUG_LOG() << "sendbuildingInfos";
|
|
QJsonObject obj;
|
|
QJsonArray arr;
|
|
|
|
|
|
QMapIterator<int, BuildingProcess *> i(m_buildingControlHandler->getBuildings());
|
|
while (i.hasNext()) {
|
|
i.next();
|
|
BuildingProcess* buildingProcess = i.value();
|
|
BuildingInfo buildingInfo = buildingProcess->buildingInfo();
|
|
quint16 port = buildingProcess->getWebsocketPort();
|
|
DEBUG_LOG() << "key=" << i.key()
|
|
<< " port= " << port
|
|
<< " buildingInfo.name=" << buildingInfo.name;
|
|
QJsonObject objIter;
|
|
objIter[QStringLiteral("building_name")] = buildingInfo.name;
|
|
objIter[QStringLiteral("port")] = port;
|
|
arr.push_back(objIter);
|
|
}
|
|
obj[QStringLiteral("type")] = QStringLiteral("Buildings");
|
|
obj[QStringLiteral("data")] = arr;
|
|
|
|
|
|
|
|
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::onNewMessage(const QString &message)
|
|
{
|
|
DEBUG_LOG() << "onNewMessage='" << message << "'";
|
|
DEBUG_LOG() << "m_buildingControlHandler->m_buildings.size()=" << m_buildingControlHandler->getBuildings().size();
|
|
|
|
if (message.indexOf(QStringLiteral("buildingRequest")) > 0)
|
|
{
|
|
sendbuildingInfos();
|
|
}
|
|
if (message.indexOf(QStringLiteral("triggerFlashlight")) > 0)
|
|
{
|
|
DEBUG_LOG() << "triggerFlashlight" ;
|
|
triggerFlashlight(message);
|
|
}
|
|
}
|
|
|
|
QJsonObject WebSocketHandler::ObjectFromString(const QString& in)
|
|
{
|
|
QJsonObject obj;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(in.toUtf8());
|
|
|
|
|
|
if(!doc.isNull())
|
|
{
|
|
if(doc.isObject())
|
|
{
|
|
obj = doc.object();
|
|
}
|
|
else
|
|
{
|
|
WARNING_LOG() << "Document is not an object";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
WARNING_LOG() << "Invalid JSON...\n" << in ;
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
|
|
void WebSocketHandler::triggerFlashlight(const QString &message)
|
|
{
|
|
DEBUG_LOG() << "triggerFlashlight message=" << message;
|
|
QJsonObject obj = ObjectFromString(message);
|
|
QJsonObject dataObj = obj.value(QStringLiteral("data")).toObject();
|
|
int structureElementId = dataObj.value(QStringLiteral("structureElementId")).toInt();
|
|
QString hostnameBsm = dataObj.value(QStringLiteral("hostnameBsm")).toString();
|
|
|
|
DEBUG_LOG() << "hostnameBsm=" << hostnameBsm
|
|
<< " structureElementId=" << structureElementId;
|
|
|
|
}
|
|
|
|
void WebSocketHandler::onNewConnection()
|
|
{
|
|
DEBUG_LOG() << "onNewConnection";
|
|
QWebSocket *ws = m_webSocketServer->nextPendingConnection();
|
|
|
|
if (!ws)
|
|
return;
|
|
|
|
connect(ws, &QWebSocket::disconnected,
|
|
this, &WebSocketHandler::onWebSocketDisconnected);
|
|
connect(ws, &QWebSocket::textMessageReceived,
|
|
this, &WebSocketHandler::onNewMessage);
|
|
|
|
m_webSockets += ws;
|
|
|
|
INFO_LOG() << "New websocket connected";
|
|
}
|
|
|
|
void WebSocketHandler::onWebSocketDisconnected()
|
|
{
|
|
DEBUG_LOG() << "onWebSocketDisconnected";
|
|
QWebSocket *ws = qobject_cast<QWebSocket *>(sender());
|
|
if (!ws)
|
|
return;
|
|
|
|
m_webSockets.removeAll(ws);
|
|
ws->deleteLater();
|
|
|
|
INFO_LOG() << "A websocket disconnected";
|
|
}
|
|
|
|
bool WebSocketHandler::sendPosition()
|
|
{
|
|
if (!isConnected() || m_webSockets.isEmpty())
|
|
return false;
|
|
|
|
return sendTelegramInternal();
|
|
}
|
|
|
|
}
|