import { AsyncResource } from 'node:async_hooks';
import type { ChargingStation } from './ChargingStation';
import { AsyncResource } from 'node:async_hooks';
import type { ChargingStation } from './ChargingStation';
import { IdTagsCache } from './IdTagsCache';
import { BaseError } from '../exception';
import { PerformanceStatistics } from '../performance';
import { IdTagsCache } from './IdTagsCache';
import { BaseError } from '../exception';
import { PerformanceStatistics } from '../performance';
): AutomaticTransactionGenerator | undefined {
if (AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo.hashId) === false) {
AutomaticTransactionGenerator.instances.set(
chargingStation.stationInfo.hashId,
): AutomaticTransactionGenerator | undefined {
if (AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo.hashId) === false) {
AutomaticTransactionGenerator.instances.set(
chargingStation.stationInfo.hashId,
this.runInAsyncScope(
this.internalStartConnector.bind(this) as (
this: AutomaticTransactionGenerator,
this.runInAsyncScope(
this.internalStartConnector.bind(this) as (
this: AutomaticTransactionGenerator,
).catch(Constants.EMPTY_FUNCTION);
} else if (this.connectorsStatus.get(connectorId)?.start === true) {
logger.warn(`${this.logPrefix(connectorId)} is already started on connector`);
).catch(Constants.EMPTY_FUNCTION);
} else if (this.connectorsStatus.get(connectorId)?.start === true) {
logger.warn(`${this.logPrefix(connectorId)} is already started on connector`);
)} started on connector and will run for ${formatDurationMilliSeconds(
this.connectorsStatus.get(connectorId).stopDate.getTime() -
)} started on connector and will run for ${formatDurationMilliSeconds(
this.connectorsStatus.get(connectorId).stopDate.getTime() -
);
while (this.connectorsStatus.get(connectorId)?.start === true) {
if (new Date() > this.connectorsStatus.get(connectorId).stopDate) {
);
while (this.connectorsStatus.get(connectorId)?.start === true) {
if (new Date() > this.connectorsStatus.get(connectorId).stopDate) {
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
.maxDelayBetweenTwoTransactions,
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
.maxDelayBetweenTwoTransactions,
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
) * 1000;
logger.info(
`${this.logPrefix(connectorId)} transaction started with id ${this.chargingStation
.getConnectorStatus(connectorId)
?.transactionId?.toString()} and will stop in ${formatDurationMilliSeconds(
) * 1000;
logger.info(
`${this.logPrefix(connectorId)} transaction started with id ${this.chargingStation
.getConnectorStatus(connectorId)
?.transactionId?.toString()} and will stop in ${formatDurationMilliSeconds(
);
await sleep(waitTrxEnd);
// Stop transaction
logger.info(
`${this.logPrefix(connectorId)} stop transaction with id ${this.chargingStation
.getConnectorStatus(connectorId)
);
await sleep(waitTrxEnd);
// Stop transaction
logger.info(
`${this.logPrefix(connectorId)} stop transaction with id ${this.chargingStation
.getConnectorStatus(connectorId)
)} stopped on connector and lasted for ${formatDurationMilliSeconds(
this.connectorsStatus.get(connectorId).stoppedDate.getTime() -
)} stopped on connector and lasted for ${formatDurationMilliSeconds(
this.connectorsStatus.get(connectorId).stoppedDate.getTime() -
): Promise<StartTransactionResponse | undefined> {
const measureId = 'StartTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
): Promise<StartTransactionResponse | undefined> {
const measureId = 'StartTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
const idTag = IdTagsCache.getInstance().getIdTag(
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.idTagDistribution,
this.chargingStation,
const idTag = IdTagsCache.getInstance().getIdTag(
this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.idTagDistribution,
this.chargingStation,
)} start transaction with an idTag '${idTag}'`;
if (this.getRequireAuthorize()) {
this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag;
)} start transaction with an idTag '${idTag}'`;
if (this.getRequireAuthorize()) {
this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag;
): Promise<StopTransactionResponse> {
const measureId = 'StopTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
): Promise<StopTransactionResponse> {
const measureId = 'StopTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
logger.warn(
`${this.logPrefix(connectorId)} stopping a not started transaction${
!isNullOrUndefined(transactionId) ? ` with id ${transactionId?.toString()}` : ''
logger.warn(
`${this.logPrefix(connectorId)} stopping a not started transaction${
!isNullOrUndefined(transactionId) ? ` with id ${transactionId?.toString()}` : ''
return logPrefix(
` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
!isNullOrUndefined(connectorId) ? ` on connector #${connectorId.toString()}` : ''
return logPrefix(
` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
!isNullOrUndefined(connectorId) ? ` on connector #${connectorId.toString()}` : ''
): void {
++this.connectorsStatus.get(connectorId).startTransactionRequests;
if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
): void {
++this.connectorsStatus.get(connectorId).startTransactionRequests;
if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {