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 <thread>
#include <loguru.hpp>
#include <SimCore/CallBackTimer.hpp>
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<SimCore::SensorTrack> Track);
std::shared_ptr<SimCore::SensorTrack> getTrack(SimCore::Identifier);
@@ -37,11 +42,21 @@ namespace TrackList
void getJsonTRackList(nlohmann::json &message);
std::map<std::string, std::shared_ptr<SimCore::SensorTrack>> getTrackStore();
/**
* @brief cheks the trackstore for old tracks to delete
*
*/
void checkTrackStore();
private:
mutable std::mutex mx_;
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 <chrono>
#include "SimCore/Messages/SensorTrack.hpp"
#include "SimCore/Messages/SimTrack.hpp"
@@ -7,10 +8,28 @@
namespace TrackList
{
TrackList::TrackList()
TrackList::TrackList():mx_()
{
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)
@@ -91,6 +110,26 @@ namespace TrackList
}
void TrackList::checkTrackStore()
{
std::lock_guard<std::mutex> lock(mx_);
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);
}
}
}

View File

@@ -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();