Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
@@ -0,0 +1,683 @@
|
||||
/**
|
||||
* @RDE_HEADER@
|
||||
* ISM Handler Main Structure Elements
|
||||
* $Id$
|
||||
* $HeadURL$
|
||||
* $Author$
|
||||
* Created by Iuliana Ojog, SLSE, 2016-11-26
|
||||
*/
|
||||
|
||||
#include "handler_main_structure_element.h"
|
||||
#include "handler_mounting_point_asset.h"
|
||||
#include "handler_instrumentation_asset.h"
|
||||
#include "object_main_structure_element.h"
|
||||
#include "object_structure_element.h"
|
||||
#include "object_instrumentation_asset.h"
|
||||
#include <ngldb/dbtransaction.h>
|
||||
#include "convert_db_to_dds.h"
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace InfrastructureManagement
|
||||
{
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
HandlerMainStructureElement::HandlerMainStructureElement(MainHandler *mainHandler)
|
||||
: Base(mainHandler, getLogger("HandlerMainStructureElement"))
|
||||
, Interface::Fts::MainStructureElementModifyRequestHandler(mainHandler->dds()->fts())
|
||||
, Interface::Fts::MainStructureElementModifyBridgeStatusRequestHandler(m_mainHandler->dds()->fts())
|
||||
, Interface::Fts::MainStructureElementModifyPositionRequestHandler(m_mainHandler->dds()->fts())
|
||||
, Interface::Fts::MainStructureElementRemoveRequestHandler(mainHandler->dds()->fts())
|
||||
, Interface::Fts::StructureElementModifyRequestHandler(mainHandler->dds()->fts())
|
||||
, Interface::Fts::StructureElementUpdateEffectOverrideRequestHandler(mainHandler->dds()->fts())
|
||||
{
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
HandlerMainStructureElement::~HandlerMainStructureElement()
|
||||
{
|
||||
qDeleteAll(m_objectMainStructureElementMap);
|
||||
// ptr in m_objectStructureElementMap are deleted inside their corresponding MainStructureElement objects
|
||||
// qDeleteAll(m_objectStructureElementMap);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
bool HandlerMainStructureElement::init()
|
||||
{
|
||||
// read all MSE from database
|
||||
QVector<Database::IsmMainStructureElement> dataVec;
|
||||
if (!db()->select(dataVec))
|
||||
{
|
||||
ERROR_LOG(m_logger) << "Error reading data from database!";
|
||||
return false;
|
||||
}
|
||||
// read all SE from database
|
||||
QVector<Database::IsmStructureElement> dataVecSE;
|
||||
if (!db()->select(dataVecSE))
|
||||
{
|
||||
ERROR_LOG(m_logger) << "Error reading data from database!";
|
||||
return false;
|
||||
}
|
||||
// TODO: add sql 'ORDER BY foo' support
|
||||
auto sortSE = [](const Database::IsmStructureElement & e1, const Database::IsmStructureElement & e2)
|
||||
{
|
||||
return e1.id < e2.id;
|
||||
};
|
||||
std::sort(dataVecSE.begin(), dataVecSE.end(), sortSE);
|
||||
|
||||
// create MSE objects
|
||||
for (const Database::IsmMainStructureElement &dbData : qAsConst(dataVec))
|
||||
{
|
||||
ObjectMainStructureElement *ifs(new ObjectMainStructureElement(dbData, m_mainHandler));
|
||||
m_objectMainStructureElementMap.insert(ifs->getId(), ifs);
|
||||
}
|
||||
DEBUG_LOG(m_logger) << "m_objectMainStructureElementMap.size() = " << m_objectMainStructureElementMap.size();
|
||||
|
||||
// create SE objects, add them to MSE
|
||||
for (const Database::IsmStructureElement &dbData : qAsConst(dataVecSE))
|
||||
{
|
||||
ObjectMainStructureElement *objMse = m_objectMainStructureElementMap.value(dbData.mainStructureElementId);
|
||||
if (!objMse || !objMse->isValid())
|
||||
{
|
||||
WARNING_LOG(m_logger) << "Cannot find Main Structure Element Id = " << dbData.mainStructureElementId
|
||||
<< " for structure element " << dbData.id;
|
||||
continue;
|
||||
}
|
||||
|
||||
ObjectStructureElement *ifse = objMse->createStructureElementObject(dbData);
|
||||
if (!ifse)
|
||||
{
|
||||
ERROR_LOG(m_logger) << "Could not create SE!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// add structure elements to global map for lookup (e.g. in 'add Footprints to MSE objects')
|
||||
for (ObjectMainStructureElement *objMse : qAsConst(m_objectMainStructureElementMap))
|
||||
m_objectStructureElementMap.unite(objMse->getStructureElements());
|
||||
|
||||
// add Footprints to MSE objects
|
||||
// read all FP from database
|
||||
QMap<int, QMap<int, Database::IsmStructureElementFootprint>> footprintSorted;
|
||||
{
|
||||
auto cb = [&footprintSorted](const Database::IsmStructureElementFootprint & fp)
|
||||
{
|
||||
footprintSorted[fp.structureElementId][fp.indexNo] = fp;
|
||||
};
|
||||
if (!db()->select(cb))
|
||||
{
|
||||
ERROR_LOG(m_logger) << "Error reading data from database!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (const QMap<int, Database::IsmStructureElementFootprint> &cont : qAsConst(footprintSorted))
|
||||
{
|
||||
if (cont.empty())
|
||||
continue;
|
||||
const int structureElementId = cont.first().structureElementId;
|
||||
const ObjectStructureElement *objSe = m_objectStructureElementMap.value(structureElementId);
|
||||
ObjectMainStructureElement *objMse = objSe ? m_objectMainStructureElementMap.value(objSe->getMainStructureElementId()) : nullptr;
|
||||
if (!objMse || !objMse->isValid())
|
||||
{
|
||||
WARNING_LOG(m_logger) << "Cannot find Main Structure Element Id = " << structureElementId
|
||||
<< " for footprint element " << cont.first().id;
|
||||
continue;
|
||||
}
|
||||
std::vector<FootprintElement> elements;
|
||||
elements.reserve(cont.size());
|
||||
FootprintElement elem;
|
||||
for (const auto &pos : cont)
|
||||
{
|
||||
elem.id(pos.id);
|
||||
elem.relPosition(Core::RelativePosition(pos.relPosition.x(),
|
||||
pos.relPosition.y(),
|
||||
pos.relPosition.z()));
|
||||
elem.isGroundLevel(pos.isGroundLevel);
|
||||
elements.push_back(elem);
|
||||
}
|
||||
objMse->setFootprintData(elements);
|
||||
}
|
||||
|
||||
DEBUG_LOG(m_logger) << "m_objectStructureElementMap.size() = " << m_objectStructureElementMap.size();
|
||||
|
||||
// publish
|
||||
for (auto it = m_objectMainStructureElementMap.begin(); it != m_objectMainStructureElementMap.end(); ++it)
|
||||
{
|
||||
const int buildingId = it.key();
|
||||
publishMainStructureElement(buildingId);
|
||||
it.value()->noticeNewProperties();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::onMainStructureElementModifyRequest(const rti::core::SampleIdentity &cookie,
|
||||
const MainStructureElementModifyRequest &request)
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "onMainStructureElementModifyRequest: " << request;
|
||||
const Core::FunctionReplyObject ret = request.id() == 0 ? onMainStructureElementCreate(request) : onMainStructureElementModify(request);
|
||||
if (ret.error())
|
||||
ERROR_LOG(m_logger) << ret()
|
||||
<< " request.id()=" << request.id();
|
||||
m_mainHandler->dds()->fts()->mainStructureElementModifyReply(cookie, ret);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Core::FunctionReplyObject HandlerMainStructureElement::onMainStructureElementCreate(const MainStructureElementModifyRequest &request)
|
||||
{
|
||||
if (request.elements().size() < 1)
|
||||
return Core::FunctionReplyObject(0, tr_lts("dds", "No structure elements given"));
|
||||
if (request.elements().size() != request.childParentRelations().size())
|
||||
return Core::FunctionReplyObject(0, tr_lts("dds", "Structure elements do not match child - parent relations"));
|
||||
|
||||
const StructureElement &seBuilding = request.elements().at(0);
|
||||
const InfrastructureManagement::StructureElementType &seType = seBuilding.structureElementTypeId();
|
||||
if ((seType != InfrastructureManagement::StructureElementType::Building &&
|
||||
seType != InfrastructureManagement::StructureElementType::Announcement &&
|
||||
seType != InfrastructureManagement::StructureElementType::ApproachingPoint &&
|
||||
seType != InfrastructureManagement::StructureElementType::MeteoDataUnit &&
|
||||
seType != InfrastructureManagement::StructureElementType::RadioStation &&
|
||||
seType != InfrastructureManagement::StructureElementType::Bridge) ||
|
||||
seBuilding.mountingPointTypeId() != InfrastructureManagement::MountingPointType::Unknown)
|
||||
return Core::FunctionReplyObject(0, tr_lts("dds", "First structure element is not from a top level type"));
|
||||
|
||||
if (request.elements().size() > 1) // Buildings must have min 2 elements
|
||||
{
|
||||
const StructureElement &seBuildingBCU = request.elements().at(1);
|
||||
if (seBuildingBCU.structureElementTypeId() != InfrastructureManagement::StructureElementType::MountingPoint ||
|
||||
seBuildingBCU.mountingPointTypeId() != InfrastructureManagement::MountingPointType::BuildingControlUnit)
|
||||
return Core::FunctionReplyObject(0, tr_lts("dds", "Second structure element is not from type 'MountingPoint' for 'BuildingControlUnit'"));
|
||||
}
|
||||
|
||||
InfrastructureManagement::ObjectMainStructureElement *ifs(new ObjectMainStructureElement(m_mainHandler));
|
||||
Core::FunctionReplyObject ret = ifs->createObject(request);
|
||||
if (ret.error())
|
||||
{
|
||||
delete ifs;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// add MSE, SE in corresponding maps
|
||||
const int buildingId = ifs->getId();
|
||||
m_objectMainStructureElementMap.insert(buildingId, ifs);
|
||||
|
||||
const ObjectStructureElement *oseBcu = ifs->getMainStructureElementMountingPoint();
|
||||
bool bMountSucessful = false;
|
||||
if (oseBcu)
|
||||
{
|
||||
// after adding elements to m_objectMainStructureElementMap / m_objectStructureElementMap!
|
||||
ObjectInstrumentationAsset *objAsset = m_mainHandler->getHandlerInstrumentationAsset()->createInstrumentationAssetForMse(ifs->getId(),
|
||||
oseBcu->getId());
|
||||
if (objAsset)
|
||||
{
|
||||
if (m_mainHandler->getHandlerMountingPointAsset()->mount(objAsset->getId(), oseBcu->getId()))
|
||||
bMountSucessful = true;
|
||||
}
|
||||
}
|
||||
if (!bMountSucessful)
|
||||
ERROR_LOG(m_logger) << "Can not create/mount an asset for main structure element " << ifs->getId();
|
||||
|
||||
// publish MSE
|
||||
publishMainStructureElement(buildingId);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Core::FunctionReplyObject HandlerMainStructureElement::onMainStructureElementModify(const MainStructureElementModifyRequest &request)
|
||||
{
|
||||
InfrastructureManagement::ObjectMainStructureElement *ifs = m_objectMainStructureElementMap.value(request.id());
|
||||
if (ifs == nullptr)
|
||||
return Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainStructureElement with id %1 not found")).arg(request.id());
|
||||
|
||||
Core::FunctionReplyObject ret = ifs->updateObject(request);
|
||||
if (ret.error())
|
||||
return ret;
|
||||
|
||||
// updating corresponding SE, if exists
|
||||
if (ifs->getStructureElementId() > 0)
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "Updating corresponding Structure Element id = " << ifs->getStructureElementId();
|
||||
|
||||
InfrastructureManagement::ObjectStructureElement *ifse = m_objectStructureElementMap.value(ifs->getStructureElementId());
|
||||
if (!ifse)
|
||||
return Core::FunctionReplyObject(request.id(), tr_lts("dds", "StructureElement with id %1 not found for MainStructureElement %2"))
|
||||
.arg(ifs->getStructureElementId()).arg(request.id());
|
||||
InfrastructureManagement::StructureElement seData = ifse->getData();
|
||||
|
||||
seData.name(request.name());
|
||||
seData.displayName(ifse->getData().displayName());
|
||||
DEBUG_LOG(m_logger) << "Updating corresponding Structure Element id = " << ifs->getStructureElementId()
|
||||
<< " seData.displayName=" << seData.displayName();
|
||||
|
||||
Core::FunctionReplyObject retSe = ifse->updateObject(seData);
|
||||
if (retSe.error())
|
||||
return retSe;
|
||||
}
|
||||
|
||||
publishMainStructureElement(request.id());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::onMainStructureElementRemoveRequest(const rti::core::SampleIdentity &cookie,
|
||||
const InfrastructureManagement::MainStructureElementRemoveRequest &request)
|
||||
{
|
||||
Core::FunctionReplyObject ret;
|
||||
int buildingId = request.id();
|
||||
|
||||
// gest MSE object
|
||||
InfrastructureManagement::ObjectMainStructureElement *ifs = m_objectMainStructureElementMap.value(buildingId);
|
||||
if (ifs == nullptr)
|
||||
{
|
||||
ret = Core::FunctionReplyObject(buildingId, tr_lts("dds", "MainStructureElement with id %1 not found")).arg(buildingId);
|
||||
m_mainHandler->dds()->fts()->mainStructureElementRemoveReply(cookie, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId()))
|
||||
{
|
||||
ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainStructureElement '%1' is used by another user!")).arg(ifs->getName());
|
||||
m_mainHandler->dds()->fts()->mainStructureElementRemoveReply(cookie, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
// Unmount and remove the BuildingControlUnit which was implicitly created and mounted upon import of the Building.
|
||||
const ObjectStructureElement *bcuMountingPoint = ifs->getMainStructureElementMountingPoint();
|
||||
int bcuAssetId = 0;
|
||||
if (bcuMountingPoint)
|
||||
bcuAssetId = m_mainHandler->getHandlerMountingPointAsset()->getMountedAssetId(bcuMountingPoint->getId());
|
||||
|
||||
if (bcuAssetId == 0 || !m_mainHandler->getHandlerInstrumentationAsset()->removeInstrumentationAssetForMse(bcuAssetId))
|
||||
WARNING_LOG(m_logger) << "Can not remove BuildingControlUnit with id " << bcuAssetId << " from main structure element which got removed: " << buildingId;
|
||||
|
||||
// remove MSE elements from local map and unmount the assets from mounting points
|
||||
const auto seElements = ifs->getStructureElements();
|
||||
for (ObjectStructureElement *ose : seElements)
|
||||
{
|
||||
if (ose->getStructureElementTypeId() == StructureElementType::MountingPoint)
|
||||
{
|
||||
int mountedAssetId = m_mainHandler->getHandlerMountingPointAsset()->getMountedAssetId(ose->getId());
|
||||
if (mountedAssetId > 0)
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "Unmounting instrumentation asset with id " << mountedAssetId << " from mounting point with id " << ose->getId();
|
||||
if (!m_mainHandler->getHandlerMountingPointAsset()->unmountAsset(mountedAssetId))
|
||||
WARNING_LOG(m_logger) << "Can not unmount instrumentation asset with id " << mountedAssetId << " from mounting point " << ose->getId();
|
||||
}
|
||||
}
|
||||
m_objectStructureElementMap.remove(ose->getId());
|
||||
}
|
||||
|
||||
DEBUG_LOG(m_logger) << "Deleting MSE id = " << buildingId;
|
||||
ret = ifs->removeObject(); // removes all from db due to fk
|
||||
if (ret.error())
|
||||
ERROR_LOG(m_logger) << ret();
|
||||
|
||||
// delete ObjectMainStructureElement (including Map)
|
||||
delete ifs;
|
||||
m_objectMainStructureElementMap.remove(buildingId);
|
||||
|
||||
dts()->removeMainStructureElementByKey(buildingId);
|
||||
dts()->removeMainFootprintByKey(buildingId);
|
||||
|
||||
m_mainHandler->dds()->fts()->mainStructureElementRemoveReply(cookie, ret);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::onMainStructureElementModifyPositionRequest(const rti::core::SampleIdentity &cookie,
|
||||
const MainStructureElementModifyPositionRequest &request)
|
||||
{
|
||||
const int mseId = request.id();
|
||||
Core::FunctionReplyObject ret;
|
||||
InfrastructureManagement::ObjectMainStructureElement *ifs = m_objectMainStructureElementMap.value(mseId);
|
||||
if (ifs == nullptr)
|
||||
ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainStructureElement with id %1 not found")).arg(mseId);
|
||||
else
|
||||
{
|
||||
ret = ifs->updatePosition(request.geoPosition());
|
||||
if (!ret.error())
|
||||
publishMainStructureElement(request.id());
|
||||
}
|
||||
m_mainHandler->dds()->fts()->mainStructureElementModifyPositionReply(cookie, ret);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::onMainStructureElementModifyBridgeStatusRequest(const rti::core::SampleIdentity &cookie,
|
||||
const MainStructureElementModifyBridgeStatusRequest &request)
|
||||
{
|
||||
static int s_id = 1;
|
||||
const int mseId = request.id();
|
||||
Core::FunctionReplyObject ret;
|
||||
InfrastructureManagement::ObjectMainStructureElement *ifs = m_objectMainStructureElementMap.value(mseId);
|
||||
if (ifs == nullptr)
|
||||
ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MainStructureElement with id %1 not found")).arg(mseId);
|
||||
else
|
||||
{
|
||||
ret = ifs->setBridgeStatus(request.status());
|
||||
if (!ret.error())
|
||||
{
|
||||
publishMainStructureElement(request.id());
|
||||
// publish BridgeStatus
|
||||
BridgeChangeStatus bcs;
|
||||
bcs.id(s_id++);
|
||||
bcs.buildingId(mseId);
|
||||
bcs.sampleTime(QDateTime::currentMSecsSinceEpoch());
|
||||
bcs.status(request.status());
|
||||
m_mainHandler->dds()->dts()->publish(bcs);
|
||||
}
|
||||
}
|
||||
m_mainHandler->dds()->fts()->mainStructureElementModifyBridgeStatusReply(cookie, ret);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::onStructureElementModifyRequest(const rti::core::SampleIdentity &cookie,
|
||||
const StructureElementModifyRequest &request)
|
||||
{
|
||||
const Core::FunctionReplyObject ret = onStructureElementModify(request);
|
||||
if (ret.error())
|
||||
ERROR_LOG(m_logger) << ret();
|
||||
m_mainHandler->dds()->fts()->structureElementModifyReply(cookie, ret);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::onStructureElementUpdateEffectOverrideRequest(const rti::core::SampleIdentity &cookie,
|
||||
const StructureElementUpdateEffectOverrideRequest &request)
|
||||
{
|
||||
INFO_LOG(m_logger) << "onStructureElementUpdateEffectOverrideRequest "
|
||||
<< " structureElementId=" << request.structureElementId()
|
||||
<< " effectId=" << request.effectId()
|
||||
<< " effectOverride=" << request.effectOverride();
|
||||
|
||||
if (!m_objectStructureElementMap.contains(request.structureElementId()))
|
||||
{
|
||||
ERROR_LOG(m_logger) << "structureElementId=" << request.structureElementId() << " not valid";
|
||||
const Core::FunctionReplyObject ret = Core::FunctionReplyObject(-1, tr_lts("dds", "structureElementId %1 not valid")).arg(request.structureElementId());
|
||||
m_mainHandler->dds()->fts()->structureElementUpdateEffectOverrideReply(cookie, ret);
|
||||
return;
|
||||
}
|
||||
InfrastructureManagement::StructureElement structureElement = getStructureElement(request.structureElementId())->getData();
|
||||
udpatePropertiesForRelayOverride(request.effectId(), request.effectOverride(), structureElement);
|
||||
InfrastructureManagement::ObjectStructureElement *ifse = m_objectStructureElementMap.value(structureElement.id());
|
||||
StructureElementModifyRequest requestSe;
|
||||
int mseId = ifse->getMainStructureElementId();
|
||||
//m_mainHandler->dds()->dts()->getBuildingId (structureElement.id(), mseId);
|
||||
requestSe.mainStructureElementId(mseId);
|
||||
requestSe.elements().resize(1);
|
||||
requestSe.elements()[0] = structureElement;
|
||||
const Core::FunctionReplyObject ret = onStructureElementModify(requestSe);
|
||||
|
||||
if (ret.error())
|
||||
ERROR_LOG(m_logger) << ret();
|
||||
m_mainHandler->dds()->fts()->structureElementUpdateEffectOverrideReply(cookie, ret);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::changeBuildingNameinMountingPoints(const StructureElement &se, MainStructureElementModifyRequest &mseData)
|
||||
{
|
||||
QString oldBuildingName;
|
||||
QString newBuildingName;
|
||||
|
||||
// search old building Display Name:
|
||||
for (auto it = m_objectStructureElementMap.begin(); it != m_objectStructureElementMap.end(); ++it)
|
||||
{
|
||||
const int structureElementId = it.key();
|
||||
const int buildingId = se.mainStructureElementId();
|
||||
ObjectStructureElement *ose = it.value();
|
||||
|
||||
DEBUG_LOG(m_logger) << "buildingId=" << buildingId
|
||||
<< " structureElementId=" << structureElementId
|
||||
<< " se.id=" << se.id()
|
||||
<< " id=" << ose->getId()
|
||||
<< " type=" << ose->getStructureElementTypeId()
|
||||
<< " displayName=" << ose->getData().displayName()
|
||||
<< " MainStructureElementId=" << ose->getMainStructureElementId();
|
||||
if ((buildingId == ose->getMainStructureElementId())
|
||||
//&& (ose->getStructureElementTypeId() == StructureElementType::Building))
|
||||
&& (ose->getData().mountingPointTypeId() == MountingPointType::BuildingControlUnit))
|
||||
{
|
||||
oldBuildingName = QString::fromStdString(ose->getData().displayName());
|
||||
newBuildingName = QString::fromStdString(se.displayName());
|
||||
DEBUG_LOG(m_logger) << "old BuildingName= " << oldBuildingName
|
||||
<< " newBuildingName= " << newBuildingName;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// search for mse
|
||||
for (auto it = m_objectStructureElementMap.begin(); it != m_objectStructureElementMap.end(); ++it)
|
||||
{
|
||||
const int structureElementId = it.key();
|
||||
const int buildingId = se.mainStructureElementId();
|
||||
ObjectStructureElement *ose = it.value();
|
||||
|
||||
DEBUG_LOG(m_logger) << "buildingId=" << buildingId
|
||||
<< " structureElementId=" << structureElementId
|
||||
<< " se.id=" << se.id()
|
||||
<< " id=" << ose->getId()
|
||||
<< " type=" << ose->getStructureElementTypeId()
|
||||
<< " displayName=" << ose->getData().displayName()
|
||||
<< " MainStructureElementId=" << ose->getMainStructureElementId();
|
||||
if ((buildingId == ose->getMainStructureElementId())
|
||||
&& ((ose->getStructureElementTypeId() == StructureElementType::MountingPoint)
|
||||
|| (ose->getStructureElementTypeId() == StructureElementType::Building)))
|
||||
{
|
||||
QString displayName = QString::fromStdString(ose->getData().displayName());
|
||||
if (displayName.contains(oldBuildingName))
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "update = displayName=" << displayName
|
||||
<< " oldBuildingName=" << oldBuildingName
|
||||
<< " newBuildingName=" << newBuildingName;
|
||||
displayName.replace(oldBuildingName, newBuildingName);
|
||||
|
||||
InfrastructureManagement::StructureElement seData = ose->getData();
|
||||
seData.displayName(displayName.toStdString());
|
||||
|
||||
// update mseData before publishing
|
||||
for (size_t j = 0; j < mseData.elements().size(); ++j)
|
||||
{
|
||||
const StructureElement &curSe = mseData.elements().at(j);
|
||||
if (curSe.id() == structureElementId)
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "i=" << j
|
||||
<< " id=" << curSe.id()
|
||||
<< " displayName=" << curSe.displayName();
|
||||
mseData.elements().at(j) = seData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Core::FunctionReplyObject retSe = ose->updateObject(seData);
|
||||
if (retSe.error())
|
||||
ERROR_LOG(m_logger) << "Error update displayName:" << displayName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
Core::FunctionReplyObject HandlerMainStructureElement::onStructureElementModify(const StructureElementModifyRequest &request)
|
||||
{
|
||||
const int mseId = request.mainStructureElementId();
|
||||
InfrastructureManagement::ObjectMainStructureElement *ifs = m_objectMainStructureElementMap.value(mseId);
|
||||
if (ifs == nullptr)
|
||||
return Core::FunctionReplyObject(request.mainStructureElementId(), tr_lts("dds", "MainStructureElement with id %1 not found")).arg(mseId);
|
||||
|
||||
MainStructureElementModifyRequest mseData;
|
||||
// copy for lazy people ...
|
||||
MainStructureElement &mse = mseData;
|
||||
mse = ifs->getData();
|
||||
|
||||
DEBUG_LOG(m_logger) << "request.mseId: " << mseId << " #elements: " << mseData.elements().size() << "request count: " << request.elements().size();
|
||||
for (const StructureElement &se : request.elements())
|
||||
{
|
||||
bool bFound = false;
|
||||
for (size_t i = 0; i < mseData.elements().size(); ++i)
|
||||
{
|
||||
const StructureElement &curSe = mseData.elements().at(i);
|
||||
if (se.id() == curSe.id())
|
||||
{
|
||||
bFound = true;
|
||||
mseData.elements()[i] = se;
|
||||
DEBUG_LOG(m_logger) << "StructureElement Modify = " << mseData.elements()[i];
|
||||
if (se.structureElementTypeId() == StructureElementType::Building
|
||||
&& se.displayName() != curSe.displayName())
|
||||
changeBuildingNameinMountingPoints(se, mseData);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!bFound)
|
||||
return Core::FunctionReplyObject(mseId,
|
||||
tr_lts("dds", "StructureElement with id %1 not found in MainStructure with id %2")).arg(se.id()).arg(mseId);
|
||||
}
|
||||
return onMainStructureElementModify(mseData);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::publishMainStructureElement(int buildingId)
|
||||
{
|
||||
DEBUG_LOG(m_logger) << " Publishing MainStructureElement buildingId = " << buildingId;
|
||||
|
||||
// retrieve MSE from map
|
||||
ObjectMainStructureElement *objMse = m_mainHandler->getHandlerMainStructureElement()->getMainStructureElement(buildingId);
|
||||
if (!objMse || !objMse->isValid())
|
||||
{
|
||||
DEBUG_LOG(m_logger) << " Could not publish MainStructureElement Id = " << buildingId;
|
||||
return;
|
||||
}
|
||||
DEBUG_LOG(m_logger) << " MSE Id = " << buildingId << " , structureElementId = " << objMse->getStructureElementId();
|
||||
|
||||
// publish
|
||||
objMse->onPublishData();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::removedStructureElements(const QMap<int, ObjectStructureElement *> &removedElements)
|
||||
{
|
||||
for (auto it = removedElements.begin(); it != removedElements.end(); ++it)
|
||||
m_objectStructureElementMap.remove(it.key());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------
|
||||
void HandlerMainStructureElement::addedStructureElements(const QMap<int, ObjectStructureElement *> &addedElements)
|
||||
{
|
||||
for (auto it = addedElements.begin(); it != addedElements.end(); ++it)
|
||||
m_objectStructureElementMap[it.key()] = it.value();
|
||||
}
|
||||
/*!
|
||||
* \brief HandlerMainStructureElement::onExerciseStateChanged
|
||||
* \param data
|
||||
* \param mod
|
||||
* \author Niklas Schmidt
|
||||
* \details If the exercise, the MSE is in, were deleted, delete the exercise from MSE properties and delete recording in MSE Structure elements and send to dds
|
||||
*/
|
||||
void HandlerMainStructureElement::onExerciseStateChanged(const ExerciseManagement::Exercise &data, DataModification mod)
|
||||
{
|
||||
Q_UNUSED(mod)
|
||||
if (data.state().underlying() != ExerciseManagement::ExerciseState::Terminated)
|
||||
return;
|
||||
|
||||
for (auto mseObj : qAsConst(m_objectMainStructureElementMap))
|
||||
{
|
||||
const MainStructureElement &mse = mseObj->getData();
|
||||
QJsonObject obj = QJsonDocument::fromJson(QByteArray::fromStdString(mse.properties())).object();
|
||||
if (obj.value(QStringLiteral("available_in_exercise")).toArray().at(0).toInt() == data.id())
|
||||
{
|
||||
MainStructureElementModifyRequest modReq;
|
||||
modReq.angle(mse.angle());
|
||||
modReq.description(mse.description());
|
||||
modReq.externalWlsId(mse.externalWlsId());
|
||||
modReq.elements(mse.elements());
|
||||
// modReq.footprint(mseObj->getFootprint());
|
||||
modReq.geoPosition(mse.geoPosition());
|
||||
modReq.groupId(mse.groupId());
|
||||
modReq.id(mse.id());
|
||||
modReq.isDeleted(mse.isDeleted());
|
||||
modReq.isUsed(mse.isUsed());
|
||||
modReq.modelId(mse.modelId());
|
||||
modReq.name(mse.name());
|
||||
std::vector<InfrastructureManagement::StructureElement> resElements;
|
||||
for (auto element : qAsConst(mse.elements()))
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(QByteArray::fromStdString(element.properties()));
|
||||
QJsonObject objEle = doc.object();
|
||||
if (objEle.contains(QStringLiteral("record")))
|
||||
{
|
||||
objEle[QStringLiteral("record")] = false;
|
||||
element.properties(QJsonDocument(objEle).toJson().toStdString());
|
||||
}
|
||||
resElements.push_back(element);
|
||||
}
|
||||
modReq.elements(resElements);
|
||||
QJsonObject root;
|
||||
try
|
||||
{
|
||||
obj.remove(QStringLiteral("available_in_exercise"));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
ERROR_LOG(m_logger) << "can't parse json";
|
||||
Q_ASSERT(false);
|
||||
}
|
||||
modReq.properties(QString::fromUtf8(QJsonDocument(obj).toJson()).toStdString());
|
||||
modReq.structureElementId(mse.structureElementId());
|
||||
modReq.structureElementTypeId(mse.structureElementTypeId());
|
||||
|
||||
onMainStructureElementModify(modReq);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandlerMainStructureElement::udpatePropertiesForRelayOverride(const int relay,
|
||||
const InfrastructureManagement::EffectOverride state,
|
||||
InfrastructureManagement::StructureElement &se)
|
||||
{
|
||||
QByteArray content = QByteArray::fromStdString(se.properties());
|
||||
const QJsonDocument doc(QJsonDocument::fromJson(content));
|
||||
|
||||
QJsonObject docObj = QJsonObject(doc.object());
|
||||
QJsonObject overridesObj = docObj.value(QStringLiteral("Overrides")).toObject();
|
||||
|
||||
bool containsRelay = false;
|
||||
|
||||
QJsonArray arrRelays = overridesObj.value(QStringLiteral("relays")).toArray();
|
||||
for (int i = 0; i < arrRelays.size(); ++i)
|
||||
{
|
||||
QJsonObject objState = arrRelays[i].toObject();
|
||||
QStringList keys = objState.keys();
|
||||
QString keyStr = keys.at(0);
|
||||
int key = keyStr.toInt();
|
||||
if (key != relay)
|
||||
continue;
|
||||
|
||||
if (state.underlying() != InfrastructureManagement::EffectOverride_def::Automatic)
|
||||
arrRelays[i] = QJsonObject({qMakePair<QString, QJsonValue>(keyStr, state.underlying())});
|
||||
else
|
||||
arrRelays.removeAt(i);
|
||||
containsRelay = true;
|
||||
break;
|
||||
}
|
||||
if (!containsRelay && state.underlying() != InfrastructureManagement::EffectOverride_def::Automatic)
|
||||
arrRelays.append(QJsonObject({qMakePair<QString, QJsonValue>(QString::number(relay), state.underlying())}));
|
||||
|
||||
if (arrRelays.isEmpty())
|
||||
overridesObj.remove(QStringLiteral("relays"));
|
||||
else
|
||||
overridesObj.insert(QStringLiteral("relays"), arrRelays);
|
||||
|
||||
if (overridesObj.isEmpty())
|
||||
docObj.remove(QStringLiteral("Overrides"));
|
||||
else
|
||||
docObj.insert(QStringLiteral("Overrides"), overridesObj);
|
||||
|
||||
QByteArray content_neu = QJsonDocument(docObj).toJson(QJsonDocument::Compact);
|
||||
|
||||
se.properties(content_neu.toStdString());
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
} // Namespace InfrastructureManagement
|
||||
Reference in new issue
Block a user