/** * Telegrams at WsTelegram * $Id$ * $HeadURL$ * $Author$ * Created by Christian Ehrlicher, NLE, 2019-09-18 */ #include "ws_telegrams.h" #include #include namespace HighLevelArchitecture { // // Helpers // JsonException::JsonException(const QJsonObject &obj, const QString &key) : m_obj(obj) , m_key(key) , m_what("Could not find key '" + key.toUtf8() + "' in json ('" + QJsonDocument(obj).toJson(QJsonDocument::Compact) + "')") {} template QJsonValue enumToJson(E e) { static_assert (std::is_enum::value, "Only useable with enumerations"); return static_cast(e); } template bool get(const QJsonObject &obj, const QString &key, T &val, bool bOptional = false) { if (bOptional) { try { val = T::fromJson(obj[key].toObject()); } catch (const JsonException &e) { Q_UNUSED(e); return false; } } else val = T::fromJson(obj[key].toObject()); return true; } // POD getter template <> bool get(const QJsonObject &obj, const QString &key, bool &val, bool bOptional) { const auto jVal = obj[key]; if (!jVal.isBool()) { if (bOptional) return false; throw JsonException(obj, key); } val = jVal.toBool(); return true; } template <> bool get(const QJsonObject &obj, const QString &key, int &val, bool bOptional) { const auto jVal = obj[key]; if (!jVal.isDouble()) { if (bOptional) return false; throw JsonException(obj, key); } val = jVal.toInt(); return true; } template <> bool get(const QJsonObject &obj, const QString &key, double &val, bool bOptional) { const auto jVal = obj[key]; if (!jVal.isDouble()) { if (bOptional) return false; throw JsonException(obj, key); } val = jVal.toDouble(); return true; } template <> bool get(const QJsonObject &obj, const QString &key, QString &val, bool bOptional) { const auto jVal = obj[key]; if (!jVal.isString()) { if (bOptional) return false; throw JsonException(obj, key); } val = jVal.toString(); return true; } // // enum getter // // FinalAttackHeadingMode template <> bool get(const QJsonObject &obj, const QString &key, FinalAttackHeadingMode &val, bool bOptional) { int iVal; get(obj, key, iVal, bOptional); switch (static_cast(iVal)) { case FinalAttackHeadingMode::None: case FinalAttackHeadingMode::HeadingOnly: case FinalAttackHeadingMode::HeadingWithSector: val = static_cast(iVal); return true; } throw JsonException(obj, key); } // // GeoPosition2D // QJsonObject GeoPosition2D::toJson() const { return QJsonObject({{QStringLiteral("lat"), lat}, {QStringLiteral("lon"), lon}, }); } GeoPosition2D GeoPosition2D::fromJson(const QJsonObject &obj) { GeoPosition2D ret; get(obj, QStringLiteral("lat"), ret.lat); get(obj, QStringLiteral("lon"), ret.lon); return ret; } // // GeoPosition3D // QJsonObject GeoPosition3D::toJson() const { return QJsonObject({{QStringLiteral("lat"), lat}, {QStringLiteral("lon"), lon}, {QStringLiteral("height"), height}, }); } GeoPosition3D GeoPosition3D::fromJson(const QJsonObject &obj) { GeoPosition3D ret; get(obj, QStringLiteral("lat"), ret.lat); get(obj, QStringLiteral("lon"), ret.lon); get(obj, QStringLiteral("height"), ret.height); return ret; } // // Area2D // QJsonObject Area2D::toJson() const { return QJsonObject({{QStringLiteral("width"), width}, {QStringLiteral("height"), height}, {QStringLiteral("centerPoint"), centerPoint.toJson()}, }); } Area2D Area2D::fromJson(const QJsonObject &obj) { Area2D ret; get(obj, QStringLiteral("width"), ret.width, true); // optional get(obj, QStringLiteral("height"), ret.height, true); // optional get(obj, QStringLiteral("centerPoint"), ret.centerPoint); return ret; } // // WsTelegramPosition // QJsonObject WsTelegramPosition::toJson() const { return QJsonObject({{QStringLiteral("objectId"), objectId}, {QStringLiteral("name"), name}, {QStringLiteral("timestamp"), timestamp}, {QStringLiteral("position"), position.toJson()}, }); } WsTelegramPosition WsTelegramPosition::fromJson(const QJsonObject &obj) { WsTelegramPosition ret; get(obj, QStringLiteral("objectId"), ret.objectId); get(obj, QStringLiteral("name"), ret.name); get(obj, QStringLiteral("timestamp"), ret.timestamp); get(obj, QStringLiteral("position"), ret.position); return ret; } // // WsTelegramRemoveAesObject // QJsonObject WsTelegramRemoveAesObject::toJson() const { return QJsonObject({{QStringLiteral("objectId"), objectId}, {QStringLiteral("name"), name}, {QStringLiteral("timestamp"), timestamp}, }); } WsTelegramRemoveAesObject WsTelegramRemoveAesObject::fromJson(const QJsonObject &obj) { WsTelegramRemoveAesObject ret; get(obj, QStringLiteral("objectId"), ret.objectId); get(obj, QStringLiteral("name"), ret.name); get(obj, QStringLiteral("timestamp"), ret.timestamp); return ret; } // // WsTelegramMineFieldObject // QJsonObject WsTelegramMineFieldObject::toJson() const { return QJsonObject({{QStringLiteral("objectId"), objectId}, {QStringLiteral("name"), name}, {QStringLiteral("timestamp"), timestamp}, {QStringLiteral("activationTime"), activationTime}, {QStringLiteral("isDeactivated"), isDeactivated}, {QStringLiteral("duration"), duration}, {QStringLiteral("area2D"), area2D.toJson()} }); } WsTelegramMineFieldObject WsTelegramMineFieldObject::fromJson(const QJsonObject &obj) { WsTelegramMineFieldObject ret; get(obj, QStringLiteral("objectId"), ret.objectId); get(obj, QStringLiteral("name"), ret.name); get(obj, QStringLiteral("timestamp"), ret.timestamp); get(obj, QStringLiteral("activationTime"), ret.activationTime); get(obj, QStringLiteral("isDeactivated"), ret.isDeactivated); get(obj, QStringLiteral("duration"), ret.duration); get(obj, QStringLiteral("area2D"), ret.area2D); return ret; } } // namespace HighLevelArchitecture