import ChargingStation from './ChargingStation';
import Constants from '../utils/Constants';
import PerformanceStatistics from '../performance/PerformanceStatistics';
+import { Status } from '../types/AutomaticTransactionGenerator';
import Utils from '../utils/Utils';
import logger from '../utils/Logger';
export default class AutomaticTransactionGenerator {
public started: boolean;
private chargingStation: ChargingStation;
- private connectorsStartStatus: Record<number, boolean>;
- private startDate!: Date;
- private lastRunDate!: Date;
- private stopDate!: Date;
+ private connectorsStatus: Map<number, Status>;
constructor(chargingStation: ChargingStation) {
this.chargingStation = chargingStation;
- this.connectorsStartStatus = {} as Record<number, boolean>;
+ this.connectorsStatus = new Map<number, Status>();
this.stopConnectors();
this.started = false;
}
logger.error(`${this.logPrefix()} trying to start while already started`);
return;
}
- const previousRunDuration = (this?.startDate && this?.lastRunDate) ? (this.lastRunDate.getTime() - this.startDate.getTime()) : 0;
- this.startDate = new Date();
- this.lastRunDate = this.startDate;
- this.stopDate = new Date(this.startDate.getTime()
- + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000
- - previousRunDuration);
this.startConnectors();
this.started = true;
- logger.info(this.logPrefix() + ' started and will run for ' + Utils.formatDurationMilliSeconds(this.stopDate.getTime() - this.startDate.getTime()));
}
public stop(): void {
}
this.stopConnectors();
this.started = false;
- logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. Stopping all transactions`);
}
private startConnectors(): void {
}
private async startConnector(connectorId: number): Promise<void> {
- logger.info(this.logPrefix(connectorId) + ' started on connector');
- let skippedTransactions = 0;
- let skippedTransactionsTotal = 0;
- this.connectorsStartStatus[connectorId] = true;
- while (this.connectorsStartStatus[connectorId]) {
- if ((new Date()) > this.stopDate) {
- this.stop();
+ 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) {
+ if ((new Date()) > this.connectorsStatus.get(connectorId).stopDate) {
+ this.stopConnector(connectorId);
break;
}
if (!this.chargingStation.isRegistered()) {
- logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered');
+ logger.error(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is not registered');
+ this.stopConnector(connectorId);
break;
}
if (!this.chargingStation.isChargingStationAvailable()) {
- logger.info(this.logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable');
- this.stop();
+ logger.info(this.logPrefix(connectorId) + ' entered in transaction loop while the charging station is unavailable');
+ this.stopConnector(connectorId);
break;
}
if (!this.chargingStation.isConnectorAvailable(connectorId)) {
- logger.info(`${this.logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`);
+ logger.info(`${this.logPrefix(connectorId)} entered in transaction loop while the connector ${connectorId} is unavailable`);
this.stopConnector(connectorId);
break;
}
if (!this.chargingStation?.ocppRequestService) {
- logger.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`);
+ 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);
await Utils.sleep(wait);
const start = Utils.secureRandom();
if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
- skippedTransactions = 0;
+ this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
// Start transaction
const startResponse = await this.startTransaction(connectorId);
if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
- logger.warn(this.logPrefix(connectorId) + ' transaction rejected');
+ logger.warn(this.logPrefix(connectorId) + ' start transaction rejected');
await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
} 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() + ' will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd));
+ logger.info(this.logPrefix(connectorId) + ' transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString() + ' started and will stop in ' + Utils.formatDurationMilliSeconds(waitTrxEnd));
await Utils.sleep(waitTrxEnd);
// Stop transaction
logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString());
await this.stopTransaction(connectorId);
}
} else {
- skippedTransactions++;
- skippedTransactionsTotal++;
- logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + skippedTransactions.toString() + '/' + skippedTransactionsTotal.toString());
+ this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++;
+ this.connectorsStatus.get(connectorId).skippedTransactions++;
+ logger.info(this.logPrefix(connectorId) + ' skipped consecutively ' + this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions.toString() + '/' + this.connectorsStatus.get(connectorId).skippedTransactions.toString() + ' transaction(s)');
}
- this.lastRunDate = new Date();
+ this.connectorsStatus.get(connectorId).lastRunDate = new Date();
}
await this.stopTransaction(connectorId);
- logger.info(this.logPrefix(connectorId) + ' stopped on connector');
+ this.connectorsStatus.get(connectorId).stoppedDate = new Date();
+ logger.info(this.logPrefix(connectorId) + ' stopped on connector and lasted for ' + Utils.formatDurationMilliSeconds(this.connectorsStatus.get(connectorId).stoppedDate.getTime() - this.connectorsStatus.get(connectorId).startDate.getTime()));
+ logger.debug(`${this.logPrefix(connectorId)} connector status %j`, this.connectorsStatus.get(connectorId));
}
private stopConnector(connectorId: number): void {
- this.connectorsStartStatus[connectorId] = false;
+ this.connectorsStatus.set(connectorId, { start: false });
+ }
+
+ private initStartConnectorStatus(connectorId: number): void {
+ this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
+ 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;
+ this.connectorsStatus.get(connectorId).startDate = new Date();
+ this.connectorsStatus.get(connectorId).stopDate = new Date(this.connectorsStatus.get(connectorId).startDate.getTime()
+ + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000
+ - previousRunDuration);
+ this.connectorsStatus.get(connectorId).start = true;
}
private async startTransaction(connectorId: number): Promise<StartTransactionResponse | AuthorizeResponse> {