From: Jérôme Benoit Date: Wed, 9 Mar 2022 20:38:05 +0000 (+0100) Subject: Add helper to get connector id for a transaction id X-Git-Tag: v1.1.54~6 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f479a7929a02e6290ec945557136bf4ee5445107;p=e-mobility-charging-stations-simulator.git Add helper to get connector id for a transaction id Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 1be6e335..36c5b8d5 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -256,19 +256,22 @@ export default class ChargingStation { } public getEnergyActiveImportRegisterByTransactionId(transactionId: number): number | undefined { + const transactionConnectorStatus = this.getConnectorStatus( + this.getConnectorIdByTransactionId(transactionId) + ); if (this.getMeteringPerTransaction()) { - for (const connectorId of this.connectors.keys()) { - if ( - connectorId > 0 && - this.getConnectorStatus(connectorId).transactionId === transactionId - ) { - return this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue; - } - } + return transactionConnectorStatus.transactionEnergyActiveImportRegisterValue; } + return transactionConnectorStatus.energyActiveImportRegisterValue; + } + + public getConnectorIdByTransactionId(transactionId: number): number | undefined { for (const connectorId of this.connectors.keys()) { - if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) { - return this.getConnectorStatus(connectorId).energyActiveImportRegisterValue; + if ( + connectorId > 0 && + this.getConnectorStatus(connectorId)?.transactionId === transactionId + ) { + return connectorId; } } } diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index ab45df16..571ac066 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -122,16 +122,9 @@ export default class OCPP16RequestService extends OCPPRequestService { timestamp: new Date().toISOString(), } as StartTransactionRequest; case OCPP16RequestCommand.STOP_TRANSACTION: - for (const id of this.chargingStation.connectors.keys()) { - if ( - id > 0 && - this.chargingStation.getConnectorStatus(id)?.transactionId === - commandParams?.transactionId - ) { - connectorId = id; - break; - } - } + connectorId = this.chargingStation.getConnectorIdByTransactionId( + commandParams?.transactionId as number + ); return { transactionId: commandParams?.transactionId, ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 27f252e1..aa48501e 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -389,17 +389,9 @@ export default class OCPP16ResponseService extends OCPPResponseService { payload: OCPP16StopTransactionResponse, requestPayload: StopTransactionRequest ): Promise { - let transactionConnectorId: number; - for (const connectorId of this.chargingStation.connectors.keys()) { - if ( - connectorId > 0 && - this.chargingStation.getConnectorStatus(connectorId)?.transactionId === - requestPayload.transactionId - ) { - transactionConnectorId = connectorId; - break; - } - } + const transactionConnectorId = this.chargingStation.getConnectorIdByTransactionId( + requestPayload.transactionId + ); if (!transactionConnectorId) { logger.error( this.chargingStation.logPrefix() +