Skip to content

Commit

Permalink
- removed OcppMainPath from OCPP config options
Browse files Browse the repository at this point in the history
- Now using module info everest prefix to access filesystem paths in System and OCPP module
- replaced boost::filesystem by std::filesystem in OCPP module
- resolve relative OCPP config (and user config) paths using ocpp_share_path

Signed-off-by: pietfried <[email protected]>
Signed-off-by: Kai-Uwe Hermann <[email protected]>
  • Loading branch information
Pietfried committed Dec 21, 2022
1 parent f73e7d4 commit 0008408
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 43 deletions.
4 changes: 2 additions & 2 deletions config/config-sil-ocpp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ active_modules:
ocpp:
module: OCPP
config_module:
OcppMainPath: share/everest/ocpp
ChargePointConfigPath: ../../../libocpp/aux/config-docker.json
ChargePointConfigPath: config-docker.json
connections:
evse_manager:
- module_id: evse_manager_1
Expand Down Expand Up @@ -177,4 +176,5 @@ active_modules:
implementation_id: evse
system:
module: System

x-module-layout: {}
4 changes: 2 additions & 2 deletions dependencies.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
everest-framework:
git: https://github.com/EVerest/everest-framework.git
git_tag: v0.1.0
git_tag: v0.2.0
sigslot:
git: https://github.com/palacaze/sigslot
git_tag: v1.2.0
Expand Down Expand Up @@ -32,4 +32,4 @@ RISE-V2G:
# OCPP
libocpp:
git: https://github.com/EVerest/libocpp.git
git_tag: v0.3.0
git_tag: v0.4.0
65 changes: 44 additions & 21 deletions modules/OCPP/OCPP.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2022 Pionix GmbH and Contributors to EVerest
#include "OCPP.hpp"
#include <fmt/core.h>
#include <fstream>

#include <boost/filesystem.hpp>
#include <boost/process.hpp>

namespace module {

const std::string EVEREST_OCPP_SHARE_PATH = "share/everest/ocpp";
const std::string INIT_SQL = "init.sql";

namespace fs = std::filesystem;

static ocpp1_6::ChargePointErrorCode get_ocpp_error_code(const std::string& evse_error) {
if (evse_error == "Car") {
return ocpp1_6::ChargePointErrorCode::OtherError;
Expand All @@ -32,14 +37,15 @@ static ocpp1_6::ChargePointErrorCode get_ocpp_error_code(const std::string& evse
}
}

void create_empty_user_config(const boost::filesystem::path& user_config_path) {
if (boost::filesystem::exists(user_config_path.parent_path())) {
void create_empty_user_config(const fs::path& user_config_path) {
if (fs::exists(user_config_path.parent_path())) {
std::ofstream fs(user_config_path.c_str());
auto user_config = json::object();
fs << user_config << std::endl;
fs.close();
} else {
EVLOG_AND_THROW(std::runtime_error("Provided UserConfigPath is invalid"));
EVLOG_AND_THROW(
std::runtime_error(fmt::format("Provided UserConfigPath is invalid: {}", user_config_path.string())));
}
}

Expand All @@ -58,42 +64,60 @@ void OCPP::init() {
invoke_init(*p_auth_validator);
invoke_init(*p_auth_provider);

boost::filesystem::path config_path = boost::filesystem::path(this->config.ChargePointConfigPath);
this->ocpp_share_path = fs::path(this->info.everest_prefix) / EVEREST_OCPP_SHARE_PATH;

auto configured_config_path = fs::path(this->config.ChargePointConfigPath);

boost::filesystem::path user_config_path = boost::filesystem::path(this->config.UserConfigPath);
// try to find the config file if it has been provided as a relative path
if (!fs::exists(configured_config_path) && configured_config_path.is_relative()) {
configured_config_path = this->ocpp_share_path / configured_config_path;
}
if (!fs::exists(configured_config_path)) {
EVLOG_AND_THROW(Everest::EverestConfigError(
fmt::format("OCPP config file is not available at given path: {} which was resolved to: {}",
this->config.ChargePointConfigPath, configured_config_path.string())));
}
const auto config_path = configured_config_path;
EVLOG_info << "OCPP config: " << config_path.string();

auto configured_user_config_path = fs::path(this->config.UserConfigPath);
// try to find the user config file if it has been provided as a relative path
if (!fs::exists(configured_user_config_path) && configured_user_config_path.is_relative()) {
configured_user_config_path = this->ocpp_share_path / configured_user_config_path;
}
const auto user_config_path = configured_user_config_path;
EVLOG_info << "OCPP user config: " << user_config_path.string();

std::ifstream ifs(config_path.c_str());
std::string config_file((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
json json_config = json::parse(config_file);
auto json_config = json::parse(config_file);
json_config.at("Core").at("NumberOfConnectors") = this->r_evse_manager.size();

if (boost::filesystem::exists(user_config_path)) {
if (fs::exists(user_config_path)) {
std::ifstream ifs(user_config_path.c_str());
std::string user_config_file((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));

try {
auto user_config = json::parse(user_config_file);
const auto user_config = json::parse(user_config_file);
EVLOG_info << "Augmenting chargepoint config with user_config entries";
json_config.merge_patch(user_config);
} catch (const json::parse_error& e) {
// rewrite empty user config if it is not parsable
EVLOG_error << "Error while parsing user config file.";
throw;
EVLOG_AND_THROW(e);
}
} else {
EVLOG_debug << "No user-config provided. Creating user config file";
create_empty_user_config(user_config_path);
}

std::shared_ptr<ocpp1_6::ChargePointConfiguration> configuration =
std::make_shared<ocpp1_6::ChargePointConfiguration>(json_config, this->config.OcppMainPath,
this->config.UserConfigPath);
const auto configuration = std::make_shared<ocpp1_6::ChargePointConfiguration>(
json_config, this->ocpp_share_path.string(), user_config_path.string());

const boost::filesystem::path sql_init_path = boost::filesystem::path(this->config.OcppMainPath) / "init.sql";
if (!std::filesystem::exists(this->config.MessageLogPath)) {
const auto sql_init_path = this->ocpp_share_path / INIT_SQL;
if (!fs::exists(this->config.MessageLogPath)) {
try {
std::filesystem::create_directory(this->config.MessageLogPath);
} catch (std::filesystem::filesystem_error const& e) {
fs::create_directory(this->config.MessageLogPath);
} catch (const fs::filesystem_error& e) {
EVLOG_AND_THROW(e);
}
}
Expand Down Expand Up @@ -314,9 +338,8 @@ void OCPP::init() {
this->r_system->call_reset(reset_type);
});

this->charge_point->register_connection_state_changed_callback([this](bool is_connected) {
this->p_main->publish_is_connected(is_connected);
});
this->charge_point->register_connection_state_changed_callback(
[this](bool is_connected) { this->p_main->publish_is_connected(is_connected); });

int32_t connector = 1;
for (auto& evse : this->r_evse_manager) {
Expand Down
2 changes: 2 additions & 0 deletions modules/OCPP/OCPP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

// ev@4bf81b14-a215-475c-a1d3-0a484ae48918:v1
// insert your custom include headers here
#include <filesystem>
#include <chrono>
#include <date/date.h>
#include <date/tz.h>
Expand Down Expand Up @@ -98,6 +99,7 @@ class OCPP : public Everest::ModuleBase {

// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1
// insert your private definitions here
std::filesystem::path ocpp_share_path;
void publish_charging_schedules();
std::thread upload_diagnostics_thread;
std::thread upload_logs_thread;
Expand Down
8 changes: 2 additions & 6 deletions modules/OCPP/manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
description: A OCPP charge point / charging station module, currently targeting OCPP-J 1.6
config:
OcppMainPath:
description: Main path of OCPP module
type: string
default: share/everest/ocpp
ChargePointConfigPath:
description: Path to the configuration file
type: string
default: charge_point_config.json
default: ocpp-config.json
UserConfigPath:
description: Path to the file of the OCPP user config
type: string
default: share/everest/ocpp/user_config.json
default: user_config.json
DatabasePath:
description: Path to the persistent SQLite database folder
type: string
Expand Down
32 changes: 20 additions & 12 deletions modules/System/main/systemImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
namespace module {
namespace main {

const std::string CONSTANTS = "constants.env";
const std::string DIAGNOSTICS_UPLOADER = "diagnostics_uploader.sh";
const std::string FIRMWARE_UPDATER = "firmware_updater.sh";
const std::string SIGNED_FIRMWARE_DOWNLOADER = "signed_firmware_downloader.sh";
const std::string SIGNED_FIRMWARE_INSTALLER = "signed_firmware_installer.sh";
const std::string EVEREST_LIBEXEC_MODULES_PATH = "libexec/everest/modules";

namespace fs = std::filesystem;

// FIXME (aw): this function needs to be refactored into some kind of utility library
Expand All @@ -38,6 +45,7 @@ fs::path create_temp_file(const fs::path& dir, const std::string& prefix) {
}

void systemImpl::init() {
this->scripts_path = fs::path(mod->info.everest_prefix) / EVEREST_LIBEXEC_MODULES_PATH / mod->info.name;
this->log_upload_running = false;
this->firmware_download_running = false;
this->firmware_installation_running = false;
Expand All @@ -55,10 +63,10 @@ void systemImpl::standard_firmware_update(const types::system::FirmwareUpdateReq
const auto date_time = Everest::Date::to_rfc3339(date::utc_clock::now());

const auto firmware_file_path = create_temp_file(fs::temp_directory_path(), "firmware-" + date_time);
const auto constants = fs::path("libexec/everest/modules/System/constants.env");
const auto constants = this->scripts_path / CONSTANTS;

this->update_firmware_thread = std::thread([this, firmware_update_request, firmware_file_path, constants]() {
const auto firmware_updater = "libexec/everest/modules/System/firmware_updater.sh";
const auto firmware_updater = this->scripts_path / FIRMWARE_UPDATER;

const std::vector<std::string> args = {constants.string(), firmware_update_request.location,
firmware_file_path.string()};
Expand All @@ -75,7 +83,7 @@ void systemImpl::standard_firmware_update(const types::system::FirmwareUpdateReq
while (firmware_status.firmware_update_status == types::system::FirmwareUpdateStatusEnum::DownloadFailed &&
retries <= total_retries) {
boost::process::ipstream stream;
boost::process::child cmd(firmware_updater, boost::process::args(args), boost::process::std_out > stream);
boost::process::child cmd(firmware_updater.string(), boost::process::args(args), boost::process::std_out > stream);
std::string temp;
retries += 1;
while (std::getline(stream, temp)) {
Expand Down Expand Up @@ -174,8 +182,8 @@ void systemImpl::download_signed_firmware(const types::system::FirmwareUpdateReq
const auto date_time = Everest::Date::to_rfc3339(date::utc_clock::now());
const auto firmware_file_path = create_temp_file(fs::temp_directory_path(), "signed_firmware-" + date_time);

const auto firmware_downloader = "libexec/everest/modules/System/signed_firmware_downloader.sh";
const auto constants = fs::path("libexec/everest/modules/System/constants.env");
const auto firmware_downloader = this->scripts_path / SIGNED_FIRMWARE_DOWNLOADER;
const auto constants = this->scripts_path / CONSTANTS;

const std::vector<std::string> download_args = {
constants.string(), firmware_update_request.location, firmware_file_path.string(),
Expand All @@ -193,7 +201,7 @@ void systemImpl::download_signed_firmware(const types::system::FirmwareUpdateReq
while (firmware_status.firmware_update_status == types::system::FirmwareUpdateStatusEnum::DownloadFailed &&
retries <= total_retries && !this->interrupt_firmware_download) {
boost::process::ipstream download_stream;
boost::process::child download_cmd(firmware_downloader, boost::process::args(download_args),
boost::process::child download_cmd(firmware_downloader.string(), boost::process::args(download_args),
boost::process::std_out > download_stream);
std::string temp;
retries += 1;
Expand Down Expand Up @@ -253,10 +261,10 @@ void systemImpl::install_signed_firmware(const types::system::FirmwareUpdateRequ
if (!this->firmware_installation_running) {
this->firmware_installation_running = true;
boost::process::ipstream install_stream;
const auto firmware_installer = "libexec/everest/modules/System/signed_firmware_installer.sh";
const auto constants = fs::path("libexec/everest/modules/System/constants.env");
const auto firmware_installer = this->scripts_path / SIGNED_FIRMWARE_INSTALLER;
const auto constants = this->scripts_path / CONSTANTS;
const std::vector<std::string> install_args = {constants.string()};
boost::process::child install_cmd(firmware_installer, boost::process::args(install_args),
boost::process::child install_cmd(firmware_installer.string(), boost::process::args(install_args),
boost::process::std_out > install_stream);
std::string temp;
while (std::getline(install_stream, temp)) {
Expand Down Expand Up @@ -315,8 +323,8 @@ systemImpl::handle_upload_logs(types::system::UploadLogsRequest& upload_logs_req
EVLOG_info << "Starting upload of log file";
this->interrupt_log_upload.exchange(false);
this->log_upload_running = true;
const auto diagnostics_uploader = "libexec/everest/modules/System/diagnostics_uploader.sh";
const auto constants = fs::path("libexec/everest/modules/System/constants.env");
const auto diagnostics_uploader = this->scripts_path / DIAGNOSTICS_UPLOADER;
const auto constants = this->scripts_path / CONSTANTS;

std::vector<std::string> args = {constants.string(), upload_logs_request.location, diagnostics_file_name,
diagnostics_file_path.string()};
Expand All @@ -330,7 +338,7 @@ systemImpl::handle_upload_logs(types::system::UploadLogsRequest& upload_logs_req
while (!uploaded && retries <= total_retries && !this->interrupt_log_upload) {

boost::process::ipstream stream;
boost::process::child cmd(diagnostics_uploader, boost::process::args(args),
boost::process::child cmd(diagnostics_uploader.string(), boost::process::args(args),
boost::process::std_out > stream);
std::string temp;
retries += 1;
Expand Down
2 changes: 2 additions & 0 deletions modules/System/main/systemImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class systemImpl : public systemImplBase {
// ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1
// insert your private definitions here

std::filesystem::path scripts_path;

std::atomic<bool> interrupt_firmware_download;
std::atomic<bool> interrupt_log_upload;

Expand Down

0 comments on commit 0008408

Please sign in to comment.