ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
database_handler.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_V16_DATABASE_HANDLER_HPP
4#define OCPP_V16_DATABASE_HANDLER_HPP
5
6#include "sqlite3.h"
7#include <fstream>
8#include <iostream>
9
10#include <ocpp/common/database/database_handler_common.hpp>
11#include <ocpp/common/schemas.hpp>
12#include <ocpp/common/support_older_cpp_versions.hpp>
13#include <ocpp/common/types.hpp>
14#include <ocpp/v16/ocpp_types.hpp>
15#include <ocpp/v16/types.hpp>
16
17namespace ocpp {
18namespace v16 {
19
22 std::string session_id;
23 int32_t connector;
24 std::string id_tag_start;
25 std::string time_start;
26 int32_t meter_start;
27 int32_t transaction_id;
28 bool csms_ack;
29 int32_t meter_last;
30 std::string meter_last_time;
31 std::string last_update;
32 std::string start_transaction_message_id;
33 std::optional<std::string> stop_transaction_message_id;
34 std::optional<int32_t> reservation_id = std::nullopt;
35 std::optional<std::string> parent_id_tag = std::nullopt;
36 std::optional<int32_t> meter_stop = std::nullopt;
37 std::optional<std::string> time_end = std::nullopt;
38 std::optional<std::string> id_tag_end = std::nullopt;
39 std::optional<std::string> stop_reason = std::nullopt;
40};
41
44private:
45 const int32_t number_of_connectors;
46
47 // Runs initialization script and initializes the CONNECTORS and AUTH_LIST_VERSION table.
48 void init_sql() override;
49 void init_connector_table();
50
51public:
52 DatabaseHandler(std::unique_ptr<common::DatabaseConnectionInterface> database,
53 const fs::path& sql_migration_files_path, int32_t number_of_connectors);
54
55 // transactions
57 void insert_transaction(const std::string& session_id, const int32_t transaction_id, const int32_t connector,
58 const std::string& id_tag_start, const std::string& time_start, const int32_t meter_start,
59 const bool csms_ack, const std::optional<int32_t> reservation_id,
60 const std::string& start_transaction_message_id);
61
63 void update_transaction(const std::string& session_id, int32_t transaction_id,
64 std::optional<CiString<20>> parent_id_tag = std::nullopt);
65
67 void update_transaction(const std::string& session_id, int32_t meter_stop, const std::string& time_end,
68 std::optional<CiString<20>> id_tag_end, std::optional<v16::Reason> stop_reason,
69 const std::string& stop_transaction_message_id);
70
73 void update_transaction_csms_ack(const int32_t transaction_id);
74
77 void update_start_transaction_message_id(const std::string& session_id,
78 const std::string& start_transaction_message_id);
79
82 void update_transaction_meter_value(const std::string& session_id, const int32_t value,
83 const std::string& timestamp);
84
87 std::vector<TransactionEntry> get_transactions(bool filter_incomplete = false);
88
89 // authorization cache
91 void insert_or_update_authorization_cache_entry(const CiString<20>& id_tag, const v16::IdTagInfo& id_tag_info);
92
94 std::optional<v16::IdTagInfo> get_authorization_cache_entry(const CiString<20>& id_tag);
95
98
99 // connector availability
101 void insert_or_update_connector_availability(int32_t connector, const v16::AvailabilityType& availability_type);
102
104 void insert_or_update_connector_availability(const std::vector<int32_t>& connectors,
105 const v16::AvailabilityType& availability_type);
106
108 v16::AvailabilityType get_connector_availability(int32_t connector);
109
111 std::map<int32_t, v16::AvailabilityType> get_connector_availability();
112
113 // local auth list management
114
116 void insert_or_ignore_local_list_version(int32_t version);
117
119 void insert_or_update_local_list_version(int32_t version);
120
122 int32_t get_local_list_version();
123
126
128 void insert_or_update_local_authorization_list(std::vector<v16::LocalAuthorizationList> local_authorization_list);
129
131 void delete_local_authorization_list_entry(const std::string& id_tag);
132
134 std::optional<v16::IdTagInfo> get_local_authorization_list_entry(const CiString<20>& id_tag);
135
138
141
143 virtual void insert_or_update_charging_profile(const int connector_id, const v16::ChargingProfile& profile);
144
146 virtual void delete_charging_profile(const int profile_id);
147
150
152 std::vector<v16::ChargingProfile> get_charging_profiles();
153
155 int get_connector_id(const int profile_id);
156
158 void insert_ocsp_update();
159
161 std::optional<DateTime> get_last_ocsp_update();
162};
163
164} // namespace v16
165} // namespace ocpp
166
167#endif // OCPP_COMMON_DATABASE_HANDLER_HPP
Definition: database_handler_common.hpp:24
This class handles the connection and operations of the SQLite database.
Definition: database_handler.hpp:43
void insert_transaction(const std::string &session_id, const int32_t transaction_id, const int32_t connector, const std::string &id_tag_start, const std::string &time_start, const int32_t meter_start, const bool csms_ack, const std::optional< int32_t > reservation_id, const std::string &start_transaction_message_id)
Inserts a transaction with the given parameter to the TRANSACTIONS table.
Definition: database_handler.cpp:45
void insert_or_ignore_local_list_version(int32_t version)
Inserts or ignores the given version in the AUTH_LIST_VERSION table.
Definition: database_handler.cpp:363
void insert_or_update_local_authorization_list(std::vector< v16::LocalAuthorizationList > local_authorization_list)
Inserts or updates a local authorization list entries local_authorization_list to the AUTH_LIST table...
Definition: database_handler.cpp:423
void insert_or_update_connector_availability(int32_t connector, const v16::AvailabilityType &availability_type)
Inserts or updates the given availability_type of the given connector to the CONNECTORS table.
Definition: database_handler.cpp:302
void clear_authorization_cache()
Deletes all entries of the AUTH_CACHE table.
Definition: database_handler.cpp:295
std::map< int32_t, v16::AvailabilityType > get_connector_availability()
Returns a map of all connectors and its AvailabilityTypes of the CONNECTORS table.
Definition: database_handler.cpp:345
int32_t get_local_list_version()
Returns the version in the AUTH_LIST_VERSION table.
Definition: database_handler.cpp:384
std::vector< TransactionEntry > get_transactions(bool filter_incomplete=false)
Returns a list of all transactions in the database. If filter_complete is true, only incomplete trans...
Definition: database_handler.cpp:169
std::optional< DateTime > get_last_ocsp_update()
Gets the only entry in the OCSP_REQUEST table.
Definition: database_handler.cpp:600
void delete_charging_profiles()
Deletes all profiles from table CHARGING_PROFILES.
Definition: database_handler.cpp:544
int get_connector_id(const int profile_id)
Returns the connector_id of the given profile_id.
Definition: database_handler.cpp:569
virtual void insert_or_update_charging_profile(const int connector_id, const v16::ChargingProfile &profile)
Inserts or updates the given profile to CHARGING_PROFILES table.
Definition: database_handler.cpp:517
std::vector< v16::ChargingProfile > get_charging_profiles()
Returns a list of all charging profiles in the CHARGING_PROFILES table.
Definition: database_handler.cpp:551
void clear_local_authorization_list()
Deletes all entries of the AUTH_LIST table.
Definition: database_handler.cpp:499
std::optional< v16::IdTagInfo > get_local_authorization_list_entry(const CiString< 20 > &id_tag)
Returns the IdTagInfo of the given id_tag if it exists in the AUTH_LIST table, else std::nullopt.
Definition: database_handler.cpp:455
int32_t get_local_authorization_list_number_of_entries()
Get the number of entries currently in the authorization list.
Definition: database_handler.cpp:506
void update_transaction_csms_ack(const int32_t transaction_id)
Updates the CSMS_ACK column for the transaction with the given transaction_id in the TRANSACTIONS tab...
Definition: database_handler.cpp:126
void insert_or_update_authorization_cache_entry(const CiString< 20 > &id_tag, const v16::IdTagInfo &id_tag_info)
Inserts or updates an authorization cache entry to the AUTH_CACHE table.
Definition: database_handler.cpp:227
void update_transaction(const std::string &session_id, int32_t transaction_id, std::optional< CiString< 20 > > parent_id_tag=std::nullopt)
Updates the given parameters for the transaction with the given session_id in the TRANSACTIONS table.
Definition: database_handler.cpp:80
void insert_or_update_local_list_version(int32_t version)
Inserts or updates the given version in the AUTH_LIST_VERSION table.
Definition: database_handler.cpp:374
void insert_ocsp_update()
Updates the ocsp entry in the OCSP_REQUEST table.
Definition: database_handler.cpp:588
void insert_or_update_local_authorization_list_entry(const CiString< 20 > &id_tag, const v16::IdTagInfo &id_tag_info)
Inserts or updates a local authorization list entry to the AUTH_LIST table.
Definition: database_handler.cpp:400
virtual void delete_charging_profile(const int profile_id)
Deletes the profile with the given profile_id.
Definition: database_handler.cpp:534
void delete_local_authorization_list_entry(const std::string &id_tag)
Deletes the authorization list entry with the given id_tag.
Definition: database_handler.cpp:445
void update_transaction_meter_value(const std::string &session_id, const int32_t value, const std::string &timestamp)
Updates the METER_LAST and METER_LAST_TIME column for the transaction with the given session_id in th...
Definition: database_handler.cpp:153
std::optional< v16::IdTagInfo > get_authorization_cache_entry(const CiString< 20 > &id_tag)
Returns the IdTagInfo of the given id_tag if it exists in the AUTH_CACHE table, else std::nullopt.
Definition: database_handler.cpp:251
void update_start_transaction_message_id(const std::string &session_id, const std::string &start_transaction_message_id)
Updates the START_TRANSACTION_MESSAGE_ID column for the transaction with the given session_id in the ...
Definition: database_handler.cpp:139
Definition: ocpp_types.hpp:146
Definition: ocpp_types.hpp:20
Struct that contains all attributes of a transaction entry in the database.
Definition: database_handler.hpp:21