export default class AutomaticTransactionGenerator {
public timeToStop: boolean;
+ private startDate!: Date;
+ private stopDate!: Date;
+ private runningDuration!: number;
private chargingStation: ChargingStation;
constructor(chargingStation: ChargingStation) {
this.timeToStop = true;
}
- public async start(): Promise<void> {
+ public start(): void {
+ this.startDate = new Date();
+ this.stopDate = new Date(this.startDate.getTime()
+ + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000
+ - (this.runningDuration ?? 0));
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<void> => {
- await this.stop();
- }, this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000);
- }
for (const connector in this.chargingStation.connectors) {
if (Utils.convertToInt(connector) > 0) {
- await this.startConnector(Utils.convertToInt(connector));
+ // Avoid hogging the event loop with a busy loop
+ setImmediate(() => {
+ this.startOnConnector(Utils.convertToInt(connector)).catch(() => { /* This is intentional */ });
+ });
}
}
- logger.info(this.logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this.chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600));
+ 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<void> {
- logger.info(this.logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
+ logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.runningDuration ?? 0)}. 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());
+ 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.timeToStop = true;
}
- private async startConnector(connectorId: number): Promise<void> {
- do {
- if (this.timeToStop) {
- logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
+ private async startOnConnector(connectorId: number): Promise<void> {
+ logger.info(this.logPrefix(connectorId) + ' started on connector');
+ let transactionSkip = 0;
+ let totalTransactionSkip = 0;
+ while (!this.timeToStop) {
+ if ((new Date()) > this.stopDate) {
+ await this.stop();
break;
}
if (!this.chargingStation.isRegistered()) {
}
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));
+ logger.info(this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait));
await Utils.sleep(wait);
- const start = Math.random();
- let skip = 0;
+ const start = Utils.secureRandom();
if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
+ transactionSkip = 0;
// Start transaction
const startResponse = await this.startTransaction(connectorId);
if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
// 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.toString() + ' will stop in ' + Utils.milliSecondsToHHMMSS(waitTrxEnd));
+ logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd));
await Utils.sleep(waitTrxEnd);
// Stop transaction
if (this.chargingStation.getConnector(connectorId)?.transactionStarted) {
}
}
} else {
- skip++;
- logger.info(this.logPrefix(connectorId) + ' transaction skipped ' + skip.toString());
+ transactionSkip++;
+ totalTransactionSkip++;
+ logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + transactionSkip.toString() + '/' + totalTransactionSkip.toString());
}
- } while (!this.timeToStop);
- logger.info(this.logPrefix(connectorId) + ' ATG STOPPED on the connector');
+ this.runningDuration = (new Date()).getTime() - this.startDate.getTime();
+ }
+ logger.info(this.logPrefix(connectorId) + ' stopped on connector');
}
- // eslint-disable-next-line consistent-this
private async startTransaction(connectorId: number): Promise<StartTransactionResponse | AuthorizeResponse> {
const measureId = 'StartTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
return startResponse;
}
- // eslint-disable-next-line consistent-this
private async stopTransaction(connectorId: number): Promise<StopTransactionResponse> {
const measureId = 'StopTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);