Skip to content

Commit ac64716

Browse files
JB-CompleoCSDR-CompleoCS
authored andcommitted
lib: ocpp/v2/charge_point: move time disconnected detection to connectivity manager
Signed-off-by: Jan Bruchhaus <jan.bruchhaus@compleo-cs.com>
1 parent e0a7ab2 commit ac64716

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

lib/everest/ocpp/include/ocpp/v2/charge_point.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,6 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
399399
}
400400
};
401401

402-
std::chrono::time_point<std::chrono::steady_clock> time_disconnected;
403-
404402
// callback struct
405403
Callbacks callbacks;
406404

lib/everest/ocpp/include/ocpp/v2/connectivity_manager.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <ocpp/v2/messages/SetNetworkProfile.hpp>
88
#include <ocpp/v2/ocpp_types.hpp>
99

10+
#include <chrono>
1011
#include <functional>
1112
#include <future>
1213
#include <optional>
14+
1315
namespace ocpp {
1416
namespace v2 {
1517

@@ -84,6 +86,11 @@ class ConnectivityManagerInterface {
8486
///
8587
virtual bool is_websocket_connected() = 0;
8688

89+
/// @brief Get the time the websocket has been disconnected.
90+
/// @return The time the websocket has been disconnected
91+
///
92+
virtual std::optional<std::chrono::time_point<std::chrono::steady_clock>> get_time_disconnected() const = 0;
93+
8794
/// \brief Connect to the websocket
8895
/// \param configuration_slot Optional the network_profile_slot to connect to. std::nullopt will select the slot
8996
/// internally.
@@ -168,6 +175,7 @@ class ConnectivityManager : public ConnectivityManagerInterface {
168175
std::optional<std::int32_t> get_priority_from_configuration_slot(const int configuration_slot) const override;
169176
const std::vector<int>& get_network_connection_slots() const override;
170177
bool is_websocket_connected() override;
178+
std::optional<std::chrono::time_point<std::chrono::steady_clock>> get_time_disconnected() const;
171179
void connect(std::optional<std::int32_t> network_profile_slot = std::nullopt) override;
172180
void disconnect() override;
173181
bool send_to_websocket(const std::string& message) override;
@@ -176,6 +184,8 @@ class ConnectivityManager : public ConnectivityManagerInterface {
176184
void confirm_successful_connection() override;
177185

178186
private:
187+
std::optional<std::chrono::time_point<std::chrono::steady_clock>> time_disconnected{};
188+
179189
/// \brief Initializes the websocket and tries to connect
180190
///
181191
void try_connect_websocket();

lib/everest/ocpp/lib/ocpp/v2/charge_point.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,11 @@ void ChargePoint::websocket_connected_callback(const int configuration_slot,
10921092
if (this->registration_status == RegistrationStatusEnum::Accepted) {
10931093
this->connectivity_manager->confirm_successful_connection();
10941094

1095-
if (this->time_disconnected.time_since_epoch() != 0s) {
1095+
if (const auto time_disconnected = this->connectivity_manager->get_time_disconnected();
1096+
time_disconnected.has_value()) {
10961097
// handle offline threshold
10971098
// Get the current time point using steady_clock
1098-
auto offline_duration = std::chrono::steady_clock::now() - this->time_disconnected;
1099+
const auto offline_duration = std::chrono::steady_clock::now() - *time_disconnected;
10991100

11001101
// B04.FR.01
11011102
// If offline period exceeds offline threshold then send the status notification for all connectors
@@ -1113,7 +1114,6 @@ void ChargePoint::websocket_connected_callback(const int configuration_slot,
11131114
this->security->init_certificate_expiration_check_timers(); // re-init as timers are stopped on disconnect
11141115
}
11151116
}
1116-
this->time_disconnected = std::chrono::time_point<std::chrono::steady_clock>();
11171117

11181118
// We have a connection again so next time it fails we should send the notification again
11191119
this->skip_invalid_csms_certificate_notifications = false;
@@ -1128,12 +1128,6 @@ void ChargePoint::websocket_disconnected_callback(const int configuration_slot,
11281128
const NetworkConnectionProfile& network_connection_profile) {
11291129
this->message_queue->pause();
11301130

1131-
// check if offline threshold has been defined
1132-
if (this->device_model->get_value<int>(ControllerComponentVariables::OfflineThreshold) != 0) {
1133-
// Get the current time point using steady_clock
1134-
this->time_disconnected = std::chrono::steady_clock::now();
1135-
}
1136-
11371131
this->security->stop_certificate_expiration_check_timers();
11381132
if (this->callbacks.connection_state_changed_callback.has_value()) {
11391133
this->callbacks.connection_state_changed_callback.value()(false, configuration_slot, network_connection_profile,

lib/everest/ocpp/lib/ocpp/v2/connectivity_manager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ bool ConnectivityManager::is_websocket_connected() {
122122
return this->websocket != nullptr && this->websocket->is_connected();
123123
}
124124

125+
std::optional<std::chrono::time_point<std::chrono::steady_clock>> ConnectivityManager::get_time_disconnected() const {
126+
return this->time_disconnected;
127+
}
128+
125129
void ConnectivityManager::connect(std::optional<std::int32_t> network_profile_slot) {
126130
if (this->network_connection_slots.empty()) {
127131
EVLOG_warning << "No network connection profiles configured, aborting websocket connection.";
@@ -401,6 +405,7 @@ void ConnectivityManager::on_websocket_connected(OcppProtocolVersion protocol) {
401405
const int actual_configuration_slot = get_active_network_configuration_slot();
402406
std::optional<NetworkConnectionProfile> network_connection_profile =
403407
this->get_network_connection_profile(actual_configuration_slot);
408+
this->time_disconnected = std::nullopt;
404409

405410
if (this->websocket_connected_callback.has_value() and network_connection_profile.has_value()) {
406411
this->websocket_connected_callback.value()(actual_configuration_slot, network_connection_profile.value(),
@@ -411,6 +416,9 @@ void ConnectivityManager::on_websocket_connected(OcppProtocolVersion protocol) {
411416
void ConnectivityManager::on_websocket_disconnected() {
412417
std::optional<NetworkConnectionProfile> network_connection_profile =
413418
this->get_network_connection_profile(this->get_active_network_configuration_slot());
419+
if (!this->time_disconnected.has_value()) {
420+
this->time_disconnected = std::chrono::steady_clock::now();
421+
}
414422

415423
if (this->websocket_disconnected_callback.has_value() and network_connection_profile.has_value()) {
416424
this->websocket_disconnected_callback.value()(this->get_active_network_configuration_slot(),

lib/everest/ocpp/tests/lib/ocpp/v2/mocks/connectivity_manager_mock.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ConnectivityManagerMock : public ConnectivityManagerInterface {
2424
(const));
2525
MOCK_METHOD(const std::vector<int>&, get_network_connection_slots, (), (const));
2626
MOCK_METHOD(bool, is_websocket_connected, ());
27+
MOCK_METHOD(std::optional<std::chrono::time_point<std::chrono::steady_clock>>, get_time_disconnected, (), (const));
2728
MOCK_METHOD(void, connect, (std::optional<std::int32_t> network_profile_slot));
2829
MOCK_METHOD(void, disconnect, ());
2930
MOCK_METHOD(bool, send_to_websocket, (const std::string& message));

0 commit comments

Comments
 (0)