X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=877339c2da8069e1a9fa5db799bffb0b864d1642;hb=331e20bbca5a7f404551666c4a3609ae92fc5e86;hp=37cbe1b00d2ff11a220f864df7a22fa297c163a5;hpb=9664ec50ce58f2fa682ad687a214d090a95d51f7;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 37cbe1b0..877339c2 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -40,27 +40,22 @@ export default class AutomaticTransactionGenerator { } private startConnectors(): void { - for (const connector in this.chargingStation.connectors) { - const connectorId = Utils.convertToInt(connector); + for (const connectorId of this.chargingStation.connectors.keys()) { if (connectorId > 0) { - // Avoid hogging the event loop with a busy loop - setImmediate(() => { - this.startConnector(connectorId).catch(() => { /* This is intentional */ }); - }); + this.startConnector(connectorId); } } } private stopConnectors(): void { - for (const connector in this.chargingStation.connectors) { - const connectorId = Utils.convertToInt(connector); + for (const connectorId of this.chargingStation.connectors.keys()) { if (connectorId > 0) { this.stopConnector(connectorId); } } } - private async startConnector(connectorId: number): Promise { + private async internalStartConnector(connectorId: number): Promise { this.initStartConnectorStatus(connectorId); logger.info(this.logPrefix(connectorId) + ' started on connector and will run for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stopDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime())); while (this.connectorsStatus.get(connectorId).start) { @@ -98,17 +93,19 @@ export default class AutomaticTransactionGenerator { this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; // Start transaction const startResponse = await this.startTransaction(connectorId); + this.connectorsStatus.get(connectorId).startTransactionRequests++; if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); - await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); + this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; } else { // Wait until end of transaction const waitTrxEnd = Utils.getRandomInteger(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); + logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd)); + this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; await Utils.sleep(waitTrxEnd); // Stop transaction - logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); + logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnectorStatus(connectorId).transactionId.toString()); await this.stopTransaction(connectorId); } } else { @@ -124,13 +121,27 @@ export default class AutomaticTransactionGenerator { logger.debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId)); } + private startConnector(connectorId: number): void { + // Avoid hogging the event loop with a busy loop + setImmediate(() => { + this.internalStartConnector(connectorId).catch(() => { /* This is intentional */ }); + }); + } + private stopConnector(connectorId: number): void { - this.connectorsStatus.set(connectorId, { start: false }); + this.connectorsStatus.set(connectorId, { ...this.connectorsStatus.get(connectorId), start: false }); } private initStartConnectorStatus(connectorId: number): void { + this.connectorsStatus.get(connectorId).authorizeRequests = this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0; + this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests = this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0; + this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests = this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0; + this.connectorsStatus.get(connectorId).startTransactionRequests = this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0; + this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests = this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0; + this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests = this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0; + this.connectorsStatus.get(connectorId).stopTransactionRequests = this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0; this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; - this.connectorsStatus.get(connectorId).skippedTransactions = 0; + this.connectorsStatus.get(connectorId).skippedTransactions = this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0; const previousRunDuration = (this?.connectorsStatus.get(connectorId)?.startDate && this?.connectorsStatus.get(connectorId)?.lastRunDate) ? (this.connectorsStatus.get(connectorId).lastRunDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()) : 0; @@ -150,13 +161,16 @@ export default class AutomaticTransactionGenerator { if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) { // Authorize idTag const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, idTag); + this.connectorsStatus.get(connectorId).authorizeRequests++; if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { + this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++; logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); // Start transaction startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, idTag); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } + this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++; PerformanceStatistics.endMeasure(measureId, beginId); return authorizeResponse; } @@ -177,12 +191,13 @@ export default class AutomaticTransactionGenerator { const beginId = PerformanceStatistics.beginMeasure(measureId); let transactionId = 0; let stopResponse: StopTransactionResponse; - if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { - transactionId = this.chargingStation.getConnector(connectorId).transactionId; + if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { + transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), this.chargingStation.getTransactionIdTag(transactionId), reason); + this.connectorsStatus.get(connectorId).stopTransactionRequests++; } else { logger.warn(`${this.logPrefix(connectorId)} trying to stop a not started transaction${transactionId ? ' ' + transactionId.toString() : ''}`); }