Skip to content

Commit

Permalink
implements configuration being written to file and does some refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: MarzellT <[email protected]>
  • Loading branch information
MarzellT committed Aug 12, 2024
1 parent 03ca814 commit 4cc7321
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 120 deletions.
1 change: 0 additions & 1 deletion modules/OCPPConfiguration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ target_link_libraries(${MODULE_NAME}
target_sources(${MODULE_NAME}
PRIVATE
"example_module/example_userImpl.cpp"
"main/config_writer.cpp"
"main/mapping_reader.cpp"
"main/util.cpp"
"main/event_handler.cpp"
Expand Down
10 changes: 8 additions & 2 deletions modules/OCPPConfiguration/OCPPConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ void OCPPConfiguration::init() {
void OCPPConfiguration::ready() {
invoke_ready(*p_example_module);

event_handler = std::make_unique<EventHandler>(config.mapping_file_path, config.user_config_path);
// auto attached = false;
// while (!attached) {
// };

const auto mapping_file_path = std::filesystem::path{config.mapping_file_path};
event_handler = std::make_unique<EventHandler>(mapping_file_path);

r_ocpp_module->call_monitor_variables(parseConfigMonitorVariables());

r_ocpp_module->subscribe_event_data([this](const auto& event_data) { event_handler->handleEvent(event_data); });
r_ocpp_module->subscribe_event_data(
[&](const auto& event_data) { event_handler->handleEvent(event_data, config.user_config_path); });
}

std::vector<types::ocpp::ComponentVariable> OCPPConfiguration::parseConfigMonitorVariables() {
Expand Down
64 changes: 0 additions & 64 deletions modules/OCPPConfiguration/main/config_writer.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions modules/OCPPConfiguration/main/config_writer.hpp

This file was deleted.

18 changes: 10 additions & 8 deletions modules/OCPPConfiguration/main/event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
// Copyright Pionix GmbH and Contributors to EVerest

#include "event_handler.hpp"
#include "util.hpp"
#include <everest/logging.hpp>

namespace module {

EventHandler::EventHandler(const std::string& config_mapping_path, const std::string& user_config_path) :
event_map(MappingReader::readMapping(config_mapping_path)), config_writer(user_config_path) {
EVLOG_info << "EventHandler initialized";
EventHandler::EventHandler(const std::filesystem::path& config_mapping_path) :
event_map(MappingReader::readMapping(config_mapping_path)) {
}

void EventHandler::handleEvent(const types::ocpp::EventData& event_data) {
void EventHandler::handleEvent(const types::ocpp::EventData& event_data, const std::string& user_config_path_string) {

// lookup event in event_map
const auto& event_name = event_data.component_variable.variable.name;

Expand All @@ -22,10 +23,11 @@ void EventHandler::handleEvent(const types::ocpp::EventData& event_data) {

const auto& everest_module_mapping = everest_module_mapping_opt.value();

// write event to config_pair
config_writer.write_to_config(everest_module_mapping.config_key, everest_module_mapping.module_name,
<#initializer #>, event_data.actual_value);
config_writer.save_config();
// write event to config
const auto user_config_path = std::filesystem::path{user_config_path_string};
auto tree = Util::load_existing_user_config(user_config_path);
tree = Util::write_value_to_tree(everest_module_mapping, event_data.actual_value, tree);
Util::save_tree_to_yaml_file(tree, user_config_path);
}

const std::optional<EverestModuleMapping>
Expand Down
6 changes: 2 additions & 4 deletions modules/OCPPConfiguration/main/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@

#pragma once

#include "config_writer.hpp"
#include "generated/types/ocpp.hpp"
#include "mapping_reader.hpp"

namespace module {

class EventHandler {
public:
EventHandler(const std::string& config_mapping_path, const std::string& user_config_path);
void handleEvent(const types::ocpp::EventData& event_data);
EventHandler(const std::filesystem::path& config_mapping_path);

Check notice on line 13 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#L13

Class 'EventHandler' has a constructor with 1 argument that is not explicit.
void handleEvent(const types::ocpp::EventData& event_data, const std::string& user_config_path_string);

private:
const std::optional<EverestModuleMapping> find_event_in_map_or_log_error(const std::string& event_name) const;
const EverestModuleMapping find_event_in_map(const std::string& event_name) const;

OcppToEverestModuleMapping event_map;

Check notice on line 20 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#L20

class member 'EventHandler::event_map' is never used.
ConfigWriter config_writer;
};

} // namespace module
6 changes: 4 additions & 2 deletions modules/OCPPConfiguration/main/mapping_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ EverestModuleMapping MappingReader::parseMappingNode(const ryml::NodeRef& node)
return {config_key, module_name, module_config_key};
}

OcppToEverestModuleMapping MappingReader::readMapping(const std::string& file_path) {
const auto tree = Util::load_yaml_file(file_path);
OcppToEverestModuleMapping MappingReader::readMapping(const std::filesystem::path& file_path) {
const auto hacked_file_path =
std::filesystem::path{"modules"} / "OCPPConfiguration" / file_path; // TODO: this is very hacky
const auto tree = Util::load_existing_user_config(hacked_file_path);

const auto root = tree.rootref();

Expand Down
3 changes: 2 additions & 1 deletion modules/OCPPConfiguration/main/mapping_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <c4/yml/std/map.hpp>
#include <filesystem>
#include <ryml.hpp>
#include <string>
#include <unordered_map>
Expand All @@ -21,7 +22,7 @@ using OcppToEverestModuleMapping = std::unordered_map<std::string, EverestModule

class MappingReader {
public:
static OcppToEverestModuleMapping readMapping(const std::string& file_path);
static OcppToEverestModuleMapping readMapping(const std::filesystem::path& file_path_string);

private:
static EverestModuleMapping parseMappingNode(const ryml::NodeRef& node);
Expand Down
51 changes: 45 additions & 6 deletions modules/OCPPConfiguration/main/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ namespace module {

namespace Util {

ryml::Tree load_yaml_file(const std::string& file_path_string) {
auto file_path =
std::filesystem::path{"modules"} / "OCPPConfiguration" / file_path_string; // TODO: this is very hacky
ryml::Tree load_existing_user_config(const std::filesystem::path& user_config_file_path) {
try {
// file exists
return load_yaml_file(user_config_file_path);
}
// file doesn't exist
catch (const std::runtime_error& e) {
return ryml::Tree();
}
}

ryml::Tree load_yaml_file(const std::filesystem::path& file_path) {
auto file = std::ifstream{file_path};
if (!file.is_open()) {
throw std::runtime_error("Unable to open file: " + file_path.string());
Expand All @@ -24,13 +33,43 @@ ryml::Tree load_yaml_file(const std::string& file_path_string) {
return ryml::parse_in_arena(ryml::to_csubstr(data));
}

void save_yaml_file(const std::string& file_path, ryml::Tree& tree) {
std::ofstream file(file_path);
void save_tree_to_yaml_file(const ryml::Tree& tree, const std::filesystem::path& file_path_string) {
const auto file_path = std::filesystem::path{file_path_string};
const auto absolute_file_path = std::filesystem::absolute(file_path);
std::ofstream file{file_path};
if (!file.is_open()) {
throw std::runtime_error("Unable to open file: " + file_path);
throw std::runtime_error("Unable to open file: " + absolute_file_path.string());
}
file << tree;
}
ryml::Tree write_value_to_tree(const EverestModuleMapping& module_mapping, const std::string& config_value,
const ryml::Tree& config_tree) {
auto root = config_tree.rootref();
if (!root.is_map()) {
root |= ryml::MAP; // mark root as map
}

auto active_modules = root["active_modules"];
active_modules |= ryml::MAP; // mark active_modules as map

const auto config_key_csubstr = ryml::to_csubstr(module_mapping.config_key.c_str());
auto config_key_node = active_modules[config_key_csubstr];
config_key_node |= ryml::MAP; // mark config_key as map

const auto module_name_csubstr = ryml::to_csubstr(module_mapping.module_name.c_str());
auto module_node = config_key_node[module_name_csubstr];
module_node |= ryml::MAP; // mark module as map

auto config_module_node = module_node["config_module"];
config_module_node |= ryml::MAP;

const auto module_config_key_csubstr = ryml::to_csubstr(module_mapping.module_config_key.c_str());
auto module_config_key_node = config_module_node[module_config_key_csubstr];
const auto config_value_csubstr = ryml::to_csubstr(config_value.c_str());
module_config_key_node << config_value_csubstr;

return config_tree;
}

} // namespace Util
} // namespace module
13 changes: 11 additions & 2 deletions modules/OCPPConfiguration/main/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@

#pragma once

#include "mapping_reader.hpp"
#include "utils/yaml_loader.hpp"
#include <c4/yml/std/map.hpp>
#include <filesystem>
#include <ryml.hpp>
#include <sstream>

namespace module {
namespace Util {

ryml::Tree load_yaml_file(const std::string& file_path_string);
void save_yaml_file(const std::string& file_path, ryml::Tree& tree);
ryml::Tree load_existing_user_config(const std::filesystem::path& user_config_file_path);

ryml::Tree load_yaml_file(const std::filesystem::path& file_path);
void save_tree_to_yaml_file(const ryml::Tree& tree, const std::filesystem::path& file_path);

ryml::Tree write_value_to_tree(const EverestModuleMapping& module_mapping, const std::string& config_value,
const ryml::Tree& config_tree);

}; // namespace Util
} // namespace module
2 changes: 1 addition & 1 deletion modules/OCPPConfiguration/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ config:
user_config_path:
description: Path to the user configuration file
type: string
default: "../../config/user-config/ocpp-remote-config.yaml"
default: "config/user-config/ocpp-remote-config.yaml"
mapping_file_path:
description: Path to the mapping file
type: string
Expand Down
2 changes: 1 addition & 1 deletion modules/Setup/WiFiSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ bool WpaCliSetup::save_config(const std::string& interface) {
return false;
}

auto output = run_application(wpa_cli, {"-i", interface, "save_config"});
auto output = run_application(wpa_cli, {"-i", interface, "save_config_tree_to_file"});
return output.exit_code == 0;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/Setup/tests/RunApplicationStub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RunApplication::RunApplication() :
{"disable_network", {{}, {{"OK"}}, 0}},
{"select_network", {{}, {{"OK"}}, 0}},
{"remove_network", {{}, {{"OK"}}, 0}},
{"save_config", {{}, {{"OK"}}, 0}},
{"save_config_tree_to_file", {{}, {{"OK"}}, 0}},
// scan_wifi uses scan and scan_results
{"scan", {{}, {{"OK"}}, 0}},
{"scan_results",
Expand Down

0 comments on commit 4cc7321

Please sign in to comment.