Browse Source

send the reports every ten seconds

Sven Czarnian 3 years ago
parent
commit
1a58301d4f
2 changed files with 41 additions and 3 deletions
  1. 34 3
      src/PlugIn.cpp
  2. 7 0
      src/PlugIn.h

+ 34 - 3
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))

+ 7 - 0
src/PlugIn.h

@@ -46,6 +46,8 @@ namespace aman {
 
         Communication                m_configuration;
         std::shared_ptr<RadarScreen> m_screen;
+        std::mutex                   m_updateQueueLock;
+        std::list<std::string>       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