From b11f638fec9414ec6f02d4962d3e30d601f392f6 Mon Sep 17 00:00:00 2001 From: Tobias Falkenstein Date: Fri, 19 Apr 2024 09:53:57 +0200 Subject: [PATCH] osi: Serialize OsiSensor active state --- osi/CMakeLists.txt | 1 + osi/include/cloe/component/osi_sensor.hpp | 8 +++++- osi/include/cloe/utility/osi_utils.hpp | 15 +++++++++++ osi/src/cloe/component/osi_sensor.cpp | 33 +++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 osi/src/cloe/component/osi_sensor.cpp diff --git a/osi/CMakeLists.txt b/osi/CMakeLists.txt index b103390de..e5a2f5e96 100644 --- a/osi/CMakeLists.txt +++ b/osi/CMakeLists.txt @@ -16,6 +16,7 @@ message(STATUS "-> Building cloe-osi library.") file(GLOB cloe-osi_PUBLIC_HEADERS "include/**/*.hpp") add_library(cloe-osi # find src -type f -name "*.cpp" \! -name "*_test.cpp" + src/cloe/component/osi_sensor.cpp src/cloe/utility/osi_ground_truth.cpp src/cloe/utility/osi_message_handler.cpp src/cloe/utility/osi_transceiver_tcp.cpp diff --git a/osi/include/cloe/component/osi_sensor.hpp b/osi/include/cloe/component/osi_sensor.hpp index ae9096b11..cdd048ed5 100644 --- a/osi/include/cloe/component/osi_sensor.hpp +++ b/osi/include/cloe/component/osi_sensor.hpp @@ -23,7 +23,8 @@ #include // for SensorData -#include // for Component +#include // for Component, Json +#include // for osi_to_json namespace cloe { @@ -41,6 +42,11 @@ class OsiSensor : public Component { * Return OSI-SensorData */ [[nodiscard]] virtual const osi3::SensorData& get() const = 0; + + /** + * Writes JSON representation into j. + */ + fable::Json active_state() const override; }; } // namespace cloe diff --git a/osi/include/cloe/utility/osi_utils.hpp b/osi/include/cloe/utility/osi_utils.hpp index a9e785115..05538d685 100644 --- a/osi/include/cloe/utility/osi_utils.hpp +++ b/osi/include/cloe/utility/osi_utils.hpp @@ -45,6 +45,21 @@ namespace cloe::utility { inline Logger osi_logger() { return logger::get("vtd/osi"); } +/** + * Serialize OSI message to a json string. + * + * Currently implemented for: + * + * - osi3::SensorData + * - osi3::SensorView + * - osi3::GroundTruth + * + * \param[in] msg OSI data to serialize + * \param[out] json_string string to serialize json into + */ +template +void osi_to_json(const OSI_T& msg, std::string* json_string); + /** * Write OSI message to a .json file. */ diff --git a/osi/src/cloe/component/osi_sensor.cpp b/osi/src/cloe/component/osi_sensor.cpp new file mode 100644 index 000000000..c16f2abb3 --- /dev/null +++ b/osi/src/cloe/component/osi_sensor.cpp @@ -0,0 +1,33 @@ +/* + * Copyright 2024 Robert Bosch GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include // for Json, parse_json + +namespace cloe { + +fable::Json OsiSensor::active_state() const { + std::string protobuf_json; + cloe::utility::osi_to_json(this->get(), &protobuf_json); + return fable::Json{ + {"sensor_data", fable::parse_json(protobuf_json)} + }; +} + +} // namespace cloe