ADD: updated Tracklist and TracklistItem with tests
This commit is contained in:
@@ -3,85 +3,32 @@
|
||||
#include <Entities/Tracklist/TracklistItem.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
#include <tuple>
|
||||
|
||||
namespace TrackList {
|
||||
|
||||
TracklistItem::TracklistItem(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData):trackID_(track->getIdentifier())
|
||||
TracklistItem::TracklistItem(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData):
|
||||
SimCore::SimTrack(*track.get())
|
||||
{
|
||||
|
||||
updateTrack(track,sensorData);
|
||||
addSensorDataToSensorList(sensorData);
|
||||
|
||||
addSensorDataToSensorList(sensorData);
|
||||
lastUpdateTimestamp_ = std::chrono::steady_clock::now();
|
||||
|
||||
}
|
||||
|
||||
TracklistItem::TracklistItem(std::shared_ptr<SimCore::SimTrack> track):trackID_(track->getIdentifier())
|
||||
TracklistItem::TracklistItem(std::shared_ptr<SimCore::SimTrack> track):
|
||||
SimCore::SimTrack(*track.get())
|
||||
{
|
||||
updateTrack(track);
|
||||
lastUpdateTimestamp_ = std::chrono::steady_clock::now();
|
||||
|
||||
updateTrack(track);
|
||||
|
||||
}
|
||||
|
||||
SimCore::Identifier TracklistItem::getID()
|
||||
{
|
||||
return trackID_;
|
||||
}
|
||||
|
||||
|
||||
void TracklistItem::setPosition(SimCore::Position position)
|
||||
{
|
||||
position_ = position;
|
||||
}
|
||||
|
||||
SimCore::Position TracklistItem::getPosition()
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
void TracklistItem::setSpeed(double speed)
|
||||
{
|
||||
speed_ = speed;
|
||||
}
|
||||
|
||||
double TracklistItem::getSpeed()
|
||||
{
|
||||
return speed_;
|
||||
}
|
||||
|
||||
void TracklistItem::setCourse(double course)
|
||||
{
|
||||
course_ = course;
|
||||
}
|
||||
|
||||
double TracklistItem::getCourse()
|
||||
{
|
||||
return course_;;
|
||||
}
|
||||
|
||||
void TracklistItem::setPitch(double pitch)
|
||||
{
|
||||
pitch_ = pitch;
|
||||
}
|
||||
|
||||
double TracklistItem::getpitch()
|
||||
{
|
||||
return pitch_;
|
||||
}
|
||||
|
||||
double TracklistItem::getBearing()
|
||||
{
|
||||
return bearing_;
|
||||
}
|
||||
double TracklistItem::getRange()
|
||||
{
|
||||
return range_;
|
||||
}
|
||||
|
||||
SimCore::ObjectSource TracklistItem::getObjectSource()
|
||||
{
|
||||
return ObjectSource_;
|
||||
}
|
||||
|
||||
|
||||
std::chrono::time_point<std::chrono::steady_clock> TracklistItem::getLastUpdateTimestamp()
|
||||
{
|
||||
@@ -91,33 +38,41 @@ namespace TrackList {
|
||||
|
||||
void TracklistItem::updateTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData)
|
||||
{
|
||||
|
||||
|
||||
position_ = track->getPosition();
|
||||
course_ = track->Course.getValue();
|
||||
speed_ = track->Speed.getValue();
|
||||
lastUpdateTimestamp_ = std::chrono::steady_clock::now();
|
||||
if(this->getIdentifier() == track->getIdentifier())
|
||||
{
|
||||
|
||||
this->setPosition(track->getPosition());
|
||||
this->Speed.setValue(track->Speed.getValue());
|
||||
this->Course.setValue(track->Course.getValue());
|
||||
|
||||
|
||||
|
||||
|
||||
if (isSensorinSensorlist(sensorData) != true) {
|
||||
addSensorDataToSensorList(sensorData);
|
||||
}
|
||||
|
||||
if (!isSensorinSensorlist(sensorData))
|
||||
{
|
||||
SensorList.push_back(sensorData);
|
||||
}
|
||||
lastUpdateTimestamp_ = std::chrono::steady_clock::now();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void TracklistItem::updateTrack(std::shared_ptr<SimCore::SimTrack> track )
|
||||
{
|
||||
|
||||
position_ = track->getPosition();
|
||||
course_ = track->Course.getValue();
|
||||
speed_ = track->Speed.getValue();
|
||||
lastUpdateTimestamp_ = std::chrono::steady_clock::now();
|
||||
|
||||
|
||||
|
||||
if(this->getIdentifier() == track->getIdentifier())
|
||||
{
|
||||
this->setPosition(track->getPosition());
|
||||
this->Speed.setValue(track->Speed.getValue());
|
||||
this->Course.setValue(track->Course.getValue());
|
||||
|
||||
}
|
||||
lastUpdateTimestamp_ = std::chrono::steady_clock::now();
|
||||
|
||||
}
|
||||
|
||||
size_t TracklistItem::getSensorCount()
|
||||
{
|
||||
return SensorList.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
#include "SimCore/Identifier.hpp"
|
||||
#include "SimCore/Messages/SimTrack.hpp"
|
||||
#include <Entities/Tracklist/Tracklist.hpp>
|
||||
#include <ranges>
|
||||
#include <utility>
|
||||
|
||||
|
||||
|
||||
#define TRACK_TIMEOUT 5 * 60 *1000
|
||||
|
||||
namespace TrackList
|
||||
{
|
||||
|
||||
TrackList::TrackList(SimCore::Identifier OwnID):OwnID_(OwnID)
|
||||
TrackList::TrackList(SimCore::Identifier OwnID):OwnID_(OwnID),TrackTimeout_(5 * 60 *1000)
|
||||
{
|
||||
stopSanitizer_ = false;
|
||||
sanitizerThread_ = std::thread(&TrackList::tracklistSanitizer,this);
|
||||
@@ -31,90 +32,40 @@ namespace TrackList
|
||||
|
||||
|
||||
|
||||
SimCore::Identifier TrackList::getTrackID(SimCore::ObjectSource source)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
return *IDMaker.getNewIdentifier(source).get();
|
||||
}
|
||||
|
||||
|
||||
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData)
|
||||
{
|
||||
|
||||
auto AllIDs = getAllIDs();
|
||||
// std::unique_lock<std::mutex> lock(mutex_);
|
||||
// lock.unlock();
|
||||
|
||||
if (AllIDs.size() == 0) {
|
||||
addNewTrack( track, sensorData);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto ID:AllIDs) {
|
||||
// std::lock_guard<std::mutex> lock(mutex_);
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
auto TracklistItem = TrackList_.find(ID.serialize());
|
||||
auto iterator = TrackList_.find(track->getIdentifier().getUUID());
|
||||
if (iterator == TrackList_.end()) {
|
||||
auto item = std::make_shared<TracklistItem>( track,sensorData);
|
||||
TrackList_.emplace(track->getIdentifier().getUUID(),item);
|
||||
|
||||
if (TracklistItem->second->isSensorIDKnown(sensorData.sensorID) == true) {
|
||||
|
||||
TracklistItem->second->updateTrack(track, sensorData);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// lock.unlock();
|
||||
|
||||
addNewTrack( track, sensorData);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}else {
|
||||
iterator->second->updateTrack(track,sensorData);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief
|
||||
/// @param track
|
||||
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> track)
|
||||
|
||||
|
||||
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> track)
|
||||
{
|
||||
|
||||
auto AllIDs = getAllIDs();
|
||||
// std::unique_lock<std::mutex> lock(mutex_);
|
||||
// lock.unlock();
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
if (AllIDs.size() == 0) {
|
||||
addNewTrack( track);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto ID:AllIDs) {
|
||||
// std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto iterator = TrackList_.find(track->getIdentifier().getUUID());
|
||||
if (iterator == TrackList_.end()) {
|
||||
auto item = std::make_shared<TracklistItem>( track);
|
||||
TrackList_.emplace(track->getIdentifier().getUUID(),item);
|
||||
|
||||
auto TracklistItem = TrackList_.find(ID.serialize());
|
||||
|
||||
if (TracklistItem != TrackList_.end()) {
|
||||
|
||||
TracklistItem->second->updateTrack(track);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// lock.unlock();
|
||||
|
||||
addNewTrack( track);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}else {
|
||||
iterator->second->updateTrack(track);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// lock.unlock();
|
||||
|
||||
}
|
||||
|
||||
void TrackList::addNewTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData)
|
||||
@@ -123,8 +74,9 @@ namespace TrackList
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto item = std::make_shared<TracklistItem>( track, sensorData);
|
||||
std::string id = track->getIdentifier().serialize();
|
||||
TrackList_.emplace(id,item);
|
||||
|
||||
TrackList_.emplace(std::make_pair(track->getIdentifier().getUUID(),item));
|
||||
|
||||
}
|
||||
|
||||
void TrackList::addNewTrack(std::shared_ptr<SimCore::SimTrack> track)
|
||||
@@ -132,8 +84,7 @@ namespace TrackList
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto item = std::make_shared<TracklistItem>( track);
|
||||
std::string id = track->getIdentifier().serialize();
|
||||
TrackList_.emplace(id,item);
|
||||
TrackList_.emplace(track->getIdentifier().getUUID(),item);
|
||||
}
|
||||
|
||||
|
||||
@@ -142,7 +93,7 @@ namespace TrackList
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
std::shared_ptr<TracklistItem> result = nullptr;
|
||||
result = TrackList_.at(TrackID.serialize());
|
||||
result = TrackList_.at(TrackID.getUUID());
|
||||
if (result == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -155,7 +106,7 @@ namespace TrackList
|
||||
|
||||
std::vector<SimCore::Identifier> list;
|
||||
for (const auto& [key,value] : TrackList_) {
|
||||
list.emplace_back(SimCore::Identifier(key));
|
||||
list.emplace_back(key);
|
||||
}
|
||||
|
||||
return list;
|
||||
@@ -168,6 +119,15 @@ namespace TrackList
|
||||
|
||||
}
|
||||
|
||||
void TrackList::setTrackTimeout(int millseconds)
|
||||
{
|
||||
this->TrackTimeout_ = millseconds;
|
||||
}
|
||||
|
||||
int TrackList::getTrackTimeoutValue()
|
||||
{
|
||||
return TrackTimeout_;
|
||||
}
|
||||
|
||||
|
||||
void TrackList::tracklistSanitizer()
|
||||
@@ -182,9 +142,11 @@ namespace TrackList
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
std::chrono::milliseconds::rep duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - it->second->getLastUpdateTimestamp() ).count();
|
||||
|
||||
if (duration >= TRACK_TIMEOUT) {
|
||||
if (duration >= TrackTimeout_) {
|
||||
|
||||
it = TrackList_.erase(it);
|
||||
LOG_S(INFO)<<"Erased Track";
|
||||
|
||||
|
||||
}else
|
||||
{
|
||||
@@ -194,9 +156,10 @@ namespace TrackList
|
||||
lock.unlock() ;
|
||||
lock.lock();
|
||||
}
|
||||
// LOG_S(INFO)<<"running";
|
||||
|
||||
lock.unlock();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
lock.release();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user