-
Notifications
You must be signed in to change notification settings - Fork 45
/
smart_charging.cpp
433 lines (375 loc) · 20 KB
/
smart_charging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
#include "ocpp/common/types.hpp"
#include "ocpp/v16/ocpp_enums.hpp"
#include <ocpp/v16/profile.hpp>
#include <ocpp/v16/smart_charging.hpp>
using namespace std::chrono;
using QueryExecutionException = ocpp::common::QueryExecutionException;
namespace ocpp {
namespace v16 {
bool validate_schedule(const ChargingSchedule& schedule, const int charging_schedule_max_periods,
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units) {
if (schedule.chargingSchedulePeriod.size() > (size_t)charging_schedule_max_periods) {
EVLOG_warning << "INVALID SCHEDULE - Number of chargingSchedulePeriod(s) is greater than configured "
"ChargingScheduleMaxPeriods of "
<< charging_schedule_max_periods;
return false;
}
if (std::find(charging_schedule_allowed_charging_rate_units.begin(),
charging_schedule_allowed_charging_rate_units.end(),
schedule.chargingRateUnit) == charging_schedule_allowed_charging_rate_units.end()) {
EVLOG_warning << "INVALID SCHEDULE - ChargingRateUnit not supported: " << schedule.chargingRateUnit;
return false;
}
for (const auto& period : schedule.chargingSchedulePeriod) {
if (period.numberPhases &&
(period.numberPhases.value() <= 0 or period.numberPhases.value() > DEFAULT_AND_MAX_NUMBER_PHASES)) {
EVLOG_warning << "INVALID SCHEDULE - Invalid number of phases: " << period.numberPhases.value();
return false;
} else if (period.limit < 0) {
EVLOG_warning << "INVALID SCHEDULE - Invalid limit: " << period.limit;
return false;
}
}
return true;
}
SmartChargingHandler::SmartChargingHandler(std::map<int32_t, std::shared_ptr<Connector>>& connectors,
std::shared_ptr<DatabaseHandler> database_handler,
const bool allow_charging_profile_without_start_schedule) :
connectors(connectors),
database_handler(database_handler),
allow_charging_profile_without_start_schedule(allow_charging_profile_without_start_schedule) {
this->clear_profiles_timer = std::make_unique<Everest::SteadyTimer>();
this->clear_profiles_timer->interval([this]() { this->clear_expired_profiles(); }, hours(HOURS_PER_DAY));
}
void SmartChargingHandler::clear_expired_profiles() {
EVLOG_debug << "Scanning all installed profiles and clearing expired profiles";
const auto now = date::utc_clock::now();
std::lock_guard<std::mutex> lk(this->charge_point_max_profiles_map_mutex);
for (auto it = this->stack_level_charge_point_max_profiles_map.cbegin();
it != this->stack_level_charge_point_max_profiles_map.cend();) {
const auto& validTo = it->second.validTo;
if (validTo.has_value() and validTo.value().to_time_point() < now) {
this->stack_level_charge_point_max_profiles_map.erase(it++);
this->database_handler->delete_charging_profile(it->second.chargingProfileId);
} else {
++it;
}
}
}
int SmartChargingHandler::get_number_installed_profiles() {
int number = 0;
std::lock_guard<std::mutex> lk_cp(this->charge_point_max_profiles_map_mutex);
std::lock_guard<std::mutex> lk_txd(this->tx_default_profiles_map_mutex);
std::lock_guard<std::mutex> lk_tx(this->tx_profiles_map_mutex);
number += this->stack_level_charge_point_max_profiles_map.size();
for (const auto& [connector_id, connector] : this->connectors) {
number += connector->stack_level_tx_default_profiles_map.size();
number += connector->stack_level_tx_profiles_map.size();
}
return number;
}
ChargingSchedule SmartChargingHandler::calculate_composite_schedule(
const std::vector<ChargingProfile>& valid_profiles, const ocpp::DateTime& start_time,
const ocpp::DateTime& end_time, const int connector_id, std::optional<ChargingRateUnit> charging_rate_unit) {
const auto enhanced_composite_schedule = this->calculate_enhanced_composite_schedule(
valid_profiles, start_time, end_time, connector_id, charging_rate_unit);
ChargingSchedule composite_schedule;
composite_schedule.chargingRateUnit = enhanced_composite_schedule.chargingRateUnit;
composite_schedule.duration = enhanced_composite_schedule.duration;
composite_schedule.startSchedule = enhanced_composite_schedule.startSchedule;
composite_schedule.minChargingRate = enhanced_composite_schedule.minChargingRate;
for (const auto enhanced_period : enhanced_composite_schedule.chargingSchedulePeriod) {
ChargingSchedulePeriod period;
period.startPeriod = enhanced_period.startPeriod;
period.limit = enhanced_period.limit;
period.numberPhases = enhanced_period.numberPhases;
composite_schedule.chargingSchedulePeriod.push_back(period);
}
return composite_schedule;
}
EnhancedChargingSchedule SmartChargingHandler::calculate_enhanced_composite_schedule(
const std::vector<ChargingProfile>& valid_profiles, const ocpp::DateTime& start_time,
const ocpp::DateTime& end_time, const int connector_id, std::optional<ChargingRateUnit> charging_rate_unit) {
std::optional<ocpp::DateTime> session_start{};
if (const auto& itt = connectors.find(connector_id); itt != connectors.end()) {
// connector exists!
if (itt->second->transaction) {
session_start.emplace(ocpp::DateTime(
floor<seconds>(itt->second->transaction->get_start_energy_wh()->timestamp.to_time_point())));
}
}
std::vector<period_entry_t> charge_point_max{};
std::vector<period_entry_t> tx_default{};
std::vector<period_entry_t> tx{};
for (const auto& profile : valid_profiles) {
std::vector<period_entry_t> periods{};
periods = ocpp::v16::calculate_profile(start_time, end_time, session_start, profile);
switch (profile.chargingProfilePurpose) {
case ChargingProfilePurposeType::ChargePointMaxProfile:
charge_point_max.insert(charge_point_max.end(), periods.begin(), periods.end());
break;
case ChargingProfilePurposeType::TxDefaultProfile:
tx_default.insert(tx_default.end(), periods.begin(), periods.end());
break;
case ChargingProfilePurposeType::TxProfile:
tx.insert(tx.end(), periods.begin(), periods.end());
break;
default:
break;
}
}
auto composite_charge_point_max =
ocpp::v16::calculate_composite_schedule(charge_point_max, start_time, end_time, charging_rate_unit);
auto composite_tx_default =
ocpp::v16::calculate_composite_schedule(tx_default, start_time, end_time, charging_rate_unit);
auto composite_tx = ocpp::v16::calculate_composite_schedule(tx, start_time, end_time, charging_rate_unit);
return ocpp::v16::calculate_composite_schedule(composite_charge_point_max, composite_tx_default, composite_tx);
}
bool SmartChargingHandler::validate_profile(
ChargingProfile& profile, const int connector_id, bool ignore_no_transaction, const int profile_max_stack_level,
const int max_charging_profiles_installed, const int charging_schedule_max_periods,
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units) {
if ((size_t)connector_id > this->connectors.size() or connector_id < 0 or profile.stackLevel < 0 or
profile.stackLevel > profile_max_stack_level) {
EVLOG_warning << "INVALID PROFILE - connector_id invalid or invalid stack level";
return false;
}
if (profile.chargingProfileKind == ChargingProfileKindType::Absolute && !profile.chargingSchedule.startSchedule) {
EVLOG_warning << "INVALID PROFILE - Absolute Profile Kind with no given startSchedule";
if (this->allow_charging_profile_without_start_schedule) {
EVLOG_warning << "Allowing profile because AllowChargingProfileWithoutStartSchedule is configured";
profile.chargingSchedule.startSchedule = ocpp::DateTime();
} else {
return false;
}
}
if (this->get_number_installed_profiles() >= max_charging_profiles_installed) {
EVLOG_warning << "Maximum amount of profiles are installed at the charging point";
return false;
}
if (!validate_schedule(profile.chargingSchedule, charging_schedule_max_periods,
charging_schedule_allowed_charging_rate_units)) {
return false;
}
if (profile.chargingProfileKind == ChargingProfileKindType::Recurring) {
if (!profile.recurrencyKind) {
EVLOG_warning << "INVALID PROFILE - Recurring Profile Kind with no given RecurrencyKind";
return false;
}
if (!profile.chargingSchedule.startSchedule) {
EVLOG_warning << "INVALID PROFILE - Recurring Profile Kind with no startSchedule";
if (this->allow_charging_profile_without_start_schedule) {
EVLOG_warning << "Allowing profile because AllowChargingProfileWithoutStartSchedule is configured";
profile.chargingSchedule.startSchedule = ocpp::DateTime();
} else {
return false;
}
}
if (profile.chargingSchedule.duration) {
int max_recurrency_duration;
if (profile.recurrencyKind == RecurrencyKindType::Daily) {
max_recurrency_duration = SECONDS_PER_DAY;
} else {
max_recurrency_duration = SECONDS_PER_DAY * DAYS_PER_WEEK;
}
if (profile.chargingSchedule.duration > max_recurrency_duration) {
EVLOG_warning
<< "Given duration of Recurring profile was > than max_recurrency_duration. Setting duration of "
"schedule to max_currency_duration";
profile.chargingSchedule.duration = max_recurrency_duration;
}
}
}
if (profile.chargingProfilePurpose == ChargingProfilePurposeType::ChargePointMaxProfile) {
if (connector_id == 0 and profile.chargingProfileKind != ChargingProfileKindType::Relative) {
return true;
} else {
EVLOG_warning
<< "INVALID PROFILE - connector_id != 0 with purpose ChargePointMaxProfile or kind is Relative";
return false;
}
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxDefaultProfile) {
return true;
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxProfile) {
if (connector_id == 0) {
EVLOG_warning << "INVALID PROFILE - connector_id is 0";
return false;
}
const auto& connector = this->connectors.at(connector_id);
if (connector->transaction == nullptr && !ignore_no_transaction) {
EVLOG_warning << "INVALID PROFILE - No active transaction at this connector";
return false;
}
if (profile.transactionId.has_value()) {
if (connector->transaction == nullptr) {
EVLOG_warning << "INVALID PROFILE - profile.transaction_id is present but no transaction is active at "
"this connector";
return false;
}
if (connector->transaction->get_transaction_id() != profile.transactionId) {
EVLOG_warning << "INVALID PROFILE - transaction_id doesn't match for purpose TxProfile";
return false;
}
}
}
return true;
}
void SmartChargingHandler::add_charge_point_max_profile(const ChargingProfile& profile) {
std::lock_guard<std::mutex> lk(this->charge_point_max_profiles_map_mutex);
this->stack_level_charge_point_max_profiles_map[profile.stackLevel] = profile;
try {
this->database_handler->insert_or_update_charging_profile(0, profile);
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not store ChargePointMaxProfile in the database: " << e.what();
}
}
void SmartChargingHandler::add_tx_default_profile(const ChargingProfile& profile, const int connector_id) {
std::lock_guard<std::mutex> lk(this->tx_default_profiles_map_mutex);
if (connector_id == 0) {
for (size_t id = 1; id <= this->connectors.size() - 1; id++) {
this->connectors.at(id)->stack_level_tx_default_profiles_map[profile.stackLevel] = profile;
try {
this->database_handler->insert_or_update_charging_profile(connector_id, profile);
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not store TxDefaultProfile in the database: " << e.what();
}
}
} else {
this->connectors.at(connector_id)->stack_level_tx_default_profiles_map[profile.stackLevel] = profile;
try {
this->database_handler->insert_or_update_charging_profile(connector_id, profile);
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not store TxDefaultProfile in the database: " << e.what();
}
}
}
void SmartChargingHandler::add_tx_profile(const ChargingProfile& profile, const int connector_id) {
std::lock_guard<std::mutex> lk(this->tx_profiles_map_mutex);
this->connectors.at(connector_id)->stack_level_tx_profiles_map[profile.stackLevel] = profile;
try {
this->database_handler->insert_or_update_charging_profile(connector_id, profile);
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not store TxProfile in the database: " << e.what();
}
}
bool SmartChargingHandler::clear_profiles(std::map<int32_t, ChargingProfile>& stack_level_profiles_map,
std::optional<int> profile_id_opt, std::optional<int> connector_id_opt,
const int connector_id, std::optional<int> stack_level_opt,
std::optional<ChargingProfilePurposeType> charging_profile_purpose_opt,
bool check_id_only) {
bool erased_at_least_one = false;
for (auto it = stack_level_profiles_map.cbegin(); it != stack_level_profiles_map.cend();) {
if (profile_id_opt && it->second.chargingProfileId == profile_id_opt.value()) {
EVLOG_info << "Clearing ChargingProfile with id: " << it->second.chargingProfileId;
try {
this->database_handler->delete_charging_profile(it->second.chargingProfileId);
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not delete ChargingProfile from the database: " << e.what();
}
stack_level_profiles_map.erase(it++);
erased_at_least_one = true;
} else if (!check_id_only and (!connector_id_opt or connector_id_opt.value() == connector_id) and
(!stack_level_opt or stack_level_opt.value() == it->first) and
(!charging_profile_purpose_opt or
charging_profile_purpose_opt.value() == it->second.chargingProfilePurpose)) {
EVLOG_info << "Clearing ChargingProfile with id: " << it->second.chargingProfileId;
try {
this->database_handler->delete_charging_profile(it->second.chargingProfileId);
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not delete ChargingProfile from the database: " << e.what();
}
stack_level_profiles_map.erase(it++);
erased_at_least_one = true;
} else {
++it;
}
}
return erased_at_least_one;
}
bool SmartChargingHandler::clear_all_profiles_with_filter(
std::optional<int> profile_id_opt, std::optional<int> connector_id_opt, std::optional<int> stack_level_opt,
std::optional<ChargingProfilePurposeType> charging_profile_purpose_opt, bool check_id_only) {
// for ChargePointMaxProfile
auto erased_charge_point_max_profile =
this->clear_profiles(this->stack_level_charge_point_max_profiles_map, profile_id_opt, connector_id_opt, 0,
stack_level_opt, charging_profile_purpose_opt, check_id_only);
bool erased_at_least_one_tx_profile = false;
for (auto& [connector_id, connector] : this->connectors) {
// for TxDefaultProfiles
auto tmp_erased_tx_default_profile =
this->clear_profiles(connector->stack_level_tx_default_profiles_map, profile_id_opt, connector_id_opt,
connector_id, stack_level_opt, charging_profile_purpose_opt, check_id_only);
// for TxProfiles
auto tmp_erased_tx_profile =
this->clear_profiles(connector->stack_level_tx_profiles_map, profile_id_opt, connector_id_opt, connector_id,
stack_level_opt, charging_profile_purpose_opt, check_id_only);
if (!erased_at_least_one_tx_profile and (tmp_erased_tx_profile or tmp_erased_tx_default_profile)) {
erased_at_least_one_tx_profile = true;
}
}
return erased_charge_point_max_profile or erased_at_least_one_tx_profile;
}
void SmartChargingHandler::clear_all_profiles() {
EVLOG_info << "Clearing all charging profiles";
std::lock_guard<std::mutex> lk_cp(this->charge_point_max_profiles_map_mutex);
std::lock_guard<std::mutex> lk_txd(this->tx_default_profiles_map_mutex);
std::lock_guard<std::mutex> lk_tx(this->tx_profiles_map_mutex);
this->stack_level_charge_point_max_profiles_map.clear();
for (auto& [connector_id, connector] : this->connectors) {
connector->stack_level_tx_default_profiles_map.clear();
connector->stack_level_tx_profiles_map.clear();
}
try {
this->database_handler->delete_charging_profiles();
} catch (const QueryExecutionException& e) {
EVLOG_warning << "Could not delete ChargingProfile from the database: " << e.what();
}
}
std::vector<ChargingProfile> SmartChargingHandler::get_valid_profiles(const ocpp::DateTime& start_time,
const ocpp::DateTime& end_time,
const int connector_id) {
std::vector<ChargingProfile> valid_profiles;
{
std::lock_guard<std::mutex> lk(charge_point_max_profiles_map_mutex);
for (const auto& [stack_level, profile] : stack_level_charge_point_max_profiles_map) {
valid_profiles.push_back(profile);
}
}
if (connector_id > 0) {
// tx_default profiles added to connector 0 are copied to every connector
std::optional<int32_t> transactionId;
const auto& itt = connectors.find(connector_id);
if (itt != connectors.end()) {
// connector exists!
if (itt->second->transaction) {
transactionId = itt->second->transaction->get_transaction_id();
}
std::lock_guard<std::mutex> lk_txd(tx_default_profiles_map_mutex);
std::lock_guard<std::mutex> lk_tx(tx_profiles_map_mutex);
for (const auto& [stack_level, profile] : itt->second->stack_level_tx_profiles_map) {
// only include profiles that match the transactionId (when there is one)
bool b_add{false};
if (profile.transactionId) {
if ((transactionId) && (transactionId.value() == profile.transactionId.value())) {
// there is a session/transaction in progress and the ID matches the profile
b_add = true;
}
} else {
// profile doesn't have a transaction ID specified
b_add = true;
}
if (b_add) {
valid_profiles.push_back(profile);
}
}
for (const auto& [stack_level, profile] : itt->second->stack_level_tx_default_profiles_map) {
valid_profiles.push_back(profile);
}
}
}
return valid_profiles;
}
} // namespace v16
} // namespace ocpp