import logger from '../utils/Logger';
export default class AutomaticTransactionGenerator {
- public timeToStop: boolean;
+ public started: boolean;
private startDate!: Date;
+ private lastRunDate!: Date;
private stopDate!: Date;
- private runningDuration!: number;
private chargingStation: ChargingStation;
constructor(chargingStation: ChargingStation) {
this.chargingStation = chargingStation;
- this.timeToStop = true;
+ this.started = false;
}
public start(): void {
this.startDate = new Date();
+ this.lastRunDate = 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
- - (this.runningDuration ?? 0));
- this.timeToStop = false;
+ - (this.lastRunDate.getTime() - this.startDate.getTime()));
+ this.started = true;
for (const connector in this.chargingStation.connectors) {
if (Utils.convertToInt(connector) > 0) {
// Avoid hogging the event loop with a busy loop
}
public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
- logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.runningDuration ?? 0)}. Stopping all transactions`);
+ if (!this.started) {
+ logger.error(`${this.logPrefix()} trying to stop while not started`);
+ return;
+ }
+ logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.lastRunDate.getTime() - this.startDate.getTime())}. 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) {
this.chargingStation.getTransactionIdTag(transactionId), reason);
}
}
- this.timeToStop = true;
+ this.started = false;
}
private async startOnConnector(connectorId: number): Promise<void> {
logger.info(this.logPrefix(connectorId) + ' started on connector');
let transactionSkip = 0;
let totalTransactionSkip = 0;
- while (!this.timeToStop) {
+ while (this.started) {
if ((new Date()) > this.stopDate) {
await this.stop();
break;
totalTransactionSkip++;
logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + transactionSkip.toString() + '/' + totalTransactionSkip.toString());
}
- this.runningDuration = (new Date()).getTime() - this.startDate.getTime();
+ this.lastRunDate = new Date();
}
logger.info(this.logPrefix(connectorId) + ' stopped on connector');
}
public stationInfo!: ChargingStationInfo;
public connectors: Connectors;
public configuration!: ChargingStationConfiguration;
- public hasStopped: boolean;
+ public stopped: boolean;
public wsConnection!: WebSocket;
public requests: Map<string, Request>;
public messageQueue: string[];
private bootNotificationResponse!: BootNotificationResponse | null;
private connectorsConfigurationHash!: string;
private wsConnectionUrl!: URL;
- private hasSocketRestarted: boolean;
+ private wsConnectionRestarted: boolean;
private autoReconnectRetryCount: number;
- private automaticTransactionGeneration!: AutomaticTransactionGenerator;
+ private automaticTransactionGenerator!: AutomaticTransactionGenerator;
private webSocketPingSetInterval!: NodeJS.Timeout;
constructor(index: number, stationTemplateFile: string) {
this.connectors = {} as Connectors;
this.initialize();
- this.hasStopped = false;
- this.hasSocketRestarted = false;
+ this.stopped = false;
+ this.wsConnectionRestarted = false;
this.autoReconnectRetryCount = 0;
this.requests = new Map<string, Request>();
this.performanceStatistics.stop();
}
this.bootNotificationResponse = null;
- this.hasStopped = true;
+ this.stopped = true;
}
public getConfigurationKey(key: string | StandardParametersKey, caseInsensitive = false): ConfigurationKey | undefined {
}
if (this.isRegistered()) {
await this.startMessageSequence();
- this.hasStopped && (this.hasStopped = false);
- if (this.hasSocketRestarted && this.isWebSocketConnectionOpened()) {
+ this.stopped && (this.stopped = false);
+ if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) {
this.flushMessageQueue();
}
} else {
logger.error(`${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`);
}
this.autoReconnectRetryCount = 0;
- this.hasSocketRestarted = false;
+ this.wsConnectionRestarted = false;
}
private async onClose(code: number, reason: string): Promise<void> {
for (const connector in this.connectors) {
if (Utils.convertToInt(connector) === 0) {
continue;
- } else if (!this.hasStopped && !this.getConnector(Utils.convertToInt(connector))?.status && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
+ } else if (!this.stopped && !this.getConnector(Utils.convertToInt(connector))?.status && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
// Send status in template at startup
await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).bootStatus);
this.getConnector(Utils.convertToInt(connector)).status = this.getConnector(Utils.convertToInt(connector)).bootStatus;
- } else if (this.hasStopped && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
+ } else if (this.stopped && this.getConnector(Utils.convertToInt(connector))?.bootStatus) {
// Send status in template after reset
await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).bootStatus);
this.getConnector(Utils.convertToInt(connector)).status = this.getConnector(Utils.convertToInt(connector)).bootStatus;
- } else if (!this.hasStopped && this.getConnector(Utils.convertToInt(connector))?.status) {
+ } else if (!this.stopped && this.getConnector(Utils.convertToInt(connector))?.status) {
// Send previous status at template reload
await this.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), this.getConnector(Utils.convertToInt(connector)).status);
} else {
private startAutomaticTransactionGenerator() {
if (this.stationInfo.AutomaticTransactionGenerator.enable) {
- if (!this.automaticTransactionGeneration) {
- this.automaticTransactionGeneration = new AutomaticTransactionGenerator(this);
+ if (!this.automaticTransactionGenerator) {
+ this.automaticTransactionGenerator = new AutomaticTransactionGenerator(this);
}
- if (this.automaticTransactionGeneration.timeToStop) {
- this.automaticTransactionGeneration.start();
+ if (!this.automaticTransactionGenerator.started) {
+ this.automaticTransactionGenerator.start();
}
}
}
this.stopHeartbeat();
// Stop the ATG
if (this.stationInfo.AutomaticTransactionGenerator.enable &&
- this.automaticTransactionGeneration &&
- !this.automaticTransactionGeneration.timeToStop) {
- await this.automaticTransactionGeneration.stop(reason);
+ this.automaticTransactionGenerator &&
+ this.automaticTransactionGenerator.started) {
+ await this.automaticTransactionGenerator.stop(reason);
} else {
for (const connector in this.connectors) {
if (Utils.convertToInt(connector) > 0 && this.getConnector(Utils.convertToInt(connector)).transactionStarted) {
this.initialize();
// Restart the ATG
if (!this.stationInfo.AutomaticTransactionGenerator.enable &&
- this.automaticTransactionGeneration) {
- await this.automaticTransactionGeneration.stop();
+ this.automaticTransactionGenerator) {
+ await this.automaticTransactionGenerator.stop();
}
this.startAutomaticTransactionGenerator();
if (this.getEnableStatistics()) {
// Stop the ATG if needed
if (this.stationInfo.AutomaticTransactionGenerator.enable &&
this.stationInfo.AutomaticTransactionGenerator.stopOnConnectionFailure &&
- this.automaticTransactionGeneration &&
- !this.automaticTransactionGeneration.timeToStop) {
- await this.automaticTransactionGeneration.stop();
+ this.automaticTransactionGenerator &&
+ this.automaticTransactionGenerator.started) {
+ await this.automaticTransactionGenerator.stop();
}
if (this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() || this.getAutoReconnectMaxRetries() === -1) {
this.autoReconnectRetryCount++;
await Utils.sleep(reconnectDelay);
logger.error(this.logPrefix() + ' Socket: reconnecting try #' + this.autoReconnectRetryCount.toString());
this.openWSConnection({ handshakeTimeout: reconnectTimeout }, true);
- this.hasSocketRestarted = true;
+ this.wsConnectionRestarted = true;
} else if (this.getAutoReconnectMaxRetries() !== -1) {
logger.error(`${this.logPrefix()} Socket reconnect failure: max retries reached (${this.autoReconnectRetryCount}) or retry disabled (${this.getAutoReconnectMaxRetries()})`);
}