|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Pionix GmbH and Contributors to EVerest |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <ocpp/v201/message_handler.hpp> |
| 7 | + |
| 8 | +#include <ocpp/v201/message_dispatcher.hpp> |
| 9 | + |
| 10 | +namespace ocpp::v201 { |
| 11 | +class DeviceModel; |
| 12 | +class AuthorizationInterface; |
| 13 | +class ConnectivityManagerInterface; |
| 14 | +class EvseManagerInterface; |
| 15 | +class DatabaseHandlerInterface; |
| 16 | +class AvailabilityInterface; |
| 17 | +class SmartChargingInterface; |
| 18 | +class TariffAndCostInterface; |
| 19 | + |
| 20 | +struct GetTransactionStatusRequest; |
| 21 | + |
| 22 | +typedef std::function<void(const TransactionEventRequest& transaction_event)> TransactionEventCallback; |
| 23 | +typedef std::function<void(const std::optional<const int32_t> evse_id, const ResetEnum& reset_type)> ResetCallback; |
| 24 | +typedef std::function<void(const TransactionEventRequest& transaction_event, |
| 25 | + const TransactionEventResponse& transaction_event_response)> |
| 26 | + TransactionEventResponseCallback; |
| 27 | +typedef std::function<RequestStartStopStatusEnum(const int32_t evse_id, const ReasonEnum& stop_reason)> |
| 28 | + StopTransactionCallback; |
| 29 | +typedef std::function<void(const int32_t evse_id)> PauseChargingCallback; |
| 30 | + |
| 31 | +class TransactionInterface : public MessageHandlerInterface { |
| 32 | +public: |
| 33 | + virtual ~TransactionInterface() = default; |
| 34 | + |
| 35 | + /// \brief Event handler that should be called when a transaction has started |
| 36 | + /// \param evse_id |
| 37 | + /// \param connector_id |
| 38 | + /// \param session_id |
| 39 | + /// \param timestamp |
| 40 | + /// \param trigger_reason |
| 41 | + /// \param meter_start |
| 42 | + /// \param id_token |
| 43 | + /// \param group_id_token Optional group id token |
| 44 | + /// \param reservation_id |
| 45 | + /// \param remote_start_id |
| 46 | + /// \param charging_state The new charging state |
| 47 | + virtual void |
| 48 | + on_transaction_started(const int32_t evse_id, const int32_t connector_id, const std::string& session_id, |
| 49 | + const DateTime& timestamp, const ocpp::v201::TriggerReasonEnum trigger_reason, |
| 50 | + const MeterValue& meter_start, const std::optional<IdToken>& id_token, |
| 51 | + const std::optional<IdToken>& group_id_token, const std::optional<int32_t>& reservation_id, |
| 52 | + const std::optional<int32_t>& remote_start_id, const ChargingStateEnum charging_state) = 0; |
| 53 | + |
| 54 | + /// \brief Event handler that should be called when a transaction has finished |
| 55 | + /// \param evse_id |
| 56 | + /// \param timestamp |
| 57 | + /// \param meter_stop |
| 58 | + /// \param reason |
| 59 | + /// \param id_token |
| 60 | + /// \param signed_meter_value |
| 61 | + /// \param charging_state |
| 62 | + virtual void on_transaction_finished(const int32_t evse_id, const DateTime& timestamp, const MeterValue& meter_stop, |
| 63 | + const ReasonEnum reason, const TriggerReasonEnum trigger_reason, |
| 64 | + const std::optional<IdToken>& id_token, |
| 65 | + const std::optional<std::string>& signed_meter_value, |
| 66 | + const ChargingStateEnum charging_state) = 0; |
| 67 | + |
| 68 | + /* OCPP message requests */ |
| 69 | + |
| 70 | + // Functional Block E: Transactions |
| 71 | + virtual void transaction_event_req(const TransactionEventEnum& event_type, const DateTime& timestamp, |
| 72 | + const ocpp::v201::Transaction& transaction, |
| 73 | + const ocpp::v201::TriggerReasonEnum& trigger_reason, const int32_t seq_no, |
| 74 | + const std::optional<int32_t>& cable_max_current, |
| 75 | + const std::optional<ocpp::v201::EVSE>& evse, |
| 76 | + const std::optional<ocpp::v201::IdToken>& id_token, |
| 77 | + const std::optional<std::vector<ocpp::v201::MeterValue>>& meter_value, |
| 78 | + const std::optional<int32_t>& number_of_phases_used, const bool offline, |
| 79 | + const std::optional<int32_t>& reservation_id, |
| 80 | + const bool initiated_by_trigger_message = false) = 0; |
| 81 | + |
| 82 | + virtual void set_remote_start_id_for_evse(const int32_t evse_id, const IdToken id_token, |
| 83 | + const int32_t remote_start_id) = 0; |
| 84 | + virtual void schedule_reset(const std::optional<int32_t> reset_scheduled_evseid) = 0; |
| 85 | +}; |
| 86 | + |
| 87 | +class TransactionBlock : public TransactionInterface { |
| 88 | +public: |
| 89 | + TransactionBlock(MessageDispatcherInterface<MessageType>& message_dispatcher, DeviceModel& device_model, |
| 90 | + ConnectivityManagerInterface& connectivity_manager, EvseManagerInterface& evse_manager, |
| 91 | + MessageQueue<v201::MessageType>& message_queue, DatabaseHandlerInterface& database_handler, |
| 92 | + AuthorizationInterface& authorization, AvailabilityInterface& availability, |
| 93 | + SmartChargingInterface& smart_charging, TariffAndCostInterface& tariff_and_cost, |
| 94 | + StopTransactionCallback stop_transaction_callback, PauseChargingCallback pause_charging_callback, |
| 95 | + std::optional<TransactionEventCallback> transaction_event_callback, |
| 96 | + std::optional<TransactionEventResponseCallback> transaction_event_response_callback, |
| 97 | + ResetCallback reset_callback); |
| 98 | + void handle_message(const ocpp::EnhancedMessage<MessageType>& message) override; |
| 99 | + void on_transaction_started(const int32_t evse_id, const int32_t connector_id, const std::string& session_id, |
| 100 | + const DateTime& timestamp, const TriggerReasonEnum trigger_reason, |
| 101 | + const MeterValue& meter_start, const std::optional<IdToken>& id_token, |
| 102 | + const std::optional<IdToken>& group_id_token, |
| 103 | + const std::optional<int32_t>& reservation_id, |
| 104 | + const std::optional<int32_t>& remote_start_id, |
| 105 | + const ChargingStateEnum charging_state) override; |
| 106 | + void on_transaction_finished(const int32_t evse_id, const DateTime& timestamp, const MeterValue& meter_stop, |
| 107 | + const ReasonEnum reason, const TriggerReasonEnum trigger_reason, |
| 108 | + const std::optional<IdToken>& id_token, |
| 109 | + const std::optional<std::string>& signed_meter_value, |
| 110 | + const ChargingStateEnum charging_state) override; |
| 111 | + void transaction_event_req(const TransactionEventEnum& event_type, const DateTime& timestamp, |
| 112 | + const Transaction& transaction, const TriggerReasonEnum& trigger_reason, |
| 113 | + const int32_t seq_no, const std::optional<int32_t>& cable_max_current, |
| 114 | + const std::optional<EVSE>& evse, const std::optional<IdToken>& id_token, |
| 115 | + const std::optional<std::vector<MeterValue>>& meter_value, |
| 116 | + const std::optional<int32_t>& number_of_phases_used, const bool offline, |
| 117 | + const std::optional<int32_t>& reservation_id, |
| 118 | + const bool initiated_by_trigger_message = false) override; |
| 119 | + void set_remote_start_id_for_evse(const int32_t evse_id, const IdToken id_token, |
| 120 | + const int32_t remote_start_id) override; |
| 121 | + void schedule_reset(const std::optional<int32_t> reset_scheduled_evseid) override; |
| 122 | + |
| 123 | +private: // Members |
| 124 | + MessageDispatcherInterface<MessageType>& message_dispatcher; |
| 125 | + DeviceModel& device_model; |
| 126 | + ConnectivityManagerInterface& connectivity_manager; |
| 127 | + EvseManagerInterface& evse_manager; |
| 128 | + MessageQueue<v201::MessageType>& message_queue; |
| 129 | + DatabaseHandlerInterface& database_handler; |
| 130 | + AuthorizationInterface& authorization; |
| 131 | + AvailabilityInterface& availability; |
| 132 | + SmartChargingInterface& smart_charging; |
| 133 | + TariffAndCostInterface& tariff_and_cost; |
| 134 | + StopTransactionCallback stop_transaction_callback; |
| 135 | + PauseChargingCallback pause_charging_callback; |
| 136 | + std::optional<TransactionEventCallback> transaction_event_callback; |
| 137 | + std::optional<TransactionEventResponseCallback> transaction_event_response_callback; |
| 138 | + ResetCallback reset_callback; |
| 139 | + |
| 140 | + std::map<int32_t, std::pair<IdToken, int32_t>> remote_start_id_per_evse; |
| 141 | + /// \brief Used when an 'OnIdle' reset is requested, to perform the reset after the charging has stopped. |
| 142 | + bool reset_scheduled; |
| 143 | + /// \brief If `reset_scheduled` is true and the reset is for a specific evse id, it will be stored in this member. |
| 144 | + std::set<int32_t> reset_scheduled_evseids; |
| 145 | + |
| 146 | +private: // Functions |
| 147 | + /* OCPP message handlers */ |
| 148 | + |
| 149 | + // Functional Block E: Transaction |
| 150 | + void handle_transaction_event_response(const EnhancedMessage<v201::MessageType>& message); |
| 151 | + void handle_get_transaction_status(const Call<GetTransactionStatusRequest> call); |
| 152 | +}; |
| 153 | +} // namespace ocpp::v201 |
0 commit comments