From 0045cef5dae3be8131969d08f70c10748922524e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 21 Sep 2021 22:36:42 +0200 Subject: [PATCH] Fix ATG run duration computation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- package-lock.json | 6 ++-- package.json | 2 +- .../AutomaticTransactionGenerator.ts | 35 ++++++++++--------- src/charging-station/ChargingStation.ts | 6 ++-- .../ocpp/OCPPRequestService.ts | 1 - src/performance/storage/StorageFactory.ts | 1 - src/worker/WorkerDynamicPool.ts | 1 - src/worker/WorkerFactory.ts | 1 - src/worker/WorkerSet.ts | 1 - src/worker/WorkerStaticPool.ts | 1 - 10 files changed, 25 insertions(+), 30 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1128785a..057a7295 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5013,9 +5013,9 @@ } }, "@types/node": { - "version": "14.17.17", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.17.tgz", - "integrity": "sha512-niAjcewgEYvSPCZm3OaM9y6YQrL2SEPH9PymtE6fuZAvFiP6ereCcvApGl2jKTq7copTIguX3PBvfP08LN4LvQ==" + "version": "14.17.18", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.18.tgz", + "integrity": "sha512-haYyibw4pbteEhkSg0xdDLAI3679L75EJ799ymVrPxOA922bPx3ML59SoDsQ//rHlvqpu+e36kcbR3XRQtFblA==" }, "@types/object-path": { "version": "0.11.1", diff --git a/package.json b/package.json index 23a18fac..c56260f9 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "@rollup/plugin-json": "^4.1.0", "@types/mocha": "^9.0.0", "@types/mochawesome": "^6.2.1", - "@types/node": "^14.17.17", + "@types/node": "^14.17.18", "@types/proper-lockfile": "^4.1.2", "@types/tar": "^4.0.5", "@types/uuid": "^8.3.1", diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 2315b5cb..de11a76f 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -21,11 +21,12 @@ export default class AutomaticTransactionGenerator { } public start(): void { + const previousRunDuration = (this?.startDate && this?.lastRunDate) ? (this.lastRunDate.getTime() - this.startDate.getTime()) : 0; this.startDate = new Date(); - this.lastRunDate = this?.lastRunDate ?? this.startDate; + this.lastRunDate = this.startDate; this.stopDate = new Date(this.startDate.getTime() + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000 - - (this.lastRunDate.getTime() - this.startDate.getTime())); + - previousRunDuration); this.started = true; for (const connector in this.chargingStation.connectors) { if (Utils.convertToInt(connector) > 0) { @@ -38,21 +39,13 @@ export default class AutomaticTransactionGenerator { logger.info(this.logPrefix() + ' started and will run for ' + Utils.formatDurationMilliSeconds(this.stopDate.getTime() - this.startDate.getTime())); } - public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise { + public stop(): void { if (!this.started) { logger.error(`${this.logPrefix()} trying to stop while not started`); return; } - logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. Stopping all transactions`); - for (const connector in this.chargingStation.connectors) { - const transactionId = this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId; - if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { - logger.info(this.logPrefix(Utils.convertToInt(connector)) + ' over. Stop transaction ' + transactionId.toString()); - await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - this.chargingStation.getTransactionIdTag(transactionId), reason); - } - } this.started = false; + logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. Stopping all transactions`); } private async startOnConnector(connectorId: number): Promise { @@ -61,7 +54,7 @@ export default class AutomaticTransactionGenerator { let skippedTransactionsTotal = 0; while (this.started) { if ((new Date()) > this.stopDate) { - await this.stop(); + this.stop(); break; } if (!this.chargingStation.isRegistered()) { @@ -70,7 +63,7 @@ export default class AutomaticTransactionGenerator { } if (!this.chargingStation.isChargingStationAvailable()) { logger.info(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable'); - await this.stop(); + this.stop(); break; } if (!this.chargingStation.isConnectorAvailable(connectorId)) { @@ -114,6 +107,7 @@ export default class AutomaticTransactionGenerator { } this.lastRunDate = new Date(); } + await this.stopTransaction(connectorId); logger.info(this.logPrefix(connectorId) + ' stopped on connector'); } @@ -148,12 +142,19 @@ export default class AutomaticTransactionGenerator { return startResponse; } - private async stopTransaction(connectorId: number): Promise { + private async stopTransaction(connectorId: number, reason: StopTransactionReason = StopTransactionReason.NONE): Promise { const measureId = 'StopTransaction with ATG'; const beginId = PerformanceStatistics.beginMeasure(measureId); const transactionId = this.chargingStation.getConnector(connectorId).transactionId; - const stopResponse = this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId)); + let stopResponse: StopTransactionResponse; + if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { + stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + this.chargingStation.getTransactionIdTag(transactionId), + reason); + } else { + logger.warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction ${transactionId}`); + } PerformanceStatistics.endMeasure(measureId, beginId); return stopResponse; } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 79336707..38b89249 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -880,7 +880,7 @@ export default class ChargingStation { if (this.stationInfo.AutomaticTransactionGenerator.enable && this.automaticTransactionGenerator && this.automaticTransactionGenerator.started) { - await this.automaticTransactionGenerator.stop(reason); + this.automaticTransactionGenerator.stop(); } else { for (const connector in this.connectors) { if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionStarted) { @@ -1012,7 +1012,7 @@ export default class ChargingStation { // Restart the ATG if (!this.stationInfo.AutomaticTransactionGenerator.enable && this.automaticTransactionGenerator) { - await this.automaticTransactionGenerator.stop(); + this.automaticTransactionGenerator.stop(); } this.startAutomaticTransactionGenerator(); if (this.getEnableStatistics()) { @@ -1045,7 +1045,7 @@ export default class ChargingStation { this.stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure && this.automaticTransactionGenerator && this.automaticTransactionGenerator.started) { - await this.automaticTransactionGenerator.stop(); + this.automaticTransactionGenerator.stop(); } if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) { this.autoReconnectRetryCount++; diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index d17c21d3..9220d0cd 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -80,7 +80,6 @@ export default abstract class OCPPRequestService { } logger.debug(`${self.chargingStation.logPrefix()} Error: %j occurred when calling command %s with parameters: %j`, error, commandName, commandParams); // Build Exception - // eslint-disable-next-line no-empty-function self.chargingStation.requests.set(messageId, [() => { /* This is intentiomal */ }, () => { /* This is intentiomal */ }, {}]); // Send error reject(error); diff --git a/src/performance/storage/StorageFactory.ts b/src/performance/storage/StorageFactory.ts index 429f61d5..5ed11484 100644 --- a/src/performance/storage/StorageFactory.ts +++ b/src/performance/storage/StorageFactory.ts @@ -7,7 +7,6 @@ import { Storage } from './Storage'; import { StorageType } from '../../types/Storage'; export class StorageFactory { - // eslint-disable-next-line @typescript-eslint/no-empty-function private constructor() { // This is intentional } diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index e5119ff6..f081b3c3 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -37,7 +37,6 @@ export default class WorkerDynamicPool extends WorkerAbstract { * @returns * @public */ - // eslint-disable-next-line @typescript-eslint/no-empty-function public async start(): Promise { // This is intentional } diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index 55cb1da7..8b979d06 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -9,7 +9,6 @@ import WorkerSet from './WorkerSet'; import WorkerStaticPool from './WorkerStaticPool'; export default class WorkerFactory { - // eslint-disable-next-line @typescript-eslint/no-empty-function private constructor() { // This is intentional } diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index b782f311..f25fd03c 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -94,7 +94,6 @@ export default class WorkerSet extends WorkerAbstract { private getLastWorkerSetElement(): WorkerSetElement { let workerSetElement: WorkerSetElement; - // eslint-disable-next-line no-empty for (workerSetElement of this.workerSet) { /* This is intentional */ } return workerSetElement; } diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index e4a8910b..cbc62fb6 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -36,7 +36,6 @@ export default class WorkerStaticPool extends WorkerAbstract { * @returns * @public */ - // eslint-disable-next-line @typescript-eslint/no-empty-function public async start(): Promise { // This is intentional } -- 2.34.1