From 87368640e25a0f352287080fa1198a48e96c15c7 Mon Sep 17 00:00:00 2001 From: Henry Winkel Date: Thu, 14 Mar 2024 17:48:54 +0100 Subject: [PATCH] ADD: added automatic delete of lost tracks --- include/Entities/Tracklist/Tracklist.hpp | 17 ++- libs/KubeControl | 2 +- libs/OrderLibrary | 2 +- src/Entities/Tracklist/Tracklist.cpp | 175 ++++++++++++++--------- tests/test_SensorManager.cpp | 4 +- 5 files changed, 128 insertions(+), 72 deletions(-) diff --git a/include/Entities/Tracklist/Tracklist.hpp b/include/Entities/Tracklist/Tracklist.hpp index 4a7a646..b6ab222 100644 --- a/include/Entities/Tracklist/Tracklist.hpp +++ b/include/Entities/Tracklist/Tracklist.hpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace TrackList { @@ -25,6 +26,10 @@ namespace TrackList { public: TrackList(); + ~TrackList(); + TrackList(const TrackList &other); + TrackList& operator=(const TrackList & oter); + void addTrack(std::shared_ptr Track); std::shared_ptr getTrack(SimCore::Identifier); @@ -36,12 +41,22 @@ namespace TrackList void getJsonTRackList(nlohmann::json &message); std::map> getTrackStore(); - + + /** + * @brief cheks the trackstore for old tracks to delete + * + */ + void checkTrackStore(); private: mutable std::mutex mx_; std::map> TrackStore_; + /// seconds after the last updated a track gets deleted + const int trackNoUpdateTime = 10; + + CallBackTimer timer_; + }; diff --git a/libs/KubeControl b/libs/KubeControl index e38214f..ada77d0 160000 --- a/libs/KubeControl +++ b/libs/KubeControl @@ -1 +1 @@ -Subproject commit e38214f4d0a9e2a17ba34011324d6284dd6f5a80 +Subproject commit ada77d0e4595f7a8b64e0afc92633c18d33223cf diff --git a/libs/OrderLibrary b/libs/OrderLibrary index 20c1465..c72e6c0 160000 --- a/libs/OrderLibrary +++ b/libs/OrderLibrary @@ -1 +1 @@ -Subproject commit 20c1465b53e3cb2794910cc81b00bb7620be87ff +Subproject commit c72e6c0d8ba4efbcf75f6b6efd8688bdbd851f01 diff --git a/src/Entities/Tracklist/Tracklist.cpp b/src/Entities/Tracklist/Tracklist.cpp index 5d15345..7ced3f9 100644 --- a/src/Entities/Tracklist/Tracklist.cpp +++ b/src/Entities/Tracklist/Tracklist.cpp @@ -1,4 +1,5 @@ #include +#include #include "SimCore/Messages/SensorTrack.hpp" #include "SimCore/Messages/SimTrack.hpp" @@ -7,88 +8,126 @@ namespace TrackList { - TrackList::TrackList() - { + TrackList::TrackList():mx_() + { - TrackStore_ = std::map>(); + TrackStore_ = std::map>(); + timer_.start(1000, std::bind(&TrackList::checkTrackStore,this)); + } + + + TrackList::~TrackList() + { + if (timer_.is_running()) { + timer_.stop(); + } + } + TrackList::TrackList(const TrackList &other):mx_() + { + this->TrackStore_ = other.TrackStore_; + } + TrackList& TrackList::operator=(const TrackList & other) + { + this->TrackStore_ = other.TrackStore_; + return *this; + } + + void TrackList::addTrack(std::shared_ptr Track) + { + std::lock_guard lock(mx_); + + auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track); + // LOG_S(INFO)<< Track->Name.getValue()<<" was: " << (inserted ? "inserted: " : "ignored: "); + if (!inserted) { TrackStore_[Track->getIdentifier().getUUID()] = Track; } + + + } + std::shared_ptr TrackList::getTrack(const SimCore::Identifier ID) + { + std::lock_guard lock(mx_); + + auto it = TrackStore_.find(ID.getUUID()); + return it->second; + } + + std::shared_ptr TrackList::getTrackBySringID(std::string ID) + { + std::lock_guard lock(mx_); + + auto it = TrackStore_.find(ID); + return it->second; + } + + void TrackList::deleteTrack(std::string ID) + { + std::lock_guard lock(mx_); + auto it = TrackStore_.find(ID); + TrackStore_.erase(it); + } + + void TrackList::deleteTrack(SimCore::Identifier ID) + { + std::lock_guard lock(mx_); + auto it = TrackStore_.find(ID.getUUID()); + TrackStore_.erase(it); + } + + std::map> TrackList::getTrackStore() + { + return TrackStore_; } - void TrackList::addTrack(std::shared_ptr Track) + + void TrackList::getJsonTRackList(nlohmann::json &message) + { + std::lock_guard lock(mx_); + try { + for (std::map>::iterator it=TrackStore_.begin(); it!=TrackStore_.end(); ++it) + { + nlohmann::json j; + j["id"] = it->first; + j["Name"] = it->second->Name.getValue(); + j["Course"] = it->second->Course.getValue(); + j["Speed"] = it->second->Speed.getValue(); + j["External"] = false; + j["Position"] = {it->second->getPosition().getGeodesicPos()[SimCore::LATITUDE], it->second->getPosition().getGeodesicPos()[SimCore::LONGITUDE]}; + j["Height"] = it->second->getPosition().getGeodesicPos()[SimCore::HEIGHT]; + j["Type"] = SimCore::Kind::EntityKindMap[it->second->EntityKind.getValue()]; + j["Side"] = SimCore::Side::EntitySideMap[it->second->EntitySide.getValue()]; + + message["Entities"].push_back(j); + } + }catch (const std::exception e) { + LOG_S(ERROR)<< e.what(); + } + } + + size_t TrackList::getSize() { std::lock_guard lock(mx_); - - auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track); - // LOG_S(INFO)<< Track->Name.getValue()<<" was: " << (inserted ? "inserted: " : "ignored: "); - if (!inserted) { TrackStore_[Track->getIdentifier().getUUID()] = Track; } - + return TrackStore_.size(); } - std::shared_ptr TrackList::getTrack(const SimCore::Identifier ID) - { - std::lock_guard lock(mx_); - auto it = TrackStore_.find(ID.getUUID()); - return it->second; - } - std::shared_ptr TrackList::getTrackBySringID(std::string ID) + void TrackList::checkTrackStore() { std::lock_guard lock(mx_); - - auto it = TrackStore_.find(ID); - return it->second; - } - - void TrackList::deleteTrack(std::string ID) - { - std::lock_guard lock(mx_); - auto it = TrackStore_.find(ID); - TrackStore_.erase(it); - } - - void TrackList::deleteTrack(SimCore::Identifier ID) - { - std::lock_guard lock(mx_); - auto it = TrackStore_.find(ID.getUUID()); - TrackStore_.erase(it); - } - - std::map> TrackList::getTrackStore() - { - return TrackStore_; - } - - - void TrackList::getJsonTRackList(nlohmann::json &message) - { - std::lock_guard lock(mx_); - try { - for (std::map>::iterator it=TrackStore_.begin(); it!=TrackStore_.end(); ++it) - { - nlohmann::json j; - j["id"] = it->first; - j["Name"] = it->second->Name.getValue(); - j["Course"] = it->second->Course.getValue(); - j["Speed"] = it->second->Speed.getValue(); - j["External"] = false; - j["Position"] = {it->second->getPosition().getGeodesicPos()[SimCore::LATITUDE], it->second->getPosition().getGeodesicPos()[SimCore::LONGITUDE]}; - j["Height"] = it->second->getPosition().getGeodesicPos()[SimCore::HEIGHT]; - j["Type"] = SimCore::Kind::EntityKindMap[it->second->EntityKind.getValue()]; - j["Side"] = SimCore::Side::EntitySideMap[it->second->EntitySide.getValue()]; - - message["Entities"].push_back(j); - } - }catch (const std::exception e) { - LOG_S(ERROR)<< e.what(); + for (auto [key, value] : TrackStore_) + { + // value->getSensorTrackMessage() + if(value->getLastUpdateTime() >= std::chrono::system_clock::now() + std::chrono::seconds(trackNoUpdateTime)) + { + auto it = TrackStore_.find(key); + TrackStore_.erase(it); + } + + } + + } - size_t TrackList::getSize() - { - std::lock_guard lock(mx_); - return TrackStore_.size(); - - } diff --git a/tests/test_SensorManager.cpp b/tests/test_SensorManager.cpp index c1b3ce6..c7f05ae 100644 --- a/tests/test_SensorManager.cpp +++ b/tests/test_SensorManager.cpp @@ -242,7 +242,9 @@ SCENARIO("Testing the SimCore SensorManager with local sensors") REQUIRE(SensorManager_->getTrackListUpdateRaw()->getSensors().size() == 4); REQUIRE(SensorManager_->getTrackListUpdateRaw()->getSensors().at(0).getTracksCount() == 3); - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::this_thread::sleep_for(std::chrono::milliseconds(10000)); + + REQUIRE(SensorManager_->getTrackListUpdateRaw()->getSensors().at(0).getTracksCount() == 0); SensorManager_->stop();