X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=31d4d85284354675676012136f30c2a2a85fd781;hb=4faad557cd49253297c7d0230db2eecfd850b4f4;hp=32d9d2e3ec9224dfc0bc2520e8a48c989313dca3;hpb=29bf6658d0689f5df26575d3b171163fe1d52d04;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 32d9d2e3..31d4d852 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,4 +1,4 @@ -import { AuthorizationStatus, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction'; +import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction'; import { PerformanceObserver, performance } from 'perf_hooks'; import ChargingStation from './ChargingStation'; @@ -39,12 +39,12 @@ export default class AutomaticTransactionGenerator { if (this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours && this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { setTimeout(() => { - this.stop(); + void this.stop(); }, this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); } for (const connector in this._chargingStation.connectors) { if (Utils.convertToInt(connector) > 0) { - this.startConnector(Utils.convertToInt(connector)); + void this.startConnector(Utils.convertToInt(connector)); } } logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); @@ -63,20 +63,33 @@ export default class AutomaticTransactionGenerator { async startConnector(connectorId: number): Promise { do { + if (this._timeToStop) { + logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made'); + break; + } + if (!this._chargingStation._isRegistered()) { + logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered'); + break; + } + if (!this._chargingStation._isChargingStationAvailable()) { + logger.info(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable'); + await this.stop(); + break; + } + if (!this._chargingStation._isConnectorAvailable(connectorId)) { + logger.info(`${this._logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`); + break; + } const wait = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.milliSecondsToHHMMSS(wait)); await Utils.sleep(wait); - if (this._timeToStop) { - logger.debug(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made'); - break; - } const start = Math.random(); let skip = 0; if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { skip = 0; // Start transaction - let startResponse: StartTransactionResponse; + let startResponse: StartTransactionResponse | AuthorizeResponse; if (this._chargingStation.getEnableStatistics()) { const startTransaction = performance.timerify(this.startTransaction); this._performanceObserver.observe({ entryTypes: ['function'] }); @@ -84,7 +97,7 @@ export default class AutomaticTransactionGenerator { } else { startResponse = await this.startTransaction(connectorId, this); } - if (startResponse.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { + if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { logger.info(this._logPrefix(connectorId) + ' transaction rejected'); await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); } else { @@ -94,7 +107,7 @@ export default class AutomaticTransactionGenerator { logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.milliSecondsToHHMMSS(waitTrxEnd)); await Utils.sleep(waitTrxEnd); // Stop transaction - if (this._chargingStation.getConnector(connectorId).transactionStarted) { + if (this._chargingStation.getConnector(connectorId)?.transactionStarted) { logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId.toString()); if (this._chargingStation.getEnableStatistics()) { const stopTransaction = performance.timerify(this.stopTransaction); @@ -114,10 +127,21 @@ export default class AutomaticTransactionGenerator { } // eslint-disable-next-line consistent-this - async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { + private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { if (self._chargingStation.hasAuthorizedTags()) { const tagId = self._chargingStation.getRandomTagId(); + if (self._chargingStation.stationInfo.AutomaticTransactionGenerator.requireAuthorize) { + // Authorize tagId + const authorizeResponse = await self._chargingStation.sendAuthorize(tagId); + if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { + logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); + // Start transaction + return await self._chargingStation.sendStartTransaction(connectorId, tagId); + } + return authorizeResponse; + } logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); + // Start transaction return await self._chargingStation.sendStartTransaction(connectorId, tagId); } logger.info(self._logPrefix(connectorId) + ' start transaction without a tagID'); @@ -125,7 +149,7 @@ export default class AutomaticTransactionGenerator { } // eslint-disable-next-line consistent-this - async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { + private async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { return await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId); } }