ADD: added new Tracklist and some changes to Entity class

This commit is contained in:
Henry Winkel
2023-11-08 15:08:05 +01:00
parent ab32c5e8df
commit 6d20c8de43
4 changed files with 99 additions and 194 deletions

View File

@@ -53,6 +53,7 @@ namespace Entities {
Entity(const SimCore::Identifier OwnID, Entity(const SimCore::Identifier OwnID,
std::string EnttityName, std::string EnttityName,
WHISPER::SourceType OwnType, WHISPER::SourceType OwnType,
double RadarCrossSection,
SimCore::Kind::EntityKind EntityKind, SimCore::Kind::EntityKind EntityKind,
SimCore::Side::EntitySide EntitySide, SimCore::Side::EntitySide EntitySide,
std::string GroundTruthAddr, std::string GroundTruthAddr,
@@ -73,6 +74,7 @@ namespace Entities {
virtual void childWorker() = 0; virtual void childWorker() = 0;
virtual void stopChild() = 0;
protected: protected:
@@ -82,6 +84,7 @@ namespace Entities {
std::string EntityName_; std::string EntityName_;
SimCore::Kind::EntityKind EntityKind_; SimCore::Kind::EntityKind EntityKind_;
SimCore::Side::EntitySide EntitySide_; SimCore::Side::EntitySide EntitySide_;
double RCS_;
ushort MovemntWorkerPort_; ushort MovemntWorkerPort_;
@@ -91,6 +94,9 @@ namespace Entities {
std::shared_ptr<WHISPER::InternalUDPSender> BroadcastServer_; std::shared_ptr<WHISPER::InternalUDPSender> BroadcastServer_;
std::string GroundTruthAddr_;
std::uint32_t GroundTruthPort_;
private: private:

View File

@@ -4,6 +4,7 @@
#include "SimCore/Identifier.hpp" #include "SimCore/Identifier.hpp"
#include "SimCore/IdentifierMaker.hpp" #include "SimCore/IdentifierMaker.hpp"
#include "SimCore/Messages/SimTrack.hpp" #include "SimCore/Messages/SimTrack.hpp"
#include "nlohmann/json.hpp"
#include <atomic> #include <atomic>
#include <cstddef> #include <cstddef>
#include <list> #include <list>
@@ -22,46 +23,22 @@ namespace TrackList
class TrackList class TrackList
{ {
public: public:
TrackList(SimCore::Identifier OwnID); TrackList();
~TrackList(); void addTrack(std::shared_ptr<SimCore::SimTrack> Track);
std::shared_ptr<SimCore::SimTrack> getTrack(SimCore::Identifier);
void stopSanitizer(); void deleteTrack(std::string ID);
void deleteTrack(SimCore::Identifier);
std::shared_ptr<SimCore::SimTrack> getTrackBySringID(std::string ID);
// SimCore::Identifier getTrackID(SimCore::ObjectSource source); size_t getSize();
void addTrack(std::shared_ptr<SimCore::SimTrack> track); void getJsonTRackList(nlohmann::json &message);
void addTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData);
std::shared_ptr<TracklistItem> getTrack(SimCore::Identifier TrackID);
std::vector<SimCore::Identifier> getAllIDs();
size_t size();
void setTrackTimeout(int millseconds);
int getTrackTimeoutValue();
private: private:
mutable std::mutex mx_;
const SimCore::Identifier OwnID_; std::map<std::string, std::shared_ptr<SimCore::SimTrack>> TrackStore_;
SimCore::IdentifierMaker IDMaker;
void tracklistSanitizer();
void addNewTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData);
void addNewTrack(std::shared_ptr<SimCore::SimTrack> track);
std::map<std::string, std::shared_ptr<TracklistItem>> TrackList_;
mutable std::mutex mutex_;
std::thread sanitizerThread_;
std::atomic_bool sanitizerIsRunning_;
std::atomic_bool stopSanitizer_;
int TrackTimeout_ = 1000;
}; };

View File

@@ -38,6 +38,7 @@ namespace Entities
Entity::Entity(const SimCore::Identifier OwnID, Entity::Entity(const SimCore::Identifier OwnID,
std::string EnttityName, std::string EnttityName,
WHISPER::SourceType OwnType, WHISPER::SourceType OwnType,
double RadarCrossSection,
SimCore::Kind::EntityKind EntityKind, SimCore::Kind::EntityKind EntityKind,
SimCore::Side::EntitySide EntitySide, SimCore::Side::EntitySide EntitySide,
std::string GroundTruthAddr, std::string GroundTruthAddr,
@@ -46,13 +47,17 @@ namespace Entities
bool online): bool online):
EntityName_(EnttityName), EntityName_(EnttityName),
EntityKind_(EntityKind), EntityKind_(EntityKind),
RCS_(RadarCrossSection),
EntitySide_(EntitySide), EntitySide_(EntitySide),
GroundTruthPort_(GroundTruthPort),
GroundTruthAddr_(GroundTruthAddr),
online_(online) online_(online)
{ {
PodController_ = std::make_unique<kubecontrol::PodController>("docs/config"); PodController_ = std::make_unique<kubecontrol::PodController>("docs/config");
OwnShipTrack = std::make_shared<SimCore::SimTrack>(OwnID, EnttityName, EntityKind,EntitySide); OwnShipTrack = std::make_shared<SimCore::SimTrack>(OwnID, EnttityName, EntityKind,EntitySide);
OwnShipTrack->setPosition(SimCore::Position()); OwnShipTrack->setPosition(SimCore::Position());
OwnShipTrack->RCS.setValue(RCS_);
MovemtServer_ = std::make_shared<DirectCommunication::DirectCommunicationServer>(__MOVEMENT_SERVER_PORT__); MovemtServer_ = std::make_shared<DirectCommunication::DirectCommunicationServer>(__MOVEMENT_SERVER_PORT__);
@@ -131,7 +136,7 @@ namespace Entities
void Entity::stop() void Entity::stop()
{ {
stopChild();
PodController_->stopAllPods(); PodController_->stopAllPods();
@@ -205,7 +210,7 @@ namespace Entities
if (newTrack != nullptr) if (newTrack != nullptr)
{ {
OwnShipTrack->setPosition(newTrack->getPosition()); OwnShipTrack->setPosition(newTrack->getPosition());
LOG_S(INFO)<< "new POS: LAT: "<< OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LATITUDE) << " LON: " << OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LONGITUDE); // LOG_S(INFO)<< "new POS: LAT: "<< OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LATITUDE) << " LON: " << OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LONGITUDE);
} }
} }

View File

@@ -1,169 +1,86 @@
#include "SimCore/Identifier.hpp"
#include "SimCore/Messages/SimTrack.hpp"
#include <Entities/Tracklist/Tracklist.hpp> #include <Entities/Tracklist/Tracklist.hpp>
#include <ranges> #include "SimCore/Messages/SimTrack.hpp"
#include <utility>
namespace TrackList namespace TrackList
{ {
TrackList::TrackList(SimCore::Identifier OwnID):OwnID_(OwnID),TrackTimeout_(5 * 60 *1000) TrackList::TrackList()
{
stopSanitizer_ = false;
sanitizerThread_ = std::thread(&TrackList::tracklistSanitizer,this);
}
TrackList::~TrackList()
{
if (sanitizerIsRunning_ == true) {
stopSanitizer();
}
}
void TrackList::stopSanitizer()
{
stopSanitizer_ = true;
sanitizerThread_.join();
}
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData)
{ {
TrackStore_ = std::map<std::string, std::shared_ptr<SimCore::SimTrack>>();
std::unique_lock<std::mutex> lock(mutex_);
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);
}else {
iterator->second->updateTrack(track,sensorData);
}
} }
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> Track)
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> track)
{ {
std::lock_guard<std::mutex> lock(mx_);
std::unique_lock<std::mutex> lock(mutex_); 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; }
auto iterator = TrackList_.find(track->getIdentifier().getUUID());
if (iterator == TrackList_.end()) {
auto item = std::make_shared<TracklistItem>( track);
TrackList_.emplace(track->getIdentifier().getUUID(),item);
}else {
iterator->second->updateTrack(track);
} }
std::shared_ptr<SimCore::SimTrack> TrackList::getTrack(const SimCore::Identifier ID)
}
void TrackList::addNewTrack(std::shared_ptr<SimCore::SimTrack> track,SensorData sensorData)
{ {
std::lock_guard<std::mutex> lock(mx_);
std::lock_guard<std::mutex> lock(mutex_); auto it = TrackStore_.find(ID.getUUID());
return it->second;
auto item = std::make_shared<TracklistItem>( track, sensorData);
TrackList_.emplace(std::make_pair(track->getIdentifier().getUUID(),item));
} }
void TrackList::addNewTrack(std::shared_ptr<SimCore::SimTrack> track) std::shared_ptr<SimCore::SimTrack> TrackList::getTrackBySringID(std::string ID)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mx_);
auto item = std::make_shared<TracklistItem>( track); auto it = TrackStore_.find(ID);
TrackList_.emplace(track->getIdentifier().getUUID(),item); return it->second;
} }
void TrackList::deleteTrack(std::string ID)
std::shared_ptr<TracklistItem> TrackList::getTrack(SimCore::Identifier TrackID)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID);
std::shared_ptr<TracklistItem> result = nullptr; TrackStore_.erase(it);
result = TrackList_.at(TrackID.getUUID());
if (result == nullptr) {
return nullptr;
}
return result;
} }
std::vector<SimCore::Identifier> TrackList::getAllIDs() void TrackList::deleteTrack(SimCore::Identifier ID)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mx_);
auto it = TrackStore_.find(ID.getUUID());
std::vector<SimCore::Identifier> list; TrackStore_.erase(it);
for (const auto& [key,value] : TrackList_) {
list.emplace_back(key);
} }
return list;
}
size_t TrackList::size() void TrackList::getJsonTRackList(nlohmann::json &message)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mx_);
return TrackList_.size(); try {
for (std::map<std::string, std::shared_ptr<SimCore::SimTrack>>::iterator it=TrackStore_.begin(); it!=TrackStore_.end(); ++it)
}
void TrackList::setTrackTimeout(int millseconds)
{ {
this->TrackTimeout_ = millseconds; 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();
}
} }
int TrackList::getTrackTimeoutValue() size_t TrackList::getSize()
{ {
return TrackTimeout_; std::lock_guard<std::mutex> lock(mx_);
} return TrackStore_.size();
void TrackList::tracklistSanitizer()
{
sanitizerIsRunning_ = true;
while(stopSanitizer_ == false)
{
std::unique_lock<std::mutex> lock(mutex_);
for ( auto it = TrackList_.begin(); it != TrackList_.end();) {
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 >= TrackTimeout_) {
it = TrackList_.erase(it);
LOG_S(INFO)<<"Erased Track";
}else
{
it++;
}
lock.unlock() ;
lock.lock();
}
// LOG_S(INFO)<<"running";
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
lock.release();
}
sanitizerIsRunning_ = false;
} }