Skip to content
Merged
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
1 change: 1 addition & 0 deletions modules/EnergyManagement/EEBUS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_link_libraries(${MODULE_NAME}
proto_control_service
proto_cs_lpc_service
everest::run_application
everest::io
)
add_dependencies(${MODULE_NAME} eebus_grpc_api_cmd)
target_include_directories(${MODULE_NAME}
Expand Down
8 changes: 8 additions & 0 deletions modules/EnergyManagement/EEBUS/ConfigValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ int ConfigValidator::get_max_nominal_power() const {
return this->config.max_nominal_power_W;
}

int ConfigValidator::get_restart_delay_s() const {
return this->config.restart_delay_s;
}

int ConfigValidator::get_reconnect_delay_s() const {
return this->config.reconnect_delay_s;
}

bool ConfigValidator::validate_eebus_service_port() const {
if (this->config.eebus_service_port < 0) {
EVLOG_error << "eebus service port is negative";
Expand Down
101 changes: 52 additions & 49 deletions modules/EnergyManagement/EEBUS/EEBUS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,70 @@ void EEBUS::init() {
}

if (this->config.manage_eebus_grpc_api_binary) {
if (config_validator->validate()) {
this->eebus_grpc_api_thread_active.store(true);
this->eebus_grpc_api_thread =
std::thread(&EEBUS::start_eebus_grpc_api, this, config_validator->get_eebus_grpc_api_binary_path(),
config_validator->get_grpc_port(), config_validator->get_certificate_path(),
config_validator->get_private_key_path());
} else {
EVLOG_critical << "Could not validate config for starting eebus_grpc_api binary";
}
this->eebus_grpc_api_thread_active.store(true);
this->eebus_grpc_api_thread = std::thread(
&EEBUS::start_eebus_grpc_api, this, config_validator->get_eebus_grpc_api_binary_path(),
config_validator->get_grpc_port(), config_validator->get_certificate_path(),
config_validator->get_private_key_path(), config_validator->get_restart_delay_s());
}

this->connection_handler = std::make_unique<EebusConnectionHandler>(config_validator);
event_handler.register_event_handler(this->connection_handler.get());

if (!this->connection_handler->initialize_connection()) {
EVLOG_critical << "Failed to initialize connection to EEBUS service";
return;
}

if (!this->connection_handler->add_lpc_use_case(this->callbacks)) {
EVLOG_critical << "Failed to add LPC use case";
return;
}

this->connection_handler->subscribe_to_events();
this->connection_handler->add_use_case(eebus::EEBusUseCase::LPC, this->callbacks);
this->connection_handler->done_adding_use_case();
}

void EEBUS::start_eebus_grpc_api(const std::filesystem::path& binary_path, int port,
const std::filesystem::path& cert_file, const std::filesystem::path& key_file) {
try {
std::vector<std::string> args;
constexpr int num_args = 6;
args.reserve(num_args);
args.emplace_back("-port");
args.emplace_back(std::to_string(port));
args.emplace_back("-certificate-path");
args.emplace_back(cert_file.string());
args.emplace_back("-private-key-path");
args.emplace_back(key_file.string());
everest::run_application::CmdOutput output = everest::run_application::run_application(
binary_path.string(), args, [this](const std::string& output_line) {
if (!output_line.empty()) {
EVLOG_debug << "eebus-grpc: " << output_line;
}
if (not this->eebus_grpc_api_thread_active) {
return everest::run_application::CmdControl::Terminate;
}
return everest::run_application::CmdControl::Continue;
});
EVLOG_info << "eebus-grpc exit code: " << output.exit_code;
} catch (const std::exception& e) {
EVLOG_critical << "start_eebus_grpc_api thread caught exception: " << e.what();
} catch (...) {
EVLOG_critical << "start_eebus_grpc_api thread caught unknown exception.";
const std::filesystem::path& cert_file, const std::filesystem::path& key_file,
int restart_delay_s) {
std::vector<std::string> args;
constexpr int num_args = 6;
args.reserve(num_args);
args.emplace_back("-port");
args.emplace_back(std::to_string(port));
args.emplace_back("-certificate-path");
args.emplace_back(cert_file.string());
args.emplace_back("-private-key-path");
args.emplace_back(key_file.string());

while (this->eebus_grpc_api_thread_active) {
try {
EVLOG_info << "Starting eebus_grpc_api binary...";
everest::run_application::CmdOutput output = everest::run_application::run_application(
binary_path.string(), args, [this](const std::string& output_line) {
if (!output_line.empty()) {
EVLOG_debug << "eebus-grpc: " << output_line;
}
if (not this->eebus_grpc_api_thread_active) {
return everest::run_application::CmdControl::Terminate;
}
return everest::run_application::CmdControl::Continue;
});
EVLOG_warning << "eebus-grpc process exited with code: " << output.exit_code;
} catch (const std::exception& e) {
EVLOG_critical << "start_eebus_grpc_api thread caught exception: " << e.what();
} catch (...) {
EVLOG_critical << "start_eebus_grpc_api thread caught unknown exception.";
}

if (this->eebus_grpc_api_thread_active) {
EVLOG_info << "Restarting eebus_grpc_api binary in " << restart_delay_s << " seconds...";
std::this_thread::sleep_for(std::chrono::seconds(restart_delay_s));
}
}
EVLOG_info << "eebus_grpc_api monitoring thread is stopping.";
}

void EEBUS::ready() {
invoke_ready(*p_main);
if (!this->connection_handler->start_service()) {
EVLOG_critical << "Failed to start EEBUS service";
return;

// Start the event handler in its own thread
event_handler_thread = std::thread([this]() { event_handler.run(running_flag); });

// Main application loop
while (running_flag) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

Expand Down
11 changes: 10 additions & 1 deletion modules/EnergyManagement/EEBUS/EEBUS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <EebusCallbacks.hpp>
#include <EebusConnectionHandler.hpp>
#include <everest/io/event/fd_event_handler.hpp>

Check warning on line 25 in modules/EnergyManagement/EEBUS/EEBUS.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EnergyManagement/EEBUS/EEBUS.hpp#L25

Include file: <everest/io/event/fd_event_handler.hpp> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <everest/io/event/timer_fd.hpp>

Check warning on line 26 in modules/EnergyManagement/EEBUS/EEBUS.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EnergyManagement/EEBUS/EEBUS.hpp#L26

Include file: <everest/io/event/timer_fd.hpp> not found. Please note: Cppcheck does not need standard library headers to get proper results.
// ev@4bf81b14-a215-475c-a1d3-0a484ae48918:v1

namespace module {
Expand All @@ -40,6 +42,8 @@
std::string serial_number;
int failsafe_control_limit_W;
int max_nominal_power_W;
int restart_delay_s;

Check warning on line 45 in modules/EnergyManagement/EEBUS/EEBUS.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EnergyManagement/EEBUS/EEBUS.hpp#L45

struct member 'Conf::restart_delay_s' is never used.
int reconnect_delay_s;

Check warning on line 46 in modules/EnergyManagement/EEBUS/EEBUS.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

modules/EnergyManagement/EEBUS/EEBUS.hpp#L46

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

class EEBUS : public Everest::ModuleBase {
Expand Down Expand Up @@ -72,13 +76,18 @@

// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1
void start_eebus_grpc_api(const std::filesystem::path& binary_path, int port,
const std::filesystem::path& cert_file, const std::filesystem::path& key_file);
const std::filesystem::path& cert_file, const std::filesystem::path& key_file,
int restart_delay_s);
std::thread eebus_grpc_api_thread;
std::atomic<bool> eebus_grpc_api_thread_active;

std::unique_ptr<EebusConnectionHandler> connection_handler;

eebus::EEBusCallbacks callbacks{};
everest::lib::io::event::fd_event_handler event_handler;
std::thread event_handler_thread;
std::atomic_bool running_flag{true};
std::shared_ptr<ConfigValidator> config_validator;
// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1
};

Expand Down
Loading
Loading