ADD: added automatic delete of lost tracks

This commit is contained in:
Henry Winkel
2024-03-14 17:48:54 +01:00
parent d56df03ed4
commit 87368640e2
5 changed files with 128 additions and 72 deletions

View File

@@ -17,6 +17,7 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <loguru.hpp> #include <loguru.hpp>
#include <SimCore/CallBackTimer.hpp>
namespace TrackList namespace TrackList
{ {
@@ -25,6 +26,10 @@ namespace TrackList
{ {
public: public:
TrackList(); TrackList();
~TrackList();
TrackList(const TrackList &other);
TrackList& operator=(const TrackList & oter);
void addTrack(std::shared_ptr<SimCore::SensorTrack> Track); void addTrack(std::shared_ptr<SimCore::SensorTrack> Track);
std::shared_ptr<SimCore::SensorTrack> getTrack(SimCore::Identifier); std::shared_ptr<SimCore::SensorTrack> getTrack(SimCore::Identifier);
@@ -36,12 +41,22 @@ namespace TrackList
void getJsonTRackList(nlohmann::json &message); void getJsonTRackList(nlohmann::json &message);
std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> getTrackStore(); std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> getTrackStore();
/**
* @brief cheks the trackstore for old tracks to delete
*
*/
void checkTrackStore();
private: private:
mutable std::mutex mx_; mutable std::mutex mx_;
std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> TrackStore_; std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> TrackStore_;
/// seconds after the last updated a track gets deleted
const int trackNoUpdateTime = 10;
CallBackTimer timer_;
}; };

View File

@@ -1,4 +1,5 @@
#include <Entities/Tracklist/Tracklist.hpp> #include <Entities/Tracklist/Tracklist.hpp>
#include <chrono>
#include "SimCore/Messages/SensorTrack.hpp" #include "SimCore/Messages/SensorTrack.hpp"
#include "SimCore/Messages/SimTrack.hpp" #include "SimCore/Messages/SimTrack.hpp"
@@ -7,88 +8,126 @@
namespace TrackList namespace TrackList
{ {
TrackList::TrackList() TrackList::TrackList():mx_()
{ {
TrackStore_ = std::map<std::string, std::shared_ptr<SimCore::SensorTrack>>(); TrackStore_ = std::map<std::string, std::shared_ptr<SimCore::SensorTrack>>();
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<SimCore::SensorTrack> Track)
{
std::lock_guard<std::mutex> 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<SimCore::SensorTrack> TrackList::getTrack(const SimCore::Identifier ID)
{
std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID.getUUID());
return it->second;
}
std::shared_ptr<SimCore::SensorTrack> TrackList::getTrackBySringID(std::string ID)
{
std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID);
return it->second;
}
void TrackList::deleteTrack(std::string ID)
{
std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID);
TrackStore_.erase(it);
}
void TrackList::deleteTrack(SimCore::Identifier ID)
{
std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID.getUUID());
TrackStore_.erase(it);
}
std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> TrackList::getTrackStore()
{
return TrackStore_;
} }
void TrackList::addTrack(std::shared_ptr<SimCore::SensorTrack> Track)
void TrackList::getJsonTRackList(nlohmann::json &message)
{
std::lock_guard<std::mutex> lock(mx_);
try {
for (std::map<std::string, std::shared_ptr<SimCore::SensorTrack>>::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<std::mutex> lock(mx_); std::lock_guard<std::mutex> lock(mx_);
return TrackStore_.size();
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<SimCore::SensorTrack> TrackList::getTrack(const SimCore::Identifier ID)
{
std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID.getUUID());
return it->second;
}
std::shared_ptr<SimCore::SensorTrack> TrackList::getTrackBySringID(std::string ID) void TrackList::checkTrackStore()
{ {
std::lock_guard<std::mutex> lock(mx_); std::lock_guard<std::mutex> lock(mx_);
for (auto [key, value] : TrackStore_)
auto it = TrackStore_.find(ID); {
return it->second; // value->getSensorTrackMessage()
} if(value->getLastUpdateTime() >= std::chrono::system_clock::now() + std::chrono::seconds(trackNoUpdateTime))
{
void TrackList::deleteTrack(std::string ID) auto it = TrackStore_.find(key);
{ TrackStore_.erase(it);
std::lock_guard<std::mutex> lock(mx_); }
auto it = TrackStore_.find(ID);
TrackStore_.erase(it);
}
void TrackList::deleteTrack(SimCore::Identifier ID)
{
std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID.getUUID());
TrackStore_.erase(it);
}
std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> TrackList::getTrackStore()
{
return TrackStore_;
}
void TrackList::getJsonTRackList(nlohmann::json &message)
{
std::lock_guard<std::mutex> lock(mx_);
try {
for (std::map<std::string, std::shared_ptr<SimCore::SensorTrack>>::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<std::mutex> lock(mx_);
return TrackStore_.size();
}

View File

@@ -242,7 +242,9 @@ SCENARIO("Testing the SimCore SensorManager with local sensors")
REQUIRE(SensorManager_->getTrackListUpdateRaw()->getSensors().size() == 4); REQUIRE(SensorManager_->getTrackListUpdateRaw()->getSensors().size() == 4);
REQUIRE(SensorManager_->getTrackListUpdateRaw()->getSensors().at(0).getTracksCount() == 3); 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(); SensorManager_->stop();