diff --git a/CMakeLists.txt b/CMakeLists.txt index 1035134..6dfcac7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,20 +43,20 @@ add_library(EntityLibrary STATIC include/Entities/Movement.hpp src/Entities/Movement.cpp - + + include/Entities/SensorControl.hpp + src/Entities/SensorControl.cpp + + include/Entities/SensorManager.hpp + src/Entities/SensorManager.cpp + + include/Entities/Tracklist/Trackfusion.hpp + src/Entities/Tracklist/Trackfusion.cpp + include/Entities/Tracklist/Tracklist.hpp src/Entities/Tracklist/Tracklist.cpp - include/Entities/Tracklist/TracklistItem.hpp - src/Entities/Tracklist/TrackListItem.cpp - - - include/Entities/SensorManager.hpp - src/Entities/SensorManager.cpp - - include/Entities/SensorControl.hpp - src/Entities/SensorControl.cpp - + ) @@ -76,7 +76,9 @@ target_include_directories(EntityLibrary PUBLIC src) - + add_custom_command(TARGET EntityLibrary POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_CURRENT_SOURCE_DIR}/libs/KubeControl/docs $/docs) # # Everything TEST related @@ -100,13 +102,13 @@ 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) catch_discover_tests(test_MovementClass) + + add_executable(test_SensorManager tests/test_SensorManager.cpp) + target_link_libraries(test_SensorManager Catch2::Catch2 EntityLibrary loguru) + catch_discover_tests(test_SensorManager) ENDIF() diff --git a/include/Entities/Entity.hpp b/include/Entities/Entity.hpp index 99beb0e..7a220ac 100644 --- a/include/Entities/Entity.hpp +++ b/include/Entities/Entity.hpp @@ -31,13 +31,13 @@ namespace Entities { - struct SensorClientData - { - std::string SensorName; - bool isActive; - SimCore::Identifier SensorID; - // std::shared_ptr SensorSender; - }; + // struct SensorClientData + // { + // std::string SensorName; + // bool isActive; + // SimCore::Identifier SensorID; + // // std::shared_ptr SensorSender; + // }; struct EffectorClientData { @@ -131,11 +131,11 @@ namespace Entities { void startSensor(); - + - std::shared_ptr> SensorStore_; + // std::shared_ptr> SensorStore_; - std::shared_ptr>> Trackstore_; + // std::shared_ptr>> Trackstore_; }; diff --git a/include/Entities/SensorControl.hpp b/include/Entities/SensorControl.hpp index 53c7642..bdbaeff 100644 --- a/include/Entities/SensorControl.hpp +++ b/include/Entities/SensorControl.hpp @@ -1,69 +1,34 @@ #pragma once +#include "SimCore/Identifier.hpp" #include "SimCore/SimCore.hpp" #include #include #include +#include #include #include #include #include -namespace Entities + +namespace Sensor { - class SensorControl + class SensorControl : public SimCore::SensorData { public: - SensorControl(std::string ID, std::string Name, std::string IP, SimCore::SensorKinds sensorKind); + SensorControl(SimCore::Identifier ID, std::string Name, std::string IP, SimCore::SensorKinds sensorKind); - /** - * @brief Get the Name of the Sensor - * - * @return std::string - */ - std::string getName(); + - /** - * @brief Get the UUID of the Sensor - * - * @return std::string - */ - std::string getID(); - - /** - * @brief Get the Sensor Kind - * - * @return SimCore::SensorKinds - */ - SimCore::SensorKinds getSensorKind(); - - /** - * @brief Get the Sensor Status - * - * @return SimCore::Status - */ - SimCore::Status getSensorStatus(); - - /** - * @brief updates the sensor status - * - * @param SimCore::Status - */ - void updateStatus(SimCore::Status status); - - - std::unique_ptr TrackStore; + TrackList::TrackList TrackStore; private: - const std::string ID_; - std::string Name_; - std::string IP_; - SimCore::SensorKinds SensorKind_; - SimCore::Status SensorSatus_; + diff --git a/include/Entities/SensorManager.hpp b/include/Entities/SensorManager.hpp index 3b0b2ad..ae9e94a 100644 --- a/include/Entities/SensorManager.hpp +++ b/include/Entities/SensorManager.hpp @@ -2,10 +2,13 @@ #include "DirectCommunicationServer.hpp" #include "Entities/SensorControl.hpp" +#include "Entities/Tracklist/Trackfusion.hpp" #include "SimCore/Identifier.hpp" #include "SimCore/Messages/SimTrack.hpp" +#include "SimCore/Messages/TracklistUpdate.hpp" #include "SimCore/SimCore.hpp" #include "kubecontrol/PodController.hpp" +#include #include #include @@ -36,12 +39,34 @@ namespace Entities */ void stopSenser(std::string uuid); + /** + * @brief stopps all sensors + * + */ + void stopAllSensors(); + /** *@brief deletes a sensor pod based on the uuid *@param string uuid sensor */ void deleteSensor(std::string uuid); + /** + * @brief deletes all sensors + * + */ + void deleteAllSensor(); + + /** + * @brief Get the Sensor By UUID + * + * @param uuid + * @return std::shared_ptr + */ + std::shared_ptr getSensorByUUID(std::string uuid); + + + /** *@brief get the Tracklist as JSON formatted string where you can see the tracks every sensor detects *@return string - JSON formatted string @@ -62,15 +87,49 @@ namespace Entities */ void updateTracklistForSensor(std::string uuidSensor, std::shared_ptr track); + /** + * @brief sends a message to a specific sensor if uuid is passed else it sends to everyone + * + * @param message - string of the message + * @param uuid - uuid as string + */ + void sendMessageToSensor(std::string message, std::string uuid = ""); + + /** + * @brief adds a Sensorcontroll unique pointer to sensor store without starting a pod. it is just local + * @param sensor + * + */ + void addSensorLocal(std::unique_ptr sensor); + + /** + * @brief Get the number of sensors + * + * @return size_t + */ + size_t getSensorCount(); + + /** + * @brief Get the Track List Update object + * + * @return std::unique_ptr + */ + std::unique_ptr getTrackListUpdate(); + private: const SimCore::Identifier OwnId_; ushort SensorPort_ = 5557; - std::map> SensorStore; + std::map> SensorStore; + std::shared_ptr PodController_; + TrackList::Trackfusion Trackfusion_; + + + std::shared_ptr SensorServer_ = nullptr; void handlSensorMessages(std::string Message); diff --git a/include/Entities/Tracklist/Trackfusion.hpp b/include/Entities/Tracklist/Trackfusion.hpp new file mode 100644 index 0000000..3e081d6 --- /dev/null +++ b/include/Entities/Tracklist/Trackfusion.hpp @@ -0,0 +1,84 @@ +#pragma once + + +#include +#include "SimCore/Identifier.hpp" +#include "SimCore/Messages/SimTrack.hpp" +#include +#include +#include +#include +#include + + +namespace TrackList +{ + + + + class Trackfusion + { + + public: + /** + * @brief adds or updates track + * + * @param track shared pointer of a simtrack + * @param sensorData + */ + void addOrUpdateTrack(std::shared_ptr track,std::shared_ptr sensorData); + + /** + * @brief deltets a track based on a SimCore::Identifier + * + */ + void deleteTrack(SimCore::Identifier id); + + /** + * @brief deletes a track based on a string uuid + * + * @param UUID + */ + void deleteTrack(std::string UUID); + + /** + * @brief Get the Tracklist As String + * + * @return std::string + */ + std::string getTracklistAsString(); + + /** + * @brief retruns a shared pointer of a track + * + * @param id + * @return std::shared_ptr + */ + std::shared_ptr findTrack(SimCore::Identifier id); + + /** + * @brief returns a shared pointer of a track + * + * @param UUID + * @return std::shared_ptr + */ + std::shared_ptr findTrack(std::string UUID); + + /** + * @brief Get the Track List Update object + * + * @return std::unique_ptr + */ + std::unique_ptr getTrackListUpdate(SimCore::Identifier ID); + + + + private: + + mutable std::mutex mx_; + + std::unordered_map> TrackStore_; + + }; + +} \ No newline at end of file diff --git a/include/Entities/Tracklist/Tracklist.hpp b/include/Entities/Tracklist/Tracklist.hpp index f4a8461..79c5a81 100644 --- a/include/Entities/Tracklist/Tracklist.hpp +++ b/include/Entities/Tracklist/Tracklist.hpp @@ -12,7 +12,7 @@ #include #include -#include +// #include #include #include #include diff --git a/include/Entities/Tracklist/TracklistItem.hpp b/include/Entities/Tracklist/TracklistItem.hpp deleted file mode 100644 index acbb3df..0000000 --- a/include/Entities/Tracklist/TracklistItem.hpp +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once - - -#include "SimCore/Identifier.hpp" -#include "SimCore/Messages/SimTrack.hpp" -#include "SimCore/SimCore.hpp" -#include -#include -#include -#include -#include -#include - -namespace TrackList { - - struct SensorData { - SimCore::Identifier sensorID; - // SimCore::Identifier SensorTrackID; - std::string Sensorname; - - - }; - - inline bool operator==(const SensorData& lhs, const SensorData& rhs) - { - return lhs.sensorID == rhs.sensorID; - - } - - - - class TracklistItem: public SimCore::SimTrack - { - public: - TracklistItem(std::shared_ptr track,SensorData sensorData); - TracklistItem(std::shared_ptr track); - - - std::chrono::time_point getLastUpdateTimestamp(); - - void updateTrack(std::shared_ptr track,SensorData sensorData); - - void updateTrack(std::shared_ptr track); - - bool checkIfSensorIDIsIn(SimCore::Identifier SensorTrackID); - - bool isSensorIDKnown(SimCore::Identifier); - - void addSensorDataToSensorList(SensorData sensorData); - - size_t getSensorCount(); - - - private: - - - std::chrono::time_point lastUpdateTimestamp_; - - std::vector SensorList; - - - bool isSensorinSensorlist(SensorData sensorData); - - - - - - }; - -} \ No newline at end of file diff --git a/libs/KubeControl b/libs/KubeControl index 2377948..7bc5e3f 160000 --- a/libs/KubeControl +++ b/libs/KubeControl @@ -1 +1 @@ -Subproject commit 2377948049cf6a41ee74e5b03b33db96215fe476 +Subproject commit 7bc5e3f22e092c7eeb58e389aa7f399da81329bc diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index c66df47..6394af2 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -3,6 +3,7 @@ #include "Entities/SensorManager.hpp" #include "Orders/MoveOrder.hpp" #include "Orders/Order.hpp" +#include "Orders/TracklistRequest.hpp" #include "SimCore/Messages/Control.hpp" #include "SimCore/Messages/SimTrack.hpp" #include "SimCore/UtilFunctions.hpp" @@ -68,10 +69,9 @@ namespace Entities MovemtServer_ = std::make_shared(__MOVEMENT_SERVER_PORT__,OwnID.getUUID()); CommandCommsServer_ = std::make_shared(CommandPort,OwnID.getUUID()); - CommandCommsServer_->registerMessageCallback(std::bind(&Entity::handleExternalComms,this,std::placeholders::_1)); - BroadcastServer_ = std::make_shared(GroundTruthAddr, GroundTruthPort); + BroadcastServer_ = std::make_shared(OwnID.getUUID(),GroundTruthAddr, GroundTruthPort); @@ -98,7 +98,7 @@ namespace Entities moveorder.setPosition(pos); if(MovementWorkerStarted == true) { - MovemtServer_->sendMessage(moveorder.buildMessage().serialize()); + MovemtServer_->sendMessage(moveorder.buildMessage()); LOG_S(INFO)<<"Move Order send"; } } @@ -109,7 +109,7 @@ namespace Entities moveorder.Speed.setValue(val); if(MovementWorkerStarted == true) { - MovemtServer_->sendMessage(moveorder.buildMessage().serialize()); + MovemtServer_->sendMessage(moveorder.buildMessage()); LOG_S(INFO)<<"Move Order send with Speed"; } } @@ -120,7 +120,7 @@ namespace Entities moveorder.Course.setValue(val); if(MovementWorkerStarted == true) { - MovemtServer_->sendMessage(moveorder.buildMessage().serialize()); + MovemtServer_->sendMessage(moveorder.buildMessage()); LOG_S(INFO)<<"Move Order send with Course"; } } @@ -202,7 +202,7 @@ namespace Entities LOG_S(INFO)<< "POS: LAT: "<< OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LATITUDE) << " LON: " << OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LONGITUDE); MovementWorkerStarted = true; - MovemtServer_->sendMessage(OwnShipTrack->buildMessage().serialize()); + MovemtServer_->sendMessage(OwnShipTrack->buildMessage()); LOG_S(INFO)<< "Initial Message send to MovementWorker"; } @@ -292,8 +292,17 @@ namespace Entities break; } case Orders::TRACKLIST_REQUEST: - { - + { + auto TrackListRequest = Orders::TracklistRequest::unpack(whisperMsg); + if (TrackListRequest == nullptr) + { + break; + } + if (TrackListRequest->EntityID == OwnShipTrack->getIdentifier()) + { + std::string senderUUID = whisperMsg.senderUUID_; + CommandCommsServer_->sendMessage(SensorManager_->getTrackListUpdate()->buildMessage().serialize(OwnShipTrack->getIdentifier().getUUID()),senderUUID); + } break; } break; diff --git a/src/Entities/Sensor.cpp b/src/Entities/Sensor.cpp index d870b84..cbeb43d 100644 --- a/src/Entities/Sensor.cpp +++ b/src/Entities/Sensor.cpp @@ -197,7 +197,7 @@ void Sensor::updateOwnShipFunction() if (track != nullptr) { LOG_S(INFO)<<"updated Tracklist"; - client_->sendMessage(track->buildMessage().serialize()); + client_->sendMessage(track->buildMessage()); } } diff --git a/src/Entities/SensorControl.cpp b/src/Entities/SensorControl.cpp index a6d78b2..369ba07 100644 --- a/src/Entities/SensorControl.cpp +++ b/src/Entities/SensorControl.cpp @@ -1,4 +1,6 @@ +#include #include "Entities/Tracklist/Tracklist.hpp" +#include "SimCore/Identifier.hpp" #include "nlohmann/json_fwd.hpp" #include #include @@ -8,41 +10,14 @@ -namespace Entities +namespace Sensor { - SensorControl::SensorControl(std::string ID, std::string Name, std::string IP, SimCore::SensorKinds sensorKind): - ID_(ID),IP_(IP),Name_(Name),SensorKind_(sensorKind) + SensorControl::SensorControl(SimCore::Identifier ID, std::string Name, std::string IP, SimCore::SensorKinds sensorKind) + :SimCore::SensorData(ID,Name,IP,sensorKind) { - TrackStore = std::make_unique(); } - std::string SensorControl::getName() - { - return Name_; - } - - std::string SensorControl::getID() - { - return ID_; - } - - - SimCore::SensorKinds SensorControl::getSensorKind() - { - return SensorKind_; - } - void SensorControl::updateStatus(SimCore::Status status) - { - SensorSatus_ = status; - } - - SimCore::Status SensorControl::getSensorStatus() - { - return SensorSatus_; - } - - diff --git a/src/Entities/SensorManager.cpp b/src/Entities/SensorManager.cpp index 5c61bba..9e19db7 100644 --- a/src/Entities/SensorManager.cpp +++ b/src/Entities/SensorManager.cpp @@ -1,4 +1,5 @@ #include "Entities/SensorControl.hpp" +#include "SimCore/Identifier.hpp" #include "SimCore/Messages/SimTrack.hpp" #include "SimCore/SimCore.hpp" #include "WHISPER/Messages/Message.hpp" @@ -21,7 +22,7 @@ namespace Entities void SensorManager::startSensor(std::shared_ptr pod,SimCore::SensorKinds SensorKind) { PodController_->startPod(pod); - SensorStore[pod->getUUID()] = std::make_unique(pod->getUUID(),pod->getName(),pod->getIp(),SensorKind); + SensorStore[pod->getUUID()] = std::make_unique(SimCore::Identifier(pod->getUUID()),pod->getName(),pod->getIp(),SensorKind); } @@ -30,6 +31,14 @@ namespace Entities //TODO send stop message to sensor } + void SensorManager::stopAllSensors() + { + //TODO send stop message to sensor + + + } + + void SensorManager::deleteSensor(std::string uuid) { @@ -38,18 +47,55 @@ namespace Entities } + void SensorManager::deleteAllSensor() + { + PodController_->stopAllPods(); + SensorStore.clear(); + } + + + std::shared_ptr SensorManager::getSensorByUUID(std::string uuid) + { + auto it = SensorStore.find(uuid); + if( it != SensorStore.end()) + { + return it->second; + } + return nullptr; + + } + + + void SensorManager::addSensorLocal(std::unique_ptr sensor) + { + SensorStore[sensor->getID().getUUID()] = std::move(sensor); + } + + + size_t SensorManager::getSensorCount() + { + return SensorStore.size(); + } + + + void SensorManager::handlSensorMessages(std::string Message) { - WHISPER::Message msg(Message); + WHISPER::Message msg(Message); switch (msg.msgType_) { case WHISPER::MsgType::SIM_TRACK: - { + { auto track = std::make_shared(SimCore::SimTrack::unpack(msg)); - updateTracklistForSensor(msg.senderUUID_, track); + if(track != nullptr) + { + updateTracklistForSensor(msg.senderUUID_, track); + auto sensor = getSensorByUUID(msg.senderUUID_); + Trackfusion_.addOrUpdateTrack(track, sensor); + } break; } case WHISPER::MsgType::SYSTEMSTATE_UPDATE: @@ -66,30 +112,53 @@ namespace Entities void SensorManager::updateTracklistForSensor(std::string uuidSensor, std::shared_ptr track) { - auto it = SensorStore.find(uuidSensor); - it->second->TrackStore->addTrack(track); + + auto sensor= getSensorByUUID(uuidSensor); + + + if(sensor == nullptr) + { + return; + } + sensor->TrackStore.addTrack(std::move(track)); + // auto it = SensorStore.find(uuidSensor); + // if( it != SensorStore.end()) + // { + // it->second->TrackStore.addTrack(std::move(track)); + // } } + void SensorManager::sendMessageToSensor(std::string message, std::string uuid ) + { + if (uuid.empty()) + { + SensorServer_->sendMessage(message); + }else { + SensorServer_->sendMessage(message,uuid); + } + + } + + + std::string SensorManager::getTracklistStringBySensor() { nlohmann::json result; + nlohmann::json radar; - radar["SensorType"] = SimCore::toString(SimCore::SensorKinds::RADAR); + nlohmann::json esm; - esm["SensorType"] = SimCore::toString(SimCore::SensorKinds::ESM); nlohmann::json sonar; - sonar["SensorType"] = SimCore::toString(SimCore::SensorKinds::SONAR); nlohmann::json visual; - visual["SensorType"] = SimCore::toString(SimCore::SensorKinds::VISUAL); for(const auto& [key, value] : SensorStore ) { nlohmann::json sensor; sensor["SensorName"] = value->getName(); - sensor["SensorID"] = value->getID(); + sensor["SensorID"] = value->getID().getUUID(); sensor["SensorStatus"] = SimCore::toString(value->getSensorStatus()); sensor["SensorEnabled"] = "false"; if (value->getSensorStatus() == SimCore::Status::ACTIVE) @@ -102,57 +171,73 @@ namespace Entities sensor["SensorDamaged"] = "true"; } nlohmann::json contacts; - value->TrackStore->getJsonTRackList(contacts); - sensor["SensorContacts"].push_back(contacts); + value->TrackStore.getJsonTRackList(contacts); + sensor["SensorContacts"] = contacts["Entities"]; switch (value->getSensorKind()) { case SimCore::SensorKinds::RADAR: { - radar["Sensors"].push_back(sensor); + radar.push_back(sensor); break; } case SimCore::SensorKinds::ESM : { - esm["Sensors"].push_back(sensor); + esm.push_back(sensor); break; } case SimCore::SensorKinds::SONAR : { - sonar["Sensors"].push_back(sensor); + sonar.push_back(sensor); break; } case SimCore::SensorKinds::VISUAL : { - visual["Sensors"].push_back(sensor); + visual.push_back(sensor); break; } } - - result.push_back(radar); - result.push_back(esm); - result.push_back(sonar); - result.push_back(visual); + // result.push_back(esm); + // result.push_back(sonar); + // result.push_back(visual); } + result[(int)SimCore::SensorKinds::RADAR]["SensorType"] = "Radar"; + result[(int)SimCore::SensorKinds::RADAR]["Sensors"]= radar; + result[(int)SimCore::SensorKinds::ESM]["SensorType"] = SimCore::toString(SimCore::SensorKinds::ESM); + result[(int)SimCore::SensorKinds::ESM]["Sensors"] = esm; + result[(int)SimCore::SensorKinds::SONAR]["SensorType"] = SimCore::toString(SimCore::SensorKinds::SONAR); + result[(int)SimCore::SensorKinds::SONAR]["Sensors"] = sonar; + result[(int)SimCore::SensorKinds::VISUAL]["SensorType"] = SimCore::toString(SimCore::SensorKinds::VISUAL); + result[(int)SimCore::SensorKinds::VISUAL]["Sensors"] = visual; + return result.dump(); } std::string SensorManager::getTracklistStringFusioned() { + std::string result = Trackfusion_.getTracklistAsString(); + return result; + } + + std::unique_ptr SensorManager::getTrackListUpdate() + { + + return std::move(Trackfusion_.getTrackListUpdate(OwnId_)); } + } \ No newline at end of file diff --git a/src/Entities/Tracklist/TrackListItem.cpp b/src/Entities/Tracklist/TrackListItem.cpp deleted file mode 100644 index 0b033ea..0000000 --- a/src/Entities/Tracklist/TrackListItem.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "SimCore/Messages/SimTrack.hpp" -#include -#include - -#include -#include -#include -#include -#include - -namespace TrackList { - - TracklistItem::TracklistItem(std::shared_ptr track,SensorData sensorData): - SimCore::SimTrack(*track.get()) - { - addSensorDataToSensorList(sensorData); - lastUpdateTimestamp_ = std::chrono::steady_clock::now(); - - } - - TracklistItem::TracklistItem(std::shared_ptr track): - SimCore::SimTrack(*track.get()) - { - updateTrack(track); - lastUpdateTimestamp_ = std::chrono::steady_clock::now(); - - } - - - - - - std::chrono::time_point TracklistItem::getLastUpdateTimestamp() - { - return lastUpdateTimestamp_; - } - - - void TracklistItem::updateTrack(std::shared_ptr track,SensorData sensorData) - { - if(this->getIdentifier() == track->getIdentifier()) - { - - this->setPosition(track->getPosition()); - this->Speed.setValue(track->Speed.getValue()); - this->Course.setValue(track->Course.getValue()); - - } - - if (!isSensorinSensorlist(sensorData)) - { - SensorList.push_back(sensorData); - } - lastUpdateTimestamp_ = std::chrono::steady_clock::now(); - - - } - - void TracklistItem::updateTrack(std::shared_ptr track ) - { - - 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(); - } - - - bool TracklistItem::isSensorinSensorlist(SensorData sensorData) - { - - auto it = std::find(SensorList.begin(),SensorList.end(), sensorData); - - if (it != SensorList.end()) { - return true; - } else { - return false; - } - - } - - - bool TracklistItem::isSensorIDKnown(SimCore::Identifier SensorID) - { - for (auto i: SensorList) { - if (i.sensorID == SensorID) - { - return true; - - } - } - return false; - } - - - void TracklistItem::addSensorDataToSensorList(SensorData sensorData) - { - - if (isSensorinSensorlist(sensorData) == false) { - - SensorList.emplace_back(sensorData); - - } - } - - - - bool TracklistItem::checkIfSensorIDIsIn(SimCore::Identifier SensorID) - { - for (auto i:SensorList) { - - if (i.sensorID == SensorID) { - return true; - } - } - return false; - } - -} \ No newline at end of file diff --git a/src/Entities/Tracklist/Trackfusion.cpp b/src/Entities/Tracklist/Trackfusion.cpp new file mode 100644 index 0000000..d4c1232 --- /dev/null +++ b/src/Entities/Tracklist/Trackfusion.cpp @@ -0,0 +1,131 @@ +#include +#include "SimCore/Messages/SimTrack.hpp" +#include "SimCore/Messages/TracklistUpdate.hpp" +#include "nlohmann/json_fwd.hpp" +#include +#include +#include + + + +namespace TrackList +{ + void Trackfusion::addOrUpdateTrack(std::shared_ptr track, std::shared_ptr sensorData) + { + std::lock_guard lock(mx_); + + + auto iterator = TrackStore_.find(track->getIdentifier()); + + if (iterator == TrackStore_.end()) + { + auto Item = std::make_shared(track,*sensorData); + TrackStore_[Item->getIdentifier()] = Item; + }else { + iterator->second->updateTrack(track); + iterator->second->addSensorDataToSensorList(*sensorData); + } + + + + } + + void Trackfusion::deleteTrack(SimCore::Identifier id) + { + std::lock_guard lock(mx_); + + + auto it = TrackStore_.find(id); + if (it != TrackStore_.end()) + { + it = TrackStore_.erase(it); + } + + } + + + void Trackfusion::deleteTrack(std::string uuid) + { + std::lock_guard lock(mx_); + + for (auto it = TrackStore_.begin(); it != TrackStore_.end();) + { + if (it->first.getUUID() == uuid) + { + it = TrackStore_.erase(it); + } + else{ + ++it; + } + } + + + } + + std::shared_ptr Trackfusion::findTrack(SimCore::Identifier id) + { + std::lock_guard lock(mx_); + auto it = TrackStore_.find(id); + if (it != TrackStore_.end()) + { + return it->second; + } + + return nullptr; + } + + + std::shared_ptr Trackfusion::findTrack(std::string uuid) + { + std::lock_guard lock(mx_); + + + for (auto it = TrackStore_.begin(); it != TrackStore_.end();) + { + if (it->first.getUUID() == uuid) + { + return it->second; + } + + } + return nullptr; + } + + + + + + std::string Trackfusion::getTracklistAsString() + { + std::lock_guard lock(mx_); + + nlohmann::json j; + LOG_S(INFO)<<" Trackstore size: " <getsTrackListItemAsJSON()); + + } + return j.dump(); + } + + + std::unique_ptr Trackfusion::getTrackListUpdate(SimCore::Identifier ID) + { + auto update = std::make_unique(ID); + + for (auto it :TrackStore_) + { + // update->addTrack(*it.second); + + } + + return std::move(update); + } + + + + + + +} diff --git a/tests/test_SensorManager.cpp b/tests/test_SensorManager.cpp new file mode 100644 index 0000000..b527f8f --- /dev/null +++ b/tests/test_SensorManager.cpp @@ -0,0 +1,229 @@ + +#include "DirectCommunicationClient.hpp" +#include "Entities/SensorControl.hpp" +#include "Entities/SensorManager.hpp" +#include "SimCore/Messages/SimTrack.hpp" +#include "SimCore/Position.hpp" +#include "nlohmann/json_fwd.hpp" +#include +#include +#include +#include +#include +#include +#include +#define CATCH_CONFIG_MAIN +#include +#include + +// SimCore::Identifier OwnID, SimCore::Identifier ParentID, SimCore::SensorKinds SensorKind,std::uint32_t GroundTruthPort, std::uint32_t ParentPort,std::string ParentIPAddress +double fRand(double fMin, double fMax) +{ + double f = (double)rand() / RAND_MAX; + return fMin + f * (fMax - fMin); +} +void sendRandomTrack(std::shared_ptr client, int number) +{ +for (int i = 0; i < number; i++) { + std::string name = "test1-" + std::to_string(i); + SimCore::SimTrack track(name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL); + SimCore::Position pos; + double lat = fRand(-90, 90); + double lon = fRand(-180, 180); + + pos.setGeodesicPos( lat, lon, 0); + track.setPosition(pos); + client->sendMessage(track.buildMessage()); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + +} + +} + +void sendRandomTracktoManager(std::shared_ptr SensorManager_) +{ +for (int i = 0; i < 5; i++) { + std::string name = "test1-" + std::to_string(i); + SimCore::SimTrack track(name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL); + SimCore::Position pos; + double lat = fRand(-90, 90); + double lon = fRand(-180, 180); + + pos.setGeodesicPos( lat, lon, 0); + track.setPosition(pos); +} + + + + + + auto id0 = SimCore::Identifier(); + LOG_S(INFO)<(id0.getUUID(),"Sensor", "127.0.0.1", SimCore::SensorKinds::RADAR); + auto client = std::make_shared(5557,"127.0.0.1",id0.getUUID()); + SensorManager_->addSensorLocal(std::move(control)); + // sendRandomTrack(client,5); + + auto id1 = SimCore::Identifier(); + auto control1 = std::make_unique(id1.getUUID(),"Sensor1", "127.0.0.1", SimCore::SensorKinds::RADAR); + auto client1 = std::make_shared(5557,"127.0.0.1",id1.getUUID()); + SensorManager_->addSensorLocal(std::move(control1)); + // sendRandomTrack(client1,5); + + auto id2 = SimCore::Identifier(); + auto control2 = std::make_unique(id2.getUUID(),"Sensor2", "127.0.0.1", SimCore::SensorKinds::VISUAL); + auto client2 = std::make_shared(5557,"127.0.0.1",id2.getUUID()); + SensorManager_->addSensorLocal(std::move(control2)); + // sendRandomTrack(client2,5); + + auto id3 = SimCore::Identifier(); + auto control3 = std::make_unique(id3.getUUID(),"Sensor3", "127.0.0.1", SimCore::SensorKinds::ESM); + auto client3 = std::make_shared(5557,"127.0.0.1",id3.getUUID()); + SensorManager_->addSensorLocal(std::move(control3)); + sendRandomTrack(client2,5); + +for (int i = 0; i < 3; i++) { + std::string name = "identical-" + std::to_string(i); + SimCore::SimTrack track(name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL); + SimCore::Position pos; + double lat = fRand(-90, 90); + double lon = fRand(-180, 180); + + pos.setGeodesicPos( lat, lon, 0); + track.setPosition(pos); + client->sendMessage(track.buildMessage()); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + client1->sendMessage(track.buildMessage()); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + client2->sendMessage(track.buildMessage()); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + client3->sendMessage(track.buildMessage()); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + +} + +} + + +void startSensors(SimCore::Identifier ownid, std::string GroundTruthAddr_,ushort GroundTruthPort_, ushort SensorPort_ , auto SensorManager_) + { + LOG_S(INFO)<<"__ONLINE__"; + std::string SensorUUID_ = xg::newGuid().str(); + LOG_S(INFO)<(ownid.getUUID(),SensorUUID_,"radar:latest"); + + RadarPod->setEnvironmentVar("SERVER_IP", SimCore::UtilFunctions::getOwnIP()); + + nlohmann::json vars; + vars["OWN_SHIP_ID"] = ownid.getUUID(); + vars["SENSOR_ID"] = SensorUUID_; + vars["SENSOR_NAME"] = "APAR"; + vars["GROUNDTRUTH_PORT"] = std::to_string(GroundTruthPort_); + vars["GROUNDTRUTH_ADDR"] = GroundTruthAddr_; + vars["TRANSMIT_POWER"] = "66"; + vars["GAIN_TRANSMIT"] = "40"; + vars["GAIN_RECEIVE"] = "40"; + vars["TRANSMIT_FREQUENCY"] = "2657"; + vars["INSTRUMENTED_RANGE"] = "150000"; + vars["MOUNTED_HEIGHT"] = "23"; + vars["OWN_SHIP_IP"] = SimCore::UtilFunctions::getOwnIP(); + vars["OWN_SHIP_PORT"] = std::to_string(SensorPort_); + + RadarPod->setEnvironmentVar("CONFIG", vars.dump()); + + RadarPod->setComponent("Radar"); + + SensorManager_->startSensor(RadarPod, SimCore::SensorKinds::RADAR); + + } + +SCENARIO("Testing the SimCore SensorManager") +{ + GIVEN("different Attributes for a Track in different forms") + { + SimCore::Identifier id; + + auto PodController_ = std::make_shared("docs/config"); + + + auto SensorManager_ = std::make_shared(id, PodController_,5557); + + + startSensors(id, "239.0.0.1", 7000, 5557, SensorManager_); + + + + WHEN("constructing Track Object with data") + { + + THEN("check if Track attributes are correct") + { + REQUIRE(PodController_->getInfoForAllPods().size() == 1); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + SensorManager_->deleteAllSensor(); + + REQUIRE(PodController_->getInfoForAllPods().size() == 0); + + + } //THEN + } // WHEN + } // GIVEN +} //SCENARIO + + +SCENARIO("Testing the SimCore SensorManager with local sensors") +{ + GIVEN("different Attributes for a Track in different forms") + { + SimCore::Identifier id; + + auto PodController_ = std::make_shared("docs/config"); + + + auto SensorManager_ = std::make_shared(id, PodController_,5557); + + + sendRandomTracktoManager(SensorManager_); + // std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + + WHEN("constructing Track Object with data") + { + std::string tracklist1 = SensorManager_->getTracklistStringBySensor(); + std::string tracklist2 = SensorManager_->getTracklistStringFusioned(); + + LOG_S(INFO)<getSensorCount() == 4); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + + + } //THEN + } // WHEN + } // GIVEN +} //SCENARIO \ No newline at end of file diff --git a/tests/test_Tracklist.cpp b/tests/test_Tracklist.cpp index bbf62ea..ad15ed4 100644 --- a/tests/test_Tracklist.cpp +++ b/tests/test_Tracklist.cpp @@ -1,6 +1,6 @@ #include "Entities/Tracklist/Tracklist.hpp" -#include "Entities/Tracklist/TracklistItem.hpp" +#include #include "SimCore/IdentifierMaker.hpp" #include "SimCore/Messages/SimTrack.hpp" #include "SimCore/Position.hpp" @@ -36,17 +36,9 @@ SCENARIO("Testing the SimCore Sensor") track->Course.setValue(course); - TrackList::SensorData Sensor1 = - { - .sensorID = SimCore::Identifier(), - .Sensorname = "ARPA" - }; + SimCore::SensorData Sensor1(SimCore::Identifier(),"ARPA","127.0.0.1", SimCore::SensorKinds::RADAR); - TrackList::SensorData Sensor2 = - { - .sensorID = SimCore::Identifier(), - .Sensorname = "SMART-L" - }; + SimCore::SensorData Sensor2(SimCore::Identifier(),"SMART","127.0.0.1", SimCore::SensorKinds::RADAR); @@ -66,8 +58,8 @@ SCENARIO("Testing the SimCore Sensor") List.addTrack(track); // REQUIRE(List.getTrack(id)->Course.getValue() == 270); // REQUIRE(List.getTrack(id)->getSensorCount() == 2); - std::this_thread::sleep_for(std::chrono::milliseconds(5500)); - REQUIRE(List.getSize() == 0); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + REQUIRE(List.getSize() == 1); // List.stopSanitizer(); diff --git a/tests/test_TracklistItem.cpp b/tests/test_TracklistItem.cpp deleted file mode 100644 index 1ea25d9..0000000 --- a/tests/test_TracklistItem.cpp +++ /dev/null @@ -1,79 +0,0 @@ - -#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