Files
qt5-legatus-legacy/Lts/Proxies/Buildings/BuildingControlUnit/web_socket_telegrams.cpp
T
2026-08-13 18:33:10 +02:00

855 lines
25 KiB
C++
Executable File

/**
* Websocket interface for building control unit - telgram definition
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2017-01-30
*/
#include "web_socket_telegrams.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
namespace BuildingControlUnit
{
namespace Json
{
namespace Telegrams
{
static QString s_aesId() { return QStringLiteral("aesId"); }
static QString s_bsmStateInfo() { return QStringLiteral("bsmStateInfo"); }
static QString s_chargeTypeId() { return QStringLiteral("chargeTypeId"); }
static QString s_connected() { return QStringLiteral("connected"); }
static QString s_count() { return QStringLiteral("count"); }
static QString s_properties() { return QStringLiteral("properties"); }
static QString s_damagePercentNew() { return QStringLiteral("damagePercentNew"); }
static QString s_damagePercentOld() { return QStringLiteral("damagePercentOld"); }
static QString s_damagePercent() { return QStringLiteral("damagePercent"); }
static QString s_damageState() { return QStringLiteral("damageState"); }
static QString s_data() { return QStringLiteral("data"); }
static QString s_delay() { return QStringLiteral("delay"); }
static QString s_deposition1() { return QStringLiteral("deposition1"); }
static QString s_deposition2() { return QStringLiteral("deposition2"); }
static QString s_destructiveFactor() { return QStringLiteral("destructiveFactor"); }
static QString s_effectSoundId() { return QStringLiteral("effectSoundId"); }
static QString s_effectTime() { return QStringLiteral("effectTime"); }
static QString s_effectOverride() { return QStringLiteral("effectOverride"); }
static QString s_effectStates() { return QStringLiteral("effectStates"); }
static QString s_fortifyFactor() { return QStringLiteral("fortifyFactor"); }
static QString s_fortifyFactors() { return QStringLiteral("fortifyFactors"); }
static QString s_hitOnRoomId() { return QStringLiteral("hitOnRoomId"); }
static QString s_hostname() { return QStringLiteral("hostname"); }
static QString s_id() { return QStringLiteral("id"); }
static QString s_numberOfFlashes() { return QStringLiteral("numberOfFlashes"); }
static QString s_offlineTime() { return QStringLiteral("offlineTime"); }
static QString s_originatorId() { return QStringLiteral("originatorId"); }
static QString s_originatorType() { return QStringLiteral("originatorType"); }
static QString s_pauseBetweenFlashesSec() { return QStringLiteral("pauseBetweenFlashesSec"); }
static QString s_penetrationFactor() { return QStringLiteral("penetrationFactor"); }
static QString s_port() { return QStringLiteral("port"); }
static QString s_pulse() { return QStringLiteral("pulse"); }
static QString s_relativePosition() { return QStringLiteral("relativePosition"); }
static QString s_impactPosition() { return QStringLiteral("relativePosition"); }
static QString s_detonationLocation() { return QStringLiteral("detonationLocation"); }
static QString s_detonationDistance() { return QStringLiteral("detonationDistance"); }
static QString s_relayStates() { return QStringLiteral("relayStates"); }
static QString s_remainingEffectPercent() { return QStringLiteral("remainingEffectPercent"); }
static QString s_rooms() { return QStringLiteral("rooms"); }
static QString s_contaminationStates() { return QStringLiteral("contaminationStates"); }
static QString s_segment() { return QStringLiteral("segment"); }
static QString s_sensorId() { return QStringLiteral("sensorId"); }
static QString s_state() { return QStringLiteral("state"); }
static QString s_relayStateTime() { return QStringLiteral("relayStateTime"); }
static QString s_timestamp() { return QStringLiteral("timestamp"); }
static QString s_type() { return QStringLiteral("type"); }
static QString s_versionInfo() { return QStringLiteral("versionInfo"); }
static QString s_x() { return QStringLiteral("x"); }
static QString s_y() { return QStringLiteral("y"); }
static QString s_z() { return QStringLiteral("z"); }
//
// Helper functions
//
static inline double toDouble(const QJsonValue &val, bool &bOk)
{
bOk = bOk && val.isDouble();
return val.toDouble();
}
static inline int toInteger(const QJsonValue &val, bool &bOk)
{
bOk = bOk && val.isDouble();
return qRound(val.toDouble());
}
static inline int64_t toInt64(const QJsonValue &val, bool &bOk)
{
bOk = bOk && val.isDouble();
return qRound64(val.toDouble());
}
static inline bool toBoolean(const QJsonValue &val, bool &bOk)
{
bOk = bOk && val.isBool();
return val.toBool();
}
//
// BaseTelegram
//
QByteArray BaseTelegram::toJsonInternal(const QJsonObject &data) const
{
//const QJsonDocument::JsonFormat f = QJsonDocument::Compact;
const QJsonDocument::JsonFormat f = QJsonDocument::Indented;
return QJsonDocument(QJsonObject({{s_type(), m_telegramId},
{s_data(), data}
})).toJson(f);
}
BaseTelegram *BaseTelegram::telegramFromJson(const QByteArray &jsonMsg)
{
const QJsonDocument doc(QJsonDocument::fromJson(jsonMsg));
const QJsonObject obj = doc.object();
const QString telId = obj[s_type()].toString();
const QJsonObject data = obj[s_data()].toObject();
if (telId.isEmpty())
return nullptr;
if (telId == SetDamageState::telegramId()) return SetDamageState::fromJson(data);
if (telId == SetContaminationState::telegramId()) return SetContaminationState::fromJson(data);
if (telId == ResetContaminationState::telegramId()) return ResetContaminationState::fromJson(data);
if (telId == SetEffectStates::telegramId()) return SetEffectStates::fromJson(data);
if (telId == SetFortifyFactor::telegramId()) return SetFortifyFactor::fromJson(data);
if (telId == RelayState::telegramId()) return RelayState::fromJson(data);
if (telId == EffectOverride::telegramId()) return EffectOverride::fromJson(data);
if (telId == HitResult::telegramId()) return HitResult::fromJson(data);
if (telId == BsmState::telegramId()) return BsmState::fromJson(data);
if (telId == BsmTriggerFlashlight::telegramId()) return BsmTriggerFlashlight::fromJson(data);
if (telId == CentralEffectSimulationHit::telegramId()) return CentralEffectSimulationHit::fromJson(data);
if (telId == CentralEffectSimulationHit3D::telegramId()) return CentralEffectSimulationHit3D::fromJson(data);
if (telId == DebugSimulateBSMHit::telegramId()) return DebugSimulateBSMHit::fromJson(data);
if (telId == GetBsmConfiguration::telegramId()) return GetBsmConfiguration::fromJson(data);
if (telId == GetBsmBitTimeout::telegramId()) return GetBsmBitTimeout::fromJson(data);
if (telId == GetBsmTechnicalStatus::telegramId()) return GetBsmTechnicalStatus::fromJson(data);
if (telId == SwitchRelay::telegramId()) return SwitchRelay::fromJson(data);
return nullptr;
}
//
// BaseReport
//
QByteArray BaseReport::toJsonInternal(const QJsonObject &_data) const
{
QJsonObject data(_data);
data[s_timestamp()] = timestamp;
return BaseTelegram::toJsonInternal(data);
}
void BaseReport::fromJsonInternal(const QJsonObject &data, BaseReport *tele, bool &bOk)
{
tele->timestamp = toInt64(data[s_timestamp()], bOk);
}
//
// BsmTriggerFlashlight
//
QByteArray BsmTriggerFlashlight::toJson() const
{
return toJsonInternal(
QJsonObject(
{
{s_hostname(), hostname},
{s_sensorId(), sensorId},
{s_numberOfFlashes(), numberOfFlashes},
{s_pauseBetweenFlashesSec(), pauseBetweenFlashesSec},
}));
}
BsmTriggerFlashlight *BsmTriggerFlashlight::fromJson(const QJsonObject &data)
{
bool bOk = true;
BsmTriggerFlashlight ret;
ret.sensorId = toInteger(data[s_sensorId()], bOk);
ret.numberOfFlashes = toInteger(data[s_numberOfFlashes()], bOk);
ret.pauseBetweenFlashesSec = toInteger(data[s_pauseBetweenFlashesSec()], bOk);
if (data.contains(s_hostname()))
{
ret.hostname = data.value(s_hostname()).toString();
bOk = !ret.hostname.isEmpty();
}
return bOk ? new BsmTriggerFlashlight(ret) : nullptr;
}
//
// SetDamageState
//
QByteArray SetDamageState::toJson() const
{
QJsonObject obj(
{
{s_originatorId(), originatorId},
{s_damageState(), static_cast<int>(damageState)}
});
if (!roomIds.isEmpty())
{
QJsonArray arr;
for (int id : roomIds)
arr += QJsonValue(id);
obj[s_rooms()] = arr;
}
return toJsonInternal(obj);
}
SetDamageState *SetDamageState::fromJson(const QJsonObject &data)
{
bool bOk = true;
SetDamageState ret;
ret.damageState = static_cast<DamageState>(toInteger(data[s_damageState()], bOk));
ret.originatorId = toInteger(data[s_originatorId()], bOk);
const QJsonArray arr = data[s_rooms()].toArray();
ret.roomIds.reserve(arr.size());
for (const auto &ele : arr)
{
int roomId = toInteger(ele, bOk);
ret.roomIds.push_back(roomId);
}
return bOk ? new SetDamageState(ret) : nullptr;
}
//
// SetContaminationState
//
SetContaminationState *SetContaminationState::fromJson(const QJsonObject &data)
{
SetContaminationState ret;
const QJsonArray jsArr = data.value(s_contaminationStates()).toArray();
for (const auto &js : jsArr)
{
const QJsonObject &obj = js.toObject();
BuildingControlUnit::ContaminationState cs;
cs.aesId = obj[s_aesId()].toInt();
cs.damagePercentNew = obj[s_damagePercentNew()].toInt();
const QJsonArray &roomArr = obj[s_rooms()].toArray();
cs.rooms.reserve(roomArr.count());
for (const auto & id : roomArr)
cs.rooms.push_back(id.toInt());
ret.contaminationStates.push_back(cs);
}
return new SetContaminationState(ret);
}
QByteArray SetContaminationState::toJson() const
{
QJsonArray contaminationArr;
for (const auto &cs : contaminationStates )
{
QJsonArray roomArr;
for (const auto & roomId : qAsConst(cs.rooms))
roomArr.append(QJsonValue(roomId));
contaminationArr.append(QJsonObject({ {s_aesId(), cs.aesId},
{s_damagePercentNew(), cs.damagePercentNew},
{s_rooms(), roomArr}}));
}
QJsonObject obj({{s_contaminationStates(), contaminationArr}});
return toJsonInternal(obj);
}
//
// ResetContaminationState
//
ResetContaminationState *ResetContaminationState::fromJson(const QJsonObject &data)
{
Q_UNUSED(data)
bool bOk = true;
ResetContaminationState ret;
return bOk ? new ResetContaminationState(ret) : nullptr;
}
QByteArray ResetContaminationState::toJson() const
{
QJsonObject obj = {};
return toJsonInternal(obj);
}
//
// SetEffectStates
//
QByteArray SetEffectStates::toJson() const
{
QString esTypeStr;
QJsonObject obj({{s_segment(), segment}});
QJsonArray arr;
for (const auto &es : effectStates)
{
switch (es.type)
{
case (BET_Unset):
esTypeStr = QStringLiteral("TypeUnknown");
break;
case (BET_Relay):
esTypeStr = QStringLiteral("relayAction");
break;
case (BET_BackgroundSound):
esTypeStr = QStringLiteral("backgroundSoundAction");
break;
case (BET_BsmFlash):
esTypeStr = QStringLiteral("bsmFlashAction");
break;
}
arr += QJsonObject(
{
{s_id(), es.id},
{s_type(), esTypeStr},
{s_effectOverride(), es.effectOverride}
});
}
obj[s_effectStates()] = arr;
return toJsonInternal(obj);
}
SetEffectStates *SetEffectStates::fromJson(const QJsonObject &data)
{
bool bOk = true;
SetEffectStates ret;
ret.segment = data[s_segment()].toString();
const QJsonArray arr = data[s_effectStates()].toArray();
ret.effectStates.reserve(arr.size());
for (const auto &ele : arr)
{
const QJsonObject obj = ele.toObject();
BcuEffectState info;
info.id = toInteger(obj[s_id()], bOk);
QString typeString = obj[s_type()].toString();
if (typeString == QStringLiteral("relayAction"))
info.type = BET_Relay;
else if (typeString == QStringLiteral("backgroundSoundAction"))
info.type = BET_BackgroundSound;
else if (typeString == QStringLiteral("bsmFlashAction"))
info.type = BET_BsmFlash;
info.effectOverride = static_cast<BcuEffectOverride>(toInteger(obj[s_effectOverride()], bOk));
ret.effectStates += info;
}
return bOk ? new SetEffectStates(ret) : nullptr;
}
//
// SwitchRelay
//
QByteArray SwitchRelay::toJson() const
{
QJsonObject obj({{s_segment(), segment},
{s_id(), switchRelayValues.id},
{s_state(), switchRelayValues.bcuRelayState},
{s_count(), switchRelayValues.count},
{s_pulse(), switchRelayValues.pulse},
{s_delay(), switchRelayValues.delay}
});
return toJsonInternal(obj);
}
SwitchRelay *SwitchRelay::fromJson(const QJsonObject &data)
{
SwitchRelay ret;
ret.segment = data[s_segment()].toString();
ret.switchRelayValues.id = data[s_id()].toInt();
ret.switchRelayValues.bcuRelayState = static_cast <BcuRelayState>(data[s_state()].toInt());
ret.switchRelayValues.count = data[s_count()].toInt();
ret.switchRelayValues.pulse = data[s_pulse()].toInt();
ret.switchRelayValues.delay = data[s_delay()].toInt();
return new SwitchRelay(ret);
}
//
// SetFortifyFactor
//
QByteArray SetFortifyFactor::toJson() const
{
QJsonObject obj({{s_originatorId(), originatorId}});
QJsonArray arr;
for (const auto &ff : fortifyFactors)
{
arr += QJsonObject(
{
{s_id(), ff.roomId},
{s_fortifyFactor(), ff.fortifyFactor}
});
}
obj[s_fortifyFactors()] = arr;
return toJsonInternal(obj);
}
SetFortifyFactor *SetFortifyFactor::fromJson(const QJsonObject &data)
{
bool bOk = true;
SetFortifyFactor ret;
ret.originatorId = toInteger(data[s_originatorId()], bOk);
const QJsonArray arr = data[s_fortifyFactors()].toArray();
ret.fortifyFactors.reserve(arr.size());
for (const auto &ele : arr)
{
const QJsonObject obj = ele.toObject();
FortifyInfo info;
info.roomId = toInteger(obj[s_id()], bOk);
info.fortifyFactor = toDouble(obj[s_fortifyFactor()], bOk);
ret.fortifyFactors += info;
}
return bOk ? new SetFortifyFactor(ret) : nullptr;
}
//
// RelayStates
//
QByteArray RelayState::toJson() const
{
QJsonObject obj({{s_segment(), segment}});
QJsonArray arr;
for (const auto &es : relayStates)
{
arr += QJsonObject(
{
{s_id(), es.id},
{s_state(), es.state},
});
}
obj[s_relayStates()] = arr;
obj[s_relayStateTime()] = stateTime;
obj[s_offlineTime()] = offlineTime;
return toJsonInternal(obj);
}
RelayState *RelayState::fromJson(const QJsonObject &data)
{
bool bOk = true;
RelayState ret;
ret.segment = data[s_segment()].toString();
ret.stateTime = data[s_relayStateTime()].toDouble();
ret.offlineTime = data[s_offlineTime()].toDouble();
const QJsonArray arr = data[s_relayStates()].toArray();
ret.relayStates.reserve(arr.size());
for (const auto &ele : arr)
{
const QJsonObject obj = ele.toObject();
RelayStateInfo info;
info.id = toInteger(obj[s_id()], bOk);
info.state = toBoolean(obj[s_state()], bOk);
ret.relayStates += info;
}
return bOk ? new RelayState(ret) : nullptr;
}
QByteArray EffectOverride::toJson() const
{
QJsonObject obj({{s_segment(), segment}});
QJsonArray arr;
for (const auto &es : overrides)
{
arr += QJsonObject(
{
{s_id(), es.id},
{s_effectOverride(), es.effectOverride},
});
}
obj[s_relayStates()] = arr;
obj[s_relayStateTime()] = stateTime;
obj[s_offlineTime()] = offlineTime;
return toJsonInternal(obj);
}
EffectOverride *EffectOverride::fromJson(const QJsonObject &data)
{
bool bOk = true;
EffectOverride ret;
ret.segment = data[s_segment()].toString();
ret.stateTime = data[s_relayStateTime()].toDouble();
ret.offlineTime = data[s_offlineTime()].toDouble();
const QJsonArray arr = data[s_relayStates()].toArray();
ret.overrides.reserve(arr.size());
for (const auto &ele : arr)
{
const QJsonObject obj = ele.toObject();
OverrideInfo info;
info.id = toInteger(obj[s_id()], bOk);
info.effectOverride = toInteger(obj[s_effectOverride()], bOk);
ret.overrides += info;
}
return bOk ? new EffectOverride(ret) : nullptr;
}
//
// CentralEffectSimulationHit
//
QByteArray CentralEffectSimulationHit::toJson() const
{
QJsonObject objPos({{s_x(), relPosition.x},
{s_y(), relPosition.y},
{s_z(), relPosition.z}
});
return toJsonInternal(
QJsonObject
{
{s_effectTime(), effectTimeMSec},
{s_chargeTypeId(), chargeTypeId},
{s_destructiveFactor(), destructiveFactor},
{s_penetrationFactor(), penetrationFactor},
{s_aesId(), aesId},
{s_relativePosition(), objPos},
{s_properties(), properties}
});
}
CentralEffectSimulationHit *CentralEffectSimulationHit::fromJson(const QJsonObject &data)
{
bool bOk = true;
CentralEffectSimulationHit ret;
ret.effectTimeMSec = toInt64(data[s_effectTime()], bOk);
ret.chargeTypeId = toInteger(data[s_chargeTypeId()], bOk);
ret.destructiveFactor = toInteger(data[s_destructiveFactor()], bOk);
ret.penetrationFactor = toInteger(data[s_penetrationFactor()], bOk);
ret.aesId = toInteger(data[s_aesId()], bOk);
const QJsonObject objPos = data[s_relativePosition()].toObject();
ret.relPosition.x = toDouble(objPos[s_x()], bOk);
ret.relPosition.y = toDouble(objPos[s_y()], bOk);
ret.relPosition.z = toDouble(objPos[s_z()], bOk);
ret.properties = data[s_properties()].toString();
return bOk ? new CentralEffectSimulationHit(ret) : nullptr;
}
//
// CentralEffectSimulationHit3D
//
QByteArray CentralEffectSimulationHit3D::toJson() const
{
QJsonObject objImpactPos(
{{s_x(), impactPosition.x},
{s_y(), impactPosition.y},
{s_z(), impactPosition.z}
});
QJsonObject objDetonationLocation(
{{s_x(), detonationLocation.x},
{s_y(), detonationLocation.y},
{s_z(), detonationLocation.z}
});
return toJsonInternal(
QJsonObject
{
{s_effectTime(), effectTimeMSec},
{s_chargeTypeId(), chargeTypeId},
{s_destructiveFactor(), destructiveFactor},
{s_penetrationFactor(), penetrationFactor},
{s_aesId(), aesId},
{s_impactPosition(), objImpactPos},
{s_detonationLocation(), objDetonationLocation},
{s_detonationDistance(), detonationDistance},
{s_properties(), properties}
});
}
CentralEffectSimulationHit3D *CentralEffectSimulationHit3D::fromJson(const QJsonObject &data)
{
bool bOk = true;
CentralEffectSimulationHit3D ret;
ret.effectTimeMSec = toInt64(data[s_effectTime()], bOk);
ret.chargeTypeId = toInteger(data[s_chargeTypeId()], bOk);
ret.destructiveFactor = toInteger(data[s_destructiveFactor()], bOk);
ret.penetrationFactor = toInteger(data[s_penetrationFactor()], bOk);
ret.aesId = toInteger(data[s_aesId()], bOk);
{
const QJsonObject objImpactPos = data[s_impactPosition()].toObject();
ret.impactPosition.x = toDouble(objImpactPos[s_x()], bOk);
ret.impactPosition.y = toDouble(objImpactPos[s_y()], bOk);
ret.impactPosition.z = toDouble(objImpactPos[s_z()], bOk);
}
{
const QJsonObject objDetonationLocation = data[s_detonationLocation()].toObject();
ret.detonationLocation.x = toDouble(objDetonationLocation[s_x()], bOk);
ret.detonationLocation.y = toDouble(objDetonationLocation[s_y()], bOk);
ret.detonationLocation.z = toDouble(objDetonationLocation[s_z()], bOk);
}
ret.detonationDistance = toDouble(data[s_detonationDistance()], bOk);
ret.properties = data[s_properties()].toString();
return bOk ? new CentralEffectSimulationHit3D(ret) : nullptr;
}
//
// DebugSimulateBSMHit
//
QByteArray DebugSimulateBSMHit::toJson() const
{
return toJsonInternal(
QJsonObject
{
{s_effectTime(), effectTimeMSec},
{s_sensorId(), sensorId},
{s_originatorId(), originatorId},
{s_chargeTypeId(), chargeTypeId},
{s_deposition1(), deposition1},
{s_deposition2(), deposition2}
});
}
DebugSimulateBSMHit *DebugSimulateBSMHit::fromJson(const QJsonObject &data)
{
bool bOk = true;
DebugSimulateBSMHit ret;
ret.effectTimeMSec = toInt64(data[s_effectTime()], bOk);
ret.sensorId = toInteger(data[s_sensorId()], bOk);
ret.originatorId = toInteger(data[s_originatorId()], bOk);
ret.chargeTypeId = toInteger(data[s_chargeTypeId()], bOk);
ret.deposition1 = toInteger(data[s_deposition1()], bOk);
ret.deposition2 = toInteger(data[s_deposition2()], bOk);
return bOk ? new DebugSimulateBSMHit(ret) : nullptr;
}
//
// HitResult
//
QByteArray HitResult::toJson() const
{
QJsonArray arr;
for (const HitRoomInfo &hri : hitResult.rooms)
{
arr += QJsonObject(
{
{s_id(), hri.roomId},
{s_damagePercent(), hri.damagePercent},
{s_remainingEffectPercent(), hri.remainingEffectPercent},
{s_fortifyFactor(), hri.fortifyFactor}
});
}
// Contaminaton states
QJsonArray csiArr;
for (const auto &csi : hitResult.contaminations)
{
QJsonArray roomIdsArr;
for (const auto &rId : csi.rooms)
roomIdsArr.append(QJsonValue(rId));
csiArr += QJsonObject({{s_aesId(), csi.aesId},
{s_rooms(), roomIdsArr}});
}
return toJsonInternal(
QJsonObject
{
{s_sensorId(), hitResult.sensorId},
{s_originatorId(), hitResult.originatorId},
{s_originatorType(), static_cast<int>(hitResult.originatorType)},
{s_chargeTypeId(), hitResult.chargeTypeId},
{s_damagePercentOld(), hitResult.damagePercentOld},
{s_damagePercentNew(), hitResult.damagePercentNew},
{s_effectSoundId(), hitResult.effectSoundId},
{s_damageState(), static_cast<int>(hitResult.damageState)},
{s_hitOnRoomId(), hitResult.hitOnRoomId},
{s_rooms(), arr},
{s_contaminationStates(), csiArr}
});
}
HitResult *HitResult::fromJson(const QJsonObject &data)
{
HitResult ret;
bool bOk = data[s_rooms()].isArray();
const QJsonArray arr = data[s_rooms()].toArray();
for (const auto &ele : arr)
{
const QJsonObject obj = ele.toObject();
HitRoomInfo info;
info.roomId = toInteger(obj[s_id()], bOk);
info.damagePercent = toInteger(obj[s_damagePercent()], bOk);
info.remainingEffectPercent = toInteger(obj[s_remainingEffectPercent()], bOk);
info.fortifyFactor = toDouble(obj[s_fortifyFactor()], bOk);
ret.hitResult.rooms += info;
}
ret.hitResult.sensorId = toInteger(data[s_sensorId()], bOk);
ret.hitResult.originatorId = toInteger(data[s_originatorId()], bOk);
ret.hitResult.originatorType = static_cast<OriginatorType>(toInteger(data[s_originatorType()], bOk));
ret.hitResult.chargeTypeId = toInteger(data[s_chargeTypeId()], bOk);
ret.hitResult.damagePercentOld = toInteger(data[s_damagePercentOld()], bOk);
ret.hitResult.damagePercentNew = toInteger(data[s_damagePercentNew()], bOk);
ret.hitResult.effectSoundId = toInteger(data[s_effectSoundId()], bOk);
ret.hitResult.damageState = static_cast<DamageState>(toInteger(data[s_damageState()], bOk));
ret.hitResult.hitOnRoomId = toInteger(data[s_hitOnRoomId()], bOk);
const QJsonArray contaminationArray = data.value(s_contaminationStates()).toArray();
for (const auto &index : contaminationArray)
{
ContaminationStateInfo csi;
const QJsonObject &inObj = index.toObject();
csi.aesId = inObj[s_aesId()].toInt();
const QJsonArray &idsArr = inObj[s_rooms()].toArray();
csi.rooms.reserve(idsArr.count());
for (const auto &id : idsArr)
csi.rooms.push_back(id.toInt());
ret.hitResult.contaminations += csi;
}
return bOk ? new HitResult(ret) : nullptr;
}
//
// BsmState
//
QByteArray BsmState::toJson() const
{
QJsonArray arr;
for (const BsmStateInfo &s : bsmStates)
{
arr += QJsonObject(
{
{s_id(), s.id},
{s_hostname(), s.hostname},
{s_port(), s.port},
{s_connected(), s.connected},
{s_versionInfo(), s.versionInfo}
});
}
return toJsonInternal(
QJsonObject
{
{s_bsmStateInfo(), arr}
});
}
BsmState *BsmState::fromJson(const QJsonObject &data)
{
BsmState ret;
bool bOk = data[s_bsmStateInfo()].isArray();
const QJsonArray arr = data[s_bsmStateInfo()].toArray();
BsmStateInfo info;
for (const auto &ele : arr)
{
const QJsonObject obj = ele.toObject();
info.id = toInt64(obj[s_id()], bOk);
info.hostname = obj[s_hostname()].toString();
info.port = toInteger(obj[s_port()], bOk);
info.connected = toBoolean(obj[s_connected()], bOk);
info.versionInfo = obj[s_versionInfo()].toString();
ret.bsmStates += info;
}
fromJsonInternal(data, &ret, bOk);
return bOk ? new BsmState(ret) : nullptr;
}
//
// GetBsmConfiguration
// currently only for request, result can be seen as debug output in the logfile
//
QByteArray GetBsmConfiguration::toJson() const
{
return toJsonInternal(
QJsonObject
{
{s_sensorId(), sensorId}
});
}
GetBsmConfiguration *GetBsmConfiguration::fromJson(const QJsonObject &data)
{
bool bOk = true;
GetBsmConfiguration ret;
ret.sensorId = toInteger(data[s_sensorId()], bOk);
return bOk ? new GetBsmConfiguration(ret) : nullptr;
}
//
// GetBsmBitTimeout
// currently only for request, result can be seen as debug output in the logfile
//
QByteArray GetBsmBitTimeout::toJson() const
{
return toJsonInternal(
QJsonObject
{
{s_sensorId(), sensorId}
});
}
GetBsmBitTimeout *GetBsmBitTimeout::fromJson(const QJsonObject &data)
{
bool bOk = true;
GetBsmBitTimeout ret;
ret.sensorId = toInteger(data[s_sensorId()], bOk);
return bOk ? new GetBsmBitTimeout(ret) : nullptr;
}
//
// GetBsmTechnicalStatus
// currently only for request, result can be seen as debug output in the logfile
//
QByteArray GetBsmTechnicalStatus::toJson() const
{
return toJsonInternal(
QJsonObject
{
{s_sensorId(), sensorId}
});
}
GetBsmTechnicalStatus *GetBsmTechnicalStatus::fromJson(const QJsonObject &data)
{
bool bOk = true;
GetBsmTechnicalStatus ret;
ret.sensorId = toInteger(data[s_sensorId()], bOk);
return bOk ? new GetBsmTechnicalStatus(ret) : nullptr;
}
} // namespace Telegrams
} // namespace Json
} // namespace BuildingControlUnit