X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FAutomaticTransactionGenerator.ts;h=948ef133acb6adf2a3b13fe7d82520e3bcef6348;hb=821c6c8229d6dfdc93ce917dfaad74e681e5a35c;hp=5f94ec42d00ea91a770c6a5ac58e5bee74895647;hpb=10570d974fe84597ed0067bb41edc104ffa64167;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 5f94ec42..948ef133 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -1,130 +1,156 @@ -import { PerformanceObserver, performance } from 'perf_hooks'; +// Partial Copyright Jerome Benoit. 2021. All Rights Reserved. + +import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction'; import ChargingStation from './ChargingStation'; import Constants from '../utils/Constants'; +import PerformanceStatistics from '../utils/PerformanceStatistics'; import Utils from '../utils/Utils'; import logger from '../utils/Logger'; export default class AutomaticTransactionGenerator { - private _chargingStation: ChargingStation; - private _timeToStop: boolean; - private _performanceObserver: PerformanceObserver; + public timeToStop: boolean; + private chargingStation: ChargingStation; constructor(chargingStation: ChargingStation) { - this._chargingStation = chargingStation; - this._timeToStop = true; - if (this._chargingStation.getEnableStatistics()) { - this._performanceObserver = new PerformanceObserver((list) => { - const entry = list.getEntries()[0]; - this._chargingStation.statistics.logPerformance(entry, Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR); - this._performanceObserver.disconnect(); - }); - } - } - - get timeToStop(): boolean { - return this._timeToStop; + this.chargingStation = chargingStation; + this.timeToStop = true; } - _logPrefix(connectorId: number = null): string { - if (connectorId) { - return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG on connector #' + connectorId + ':'); + public async start(): Promise { + this.timeToStop = false; + if (this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours && + this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { + // eslint-disable-next-line @typescript-eslint/no-misused-promises + setTimeout(async (): Promise => { + await this.stop(); + }, this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); } - return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG:'); - } - - start(): void { - this._timeToStop = false; - if (this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours && - this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) { - setTimeout(() => { - this.stop(); - }, this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000); - } - for (const connector in this._chargingStation.connectors) { + for (const connector in this.chargingStation.connectors) { if (Utils.convertToInt(connector) > 0) { - this.startConnector(Utils.convertToInt(connector)); + await this.startConnector(Utils.convertToInt(connector)); } } - logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondstoHHMMSS(this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); + logger.info(this.logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600)); } - async stop(reason = ''): Promise { - logger.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS'); - for (const connector in this._chargingStation.connectors) { - if (this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { - logger.info(this._logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId); - await this._chargingStation.sendStopTransaction(this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId, reason); + public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise { + logger.info(this.logPrefix() + ' ATG OVER => 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)) + ' ATG OVER. Stop transaction ' + transactionId.toString()); + await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + this.chargingStation.getTransactionIdTag(transactionId), reason); } } - this._timeToStop = true; + this.timeToStop = true; } - async startConnector(connectorId: number): Promise { + private async startConnector(connectorId: number): Promise { do { - const wait = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions, - this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000; - logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000)); - await Utils.sleep(wait); - if (this._timeToStop) { - logger.debug(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made'); + 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; + } + if (!this.chargingStation?.ocppRequestService) { + logger.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`); + do { + await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); + } while (!this.chargingStation?.ocppRequestService); + } + 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); const start = Math.random(); let skip = 0; - if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { - skip = 0; + if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) { // Start transaction - let startResponse; - if (this._chargingStation.getEnableStatistics()) { - const startTransaction = performance.timerify(this.startTransaction); - this._performanceObserver.observe({ entryTypes: ['function'] }); - startResponse = await startTransaction(connectorId, this); - } else { - startResponse = await this.startTransaction(connectorId, this); - } - if (startResponse.idTagInfo.status !== 'Accepted') { - logger.info(this._logPrefix(connectorId) + ' transaction rejected'); + const startResponse = await this.startTransaction(connectorId); + if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) { + logger.warn(this.logPrefix(connectorId) + ' transaction rejected'); await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME); } else { // Wait until end of transaction - const waitTrxEnd = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, - this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; - logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId + ' will stop in ' + Utils.secondstoHHMMSS(waitTrxEnd / 1000)); + const waitTrxEnd = Utils.getRandomInt(this.chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration, + this.chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000; + 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) { - logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId); - if (this._chargingStation.getEnableStatistics()) { - const stopTransaction = performance.timerify(this.stopTransaction); - this._performanceObserver.observe({ entryTypes: ['function'] }); - await stopTransaction(connectorId, this); - } else { - await this.stopTransaction(connectorId, this); - } + if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { + logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString()); + await this.stopTransaction(connectorId); } } } else { skip++; - logger.info(this._logPrefix(connectorId) + ' transaction skipped ' + skip.toString()); + logger.info(this.logPrefix(connectorId) + ' transaction skipped ' + skip.toString()); } - } while (!this._timeToStop); - logger.info(this._logPrefix(connectorId) + ' ATG STOPPED on the connector'); + } while (!this.timeToStop); + logger.info(this.logPrefix(connectorId) + ' ATG STOPPED on the connector'); } // eslint-disable-next-line consistent-this - async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { - if (self._chargingStation.hasAuthorizedTags()) { - const tagId = self._chargingStation.getRandomTagId(); - logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId); - return await self._chargingStation.sendStartTransaction(connectorId, tagId); + private async startTransaction(connectorId: number): Promise { + const measureId = 'StartTransaction with ATG'; + const beginId = PerformanceStatistics.beginMeasure(measureId); + let startResponse: StartTransactionResponse; + if (this.chargingStation.hasAuthorizedTags()) { + const tagId = this.chargingStation.getRandomTagId(); + if (this.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) { + // Authorize tagId + const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(connectorId, tagId); + if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { + logger.info(this.logPrefix(connectorId) + ' start transaction for tagID ' + tagId); + // Start transaction + startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId); + PerformanceStatistics.endMeasure(measureId, beginId); + return startResponse; + } + PerformanceStatistics.endMeasure(measureId, beginId); + return authorizeResponse; + } + logger.info(this.logPrefix(connectorId) + ' start transaction for tagID ' + tagId); + // Start transaction + startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId); + PerformanceStatistics.endMeasure(measureId, beginId); + return startResponse; } - logger.info(self._logPrefix(connectorId) + ' start transaction without a tagID'); - return await self._chargingStation.sendStartTransaction(connectorId); + logger.info(this.logPrefix(connectorId) + ' start transaction without a tagID'); + startResponse = await this.chargingStation.ocppRequestService.sendStartTransaction(connectorId); + PerformanceStatistics.endMeasure(measureId, beginId); + return startResponse; } // eslint-disable-next-line consistent-this - async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise { - await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId); + private async stopTransaction(connectorId: number): 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)); + PerformanceStatistics.endMeasure(measureId, beginId); + return stopResponse; + } + + private logPrefix(connectorId?: number): string { + if (connectorId) { + return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG on connector #' + connectorId.toString() + ':'); + } + return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG:'); } }