From 3333fadf204d7c73e16a9e7b93213001f9ce6d2e Mon Sep 17 00:00:00 2001 From: hwinkel Date: Wed, 5 Jul 2023 22:11:44 +0200 Subject: [PATCH] ADD: added json lib and made some changes in the entity class --- .gitmodules | 3 ++ CMakeLists.txt | 6 ++++ include/Entities/Entity.hpp | 4 ++- libs/SimCore | 2 +- libs/nlohmannJSON | 1 + src/Entities/Entity.cpp | 62 +++++++++++++++++++++++-------------- 6 files changed, 53 insertions(+), 25 deletions(-) create mode 160000 libs/nlohmannJSON diff --git a/.gitmodules b/.gitmodules index e20bbe9..b408c5c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "libs/whisper-com"] path = libs/SimCore/libs/whisper-com url = ssh://git@dev-gitea.ftewa.ti.unibw-hamburg.de:12000/hwinkel/whisper-com.git +[submodule "libs/nlohmannJSON"] + path = libs/nlohmannJSON + url = https://github.com/nlohmann/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt index a80895e..98068d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,11 @@ set(TEST_SIMCORE_LIBRARY OFF CACHE INTERNAL "") add_subdirectory(libs/SimCore EXCLUDE_FROM_ALL) ENDIF() +IF(NOT TARGET nlohmann_json) +set(JSON_BuildTests_INIT OFF CACHE INTERNAL "") + add_subdirectory(libs/nlohmannJSON EXCLUDE_FROM_ALL) +ENDIF() + protobuf_generate_cpp(PROTO_PATH include/Orders/protos CPP_PATH include/Orders/protos HPP_PATH include/Orders/protos) @@ -73,6 +78,7 @@ target_link_libraries(EntityLibrary SimCore eigen loguru +nlohmann_json ) # add_dependencies(SimCore protoc) diff --git a/include/Entities/Entity.hpp b/include/Entities/Entity.hpp index 73ea315..f3d5732 100644 --- a/include/Entities/Entity.hpp +++ b/include/Entities/Entity.hpp @@ -19,6 +19,8 @@ #include #include #include +#include + #include #include #include @@ -98,7 +100,7 @@ namespace Entities { void startMovementWorker(); std::atomic MovementWorkerStarted = false; std::shared_ptr MovemtServer_ = nullptr; - + void handleMovement(); diff --git a/libs/SimCore b/libs/SimCore index 1a878af..0123a22 160000 --- a/libs/SimCore +++ b/libs/SimCore @@ -1 +1 @@ -Subproject commit 1a878af6a57ca7d94bcf7bcd9d688340e6ab6c05 +Subproject commit 0123a22aacae6973146bada32d966fcaa58dbddc diff --git a/libs/nlohmannJSON b/libs/nlohmannJSON new file mode 160000 index 0000000..5d27543 --- /dev/null +++ b/libs/nlohmannJSON @@ -0,0 +1 @@ +Subproject commit 5d2754306d67d1e654a1a34e1d2e74439a9d53b3 diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index a9ac262..eb2f665 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -34,6 +34,7 @@ namespace Entities { OwnShipTrack = std::make_shared(OwnID, OwnType, EntityKind); + OwnShipTrack->setPosition(SimCore::Position()); MovemtServer_ = std::make_shared(__MOVEMENT_SERVER_PORT__); @@ -52,19 +53,31 @@ namespace Entities void Entity::setPosition(SimCore::Position pos) { OwnShipTrack->setPosition(pos); + SimCore::SimTrack newSimTrack(OwnShipTrack->getIdentifier(),WHISPER::SourceType::ENTITY,EntityKind_); + newSimTrack.setPosition(pos); } void Entity::setSpeed(double val) { OwnShipTrack->Speed.setValue(val); + SimCore::SimTrack newSimTrack(OwnShipTrack->getIdentifier(),WHISPER::SourceType::ENTITY,EntityKind_); + newSimTrack.Speed.setValue(val); + MovemtServer_->sendMessage(newSimTrack.buildMessage().serialize()); + } void Entity::setCourse(double val) { OwnShipTrack->Course.setValue(val); - + SimCore::SimTrack newSimTrack(OwnShipTrack->getIdentifier(),WHISPER::SourceType::ENTITY,EntityKind_); + newSimTrack.Course.setValue(val); + LOG_S(INFO)<< "NEW Course: "<< val; + MovemtServer_->sendMessage(newSimTrack.buildMessage().serialize()); } void Entity::setPitch( double val) { OwnShipTrack->Pitch.setValue(val); + SimCore::SimTrack newSimTrack(OwnShipTrack->getIdentifier(),WHISPER::SourceType::ENTITY,EntityKind_); + newSimTrack.Pitch.setValue(val); + } void Entity::start() @@ -107,15 +120,33 @@ namespace Entities if (MovemtServer_->countClients() > 0 ) { - std::string msg = MovemtServer_->getLatestMessage(); - if (msg == "Hello Server") - { + if(OwnShipTrack->getPosition().isValid()) + { + LOG_S(INFO)<< "POS: LAT: "<< OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LATITUDE) << " LON: " << OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LONGITUDE); + MovementWorkerStarted = true; MovemtServer_->sendMessage(OwnShipTrack->buildMessage().serialize()); - } - } - + LOG_S(INFO)<< "Initial Message send to MovementWorker"; + } + } + } + + void Entity::handleMovement() + { + if (!MovementWorkerStarted) + { + startMovementWorker(); + }else + { + std::string msg = MovemtServer_->getLatestMessage(); + auto newTrack = SimCore::SimTrack::unpack(msg); + if (newTrack != nullptr) + { + OwnShipTrack->setPosition(newTrack->getPosition()); + LOG_S(INFO)<< "new POS: LAT: "<< OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LATITUDE) << " LON: " << OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LONGITUDE); + } + } } @@ -127,22 +158,7 @@ namespace Entities LOG_S(INFO)<< "main loop started"; while (!stopMainLoop) { - if (!MovementWorkerStarted) - { - startMovementWorker(); - }else - { - std::string msg = MovemtServer_->getLatestMessage(); - auto newTrack = SimCore::SimTrack::unpack(msg); - if (newTrack != nullptr) - { - OwnShipTrack->setPosition(newTrack->getPosition()); - LOG_S(INFO)<< "new POS: LAT: "<< OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LATITUDE) << " LON: " << OwnShipTrack->getPosition().getGeodesicPos()(SimCore::LONGITUDE); - } - - - - } + handleMovement(); std::this_thread::sleep_for(std::chrono::milliseconds(500));