Skip to content

Commit 3cc37c2

Browse files
committed
refactor + some multi threading changes
Signed-off-by: Martin Litre <mnlitre@gmail.com>
1 parent 8e302ec commit 3cc37c2

4 files changed

Lines changed: 43 additions & 33 deletions

File tree

modules/EnergyManagement/EEBUS/EEBUS.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ bool wait_for_channel_ready(const std::shared_ptr<grpc::Channel>& channel, std::
7373

7474
} // namespace
7575

76-
void EEBUS::set_use_case_event_reader(std::unique_ptr<UseCaseEventReader> reader) {
77-
this->reader = std::move(reader);
76+
EEBUS::~EEBUS() {
77+
if (this->config.manage_eebus_grpc_api_binary and this->eebus_grpc_api_thread_active.exchange(false)) {
78+
this->eebus_grpc_api_thread.join();
79+
}
7880
}
7981

8082
void EEBUS::init() {
@@ -90,6 +92,7 @@ void EEBUS::init() {
9092
}
9193

9294
if (this->config.manage_eebus_grpc_api_binary) {
95+
this->eebus_grpc_api_thread_active.store(true);
9396
this->eebus_grpc_api_thread =
9497
std::thread(start_eebus_grpc_api, this->config_validator->get_eebus_grpc_api_binary_path(),
9598
this->config.control_service_rpc_port, this->config_validator->get_certificate_path(),
@@ -119,7 +122,10 @@ void EEBUS::init() {
119122
this->cs_calls->call_start_setup();
120123
EVLOG_critical << "ML DEBUG PASSED CALL START SETUP";
121124
this->cs_calls->call_register_remote_ski(this->config.eebus_ems_ski);
122-
std::string endpoint = this->cs_calls->call_add_use_case();
125+
this->lpc_use_case = control_service::CreateUseCase(
126+
control_service::UseCase_ActorType_Enum::UseCase_ActorType_Enum_ControllableSystem,
127+
control_service::UseCase_NameType_Enum::UseCase_NameType_Enum_limitationOfPowerConsumption);
128+
std::string endpoint = this->cs_calls->call_add_use_case(&this->lpc_use_case);
123129

124130
std::shared_ptr<grpc::Channel> channel2 = grpc::CreateChannel(endpoint, grpc::InsecureChannelCredentials());
125131
this->failed = !wait_for_channel_ready(channel2, std::chrono::seconds(eebus_one_minute));
@@ -135,21 +141,21 @@ void EEBUS::init() {
135141
this->cs_lpc_calls->call_set_failsafe_consumption_active_power_limit();
136142
this->cs_lpc_calls->call_set_failsafe_duration_minimum();
137143

138-
this->cs_calls->subscribe_use_case_events(this, this->cs_lpc_stub);
144+
this->cs_calls->subscribe_use_case_events(this, this->cs_lpc_stub, &this->lpc_use_case);
139145
}
140146

141147
void EEBUS::ready() {
142148
invoke_ready(*p_main);
143149

144-
if (this->failed) {
150+
if (this->failed and this->eebus_grpc_api_thread_active.exchange(false)) {
145151
EVLOG_error << "EEBUS module failed to initialize";
146152
this->eebus_grpc_api_thread.join();
147153
return;
148154
}
149155

150156
this->cs_calls->call_start_service();
151157

152-
if (this->config.manage_eebus_grpc_api_binary) {
158+
if (this->config.manage_eebus_grpc_api_binary and this->eebus_grpc_api_thread_active.exchange(false)) {
153159
this->eebus_grpc_api_thread.join();
154160
}
155161
}

modules/EnergyManagement/EEBUS/EEBUS.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class EEBUS : public Everest::ModuleBase {
6161
const Conf& config;
6262

6363
// ev@1fce4c5e-0ab8-41bb-90f7-14277703d2ac:v1
64-
void set_use_case_event_reader(std::unique_ptr<UseCaseEventReader> reader);
64+
~EEBUS() override;
6565
// ev@1fce4c5e-0ab8-41bb-90f7-14277703d2ac:v1
6666

6767
protected:
@@ -77,14 +77,15 @@ class EEBUS : public Everest::ModuleBase {
7777
// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1
7878
std::shared_ptr<control_service::ControlService::Stub> control_service_stub;
7979
std::shared_ptr<cs_lpc::ControllableSystemLPCControl::Stub> cs_lpc_stub;
80-
std::unique_ptr<UseCaseEventReader> reader;
8180
std::thread eebus_grpc_api_thread;
81+
std::atomic<bool> eebus_grpc_api_thread_active;
8282

8383
std::unique_ptr<grpc_calls::ControlServiceCalls> cs_calls;
8484
std::unique_ptr<grpc_calls::ControllableSystemLPCControlCalls> cs_lpc_calls;
8585
std::unique_ptr<ConfigValidator> config_validator;
8686

8787
bool failed;
88+
control_service::UseCase lpc_use_case;
8889
// ev@211cfdbe-f69a-4cd6-a4ec-f8aaa3d1b6c8:v1
8990
};
9091

modules/EnergyManagement/EEBUS/grpc_calls/ControlServiceCalls.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ namespace module {
88
namespace grpc_calls {
99

1010
ControlServiceCalls::ControlServiceCalls(const std::shared_ptr<control_service::ControlService::Stub>& stub) :
11-
stub(stub) {
11+
control_service_stub(stub) {
1212
this->entity_address = common_types::CreateEntityAddress(std::vector<uint32_t>{1});
13-
this->use_case = control_service::CreateUseCase(
14-
control_service::UseCase_ActorType_Enum::UseCase_ActorType_Enum_ControllableSystem,
15-
control_service::UseCase_NameType_Enum::UseCase_NameType_Enum_limitationOfPowerConsumption);
1613
}
1714

1815
void ControlServiceCalls::call_set_config(uint32_t port, std::string vendor_code, std::string device_brand,
@@ -25,47 +22,49 @@ void ControlServiceCalls::call_set_config(uint32_t port, std::string vendor_code
2522
control_service::DeviceType_Enum::DeviceType_Enum_CHARGING_STATION,
2623
std::vector<control_service::EntityType_Enum>{control_service::EntityType_Enum::EntityType_Enum_EVSE}, 4);
2724
control_service::EmptyResponse response;
28-
control_service::CallSetConfig(this->stub, request, &response);
25+
control_service::CallSetConfig(this->control_service_stub, request, &response);
2926
}
3027

3128
void ControlServiceCalls::call_start_setup() {
3229
control_service::EmptyResponse response;
33-
control_service::CallStartSetup(this->stub, control_service::EmptyRequest(), &response);
30+
control_service::CallStartSetup(this->control_service_stub, control_service::EmptyRequest(), &response);
3431
}
3532

3633
void ControlServiceCalls::call_register_remote_ski(const std::string& remote_ski) {
3734
control_service::RegisterRemoteSkiRequest request;
3835
request = control_service::CreateRegisterRemoteSkiRequest(remote_ski);
3936
control_service::EmptyResponse response;
40-
control_service::CallRegisterRemoteSki(this->stub, request, &response);
37+
control_service::CallRegisterRemoteSki(this->control_service_stub, request, &response);
4138
}
4239

4340
void ControlServiceCalls::call_start_service() {
4441
control_service::EmptyResponse response;
45-
control_service::CallStartService(this->stub, control_service::EmptyRequest(), &response);
42+
control_service::CallStartService(this->control_service_stub, control_service::EmptyRequest(), &response);
4643
}
4744

48-
std::string ControlServiceCalls::call_add_use_case() {
45+
std::string ControlServiceCalls::call_add_use_case(control_service::UseCase* use_case) {
4946
control_service::AddUseCaseRequest request =
50-
control_service::CreateAddUseCaseRequest(&this->entity_address, &this->use_case);
47+
control_service::CreateAddUseCaseRequest(&this->entity_address, use_case);
5148
control_service::AddUseCaseResponse response;
52-
control_service::CallAddUseCase(this->stub, request, &response);
49+
control_service::CallAddUseCase(this->control_service_stub, request, &response);
5350

5451
std::ignore = request.release_entity_address();
5552
std::ignore = request.release_use_case();
5653

5754
return response.endpoint();
5855
}
5956

60-
void ControlServiceCalls::subscribe_use_case_events(
61-
module::EEBUS* eebus_module, const std::shared_ptr<cs_lpc::ControllableSystemLPCControl::Stub>& cs_lpc_stub) {
62-
control_service::SubscribeUseCaseEventsRequest request =
63-
control_service::CreateSubscribeUseCaseEventsRequest(&this->entity_address, &this->use_case);
64-
eebus_module->set_use_case_event_reader(
65-
std::make_unique<UseCaseEventReader>(this->stub, cs_lpc_stub, request, eebus_module));
66-
67-
std::ignore = request.release_entity_address();
68-
std::ignore = request.release_use_case();
57+
void ControlServiceCalls::subscribe_use_case_events(module::EEBUS* eebus_module, use_case_stubs stub,
58+
control_service::UseCase* use_case) {
59+
if (std::holds_alternative<std::shared_ptr<cs_lpc::ControllableSystemLPCControl::Stub>>(stub)) {
60+
control_service::SubscribeUseCaseEventsRequest request =
61+
control_service::CreateSubscribeUseCaseEventsRequest(&this->entity_address, use_case);
62+
this->lpc_uc_reader = std::make_unique<UseCaseEventReader>(
63+
this->control_service_stub, std::get<std::shared_ptr<cs_lpc::ControllableSystemLPCControl::Stub>>(stub),
64+
request, eebus_module);
65+
std::ignore = request.release_entity_address();
66+
std::ignore = request.release_use_case();
67+
}
6968
}
7069

7170
} // namespace grpc_calls

modules/EnergyManagement/EEBUS/include/grpc_calls/ControlServiceCalls.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
#include <control_service/control_service.grpc-ext.pb.h>
77
#include <usecases/cs/lpc/service.grpc-ext.pb.h>
88

9+
#include <UseCaseEventReader.hpp>
10+
911
namespace module {
1012

1113
class EEBUS;
1214

1315
namespace grpc_calls {
1416

17+
using use_case_stubs = std::variant<std::shared_ptr<cs_lpc::ControllableSystemLPCControl::Stub>>;
18+
1519
class ControlServiceCalls {
1620
public:
1721
explicit ControlServiceCalls(const std::shared_ptr<control_service::ControlService::Stub>& stub);
@@ -20,17 +24,17 @@ class ControlServiceCalls {
2024
std::string serial_number);
2125
void call_start_setup();
2226
void call_register_remote_ski(const std::string& remote_ski);
23-
std::string call_add_use_case();
27+
std::string call_add_use_case(control_service::UseCase* use_case);
2428

25-
void subscribe_use_case_events(module::EEBUS* eebus_module,
26-
const std::shared_ptr<cs_lpc::ControllableSystemLPCControl::Stub>& cs_lpc_stub);
29+
void subscribe_use_case_events(module::EEBUS* eebus_module, use_case_stubs stub,
30+
control_service::UseCase* use_case);
2731

2832
void call_start_service();
2933

3034
private:
31-
const std::shared_ptr<control_service::ControlService::Stub> stub;
35+
std::shared_ptr<control_service::ControlService::Stub> control_service_stub;
3236
common_types::EntityAddress entity_address;
33-
control_service::UseCase use_case;
37+
std::unique_ptr<UseCaseEventReader> lpc_uc_reader;
3438
};
3539

3640
} // namespace grpc_calls

0 commit comments

Comments
 (0)