From 1a58301d4fad8352d6b09721d566766d56234147 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Sat, 13 Nov 2021 23:03:34 +0100 Subject: [PATCH] send the reports every ten seconds --- src/PlugIn.cpp | 37 ++++++++++++++++++++++++++++++++++--- src/PlugIn.h | 7 +++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/PlugIn.cpp b/src/PlugIn.cpp index f3a0c5f..b8c5b4f 100644 --- a/src/PlugIn.cpp +++ b/src/PlugIn.cpp @@ -34,7 +34,10 @@ PlugIn::PlugIn() : PLUGIN_VERSION, PLUGIN_DEVELOPER, PLUGIN_COPYRIGHT), - m_configuration() { + m_configuration(), + m_screen(), + m_updateQueueLock(), + m_updateQueue() { GOOGLE_PROTOBUF_VERIFY_VERSION; this->DisplayUserMessage(PLUGIN_NAME, "INFO", (std::string("Loaded ") + PLUGIN_NAME + " " + PLUGIN_VERSION).c_str(), true, true, false, false, false); @@ -201,8 +204,8 @@ void PlugIn::generateAircraftReportMessage(const EuroScopePlugIn::CRadarTarget& std::string iafName; for (auto element = this->SectorFileElementSelectFirst(EuroScopePlugIn::SECTOR_ELEMENT_STAR); - true == element.IsValid(); - element = this->SectorFileElementSelectNext(element, EuroScopePlugIn::SECTOR_ELEMENT_STAR)) + true == element.IsValid(); + element = this->SectorFileElementSelectNext(element, EuroScopePlugIn::SECTOR_ELEMENT_STAR)) { auto split = String::splitString(element.GetName(), " "); @@ -260,6 +263,34 @@ void PlugIn::generateAircraftReportMessage(const EuroScopePlugIn::CRadarTarget& auto reportTime = std::chrono::utc_clock::now(); stream << std::format("{0:%Y%m%d%H%M%S}", reportTime); report->set_reporttime(String::splitString(stream.str(), ".")[0]); +} + +void PlugIn::OnRadarTargetPositionUpdate(EuroScopePlugIn::CRadarTarget radarTarget) { + /* do nothing if the reporter is not initialized and ignore invalid targets */ + if (false == BackendNotification::instance().initialized() || false == radarTarget.IsValid()) + return; + + std::lock_guard guard(this->m_updateQueueLock); + this->m_updateQueue.push_back(radarTarget.GetCallsign()); +} + +void PlugIn::OnTimer(int counter) { + if (false == BackendNotification::instance().initialized() || 0 != (counter % 10)) + return; + + this->m_updateQueueLock.lock(); + aman::AircraftUpdate update; + + for (auto target = this->RadarTargetSelectFirst(); true == target.IsValid(); target = this->RadarTargetSelectNext(target)) { + auto it = std::find(this->m_updateQueue.cbegin(), this->m_updateQueue.cend(), target.GetCallsign()); + if (this->m_updateQueue.cend() != it) { + auto report = update.add_reports(); + this->generateAircraftReportMessage(target, report); + } + } + + this->m_updateQueue.clear(); + this->m_updateQueueLock.unlock(); /* send the report */ if (false == BackendNotification::instance().send(update)) diff --git a/src/PlugIn.h b/src/PlugIn.h index 30491fd..bda4a27 100644 --- a/src/PlugIn.h +++ b/src/PlugIn.h @@ -46,6 +46,8 @@ namespace aman { Communication m_configuration; std::shared_ptr m_screen; + std::mutex m_updateQueueLock; + std::list m_updateQueue; public: /** @@ -73,6 +75,11 @@ namespace aman { */ EuroScopePlugIn::CRadarScreen* OnRadarScreenCreated(const char* displayName, bool needsRadarContent, bool geoReferenced, bool canBeSaved, bool canBeCreated) override; + /** + * @brief Called every second + * @param[in] counter The counter that indicates the seconds + */ + void OnTimer(int counter) override; /** * @brief Called as soon as a radar target position is updated * @param[in] radarTarget The updated radar target