84 lines
2.6 KiB
C++
Executable File
84 lines
2.6 KiB
C++
Executable File
/**
|
|
* Websocket interface for building control unit
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Christian Ehrlicher, SLSE, 2017-01-30
|
|
*/
|
|
|
|
#include "abstract_socket_handler.h"
|
|
#include "guez_nasat_telegrams.h"
|
|
#include "settings.h"
|
|
#include <QDateTime>
|
|
#include <QJsonDocument>
|
|
|
|
namespace GuezNasat
|
|
{
|
|
AbstractSocketHandler::AbstractSocketHandler(const char *loggerName, QObject *parent)
|
|
: QObject(parent)
|
|
, m_logger(getLogger(loggerName))
|
|
{}
|
|
|
|
void AbstractSocketHandler::setSimulationStatus(int nasatId, bool bActivateSimulation)
|
|
{
|
|
NasatSimulationRequest req;
|
|
req.aircraftId = nasatId;
|
|
req.timestamp = QDateTime::currentSecsSinceEpoch();
|
|
req.enableSimulation = bActivateSimulation;
|
|
const QPointF pos = Settings::simulationPositionDegree();
|
|
req.position.lon = pos.x();
|
|
req.position.lat = pos.y();
|
|
sendTelegram(&req);
|
|
}
|
|
|
|
bool AbstractSocketHandler::sendTelegram(const NasatBase *tele)
|
|
{
|
|
if (!isConnected() || !tele)
|
|
return false;
|
|
|
|
QJsonObject obj;
|
|
obj[QStringLiteral("data")] = tele->toJson();
|
|
obj[QStringLiteral("type")] = tele->type();
|
|
return sendTelegramInternal(QJsonDocument(obj).toJson(QJsonDocument::Compact));
|
|
}
|
|
|
|
void AbstractSocketHandler::textMessageReceived(const QByteArray &msg)
|
|
{
|
|
if (msg.isEmpty())
|
|
{
|
|
ERROR_LOG(m_logger) << "Empty message received";
|
|
return;
|
|
}
|
|
const QJsonObject obj = QJsonDocument::fromJson(msg).object();
|
|
DEBUG_LOG(m_logger) << "incoming telegram: " << msg;
|
|
|
|
const QJsonValue type = obj.value(QStringLiteral("type"));
|
|
if (!type.isString())
|
|
{
|
|
ERROR_LOG(m_logger) << "Can not read type id: " << msg;
|
|
return;
|
|
}
|
|
const QString typeStr = type.toString();
|
|
const QJsonObject data = obj.value(QStringLiteral("data")).toObject();
|
|
try
|
|
{
|
|
if (typeStr.compare(Constraints<NasatPosition>::type(), Qt::CaseInsensitive) == 0)
|
|
emit positionReceived(NasatPosition::fromJson(data));
|
|
else if (typeStr.compare(Constraints<NasatWeaponRelease>::type(), Qt::CaseInsensitive) == 0)
|
|
emit weaponReleaseReceived(NasatWeaponRelease::fromJson(data));
|
|
else if (typeStr.compare(Constraints<NasatNineLiner>::type(), Qt::CaseInsensitive) == 0)
|
|
emit nineLinerReceived(NasatNineLiner::fromJson(data));
|
|
else if (typeStr.compare(Constraints<NasatSimulationStatus>::type(), Qt::CaseInsensitive) == 0)
|
|
emit simulationStatusReceived(NasatSimulationStatus::fromJson(data));
|
|
else
|
|
ERROR_LOG(m_logger) << "Got unknown json telegram: " << msg;
|
|
}
|
|
catch (const JsonException &ex)
|
|
{
|
|
ERROR_LOG(m_logger) << "Could not parse json telegram: " << msg
|
|
<< ", error: " << ex.what();
|
|
}
|
|
}
|
|
|
|
} // namespace GuezNasat
|