Initial commit

This commit is contained in:
torsten committed 2026-08-13 18:33:10 +02:00
commit b7d90b9afe
16245 files changed
+3396666

No files matched your search

@@ -0,0 +1,24 @@
#
# $Id$
# $HeadURL$
# $Author$
#
SET(json_SRCS
json_api.cpp
json_api.h
web_socket_handler.cpp
web_socket_handler.h
)
NGL_ADD_LIBRARY2(AAR SV BuildingControlJson SHARED ${json_SRCS})
NGL_TARGET_LINK_LIBRARIES(BuildingControlJson
PRIVATE
Qt5::WebSockets
PUBLIC
Qt5::Core
Qt5::Network
BuildingControlCommon
BuildingControlBsm
BuildingControlVwm
)
+284
View File
@@ -0,0 +1,284 @@
/**
* Websocket C++ API for building control unit
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2017-01-30
*/
#include "json_api.h"
#include "web_socket_handler.h"
#include "web_socket_telegrams.h"
#include <QWebSocket>
#include <QtGlobal>
namespace BuildingControlUnit
{
namespace Json
{
Api::Api(QObject *parent)
: QObject(parent)
, m_webSocketHandler(new WebSocketHandler(this))
, m_logger(getLogger("Json.Api"))
{
connect(m_webSocketHandler, &WebSocketHandler::telegramReceived,
this, &Api::onTelegramReceived);
connect(m_webSocketHandler, &WebSocketHandler::webSocketConnected,
this, &Api::webSocketConnected);
}
bool Api::init(quint16 listenPort)
{
return m_webSocketHandler->init(listenPort);
}
void Api::onHitResult(const HitResult &hitResult, const QMap<int, ContaminationState> &contaminationStates, bool playSound)
{
Q_UNUSED(playSound);
sendHitResult(hitResult, contaminationStates, nullptr);
}
void Api::sendHitResult(const HitResult &hitResult, const QMap <int, ContaminationState> &contaminationStates, QWebSocket *ws)
{
Telegrams::HitResult tele;
tele.timestamp = hitResult.effectTimeMSec;
tele.hitResult.sensorId = hitResult.sensorId;
tele.hitResult.originatorId = hitResult.originatorId;
tele.hitResult.originatorType = hitResult.originatorType;
tele.hitResult.chargeTypeId = hitResult.chargeTypeId;
tele.hitResult.damagePercentOld = hitResult.damagePercentOld;
tele.hitResult.damagePercentNew = hitResult.damagePercentNew;
tele.hitResult.effectSoundId = hitResult.effectSoundId;
tele.hitResult.damageState = hitResult.damageState;
tele.hitResult.rooms.reserve(hitResult.rooms.size());
for (const HitRoom &hr : hitResult.rooms)
{
Telegrams::HitResult::HitRoomInfo hri;
hri.roomId = hr.roomId;
hri.damagePercent = hr.damagePercent;
hri.remainingEffectPercent = hr.remainingEffectPercent;
hri.fortifyFactor = hr.fortifyFactor;
if (hr.directHit)
tele.hitResult.hitOnRoomId = hr.roomId;
tele.hitResult.rooms += hri;
}
for (const auto &cs : contaminationStates)
{
Telegrams::HitResult::ContaminationStateInfo csi;
csi.aesId = cs.aesId;
csi.rooms = cs.rooms;
tele.hitResult.contaminations += csi;
}
DEBUG_LOG(m_logger) << LOGFUNCTION << ", tele = " << tele.toJson();
m_webSocketHandler->sendTelegram(&tele, ws);
}
void Api::onBsmState(const QVector<BSM::BsmState> &bsmStates)
{
sendBsmState(bsmStates, nullptr);
}
void Api::onRelayState(const RelayState &relayState)
{
sendRelayState(relayState.hostname, relayState.stateTime, relayState.offlineTime, relayState.relayStates, nullptr);
}
void Api::sendBsmState(const QVector<BSM::BsmState> &bsmStates, QWebSocket *ws)
{
Telegrams::BsmState tele;
tele.timestamp = QDateTime::currentMSecsSinceEpoch();
tele.bsmStates.reserve(bsmStates.size());
Telegrams::BsmState::BsmStateInfo bsi;
for (const BSM::BsmState &state : bsmStates)
{
bsi.connected = state.bConnected;
bsi.hostname = state.hostname;
bsi.id = state.id;
bsi.port = state.port;
bsi.versionInfo = state.versionInfo;
tele.bsmStates += bsi;
}
m_webSocketHandler->sendTelegram(&tele, ws);
}
void Api::sendRelayState(const QString &segment,
const qint64 stateTime,
const qint64 offlineTime,
const QVector<RelayStateInfo> &relayStates,
QWebSocket *ws)
{
Telegrams::RelayState tele;
tele.segment = segment;
tele.stateTime = stateTime;
tele.offlineTime = offlineTime;
Telegrams::RelayState::RelayStateInfo rsi;
for (const RelayStateInfo &state : relayStates)
{
rsi.id = state.id;
rsi.state = state.state;
tele.relayStates += rsi;
}
m_webSocketHandler->sendTelegram(&tele, ws);
}
void Api::sendEffectOverrides(const QString &segment,
const qint64 stateTime,
const qint64 offlineTime,
const QVector<BcuEffectState> &effectOverrides,
QWebSocket *ws)
{
Telegrams::EffectOverride tele;
tele.segment = segment;
tele.stateTime = stateTime;
tele.offlineTime = offlineTime;
Telegrams::EffectOverride::OverrideInfo rsi;
for (const BcuEffectState &state : effectOverrides)
{
rsi.id = state.id;
rsi.effectOverride = state.effectOverride;
tele.overrides += rsi;
}
m_webSocketHandler->sendTelegram(&tele, ws);
}
void Api::onTelegramReceived(const std::shared_ptr<Telegrams::BaseTelegram> &tele)
{
DEBUG_LOG(m_logger) << "tele->telegramId(): " << tele->telegramId();
if (tele->telegramId() == Telegrams::SetDamageState::telegramId())
{
Telegrams::SetDamageState *sds = static_cast<Telegrams::SetDamageState *>(tele.get());
emit damageStateRequest(sds->originatorId, sds->damageState, sds->roomIds);
return;
}
if (tele->telegramId() == Telegrams::ResetContaminationState::telegramId())
{
emit resetContaminationRequest();
return;
}
if (tele->telegramId() == Telegrams::SetContaminationState::telegramId())
{
Telegrams::SetContaminationState *scs = static_cast<Telegrams::SetContaminationState *> (tele.get());
emit setContaminationStateRequest(scs->contaminationStates);
return;
}
if (tele->telegramId() == Telegrams::SetEffectStates::telegramId())
{
Telegrams::SetEffectStates *sff = static_cast<Telegrams::SetEffectStates *>(tele.get());
QVector<BcuEffectState> val;
val.reserve(sff->effectStates.size());
for(const auto &fi : qAsConst(sff->effectStates))
{
BcuEffectState ff;
ff.id = fi.id;
ff.type = fi.type;
ff.effectOverride = fi.effectOverride;
val += ff;
}
emit setEffectStatesRequest(sff->segment, val);
return;
}
if (tele->telegramId() == Telegrams::SwitchRelay::telegramId())
{
Telegrams::SwitchRelay *sff = static_cast<Telegrams::SwitchRelay *>(tele.get());
emit switchRelayRequest(sff->segment, sff->switchRelayValues);
return;
}
if (tele->telegramId() == Telegrams::SetFortifyFactor::telegramId())
{
Telegrams::SetFortifyFactor *sff = static_cast<Telegrams::SetFortifyFactor *>(tele.get());
QVector<FortifyInfo> val;
val.reserve(sff->fortifyFactors.size());
for(const auto &fi : qAsConst(sff->fortifyFactors))
{
FortifyInfo ff;
ff.roomId = fi.roomId;
ff.fortifyFactor = fi.fortifyFactor;
val += ff;
}
emit setFortifyFactorRequest(sff->originatorId, val);
return;
}
if (tele->telegramId() == Telegrams::RelayState::telegramId())
{
Telegrams::RelayState *sff = static_cast<Telegrams::RelayState *>(tele.get());
QVector<RelayStateInfo> val;
val.reserve(sff->relayStates.size());
for(const auto &fi : qAsConst(sff->relayStates))
{
RelayStateInfo ff;
ff.id = fi.id;
ff.state = fi.state;
val += ff;
}
emit relayStatesRequest(sff->segment, val);
return;
}
if (tele->telegramId() == Telegrams::BsmTriggerFlashlight::telegramId())
{
Telegrams::BsmTriggerFlashlight *btf = static_cast<Telegrams::BsmTriggerFlashlight *>(tele.get());
emit bsmTriggerFlashlightRequest(btf->sensorId, btf->hostname, btf->numberOfFlashes, btf->pauseBetweenFlashesSec);
return;
}
if (tele->telegramId() == Telegrams::CentralEffectSimulationHit::telegramId())
{
Telegrams::CentralEffectSimulationHit *cesHit = static_cast<Telegrams::CentralEffectSimulationHit*>(tele.get());
emit centralEffectSimulationHit(cesHit->effectTimeMSec, cesHit->chargeTypeId,
cesHit->destructiveFactor, cesHit->penetrationFactor,
cesHit->aesId,
LtsUtils::QPoint3D(cesHit->relPosition.x, cesHit->relPosition.y, cesHit->relPosition.z),
cesHit->properties);
return;
}
if (tele->telegramId() == Telegrams::CentralEffectSimulationHit3D::telegramId())
{
Telegrams::CentralEffectSimulationHit3D *cesHit = static_cast<Telegrams::CentralEffectSimulationHit3D*>(tele.get());
emit centralEffectSimulationHit3D(cesHit->effectTimeMSec, cesHit->chargeTypeId,
cesHit->destructiveFactor, cesHit->penetrationFactor,
cesHit->aesId,
LtsUtils::QPoint3D(cesHit->impactPosition.x, cesHit->impactPosition.y, cesHit->impactPosition.z),
LtsUtils::QPoint3D(cesHit->detonationLocation.x, cesHit->detonationLocation.y, cesHit->detonationLocation.z),
cesHit->detonationDistance,
cesHit->properties);
return;
}
if (tele->telegramId() == Telegrams::DebugSimulateBSMHit::telegramId())
{
Telegrams::DebugSimulateBSMHit *bsmHit = static_cast<Telegrams::DebugSimulateBSMHit*>(tele.get());
qint64 effectTimeMSec = bsmHit->effectTimeMSec ? bsmHit->effectTimeMSec : QDateTime::currentMSecsSinceEpoch();
emit simulateBSMHit(effectTimeMSec, bsmHit->sensorId, bsmHit->originatorId, bsmHit->chargeTypeId, bsmHit->deposition1, bsmHit->deposition2);
return;
}
if (tele->telegramId() == Telegrams::GetBsmConfiguration::telegramId())
{
Telegrams::GetBsmConfiguration *tel = static_cast<Telegrams::GetBsmConfiguration*>(tele.get());
emit getBsmConfiguration(tel->sensorId);
return;
}
if (tele->telegramId() == Telegrams::GetBsmBitTimeout::telegramId())
{
Telegrams::GetBsmBitTimeout *tel = static_cast<Telegrams::GetBsmBitTimeout*>(tele.get());
emit getBsmBitTimeout(tel->sensorId);
return;
}
if (tele->telegramId() == Telegrams::GetBsmTechnicalStatus::telegramId())
{
Telegrams::GetBsmTechnicalStatus *tel = static_cast<Telegrams::GetBsmTechnicalStatus*>(tele.get());
emit getBsmTechnicalStatus(tel->sensorId);
return;
}
WARNING_LOG(m_logger) << "Received unhandled telegram " << tele->telegramId();
}
} // namespace Json
} // namespace BuildingControlUnit
+88
View File
@@ -0,0 +1,88 @@
/**
* Websocket C++ API for building control unit
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2017-01-30
*/
#pragma once
#include <QObject>
#include <Json/buildingcontroljson_export.h>
#include <VWM/vwm_types.h>
#include <BSM/bsm_types.h>
#include <logger.h>
#include <memory>
#include <bcu_types.h>
class QWebSocket;
namespace BuildingControlUnit
{
namespace Json
{
namespace Telegrams
{
class BaseTelegram;
}
class WebSocketHandler;
class BUILDINGCONTROLJSON_EXPORT Api : public QObject
{
Q_OBJECT
public:
explicit Api(QObject *parent = nullptr);
bool init(quint16 listenPort);
void sendHitResult(const HitResult &hitResult, const QMap<int, ContaminationState> &contaminationStates, QWebSocket *ws);
void sendBsmState(const QVector<BSM::BsmState> &bsmStates, QWebSocket *ws);
void sendRelayState(const QString &segment,
const qint64 stateTime,
const qint64 offlineTime,
const QVector<RelayStateInfo> &relayStates,
QWebSocket *ws);
void sendEffectOverrides(const QString &segment,
const qint64 stateTime,
const qint64 offlineTime,
const QVector<BcuEffectState> &effectOverrides,
QWebSocket *ws);
public Q_SLOTS:
void onHitResult(const HitResult &hitResult, const QMap<int, ContaminationState> &contaminationStates, bool playSound);
void onBsmState(const QVector<BSM::BsmState> &bsmStates);
void onRelayState(const RelayState &relayState);
Q_SIGNALS:
void damageStateRequest(int originatorId, DamageState damageState, const QVector<int> &roomIds);
void setContaminationStateRequest(const std::vector<BuildingControlUnit::ContaminationState> &contaminationStates);
void resetContaminationRequest();
void setEffectStatesRequest( const QString &hostname, const QVector<BcuEffectState> &effectStates);
void switchRelayRequest( const QString &hostname, const SwitchRelayValues &switchRelayValues);
void setFortifyFactorRequest(int originatorId, const QVector<FortifyInfo> &foritfyInfos);
void relayStatesRequest(const QString &hostname, const QVector<RelayStateInfo> &RelayStateInfos);
void bsmTriggerFlashlightRequest(int sensorId, const QString &hostname, int numberOfFlashes, int pauseBetweenFlashesSec);
// tasInd
void centralEffectSimulationHit(const qint64 effectTimeMSec, const int chargeTypeId,
const int destructiveFactor, const int penetrationFactor,
const int aesId, const LtsUtils::QPoint3D &relImpact,
const QString &properties);
void centralEffectSimulationHit3D(const qint64 effectTimeMSec, const int chargeTypeId,
const int destructiveFactor, const int penetrationFactor,
const int aesId, const LtsUtils::QPoint3D &relImpact,
const LtsUtils::QPoint3D &relDetonationLocation,
const double detonationDistance,
const QString &properties);
// debug - simulate BSM hit
void simulateBSMHit(qint64 timeMSec, int sensorId, int originatorId,
int chargeTypeId, int deposition1, int deposition2);
// request status for a bsm
void getBsmConfiguration(int sensorId);
void getBsmBitTimeout(int sensorId);
void getBsmTechnicalStatus(int sensorId);
void webSocketConnected(QWebSocket *ws);
private Q_SLOTS:
void onTelegramReceived(const std::shared_ptr<Telegrams::BaseTelegram> &tele);
private:
WebSocketHandler *m_webSocketHandler;
NGL::INGLLogger &m_logger;
};
} // namespace Json
} // namespace BuildingControlUnit
@@ -0,0 +1,110 @@
/**
* Websocket interface for building control unit
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2017-01-30
*/
#include "web_socket_handler.h"
#include "web_socket_telegrams.h"
#include <QWebSocket>
#include <iostream>
namespace BuildingControlUnit
{
namespace Json
{
WebSocketHandler::WebSocketHandler(QObject *parent)
: QObject(parent)
, m_webSocketServer(new QWebSocketServer(QStringLiteral("BCU"), QWebSocketServer::NonSecureMode, this))
, m_logger(getLogger("Json.WebSocketHandler"))
{}
WebSocketHandler::~WebSocketHandler()
{
for (QWebSocket *s : qAsConst(m_webSockets))
{
s->disconnect(this);
s->close();
}
qDeleteAll(m_webSockets);
}
bool WebSocketHandler::init(quint16 listenPort)
{
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
{
WARNING_LOG(m_logger) << "Can not listen on port " << listenPort << " - aborting";
return false;
}
// inform calling process via stdout about the port/url
std::cout << "web socket listen port: " << m_webSocketServer->serverPort() << std::endl;
std::cout << "web socket url: " << m_webSocketServer->serverUrl().toString() << std::endl;
connect(m_webSocketServer, &QWebSocketServer::newConnection,
this, &WebSocketHandler::onNewConnection);
return true;
}
bool WebSocketHandler::sendTelegram(const Telegrams::BaseTelegram *tele, QWebSocket *wsDirect)
{
if (!isConnected() || !tele)
return false;
const QString jsonStr = QString::fromUtf8(tele->toJson());
DEBUG_LOG(m_logger) << "jsonStr" << jsonStr;
bool bOk = false;
for (QWebSocket *ws : qAsConst(m_webSockets))
{
if (wsDirect != nullptr && wsDirect != ws)
continue;
if (ws->sendTextMessage(jsonStr))
bOk = true;
}
return bOk;
}
void WebSocketHandler::onNewConnection()
{
QWebSocket *ws = m_webSocketServer->nextPendingConnection();
if (!ws)
return;
DEBUG_LOG(m_logger) << "New websocket connection from " << ws->peerAddress().toString() << ":" << ws->peerPort();
connect(ws, &QWebSocket::textMessageReceived,
this, &WebSocketHandler::onTextMessageReceived);
connect(ws, &QWebSocket::disconnected,
this, &WebSocketHandler::onWebSocketDisconnected);
m_webSockets += ws;
emit webSocketConnected(ws);
}
void WebSocketHandler::onTextMessageReceived(const QString &msg)
{
DEBUG_LOG(m_logger) << "new text message: " << msg;
auto tele = std::shared_ptr<Telegrams::BaseTelegram>(Telegrams::BaseTelegram::telegramFromJson(msg.toUtf8()));
if (!tele)
{
WARNING_LOG(m_logger) << "Error parsing incoming json data: '" << msg << "'";
return;
}
emit telegramReceived(tele);
}
void WebSocketHandler::onWebSocketDisconnected()
{
QWebSocket *ws = qobject_cast<QWebSocket *>(sender());
if (!ws)
return;
m_webSockets.remove(ws);
ws->deleteLater();
}
} // namespace Json
} // namespace BuildingControlUnit
@@ -0,0 +1,55 @@
/**
* Websocket interface for building control unit
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2017-01-30
*/
#pragma once
#include <QObject>
#include <QQueue>
#include <QWebSocketServer>
#include <memory>
#include <logger.h>
namespace BuildingControlUnit
{
namespace Json
{
namespace Telegrams
{
class BaseTelegram;
}
class WebSocketHandler : public QObject
{
Q_OBJECT
public:
explicit WebSocketHandler(QObject *parent = nullptr);
~WebSocketHandler();
bool init(quint16 listenPort);
bool isConnected() const
{
return !m_webSockets.isEmpty();
}
bool sendTelegram(const Telegrams::BaseTelegram *tele, QWebSocket *wsDirect);
Q_SIGNALS:
void telegramReceived(const std::shared_ptr<Telegrams::BaseTelegram> &tele);
void webSocketConnected(QWebSocket *ws);
private Q_SLOTS:
void onNewConnection();
void onWebSocketDisconnected();
void onTextMessageReceived(const QString &msg);
private:
QWebSocketServer *m_webSocketServer;
QSet<QWebSocket *> m_webSockets;
LOGGER_T m_logger;
};
} // namespace Json
} // namespace BuildingControlUnit