diff --git a/CMakeLists.txt b/CMakeLists.txt index d00d85d..94c04af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,10 @@ IF (${TEST_ENTITIY_LIBRARY}) target_link_libraries(test_Tracklist Catch2::Catch2 EntityLibrary loguru) catch_discover_tests(test_Tracklist) + add_executable(test_TracklistItem tests/test_TracklistItem.cpp) + target_link_libraries(test_TracklistItem Catch2::Catch2 EntityLibrary loguru) + catch_discover_tests(test_TracklistItem) + add_executable(test_MovementClass tests/test_MovementClass.cpp) target_link_libraries(test_MovementClass Catch2::Catch2 EntityLibrary loguru) diff --git a/include/Entities/Tracklist/Tracklist.hpp b/include/Entities/Tracklist/Tracklist.hpp index 366a9e8..769979e 100644 --- a/include/Entities/Tracklist/Tracklist.hpp +++ b/include/Entities/Tracklist/Tracklist.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -26,7 +27,7 @@ namespace TrackList void stopSanitizer(); - SimCore::Identifier getTrackID(SimCore::ObjectSource source); + // SimCore::Identifier getTrackID(SimCore::ObjectSource source); void addTrack(std::shared_ptr track); @@ -38,6 +39,9 @@ namespace TrackList size_t size(); + void setTrackTimeout(int millseconds); + int getTrackTimeoutValue(); + private: const SimCore::Identifier OwnID_; @@ -56,6 +60,7 @@ namespace TrackList std::thread sanitizerThread_; std::atomic_bool sanitizerIsRunning_; std::atomic_bool stopSanitizer_; + int TrackTimeout_ = 1000; diff --git a/include/Entities/Tracklist/TracklistItem.hpp b/include/Entities/Tracklist/TracklistItem.hpp index 415763d..acbb3df 100644 --- a/include/Entities/Tracklist/TracklistItem.hpp +++ b/include/Entities/Tracklist/TracklistItem.hpp @@ -6,6 +6,7 @@ #include "SimCore/SimCore.hpp" #include #include +#include #include #include #include @@ -28,30 +29,12 @@ namespace TrackList { - class TracklistItem + class TracklistItem: public SimCore::SimTrack { public: TracklistItem(std::shared_ptr track,SensorData sensorData); TracklistItem(std::shared_ptr track); - SimCore::Identifier getID(); - - void setPosition(SimCore::Position position); - SimCore::Position getPosition(); - - void setSpeed(double speed); - double getSpeed(); - - void setCourse(double course); - double getCourse(); - - void setPitch(double pitch); - double getpitch(); - - double getBearing(); - double getRange(); - - SimCore::ObjectSource getObjectSource(); std::chrono::time_point getLastUpdateTimestamp(); @@ -65,30 +48,14 @@ namespace TrackList { void addSensorDataToSensorList(SensorData sensorData); + size_t getSensorCount(); + private: - const SimCore::Identifier trackID_; - - /// position of the track - SimCore::Position position_; - /// speed the track - double speed_ = 0; - /// course of the track - double course_ = 0; - - double pitch_ = 0; - - /// bearing - double bearing_; - ///range in meters - double range_; - //environment (AIR,SURFACE,SUBSURFACE,SPACE) - SimCore::Kind::EntityKind kind_; + std::chrono::time_point lastUpdateTimestamp_; - SimCore::ObjectSource ObjectSource_; - std::vector SensorList; diff --git a/libs/OrderLibrary b/libs/OrderLibrary index 836cee9..f4f9819 160000 --- a/libs/OrderLibrary +++ b/libs/OrderLibrary @@ -1 +1 @@ -Subproject commit 836cee9d6ca80582fad2e376d0c8632e58c85397 +Subproject commit f4f98191bfc16fb31480262f14addb9270023559 diff --git a/libs/json b/libs/json index 6eab7a2..5d27543 160000 --- a/libs/json +++ b/libs/json @@ -1 +1 @@ -Subproject commit 6eab7a2b187b10b2494e39c1961750bfd1bda500 +Subproject commit 5d2754306d67d1e654a1a34e1d2e74439a9d53b3 diff --git a/libs/nlohmannJSON b/libs/nlohmannJSON index 6eab7a2..5d27543 160000 --- a/libs/nlohmannJSON +++ b/libs/nlohmannJSON @@ -1 +1 @@ -Subproject commit 6eab7a2b187b10b2494e39c1961750bfd1bda500 +Subproject commit 5d2754306d67d1e654a1a34e1d2e74439a9d53b3 diff --git a/src/Entities/Tracklist/TrackListItem.cpp b/src/Entities/Tracklist/TrackListItem.cpp index 8f8605d..0b033ea 100644 --- a/src/Entities/Tracklist/TrackListItem.cpp +++ b/src/Entities/Tracklist/TrackListItem.cpp @@ -3,85 +3,32 @@ #include #include +#include #include #include #include namespace TrackList { - TracklistItem::TracklistItem(std::shared_ptr track,SensorData sensorData):trackID_(track->getIdentifier()) + TracklistItem::TracklistItem(std::shared_ptr 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 track):trackID_(track->getIdentifier()) + TracklistItem::TracklistItem(std::shared_ptr 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 TracklistItem::getLastUpdateTimestamp() { @@ -91,33 +38,41 @@ namespace TrackList { void TracklistItem::updateTrack(std::shared_ptr 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 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(); } diff --git a/src/Entities/Tracklist/Tracklist.cpp b/src/Entities/Tracklist/Tracklist.cpp index 3564008..2d2f588 100644 --- a/src/Entities/Tracklist/Tracklist.cpp +++ b/src/Entities/Tracklist/Tracklist.cpp @@ -1,14 +1,15 @@ +#include "SimCore/Identifier.hpp" #include "SimCore/Messages/SimTrack.hpp" #include +#include +#include -#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 lock(mutex_); - - return *IDMaker.getNewIdentifier(source).get(); - } - - void TrackList::addTrack(std::shared_ptr track,SensorData sensorData) { - auto AllIDs = getAllIDs(); - // std::unique_lock lock(mutex_); - // lock.unlock(); - if (AllIDs.size() == 0) { - addNewTrack( track, sensorData); - } - else - { - for (auto ID:AllIDs) { - // std::lock_guard lock(mutex_); + std::unique_lock lock(mutex_); - auto TracklistItem = TrackList_.find(ID.serialize()); + auto iterator = TrackList_.find(track->getIdentifier().getUUID()); + if (iterator == TrackList_.end()) { + auto item = std::make_shared( 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 track) + + + void TrackList::addTrack(std::shared_ptr track) { - auto AllIDs = getAllIDs(); - // std::unique_lock lock(mutex_); - // lock.unlock(); + std::unique_lock lock(mutex_); - if (AllIDs.size() == 0) { - addNewTrack( track); - } - else - { - for (auto ID:AllIDs) { - // std::lock_guard 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); - 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 track,SensorData sensorData) @@ -123,8 +74,9 @@ namespace TrackList std::lock_guard lock(mutex_); auto item = std::make_shared( 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 track) @@ -132,8 +84,7 @@ namespace TrackList std::lock_guard lock(mutex_); auto item = std::make_shared( 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 lock(mutex_); std::shared_ptr 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 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(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(); } diff --git a/tests/test_Tracklist.cpp b/tests/test_Tracklist.cpp index 1bb825d..543d77b 100644 --- a/tests/test_Tracklist.cpp +++ b/tests/test_Tracklist.cpp @@ -22,20 +22,57 @@ SCENARIO("Testing the SimCore Sensor") { GIVEN("different Attributes for a Track in different forms") { - + SimCore::Identifier OwnID; + TrackList::TrackList List(OwnID); + List.setTrackTimeout(5000); + + double speed = 10; + double course = 90; + SimCore::Identifier id; + auto track = std::make_shared(id,"Hamburg", SimCore::Kind::EntityKind::SURFACE,SimCore::Side::FRIEND); + SimCore::Position pos; + pos.setGeodesicPos(55, 8, 0); + track->setPosition(pos); + track->Speed.setValue(speed); + track->Course.setValue(course); + TrackList::SensorData Sensor1 = + { + .sensorID = SimCore::Identifier(), + .Sensorname = "ARPA" + }; + + TrackList::SensorData Sensor2 = + { + .sensorID = SimCore::Identifier(), + .Sensorname = "SMART-L" + }; + WHEN("constructing Track Object with data") { - + List.addTrack(track,Sensor1); THEN("check if Track attributes are correct") { - // REQUIRE(InternalTracklist.size() == 1); - + REQUIRE(List.size() == 1); + REQUIRE(List.getTrack(id)->getPosition().getGeocentricPos() == pos.getGeocentricPos()); + REQUIRE(List.getTrack(id)->Speed.getValue() == speed); + REQUIRE(List.getTrack(id)->Course.getValue() == course); + REQUIRE(List.getTrack(id)->getSensorCount() == 1); + track->Course.setValue(270); + List.addTrack(track,Sensor2); + // REQUIRE(List.getTrack(id)->Course.getValue() == 270); + REQUIRE(List.getTrack(id)->getSensorCount() == 2); + std::this_thread::sleep_for(std::chrono::milliseconds(5500)); + REQUIRE(List.size() == 0); + + List.stopSanitizer(); + + } //THEN } // WHEN diff --git a/tests/test_TracklistItem.cpp b/tests/test_TracklistItem.cpp new file mode 100644 index 0000000..1ea25d9 --- /dev/null +++ b/tests/test_TracklistItem.cpp @@ -0,0 +1,79 @@ + +#include "Entities/Tracklist/Tracklist.hpp" +#include "Entities/Tracklist/TracklistItem.hpp" +#include "SimCore/IdentifierMaker.hpp" +#include "SimCore/Messages/SimTrack.hpp" +#include "SimCore/Position.hpp" +#include "WHISPER/Messages/Message.hpp" +#include +#include +#include +#include +#define CATCH_CONFIG_MAIN +#include +#include + + + + + + +SCENARIO("Testing the SimCore Sensor") +{ + GIVEN("different Attributes for a Track in different forms") + { + double speed = 10; + double course = 90; + SimCore::Identifier id; + auto track = std::make_shared(id,"Hamburg", SimCore::Kind::EntityKind::SURFACE,SimCore::Side::FRIEND); + SimCore::Position pos; + pos.setGeodesicPos(55, 8, 0); + track->setPosition(pos); + track->Speed.setValue(speed); + track->Course.setValue(course); + + TrackList::TracklistItem item(track); + + TrackList::SensorData Sensor1 = + { + .sensorID = SimCore::Identifier(), + .Sensorname = "ARPA" + }; + + TrackList::SensorData Sensor2 = + { + .sensorID = SimCore::Identifier(), + .Sensorname = "SMART-L" + }; + + item.addSensorDataToSensorList(Sensor1); + + + + + + + + WHEN("constructing Track Object with data") + { + track->Course.setValue(270); + + item.updateTrack(track,Sensor2); + + THEN("check if Track attributes are correct") + { + + REQUIRE(item.getPosition().getGeocentricPos() == pos.getGeocentricPos()); + REQUIRE(item.Speed.getValue() == speed); + REQUIRE(item.Course.getValue() != course); + REQUIRE(item.isSensorIDKnown(Sensor1.sensorID) == true); + + REQUIRE(item.getSensorCount() == 2); + REQUIRE(item.getLastUpdateTimestamp() < std::chrono::steady_clock::now()); + + + + } //THEN + } // WHEN + } // GIVEN +} //SCENARIO \ No newline at end of file