Skip to content
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
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/config-sil-ocpp-custom-extension.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
active_modules:
ocpp-configuration-instance:
module: OCPPConfiguration
connections:
ocpp_module:
- module_id: ocpp
implementation_id: ocpp_generic
config_module:
user_config_file_name: "config-sil-ocpp-custom-extension.yaml"
iso15118_charger:
module: EvseV2G
config_module:
Expand Down
1 change: 1 addition & 0 deletions modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ev_add_module(JsTibber)
ev_add_module(LemDCBM400600)
ev_add_module(OCPP)
ev_add_module(OCPP201)
ev_add_module(OCPPConfiguration)
ev_add_module(PacketSniffer)
ev_add_module(PersistentStore)
ev_add_module(PN532TokenProvider)
Expand Down
46 changes: 46 additions & 0 deletions modules/OCPPConfiguration/CMakeLists.txt
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
32 changes: 32 additions & 0 deletions modules/OCPPConfiguration/OCPPConfiguration.cpp
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);
Copy link
Contributor

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:

  • Check if mapping file exists and is valid and warn if it doesn't
  • Check if user config exists and warn if it doesn't

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.

Copy link
Member Author

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?

}

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
69 changes: 69 additions & 0 deletions modules/OCPPConfiguration/OCPPConfiguration.hpp
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
35 changes: 35 additions & 0 deletions modules/OCPPConfiguration/doc.rst
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.
16 changes: 16 additions & 0 deletions modules/OCPPConfiguration/main/emptyImpl.cpp
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
60 changes: 60 additions & 0 deletions modules/OCPPConfiguration/main/emptyImpl.hpp
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
81 changes: 81 additions & 0 deletions modules/OCPPConfiguration/main/event_handler.cpp
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
Loading
Loading