From 56563a3c157094a61fdb985b0fb7d95ef916bc8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 31 Jul 2023 21:40:30 +0200 Subject: [PATCH] fix: fix valid reservation detection at start transaction MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 2 +- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 12 +++++++----- .../ocpp/1.6/OCPP16ResponseService.ts | 19 +++++++++++++++---- .../ocpp/1.6/OCPP16ServiceUtils.ts | 11 ++++++----- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index b6aa7b98..5f4f58b4 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -986,7 +986,7 @@ export class ChargingStation { break; default: // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - throw new Error(`Unknown reservation termination reason '${reason}'`); + throw new BaseError(`Unknown reservation termination reason '${reason}'`); } } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 7a9e1a4e..96ed5a8c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -6,7 +6,7 @@ import { URL, fileURLToPath } from 'node:url'; import type { JSONSchemaType } from 'ajv'; import { Client, type FTPResponse } from 'basic-ftp'; -import { secondsToMilliseconds } from 'date-fns'; +import { isWithinInterval, secondsToMilliseconds } from 'date-fns'; import { create } from 'tar'; import { OCPP16Constants } from './OCPP16Constants'; @@ -678,8 +678,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { .chargingProfiles!) { // FIXME: build the composite schedule including the local power limit, the stack level, the charging rate unit, etc. if ( - chargingProfile.chargingSchedule.startSchedule! >= startDate && - chargingProfile.chargingSchedule.startSchedule! <= endDate + isWithinInterval(chargingProfile.chargingSchedule.startSchedule!, { + start: startDate, + end: endDate, + }) ) { compositeSchedule = chargingProfile.chargingSchedule; break; @@ -999,7 +1001,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { } } logger.warn( - `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id: + `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId.toString()}`, ); return OCPP16Constants.OCPP_RESPONSE_REJECTED; @@ -1245,7 +1247,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { if (accessResponse.code === 220) { ftpClient.trackProgress((info) => { logger.info( - `${chargingStation.logPrefix()} ${ + `${chargingStation.logPrefix()} ${moduleName}.handleRequestGetDiagnostics: ${ info.bytes / 1024 } bytes transferred from diagnostics archive ${info.name}`, ); diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index f1a17068..a2cfc531 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -647,7 +647,7 @@ export class OCPP16ResponseService extends OCPPResponseService { )!; if (reservation.idTag !== requestPayload.idTag) { logger.warn( - `${chargingStation.logPrefix()} Transaction reserved ${ + `${chargingStation.logPrefix()} Reserved transaction ${ payload.transactionId } started with a different idTag ${requestPayload.idTag} than the reservation one ${ reservation.idTag @@ -656,7 +656,7 @@ export class OCPP16ResponseService extends OCPPResponseService { } if (hasReservationExpired(reservation)) { logger.warn( - `${chargingStation.logPrefix()} Transaction reserved ${ + `${chargingStation.logPrefix()} Reserved transaction ${ payload.transactionId } started with expired reservation ${ requestPayload.reservationId @@ -704,8 +704,19 @@ export class OCPP16ResponseService extends OCPPResponseService { ); } else { logger.warn( - `${chargingStation.logPrefix()} Starting transaction with id ${payload.transactionId.toString()} REJECTED with status '${payload - .idTagInfo?.status}', idTag '${requestPayload.idTag}'`, + `${chargingStation.logPrefix()} Starting transaction with id ${payload.transactionId.toString()} REJECTED on ${ + chargingStation.stationInfo.chargingStationId + }#${transactionConnectorId.toString()} with status '${payload.idTagInfo?.status}', idTag '${ + requestPayload.idTag + }'${ + OCPP16ServiceUtils.hasReservation( + chargingStation, + transactionConnectorId, + requestPayload.idTag, + ) + ? `, reservationId '${requestPayload.reservationId}'` + : '' + }`, ); await this.resetConnectorOnStartTransactionError(chargingStation, transactionConnectorId); } diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index b6229b90..6765722c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -939,16 +939,17 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { (chargingStation.getConnectorStatus(connectorId)?.status === OCPP16ChargePointStatus.Reserved && connectorReservation && + !hasReservationExpired(connectorReservation) && // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - (hasReservationExpired(connectorReservation) || connectorReservation?.idTag !== idTag)) || + connectorReservation?.idTag === idTag) || (chargingStation.getConnectorStatus(0)?.status === OCPP16ChargePointStatus.Reserved && chargingStationReservation && - (hasReservationExpired(chargingStationReservation) || - chargingStationReservation?.idTag !== idTag)) + !hasReservationExpired(chargingStationReservation) && + chargingStationReservation?.idTag === idTag) ) { - return false; + return true; } - return true; + return false; }; public static parseJsonSchemaFile( -- 2.34.1