/** * Handler for infrastructure data * $Id$ * $HeadURL$ * $Author: * Created by hjl, SLSE, 2016-09-21 */ #include "handler_participant_event.h" #include #include "web_socket_server.h" #include "settings.h" #include #include #include #include using namespace ExerciseEventManagement; HandlerParticipantEvent::HandlerParticipantEvent(MainHandler *mainHandler, const std::shared_ptr &ddsHandler, const std::shared_ptr &db, const std::shared_ptr &eventServer) : m_ddsHandler(ddsHandler) , m_dbHandler(db) , m_eventServer(eventServer) , m_mainHandler(mainHandler) , m_logger(getLogger("HandlerParticipantEvent")) { } // end of ctor() //----------------------------------------------------------------------------- HandlerParticipantEvent::~HandlerParticipantEvent() { } // end of dtor() //----------------------------------------------------------------------------- bool HandlerParticipantEvent::init() { INFO_LOG(m_logger) << "start init() DTC CentralEffectSimulation "; // DTC CentralEffectSimulation m_cesDtc = CentralEffectSimulation::Interface::Dtc::create(m_ddsHandler->ddsConnector()); if (!m_cesDtc) { ERROR_LOG(m_logger) << "Could not create DTC CentralEffectSimulation"; return false; } // Subsribe for ParticipantEvent (for analyzing data) connect(m_cesDtc.get(), &CentralEffectSimulation::Interface::Dtc::participantEventChanged, this, &HandlerParticipantEvent::onParticipantEventChanged); if (!m_cesDtc->enableParticipantEvent() ) { ERROR_LOG(m_logger) << "init() DTC CentralEffectSimulation failed"; return false; } // DTC ProjectDataManagement m_pdmDtc = ProjectDataManagement::Interface::Dtc::create(m_ddsHandler->ddsConnector()); if (!m_pdmDtc) { ERROR_LOG(m_logger) << "Could not create DTC ProjectDataManagement"; return false; } m_pdmDtc->enableChargeType(); m_aesDtc = AreaEffectSimulation::Interface::Dtc::create(m_ddsHandler->ddsConnector()); m_aesDtc->enableAll(); connect(m_aesDtc.get(), &AreaEffectSimulation::Interface::Dtc::aesItemViewChanged, this, &HandlerParticipantEvent::onAesItemViewChanged); connect(m_aesDtc.get(), &AreaEffectSimulation::Interface::Dtc::explosionSingleChanged, this, &HandlerParticipantEvent::onExplosionSingleChanged); return true; } // end of init() //----------------------------------------------------------------------------- void HandlerParticipantEvent::onAesItemViewChanged(const AreaEffectSimulation::AesItemView &data, DataModification modType) { DEBUG_LOG(m_logger) << "AesItemId: " << data.aesItemId() << ", effectState: " << data.effectStateId(); Database::EemEvent eData = m_aesMap[data.id()]; eData.sampleTime = data.effectStart(); if (data.partyId().is_set()) eData.partyId = data.partyId().get(); switch(modType) { case DataModification::eAdded: m_itemViews.insert(data.aesItemId(), data.aesTypeId()); DEBUG_LOG(m_logger) << "eAdded: AesItemId: " << data.aesItemId() << ", effectState: " << data.effectStateId(); m_aesEffectState.insert(data.aesItemId(), data.effectStateId()); if (data.aesTypeId() == AreaEffectSimulation::AesType_def::AtBorderline) { AreaEffectSimulation::AesItem aesItem; m_aesDtc->aesItemFromId(data.aesItemId(), aesItem); QJsonObject obj; obj[QStringLiteral("AesId")] = QJsonValue(aesItem.id()); obj[QStringLiteral("AesName")] = QJsonValue(QString::fromStdString(aesItem.name())); obj[QStringLiteral("AesType")] = QJsonValue(data.aesTypeId().underlying()); eData.properties = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact)); eData.eventTypeId = ProjectDataManagement::EventType::Borderline; eData.position = LtsUtils::QPoint3D(data.boundingBoxBottomLeft().lon(), data.boundingBoxBottomLeft().lat(), 0.0); if (!m_dbHandler->db()->insert(eData)) ERROR_LOG(m_logger) << "Error while inserting event data from AES "; else publishData(eData); } break; case DataModification::eModified: { AreaEffectSimulation::AesEffectState effectState = m_aesEffectState.value(data.aesItemId()); DEBUG_LOG(m_logger) << "eModified: AesItemId: " << data.aesItemId() << ", effectState: " << data.effectStateId(); DEBUG_LOG(m_logger) << "effectState: " << effectState; // insert event only once if (effectState != AreaEffectSimulation::AesEffectState::EsPrepared && (effectState == AreaEffectSimulation::AesEffectState_def::EsActivated && data.effectStateId() != AreaEffectSimulation::AesEffectState_def::EsExpired)) return; INFO_LOG(m_logger) << "new/update event: aesId: " << data.aesItemId(); m_aesEffectState.insert(data.aesItemId(), data.effectStateId()); m_itemViews.insert(data.aesItemId(), data.aesTypeId()); eData.position = LtsUtils::QPoint3D(data.boundingBoxBottomLeft().lon(), data.boundingBoxBottomLeft().lat(), 0.0); INFO_LOG(m_logger) << "aesType: " << data.aesTypeId(); switch (data.aesTypeId().underlying()) { case AreaEffectSimulation::AesType::AtMineArea: eData.eventTypeId = ProjectDataManagement::EventType::MineArea; break; case AreaEffectSimulation::AesType::AtExplosionArea: eData.eventTypeId = ProjectDataManagement::EventType::HitEffectSimulation; break; case AreaEffectSimulation::AesType::AtExplosionCluster: eData.eventTypeId = ProjectDataManagement::EventType::ExplosionCluster; break; case AreaEffectSimulation::AesType::AtMineAreaSingleMines: return; case AreaEffectSimulation::AesType::AtNContamination: eData.eventTypeId = ProjectDataManagement::EventType::NContamination; break; case AreaEffectSimulation::AesType::AtBCContamination: eData.eventTypeId = ProjectDataManagement::EventType::BCContamination; break; case AreaEffectSimulation::AesType::AtBorderline: eData.eventTypeId = ProjectDataManagement::EventType::Borderline; break; case AreaEffectSimulation::AesType::AtDangerArea: eData.eventTypeId = ProjectDataManagement::EventType::DangerArea; break; case AreaEffectSimulation::AesType::AtFunctionalArea: eData.eventTypeId = ProjectDataManagement::EventType::FunctionalArea; break; case AreaEffectSimulation::AesType::AtMineCorridor: eData.eventTypeId = ProjectDataManagement::EventType::MineCorridor; break; case AreaEffectSimulation::AesType::AtTechnicalArea: eData.eventTypeId = ProjectDataManagement::EventType::TechnicalArea; break; case AreaEffectSimulation::AesType::AtGasPipe: eData.eventTypeId = ProjectDataManagement::EventType::GasPipe; break; // No ExerciseEvent for AesType default: { INFO_LOG(m_logger) << "No ExerciseEvent for AesType:" << data.aesTypeId().underlying(); return; } } AreaEffectSimulation::AesItem aesItem; m_aesDtc->aesItemFromId(data.aesItemId(), aesItem); if (eData.eventTypeId == ProjectDataManagement::EventType::ExplosionCluster) { ProjectDataManagement::GearClass gearClass = m_pdmDtc->gearClassFromChargeTypeId(aesItem.chargeStruct().chargeTypeId()); if (gearClass == ProjectDataManagement::GearClass_def::AirSupport) eData.eventTypeId = ProjectDataManagement::EventType::AirSupport; } QJsonObject obj; obj[QStringLiteral("AesId")] = QJsonValue(aesItem.id()); obj[QStringLiteral("AesName")] = QJsonValue(QString::fromStdString(aesItem.name())); obj[QStringLiteral("AesType")] = QJsonValue(data.aesTypeId().underlying()); if (data.aesTypeId().underlying() != AreaEffectSimulation::AesType::AtBorderline) { obj[QStringLiteral("duration")] = QJsonValue(aesItem.effectDuration()); obj[QStringLiteral("ChargeTypeId")] = QJsonValue(data.chargeStruct().chargeTypeId()); obj[QStringLiteral("endTime")] = QJsonValue(data.effectStart() + aesItem.effectDuration()); } eData.properties = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact)); if (eData.id <= 0) { if (!m_dbHandler->db()->insert(eData)) { ERROR_LOG(m_logger) << "Error while inserting event data from AES "; break; } m_aesMap.insert(data.id(), eData); } else { if (!m_dbHandler->db()->update(eData)) { ERROR_LOG(m_logger) << "Error while updating event data from AES "; break; } } publishData(eData); break; } case DataModification::eRemoved: { m_itemViews.remove(data.aesItemId()); m_aesMap.remove(data.id()); break; } } } //----------------------------------------------------------------------------- void HandlerParticipantEvent::onExplosionSingleChanged(const AreaEffectSimulation::ExplosionSingle &data, DataModification modType) { Database::EemEvent eData = m_aesMap[data.id()]; if ( !data.effectStart().is_set()) { return; } eData.sampleTime = data.effectStart().get(); if (data.partyId().is_set()) eData.partyId = data.partyId().get(); switch(modType) { case DataModification::eAdded: m_aesEffectState.insert(data.id(), data.effectStateId()); break; case DataModification::eModified: { AreaEffectSimulation::AesEffectState effectState = m_aesEffectState.value(data.id()); // insert event only once if (effectState != AreaEffectSimulation::AesEffectState::EsPrepared && (effectState == AreaEffectSimulation::AesEffectState_def::EsActivated && data.effectStateId() != AreaEffectSimulation::AesEffectState_def::EsExpired)) return; m_aesEffectState.insert(data.id(), data.effectStateId()); //m_itemViews.insert(data.id(), data.aesTypeId()); eData.eventTypeId = ProjectDataManagement::EventType::FireEvent; AreaEffectSimulation::AesItem aesItem; m_aesDtc->aesItemFromId(data.id(), aesItem); QJsonObject obj; obj[QStringLiteral("AesId")] = QJsonValue(aesItem.id()); obj[QStringLiteral("AesName")] = QJsonValue(QString::fromStdString(aesItem.name())); obj[QStringLiteral("AesType")] = QJsonValue(AreaEffectSimulation::AesType::AtExplosionSingle); obj[QStringLiteral("ChargeTypeId")] = QJsonValue(data.chargeStruct().chargeTypeId()); obj[QStringLiteral("duration")] = QJsonValue(aesItem.effectDuration()); obj[QStringLiteral("endTime")] = QJsonValue(data.effectStart().get() + aesItem.effectDuration()); eData.properties = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact)); //const Core::GeoPosition geoPosition ( data.impactPosition().lon(), data.impactPosition().lat(), 0); const LtsUtils::QPoint3D pos3D (data.impactPosition().lon(), data.impactPosition().lat(), 0); eData.position = pos3D; if (eData.id <= 0) { if (!m_dbHandler->db()->insert(eData)) { ERROR_LOG(m_logger) << "Error while inserting event data from AES "; break; } m_aesMap.insert(data.id(), eData); } else { if (!m_dbHandler->db()->update(eData)) { ERROR_LOG(m_logger) << "Error while updating event data from AES "; break; } } publishData(eData); break; } default: DEBUG_LOG(m_logger) << "Nothing to do"; } } //----------------------------------------------------------------------------- void HandlerParticipantEvent::onParticipantEventChanged(const CentralEffectSimulation::ParticipantEvent &data, DataModification modType) { if (modType == DataModification::eRemoved) return; Database::EemEvent eData; eData.participantId = data.participantId(); eData.equipmentNumber = data.equipmentNumber(); eData.partyId = data.partyId(); eData.sampleTime = data.eventTime(); eData.position = LtsUtils::QPoint3D(data.position().lon(), data.position().lat(), data.position().z()); DEBUG_LOG(m_logger) << " onParticipantEventChanged " << " position=" << data.position() << " eData.position=" << eData.position; eData.eventTypeId = ProjectDataManagement::EventType_def::Unknown; QJsonObject obj; obj[QStringLiteral("AesId")] = QJsonValue(data.aesItemId()); obj[QStringLiteral("AesType")] = QJsonValue(m_itemViews.value(data.aesItemId()).underlying()); obj[QStringLiteral("AesName")] = QJsonValue(QString::fromStdString(data.aesName())); obj[QStringLiteral("FriendlyFire")] = QJsonValue(data.friendlyFire()); switch(data.eventTypeId().underlying()) { case ProjectDataManagement::EventType_def::HitEffectSimulation: eData.eventTypeId = ProjectDataManagement::EventType_def::HitEffectSimulation; break; case ProjectDataManagement::EventType_def::HitMine: eData.eventTypeId = ProjectDataManagement::EventType_def::HitMine; break; case ProjectDataManagement::EventType_def::HitN: eData.eventTypeId = ProjectDataManagement::EventType_def::HitN; break; case ProjectDataManagement::EventType_def::HitC: eData.eventTypeId = ProjectDataManagement::EventType_def::HitC; break; case ProjectDataManagement::EventType_def::EnterDangerArea: eData.eventTypeId = ProjectDataManagement::EventType_def::EnterDangerArea; break; case ProjectDataManagement::EventType_def::LeaveDangerArea: eData.eventTypeId = ProjectDataManagement::EventType_def::LeaveDangerArea; break; case ProjectDataManagement::EventType_def::EnterMineArea: eData.eventTypeId = ProjectDataManagement::EventType_def::EnterMineArea; break; case ProjectDataManagement::EventType_def::LeaveMineArea: eData.eventTypeId = ProjectDataManagement::EventType_def::LeaveMineArea; break; case ProjectDataManagement::EventType_def::EnterNuclear: eData.eventTypeId = ProjectDataManagement::EventType_def::EnterNuclear; break; case ProjectDataManagement::EventType_def::LeaveNuclear: eData.eventTypeId = ProjectDataManagement::EventType_def::LeaveNuclear; break; case ProjectDataManagement::EventType_def::CrossLocalBorderline: eData.eventTypeId = ProjectDataManagement::EventType_def::CrossLocalBorderline; obj[QStringLiteral("Global")] = QJsonValue(false); break; case ProjectDataManagement::EventType_def::CrossGlobalBorderline: eData.eventTypeId = ProjectDataManagement::EventType_def::CrossGlobalBorderline; obj[QStringLiteral("Global")] = QJsonValue(true); break; case ProjectDataManagement::EventType_def::EnterFunctionalArea: eData.eventTypeId = ProjectDataManagement::EventType_def::EnterFunctionalArea; break; case ProjectDataManagement::EventType_def::LeaveFunctionalArea: eData.eventTypeId = ProjectDataManagement::EventType_def::LeaveFunctionalArea; break; case ProjectDataManagement::EventType_def::EnterHazardDistance: eData.eventTypeId = ProjectDataManagement::EventType_def::EnterHazardDistance; break; case ProjectDataManagement::EventType_def::LeaveHazardDistance: eData.eventTypeId = ProjectDataManagement::EventType_def::LeaveHazardDistance; break; case ProjectDataManagement::EventType_def::AirSupport: eData.eventTypeId = ProjectDataManagement::EventType_def::AirSupport; break; default: return; } eData.properties = QString::fromUtf8(QJsonDocument(obj).toJson(QJsonDocument::Compact)); if (!m_dbHandler->db()->insert(eData)) ERROR_LOG(m_logger) << "Error while inserting event data from CES "; else publishData(eData); } // end of onParticipantEventChanged() //----------------------------------------------------------------------------- void HandlerParticipantEvent::publishData(const Database::EemEvent &data) { m_eventServer->sendEvent(Database::toJson(data)); ExerciseEventManagement::Event ddsData = toDdsData(data); if (!m_ddsHandler->dts()->publish(ddsData)) ERROR_LOG(m_logger) << "Error publishing event-data to dds"; // take data from dds-bus after 10s, we need just to make sure the recording service got it m_mainHandler->addEventIdToRemove(ddsData.id()); } ExerciseEventManagement::Event HandlerParticipantEvent::toDdsData(const Database::EemEvent &data) { ExerciseEventManagement::Event ddsData; ddsData.id(data.id); ddsData.sampleTime(data.sampleTime); ddsData.participantId(data.participantId); ddsData.partyId(data.partyId); ddsData.equipmentNumber(data.equipmentNumber); ddsData.eventTypeId(data.eventTypeId); ddsData.properties(data.properties.toStdString()); // Core::GeoPosition LtsUtils::QPoint3D Core::GeoPosition position(data.position.x(), data.position.y(),data.position.z()); ddsData.position(position); return ddsData; }