244 lines
6.9 KiB
C++
Executable File
244 lines
6.9 KiB
C++
Executable File
#include "websocket_handler.h"
|
|
#include <utility>
|
|
|
|
#include "settings.h"
|
|
|
|
#include <NGLLogger/NGLLoggerBootstrap.h>
|
|
|
|
#include <QtCore/QPointF>
|
|
#include <QtCore/QTimer>
|
|
#include <QtCore/QJsonDocument>
|
|
#include <QtCore/QJsonObject>
|
|
#include <QtCore/QJsonArray>
|
|
|
|
namespace TrainingAreaBaseOC {
|
|
|
|
WebsocketHandler::WebsocketHandler(QObject *parent, int exerciseId,
|
|
const std::shared_ptr<Interface::Dts> dtsTabOc, const std::shared_ptr<Interface::Dtc> dtcTabOc) : QObject(parent)
|
|
, m_exerciseId(exerciseId)
|
|
, m_dtsTaboc(dtsTabOc)
|
|
, m_dtcTaboc(dtcTabOc),
|
|
m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger()) {
|
|
m_tryConnectionTimer = new QTimer(this);
|
|
connect(m_tryConnectionTimer, &QTimer::timeout, this, &WebsocketHandler::onTryConnectionTimerTimeout);
|
|
m_tryConnectionTimer->start(10 * 1000);
|
|
connect(m_dtcTaboc.get(), &TrainingAreaBaseOC::Interface::Dtc::specificsEquipmentChanged,
|
|
this, [this](){
|
|
m_ownEquipmentId = m_dtcTaboc->specificsEquipment(1).equipmentNumber;
|
|
});
|
|
m_ownEquipmentId = m_dtcTaboc->specificsEquipment(1).equipmentNumber;
|
|
|
|
|
|
m_fetchPositionTimer = new QTimer(this);
|
|
connect(m_fetchPositionTimer, &QTimer::timeout, this, &WebsocketHandler::onFetchPositionTimerTimeout);
|
|
|
|
m_socket = new QWebSocket(QString(), QWebSocketProtocol::Version13, this);
|
|
|
|
connect(m_socket, &QWebSocket::stateChanged,
|
|
this, &WebsocketHandler::onNewSocketState);
|
|
connect(m_socket, &QWebSocket::textMessageReceived,
|
|
this, &WebsocketHandler::onNewTextMessageFromSocket);
|
|
|
|
}
|
|
|
|
void WebsocketHandler::onTryConnectionTimerTimeout()
|
|
{
|
|
if (m_socket->state() != QAbstractSocket::UnconnectedState)
|
|
m_socket->disconnect();
|
|
connectToServer();
|
|
|
|
|
|
|
|
}
|
|
|
|
void WebsocketHandler::onFetchPositionTimerTimeout()
|
|
{
|
|
sendPositionRequest();
|
|
}
|
|
|
|
void WebsocketHandler::onNewSocketState(QAbstractSocket::SocketState state)
|
|
{
|
|
|
|
if (m_tryConnectionTimer->isActive())
|
|
{
|
|
|
|
switch (state)
|
|
{
|
|
case QAbstractSocket::ConnectedState:
|
|
m_state = WebSocketState::CONNECTEDTOSERVER;
|
|
sendServicePortRequest();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return;
|
|
}
|
|
else if (m_state == WebSocketState::CONNECTTOPPM)
|
|
{
|
|
|
|
switch(state)
|
|
{
|
|
case QAbstractSocket::ConnectedState:
|
|
m_state = WebSocketState::CONNECTEDTOPPM;
|
|
m_fetchPositionTimer->start(3 * 1000);
|
|
break;
|
|
case QAbstractSocket::UnconnectedState:
|
|
m_state = WebSocketState::DISCONNECTED;
|
|
m_tryConnectionTimer->start();
|
|
default:
|
|
break;
|
|
}
|
|
return;
|
|
}
|
|
else if (m_state == WebSocketState::CONNECTEDTOPPM)
|
|
{
|
|
switch(state)
|
|
{
|
|
case QAbstractSocket::UnconnectedState:
|
|
case QAbstractSocket::ClosingState:
|
|
m_fetchPositionTimer->stop();
|
|
m_tryConnectionTimer->start();
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void WebsocketHandler::onNewTextMessageFromSocket(const QString &message)
|
|
{
|
|
|
|
if (m_state == WebSocketState::CONNECTEDTOSERVER)
|
|
parseServices(message);
|
|
if(m_state == WebSocketState::CONNECTEDTOPPM)
|
|
onSendOwnPosition(message);
|
|
|
|
}
|
|
|
|
void WebsocketHandler::connectToExPPM(int exPort)
|
|
{
|
|
m_socket->open(QUrl(QStringLiteral("ws://")+Settings::remoteServerHostname()+QStringLiteral(":")+ QString::number(exPort)));
|
|
}
|
|
|
|
void WebsocketHandler::sendPositionRequest()
|
|
{
|
|
if (m_ownEquipmentId == 0)
|
|
return;
|
|
m_socket->sendTextMessage(QStringLiteral("{\"type\": \"ocPositionData\",\"uuid\": \"7777\", \"data\":{ \"equipmentNr\":")+ QString::number(m_ownEquipmentId) + QStringLiteral("} }"));
|
|
}
|
|
|
|
void WebsocketHandler::onSendOwnPosition(const QString &message)
|
|
{
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
|
|
if (!doc.isObject())
|
|
{
|
|
WARNING_LOG(m_logger) << "No Valid Object in " << message;
|
|
return;
|
|
}
|
|
if (doc.object().value(QStringLiteral("data")).toObject().isEmpty())
|
|
{
|
|
WARNING_LOG(m_logger) << "No Data Array in" << message;
|
|
return;
|
|
}
|
|
|
|
const QJsonObject obj = doc.object().value(QStringLiteral("data")).toObject();
|
|
|
|
TrainingAreaBaseOC::OwnPosition outData;
|
|
outData.id = 1;
|
|
|
|
|
|
|
|
if (obj.contains(QStringLiteral("lon")) && obj.contains(QStringLiteral("lat")))
|
|
{
|
|
DEBUG_LOG(m_logger) << "Got message with position";
|
|
outData.position = Core::GeoPosition(obj.value(QStringLiteral("lon")).toDouble(),obj.value(QStringLiteral("lat")).toDouble(),obj.value(QStringLiteral("height")).toDouble());
|
|
outData.timeStamp = QDateTime::currentMSecsSinceEpoch();
|
|
if (obj.contains(QStringLiteral("buildingId")) && obj.contains(QStringLiteral("roomId")) )
|
|
{
|
|
outData.moutObjectId = obj.value(QStringLiteral("buildingId")).toInt();
|
|
outData.moutRoomId = obj.value(QStringLiteral("roomId")).toInt();
|
|
}
|
|
}
|
|
else
|
|
outData.position = Core::GeoPosition(nan(""), nan(""),0);
|
|
|
|
m_dtsTaboc->publish(outData);
|
|
|
|
}
|
|
|
|
void WebsocketHandler::connectToServer()
|
|
{
|
|
|
|
DEBUG_LOG(m_logger) << "Try connect to servicemanagement " << QUrl(QStringLiteral("ws://")+Settings::remoteServerHostname()+QStringLiteral(":")+ QString::number(7300)).toString();
|
|
m_socket->open(QUrl(QStringLiteral("ws://")+Settings::remoteServerHostname()+QStringLiteral(":")+ QString::number(7300)));
|
|
}
|
|
|
|
void WebsocketHandler::sendServicePortRequest()
|
|
{
|
|
m_socket->sendTextMessage(QStringLiteral("{\"type\": \"servicePortsInformationRequest\",\"uuid\": \"7777\"}"));
|
|
DEBUG_LOG(m_logger) << "Wait For Services Reply";
|
|
}
|
|
|
|
void WebsocketHandler::parseServices(const QString &message)
|
|
{
|
|
m_tryConnectionTimer->stop();
|
|
|
|
DEBUG_LOG(m_logger) << "Received SERVICES MESSAGE: " << message;
|
|
m_socket->close();
|
|
int exPort = -1;
|
|
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8());
|
|
if (!doc.isObject())
|
|
{
|
|
WARNING_LOG(m_logger) << "No Valid Object in " << message;
|
|
return;
|
|
}
|
|
if (!doc.object().value(QStringLiteral("data")).isArray())
|
|
{
|
|
WARNING_LOG(m_logger) << "No Data Array in" << message;
|
|
return;
|
|
}
|
|
|
|
const QJsonArray exercises = doc.object().value(QStringLiteral("data")).toArray();
|
|
QJsonArray services;
|
|
|
|
for (const QJsonValue &exercise : exercises)
|
|
{
|
|
const QJsonObject &exe = exercise.toObject();
|
|
if (exe.value(QStringLiteral("exerciseId")).toInt(-1) == m_exerciseId)
|
|
{
|
|
services = exe.value(QStringLiteral("services")).toArray();
|
|
break;
|
|
}
|
|
}
|
|
|
|
for (const QJsonValue &service : std::as_const(services))
|
|
{
|
|
const QJsonObject &obj = service.toObject();
|
|
|
|
if (obj.value(QStringLiteral("name")).toString() == QStringLiteral("ParticipantManagement"))
|
|
{
|
|
const QJsonArray ports = obj.value(QStringLiteral("ports")).toArray();
|
|
for (const QJsonValue &port : ports)
|
|
{
|
|
if (port.toObject().value(QStringLiteral("typeId")).toInt() == 1)
|
|
exPort = port.toObject().value(QStringLiteral("number")).toInt(-1);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (exPort > 0)
|
|
{
|
|
DEBUG_LOG(m_logger) << "Connect To ppm ON: " << exPort;
|
|
connectToExPPM(exPort);
|
|
m_state = WebSocketState::CONNECTTOPPM;
|
|
}
|
|
else
|
|
m_tryConnectionTimer->start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|