From 1fe0632adfa4067470497e2de02249fc84a4e218 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 24 May 2023 22:56:29 +0200 Subject: [PATCH] refactor(simulator): factor out default ATG configuration MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 24 +++++++++---------- src/charging-station/ChargingStation.ts | 4 ++-- src/charging-station/ChargingStationUtils.ts | 14 +++-------- .../UIServiceWorkerBroadcastChannel.ts | 2 +- .../ocpp/1.6/OCPP16ResponseService.ts | 2 +- src/performance/PerformanceStatistics.ts | 6 ++--- src/utils/Constants.ts | 16 ++++++++++++- src/worker/WorkerSet.ts | 2 +- 8 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 011f1a15..32c8a3bf 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -265,8 +265,8 @@ export class AutomaticTransactionGenerator extends AsyncResource { await this.stopTransaction(connectorId); } } else { - this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++; - this.connectorsStatus.get(connectorId).skippedTransactions++; + ++this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions; + ++this.connectorsStatus.get(connectorId).skippedTransactions; logger.info( `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus .get(connectorId) @@ -304,7 +304,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { this.connectorsStatus.get(connectorId).stopDate = new Date( this.connectorsStatus.get(connectorId).startDate.getTime() + (this.chargingStation.getAutomaticTransactionGeneratorConfiguration().stopAfterHours ?? - Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * + Constants.DEFAULT_ATG_STOP_AFTER_HOURS) * 3600 * 1000 - previousRunDuration @@ -381,9 +381,9 @@ export class AutomaticTransactionGenerator extends AsyncResource { >(this.chargingStation, RequestCommand.AUTHORIZE, { idTag, }); - this.connectorsStatus.get(connectorId).authorizeRequests++; + ++this.connectorsStatus.get(connectorId).authorizeRequests; if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { - this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++; + ++this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests; logger.info(startTransactionLogMsg); // Start transaction startResponse = await this.chargingStation.ocppRequestService.requestHandler< @@ -397,7 +397,7 @@ export class AutomaticTransactionGenerator extends AsyncResource { PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } - this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++; + ++this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests; PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } @@ -433,11 +433,11 @@ export class AutomaticTransactionGenerator extends AsyncResource { let stopResponse: StopTransactionResponse; if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason); - this.connectorsStatus.get(connectorId).stopTransactionRequests++; + ++this.connectorsStatus.get(connectorId).stopTransactionRequests; if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { - this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests++; + ++this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests; } else { - this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++; + ++this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests; } } else { const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId; @@ -469,12 +469,12 @@ export class AutomaticTransactionGenerator extends AsyncResource { connectorId: number, startResponse: StartTransactionResponse ): void { - this.connectorsStatus.get(connectorId).startTransactionRequests++; + ++this.connectorsStatus.get(connectorId).startTransactionRequests; if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { - this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; + ++this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests; } else { logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`); - this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; + ++this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests; } } } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index f47952ca..a8d96e13 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1654,7 +1654,7 @@ export class ChargingStation { skipBufferingOnError: true, }); if (this.isRegistered() === false) { - this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++; + this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount; await Utils.sleep( this?.bootNotificationResponse?.interval ? this.bootNotificationResponse.interval * 1000 @@ -2216,7 +2216,7 @@ export class ChargingStation { this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1 ) { - this.autoReconnectRetryCount++; + ++this.autoReconnectRetryCount; const reconnectDelay = this.getReconnectExponentialDelay() ? Utils.exponentialDelay(this.autoReconnectRetryCount) : this.getConnectionTimeout() * 1000; diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index a329eb53..f86c5d78 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -161,18 +161,10 @@ export class ChargingStationUtils { throw new BaseError(errorMsg); } if (Utils.isEmptyObject(stationTemplate.AutomaticTransactionGenerator)) { - stationTemplate.AutomaticTransactionGenerator = { - enable: false, - minDuration: 60, - maxDuration: 120, - minDelayBetweenTwoTransactions: 15, - maxDelayBetweenTwoTransactions: 30, - probabilityOfStart: 1, - stopAfterHours: 0.3, - stopOnConnectionFailure: true, - }; + stationTemplate.AutomaticTransactionGenerator = Constants.DEFAULT_ATG_CONFIGURATION; logger.warn( - `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default values` + `${logPrefix} Empty automatic transaction generator configuration from template file ${templateFile}, set to default: %j`, + Constants.DEFAULT_ATG_CONFIGURATION ); } if ( diff --git a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts index 25d62962..b66cd8a0 100644 --- a/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/UIServiceWorkerBroadcastChannel.ts @@ -47,7 +47,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel { } else if ( this.responses.get(uuid)?.responsesReceived <= this.responses.get(uuid)?.responsesExpected ) { - this.responses.get(uuid).responsesReceived++; + ++this.responses.get(uuid).responsesReceived; this.responses.get(uuid)?.responses.push(responsePayload); } if ( diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 80ed03b6..1999473d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -612,7 +612,7 @@ export class OCPP16ResponseService extends OCPPResponseService { }#${transactionConnectorId.toString()} for idTag '${requestPayload.idTag}'` ); if (chargingStation.stationInfo.powerSharedByConnectors) { - chargingStation.powerDivider++; + ++chargingStation.powerDivider; } const configuredMeterValueSampleInterval = ChargingStationConfigurationUtils.getConfigurationKey( diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 587532b7..61ef1b01 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -78,7 +78,7 @@ export class PerformanceStatistics { this.statistics.statisticsData.has(command) && this.statistics.statisticsData.get(command)?.countRequest ) { - this.statistics.statisticsData.get(command).countRequest++; + ++this.statistics.statisticsData.get(command).countRequest; } else { this.statistics.statisticsData.set(command, { ...this.statistics.statisticsData.get(command), @@ -91,7 +91,7 @@ export class PerformanceStatistics { this.statistics.statisticsData.has(command) && this.statistics.statisticsData.get(command)?.countResponse ) { - this.statistics.statisticsData.get(command).countResponse++; + ++this.statistics.statisticsData.get(command).countResponse; } else { this.statistics.statisticsData.set(command, { ...this.statistics.statisticsData.get(command), @@ -104,7 +104,7 @@ export class PerformanceStatistics { this.statistics.statisticsData.has(command) && this.statistics.statisticsData.get(command)?.countError ) { - this.statistics.statisticsData.get(command).countError++; + ++this.statistics.statisticsData.get(command).countError; } else { this.statistics.statisticsData.set(command, { ...this.statistics.statisticsData.get(command), diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index 8608e480..df1c879b 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -1,3 +1,5 @@ +import type { AutomaticTransactionGeneratorConfiguration } from '../types'; + export class Constants { static readonly DEFAULT_BOOT_NOTIFICATION_INTERVAL = 60000; // Ms static readonly DEFAULT_HEARTBEAT_INTERVAL = 60000; // Ms @@ -5,7 +7,19 @@ export class Constants { static readonly CHARGING_STATION_DEFAULT_RESET_TIME = 60000; // Ms static readonly CHARGING_STATION_ATG_INITIALIZATION_TIME = 1000; // Ms - static readonly CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS = 0.25; // Hours + + static readonly DEFAULT_ATG_STOP_AFTER_HOURS = 0.25; // Hours + static readonly DEFAULT_ATG_CONFIGURATION: AutomaticTransactionGeneratorConfiguration = + Object.freeze({ + enable: false, + minDuration: 60, + maxDuration: 120, + minDelayBetweenTwoTransactions: 15, + maxDelayBetweenTwoTransactions: 30, + probabilityOfStart: 1, + stopAfterHours: Constants.DEFAULT_ATG_STOP_AFTER_HOURS, + stopOnConnectionFailure: true, + }); // See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string static readonly SEMVER_PATTERN = diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index eaa19ac8..513efb50 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -55,7 +55,7 @@ export class WorkerSet extends WorkerAbstract { id: WorkerMessageEvents.startWorkerElement, data: elementData, }); - this.getLastWorkerSetElement().numberOfWorkerElements++; + ++this.getLastWorkerSetElement().numberOfWorkerElements; // Start element sequentially to optimize memory at startup if (this.workerOptions.elementStartDelay > 0) { await WorkerUtils.sleep(this.workerOptions.elementStartDelay); -- 2.34.1