Skip to content

Commit

Permalink
Tuesday Backup
Browse files Browse the repository at this point in the history
  • Loading branch information
andistorm committed Sep 5, 2023
1 parent 11f237f commit 9d3b6ec
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 40 deletions.
4 changes: 2 additions & 2 deletions include/framework/everest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class Everest {
/// \brief Subscribes to an error of another module indentified by the given \p req and error type
/// \p error_type. The given \p callback is called when a new error is raised
///
void subscribe_error(const Requirement& req, const Error::ErrorType& error_type, const ErrorCallback& callback);
void subscribe_error(const Requirement& req, const std::string& error_type, const JsonCallback& callback);

///
/// \brief Subscribes to an error cleared event of another module indentified by the given \p req and error type
/// \p error_type. The given \p callback is called when an error is cleared
///
void subscribe_error_cleared(const Requirement& req, const Error::ErrorType& error_type, const ErrorCallback& callback);
void subscribe_error_cleared(const Requirement& req, const std::string& error_type, const JsonCallback& callback);

///
/// \brief Requests to clear the error with the given \p uuid of the given \p impl_id
Expand Down
44 changes: 35 additions & 9 deletions include/utils/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
#include <map>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/lexical_cast.hpp>

#include <filesystem>

#include <fmt/format.h>

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

// custom exception
class NotValidErrorType : public std::exception {
public:
Expand All @@ -33,8 +39,7 @@ class Error {
};
static const std::string severity_to_string(const Severity& s);
static const Severity string_to_severity(const std::string& s);

// LTODO: Better name?

struct ModuleIdentifier {
public:
ModuleIdentifier(
Expand All @@ -44,7 +49,7 @@ class Error {
const std::string module;
const std::string implementation;
};
using time_point = std::chrono::time_point<std::chrono::system_clock>;
using time_point = std::chrono::time_point<date::utc_clock>;

using ErrorType = std::string;
static const std::string get_description_from_error_type(const ErrorType& error_type);
Expand All @@ -55,6 +60,7 @@ class Error {
const std::string& message,
const ModuleIdentifier& from,
const bool persistent,
const Severity& severity,
const time_point& timestamp,
const boost::uuids::uuid& uuid
) :
Expand All @@ -63,6 +69,7 @@ class Error {
message(message),
from(from),
persistent(persistent),
severity(severity),
timestamp(timestamp),
uuid(uuid)
{
Expand All @@ -73,37 +80,56 @@ class Error {
Error(
const ErrorType& type,
const std::string& message,
const ModuleIdentifier& from
const ModuleIdentifier& from,
const Severity& severity
) : Error(
type,
message,
from,
false,
std::chrono::system_clock::now(),
severity,
date::utc_clock::now(),
boost::uuids::random_generator()()
) {};
Error(
const ErrorType& type,
const std::string& message,
const std::string& from_module,
const std::string& from_implementation
const std::string& from_implementation,
const Severity& severity
) : Error(
type,
message,
ModuleIdentifier(from_module, from_implementation)
ModuleIdentifier(from_module, from_implementation),
severity
) {};
Error(

Check notice on line 106 in include/utils/error.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/utils/error.hpp#L106

Class 'Error' has a constructor with 1 argument that is not explicit.
const nlohmann::json& error
) : Error(
error["type"].get<std::string>(),
error["message"].get<std::string>(),
ModuleIdentifier(
error["from"]["module"].get<std::string>(),
error["from"]["implementation"].get<std::string>()
),
error["persistent"].get<bool>(),
string_to_severity(error["severity"].get<std::string>()),
Everest::Date::from_rfc3339(error["timestamp"].get<std::string>()),
boost::lexical_cast<boost::uuids::uuid>(error["uuid"].get<std::string>())
) {};
// LTODO: Should be called somewhere
static void initialize_error_types(std::filesystem::path error_types_dir);

private:
public:
const ErrorType type;
const std::string description;
const std::string message;
const bool persistent;
const Severity severity;
const ModuleIdentifier from;
const time_point timestamp;
const boost::uuids::uuid uuid;

private:
inline static std::map<ErrorType, std::string> error_types = {};
};

Expand Down
3 changes: 1 addition & 2 deletions include/utils/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ using Arguments = std::map<std::string, ArgumentType>;
using ReturnType = std::vector<std::string>;
using JsonCallback = std::function<void(json)>;
using ValueCallback = std::function<void(Value)>;
// LTODO: ErrorCallback = std::function<void(Error)>;
using ErrorCallback = std::function<void(json)>;
using ErrorCallback = std::function<void(Error)>;
using ConfigEntry = std::variant<std::string, bool, int, double>;
using ConfigMap = std::map<std::string, ConfigEntry>;
using ModuleConfigs = std::map<std::string, ConfigMap>;
Expand Down
11 changes: 7 additions & 4 deletions lib/error.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
#include <utils/error.hpp>
#include <utils/yaml_loader.hpp>
#include <everest/logging.hpp>

#include <nlohmann/json.hpp>
#include <list>
Expand Down Expand Up @@ -49,10 +51,10 @@ void Error::initialize_error_types(std::filesystem::path error_types_dir) {
continue;
}
std::string prefix = entry.path().stem().string();
std::ifstream file(entry.path());
nlohmann::json error_type_file = nlohmann::json::parse(file);
nlohmann::json error_type_file = Everest::load_yaml(entry.path());
if (!error_type_file.contains("errors")) {
throw std::runtime_error(fmt::format("Error type file '{}' does not contain 'errors' key.", entry.path().string()));
EVLOG_warning << "Error type file '" << entry.path().string() << "' does not contain 'errors' key.";
continue;
}
if (!error_type_file["errors"].is_array()) {
throw std::runtime_error(fmt::format("Error type file '{}' does not contain an array under 'errors' key.", entry.path().string()));
Expand All @@ -68,7 +70,8 @@ void Error::initialize_error_types(std::filesystem::path error_types_dir) {
if (exists(complete_name)) {
throw std::runtime_error(fmt::format("Error type file '{}' contains an error with the name '{}' which is already defined.", entry.path().string(), complete_name));
}
error_types[complete_name] = error["descritpion"].get<std::string>();
std::string description = error["description"].get<std::string>();
error_types[complete_name] = description;
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions lib/everest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void Everest::subscribe_var(const Requirement& req, const std::string& var_name,
this->mqtt_abstraction.register_handler(var_topic, token, QOS::QOS2);
}

void Everest::subscribe_error(const Requirement& req, const Error::ErrorType& error_type, const ErrorCallback& callback) {
void Everest::subscribe_error(const Requirement& req, const std::string& error_type, const JsonCallback& callback) {
BOOST_LOG_FUNCTION();

EVLOG_debug << fmt::format("subscribing to error: {}:{}", req.id, error_type);
Expand Down Expand Up @@ -413,14 +413,13 @@ void Everest::subscribe_error(const Requirement& req, const Error::ErrorType& er
};

const auto error_topic = fmt::format("{}/error/{}", this->config.mqtt_prefix(requirement_module_id, requirement_impl_id), error_type);
// const auto error_topic = fmt::format("{}/error/{}", this->config.mqtt_module_prefix(requirement_module_id), Error::error_type_to_string(error_type));

std::shared_ptr<TypedHandler> token =
std::make_shared<TypedHandler>(error_type, HandlerType::SubscribeError, std::make_shared<Handler>(handler));
this->mqtt_abstraction.register_handler(error_topic, token, QOS::QOS2);
}

void Everest::subscribe_error_cleared(const Requirement& req, const Error::ErrorType& error_type, const ErrorCallback& callback) {
void Everest::subscribe_error_cleared(const Requirement& req, const std::string& error_type, const JsonCallback& callback) {
BOOST_LOG_FUNCTION();

EVLOG_debug << fmt::format("subscribing to error cleared: {}:{}", req.id, error_type);
Expand Down Expand Up @@ -468,11 +467,8 @@ boost::uuids::uuid Everest::raise_error(
const std::string& severity
) {
BOOST_LOG_FUNCTION();

std::string timestamp = date::format("%FT%TZ", date::make_zoned(date::current_zone(), date::floor<std::chrono::milliseconds>(std::chrono::system_clock::now())));
// LTODO: DO I really want to send description in every error object?
std::string description = "No description provided";
// TODO: allow persistent errors
std::string now_str = Date::to_rfc3339(date::utc_clock::now());
std::string description = Error::get_description_from_error_type(error_type);
bool persistent = false;
boost::uuids::uuid uuid = boost::uuids::random_generator()();

Expand All @@ -485,7 +481,7 @@ boost::uuids::uuid Everest::raise_error(
{"module", this->module_id},
{"implementation", impl_id}
}},
{"timestamp", timestamp},
{"timestamp", now_str},
{"uuid", boost::uuids::to_string(uuid)},
{"severity", severity}
});
Expand Down
16 changes: 12 additions & 4 deletions lib/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,25 @@ int ModuleLoader::initialize() {
module_adapter.subscribe_error = [&everest](
const Requirement& req,
const Error::ErrorType error_type,
const ErrorCallback& callback
const ErrorCallback& error_callback
) {
return everest.subscribe_error(req, error_type, callback);
JsonCallback json_callback = [error_callback](json j) {
const Error error(j);
error_callback(error);
};
return everest.subscribe_error(req, error_type, json_callback);
};

module_adapter.subscribe_error_cleared = [&everest](
const Requirement& req,
const Error::ErrorType error_type,
const ErrorCallback& callback
const ErrorCallback& error_callback
) {
return everest.subscribe_error_cleared(req, error_type, callback);
JsonCallback json_callback = [error_callback](json j) {
const Error error(j);
return error_callback(error);
};
return everest.subscribe_error_cleared(req, error_type, json_callback);
};

module_adapter.raise_error = [&everest](
Expand Down
1 change: 1 addition & 0 deletions schemas/error.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $defs:
name:
type: string
minLength: 2
pattern: ^[A-Z][A-Za-z0-9]*$
description:
type: string
minLength: 2
Expand Down
17 changes: 7 additions & 10 deletions schemas/interface.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,13 @@ properties:
# add empty vars if not already present
default: {}

# LTODO: Rework
errors:
description: Defines the errors of this unit
type: object
patternProperties:
^[a-zA-Z_][a-zA-Z0-9_.-]*$:
description: json schema declaring a namespace of errors
$ref: '#/$defs/error_namespace_subschema'
additionalProperties: false
default: {}

description: This describes a list of exported errors of this unit
type: array
items:
type: string
pattern: ^[a-z][a-z0-9_]*/[A-Z][a-zA-Z0-9]*$
# add empty errors if not already present
default: []

additionalProperties: false

0 comments on commit 9d3b6ec

Please sign in to comment.