+import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/1.6/Transaction';
import { PerformanceObserver, performance } from 'perf_hooks';
import ChargingStation from './ChargingStation';
_logPrefix(connectorId: number = null): string {
if (connectorId) {
- return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG on connector #' + connectorId + ':');
+ return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG on connector #' + connectorId.toString() + ':');
}
return Utils.logPrefix(' ' + this._chargingStation.stationInfo.name + ' ATG:');
}
if (this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours &&
this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours > 0) {
setTimeout(() => {
- this.stop();
+ void this.stop();
}, this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600 * 1000);
}
for (const connector in this._chargingStation.connectors) {
if (Utils.convertToInt(connector) > 0) {
- this.startConnector(Utils.convertToInt(connector));
+ void this.startConnector(Utils.convertToInt(connector));
}
}
- logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondstoHHMMSS(this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600));
+ logger.info(this._logPrefix() + ' ATG started and will stop in ' + Utils.secondsToHHMMSS(this._chargingStation.stationInfo.AutomaticTransactionGenerator.stopAfterHours * 3600));
}
- async stop(reason = ''): Promise<void> {
+ async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
logger.info(this._logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
for (const connector in this._chargingStation.connectors) {
if (this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) {
- logger.info(this._logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId);
+ logger.info(this._logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId.toString());
await this._chargingStation.sendStopTransaction(this._chargingStation.getConnector(Utils.convertToInt(connector)).transactionId, reason);
}
}
async startConnector(connectorId: number): Promise<void> {
do {
- const wait = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
- this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
- logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.secondstoHHMMSS(wait / 1000));
- await Utils.sleep(wait);
if (this._timeToStop) {
- logger.debug(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
+ logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
+ break;
+ }
+ if (!this._chargingStation._isRegistered()) {
+ logger.error(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is not registered');
+ break;
+ }
+ if (!this._chargingStation._isChargingStationAvailable()) {
+ logger.info(this._logPrefix(connectorId) + ' Entered in transaction loop while the charging station is unavailable');
+ await this.stop();
break;
}
+ if (!this._chargingStation._isConnectorAvailable(connectorId)) {
+ logger.info(`${this._logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`);
+ break;
+ }
+ const wait = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDelayBetweenTwoTransactions,
+ this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDelayBetweenTwoTransactions) * 1000;
+ logger.info(this._logPrefix(connectorId) + ' wait for ' + Utils.milliSecondsToHHMMSS(wait));
+ await Utils.sleep(wait);
const start = Math.random();
let skip = 0;
if (start < this._chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
skip = 0;
// Start transaction
- let startResponse;
+ let startResponse: StartTransactionResponse | AuthorizeResponse;
if (this._chargingStation.getEnableStatistics()) {
const startTransaction = performance.timerify(this.startTransaction);
this._performanceObserver.observe({ entryTypes: ['function'] });
} else {
startResponse = await this.startTransaction(connectorId, this);
}
- if (startResponse.idTagInfo.status !== 'Accepted') {
+ if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
logger.info(this._logPrefix(connectorId) + ' transaction rejected');
await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
} else {
// Wait until end of transaction
const waitTrxEnd = Utils.getRandomInt(this._chargingStation.stationInfo.AutomaticTransactionGenerator.maxDuration,
this._chargingStation.stationInfo.AutomaticTransactionGenerator.minDuration) * 1000;
- logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId + ' will stop in ' + Utils.secondstoHHMMSS(waitTrxEnd / 1000));
+ logger.info(this._logPrefix(connectorId) + ' transaction ' + this._chargingStation.getConnector(connectorId).transactionId.toString() + ' will stop in ' + Utils.milliSecondsToHHMMSS(waitTrxEnd));
await Utils.sleep(waitTrxEnd);
// Stop transaction
- if (this._chargingStation.getConnector(connectorId).transactionStarted) {
- logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId);
+ if (this._chargingStation.getConnector(connectorId)?.transactionStarted) {
+ logger.info(this._logPrefix(connectorId) + ' stop transaction ' + this._chargingStation.getConnector(connectorId).transactionId.toString());
if (this._chargingStation.getEnableStatistics()) {
const stopTransaction = performance.timerify(this.stopTransaction);
this._performanceObserver.observe({ entryTypes: ['function'] });
}
// eslint-disable-next-line consistent-this
- async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<unknown> {
+ private async startTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StartTransactionResponse | AuthorizeResponse> {
if (self._chargingStation.hasAuthorizedTags()) {
const tagId = self._chargingStation.getRandomTagId();
+ if (self._chargingStation.stationInfo.AutomaticTransactionGenerator.requireAuthorize) {
+ // Authorize tagId
+ const authorizeResponse = await self._chargingStation.sendAuthorize(tagId);
+ if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
+ logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
+ // Start transaction
+ return await self._chargingStation.sendStartTransaction(connectorId, tagId);
+ }
+ return authorizeResponse;
+ }
logger.info(self._logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
+ // Start transaction
return await self._chargingStation.sendStartTransaction(connectorId, tagId);
}
logger.info(self._logPrefix(connectorId) + ' start transaction without a tagID');
}
// eslint-disable-next-line consistent-this
- async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<void> {
- await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId);
+ private async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StopTransactionResponse> {
+ return await self._chargingStation.sendStopTransaction(self._chargingStation.getConnector(connectorId).transactionId);
}
}