Skip to content

Commit 82b904e

Browse files
committed
make websocket callbacks optional
Signed-off-by: Soumya Subramanya <s.subramanya@alfen.com>
1 parent debd995 commit 82b904e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

include/ocpp/v201/charge_point.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@ struct Callbacks {
164164

165165
/* Callbacks for networking */
166166
/// \brief register a \p callback that is called when the websocket is connected successfully
167-
std::function < void(std::optional<NetworkConnectionProfile> network_profile)> websocket_connected_callback;
167+
std::optional<std::function<void(const std::optional<NetworkConnectionProfile>& network_connection_profile)>>
168+
websocket_connected_callback;
168169

169170
/// \brief register a \p callback that is called when the websocket connection is disconnected
170-
std::function<void()>websocket_disconnected_callback;
171+
std::optional<std::function<void()>> websocket_disconnected_callback;
171172

172173
// callback is called when receiving a SetNetworkProfile.req from the CSMS
173174
std::optional<std::function<SetNetworkProfileStatusEnum(

lib/ocpp/v201/charge_point.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <stdexcept>
1313
#include <string>
14-
#include <future>
1514
using namespace std::chrono_literals;
1615

1716
const auto DEFAULT_MAX_CUSTOMER_INFORMATION_DATA_LENGTH = 51200;
@@ -30,16 +29,17 @@ bool Callbacks::all_callbacks_valid() const {
3029
// this->connector_effective_operative_status_changed_callback != nullptr and
3130
this->get_log_request_callback != nullptr and this->unlock_connector_callback != nullptr and
3231
this->remote_start_transaction_callback != nullptr and this->is_reservation_for_token_callback != nullptr and
33-
this->update_firmware_request_callback != nullptr and this->websocket_connected_callback != nullptr and
34-
this->websocket_disconnected_callback != nullptr and
32+
this->update_firmware_request_callback != nullptr and
3533
(!this->variable_changed_callback.has_value() or this->variable_changed_callback.value() != nullptr) and
3634
(!this->validate_network_profile_callback.has_value() or
3735
this->validate_network_profile_callback.value() != nullptr) and
3836
(!this->configure_network_connection_profile_callback.has_value() or
3937
this->configure_network_connection_profile_callback.value() != nullptr) and
4038
(!this->time_sync_callback.has_value() or this->time_sync_callback.value() != nullptr) and
4139
(!this->boot_notification_callback.has_value() or this->boot_notification_callback.value() != nullptr) and
42-
(!this->ocpp_messages_callback.has_value() or this->ocpp_messages_callback.value() != nullptr);
40+
(!this->ocpp_messages_callback.has_value() or this->ocpp_messages_callback.value() != nullptr) and
41+
(!this->websocket_connected_callback.has_value() or this->websocket_connected_callback != nullptr) and
42+
(!this->websocket_disconnected_callback.has_value() or this->websocket_disconnected_callback != nullptr);
4343
}
4444

4545
ChargePoint::ChargePoint(const std::map<int32_t, int32_t>& evse_connector_structure,
@@ -697,8 +697,11 @@ void ChargePoint::init_websocket() {
697697
AttributeEnum::Actual, std::to_string(security_profile));
698698
}
699699

700-
// call the registered websocket connected callback
701-
this->callbacks.websocket_connected_callback(network_connection_profile);
700+
// call the registered websocket connected callback if it exists
701+
if(this->callbacks.websocket_connected_callback.has_value())
702+
{
703+
this->callbacks.websocket_connected_callback.value()(network_connection_profile);
704+
}
702705

703706
if (this->registration_status == RegistrationStatusEnum::Accepted and
704707
this->time_disconnected.time_since_epoch() != 0s) {
@@ -732,7 +735,11 @@ void ChargePoint::init_websocket() {
732735
// Get the current time point using steady_clock
733736
this->time_disconnected = std::chrono::steady_clock::now();
734737
}
735-
this->callbacks.websocket_disconnected_callback();
738+
//call the disconnected callback if it exists
739+
if(this->callbacks.websocket_disconnected_callback.has_value())
740+
{
741+
this->callbacks.websocket_disconnected_callback.value();
742+
}
736743
this->client_certificate_expiration_check_timer.stop();
737744
this->v2g_certificate_expiration_check_timer.stop();
738745
});

0 commit comments

Comments
 (0)