From: Jérôme Benoit Date: Mon, 29 Jan 2024 15:27:01 +0000 (+0100) Subject: fix: ensure remote start transaction triggers a delayed start transaction X-Git-Tag: v1.2.33~10^2~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=54510a60e9114c2076c86e6c90fe73c0dfefdb45;p=e-mobility-charging-stations-simulator.git fix: ensure remote start transaction triggers a delayed start transaction Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 9e0fed9d..9b3748d3 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -404,6 +404,41 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { .bind(this) ] ]) + this.on( + OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, + (chargingStation: ChargingStation, connectorId: number, idTag: string) => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + chargingStation.getConnectorStatus(connectorId)!.transactionRemoteStarted = true + chargingStation.ocppRequestService + .requestHandler( + chargingStation, + OCPP16RequestCommand.START_TRANSACTION, + { + connectorId, + idTag + } + ) + .then(response => { + if (response.status === OCPP16AuthorizationStatus.ACCEPTED) { + logger.debug(` + ${chargingStation.logPrefix()} Remote start transaction ACCEPTED on ${ + chargingStation.stationInfo?.chargingStationId + }#${connectorId} for idTag '${idTag}'`) + } else { + logger.debug(` + ${chargingStation.logPrefix()} Remote start transaction REJECTED on ${ + chargingStation.stationInfo?.chargingStationId + }#${connectorId} for idTag '${idTag}'`) + } + }) + .catch(error => { + logger.error( + `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote start transaction error:`, + error + ) + }) + } + ) this.validatePayload = this.validatePayload.bind(this) } @@ -1011,30 +1046,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { idTag ) } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - chargingStation.getConnectorStatus(transactionConnectorId)!.transactionRemoteStarted = true - if ( - ( - await chargingStation.ocppRequestService.requestHandler< - OCPP16StartTransactionRequest, - OCPP16StartTransactionResponse - >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, { - connectorId: transactionConnectorId, - idTag - }) - ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED - ) { - logger.debug(` - ${chargingStation.logPrefix()} Transaction remotely STARTED on ${ - chargingStation.stationInfo?.chargingStationId - }#${transactionConnectorId} for idTag '${idTag}'`) - return OCPP16Constants.OCPP_RESPONSE_ACCEPTED - } - return await this.notifyRemoteStartTransactionRejected( + this.emit( + OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, chargingStation, transactionConnectorId, idTag ) + return OCPP16Constants.OCPP_RESPONSE_ACCEPTED } private async notifyRemoteStartTransactionRejected ( @@ -1051,7 +1069,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) } logger.warn( - `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'` + `${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'` ) return OCPP16Constants.OCPP_RESPONSE_REJECTED } diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 72035697..49128ccb 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,3 +1,5 @@ +import { EventEmitter } from 'node:events' + import _Ajv, { type ValidateFunction } from 'ajv' import _ajvFormats from 'ajv-formats' @@ -20,7 +22,7 @@ const ajvFormats = _ajvFormats.default const moduleName = 'OCPPIncomingRequestService' -export abstract class OCPPIncomingRequestService { +export abstract class OCPPIncomingRequestService extends EventEmitter { private static instance: OCPPIncomingRequestService | null = null private readonly version: OCPPVersion protected readonly ajv: Ajv @@ -30,6 +32,7 @@ export abstract class OCPPIncomingRequestService { > protected constructor (version: OCPPVersion) { + super() this.version = version this.ajv = new Ajv({ keywords: ['javaType'],