From 703530a67d0a0ad2d2343df834e491f5e52fc31b Mon Sep 17 00:00:00 2001 From: pietfried Date: Tue, 2 Jan 2024 16:44:45 +0100 Subject: [PATCH] * changed type of id_tag of TransactionFinished and StopTransactionRequest types from string to ProvidedIdToken in order to be able to use the additional data * refactored Auth, OCPP, OCPP201 and EvseManager module by adressing the type changes Signed-off-by: pietfried --- modules/Auth/lib/AuthHandler.cpp | 4 +- modules/EvseManager/Charger.cpp | 8 ++-- modules/EvseManager/Charger.hpp | 5 +- modules/EvseManager/evse/evse_managerImpl.cpp | 6 +-- modules/OCPP/OCPP.cpp | 4 +- modules/OCPP201/OCPP201.cpp | 48 +++++++++++++++---- types/evse_manager.yaml | 10 ++-- 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/modules/Auth/lib/AuthHandler.cpp b/modules/Auth/lib/AuthHandler.cpp index ad1b04f95..b03ae0d7a 100644 --- a/modules/Auth/lib/AuthHandler.cpp +++ b/modules/Auth/lib/AuthHandler.cpp @@ -111,7 +111,7 @@ TokenHandlingResult AuthHandler::handle_token(const ProvidedIdToken& provided_to if (connector_used_for_transaction != -1) { StopTransactionRequest req; req.reason = StopTransactionReason::Local; - req.id_tag.emplace(provided_token.id_token); + req.id_tag.emplace(provided_token); this->stop_transaction_callback(this->connectors.at(connector_used_for_transaction)->evse_index, req); EVLOG_info << "Transaction was stopped because id_token was used for transaction"; return TokenHandlingResult::USED_TO_STOP_TRANSACTION; @@ -154,7 +154,7 @@ TokenHandlingResult AuthHandler::handle_token(const ProvidedIdToken& provided_to } else { StopTransactionRequest req; req.reason = StopTransactionReason::Local; - req.id_tag.emplace(provided_token.id_token); + req.id_tag.emplace(provided_token); this->stop_transaction_callback(this->connectors.at(connector_used_for_transaction)->evse_index, req); EVLOG_info << "Transaction was stopped because parent_id_token was used for transaction"; diff --git a/modules/EvseManager/Charger.cpp b/modules/EvseManager/Charger.cpp index 33177a3d9..d5cee4327 100644 --- a/modules/EvseManager/Charger.cpp +++ b/modules/EvseManager/Charger.cpp @@ -924,7 +924,7 @@ bool Charger::cancelTransaction(const types::evse_manager::StopTransactionReques transaction_active = false; last_stop_transaction_reason = request.reason; if (request.id_tag) { - stop_transaction_id_tag = request.id_tag.value(); + stop_transaction_id_token = request.id_tag.value(); } signalEvent(types::evse_manager::SessionEventEnum::ChargingFinished); signalEvent(types::evse_manager::SessionEventEnum::TransactionFinished); @@ -963,7 +963,7 @@ void Charger::stopSession() { void Charger::startTransaction() { std::lock_guard lock(stateMutex); - stop_transaction_id_tag.clear(); + stop_transaction_id_token.reset(); transaction_active = true; signalEvent(types::evse_manager::SessionEventEnum::TransactionStarted); } @@ -976,9 +976,9 @@ void Charger::stopTransaction() { signalEvent(types::evse_manager::SessionEventEnum::TransactionFinished); } -std::string Charger::getStopTransactionIdTag() { +std::optional Charger::getStopTransactionIdToken() { std::lock_guard lock(stateMutex); - return stop_transaction_id_tag; + return stop_transaction_id_token; } types::evse_manager::StopTransactionReason Charger::getTransactionFinishedReason() { diff --git a/modules/EvseManager/Charger.hpp b/modules/EvseManager/Charger.hpp index 032cdfccf..33c4aa171 100644 --- a/modules/EvseManager/Charger.hpp +++ b/modules/EvseManager/Charger.hpp @@ -88,6 +88,7 @@ class Charger { void Authorize(bool a, const types::authorization::ProvidedIdToken& token); bool DeAuthorize(); types::authorization::ProvidedIdToken getIdToken(); + std::optional getStopTransactionIdToken(); bool Authorized_PnC(); bool Authorized_EIM(); @@ -107,7 +108,6 @@ class Charger { bool cancelTransaction(const types::evse_manager::StopTransactionRequest& request); // cancel transaction ahead of time when car is still plugged - std::string getStopTransactionIdTag(); types::evse_manager::StopTransactionReason getTransactionFinishedReason(); // get reason for last finished event types::evse_manager::StartSessionReason getSessionStartedReason(); // get reason for last session start event @@ -214,7 +214,6 @@ class Charger { bool transactionActive(); bool transaction_active; bool session_active; - std::string stop_transaction_id_tag; types::evse_manager::StopTransactionReason last_stop_transaction_reason; types::evse_manager::StartSessionReason last_start_session_reason; @@ -270,6 +269,8 @@ class Charger { bool authorized_pnc; types::authorization::ProvidedIdToken id_token; + std::optional + stop_transaction_id_token; // only set in case transaction was stopped locally // AC or DC ChargeMode charge_mode{0}; diff --git a/modules/EvseManager/evse/evse_managerImpl.cpp b/modules/EvseManager/evse/evse_managerImpl.cpp index 3e445b55a..62cfaf9ea 100644 --- a/modules/EvseManager/evse/evse_managerImpl.cpp +++ b/modules/EvseManager/evse/evse_managerImpl.cpp @@ -218,12 +218,8 @@ void evse_managerImpl::ready() { transaction_finished.meter_value = mod->get_latest_powermeter_data_billing(); auto reason = mod->charger->getTransactionFinishedReason(); - const auto id_tag = mod->charger->getStopTransactionIdTag(); - transaction_finished.reason.emplace(reason); - if (!id_tag.empty()) { - transaction_finished.id_tag.emplace(id_tag); - } + transaction_finished.id_tag = mod->charger->getStopTransactionIdToken(); double energy_import = transaction_finished.meter_value.energy_Wh_import.total; diff --git a/modules/OCPP/OCPP.cpp b/modules/OCPP/OCPP.cpp index 37a305a7d..fbe53a04d 100644 --- a/modules/OCPP/OCPP.cpp +++ b/modules/OCPP/OCPP.cpp @@ -191,8 +191,8 @@ void OCPP::process_session_event(int32_t evse_id, const types::evse_manager::Ses types::evse_manager::stop_transaction_reason_to_string(transaction_finished.reason.value())); const auto signed_meter_value = transaction_finished.signed_meter_value; std::optional> id_tag_opt = std::nullopt; - if (transaction_finished.id_tag) { - id_tag_opt.emplace(ocpp::CiString<20>(transaction_finished.id_tag.value())); + if (transaction_finished.id_tag.has_value()) { + id_tag_opt.emplace(ocpp::CiString<20>(transaction_finished.id_tag.value().id_token)); } this->charge_point->on_transaction_stopped(ocpp_connector_id, session_event.uuid, reason, timestamp, energy_Wh_import, id_tag_opt, signed_meter_value); diff --git a/modules/OCPP201/OCPP201.cpp b/modules/OCPP201/OCPP201.cpp index 1b2165480..39911dde4 100644 --- a/modules/OCPP201/OCPP201.cpp +++ b/modules/OCPP201/OCPP201.cpp @@ -468,6 +468,38 @@ ocpp::v201::ReasonEnum get_reason(types::evse_manager::StopTransactionReason rea } } +ocpp::v201::IdTokenEnum get_id_token_enum(types::authorization::IdTokenType id_token_type) { + switch (id_token_type) { + case types::authorization::IdTokenType::Central: + return ocpp::v201::IdTokenEnum::Central; + case types::authorization::IdTokenType::eMAID: + return ocpp::v201::IdTokenEnum::eMAID; + case types::authorization::IdTokenType::MacAddress: + return ocpp::v201::IdTokenEnum::MacAddress; + case types::authorization::IdTokenType::ISO14443: + return ocpp::v201::IdTokenEnum::ISO14443; + case types::authorization::IdTokenType::ISO15693: + return ocpp::v201::IdTokenEnum::ISO15693; + case types::authorization::IdTokenType::KeyCode: + return ocpp::v201::IdTokenEnum::KeyCode; + case types::authorization::IdTokenType::Local: + return ocpp::v201::IdTokenEnum::Local; + case types::authorization::IdTokenType::NoAuthorization: + return ocpp::v201::IdTokenEnum::NoAuthorization; + default: + throw std::runtime_error("Could not convert IdTokenEnum"); + } +} + +ocpp::v201::IdToken get_id_token(const types::authorization::ProvidedIdToken& provided_id_token) { + ocpp::v201::IdToken id_token; + id_token.idToken = provided_id_token.id_token; + if (provided_id_token.id_token_type.has_value()) { + id_token.type = get_id_token_enum(provided_id_token.id_token_type.value()); + } + return id_token; +} + void OCPP201::init_evse_ready_map() { std::lock_guard lk(this->evse_ready_mutex); for (size_t evse_id = 1; evse_id <= this->r_evse_manager.size(); evse_id++) { @@ -853,15 +885,7 @@ void OCPP201::ready() { const auto reservation_id = transaction_started.reservation_id; const auto remote_start_id = transaction_started.id_tag.request_id; - ocpp::v201::IdToken id_token; - id_token.idToken = transaction_started.id_tag.id_token; - if (transaction_started.id_tag.id_token_type.has_value()) { - id_token.type = - ocpp::v201::conversions::string_to_id_token_enum(types::authorization::id_token_type_to_string( - transaction_started.id_tag.id_token_type.value())); - } else { - id_token.type = ocpp::v201::IdTokenEnum::Local; // FIXME(piet) - } + ocpp::v201::IdToken id_token = get_id_token(transaction_started.id_tag); // assume cable has been plugged in first and then authorized auto trigger_reason = ocpp::v201::TriggerReasonEnum::Authorized; @@ -893,8 +917,12 @@ void OCPP201::ready() { reason = get_reason(transaction_finished.reason.value()); } + std::optional id_token = std::nullopt; + if (transaction_finished.id_tag.has_value()) { + id_token = get_id_token(transaction_finished.id_tag.value()); + } + const auto signed_meter_value = transaction_finished.signed_meter_value; - const auto id_token = transaction_finished.id_tag; this->charge_point->on_transaction_finished(evse_id, timestamp, meter_value, reason, id_token, signed_meter_value, diff --git a/types/evse_manager.yaml b/types/evse_manager.yaml index 43fe98754..2fc04c07a 100644 --- a/types/evse_manager.yaml +++ b/types/evse_manager.yaml @@ -13,8 +13,9 @@ types: type: string $ref: /evse_manager#/StopTransactionReason id_tag: - description: Id tag that was used to stop the transaction. - type: string + description: Id tag that was used to stop the transaction. Only present if transaction was stopped locally. + type: object + $ref: /authorization#/ProvidedIdToken StopTransactionReason: description: >- Reason for stopping transaction @@ -219,8 +220,9 @@ types: type: string $ref: /evse_manager#/StopTransactionReason id_tag: - description: Id tag that was used to stop the transaction - type: string + description: Id tag that was used to stop the transaction. Only present if transaction was stopped locally. + type: object + $ref: /authorization#/ProvidedIdToken ErrorEnum: description: >- Note this is only kept for compatibility with the legacy error handling and will be removed soon.