From: Jérôme Benoit Date: Wed, 10 Apr 2024 09:08:06 +0000 (+0200) Subject: fix: send preparing connector status before `StartTransaction` X-Git-Tag: v1.3.2~3^2~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a9671b9e315a2807c2659fdda962203229d35c0a;p=e-mobility-charging-stations-simulator.git fix: send preparing connector status before `StartTransaction` Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 926915fb..ecd858c9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -2221,21 +2221,28 @@ export class ChargingStation extends EventEmitter { for (const [evseId, evseStatus] of this.evses) { if (evseId > 0) { for (const [connectorId, connectorStatus] of evseStatus.connectors) { - const connectorBootStatus = getBootConnectorStatus(this, connectorId, connectorStatus) - await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus, evseId) + await sendAndSetConnectorStatus( + this, + connectorId, + getBootConnectorStatus(this, connectorId, connectorStatus), + evseId + ) } } } } else { for (const connectorId of this.connectors.keys()) { if (connectorId > 0) { - const connectorBootStatus = getBootConnectorStatus( + await sendAndSetConnectorStatus( this, connectorId, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getConnectorStatus(connectorId)! + getBootConnectorStatus( + this, + connectorId, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.getConnectorStatus(connectorId)! + ) ) - await sendAndSetConnectorStatus(this, connectorId, connectorBootStatus) } } } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index f4a89024..6cafcd4b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -1153,7 +1153,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ): Promise { const { connectorId: transactionConnectorId, idTag, chargingProfile } = commandPayload if (!chargingStation.hasConnector(transactionConnectorId)) { - return await this.notifyRemoteStartTransactionRejected( + return this.notifyRemoteStartTransactionRejected( chargingStation, transactionConnectorId, idTag @@ -1163,7 +1163,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { !chargingStation.isChargingStationAvailable() || !chargingStation.isConnectorAvailable(transactionConnectorId) ) { - return await this.notifyRemoteStartTransactionRejected( + return this.notifyRemoteStartTransactionRejected( chargingStation, transactionConnectorId, idTag @@ -1174,17 +1174,12 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { chargingStation.getAuthorizeRemoteTxRequests() && !(await OCPP16ServiceUtils.isIdTagAuthorized(chargingStation, transactionConnectorId, idTag)) ) { - return await this.notifyRemoteStartTransactionRejected( + return this.notifyRemoteStartTransactionRejected( chargingStation, transactionConnectorId, idTag ) } - await OCPP16ServiceUtils.sendAndSetConnectorStatus( - chargingStation, - transactionConnectorId, - OCPP16ChargePointStatus.Preparing - ) if ( chargingProfile != null && !this.setRemoteStartTransactionChargingProfile( @@ -1193,33 +1188,26 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { chargingProfile ) ) { - return await this.notifyRemoteStartTransactionRejected( + return this.notifyRemoteStartTransactionRejected( chargingStation, transactionConnectorId, idTag ) } logger.debug( - `${chargingStation.logPrefix()} Remote start transaction ACCEPTED on connector id ${transactionConnectorId}, idTag '${idTag}'` + `${chargingStation.logPrefix()} Remote start transaction ACCEPTED on ${chargingStation.stationInfo?.chargingStationId}#${transactionConnectorId}}, idTag '${idTag}'` ) return OCPP16Constants.OCPP_RESPONSE_ACCEPTED } - private async notifyRemoteStartTransactionRejected ( + private notifyRemoteStartTransactionRejected ( chargingStation: ChargingStation, connectorId: number, idTag: string - ): Promise { + ): GenericResponse { const connectorStatus = chargingStation.getConnectorStatus(connectorId) - if (connectorStatus?.status !== OCPP16ChargePointStatus.Available) { - await OCPP16ServiceUtils.sendAndSetConnectorStatus( - chargingStation, - connectorId, - OCPP16ChargePointStatus.Available - ) - } logger.debug( - `${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'` + `${chargingStation.logPrefix()} Remote start transaction REJECTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'` ) return OCPP16Constants.OCPP_RESPONSE_REJECTED } @@ -1232,7 +1220,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { if (chargingProfile.chargingProfilePurpose === OCPP16ChargingProfilePurposeType.TX_PROFILE) { OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, chargingProfile) logger.debug( - `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}: %j`, + `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on ${chargingStation.stationInfo?.chargingStationId}#${connectorId}`, chargingProfile ) return true diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index d673e8a3..b9643469 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -167,11 +167,21 @@ export class OCPP16RequestService extends OCPPRequestService { public async requestHandler( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, - commandParams?: JsonType, + commandParams?: RequestType, params?: RequestParams ): Promise { // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc. if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName)) { + // Post request actions hook + switch (commandName) { + case OCPP16RequestCommand.START_TRANSACTION: + await OCPP16ServiceUtils.sendAndSetConnectorStatus( + chargingStation, + (commandParams as OCPP16StartTransactionRequest).connectorId, + OCPP16ChargePointStatus.Preparing + ) + break + } return (await this.sendMessage( chargingStation, generateUUID(), diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 24108ba4..e6c3b434 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -816,22 +816,10 @@ export class OCPP16ResponseService extends OCPPResponseService { chargingStation: ChargingStation, connectorId: number ): Promise { + chargingStation.stopMeterValues(connectorId) const connectorStatus = chargingStation.getConnectorStatus(connectorId) resetConnectorStatus(connectorStatus) - chargingStation.stopMeterValues(connectorId) - if (chargingStation.getReservationBy('connectorId', connectorId) != null) { - await OCPP16ServiceUtils.sendAndSetConnectorStatus( - chargingStation, - connectorId, - OCPP16ChargePointStatus.Reserved - ) - } else if (connectorStatus?.status !== OCPP16ChargePointStatus.Available) { - await OCPP16ServiceUtils.sendAndSetConnectorStatus( - chargingStation, - connectorId, - OCPP16ChargePointStatus.Available - ) - } + await OCPP16ServiceUtils.restoreConnectorStatus(chargingStation, connectorId, connectorStatus) } private async handleResponseStopTransaction ( diff --git a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts index 6673b9b4..c9c4b17b 100644 --- a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts @@ -75,11 +75,12 @@ export class OCPP20RequestService extends OCPPRequestService { public async requestHandler( chargingStation: ChargingStation, commandName: OCPP20RequestCommand, - commandParams?: JsonType, + commandParams?: RequestType, params?: RequestParams ): Promise { // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc. if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName)) { + // TODO: post request actions hook return (await this.sendMessage( chargingStation, generateUUID(), diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index d7757eaa..3ac768c2 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -481,8 +481,7 @@ export abstract class OCPPRequestService { public abstract requestHandler( chargingStation: ChargingStation, commandName: RequestCommand, - // FIXME: should be ReqType - commandParams?: JsonType, + commandParams?: ReqType, params?: RequestParams ): Promise } diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 8a42bf67..02755c4e 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -18,7 +18,8 @@ import { type AuthorizeResponse, ChargePointErrorCode, ChargingStationEvents, - type ConnectorStatusEnum, + type ConnectorStatus, + ConnectorStatusEnum, CurrentType, ErrorType, FileType, @@ -194,6 +195,18 @@ export const sendAndSetConnectorStatus = async ( }) } +export const restoreConnectorStatus = async ( + chargingStation: ChargingStation, + connectorId: number, + connectorStatus: ConnectorStatus | undefined +): Promise => { + if (connectorStatus?.reservation != null) { + await sendAndSetConnectorStatus(chargingStation, connectorId, ConnectorStatusEnum.Reserved) + } else if (connectorStatus?.status !== ConnectorStatusEnum.Available) { + await sendAndSetConnectorStatus(chargingStation, connectorId, ConnectorStatusEnum.Available) + } +} + const checkConnectorStatusTransition = ( chargingStation: ChargingStation, connectorId: number, @@ -1228,6 +1241,7 @@ const getMeasurandDefaultLocation = ( export class OCPPServiceUtils { public static readonly getMessageTypeString = getMessageTypeString public static readonly sendAndSetConnectorStatus = sendAndSetConnectorStatus + public static readonly restoreConnectorStatus = restoreConnectorStatus public static readonly isIdTagAuthorized = isIdTagAuthorized public static readonly buildTransactionEndMeterValue = buildTransactionEndMeterValue protected static getSampledValueTemplate = getSampledValueTemplate