Skip to content

Commit

Permalink
* Added interface for message handling
Browse files Browse the repository at this point in the history
* DataTransferInterface inherits from this MessageHandlerInterface

Signed-off-by: Piet Gömpel <[email protected]>
  • Loading branch information
Pietfried committed Nov 19, 2024
1 parent 9578416 commit 773ad14
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 131 deletions.
9 changes: 9 additions & 0 deletions doc/message_dispatching.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class v201_MessageDispatcher {
- RegistrationStatusEnum& registration_status
}
class v201_MessageHandlerInterface {
+handle_message(EnhancedMessage~v201_MessageType~ message)
}
class v16_MessageHandlerInterface {
+handle_message(EnhancedMessage~v16_MessageType~ message)
}
class v201_DataTransferInterface {
+data_transfer_req(request: DataTransferRequest): std::optional~DataTransferResponse~
+handle_data_transfer_req(call: Call~DataTransferRequest~)
Expand All @@ -44,6 +52,7 @@ class v16_ChargePoint {
MessageDispatcherInterface <|-- v16_MessageDispatcher
MessageDispatcherInterface <|-- v201_MessageDispatcher
v201_DataTransferInterface <|-- v201_DataTransfer
v201_MessageHandlerInterface <|-- v201_DataTransferInterface
MessageDispatcherInterface *-- v201_DataTransfer
MessageDispatcherInterface *-- v201_ChargePoint
v201_DataTransferInterface *-- v201_ChargePoint
Expand Down
10 changes: 5 additions & 5 deletions include/ocpp/v201/functional_blocks/data_transfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
#pragma once

#include <ocpp/v201/message_dispatcher.hpp>
#include <ocpp/v201/message_handler.hpp>
#include <ocpp/v201/messages/DataTransfer.hpp>

namespace ocpp {
namespace v201 {

class DataTransferInterface {
class DataTransferInterface : public MessageHandlerInterface {

public:
virtual ~DataTransferInterface() {};

/// \brief Sends a DataTransfer.req message to the CSMS using the given parameters
/// \param vendorId
/// \param messageId
Expand All @@ -26,9 +29,6 @@ class DataTransferInterface {
/// \return DataTransferResponse containing the result from CSMS. In case no response is received from the CSMS
/// because the message timed out or the charging station is offline, std::nullopt is returned
virtual std::optional<DataTransferResponse> data_transfer_req(const DataTransferRequest& request) = 0;

/// \brief Handles the given DataTransfer.req \p call by the CSMS by responding with a CallResult
virtual void handle_data_transfer_req(Call<DataTransferRequest> call) = 0;
};

class DataTransfer : public DataTransferInterface {
Expand All @@ -49,7 +49,7 @@ class DataTransfer : public DataTransferInterface {
is_websocket_connected(is_websocket_connected),
response_timeout(response_timeout){};

void handle_data_transfer_req(Call<DataTransferRequest> call) override;
void handle_message(const EnhancedMessage<MessageType>& message) override;

std::optional<DataTransferResponse> data_transfer_req(const CiString<255>& vendorId,
const std::optional<CiString<50>>& messageId,
Expand Down
39 changes: 39 additions & 0 deletions include/ocpp/v201/message_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#pragma once

#include <ocpp/common/message_queue.hpp>

namespace ocpp {
namespace v201 {

/// \brief Interface for handling OCPP2.0.1 CALL messages from the CSMS. Classes implementing a functional block shall
/// extend this interface.
class MessageHandlerInterface {

public:
virtual ~MessageHandlerInterface() {
}
/// \brief Handles the given \p message from the CSMS. This includes dispatching a CALLRESULT as a response to the
/// incoming \p message .
/// @param message
virtual void handle_message(const EnhancedMessage<MessageType>& message) = 0;
};

class MessageTypeNotImplementedException : public std::exception {
private:
std::string message;

public:
MessageTypeNotImplementedException(MessageType message_type) :

Check notice on line 29 in include/ocpp/v201/message_handler.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/ocpp/v201/message_handler.hpp#L29

Class 'MessageTypeNotImplementedException' has a constructor with 1 argument that is not explicit.
message("Message is not implemented: " + conversions::messagetype_to_string(message_type)) {
}

const char* what() const noexcept override {
return message.c_str();
}
};

} // namespace v201
} // namespace ocpp
254 changes: 131 additions & 123 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,133 +1260,141 @@ void ChargePoint::remove_network_connection_profiles_below_actual_security_profi

void ChargePoint::handle_message(const EnhancedMessage<v201::MessageType>& message) {
const auto& json_message = message.message;
switch (message.messageType) {
case MessageType::BootNotificationResponse:
this->handle_boot_notification_response(json_message);
break;
case MessageType::SetVariables:
this->handle_set_variables_req(json_message);
break;
case MessageType::GetVariables:
this->handle_get_variables_req(message);
break;
case MessageType::GetBaseReport:
this->handle_get_base_report_req(json_message);
break;
case MessageType::GetReport:
this->handle_get_report_req(message);
break;
case MessageType::Reset:
this->handle_reset_req(json_message);
break;
case MessageType::SetNetworkProfile:
this->handle_set_network_profile_req(json_message);
break;
case MessageType::ChangeAvailability:
this->handle_change_availability_req(json_message);
break;
case MessageType::TransactionEventResponse:
this->handle_transaction_event_response(message);
break;
case MessageType::RequestStartTransaction:
this->handle_remote_start_transaction_request(json_message);
break;
case MessageType::RequestStopTransaction:
this->handle_remote_stop_transaction_request(json_message);
break;
case MessageType::DataTransfer:
this->data_transfer->handle_data_transfer_req(json_message);
break;
case MessageType::GetLog:
this->handle_get_log_req(json_message);
break;
case MessageType::ClearCache:
this->handle_clear_cache_req(json_message);
break;
case MessageType::UpdateFirmware:
this->handle_firmware_update_req(json_message);
break;
case MessageType::UnlockConnector:
this->handle_unlock_connector(json_message);
break;
case MessageType::TriggerMessage:
this->handle_trigger_message(json_message);
break;
case MessageType::SignCertificateResponse:
this->handle_sign_certificate_response(json_message);
break;
case MessageType::HeartbeatResponse:
this->handle_heartbeat_response(json_message);
break;
case MessageType::SendLocalList:
this->handle_send_local_authorization_list_req(json_message);
break;
case MessageType::GetLocalListVersion:
this->handle_get_local_authorization_list_version_req(json_message);
break;
case MessageType::CertificateSigned:
this->handle_certificate_signed_req(json_message);
break;
case MessageType::GetTransactionStatus:
this->handle_get_transaction_status(json_message);
break;
case MessageType::GetInstalledCertificateIds:
this->handle_get_installed_certificate_ids_req(json_message);
break;
case MessageType::InstallCertificate:
this->handle_install_certificate_req(json_message);
break;
case MessageType::DeleteCertificate:
this->handle_delete_certificate_req(json_message);
break;
case MessageType::CustomerInformation:
this->handle_customer_information_req(json_message);
break;
case MessageType::SetChargingProfile:
this->handle_set_charging_profile_req(json_message);
break;
case MessageType::ClearChargingProfile:
this->handle_clear_charging_profile_req(json_message);
break;
case MessageType::GetChargingProfiles:
this->handle_get_charging_profiles_req(json_message);
break;
case MessageType::GetCompositeSchedule:
this->handle_get_composite_schedule_req(json_message);
break;
case MessageType::SetMonitoringBase:
this->handle_set_monitoring_base_req(json_message);
break;
case MessageType::SetMonitoringLevel:
this->handle_set_monitoring_level_req(json_message);
break;
case MessageType::SetVariableMonitoring:
this->handle_set_variable_monitoring_req(message);
break;
case MessageType::GetMonitoringReport:
this->handle_get_monitoring_report_req(json_message);
break;
case MessageType::ClearVariableMonitoring:
this->handle_clear_variable_monitoring_req(json_message);
break;
case MessageType::GetDisplayMessages:
this->handle_get_display_message(json_message);
break;
case MessageType::SetDisplayMessage:
this->handle_set_display_message(json_message);
break;
case MessageType::ClearDisplayMessage:
this->handle_clear_display_message(json_message);
break;
case MessageType::CostUpdated:
this->handle_costupdated_req(json_message);
break;
default:
try {
switch (message.messageType) {
case MessageType::BootNotificationResponse:
this->handle_boot_notification_response(json_message);
break;
case MessageType::SetVariables:
this->handle_set_variables_req(json_message);
break;
case MessageType::GetVariables:
this->handle_get_variables_req(message);
break;
case MessageType::GetBaseReport:
this->handle_get_base_report_req(json_message);
break;
case MessageType::GetReport:
this->handle_get_report_req(message);
break;
case MessageType::Reset:
this->handle_reset_req(json_message);
break;
case MessageType::SetNetworkProfile:
this->handle_set_network_profile_req(json_message);
break;
case MessageType::ChangeAvailability:
this->handle_change_availability_req(json_message);
break;
case MessageType::TransactionEventResponse:
this->handle_transaction_event_response(message);
break;
case MessageType::RequestStartTransaction:
this->handle_remote_start_transaction_request(json_message);
break;
case MessageType::RequestStopTransaction:
this->handle_remote_stop_transaction_request(json_message);
break;
case MessageType::DataTransfer:
this->data_transfer->handle_message(message);
break;
case MessageType::GetLog:
this->handle_get_log_req(json_message);
break;
case MessageType::ClearCache:
this->handle_clear_cache_req(json_message);
break;
case MessageType::UpdateFirmware:
this->handle_firmware_update_req(json_message);
break;
case MessageType::UnlockConnector:
this->handle_unlock_connector(json_message);
break;
case MessageType::TriggerMessage:
this->handle_trigger_message(json_message);
break;
case MessageType::SignCertificateResponse:
this->handle_sign_certificate_response(json_message);
break;
case MessageType::HeartbeatResponse:
this->handle_heartbeat_response(json_message);
break;
case MessageType::SendLocalList:
this->handle_send_local_authorization_list_req(json_message);
break;
case MessageType::GetLocalListVersion:
this->handle_get_local_authorization_list_version_req(json_message);
break;
case MessageType::CertificateSigned:
this->handle_certificate_signed_req(json_message);
break;
case MessageType::GetTransactionStatus:
this->handle_get_transaction_status(json_message);
break;
case MessageType::GetInstalledCertificateIds:
this->handle_get_installed_certificate_ids_req(json_message);
break;
case MessageType::InstallCertificate:
this->handle_install_certificate_req(json_message);
break;
case MessageType::DeleteCertificate:
this->handle_delete_certificate_req(json_message);
break;
case MessageType::CustomerInformation:
this->handle_customer_information_req(json_message);
break;
case MessageType::SetChargingProfile:
this->handle_set_charging_profile_req(json_message);
break;
case MessageType::ClearChargingProfile:
this->handle_clear_charging_profile_req(json_message);
break;
case MessageType::GetChargingProfiles:
this->handle_get_charging_profiles_req(json_message);
break;
case MessageType::GetCompositeSchedule:
this->handle_get_composite_schedule_req(json_message);
break;
case MessageType::SetMonitoringBase:
this->handle_set_monitoring_base_req(json_message);
break;
case MessageType::SetMonitoringLevel:
this->handle_set_monitoring_level_req(json_message);
break;
case MessageType::SetVariableMonitoring:
this->handle_set_variable_monitoring_req(message);
break;
case MessageType::GetMonitoringReport:
this->handle_get_monitoring_report_req(json_message);
break;
case MessageType::ClearVariableMonitoring:
this->handle_clear_variable_monitoring_req(json_message);
break;
case MessageType::GetDisplayMessages:
this->handle_get_display_message(json_message);
break;
case MessageType::SetDisplayMessage:
this->handle_set_display_message(json_message);
break;
case MessageType::ClearDisplayMessage:
this->handle_clear_display_message(json_message);
break;
case MessageType::CostUpdated:
this->handle_costupdated_req(json_message);
break;
default:
if (message.messageTypeId == MessageTypeId::CALL) {
const auto call_error = CallError(message.uniqueId, "NotImplemented", "", json({}));
this->message_dispatcher->dispatch_call_error(call_error);
}
break;
}
} catch (const MessageTypeNotImplementedException& e) {
EVLOG_warning << e.what();
if (message.messageTypeId == MessageTypeId::CALL) {
const auto call_error = CallError(message.uniqueId, "NotImplemented", "", json({}));
this->message_dispatcher->dispatch_call_error(call_error);
}
break;
}
}

Expand Down
8 changes: 7 additions & 1 deletion lib/ocpp/v201/functional_blocks/data_transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
namespace ocpp {
namespace v201 {

void DataTransfer::handle_data_transfer_req(Call<DataTransferRequest> call) {
void DataTransfer::handle_message(const EnhancedMessage<MessageType>& message) {

if (message.messageType != MessageType::DataTransfer) {
throw MessageTypeNotImplementedException(message.messageType);
}

Call<DataTransferRequest> call = message.message;
const auto msg = call.msg;
DataTransferResponse response;
response.status = DataTransferStatusEnum::UnknownVendorId;
Expand Down
Loading

0 comments on commit 773ad14

Please sign in to comment.