import { RequestCommand } from '../types/ocpp/Requests';
import {
AuthorizationStatus,
- AuthorizeRequest,
- AuthorizeResponse,
- StartTransactionRequest,
- StartTransactionResponse,
+ type AuthorizeRequest,
+ type AuthorizeResponse,
+ type StartTransactionRequest,
+ type StartTransactionResponse,
StopTransactionReason,
- StopTransactionResponse,
+ type StopTransactionResponse,
} from '../types/ocpp/Transaction';
import Constants from '../utils/Constants';
import logger from '../utils/Logger';
automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration,
chargingStation: ChargingStation
) {
+ this.started = false;
this.configuration = automaticTransactionGeneratorConfiguration;
this.chargingStation = chargingStation;
this.connectorsStatus = new Map<number, Status>();
this.stopConnectors();
- this.started = false;
}
public static getInstance(
public start(): void {
if (this.started === true) {
- logger.warn(`${this.logPrefix()} trying to start while already started`);
+ logger.warn(`${this.logPrefix()} is already started`);
return;
}
this.startConnectors();
public stop(): void {
if (this.started === false) {
- logger.warn(`${this.logPrefix()} trying to stop while not started`);
+ logger.warn(`${this.logPrefix()} is already stopped`);
return;
}
this.stopConnectors();
public startConnector(connectorId: number): void {
if (this.chargingStation.connectors.has(connectorId) === false) {
- logger.warn(`${this.logPrefix(connectorId)} trying to start on non existing connector`);
+ logger.warn(`${this.logPrefix(connectorId)} starting on non existing connector`);
return;
}
if (this.connectorsStatus.get(connectorId)?.start === false) {
});
});
} else if (this.connectorsStatus.get(connectorId)?.start === true) {
- logger.warn(`${this.logPrefix(connectorId)} already started on connector`);
+ logger.warn(`${this.logPrefix(connectorId)} is already started on connector`);
}
}
public stopConnector(connectorId: number): void {
- this.connectorsStatus.set(connectorId, {
- ...this.connectorsStatus.get(connectorId),
- start: false,
- });
+ if (this.chargingStation.connectors.has(connectorId) === false) {
+ logger.warn(`${this.logPrefix(connectorId)} stopping on non existing connector`);
+ return;
+ }
+ if (this.connectorsStatus.get(connectorId)?.start === true) {
+ this.connectorsStatus.set(connectorId, {
+ ...this.connectorsStatus.get(connectorId),
+ start: false,
+ });
+ } else if (this.connectorsStatus.get(connectorId)?.start === false) {
+ logger.warn(`${this.logPrefix(connectorId)} is already stopped on connector`);
+ }
}
private startConnectors(): void {
this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
// Start transaction
const startResponse = await this.startTransaction(connectorId);
- this.connectorsStatus.get(connectorId).startTransactionRequests++;
- if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
- logger.warn(this.logPrefix(connectorId) + ' start transaction rejected');
- this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
- } else {
+ if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
// Wait until end of transaction
const waitTrxEnd =
Utils.getRandomInteger(this.configuration.maxDuration, this.configuration.minDuration) *
' started and will stop in ' +
Utils.formatDurationMilliSeconds(waitTrxEnd)
);
- this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
await Utils.sleep(waitTrxEnd);
// Stop transaction
logger.info(
}
this.connectorsStatus.get(connectorId).lastRunDate = new Date();
}
- await this.stopTransaction(connectorId);
this.connectorsStatus.get(connectorId).stoppedDate = new Date();
logger.info(
this.logPrefix(connectorId) +
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;
this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0;
this.connectorsStatus.get(connectorId).skippedTransactions =
this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0;
private async startTransaction(
connectorId: number
- ): Promise<StartTransactionResponse | AuthorizeResponse> {
+ ): Promise<StartTransactionResponse | undefined> {
const measureId = 'StartTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
let startResponse: StartTransactionResponse;
const idTag = this.chargingStation.getRandomIdTag();
const startTransactionLogMsg = `${this.logPrefix(
connectorId
- )} start transaction for idTag '${idTag}'`;
+ )} start transaction with an idTag '${idTag}'`;
if (this.getRequireAuthorize()) {
this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag;
// Authorize idTag
connectorId,
idTag,
});
+ this.handleStartTransactionResponse(connectorId, startResponse);
PerformanceStatistics.endMeasure(measureId, beginId);
return startResponse;
}
this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++;
PerformanceStatistics.endMeasure(measureId, beginId);
- return authorizeResponse;
+ return startResponse;
}
logger.info(startTransactionLogMsg);
// Start transaction
connectorId,
idTag,
});
+ this.handleStartTransactionResponse(connectorId, startResponse);
PerformanceStatistics.endMeasure(measureId, beginId);
return startResponse;
}
StartTransactionRequest,
StartTransactionResponse
>(this.chargingStation, RequestCommand.START_TRANSACTION, { connectorId });
+ this.handleStartTransactionResponse(connectorId, startResponse);
PerformanceStatistics.endMeasure(measureId, beginId);
return startResponse;
}
const measureId = 'StopTransaction with ATG';
const beginId = PerformanceStatistics.beginMeasure(measureId);
let stopResponse: StopTransactionResponse;
- if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) {
+ if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason);
this.connectorsStatus.get(connectorId).stopTransactionRequests++;
+ if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+ this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests++;
+ } else {
+ this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++;
+ }
} else {
const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId;
logger.warn(
- `${this.logPrefix(connectorId)} trying to stop a not started transaction${
+ `${this.logPrefix(connectorId)} stopping a not started transaction${
transactionId ? ' ' + transactionId.toString() : ''
}`
);
}:`
);
}
+
+ private handleStartTransactionResponse(
+ connectorId: number,
+ startResponse: StartTransactionResponse
+ ): void {
+ this.connectorsStatus.get(connectorId).startTransactionRequests++;
+ if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+ this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++;
+ } else {
+ logger.warn(this.logPrefix(connectorId) + ' start transaction rejected');
+ this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++;
+ }
+ }
}