From: Jérôme Benoit Date: Sun, 7 Jan 2024 22:09:35 +0000 (+0100) Subject: fix: fix off-by-one in ATG statuses handling X-Git-Tag: v1.2.32~52 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0a1dd746244cebb3633d88c6650839d9f32227fe;p=e-mobility-charging-stations-simulator.git fix: fix off-by-one in ATG statuses handling Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 38efbf51..d056d8ec 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -22,6 +22,7 @@ import { cloneObject, formatDurationMilliSeconds, getRandomInteger, + isValidTime, logPrefix, logger, secureRandom, @@ -226,9 +227,9 @@ export class AutomaticTransactionGenerator { } } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions! + ++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.skippedTransactions! + ++this.connectorsStatus.get(connectorId)!.skippedTransactions logger.info( `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus.get( connectorId @@ -258,11 +259,9 @@ export class AutomaticTransactionGenerator { } private setStartConnectorStatus (connectorId: number): void { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0 const previousRunDuration = - this.connectorsStatus.get(connectorId)?.startDate != null && - this.connectorsStatus.get(connectorId)?.lastRunDate != null + isValidTime(this.connectorsStatus.get(connectorId)?.startDate) && + isValidTime(this.connectorsStatus.get(connectorId)?.lastRunDate) ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.connectorsStatus.get(connectorId)!.lastRunDate!.getTime() - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -280,6 +279,9 @@ export class AutomaticTransactionGenerator { ) - previousRunDuration ) + delete this.connectorsStatus.get(connectorId)?.stoppedDate + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.connectorsStatus.get(connectorId)!.start = true } @@ -287,6 +289,11 @@ export class AutomaticTransactionGenerator { private canStartConnector (connectorId: number): boolean { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) { + logger.info( + `${this.logPrefix( + connectorId + )} entered in transaction loop while the ATG stop date has been reached` + ) return false } if (!this.chargingStation.inAcceptedState()) { @@ -366,10 +373,10 @@ export class AutomaticTransactionGenerator { private getConnectorStatus (connectorId: number): Status { const connectorStatus = - this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId] != null + this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId - 1] != null ? cloneObject( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId] + this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1] ) : undefined this.resetConnectorStatus(connectorStatus) @@ -395,10 +402,6 @@ export class AutomaticTransactionGenerator { if (connectorStatus == null) { return } - delete connectorStatus.startDate - delete connectorStatus.lastRunDate - delete connectorStatus.stopDate - delete connectorStatus.stoppedDate if ( !this.started && (connectorStatus.start || @@ -426,10 +429,10 @@ export class AutomaticTransactionGenerator { )} start transaction with an idTag '${idTag}'` if (this.getRequireAuthorize()) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.authorizeRequests! + ++this.connectorsStatus.get(connectorId)!.authorizeRequests if (await isIdTagAuthorized(this.chargingStation, connectorId, idTag)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests! + ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests logger.info(startTransactionLogMsg) // Start transaction startResponse = await this.chargingStation.ocppRequestService.requestHandler< @@ -444,7 +447,7 @@ export class AutomaticTransactionGenerator { return startResponse } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.rejectedAuthorizeRequests! + ++this.connectorsStatus.get(connectorId)!.rejectedAuthorizeRequests PerformanceStatistics.endMeasure(measureId, beginId) return startResponse } @@ -487,13 +490,13 @@ export class AutomaticTransactionGenerator { ) stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.stopTransactionRequests! + ++this.connectorsStatus.get(connectorId)!.stopTransactionRequests if (stopResponse.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.acceptedStopTransactionRequests! + ++this.connectorsStatus.get(connectorId)!.acceptedStopTransactionRequests } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.rejectedStopTransactionRequests! + ++this.connectorsStatus.get(connectorId)!.rejectedStopTransactionRequests } } else { const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId @@ -526,14 +529,14 @@ export class AutomaticTransactionGenerator { startResponse: StartTransactionResponse ): void { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.startTransactionRequests! + ++this.connectorsStatus.get(connectorId)!.startTransactionRequests if (startResponse.idTagInfo.status === AuthorizationStatus.ACCEPTED) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.acceptedStartTransactionRequests! + ++this.connectorsStatus.get(connectorId)!.acceptedStartTransactionRequests } else { logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - ++this.connectorsStatus.get(connectorId)!.rejectedStartTransactionRequests! + ++this.connectorsStatus.get(connectorId)!.rejectedStartTransactionRequests } } } diff --git a/src/types/AutomaticTransactionGenerator.ts b/src/types/AutomaticTransactionGenerator.ts index 68fd2fe3..670590d4 100644 --- a/src/types/AutomaticTransactionGenerator.ts +++ b/src/types/AutomaticTransactionGenerator.ts @@ -23,17 +23,17 @@ export interface Status { lastRunDate?: Date stopDate?: Date stoppedDate?: Date - authorizeRequests?: number - acceptedAuthorizeRequests?: number - rejectedAuthorizeRequests?: number - startTransactionRequests?: number - acceptedStartTransactionRequests?: number - rejectedStartTransactionRequests?: number - stopTransactionRequests?: number - acceptedStopTransactionRequests?: number - rejectedStopTransactionRequests?: number - skippedConsecutiveTransactions?: number - skippedTransactions?: number + authorizeRequests: number + acceptedAuthorizeRequests: number + rejectedAuthorizeRequests: number + startTransactionRequests: number + acceptedStartTransactionRequests: number + rejectedStartTransactionRequests: number + stopTransactionRequests: number + acceptedStopTransactionRequests: number + rejectedStopTransactionRequests: number + skippedConsecutiveTransactions: number + skippedTransactions: number } export interface ChargingStationAutomaticTransactionGeneratorConfiguration {