diff --git a/include/Entities/Entity.hpp b/include/Entities/Entity.hpp index b163f46..aff3d21 100644 --- a/include/Entities/Entity.hpp +++ b/include/Entities/Entity.hpp @@ -53,12 +53,13 @@ namespace Entities { Entity(const SimCore::Identifier OwnID, std::string EnttityName, WHISPER::SourceType OwnType, + double RadarCrossSection, SimCore::Kind::EntityKind EntityKind, SimCore::Side::EntitySide EntitySide, std::string GroundTruthAddr, std::uint32_t GroundTruthPort, ushort CommandPort, - bool online); + bool online); ~Entity(); void start(); @@ -73,6 +74,7 @@ namespace Entities { virtual void childWorker() = 0; + virtual void stopChild() = 0; protected: @@ -82,6 +84,7 @@ namespace Entities { std::string EntityName_; SimCore::Kind::EntityKind EntityKind_; SimCore::Side::EntitySide EntitySide_; + double RCS_; ushort MovemntWorkerPort_; @@ -91,6 +94,9 @@ namespace Entities { std::shared_ptr BroadcastServer_; + std::string GroundTruthAddr_; + std::uint32_t GroundTruthPort_; + private: diff --git a/include/Entities/Tracklist/Tracklist.hpp b/include/Entities/Tracklist/Tracklist.hpp index 769979e..f4a8461 100644 --- a/include/Entities/Tracklist/Tracklist.hpp +++ b/include/Entities/Tracklist/Tracklist.hpp @@ -4,6 +4,7 @@ #include "SimCore/Identifier.hpp" #include "SimCore/IdentifierMaker.hpp" #include "SimCore/Messages/SimTrack.hpp" +#include "nlohmann/json.hpp" #include #include #include @@ -19,49 +20,25 @@ namespace TrackList { - class TrackList + class TrackList { - public: - TrackList(SimCore::Identifier OwnID); - ~TrackList(); + public: + TrackList(); + void addTrack(std::shared_ptr Track); + std::shared_ptr getTrack(SimCore::Identifier); + + void deleteTrack(std::string ID); + void deleteTrack(SimCore::Identifier); + std::shared_ptr getTrackBySringID(std::string ID); - void stopSanitizer(); + size_t getSize(); - // SimCore::Identifier getTrackID(SimCore::ObjectSource source); - - void addTrack(std::shared_ptr track); - - void addTrack(std::shared_ptr track,SensorData sensorData); - - std::shared_ptr getTrack(SimCore::Identifier TrackID); - - std::vector getAllIDs(); - - size_t size(); - - void setTrackTimeout(int millseconds); - int getTrackTimeoutValue(); - - private: - - const SimCore::Identifier OwnID_; - - SimCore::IdentifierMaker IDMaker; - - void tracklistSanitizer(); - - void addNewTrack(std::shared_ptr track,SensorData sensorData); - void addNewTrack(std::shared_ptr track); - - std::map> TrackList_; - - mutable std::mutex mutex_; - - std::thread sanitizerThread_; - std::atomic_bool sanitizerIsRunning_; - std::atomic_bool stopSanitizer_; - int TrackTimeout_ = 1000; + void getJsonTRackList(nlohmann::json &message); + + private: + mutable std::mutex mx_; + std::map> TrackStore_; }; diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 8821616..dad106c 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -38,6 +38,7 @@ namespace Entities Entity::Entity(const SimCore::Identifier OwnID, std::string EnttityName, WHISPER::SourceType OwnType, + double RadarCrossSection, SimCore::Kind::EntityKind EntityKind, SimCore::Side::EntitySide EntitySide, std::string GroundTruthAddr, @@ -46,13 +47,17 @@ namespace Entities bool online): EntityName_(EnttityName), EntityKind_(EntityKind), + RCS_(RadarCrossSection), EntitySide_(EntitySide), + GroundTruthPort_(GroundTruthPort), + GroundTruthAddr_(GroundTruthAddr), online_(online) { PodController_ = std::make_unique("docs/config"); OwnShipTrack = std::make_shared(OwnID, EnttityName, EntityKind,EntitySide); OwnShipTrack->setPosition(SimCore::Position()); + OwnShipTrack->RCS.setValue(RCS_); MovemtServer_ = std::make_shared(__MOVEMENT_SERVER_PORT__); @@ -131,7 +136,7 @@ namespace Entities void Entity::stop() { - + stopChild(); PodController_->stopAllPods(); @@ -205,7 +210,7 @@ namespace Entities if (newTrack != nullptr) { 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); } } diff --git a/src/Entities/Tracklist/Tracklist.cpp b/src/Entities/Tracklist/Tracklist.cpp index 2d2f588..bc252ed 100644 --- a/src/Entities/Tracklist/Tracklist.cpp +++ b/src/Entities/Tracklist/Tracklist.cpp @@ -1,171 +1,88 @@ -#include "SimCore/Identifier.hpp" -#include "SimCore/Messages/SimTrack.hpp" #include -#include -#include +#include "SimCore/Messages/SimTrack.hpp" namespace TrackList { - TrackList::TrackList(SimCore::Identifier OwnID):OwnID_(OwnID),TrackTimeout_(5 * 60 *1000) - { - stopSanitizer_ = false; - sanitizerThread_ = std::thread(&TrackList::tracklistSanitizer,this); - } + TrackList::TrackList() + { - TrackList::~TrackList() - { - if (sanitizerIsRunning_ == true) { - stopSanitizer(); - } - } + TrackStore_ = std::map>(); + } + + 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; } - void TrackList::stopSanitizer() - { - stopSanitizer_ = true; - sanitizerThread_.join(); + } + 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); + } + 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()]; - void TrackList::addTrack(std::shared_ptr track,SensorData sensorData) - { + message["Entities"].push_back(j); + } + }catch (const std::exception e) { + LOG_S(ERROR)<< e.what(); + } + } + size_t TrackList::getSize() + { + std::lock_guard lock(mx_); + return TrackStore_.size(); - std::unique_lock lock(mutex_); - - auto iterator = TrackList_.find(track->getIdentifier().getUUID()); - if (iterator == TrackList_.end()) { - auto item = std::make_shared( track,sensorData); - TrackList_.emplace(track->getIdentifier().getUUID(),item); - - }else { - iterator->second->updateTrack(track,sensorData); - - } - } - - - - void TrackList::addTrack(std::shared_ptr track) - { - - std::unique_lock lock(mutex_); - - auto iterator = TrackList_.find(track->getIdentifier().getUUID()); - if (iterator == TrackList_.end()) { - auto item = std::make_shared( track); - TrackList_.emplace(track->getIdentifier().getUUID(),item); - - }else { - iterator->second->updateTrack(track); - - } - - } - - void TrackList::addNewTrack(std::shared_ptr track,SensorData sensorData) - { - - std::lock_guard lock(mutex_); - - auto item = std::make_shared( track, sensorData); - - TrackList_.emplace(std::make_pair(track->getIdentifier().getUUID(),item)); - - } - - void TrackList::addNewTrack(std::shared_ptr track) - { - std::lock_guard lock(mutex_); - - auto item = std::make_shared( track); - TrackList_.emplace(track->getIdentifier().getUUID(),item); - } - - - std::shared_ptr TrackList::getTrack(SimCore::Identifier TrackID) - { - std::lock_guard lock(mutex_); - - std::shared_ptr result = nullptr; - result = TrackList_.at(TrackID.getUUID()); - if (result == nullptr) { - return nullptr; - } - return result; - } - - std::vector TrackList::getAllIDs() - { - std::lock_guard lock(mutex_); - - std::vector list; - for (const auto& [key,value] : TrackList_) { - list.emplace_back(key); - } - - return list; - } - - size_t TrackList::size() - { - std::lock_guard lock(mutex_); - return TrackList_.size(); - - } - - void TrackList::setTrackTimeout(int millseconds) - { - this->TrackTimeout_ = millseconds; - } - - int TrackList::getTrackTimeoutValue() - { - return TrackTimeout_; - } - - - void TrackList::tracklistSanitizer() - { - sanitizerIsRunning_ = true; - - while(stopSanitizer_ == false) - { - std::unique_lock 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(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; - - } + }