From cae1e6bd4cc80bc36b6fef8eadb363efc6393a82 Mon Sep 17 00:00:00 2001 From: MarzellT Date: Tue, 27 Aug 2024 13:17:17 +0200 Subject: [PATCH] changes how paths are handled and does some small refactorings Signed-off-by: MarzellT --- config/config-sil-ocpp-custom-extension.yaml | 2 +- modules/OCPPConfiguration/CMakeLists.txt | 13 +++++++++++++ .../OCPPConfiguration/OCPPConfiguration.cpp | 4 ++-- .../OCPPConfiguration/OCPPConfiguration.hpp | 4 ++-- modules/OCPPConfiguration/doc.rst | 13 +++++++++---- .../OCPPConfiguration/main/event_handler.cpp | 18 +++++++++++------- .../OCPPConfiguration/main/event_handler.hpp | 13 +++++++------ .../main/everest_config_mapping.hpp | 1 + .../OCPPConfiguration/main/mapping_reader.cpp | 8 ++++---- .../OCPPConfiguration/main/mapping_reader.hpp | 5 +++-- modules/OCPPConfiguration/main/util.cpp | 3 +-- modules/OCPPConfiguration/manifest.yaml | 8 ++++---- .../OCPPConfiguration/mappings/CMakeLists.txt | 10 ++++++++++ .../{ => mappings}/example-config-mapping.yaml | 0 14 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 modules/OCPPConfiguration/mappings/CMakeLists.txt rename modules/OCPPConfiguration/{ => mappings}/example-config-mapping.yaml (100%) diff --git a/config/config-sil-ocpp-custom-extension.yaml b/config/config-sil-ocpp-custom-extension.yaml index a7e655cf4a..0a0040e20c 100644 --- a/config/config-sil-ocpp-custom-extension.yaml +++ b/config/config-sil-ocpp-custom-extension.yaml @@ -6,7 +6,7 @@ active_modules: - module_id: ocpp implementation_id: ocpp_generic config_module: - user_config_path: "config/user-config/ocpp-remote-config.yaml" + user_config_file_name: "config-sil-ocpp-custom-extension.yaml" iso15118_charger: module: EvseV2G config_module: diff --git a/modules/OCPPConfiguration/CMakeLists.txt b/modules/OCPPConfiguration/CMakeLists.txt index 1780caca88..29003b8283 100644 --- a/modules/OCPPConfiguration/CMakeLists.txt +++ b/modules/OCPPConfiguration/CMakeLists.txt @@ -29,4 +29,17 @@ target_sources(${MODULE_NAME} # insert other things like install cmds etc here target_compile_features(${MODULE_NAME} PRIVATE cxx_std_17) set_target_properties(${MODULE_NAME} PROPERTIES CXX_EXTENSIONS OFF) + +# add a compiler definition of the config install directory so that it can be accessed in code +set( + "${PROJECT_NAME}_${MODULE_NAME}_USER_CONFIG_INSTALL_DIRECTORY" + "${CMAKE_INSTALL_PREFIX}/etc/everest/user-config" +) +target_compile_definitions( + ${MODULE_NAME} + PRIVATE + USER_CONFIG_INSTALL_DIRECTORY="${${PROJECT_NAME}_${MODULE_NAME}_USER_CONFIG_INSTALL_DIRECTORY}" +) + +add_subdirectory(mappings) # ev@c55432ab-152c-45a9-9d2e-7281d50c69c3:v1 diff --git a/modules/OCPPConfiguration/OCPPConfiguration.cpp b/modules/OCPPConfiguration/OCPPConfiguration.cpp index 438abed356..19cd1e2dbd 100644 --- a/modules/OCPPConfiguration/OCPPConfiguration.cpp +++ b/modules/OCPPConfiguration/OCPPConfiguration.cpp @@ -13,7 +13,7 @@ void OCPPConfiguration::init() { void OCPPConfiguration::ready() { invoke_ready(*p_main); - const auto mapping_file_path = std::filesystem::path{config.mapping_file_path}; + const auto mapping_file_path = std::filesystem::path{config.mapping_file_name}; try { event_handler = std::make_unique(mapping_file_path); @@ -25,7 +25,7 @@ void OCPPConfiguration::ready() { r_ocpp_module->call_monitor_variables(event_handler->get_monitor_variables()); r_ocpp_module->subscribe_event_data( - [&](const auto& event_data) { event_handler->try_handle_event(event_data, config.user_config_path); }); + [&](const auto& event_data) { event_handler->try_handle_event(event_data, config.user_config_file_name); }); } } // namespace module diff --git a/modules/OCPPConfiguration/OCPPConfiguration.hpp b/modules/OCPPConfiguration/OCPPConfiguration.hpp index 418f70b8bb..303119e3c2 100644 --- a/modules/OCPPConfiguration/OCPPConfiguration.hpp +++ b/modules/OCPPConfiguration/OCPPConfiguration.hpp @@ -24,8 +24,8 @@ namespace module { struct Conf { - std::string user_config_path; - std::string mapping_file_path; + std::string user_config_file_name; + std::string mapping_file_name; }; class OCPPConfiguration : public Everest::ModuleBase { diff --git a/modules/OCPPConfiguration/doc.rst b/modules/OCPPConfiguration/doc.rst index 8b3ec9285e..701b1d6bd8 100644 --- a/modules/OCPPConfiguration/doc.rst +++ b/modules/OCPPConfiguration/doc.rst @@ -14,10 +14,15 @@ The following configuration options are available: .. _user_config_path: -**user_config_path** - - **Description**: Path to the user configuration file. +**user_config_file_name** + - **Description**: File name of the user configuration file. + This should match the name of the loaded config. + When an ocpp configuration request is successfully handled this module will write the configuration + parameters into this file in the installation directory. If this file shouldn't exist it will be + automatically created. .. _mapping_file_path: -**mapping_file_path** - - **Description**: Path to the mapping file. \ No newline at end of file +**mapping_file_name** + - **Description**: Name of the mapping file. + This file has to be in the `mappings` directory of this module which will be installed. \ No newline at end of file diff --git a/modules/OCPPConfiguration/main/event_handler.cpp b/modules/OCPPConfiguration/main/event_handler.cpp index 120967a1ed..ecf1f1a475 100644 --- a/modules/OCPPConfiguration/main/event_handler.cpp +++ b/modules/OCPPConfiguration/main/event_handler.cpp @@ -4,16 +4,17 @@ #include "event_handler.hpp" #include "util.hpp" #include + #include namespace module { -EventHandler::EventHandler(const std::filesystem::path& config_mapping_path) : - config_mapping(mapping_reader::read_mapping(config_mapping_path)) { +EventHandler::EventHandler(const std::filesystem::path& config_mapping_file_name) : + config_mapping(mapping_reader::read_mapping(config_mapping_file_name)) { } void EventHandler::try_handle_event(const types::ocpp::EventData& event_data, - const std::string& user_config_path_string) noexcept { + const std::filesystem::path& user_config_file_name) noexcept { try { const auto& everest_module_mapping_opt = get_optional_mapping_by_component_variable(event_data.component_variable); @@ -24,10 +25,13 @@ void EventHandler::try_handle_event(const types::ocpp::EventData& event_data, const auto& everest_module_mapping = everest_module_mapping_opt.value(); - write_event_to_config(event_data, user_config_path_string, everest_module_mapping); + write_event_to_config(event_data, user_config_file_name, everest_module_mapping); + + EVLOG_info << "Handled ocpp configuration request: " << event_data.component_variable.variable.name << " " + << event_data.actual_value; } catch (const std::runtime_error& e) { - EVLOG_info << "Couldn't handle event: " << e.what(); + EVLOG_info << "Couldn't handle ocpp configuration request: " << e.what(); } } @@ -42,9 +46,9 @@ std::vector EventHandler::get_monitor_variables( } void EventHandler::write_event_to_config(const types::ocpp::EventData& event_data, - const std::string& user_config_path_string, + const std::filesystem::path& user_config_file_name, const EverestConfigMapping& everest_module_mapping) { - const auto user_config_path = std::filesystem::path{user_config_path_string}; + const auto user_config_path = std::filesystem::path{USER_CONFIG_INSTALL_DIRECTORY} / user_config_file_name; auto tree = util::try_to_load_existing_user_config(user_config_path); util::write_value_to_tree(everest_module_mapping, event_data.actual_value, tree); util::save_tree_to_yaml_file(tree, user_config_path); diff --git a/modules/OCPPConfiguration/main/event_handler.hpp b/modules/OCPPConfiguration/main/event_handler.hpp index b126ec8a8c..c0ffcbd118 100644 --- a/modules/OCPPConfiguration/main/event_handler.hpp +++ b/modules/OCPPConfiguration/main/event_handler.hpp @@ -19,19 +19,19 @@ class EventHandler { /** * Create an EventHandler with the given mapping file. * - * @param config_mapping_path Path to the mapping file. + * @param config_mapping_file_name Name of the mapping file. */ - explicit EventHandler(const std::filesystem::path& config_mapping_path); + explicit EventHandler(const std::filesystem::path& config_mapping_file_name); /** * Try to handle the given event. If successful, the event will be written to the user configuration file given. * * @param event_data The event to handle. Usually received from the OCPP module. - * @param user_config_path_string The path to the user configuration file. This file will be read from and written - * to. + * @param user_config_file_name File name of the user configuration file. This name should match the loaded configs + * name. */ void try_handle_event(const types::ocpp::EventData& event_data, - const std::string& user_config_path_string) noexcept; + const std::filesystem::path& user_config_file_name) noexcept; /** * Get the unique monitor variables that are used by this EventHandler. @@ -45,7 +45,8 @@ class EventHandler { find_mapping_by_component_variable(const types::ocpp::ComponentVariable& component_variable) const; OcppToEverestConfigMapping config_mapping; - void write_event_to_config(const types::ocpp::EventData& event_data, const std::string& user_config_path_string, + void write_event_to_config(const types::ocpp::EventData& event_data, + const std::filesystem::path& user_config_file_name, const EverestConfigMapping& everest_module_mapping); }; diff --git a/modules/OCPPConfiguration/main/everest_config_mapping.hpp b/modules/OCPPConfiguration/main/everest_config_mapping.hpp index 8a5387840e..c578c2ae46 100644 --- a/modules/OCPPConfiguration/main/everest_config_mapping.hpp +++ b/modules/OCPPConfiguration/main/everest_config_mapping.hpp @@ -4,6 +4,7 @@ #pragma once #include "generated/types/ocpp.hpp" + #include // Required to use for std::unordered_map with types::ocpp::ComponentVariable as key diff --git a/modules/OCPPConfiguration/main/mapping_reader.cpp b/modules/OCPPConfiguration/main/mapping_reader.cpp index 993ca1a111..7c6e29ce9e 100644 --- a/modules/OCPPConfiguration/main/mapping_reader.cpp +++ b/modules/OCPPConfiguration/main/mapping_reader.cpp @@ -4,6 +4,7 @@ #include "mapping_reader.hpp" #include "everest/logging.hpp" #include "util.hpp" + #include namespace module { @@ -110,11 +111,10 @@ EverestConfigMapping parse_maps_to_node(const ryml::NodeRef& node) { } // namespace -OcppToEverestConfigMapping mapping_reader::read_mapping(const std::filesystem::path& file_path) { - const auto hacked_file_path = - std::filesystem::path{"modules"} / "OCPPConfiguration" / file_path; // TODO: this is very hacky +OcppToEverestConfigMapping mapping_reader::read_mapping(const std::filesystem::path& file_name) { + const auto mapping_file_path = MAPPING_INSTALL_DIRECTORY / file_name; - const auto tree = util::load_yaml_file(hacked_file_path); + const auto tree = util::load_yaml_file(mapping_file_path); const auto root = tree.rootref(); return parse_mapping(root); diff --git a/modules/OCPPConfiguration/main/mapping_reader.hpp b/modules/OCPPConfiguration/main/mapping_reader.hpp index 48baa40bab..86bb2b8b97 100644 --- a/modules/OCPPConfiguration/main/mapping_reader.hpp +++ b/modules/OCPPConfiguration/main/mapping_reader.hpp @@ -5,6 +5,7 @@ #include "everest_config_mapping.hpp" #include "generated/types/ocpp.hpp" + #include #include #include @@ -28,10 +29,10 @@ namespace mapping_reader { /** * Reads the mapping file and returns the mapping. * - * @param file_path The path to the mapping file. + * @param file_name The file name of the mapping file. * @return The mapping. */ -OcppToEverestConfigMapping read_mapping(const std::filesystem::path& file_path); +OcppToEverestConfigMapping read_mapping(const std::filesystem::path& file_name); }; // namespace mapping_reader diff --git a/modules/OCPPConfiguration/main/util.cpp b/modules/OCPPConfiguration/main/util.cpp index 3536742aee..9826d62c58 100644 --- a/modules/OCPPConfiguration/main/util.cpp +++ b/modules/OCPPConfiguration/main/util.cpp @@ -2,8 +2,7 @@ // Copyright Pionix GmbH and Contributors to EVerest #include "util.hpp" -#include "everest/logging.hpp" -#include "generated/types/ocpp.hpp" + #include #include #include diff --git a/modules/OCPPConfiguration/manifest.yaml b/modules/OCPPConfiguration/manifest.yaml index 9a0ca8f1ad..d2517f13ea 100644 --- a/modules/OCPPConfiguration/manifest.yaml +++ b/modules/OCPPConfiguration/manifest.yaml @@ -1,10 +1,10 @@ description: This module reacts to OCPP configuration change requests. config: - user_config_path: - description: Path to the user configuration file + user_config_file_name: + description: Name of the user configuration file type: string - mapping_file_path: - description: Path to the mapping file + mapping_file_name: + description: Name of the mapping file (in the `mappings` directory). type: string default: "example-config-mapping.yaml" provides: diff --git a/modules/OCPPConfiguration/mappings/CMakeLists.txt b/modules/OCPPConfiguration/mappings/CMakeLists.txt new file mode 100644 index 0000000000..aea50341d0 --- /dev/null +++ b/modules/OCPPConfiguration/mappings/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copy all mapping files (yaml) to the install directory +# and add a compile definition to it so the path can be used in code. + +set("${PROJECT_NAME}_${MODULE_NAME}_MAPPING_DIR" "${CMAKE_INSTALL_PREFIX}/etc/everest/ocpp-configuration/mappings") + +install(DIRECTORY "." + DESTINATION "${${PROJECT_NAME}_${MODULE_NAME}_MAPPING_DIR}" + FILES_MATCHING PATTERN "*.yaml") + +target_compile_definitions(${MODULE_NAME} PRIVATE MAPPING_INSTALL_DIRECTORY="${${PROJECT_NAME}_${MODULE_NAME}_MAPPING_DIR}") diff --git a/modules/OCPPConfiguration/example-config-mapping.yaml b/modules/OCPPConfiguration/mappings/example-config-mapping.yaml similarity index 100% rename from modules/OCPPConfiguration/example-config-mapping.yaml rename to modules/OCPPConfiguration/mappings/example-config-mapping.yaml