Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Optimize ATG run duration handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
Jérôme Benoit committed Sep 18, 2021
1 parent 2d7040b commit 265e426
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
23 changes: 14 additions & 9 deletions src/charging-station/AutomaticTransactionGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import Utils from '../utils/Utils';
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
Expand All @@ -38,7 +39,11 @@ export default class AutomaticTransactionGenerator {
}

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) {
Expand All @@ -47,14 +52,14 @@ export default class AutomaticTransactionGenerator {
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;
Expand Down Expand Up @@ -107,7 +112,7 @@ export default class AutomaticTransactionGenerator {
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');
}
Expand Down
50 changes: 25 additions & 25 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class ChargingStation {
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[];
Expand All @@ -55,9 +55,9 @@ export default class ChargingStation {
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) {
Expand All @@ -66,8 +66,8 @@ export default class ChargingStation {
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>();
Expand Down Expand Up @@ -345,7 +345,7 @@ export default class ChargingStation {
this.performanceStatistics.stop();
}
this.bootNotificationResponse = null;
this.hasStopped = true;
this.stopped = true;
}

public getConfigurationKey(key: string | StandardParametersKey, caseInsensitive = false): ConfigurationKey | undefined {
Expand Down Expand Up @@ -619,15 +619,15 @@ export default class ChargingStation {
}
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> {
Expand Down Expand Up @@ -839,15 +839,15 @@ export default class ChargingStation {
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 {
Expand All @@ -862,11 +862,11 @@ export default class ChargingStation {

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();
}
}
}
Expand All @@ -878,9 +878,9 @@ export default class ChargingStation {
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) {
Expand Down Expand Up @@ -1011,8 +1011,8 @@ export default class ChargingStation {
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()) {
Expand Down Expand Up @@ -1043,9 +1043,9 @@ export default class ChargingStation {
// 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++;
Expand All @@ -1055,7 +1055,7 @@ export default class ChargingStation {
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()})`);
}
Expand Down

0 comments on commit 265e426

Please sign in to comment.