-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Ocpp Configuration Module #799
Open
MarzellT
wants to merge
15
commits into
main
Choose a base branch
from
tm-adding-ocpp-configuration-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3384c3a
wip: first commit
MarzellT ea322e0
adds mapping reading and creation of the user config
MarzellT c15d8f1
implements configuration being written to file and does some refactoring
MarzellT a614c27
fixes writing config failing if file doesn't exist and adds component…
MarzellT b0f3faa
makes the module be able to handle OCPP 2.0.1
MarzellT 71b2c71
refactors some function names to obey naming convenctions
MarzellT a73e349
refactors mapping file
MarzellT 68f5dce
reverts accidental changes
MarzellT a9f5e54
first iteration of review changes
MarzellT 92a6e25
reverts ocpp config back to original state but changes custom extensi…
MarzellT cf629d2
adds documentation for classes and public functions and does some sma…
MarzellT 6d9795b
changes how paths are handled and does some small refactorings
MarzellT 10db827
adds schema for validating the mapping files
MarzellT 47cdbbe
Merge branch 'main' into tm-adding-ocpp-configuration-module
Pietfried 00b116a
Merge branch 'main' into tm-adding-ocpp-configuration-module
Pietfried File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# | ||
# AUTO GENERATED - MARKED REGIONS WILL BE KEPT | ||
# template version 3 | ||
# | ||
|
||
# module setup: | ||
# - ${MODULE_NAME}: module name | ||
ev_setup_cpp_module() | ||
|
||
# ev@bcc62523-e22b-41d7-ba2f-825b493a3c97:v1 | ||
# insert your custom targets and additional config variables here | ||
|
||
target_link_libraries(${MODULE_NAME} | ||
PRIVATE | ||
ryml::ryml | ||
) | ||
|
||
target_sources(${MODULE_NAME} | ||
PRIVATE | ||
"main/emptyImpl.cpp" | ||
"main/mapping_reader.cpp" | ||
"main/util.cpp" | ||
"main/event_handler.cpp" | ||
"main/everest_config_mapping.cpp" | ||
) | ||
# ev@bcc62523-e22b-41d7-ba2f-825b493a3c97:v1 | ||
|
||
# ev@c55432ab-152c-45a9-9d2e-7281d50c69c3:v1 | ||
# 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) | ||
add_subdirectory(mapping_schemas) | ||
# ev@c55432ab-152c-45a9-9d2e-7281d50c69c3:v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
#include "OCPPConfiguration.hpp" | ||
#include "main/event_handler.hpp" | ||
#include <everest/logging.hpp> | ||
|
||
namespace module { | ||
|
||
void OCPPConfiguration::init() { | ||
invoke_init(*p_main); | ||
} | ||
|
||
void OCPPConfiguration::ready() { | ||
invoke_ready(*p_main); | ||
|
||
try { | ||
event_handler = std::make_unique<EventHandler>(config.mapping_file_name, config.schema_file_name); | ||
} catch (const std::runtime_error& e) { | ||
EVLOG_warning << "Failed to create event handler: " << e.what() << "No events will be handled!"; | ||
return; | ||
} catch (const std::exception& e) { | ||
EVLOG_warning << "Failed to create event handler: " << e.what() << "No events will be handled!"; | ||
return; | ||
} | ||
|
||
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_file_name); }); | ||
} | ||
|
||
} // namespace module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
#ifndef OCPPCONFIGURATION_HPP | ||
#define OCPPCONFIGURATION_HPP | ||
|
||
// | ||
// AUTO GENERATED - MARKED REGIONS WILL BE KEPT | ||
// template version 2 | ||
// | ||
|
||
#include "ld-ev.hpp" | ||
|
||
// headers for provided interface implementations | ||
#include <generated/interfaces/empty/Implementation.hpp> | ||
|
||
// headers for required interface implementations | ||
#include <generated/interfaces/ocpp/Interface.hpp> | ||
|
||
// ev@4bf81b14-a215-475c-a1d3-0a484ae48918:v1 | ||
// insert your custom include headers here | ||
#include "main/event_handler.hpp" | ||
// ev@4bf81b14-a215-475c-a1d3-0a484ae48918:v1 | ||
|
||
namespace module { | ||
|
||
struct Conf { | ||
std::string user_config_file_name; | ||
std::string mapping_file_name; | ||
std::string schema_file_name; | ||
}; | ||
|
||
class OCPPConfiguration : public Everest::ModuleBase { | ||
public: | ||
OCPPConfiguration() = delete; | ||
OCPPConfiguration(const ModuleInfo& info, std::unique_ptr<emptyImplBase> p_main, | ||
std::unique_ptr<ocppIntf> r_ocpp_module, Conf& config) : | ||
ModuleBase(info), p_main(std::move(p_main)), r_ocpp_module(std::move(r_ocpp_module)), config(config){}; | ||
|
||
const std::unique_ptr<emptyImplBase> p_main; | ||
const std::unique_ptr<ocppIntf> r_ocpp_module; | ||
const Conf& config; | ||
|
||
// ev@1fce4c5e-0ab8-41bb-90f7-14277703d2ac:v1 | ||
// insert your public definitions here | ||
// ev@1fce4c5e-0ab8-41bb-90f7-14277703d2ac:v1 | ||
|
||
protected: | ||
// ev@4714b2ab-a24f-4b95-ab81-36439e1478de:v1 | ||
// insert your protected definitions here | ||
// ev@4714b2ab-a24f-4b95-ab81-36439e1478de:v1 | ||
|
||
private: | ||
friend class LdEverest; | ||
void init(); | ||
void ready(); | ||
|
||
// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1 | ||
// insert your private definitions here | ||
std::unique_ptr<EventHandler> event_handler; | ||
// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1 | ||
}; | ||
|
||
// ev@087e516b-124c-48df-94fb-109508c7cda9:v1 | ||
// insert other definitions here | ||
// ev@087e516b-124c-48df-94fb-109508c7cda9:v1 | ||
|
||
} // namespace module | ||
|
||
#endif // OCPPCONFIGURATION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
******************************************* | ||
OCPPConfiguration | ||
******************************************* | ||
|
||
This module reacts to OCPP configuration change requests. | ||
It does so by writing the requested changes to a user-config file. | ||
This requires a mapping between OCPP variable names and the corresponding configuration file entries. | ||
The configuration file is specified in the manifest file of the module (see :ref:`_mapping_file_path`). | ||
|
||
Configuration Options | ||
---------------------- | ||
|
||
The following configuration options are available: | ||
|
||
.. _user_config_name: | ||
|
||
**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_name: | ||
|
||
**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. | ||
|
||
.. _schema_file_name: | ||
|
||
**schema_file_name** | ||
- **Description**: Name of the schema file. | ||
This file has to be in the `mapping_schemas` directory of this module which will be installed. | ||
This should probably be set to the default. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
|
||
#include "emptyImpl.hpp" | ||
|
||
namespace module { | ||
namespace main { | ||
|
||
void emptyImpl::init() { | ||
} | ||
|
||
void emptyImpl::ready() { | ||
} | ||
|
||
} // namespace main | ||
} // namespace module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
#ifndef MAIN_EMPTY_IMPL_HPP | ||
#define MAIN_EMPTY_IMPL_HPP | ||
|
||
// | ||
// AUTO GENERATED - MARKED REGIONS WILL BE KEPT | ||
// template version 3 | ||
// | ||
|
||
#include <generated/interfaces/empty/Implementation.hpp> | ||
|
||
#include "../OCPPConfiguration.hpp" | ||
|
||
// ev@75ac1216-19eb-4182-a85c-820f1fc2c091:v1 | ||
// insert your custom include headers here | ||
// ev@75ac1216-19eb-4182-a85c-820f1fc2c091:v1 | ||
|
||
namespace module { | ||
namespace main { | ||
|
||
struct Conf {}; | ||
|
||
class emptyImpl : public emptyImplBase { | ||
public: | ||
emptyImpl() = delete; | ||
emptyImpl(Everest::ModuleAdapter* ev, const Everest::PtrContainer<OCPPConfiguration>& mod, Conf& config) : | ||
emptyImplBase(ev, "main"), mod(mod), config(config){}; | ||
|
||
// ev@8ea32d28-373f-4c90-ae5e-b4fcc74e2a61:v1 | ||
// insert your public definitions here | ||
// ev@8ea32d28-373f-4c90-ae5e-b4fcc74e2a61:v1 | ||
|
||
protected: | ||
// no commands defined for this interface | ||
|
||
// ev@d2d1847a-7b88-41dd-ad07-92785f06f5c4:v1 | ||
// insert your protected definitions here | ||
// ev@d2d1847a-7b88-41dd-ad07-92785f06f5c4:v1 | ||
|
||
private: | ||
const Everest::PtrContainer<OCPPConfiguration>& mod; | ||
const Conf& config; | ||
|
||
virtual void init() override; | ||
virtual void ready() override; | ||
|
||
// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1 | ||
// insert your private definitions here | ||
// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1 | ||
}; | ||
|
||
// ev@3d7da0ad-02c2-493d-9920-0bbbd56b9876:v1 | ||
// insert other definitions here | ||
// ev@3d7da0ad-02c2-493d-9920-0bbbd56b9876:v1 | ||
|
||
} // namespace main | ||
} // namespace module | ||
|
||
#endif // MAIN_EMPTY_IMPL_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
|
||
#include "event_handler.hpp" | ||
#include "util.hpp" | ||
#include <everest/logging.hpp> | ||
|
||
#include <unordered_set> | ||
|
||
namespace module { | ||
|
||
EventHandler::EventHandler(const std::filesystem::path& config_mapping_file_name, | ||
const std::filesystem::path& mapping_schema_file_name) : | ||
config_mapping(mapping_reader::read_mapping(config_mapping_file_name, mapping_schema_file_name)) { | ||
} | ||
|
||
void EventHandler::try_handle_event(const types::ocpp::EventData& event_data, | ||
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); | ||
|
||
if (!everest_module_mapping_opt.has_value()) { | ||
return; | ||
} | ||
|
||
const auto& everest_module_mapping = everest_module_mapping_opt.value(); | ||
|
||
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 ocpp configuration request: " << e.what(); | ||
} | ||
} | ||
|
||
std::vector<types::ocpp::ComponentVariable> EventHandler::get_monitor_variables() const noexcept { | ||
auto monitor_variables = std::unordered_set<types::ocpp::ComponentVariable>{}; | ||
|
||
for (const auto& [component_variable, _] : config_mapping) { | ||
monitor_variables.insert(component_variable); | ||
} | ||
|
||
return {monitor_variables.begin(), monitor_variables.end()}; | ||
} | ||
|
||
void EventHandler::write_event_to_config(const types::ocpp::EventData& event_data, | ||
const std::filesystem::path& user_config_file_name, | ||
const EverestConfigMapping& everest_module_mapping) { | ||
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); | ||
} | ||
|
||
const std::optional<EverestConfigMapping> EventHandler::get_optional_mapping_by_component_variable( | ||
const types::ocpp::ComponentVariable& component_variable) const noexcept { | ||
try { | ||
const auto& everest_module_mapping = find_mapping_by_component_variable(component_variable); | ||
return everest_module_mapping; | ||
} catch (const std::runtime_error& e) { | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
const EverestConfigMapping | ||
EventHandler::find_mapping_by_component_variable(const types::ocpp::ComponentVariable& component_variable) const { | ||
const auto& mapping = config_mapping.find(component_variable); | ||
|
||
// check if event is in event_map | ||
if (mapping == config_mapping.end()) { | ||
throw std::runtime_error("Component Variable not found in config mapping:\n Component:\t" + | ||
std::string(component_variable.component.name) + "\n Variable:\t" + | ||
std::string(component_variable.variable.name)); | ||
} | ||
|
||
return mapping->second; | ||
} | ||
} // namespace module |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should verify the configuration parameters in the init:
In case the configuration could not be verified this module is not operational, but it should not crash, since it doesn't provide any interface and shold not cause a crash of everest ever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first case is handled implicitly now but I think the second case shouldn't generate a warning and just silently create the file. Do you disagree?