// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+import { AsyncResource } from 'async_hooks';
+
import BaseError from '../exception/BaseError';
import PerformanceStatistics from '../performance/PerformanceStatistics';
import {
import type ChargingStation from './ChargingStation';
import { ChargingStationUtils } from './ChargingStationUtils';
-export default class AutomaticTransactionGenerator {
+const moduleName = 'AutomaticTransactionGenerator';
+
+export default class AutomaticTransactionGenerator extends AsyncResource {
private static readonly instances: Map<string, AutomaticTransactionGenerator> = new Map<
string,
AutomaticTransactionGenerator
automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration,
chargingStation: ChargingStation
) {
+ super(moduleName);
this.started = false;
this.configuration = automaticTransactionGeneratorConfiguration;
this.chargingStation = chargingStation;
throw new BaseError(`Connector ${connectorId} does not exist`);
}
if (this.connectorsStatus.get(connectorId)?.start === false) {
- // Avoid hogging the event loop with a busy loop
- setImmediate(() => {
- this.internalStartConnector(connectorId).catch(() => {
- /* This is intentional */
- });
+ this.runInAsyncScope(
+ this.internalStartConnector.bind(this) as (
+ this: AutomaticTransactionGenerator,
+ ...args: any[]
+ ) => Promise<void>,
+ this,
+ connectorId
+ ).catch(() => {
+ /* This is intentional */
});
} else if (this.connectorsStatus.get(connectorId)?.start === true) {
logger.warn(`${this.logPrefix(connectorId)} is already started on connector`);
}
private async internalStartConnector(connectorId: number): Promise<void> {
- this.initializeConnectorStatus(connectorId);
- this.connectorsStatus.get(connectorId).start = true;
+ this.setStartConnectorStatus(connectorId);
logger.info(
this.logPrefix(connectorId) +
' started on connector and will run for ' +
this.stopConnector(connectorId);
break;
}
- if (!this.chargingStation.isInAcceptedState()) {
+ if (this.chargingStation.isInAcceptedState() === false) {
logger.error(
this.logPrefix(connectorId) +
' entered in transaction loop while the charging station is not in accepted state'
this.stopConnector(connectorId);
break;
}
- if (!this.chargingStation.isChargingStationAvailable()) {
+ if (this.chargingStation.isChargingStationAvailable() === false) {
logger.info(
this.logPrefix(connectorId) +
' entered in transaction loop while the charging station is unavailable'
this.stopConnector(connectorId);
break;
}
- if (!this.chargingStation.isConnectorAvailable(connectorId)) {
+ if (this.chargingStation.isConnectorAvailable(connectorId) === false) {
logger.info(
`${this.logPrefix(
connectorId
);
}
- private initializeConnectorStatus(connectorId: number): void {
- this.connectorsStatus.get(connectorId).authorizeRequests =
- this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0;
- this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests =
- this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0;
- this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests =
- this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0;
- this.connectorsStatus.get(connectorId).startTransactionRequests =
- this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests =
- this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests =
- this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).stopTransactionRequests =
- this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests =
- this?.connectorsStatus.get(connectorId)?.acceptedStopTransactionRequests ?? 0;
- this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests =
- this?.connectorsStatus.get(connectorId)?.rejectedStopTransactionRequests ?? 0;
+ private setStartConnectorStatus(connectorId: number): void {
this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
- this.connectorsStatus.get(connectorId).skippedTransactions =
- this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0;
const previousRunDuration =
this?.connectorsStatus.get(connectorId)?.startDate &&
this?.connectorsStatus.get(connectorId)?.lastRunDate
1000 -
previousRunDuration
);
- this.connectorsStatus.get(connectorId).start =
- this?.connectorsStatus.get(connectorId)?.start ?? false;
+ this.connectorsStatus.get(connectorId).start = true;
}
private initializeConnectorsStatus(): void {