ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
transaction.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_V16_TRANSACTION_HPP
4#define OCPP_V16_TRANSACTION_HPP
5
6#include <memory>
7#include <random>
8
9#include <everest/timer.hpp>
10#include <ocpp/v16/ocpp_types.hpp>
11#include <ocpp/v16/types.hpp>
12
13namespace ocpp {
14namespace v16 {
15
20 double energy_Wh;
22 this->timestamp = timestamp;
23 this->energy_Wh = energy_Wh;
24 }
25};
26
29private:
30 std::optional<int32_t> transaction_id;
31 int32_t internal_transaction_id;
32 int32_t connector;
33 std::string session_id;
34 CiString<20> id_token;
35 std::shared_ptr<StampedEnergyWh> start_energy_wh;
36 std::optional<int32_t> reservation_id;
37 bool active;
38 bool finished;
39 bool has_signed_meter_values;
40 std::unique_ptr<Everest::SteadyTimer> meter_values_sample_timer;
41 std::string start_transaction_message_id;
42 std::string stop_transaction_message_id;
43 std::shared_ptr<StampedEnergyWh> stop_energy_wh;
44 std::mutex meter_values_mutex;
45 std::vector<MeterValue> meter_values;
46
47public:
50 Transaction(const int32_t transaction_id, const int32_t& connector, const std::string& session_id,
51 const CiString<20>& id_token, const double meter_start, std::optional<int32_t> reservation_id,
52 const ocpp::DateTime& timestamp, std::unique_ptr<Everest::SteadyTimer> meter_values_sample_timer);
53
56 std::shared_ptr<StampedEnergyWh> get_start_energy_wh();
57
60 void add_stop_energy_wh(std::shared_ptr<StampedEnergyWh> stop_energy_wh);
61
64 std::shared_ptr<StampedEnergyWh> get_stop_energy_wh();
65
68 std::optional<int32_t> get_reservation_id();
69
72 int32_t get_connector();
73
77
79 void add_meter_value(MeterValue meter_value);
80
83 std::vector<MeterValue> get_meter_values();
84
87 bool change_meter_values_sample_interval(int32_t interval);
88
91
94 std::optional<int32_t> get_transaction_id();
95
98
101 std::string get_session_id();
102
104 void set_start_transaction_message_id(const std::string& message_id);
105
108
110 void set_stop_transaction_message_id(const std::string& message_id);
111
114
116 void set_transaction_id(int32_t transaction_id);
117
120 std::vector<TransactionData> get_transaction_data();
121
123 void stop();
124
127 bool is_active();
128
130 bool is_finished();
131
134 void set_finished();
135
139
144};
145
148private:
149 std::mutex active_transactions_mutex;
150 // size is equal to the number of connectors
151 int32_t number_of_connectors;
152
153 std::vector<std::shared_ptr<Transaction>> active_transactions;
154 // size does not depend on the number of connectors
155 std::vector<std::shared_ptr<Transaction>> stopped_transactions;
156
157 std::mt19937 gen;
158 std::uniform_int_distribution<int32_t> distr;
159
160public:
162 explicit TransactionHandler(int32_t number_of_connectors);
163
166
168 void add_transaction(std::shared_ptr<Transaction> transaction);
169
171 void add_stopped_transaction(int32_t connector);
172
175 bool remove_active_transaction(int32_t connector);
176
178 void erase_stopped_transaction(std::string stop_transaction_message_id);
179
182 std::shared_ptr<Transaction> get_transaction(int32_t connector);
183
187 std::shared_ptr<Transaction> get_transaction(const std::string& start_transaction_message_id);
188
194 std::shared_ptr<Transaction> get_transaction_from_id_tag(const std::string& id_tag);
195
198 int32_t get_connector_from_transaction_id(int32_t transaction_id);
199
201 void add_meter_value(int32_t connector, const MeterValue& meter_value);
202
205 void change_meter_values_sample_intervals(int32_t interval);
206
207 // \brief Provides the IdTag that was associated with the transaction with the provided
210 std::optional<CiString<20>> get_authorized_id_tag(const std::string& stop_transaction_message_id);
211
214 bool transaction_active(int32_t connector);
215};
216
217} // namespace v16
218} // namespace ocpp
219
220#endif // OCPP_V16_TRANSACTION_HPP
Contains a DateTime implementation that can parse and create RFC 3339 compatible strings.
Definition: types.hpp:109
Contains transactions for all available connectors and manages access to these transactions.
Definition: transaction.hpp:147
std::optional< CiString< 20 > > get_authorized_id_tag(const std::string &stop_transaction_message_id)
Definition: transaction.cpp:264
void erase_stopped_transaction(std::string stop_transaction_message_id)
Erases a transaction with the provided stop_transaction_message_id.
Definition: transaction.cpp:184
void add_transaction(std::shared_ptr< Transaction > transaction)
Adds the given transaction the vector of transactions.
Definition: transaction.cpp:163
bool remove_active_transaction(int32_t connector)
Removes a transaction from the provided connector.
Definition: transaction.cpp:172
void add_meter_value(int32_t connector, const MeterValue &meter_value)
Adds a clock aligned meter_value to the transaction on the provided connector.
Definition: transaction.cpp:247
int32_t get_negative_random_transaction_id()
Returns a negative random transaction_id.
Definition: transaction.cpp:159
TransactionHandler(int32_t number_of_connectors)
Creates and manages transactions for the provided number_of_connectors.
Definition: transaction.cpp:150
std::shared_ptr< Transaction > get_transaction_from_id_tag(const std::string &id_tag)
Returns the transaction associated with the given id tag.
Definition: transaction.cpp:218
void change_meter_values_sample_intervals(int32_t interval)
Modifies the sample interval of the meter values sample timer on all connectors. The provided interva...
Definition: transaction.cpp:255
int32_t get_connector_from_transaction_id(int32_t transaction_id)
Provides the connector on which a transaction with the given transaction_id is running.
Definition: transaction.cpp:233
bool transaction_active(int32_t connector)
Indicates if there is an active transaction at the proveded connector.
Definition: transaction.cpp:273
std::shared_ptr< Transaction > get_transaction(int32_t connector)
Returns the transaction associated with the transaction at the provided connector.
Definition: transaction.cpp:193
void add_stopped_transaction(int32_t connector)
Adds the transaction at the connector to the vector of stopped transactions.
Definition: transaction.cpp:168
Contains all transaction related data, such as the ID and power meter values.
Definition: transaction.hpp:28
std::optional< int32_t > get_reservation_id()
Provides the reservation id of the transaction if present.
Definition: transaction.cpp:146
void set_has_signed_meter_values()
Sets the has_signed_meter_value flag for this transaction, this function is called from on_transactio...
Definition: transaction.cpp:133
std::string get_stop_transaction_message_id()
Provides the stop transaction message id.
Definition: transaction.cpp:86
bool is_active()
Indicates if the transaction is active. Active means that the transaction for this session is not nul...
Definition: transaction.cpp:112
void add_meter_value(MeterValue meter_value)
Adds the provided meter_value to a chronological list of powermeter values.
Definition: transaction.cpp:38
bool is_finished()
Indicates if a StopTransaction.req for this transaction has already been pushed to the message queue.
Definition: transaction.cpp:116
int32_t get_internal_transaction_id()
Returns the internal transaction id.
Definition: transaction.cpp:66
std::optional< int32_t > get_transaction_id()
Provides the id of this transaction.
Definition: transaction.cpp:62
std::string get_start_transaction_message_id()
Provides the start transaction message id.
Definition: transaction.cpp:78
void set_finished()
Sets the finished flag for this transaction. This is done when a StopTransaction.req has been pushed ...
Definition: transaction.cpp:120
std::string get_session_id()
Provides the id of this session.
Definition: transaction.cpp:70
void stop()
Marks the transaction as stopped/inactive.
Definition: transaction.cpp:105
int32_t get_connector()
Provides the connector of this transaction.
Definition: transaction.cpp:30
Transaction(const int32_t transaction_id, const int32_t &connector, const std::string &session_id, const CiString< 20 > &id_token, const double meter_start, std::optional< int32_t > reservation_id, const ocpp::DateTime &timestamp, std::unique_ptr< Everest::SteadyTimer > meter_values_sample_timer)
Creates a new Transaction object, taking ownership of the provided meter_values_sample_timer on the p...
Definition: transaction.cpp:12
void set_start_transaction_message_id(const std::string &message_id)
Sets the start transaction message id using the provides message_id.
Definition: transaction.cpp:74
bool get_has_signed_meter_values()
Indicates if this transaction has signed meter values or not, this function is called from on_transac...
Definition: transaction.cpp:137
CiString< 20 > get_id_tag()
Provides the authorized id tag of this Transaction.
Definition: transaction.cpp:34
void set_transaction_id(int32_t transaction_id)
Sets the transaction id.
Definition: transaction.cpp:90
bool change_meter_values_sample_interval(int32_t interval)
Changes the sample interval of the powermeter values sampling timer.
Definition: transaction.cpp:57
void set_stop_transaction_message_id(const std::string &message_id)
Sets the stop transaction message id using the provides message_id.
Definition: transaction.cpp:82
std::vector< TransactionData > get_transaction_data()
Provides all recorded sampled and clock aligned powermeter values.
Definition: transaction.cpp:94
void add_clock_aligned_meter_value(MeterValue meter_value)
Adds the provided meter_value to a chronological list of clock aligned powermeter values.
void add_stop_energy_wh(std::shared_ptr< StampedEnergyWh > stop_energy_wh)
Adds the energy in Wh stop_energy_wh to the transaction. This also stops the collection of further me...
Definition: transaction.cpp:128
std::shared_ptr< StampedEnergyWh > get_stop_energy_wh()
Provides the energy in Wh at the end of the transaction.
Definition: transaction.cpp:142
std::shared_ptr< StampedEnergyWh > get_start_energy_wh()
Provides the energy in Wh at the start of the transaction.
Definition: transaction.cpp:124
std::vector< MeterValue > get_meter_values()
Provides all recorded powermeter values.
Definition: transaction.cpp:52
Definition: ocpp_types.hpp:132
A structure that contains a energy value in Wh that can be used for start/stop energy values and a co...
Definition: transaction.hpp:18
ocpp::DateTime timestamp
A timestamp associated with the energy value.
Definition: transaction.hpp:19
double energy_Wh
The energy value in Wh.
Definition: transaction.hpp:20