Skip to content

Commit

Permalink
changes how paths are handled and does some small refactorings
Browse files Browse the repository at this point in the history
Signed-off-by: MarzellT <[email protected]>
  • Loading branch information
MarzellT committed Aug 27, 2024
1 parent 96e7325 commit cae1e6b
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 34 deletions.
2 changes: 1 addition & 1 deletion config/config-sil-ocpp-custom-extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions modules/OCPPConfiguration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions modules/OCPPConfiguration/OCPPConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventHandler>(mapping_file_path);
Expand All @@ -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
4 changes: 2 additions & 2 deletions modules/OCPPConfiguration/OCPPConfiguration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
namespace module {

struct Conf {
std::string user_config_path;
std::string mapping_file_path;
std::string user_config_file_name;

Check notice on line 27 in modules/OCPPConfiguration/OCPPConfiguration.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/OCPPConfiguration/OCPPConfiguration.hpp#L27

struct member 'Conf::user_config_file_name' is never used.
std::string mapping_file_name;

Check notice on line 28 in modules/OCPPConfiguration/OCPPConfiguration.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/OCPPConfiguration/OCPPConfiguration.hpp#L28

struct member 'Conf::mapping_file_name' is never used.
};

class OCPPConfiguration : public Everest::ModuleBase {
Expand Down
13 changes: 9 additions & 4 deletions modules/OCPPConfiguration/doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
**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.
18 changes: 11 additions & 7 deletions modules/OCPPConfiguration/main/event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
#include "event_handler.hpp"
#include "util.hpp"
#include <everest/logging.hpp>

#include <unordered_set>

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);
Expand All @@ -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();
}
}

Expand All @@ -42,9 +46,9 @@ std::vector<types::ocpp::ComponentVariable> 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);
Expand Down
13 changes: 7 additions & 6 deletions modules/OCPPConfiguration/main/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -45,7 +45,8 @@ class EventHandler {
find_mapping_by_component_variable(const types::ocpp::ComponentVariable& component_variable) const;

OcppToEverestConfigMapping config_mapping;

Check notice on line 47 in modules/OCPPConfiguration/main/event_handler.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/OCPPConfiguration/main/event_handler.hpp#L47

class member 'EventHandler::config_mapping' is never used.
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);
};

Expand Down
1 change: 1 addition & 0 deletions modules/OCPPConfiguration/main/everest_config_mapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "generated/types/ocpp.hpp"

#include <string>

// Required to use for std::unordered_map with types::ocpp::ComponentVariable as key
Expand Down
8 changes: 4 additions & 4 deletions modules/OCPPConfiguration/main/mapping_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "mapping_reader.hpp"
#include "everest/logging.hpp"
#include "util.hpp"

#include <iostream>

namespace module {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions modules/OCPPConfiguration/main/mapping_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "everest_config_mapping.hpp"
#include "generated/types/ocpp.hpp"

#include <c4/yml/std/map.hpp>
#include <filesystem>
#include <ryml.hpp>
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions modules/OCPPConfiguration/main/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Copyright Pionix GmbH and Contributors to EVerest

#include "util.hpp"
#include "everest/logging.hpp"
#include "generated/types/ocpp.hpp"

#include <filesystem>
#include <fstream>
#include <sstream>
Expand Down
8 changes: 4 additions & 4 deletions modules/OCPPConfiguration/manifest.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
10 changes: 10 additions & 0 deletions modules/OCPPConfiguration/mappings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")

0 comments on commit cae1e6b

Please sign in to comment.