Skip to content

Commit

Permalink
Use to_json/from_json
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Heinrich <[email protected]>
  • Loading branch information
andistorm committed Dec 6, 2023
1 parent b3a5dad commit da50fd4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 60 deletions.
41 changes: 33 additions & 8 deletions include/utils/error/error_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,40 @@
#ifndef UTILS_ERROR_JSON_HPP
#define UTILS_ERROR_JSON_HPP

#include <utils/error/error.hpp>
#include <nlohmann/json.hpp>

namespace Everest {
namespace error {
NLOHMANN_JSON_NAMESPACE_BEGIN
template <>
struct adl_serializer<Everest::error::Error> {
static void to_json(json& j, const Everest::error::Error& e) {
j = {
{"type", e.type},
{"description", e.description},
{"message", e.message},
{"from", {
{"module", e.from.module_id},
{"implementation", e.from.implementation_id}
}},
{"severity", Everest::error::severity_to_string(e.severity)},
{"timestamp", Everest::Date::to_rfc3339(e.timestamp)},
{"uuid", e.uuid.uuid},
{"state", Everest::error::state_to_string(e.state)}
};
}
static Everest::error::Error from_json(const json& j) {
Everest::error::ErrorType type = j.at("type");
std::string message = j.at("message");
std::string description = j.at("description");
ImplementationIdentifier from =
ImplementationIdentifier(j.at("from").at("module"), j.at("from").at("implementation"));
Everest::error::Severity severity = Everest::error::string_to_severity(j.at("severity"));
Everest::error::Error::time_point timestamp = Everest::Date::from_rfc3339(j.at("timestamp"));
Everest::error::UUID uuid(j.at("uuid"));
Everest::error::State state = Everest::error::string_to_state(j.at("state"));

Error json_to_error(const json& j);
json error_to_json(const Error& e);

} // namespace error
} // namespace Everest
return {type, message, description, from, severity, timestamp, uuid, state};
}
};

NLOHMANN_JSON_NAMESPACE_END
#endif // UTILS_ERROR_JSON_HPP
1 change: 0 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ target_sources(framework
error/error_comm_bridge.cpp
error/error_database_map.cpp
error/error_filter.cpp
error/error_json.cpp
error/error_manager.cpp
error/error_type_map.cpp
everest.cpp
Expand Down
4 changes: 2 additions & 2 deletions lib/error/error_comm_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void ErrorCommBridge::handle_error(const json& data) {
BOOST_LOG_FUNCTION();
EVLOG_debug << "Received error: " << data.dump(1);

ErrorPtr error = std::make_shared<Error>(json_to_error(data));
ErrorPtr error = std::make_shared<Error>(data.get<Error>());
this->error_manager->raise_error(error);
}

Expand Down Expand Up @@ -75,7 +75,7 @@ void ErrorCommBridge::handle_request_clear_error(const json& data) {
for (auto& error : cleared_errors) {
std::string error_cleared_topic =
error->from.module_id + "/" + error->from.implementation_id + "/error-cleared/" + error->type;
this->send_json_message(error_cleared_topic, error_to_json(*error));
this->send_json_message(error_cleared_topic, json(*error));
}

// send result
Expand Down
42 changes: 0 additions & 42 deletions lib/error/error_json.cpp

This file was deleted.

4 changes: 1 addition & 3 deletions lib/everest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,9 @@ std::string Everest::raise_error(const std::string& impl_id, const std::string&

error::Error error(error_type, message, description, this->module_id, impl_id, severity_enum);

json data = error::error_to_json(error);

const auto error_topic = fmt::format("{}/error/{}", this->config.mqtt_prefix(this->module_id, impl_id), error_type);

this->mqtt_abstraction.publish(error_topic, data, QOS::QOS2);
this->mqtt_abstraction.publish(error_topic, json(error), QOS::QOS2);
return error.uuid.uuid;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,23 +414,23 @@ int ModuleLoader::initialize() {

module_adapter.subscribe_error = [&everest](const Requirement& req, const std::string& error_type,
const error::ErrorCallback& error_callback) {
JsonCallback json_callback = [error_callback](json j) { error_callback(error::json_to_error(j)); };
JsonCallback json_callback = [error_callback](json j) { error_callback(j.get<error::Error>()); };
return everest.subscribe_error(req, error_type, json_callback);
};

module_adapter.subscribe_all_errors = [&everest](const error::ErrorCallback& error_callback) {
JsonCallback json_callback = [error_callback](json j) { error_callback(error::json_to_error(j)); };
JsonCallback json_callback = [error_callback](json j) { error_callback(j.get<error::Error>()); };
return everest.subscribe_all_errors(json_callback);
};

module_adapter.subscribe_error_cleared = [&everest](const Requirement& req, const std::string& error_type,
const error::ErrorCallback& error_callback) {
JsonCallback json_callback = [error_callback](json j) { return error_callback(error::json_to_error(j)); };
JsonCallback json_callback = [error_callback](json j) { return error_callback(j.get<error::Error>()); };
return everest.subscribe_error_cleared(req, error_type, json_callback);
};

module_adapter.subscribe_all_errors_cleared = [&everest](const error::ErrorCallback& error_callback) {
JsonCallback json_callback = [error_callback](json j) { return error_callback(error::json_to_error(j)); };
JsonCallback json_callback = [error_callback](json j) { return error_callback(j.get<error::Error>()); };
return everest.subscribe_all_errors_cleared(json_callback);
};

Expand Down

0 comments on commit da50fd4

Please sign in to comment.