/** * @RDE_HEADER@ * ISM Handler Mounting Point Assets * $Id$ * $HeadURL$ * $Author$ * Created by Iuliana Ojog, SLSE, 2016-11-26 */ #include "handler_mounting_point_asset.h" #include "object_mounting_point_asset.h" #include "object_structure_element.h" #include "handler_instrumentation_asset.h" #include "handler_main_structure_element.h" #include "object_instrumentation_asset.h" #include "main_handler.h" #include "settings.h" //------------------------------------------------------------------------------------------------------------- namespace InfrastructureManagement { HandlerMountingPointAsset::HandlerMountingPointAsset(MainHandler *mainHandler) : Base(mainHandler, getLogger("HandlerMountingPointAsset")) , Interface::Fts::MountingPointAssetModifyRequestHandler(mainHandler->dds()->fts()) , Interface::Fts::MountingPointAssetRemoveRequestHandler(mainHandler->dds()->fts()) { } //------------------------------------------------------------------------------------------------------------- HandlerMountingPointAsset::~HandlerMountingPointAsset() { qDeleteAll(m_objectMountingPointAssetMap); } //------------------------------------------------------------------------------------------------------------- bool HandlerMountingPointAsset::init() { bool bRet = true; auto cb = [this, &bRet] (const Database::IsmMountingPointAsset &data) { InfrastructureManagement::MountingPointAsset ddsData = ObjectMountingPointAsset::toDdsData(data); ObjectMountingPointAsset *ifs(new ObjectMountingPointAsset(m_mainHandler)); Core::FunctionReplyObject ret = ifs->createObject(ddsData); if (ret.error()) { delete ifs; ERROR_LOG(m_logger) << ret(); bRet = false; return; } m_objectMountingPointAssetMap.insert(ifs->getId(), ifs); }; // read all Mounting Point Assets from database if (!db()->select(cb)) { ERROR_LOG(m_logger) << "Error reading data from database!"; return false; } DEBUG_LOG(m_logger) << "m_objectMountingPointAssetMap.size() = " << m_objectMountingPointAssetMap.size(); return true; } //-------------------------------------------------------------------------------------------------- void HandlerMountingPointAsset::onMountingPointAssetModifyRequest(const rti::core::SampleIdentity &cookie, const MountingPointAssetModifyRequest &request) { DEBUG_LOG(m_logger) << "onMountingPointAssetModifyRequest: " << request; const Core::FunctionReplyObject ret = request.id() > 0 ? mountingPointAssetModify(request) : mountingPointAssetCreate(request); if (ret.error()) WARNING_LOG(m_logger) << ret(); m_mainHandler->dds()->fts()->mountingPointAssetModifyReply(cookie, ret); } //------------------------------------------------------------------------------------------------------------- Core::FunctionReplyObject HandlerMountingPointAsset::mountingPointAssetCreate(const InfrastructureManagement::MountingPointAssetModifyRequest &request) { MountingPointAsset newData; newData.structureElementId(request.structureElementId()); newData.assetId(request.assetId()); newData.name(request.name()); newData.lastUpdated(QDateTime::currentMSecsSinceEpoch()); ObjectMountingPointAsset *ifs(new ObjectMountingPointAsset(m_mainHandler)); Core::FunctionReplyObject ret = ifs->createObject(newData); if (ret.error()) { delete ifs; ifs = nullptr; return ret; } syncAvailableInExercise(request.structureElementId(), request.assetId()); m_objectMountingPointAssetMap.insert(ifs->getId(), ifs); return ret; } //------------------------------------------------------------------------------------------------------------- Core::FunctionReplyObject HandlerMountingPointAsset::mountingPointAssetModify(const InfrastructureManagement::MountingPointAssetModifyRequest &request) { ObjectMountingPointAsset *ifs = m_objectMountingPointAssetMap.value(request.id()); if (ifs == nullptr) return Core::FunctionReplyObject(request.id(), tr_lts("dds", "MountingPointAsset with id %1 not found")).arg(request.id()); MountingPointAsset ddsData; ddsData.id(request.id()); ddsData.structureElementId(request.structureElementId()); ddsData.assetId(request.assetId()); ddsData.name(request.name()); ddsData.lastUpdated(QDateTime::currentMSecsSinceEpoch()); int32_t oldAssetId = ifs->getAssetId(); Core::FunctionReplyObject ret = ifs->updateObject(ddsData); if (ret.error()) return ret; if (oldAssetId != ifs->getAssetId()) m_mainHandler->getHandlerInstrumentationAsset()->setAssetAvailableInExercise(oldAssetId, std::vector()); syncAvailableInExercise(ifs->getStructureElementId(), ifs->getAssetId()); return ret; } //------------------------------------------------------------------------------------------------------------- void HandlerMountingPointAsset::onMountingPointAssetRemoveRequest(const rti::core::SampleIdentity &cookie, const Core::RemoveElementRequest &request) { Core::FunctionReplyObject ret; ObjectMountingPointAsset *ifs = m_objectMountingPointAssetMap.value(request.id()); if (ifs == nullptr) { ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MountingPointAsset with id %1 not found")).arg(request.id()); m_mainHandler->dds()->fts()->mountingPointAssetRemoveReply(cookie, ret); return; } if (m_mainHandler->dds()->topicLockMaster()->isLocked(ifs->getData(), m_mainHandler->exerciseId())) { ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "MountingPointAsset id %1 is used by another user!")).arg(ifs->getId()); m_mainHandler->dds()->fts()->mountingPointAssetRemoveReply(cookie, ret); return; } int32_t assetId = ifs->getAssetId(); ret = ifs->removeObject(); if (ret.error()) ERROR_LOG(m_logger) << ret(); else { // delete ObjectMountingPointAsset (including MapStateMachine) delete ifs; m_objectMountingPointAssetMap.remove(request.id()); } m_mainHandler->dds()->fts()->mountingPointAssetRemoveReply(cookie, ret); if (!ret.error()) m_mainHandler->getHandlerInstrumentationAsset()->setAssetAvailableInExercise(assetId, std::vector()); } //------------------------------------------------------------------------------------------------------------- int HandlerMountingPointAsset::getMountingPointAssetId(int assetId, int structureElementId) const { for (auto it = m_objectMountingPointAssetMap.begin(); it != m_objectMountingPointAssetMap.end(); ++it) { InfrastructureManagement::ObjectMountingPointAsset *mpa = it.value(); if (mpa->getAssetId() == assetId && mpa->getStructureElementId() == structureElementId) return mpa->getId(); } return 0; } //------------------------------------------------------------------------------------------------------------- bool InfrastructureManagement::HandlerMountingPointAsset::mount(int32_t assetId, int32_t mountingPointId, const std::string &name) { ObjectInstrumentationAsset *asset = m_mainHandler->getHandlerInstrumentationAsset()->getAssetFromId(assetId); if (asset == nullptr) { WARNING_LOG(m_logger) << "Can not find asset with id " << assetId; return false; } ObjectStructureElement *mountingPoint = m_mainHandler->getHandlerMainStructureElement()->getStructureElement(mountingPointId); if (mountingPoint == nullptr) { WARNING_LOG(m_logger) << "Can not find mounting point with id " << mountingPointId; return false; } if (asset->getData().assetStateId() == InstrumentationAssetState::Defect ) { WARNING_LOG(m_logger) << "Can not mount asset with DEFECT state. asset_id: " << assetId <<" name: "<getData().name(); return false; } if (mountingPoint->getStructureElementTypeId() != StructureElementType::MountingPoint) { WARNING_LOG(m_logger) << "Structure Element " << mountingPointId << " type is not mounting point: " << mountingPoint->getStructureElementTypeId(); return false; } auto assetMpType = asset->getData().mountingPointTypeId(); auto mpType = mountingPoint->getData().mountingPointTypeId(); if (!Settings::distinguishBetweenIndoorAndOutdoorIPS()) { if (assetMpType == MountingPointType::OutdoorIPS) assetMpType = MountingPointType::IndoorIPS; if (mpType == MountingPointType::OutdoorIPS) mpType = MountingPointType::IndoorIPS; } if (assetMpType != mpType) { WARNING_LOG(m_logger) << "Structure Element " << mountingPointId << " and asset " << assetId << " - mouting point type id do not match: " << mountingPoint->getData().mountingPointTypeId() << " != " << asset->getData().mountingPointTypeId(); return false; } unmountAsset(assetId); unmountMountingPoint(mountingPointId); MountingPointAsset data; data.structureElementId(mountingPointId); data.assetId(assetId); data.name(name.empty() ? asset->getName().toStdString() : name); data.lastUpdated(QDateTime::currentMSecsSinceEpoch()); ObjectMountingPointAsset *newMountingPointAsset = new ObjectMountingPointAsset(m_mainHandler); if (newMountingPointAsset->createObject(data).error()) { newMountingPointAsset->removeObject(); delete newMountingPointAsset; newMountingPointAsset = nullptr; return false; } m_mainHandler->getHandlerInstrumentationAsset()->setAssetState(assetId, InstrumentationAssetState::Mounted); m_objectMountingPointAssetMap.insert(newMountingPointAsset->getId(), newMountingPointAsset); syncAvailableInExercise(data.structureElementId(), data.assetId()); return true; } //------------------------------------------------------------------------------------------------------------- bool InfrastructureManagement::HandlerMountingPointAsset::unmountAsset(int32_t assetId) { for (auto it = m_objectMountingPointAssetMap.begin(); it != m_objectMountingPointAssetMap.end(); ++it) { ObjectMountingPointAsset *object = it.value(); if (object->getAssetId() == assetId) return unmount(object); } return false; } //------------------------------------------------------------------------------------------------------------- bool InfrastructureManagement::HandlerMountingPointAsset::unmountMountingPoint(int32_t mountingPointId) { for (auto it = m_objectMountingPointAssetMap.begin(); it != m_objectMountingPointAssetMap.end(); ++it) { ObjectMountingPointAsset *object = it.value(); if (object->getStructureElementId() == mountingPointId) return unmount(object); } return true; // no mounting point asset found for this mounting point means it is already unmounted } //------------------------------------------------------------------------------------------------------------- bool InfrastructureManagement::HandlerMountingPointAsset::unmount(ObjectMountingPointAsset *object) { // remove from dds if (!dts()->removeMountingPointAssetByKey(object->getId())) { ERROR_LOG(m_logger) << "Error while removing MountingPointAsset with id " << object->getId() << " from DDS"; return false; } // remove from database if (!db()->removeIsmMountingPointAsset(object->getId())) { ERROR_LOG(m_logger) << "Error while removing MountingPointAsset with id " << object->getId() << " from database"; return false; } m_objectMountingPointAssetMap.remove(object->getId()); m_mainHandler->getHandlerInstrumentationAsset()->setAssetState(object->getAssetId(), InstrumentationAssetState::Unmounted); m_mainHandler->getHandlerInstrumentationAsset()->setAssetAvailableInExercise(object->getAssetId(), std::vector()); delete object; return true; } //------------------------------------------------------------------------------------------------------------- bool HandlerMountingPointAsset::syncAvailableInExercise(int32_t structureElementId, int32_t assetId) const { ObjectStructureElement *structureElement = m_mainHandler->getHandlerMainStructureElement()->getStructureElement(structureElementId); if (!structureElement) return false; ObjectMainStructureElement *mainStructureElement = structureElement->getMainStructureElement(); if (!mainStructureElement) return false; return m_mainHandler->getHandlerInstrumentationAsset()->setAssetAvailableInExercise(assetId, mainStructureElement->getAvailableInExercise()); } //------------------------------------------------------------------------------------------------------------- int32_t InfrastructureManagement::HandlerMountingPointAsset::getMountedAssetId(int32_t mountingPointId) const { auto it = std::find_if(m_objectMountingPointAssetMap.cbegin(), m_objectMountingPointAssetMap.cend(), [mountingPointId](const ObjectMountingPointAsset *mpa) { return mpa->getStructureElementId() == mountingPointId;}); return it == m_objectMountingPointAssetMap.cend() ? 0 : it.value()->getAssetId(); } //------------------------------------------------------------------------------------------------------------- int32_t InfrastructureManagement::HandlerMountingPointAsset::getMountedStructureElementId(int32_t assetId) const { auto it = std::find_if(m_objectMountingPointAssetMap.cbegin(), m_objectMountingPointAssetMap.cend(), [assetId](const ObjectMountingPointAsset *mpa) { return mpa->getAssetId() == assetId;}); return it == m_objectMountingPointAssetMap.cend() ? 0 : it.value()->getStructureElementId(); } //------------------------------------------------------------------------------------------------------------- void HandlerMountingPointAsset::setAvailableInExerciseForAssetOnMountingPoint(int32_t mountingPointId, const std::vector &exercises) { const int assetId = getMountedAssetId(mountingPointId); if (assetId <= 0) return; m_mainHandler->getHandlerInstrumentationAsset()->setAssetAvailableInExercise(assetId, exercises); } } // Namespace InfrastructureManagement