/** * Handler ShootbackTargets (L400) * $Id$ * $HeadURL$ * $Author$ * Created by N. Thoden, NLE, 06.04.2020 */ #include "main_handler.h" #include "handler_target_information.h" #include "object_target_information.h" using namespace ParticipantManagement; //-------------------------------------------------------------------------- HandlerTargetInformation::HandlerTargetInformation(MainHandler *mainHandler) : ModifyTargetInformationRequestHandler(mainHandler->fts()) , RemoveTargetInformationRequestHandler(mainHandler->fts()) , m_mainHandler(mainHandler) , m_logger(getLogger("HandlerTargetInformation")) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } //-------------------------------------------------------------------------- HandlerTargetInformation::~HandlerTargetInformation() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } //-------------------------------------------------------------------------- bool HandlerTargetInformation::init() { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; bool bRet = true; // create objects auto cb = [this, &bRet](const Database::PpmTargetInformation & data) { DEBUG_LOG(m_logger) << "Reading from DB PpmTargetInformation with id = " << data.id; ObjectTargetInformation *ifs(new ObjectTargetInformation(m_mainHandler)); Core::FunctionReplyObject ret = ifs->createObject(ObjectTargetInformation::toDdsData(data)); if (ret.error()) { delete ifs; ERROR_LOG(m_logger) << ret(); bRet = false; return; } m_objectMap.insert(ifs->getId(), ifs); }; if (!m_mainHandler->db()->select(cb) || !bRet) { ERROR_LOG(m_logger) << "Error reading data from database!"; return false; } DEBUG_LOG(m_logger) << "m_objectMap.size() = " << m_objectMap.size(); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; return true; } //-------------------------------------------------------------------------- void HandlerTargetInformation::onModifyTargetInformationRequest(const rti::core::SampleIdentity &cookie, const ModifyTargetInformationRequest &request) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; const auto ret = targetInformationModifyRequest(request); if (ret.error()) WARNING_LOG(m_logger) << ret(); m_mainHandler->fts()->modifyTargetInformationReply(cookie, ret); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; } //-------------------------------------------------------------------------- Core::FunctionReplyObject HandlerTargetInformation::targetInformationModifyRequest(const ModifyTargetInformationRequest &request) { TRACE_LOG(m_logger) << LOGFUNCTION << " START"; const int requestId = request.targetInfo().id(); // modify if (requestId > 0) { ObjectTargetInformation *ifs = m_objectMap.value(requestId); if (!ifs) return Core::FunctionReplyObject(requestId, tr_lts("dds", "TargetInformation with id %1 not found")).arg(requestId); auto ret = ifs->updateObject(request.targetInfo()); return ret; } // create ObjectTargetInformation *ifs(new ObjectTargetInformation(m_mainHandler)); auto ret = ifs->createObject(request.targetInfo()); if (ret.error()) { delete ifs; ifs = nullptr; } else m_objectMap.insert(ifs->getId(), ifs); TRACE_LOG(m_logger) << LOGFUNCTION << " END"; return ret; } //-------------------------------------------------------------------------- void HandlerTargetInformation::onRemoveTargetInformationRequest(const rti::core::SampleIdentity &cookie, const RemoveTargetInformationRequest &request) { TRACE_LOG(m_logger) << LOGFUNCTION << " request=" << request; Core::FunctionReplyObject ret; ObjectTargetInformation *ifs = m_objectMap.value(request.id()); if (ifs == nullptr) { ret = Core::FunctionReplyObject(request.id(), tr_lts("dds", "ShootbackTarget with id %1 not found")).arg(request.id()); m_mainHandler->fts()->removeTargetInformationReply(cookie, ret); return; } ret = ifs->removeObject(); if (ret.error()) ERROR_LOG(m_logger) << ret(); else { // delete Object delete ifs; m_objectMap.remove(request.id()); } m_mainHandler->fts()->removeTargetInformationReply(cookie, ret); return; }