Skip to content

Commit

Permalink
Removed is_websocket_connected callback and clang format
Browse files Browse the repository at this point in the history
Signed-off-by: Piet Gömpel <[email protected]>
  • Loading branch information
Pietfried committed Nov 19, 2024
1 parent 773ad14 commit 67e86de
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 47 deletions.
6 changes: 2 additions & 4 deletions include/ocpp/v201/functional_blocks/data_transfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace v201 {
class DataTransferInterface : public MessageHandlerInterface {

public:
virtual ~DataTransferInterface() {};
virtual ~DataTransferInterface(){};

/// \brief Sends a DataTransfer.req message to the CSMS using the given parameters
/// \param vendorId
Expand All @@ -37,16 +37,14 @@ class DataTransfer : public DataTransferInterface {
MessageDispatcherInterface<MessageType>& message_dispatcher;
std::optional<std::function<DataTransferResponse(const DataTransferRequest& request)>> data_transfer_callback;
std::chrono::seconds response_timeout;
std::function<bool()> is_websocket_connected;

public:
DataTransfer(MessageDispatcherInterface<MessageType>& message_dispatcher,
const std::optional<std::function<DataTransferResponse(const DataTransferRequest& request)>>&
data_transfer_callback,
const std::function<bool()> is_websocket_connected, const std::chrono::seconds response_timeout) :
const std::chrono::seconds response_timeout) :
message_dispatcher(message_dispatcher),
data_transfer_callback(data_transfer_callback),
is_websocket_connected(is_websocket_connected),
response_timeout(response_timeout){};

void handle_message(const EnhancedMessage<MessageType>& message) override;
Expand Down
6 changes: 1 addition & 5 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,11 +1171,7 @@ void ChargePoint::initialize(const std::map<int32_t, int32_t>& evse_connector_st
this->message_dispatcher =
std::make_unique<MessageDispatcher>(*this->message_queue, *this->device_model, registration_status);
this->data_transfer = std::make_unique<DataTransfer>(
*this->message_dispatcher, this->callbacks.data_transfer_callback,
[&connectivity_manager = this->connectivity_manager]() {
return connectivity_manager->is_websocket_connected();
},
DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
*this->message_dispatcher, this->callbacks.data_transfer_callback, DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

if (this->callbacks.configure_network_connection_profile_callback.has_value()) {
this->connectivity_manager->set_configure_network_connection_profile_callback(
Expand Down
4 changes: 0 additions & 4 deletions lib/ocpp/v201/functional_blocks/data_transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ std::optional<DataTransferResponse> DataTransfer::data_transfer_req(const DataTr
ocpp::Call<DataTransferRequest> call(request);
auto data_transfer_future = this->message_dispatcher.dispatch_call_async(call);

if (not this->is_websocket_connected()) {
return std::nullopt;
}

if (data_transfer_future.wait_for(this->response_timeout) == std::future_status::timeout) {
EVLOG_warning << "Waiting for DataTransfer.conf future timed out";
return std::nullopt;
Expand Down
42 changes: 8 additions & 34 deletions tests/lib/ocpp/v201/functional_blocks/test_data_transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ DataTransferRequest create_example_request() {
return request;
}

bool is_websocket_connected() {
return true;
}

TEST(DataTransferTest, HandleDataTransferReq_NotImplemented) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, std::nullopt, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();
ocpp::Call<DataTransferRequest> call(request);
Expand All @@ -42,8 +37,7 @@ TEST(DataTransferTest, HandleDataTransferReq_NotImplemented) {

TEST(DataTransferTest, HandleDataTransferReq_NoCallback) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, std::nullopt, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();
ocpp::Call<DataTransferRequest> call(request);
Expand All @@ -68,16 +62,14 @@ TEST(DataTransferTest, HandleDataTransferReq_WithCallback) {
return response;
};

DataTransfer data_transfer(mock_dispatcher, callback, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, callback, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();
ocpp::Call<DataTransferRequest> call(request);
ocpp::EnhancedMessage<MessageType> enhanced_message;
enhanced_message.messageType = MessageType::DataTransfer;
enhanced_message.message = call;


EXPECT_CALL(mock_dispatcher, dispatch_call_result(_)).WillOnce(Invoke([](const json& call_result) {
auto response = call_result[ocpp::CALLRESULT_PAYLOAD].get<DataTransferResponse>();
EXPECT_EQ(response.status, DataTransferStatusEnum::Accepted);
Expand All @@ -86,24 +78,9 @@ TEST(DataTransferTest, HandleDataTransferReq_WithCallback) {
data_transfer.handle_message(enhanced_message);
}

TEST(DataTransferTest, DataTransferReq_NotConnected) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(
mock_dispatcher, std::nullopt, []() { return false; }, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();

EXPECT_CALL(mock_dispatcher, dispatch_call_async(_, _)).Times(1);

auto response = data_transfer.data_transfer_req(request);

EXPECT_FALSE(response.has_value());
}

TEST(DataTransferTest, DataTransferReq_Offline) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, std::nullopt, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();

Expand All @@ -120,7 +97,7 @@ TEST(DataTransferTest, DataTransferReq_Offline) {

TEST(DataTransferTest, DataTransferReq_Timeout) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected, std::chrono::seconds(1));
DataTransfer data_transfer(mock_dispatcher, std::nullopt, std::chrono::seconds(1));

DataTransferRequest request = create_example_request();

Expand All @@ -138,8 +115,7 @@ TEST(DataTransferTest, DataTransferReq_Timeout) {

TEST(DataTransferTest, DataTransferReq_Accepted) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, std::nullopt, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();

Expand All @@ -163,8 +139,7 @@ TEST(DataTransferTest, DataTransferReq_Accepted) {

TEST(DataTransferTest, DataTransferReq_EnumConversionException) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, std::nullopt, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();

Expand All @@ -191,8 +166,7 @@ TEST(DataTransferTest, DataTransferReq_EnumConversionException) {

TEST(DataTransferTest, DataTransferReq_JsonException) {
MockMessageDispatcher mock_dispatcher;
DataTransfer data_transfer(mock_dispatcher, std::nullopt, is_websocket_connected,
ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);
DataTransfer data_transfer(mock_dispatcher, std::nullopt, ocpp::DEFAULT_WAIT_FOR_FUTURE_TIMEOUT);

DataTransferRequest request = create_example_request();

Expand Down

0 comments on commit 67e86de

Please sign in to comment.