Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Services/CentralEffectSimulation/ModuleTests/ces_tests_ownership.cpp
T
berrux-dev 2df80f90a7 Migrates CentralEffectSimulation to generated rtiddsgen API
Converts accessor calls to plain member access across ces service.
2026-09-21 02:45:54 +02:00

414 lines
14 KiB
C++
Executable File

/**
* Msg Function Topic Module Test
* $Id$
* $HeadURL$
* $Author$
*/
#include "ces_tests_ownership.h"
#include "CentralEffectSimulation/ces_dtc.h"
#include "CentralEffectSimulation/ces_ftc.h"
#include <nglapplication/nglcoreapplication.h>
#include <LtsCommandLineParser/LtsCommandLineParser.h>
#include <QtCore/QTimer>
#include <QtCore/QDateTime>
#include <QtGui/QPolygonF>
/* ----------------------------------------------------------------------------------
* HOW TO RUN THE TESTS (example)
* ----------------------------------------------------------------------------------
* 0. Preconditions:
* load BT56 (BUILD)
* make db
* Start services: (SV)
* ./bin/svcCentralEffectSimulation -e 42 &
* ./cesTestsOwnership -t 1 -e 42 // 1 Participant in Bldg 56
* ./cesTestsOwnership -t 2 -e 42 // Participants
* ./cesTestsOwnership -t 3 -e 42 // Participants with vulnarability kill are not count
* ./cesTestsOwnership -t 4 -e 42 // Participants in Bldg 56
* ./cesTestsOwnership -t 5 -e 42 // reset Ownership
*/
namespace CentralEffectSimulation
{
CesTestsOwnership::CesTestsOwnership(QObject *parent)
: QObject(parent)
, m_bTestsSuccessful(true)
, m_housePos(LtsCoordTrans::UtmCoordinate::fromMgrs(QStringLiteral("32UMD8000090000")))
, m_logger(getLogger("CesTestsOwnership"))
, m_testId(0)
, m_participantIndex(0)
, m_DynamicParticipantIndex(0)
{}
CesTestsOwnership::~CesTestsOwnership()
{}
bool CesTestsOwnership::init(int domainId, int exerciseId, int testId)
{
bool bRet = true;
INFO_LOG(m_logger) << "init begin";
m_testId = testId;
m_buildingId = 7; // ism_main_structure_element:id
m_ddsConnector42 = std::make_shared<DDSConnector::DdsConnector>(domainId, IDL::PartitionType::Exercise, exerciseId);
m_ddsConnector42->init();
m_ddsConnector42->initPublisherSubscriber();
m_ppmDts = ParticipantManagement::Interface::Dts::create(m_ddsConnector42);
bRet &= m_ppmDts->enableParticipantPosition();
bRet &= m_ppmDts->enableParticipant();
bRet &= m_ppmDts->enableDynamicParticipantData();
m_cesDtc = CentralEffectSimulation::Interface::Dtc::create(m_ddsConnector42);
bRet &= m_cesDtc->enableAll();
m_cesFtc = CentralEffectSimulation::Interface::Ftc::create(m_ddsConnector42);
bRet &= m_cesFtc->enableAll();
m_participants.resize(5);
m_participants[0].participantId = 20;
m_participants[0].equipmentNumber = 60;
m_participants[0].relX = -1;
m_participants[0].relY = -1;
m_participants[0].partyId = 2;
m_participants[0].buildingId = m_buildingId;
m_participants[0].roomId = 58;
m_participants[1].participantId = 21;
m_participants[1].equipmentNumber = 61;
m_participants[1].relX = -10;
m_participants[1].relY = -1;
m_participants[1].partyId = 2;
m_participants[1].buildingId = m_buildingId;
m_participants[1].roomId = 32;
m_participants[2].participantId = 22;
m_participants[2].equipmentNumber = 62;
m_participants[2].relX = -10;
m_participants[2].relY = -1;
m_participants[2].partyId = 3;
m_participants[2].buildingId = m_buildingId;
m_participants[2].roomId = 32;
m_participants[3].participantId = 23;
m_participants[3].equipmentNumber = 63;
m_participants[3].relX = -10;
m_participants[3].relY = -1;
m_participants[3].partyId = 3;
m_participants[3].buildingId = m_buildingId;
m_participants[3].roomId = 32;
m_participants[4].participantId = 24;
m_participants[4].equipmentNumber = 64;
m_participants[4].relX = -10;
m_participants[4].relY = -1;
m_participants[4].partyId = 2;
m_participants[4].buildingId = m_buildingId;
m_participants[4].roomId = 58;
return bRet;
}
void CesTestsOwnership::startTest()
{
INFO_LOG(m_logger) << "startTest m_testId=" << m_testId;
if (m_testId <= 1) // 1 Participant in BT56
{
QTimer::singleShot(0, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(500, this, &CesTestsOwnership::checkBuildingOwnership);
}
else if (m_testId <= 2) // Participants with vulnarability kill are not count
{
// 1 participant with party=1 combatReady for use and
// 2 participants with party=2
m_participants[0].roomId = 32;
m_participants[1].roomId = 32;
m_participants[2].roomId = 32;
QTimer::singleShot(0, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(500, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1000, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1500, this, &CesTestsOwnership::checkBuildingOwnership);
}
else if (m_testId <= 3) // Participants with vulnarability kill are not count
{
// 1 participant with party=1 combatReady for use and
// 2 participants with party=2 killed in the same room
m_participants[0].roomId = 32;
m_participants[1].roomId = 32;
m_participants[2].roomId = 32;
QTimer::singleShot(0, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(500, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1000, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1500, this, &CesTestsOwnership::tryCreateDynamicParticipantData); // Kill for Participant 0 (participantId = 20)
QTimer::singleShot(1700, this, &CesTestsOwnership::tryCreateDynamicParticipantData); // Kill for Participant 1 (participantId = 21)
QTimer::singleShot(2500, this, &CesTestsOwnership::checkBuildingOwnership);
}
else if (m_testId <= 4) // Participants in BT56
{
QTimer::singleShot(0, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(500, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1000, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1500, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(2000, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(2500, this, &CesTestsOwnership::checkBuildingOwnership);
}
else if (m_testId <= 5) // reset ownership
{
QTimer::singleShot(0, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(500, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1000, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(1500, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(2000, this, &CesTestsOwnership::tryCreateParticipantPosition);
QTimer::singleShot(2500, this, &CesTestsOwnership::trySetOwnershipRequest);
QTimer::singleShot(3000, this, &CesTestsOwnership::checkBuildingOwnership);
}
else
{
ERROR_LOG(m_logger) << "Unknown test id = " << m_testId;
failExit();
}
QTimer::singleShot(6000, this, &CesTestsOwnership::failExit);
}
// /////////////////////////////////////////////////////
/// \brief CesTestsOwnership::tryCreateDynamicParticipantData
///
void CesTestsOwnership::tryCreateDynamicParticipantData()
{
ParticipantManagement::DynamicParticipantData dp;
const qint64 time = QDateTime::currentMSecsSinceEpoch();
const rti::core::int64 esTime = static_cast<rti::core::int64>(time);
ParticipantManagement::StateInformation si;
si.stateTime = esTime;
si.vulnerabilityState = ProjectDataManagement::VulnerabilityState::VulnerabilityState_CatastrophicKill;
dp.participantId = m_participants[m_DynamicParticipantIndex].participantId;
dp.stateHistory.push_back(si);
dp.sampleTime = esTime;
m_DynamicParticipantIndex++;
DEBUG_LOG(m_logger) << "Publishing DynamicParticipantData";
if (!m_ppmDts->publish(dp))
{
ERROR_LOG(m_logger) << "Error requesting create ParticipantPosition";
failExit();
}
}
// /////////////////////////////////////////////////////
/// \brief CesTestsOwnership::tryCreateParticipantPosition
///
void CesTestsOwnership::tryCreateParticipantPosition()
{
ParticipantManagement::ParticipantPosition pp;
const qint64 time = QDateTime::currentMSecsSinceEpoch();
const rti::core::int64 posTime = static_cast<rti::core::int64>(time);
QPointF posF = m_housePos.geographic(m_participants[m_participantIndex].relX,
m_participants[m_participantIndex].relY);
const Core::GeoPosition pos(Core::GeoPosition(posF.x(), posF.y(), 0));
pp.buildingId = m_participants[m_participantIndex].buildingId;
pp.participantId = m_participants[m_participantIndex].participantId;
pp.partyId = m_participants[m_participantIndex].partyId;
pp.roomId = m_participants[m_participantIndex].roomId;
m_participantIndex++;
pp.sampleTime = posTime;
pp.pos = pos;
DEBUG_LOG(m_logger) << "Publishing ParticipantPosition";
if (!m_ppmDts->publish(pp))
{
ERROR_LOG(m_logger) << "Error requesting create ParticipantPosition";
failExit();
}
}
// /////////////////////////////////////////////////////
/// \brief CesTestsOwnership::tryCreateParticipant
///
void CesTestsOwnership::tryCreateParticipant()
{
ParticipantManagement::Participant pt;
pt.id = m_participants[m_participantIndex].participantId;
pt.equipmentNumber = m_participants[m_participantIndex].equipmentNumber;
DEBUG_LOG(m_logger) << "Publishing Participant";
if (!m_ppmDts->publish(pt))
{
ERROR_LOG(m_logger) << "Error requesting create Participant";
failExit();
}
}
void CesTestsOwnership::trySetOwnershipRequest()
{
int32_t partyId = 0;
DEBUG_LOG(m_logger) << " request.buildingId() = " << m_buildingId
<< " request.partyId() = " << m_buildingId;
if (!m_cesFtc->setOwnershipRequest(SetOwnershipRequest(m_buildingId, partyId), this))
{
ERROR_LOG(m_logger) << "Error requesting create data";
failExit();
}
}
void CesTestsOwnership::onSetOwnershipReply(const Core::FunctionReplyObject &replyObject)
{
INFO_LOG(m_logger) << "onSetOwnershipReply() replyObject.id=" << replyObject.id();
}
void CesTestsOwnership::checkBuildingOwnership()
{
INFO_LOG(m_logger) << "checkBuildingOwnership";
/*
m_cesDtc->waitForHistoricalDataBuildingOwnership();
QMap < int ,CentralEffectSimulation::BuildingOwnership> buildingOwnership = m_cesDtc->buildingOwnerships();
INFO_LOG(m_logger) << "Recived ParicipantEvent BuildingOwnership:"<<buildingOwnership.buildingId();
for (unsigned int i = 0; i < buildingOwnership.ownerships().size(); i++)
{
CentralEffectSimulation::Ownership ownership = buildingOwnership.ownerships().at(i);
INFO_LOG(m_logger) << "ownership["<<i<<"].floorId="<< ownership.floorId()
<< " ownership["<<i<<"].partyId="<< ownership.partyId();
}
switch (m_testId)
{
case 1:
{
if ( buildingOwnership.ownerships().size() == 1
&& buildingOwnership.ownerships().at(0).floorId() == 57
&& buildingOwnership.ownerships().at(0).partyId() == 2)
{
ExitSuccess();
}
break;
}
case 2:
{
if ( buildingOwnership.ownerships().size() == 1
&& buildingOwnership.ownerships().at(0).floorId() == 24
&& buildingOwnership.ownerships().at(0).partyId() == 2)
{
ExitSuccess();
}
break;
}
case 3:
{
if ( buildingOwnership.ownerships().size() == 1
&& buildingOwnership.ownerships().at(0).floorId() == 24
&& buildingOwnership.ownerships().at(0).partyId() == 3)
{
ExitSuccess();
}
break;
}
case 4:
{
if ( buildingOwnership.ownerships().size() == 2
&& buildingOwnership.ownerships().at(0).floorId() == 57
&& buildingOwnership.ownerships().at(0).partyId() == 2
&& buildingOwnership.ownerships().at(1).floorId() == 24
&& buildingOwnership.ownerships().at(1).partyId() == 3)
{
ExitSuccess();
}
break;
}
case 5:
{
if ( buildingOwnership.ownerships().size() == 3
&& buildingOwnership.ownerships().at(0).floorId() == 57
&& buildingOwnership.ownerships().at(0).partyId() == 0
&& buildingOwnership.ownerships().at(1).floorId() == 24
&& buildingOwnership.ownerships().at(1).partyId() == 0
&& buildingOwnership.ownerships().at(2).floorId() == 89
&& buildingOwnership.ownerships().at(2).partyId() == 0)
{
ExitSuccess();
}
break;
}
} // end switch
*/
ExitSuccess();
}
void CesTestsOwnership::failExit()
{
DEBUG_LOG(m_logger) << "FailExit";
m_bTestsSuccessful = false;
QTimer::singleShot(50, qApp, QCoreApplication::quit);
}
void CesTestsOwnership::ExitSuccess()
{
DEBUG_LOG(m_logger) << "ExitSuccess";
m_bTestsSuccessful = true;
QTimer::singleShot(50, qApp, QCoreApplication::quit);
}
}
// ------------------------------------------------------------------
class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser
{
public:
CmdLineParser()
: LtsCommandLineParser()
{
addOption({ QStringList() << QLatin1String("t") << QLatin1String("testId")
, QLatin1String("TestId.")
, QLatin1String("Default: 0 (all tests)")
, QLatin1String("0")
});
addOption({ QStringList() << QLatin1String("a") << QLatin1String("trainingAreaId")
, QLatin1String("TrainingAreaId.")
, QLatin1String("Default: 0 (Unknown)")
, QLatin1String("0")
});
}
int testId() const { return isSet(QLatin1String("t")) ? value(QLatin1String("t")).toInt() : -1; }
int trainingAreaId() const { return isSet(QLatin1String("a")) ? value(QLatin1String("a")).toInt() : -1; }
};
// ------------------------------------------------------------------
int main(int argc, char **argv)
{
NGLCoreApplication app(argc, argv);
app.createLogger("CES", "log4cxx_ces.props");
CmdLineParser options;
if (!options.init())
return -1;
int iRet = -1;
{
std::unique_ptr<CentralEffectSimulation::CesTestsOwnership> cesTests(new CentralEffectSimulation::CesTestsOwnership);
if (!cesTests->init(options.domainId(), options.exerciseId(), options.testId()))
return iRet;
cesTests->startTest();
STARTUP_LOG(app.baseLogger());
app.exec();
iRet = cesTests->testsSuccessful() ? 0 : 1;
fprintf(stderr, "%d\n", iRet);
DEBUG_LOG(app.baseLogger()) << "** TEST result = " << iRet;
SHUTDOWN_LOG(app.baseLogger());
}
return iRet;
}