From 68c993d5b34c191022256f26d4e95e5f3b9974d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 8 Mar 2022 10:32:15 +0100 Subject: [PATCH] Convert sendStopTransaction to OCPP message sending handler MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 34 ++++++++-- src/charging-station/ChargingStation.ts | 28 ++++++-- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 64 +++++++++++++++--- .../ocpp/1.6/OCPP16RequestService.ts | 66 ++++--------------- .../ocpp/OCPPRequestService.ts | 7 -- 5 files changed, 118 insertions(+), 81 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index d76ff970..a916000f 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -10,6 +10,7 @@ import { import type ChargingStation from './ChargingStation'; import Constants from '../utils/Constants'; +import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils'; import PerformanceStatistics from '../performance/PerformanceStatistics'; import { RequestCommand } from '../types/ocpp/Requests'; import { Status } from '../types/AutomaticTransactionGenerator'; @@ -329,12 +330,33 @@ export default class AutomaticTransactionGenerator { let stopResponse: StopTransactionResponse; if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; - stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction( - transactionId, - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - this.chargingStation.getTransactionIdTag(transactionId), - reason - ); + if ( + this.chargingStation.getBeginEndMeterValues() && + this.chargingStation.getOcppStrictCompliance() && + !this.chargingStation.getOutOfOrderEndMeterValues() + ) { + // FIXME: Implement OCPP version agnostic helpers + const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( + this.chargingStation, + connectorId, + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) + ); + await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues( + connectorId, + transactionId, + transactionEndMeterValue + ); + } + stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( + RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.chargingStation.getTransactionIdTag(transactionId), + reason, + } + )) as StopTransactionResponse; this.connectorsStatus.get(connectorId).stopTransactionRequests++; } else { logger.warn( diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 539ab309..e90bd60b 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -44,6 +44,7 @@ import { MessageType } from '../types/ocpp/MessageType'; import OCPP16IncomingRequestService from './ocpp/1.6/OCPP16IncomingRequestService'; import OCPP16RequestService from './ocpp/1.6/OCPP16RequestService'; import OCPP16ResponseService from './ocpp/1.6/OCPP16ResponseService'; +import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils'; import OCPPError from '../exception/OCPPError'; import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService'; import OCPPRequestService from './ocpp/OCPPRequestService'; @@ -1337,12 +1338,29 @@ export default class ChargingStation { for (const connectorId of this.connectors.keys()) { if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted) { const transactionId = this.getConnectorStatus(connectorId).transactionId; - await this.ocppRequestService.sendStopTransaction( + if ( + this.getBeginEndMeterValues() && + this.getOcppStrictCompliance() && + !this.getOutOfOrderEndMeterValues() + ) { + // FIXME: Implement OCPP version agnostic helpers + const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( + this, + connectorId, + this.getEnergyActiveImportRegisterByTransactionId(transactionId) + ); + await this.ocppRequestService.sendTransactionEndMeterValues( + connectorId, + transactionId, + transactionEndMeterValue + ); + } + await this.ocppRequestService.sendMessageHandler(RequestCommand.STOP_TRANSACTION, { transactionId, - this.getEnergyActiveImportRegisterByTransactionId(transactionId), - this.getTransactionIdTag(transactionId), - reason - ); + meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.getTransactionIdTag(transactionId), + reason, + }); } } } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 6aae66f6..b791f713 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -37,6 +37,7 @@ import { OCPP16AuthorizeResponse, OCPP16StartTransactionResponse, OCPP16StopTransactionReason, + OCPP16StopTransactionResponse, } from '../../../types/ocpp/1.6/Transaction'; import type ChargingStation from '../../ChargingStation'; @@ -48,6 +49,7 @@ import { JsonType } from '../../../types/JsonType'; import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; +import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; import OCPPError from '../../../exception/OCPPError'; @@ -204,12 +206,33 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; - const stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction( - transactionId, - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - this.chargingStation.getTransactionIdTag(transactionId), - OCPP16StopTransactionReason.UNLOCK_COMMAND - ); + if ( + this.chargingStation.getBeginEndMeterValues() && + this.chargingStation.getOcppStrictCompliance() && + !this.chargingStation.getOutOfOrderEndMeterValues() + ) { + // FIXME: Implement OCPP version agnostic helpers + const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( + this.chargingStation, + connectorId, + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) + ); + await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues( + connectorId, + transactionId, + transactionEndMeterValue + ); + } + const stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( + OCPP16RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.chargingStation.getTransactionIdTag(transactionId), + reason: OCPP16StopTransactionReason.UNLOCK_COMMAND, + } + )) as OCPP16StopTransactionResponse; if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { return Constants.OCPP_RESPONSE_UNLOCKED; } @@ -726,10 +749,31 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer ); this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.FINISHING; - await this.chargingStation.ocppRequestService.sendStopTransaction( - transactionId, - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - this.chargingStation.getTransactionIdTag(transactionId) + if ( + this.chargingStation.getBeginEndMeterValues() && + this.chargingStation.getOcppStrictCompliance() && + !this.chargingStation.getOutOfOrderEndMeterValues() + ) { + // FIXME: Implement OCPP version agnostic helpers + const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( + this.chargingStation, + connectorId, + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) + ); + await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues( + connectorId, + transactionId, + transactionEndMeterValue + ); + } + await this.chargingStation.ocppRequestService.sendMessageHandler( + OCPP16RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.chargingStation.getTransactionIdTag(transactionId), + } ); return Constants.OCPP_RESPONSE_ACCEPTED; } diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index ca024649..7ef652f0 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -2,9 +2,6 @@ import { AuthorizeRequest, - OCPP16StartTransactionResponse, - OCPP16StopTransactionReason, - OCPP16StopTransactionResponse, StartTransactionRequest, StopTransactionRequest, } from '../../../types/ocpp/1.6/Transaction'; @@ -60,53 +57,6 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } - public async sendStopTransaction( - transactionId: number, - meterStop: number, - idTag?: string, - reason: OCPP16StopTransactionReason = OCPP16StopTransactionReason.NONE - ): Promise { - let connectorId: number; - for (const id of this.chargingStation.connectors.keys()) { - if (id > 0 && this.chargingStation.getConnectorStatus(id)?.transactionId === transactionId) { - connectorId = id; - break; - } - } - const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue( - this.chargingStation, - connectorId, - meterStop - ); - // FIXME: should be a callback, each OCPP commands implementation must do only one job - this.chargingStation.getBeginEndMeterValues() && - this.chargingStation.getOcppStrictCompliance() && - !this.chargingStation.getOutOfOrderEndMeterValues() && - (await this.sendTransactionEndMeterValues( - connectorId, - transactionId, - transactionEndMeterValue - )); - const payload: StopTransactionRequest = { - transactionId, - ...(!Utils.isUndefined(idTag) && { idTag }), - meterStop, - timestamp: new Date().toISOString(), - ...(reason && { reason }), - ...(this.chargingStation.getTransactionDataMeterValues() && { - transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( - this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, - transactionEndMeterValue - ), - }), - }; - return (await this.sendMessage( - Utils.generateUUID(), - payload, - OCPP16RequestCommand.STOP_TRANSACTION - )) as OCPP16StartTransactionResponse; - } - public async sendMeterValues( connectorId: number, transactionId: number, @@ -169,6 +119,7 @@ export default class OCPP16RequestService extends OCPPRequestService { commandName: OCPP16RequestCommand, commandParams?: JsonType ): JsonType { + let connectorId: number; switch (commandName) { case OCPP16RequestCommand.AUTHORIZE: return { @@ -230,6 +181,16 @@ 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; + } + } return { transactionId: commandParams?.transactionId, ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), @@ -238,11 +199,10 @@ export default class OCPP16RequestService extends OCPPRequestService { ...(commandParams?.reason && { reason: commandParams.reason }), ...(this.chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( - this.chargingStation.getConnectorStatus(commandParams?.connectorId as number) - .transactionBeginMeterValue, + this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, OCPP16ServiceUtils.buildTransactionEndMeterValue( this.chargingStation, - commandParams?.connectorId as number, + connectorId, commandParams?.meterStop as number ) ), diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index f4a1d169..4fe2c141 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -325,13 +325,6 @@ export default abstract class OCPPRequestService { params?: SendParams ): Promise; - public abstract sendStopTransaction( - transactionId: number, - meterStop: number, - idTag?: string, - reason?: StopTransactionReason - ): Promise; - public abstract sendMeterValues( connectorId: number, transactionId: number, -- 2.34.1