Files
qt5-legatus-legacy/Lts/Services/InfrastructureManagement/handler_effect_area.cpp
T
2026-08-13 18:33:10 +02:00

180 lines
6.5 KiB
C++
Executable File

/**
* @RDE_HEADER@
* ISM Handler Sound Area
* $Id$
* $HeadURL$
* $Author$
* Created by Filip Biedziak, NLE, 2020-08-05
*/
#include "handler_effect_area.h"
#include "handler_main_structure_element.h"
#include "handler_mounting_point_asset.h"
#include "object_main_structure_element.h"
#include "object_effect_area.h"
#include "main_handler.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
namespace InfrastructureManagement
{
HandlerEffectArea::HandlerEffectArea(MainHandler *mainHandler)
: Base(mainHandler, getLogger("HandlerEffectArea"))
{
}
HandlerEffectArea::~HandlerEffectArea()
{
qDeleteAll(m_objectEffectAreaMap);
}
//-----------------------------------------------------------------------------------------------------------------------------
// PUBLIC MEMBER FUNCTIONS
//-----------------------------------------------------------------------------------------------------------------------------
bool HandlerEffectArea::init()
{
const QHash<int, ObjectMainStructureElement *> &objMseMap = m_mainHandler->getHandlerMainStructureElement()->getMainStructureElementsMap();
for (const auto &objMse : qAsConst(objMseMap))
{
ObjectEffectArea *effectAreaObj = createEffectAreaForMse(objMse->getData());
if (effectAreaObj)
m_objectEffectAreaMap.insert(effectAreaObj->getId(), effectAreaObj);
}
if (m_logger.isDebugEnabled())
{
DEBUG_LOG(m_logger) << "number of EffectAreas: " << m_objectEffectAreaMap.size();
for (const auto &EffectAreaObj : qAsConst(m_objectEffectAreaMap))
DEBUG_LOG(m_logger) << "EffectArea ID: " << EffectAreaObj->getId()
<< " | area EGCU ID: " << EffectAreaObj->getAreaEgcuId();
}
return true;
}
int HandlerEffectArea::getSeId(const int mountPointId, const InfrastructureManagement::MainStructureElement &mse) const
{
for ( unsigned int i = 0; i < mse.elements().size(); i++)
{
ERROR_LOG(m_logger) << "mountPointId=" << mountPointId
<< " id=" << mse.elements().at(i).id()
<< " mpId="<< mse.elements().at(i).mountingPointId();
if ( mountPointId ==mse.elements().at(i).mountingPointId() )
{
ERROR_LOG(m_logger) << "return id=" << mse.elements().at(i).id();
return mse.elements().at(i).id();
}
}
return 0;
}
ObjectEffectArea *HandlerEffectArea::createEffectAreaForMse(const InfrastructureManagement::MainStructureElement &mse)
{
std::string properties = mse.properties();
if (properties.find("soundArea") == std::string::npos) // building has no associated sound area
return nullptr;
DEBUG_LOG(m_logger) << "createEffectAreaForMse id=" << mse.id()
<< " propoerties=" << properties;
QJsonObject jsonObj = QJsonDocument::fromJson(QByteArray::fromStdString(properties)).object().value(QStringLiteral("soundArea")).toObject();
int32_t id = jsonObj.value(QStringLiteral("id")).toInt();
int32_t mainEgcuMountpoint = jsonObj.value(QStringLiteral("mainEgcuMountpoint")).toInt();
DEBUG_LOG(m_logger) << "id=" << id
<< " mainEgcuMountpoint=" << mainEgcuMountpoint;
if (m_objectEffectAreaMap.contains(id)) // SoundArea has already been created during the import of another building
{
DEBUG_LOG(m_logger) << "return nullptr";
return nullptr;
}
EffectArea effectArea;
effectArea.id(id);
int seId = getSeId(mainEgcuMountpoint, mse);
effectArea.areaEgcuId(seId);
effectArea.egcuIds(egcuIdsFromJsonObject(jsonObj, mse));
effectArea.geometry(geometryFromJsonObject(jsonObj));
DEBUG_LOG(m_logger) << "mainEgcuMountpoint=" << mainEgcuMountpoint
<< " effectArea.areaEgcuId()=" << effectArea.areaEgcuId()
<< " effectArea.egcuIds().size()=" << effectArea.egcuIds().size()
<< " effectArea.name()=" << effectArea.name();
ObjectEffectArea *effectAreaObj = new ObjectEffectArea(m_mainHandler);
effectAreaObj->createObject(effectArea);
return effectAreaObj;
}
//-----------------------------------------------------------------------------------------------------------------------------
// PRIVATE MEMBER FUNCTIONS
//-----------------------------------------------------------------------------------------------------------------------------
std::vector<int32_t> HandlerEffectArea::egcuIdsFromJsonObject(const QJsonObject &jsonObj, const InfrastructureManagement::MainStructureElement &mse ) const
{
DEBUG_LOG(m_logger) << "egcuIdsFromJsonObject";
std::vector<int32_t> egcuIds;
const QJsonArray &egcuMountPoints = jsonObj.value(QStringLiteral("egcuMountpoints")).toArray(); // qAsConst does not work with r-value
DEBUG_LOG(m_logger) << "egcuMountPoints.size=" << egcuMountPoints.size();
for (const auto &egcuMountPnt : qAsConst(egcuMountPoints))
{
QJsonObject element = egcuMountPnt.toObject();
int mountingPointId = element.value(QStringLiteral("id")).toInt();
int seId = getSeId(mountingPointId, mse);
int32_t egcuId = m_mainHandler->getHandlerMountingPointAsset()->getMountedAssetId(seId);
DEBUG_LOG(m_logger) << "egcuIdsFromJsonObject mountingPointId=" << mountingPointId
<< " seId=" << seId
<< " egcuId=" << egcuId;
if (egcuId > 0)
egcuIds.push_back(egcuId);
} // for
if (m_logger.isDebugEnabled())
{
DEBUG_LOG(m_logger) << "number of EGCUs found: " << egcuIds.size();
//qCritical() << "EGCU IDs: " << egcuIds;
} // if
return egcuIds;
}
std::vector<Core::GeoPosition2D> HandlerEffectArea::geometryFromJsonObject(const QJsonObject &jsonObj) const
{
std::vector<Core::GeoPosition2D> geometry;
Core::GeoPosition2D geoPos2D;
const QJsonArray &points = jsonObj.value(QStringLiteral("geometry")).toArray(); // qAsConst does not work with r-value
for (const auto &point : points)
{
QJsonObject pnt = point.toObject(); // 2D vertex of the polygon that defines the sound area geometry
geoPos2D.lat(pnt.value(QStringLiteral("lat")).toDouble());
geoPos2D.lon(pnt.value(QStringLiteral("lon")).toDouble());
geometry.push_back(geoPos2D);
} // for
if (m_logger.isDebugEnabled())
{
DEBUG_LOG(m_logger) << "number of vertices found: " << geometry.size();
for (const auto &geoPos : geometry)
DEBUG_LOG(m_logger) << "lat = " << geoPos.lat() << " | lon = " << geoPos.lon();
} // if
return geometry;
}
} // namespace InfrastructureManagement