import { AuthorizationStatus, AuthorizeResponse, StartTransactionResponse, StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction';
-import { PerformanceObserver, performance } from 'perf_hooks';
import ChargingStation from './ChargingStation';
import Constants from '../utils/Constants';
+import { PerformanceObserver } from 'perf_hooks';
+import PerformanceStatistics from '../utils/PerformanceStatistics';
import Utils from '../utils/Utils';
import logger from '../utils/Logger';
export default class AutomaticTransactionGenerator {
public timeToStop: boolean;
private chargingStation: ChargingStation;
- private performanceObserver: PerformanceObserver;
+ private performanceObserver!: PerformanceObserver;
constructor(chargingStation: ChargingStation) {
this.chargingStation = chargingStation;
this.timeToStop = true;
if (this.chargingStation.getEnableStatistics()) {
- this.performanceObserver = new PerformanceObserver((list) => {
- const entry = list.getEntries()[0];
- this.chargingStation.statistics.logPerformance(entry, Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR);
- this.performanceObserver.disconnect();
- });
+ this.performanceObserver = PerformanceStatistics.initPerformanceObserver(Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR, this.chargingStation.performanceStatistics,
+ this.performanceObserver);
}
}
const transactionId = this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId;
if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) {
logger.info(this.logPrefix(Utils.convertToInt(connector)) + ' ATG OVER. Stop transaction ' + transactionId.toString());
- await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getTransactionMeterStop(transactionId),
+ await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
this.chargingStation.getTransactionIdTag(transactionId), reason);
}
}
this.timeToStop = true;
}
- public async startConnector(connectorId: number): Promise<void> {
+ private async startConnector(connectorId: number): Promise<void> {
do {
if (this.timeToStop) {
logger.error(this.logPrefix(connectorId) + ' Entered in transaction loop while a request to stop it was made');
const start = Math.random();
let skip = 0;
if (start < this.chargingStation.stationInfo.AutomaticTransactionGenerator.probabilityOfStart) {
- skip = 0;
// Start transaction
- let startResponse: StartTransactionResponse | AuthorizeResponse;
+ let startTransaction: (connectorId: number, self: AutomaticTransactionGenerator) => Promise<StartTransactionResponse | AuthorizeResponse>;
if (this.chargingStation.getEnableStatistics()) {
- const startTransaction = performance.timerify(this.startTransaction);
- this.performanceObserver.observe({ entryTypes: ['function'] });
- startResponse = await startTransaction(connectorId, this);
+ startTransaction = PerformanceStatistics.timedFunction(this.startTransaction.bind(this), this.performanceObserver);
} else {
- startResponse = await this.startTransaction(connectorId, this);
+ startTransaction = this.startTransaction.bind(this);
}
+ const startResponse = await startTransaction(connectorId, this);
if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
- logger.info(this.logPrefix(connectorId) + ' transaction rejected');
+ logger.warn(this.logPrefix(connectorId) + ' transaction rejected');
await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
} else {
// Wait until end of transaction
// Stop transaction
if (this.chargingStation.getConnector(connectorId)?.transactionStarted) {
logger.info(this.logPrefix(connectorId) + ' stop transaction ' + this.chargingStation.getConnector(connectorId).transactionId.toString());
+ let stopTransaction: (connectorId: number, self: AutomaticTransactionGenerator) => Promise<StopTransactionResponse>;
if (this.chargingStation.getEnableStatistics()) {
- const stopTransaction = performance.timerify(this.stopTransaction);
- this.performanceObserver.observe({ entryTypes: ['function'] });
- await stopTransaction(connectorId, this);
+ stopTransaction = PerformanceStatistics.timedFunction(this.stopTransaction.bind(this), this.performanceObserver);
} else {
- await this.stopTransaction(connectorId, this);
+ stopTransaction = this.stopTransaction.bind(this);
}
+ await stopTransaction(connectorId, this);
}
}
} else {
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) {
+ if (self.chargingStation.getAutomaticTransactionGeneratorRequireAuthorize()) {
// Authorize tagId
- const authorizeResponse = await self.chargingStation.ocppRequestService.sendAuthorize(tagId);
+ const authorizeResponse = await self.chargingStation.ocppRequestService.sendAuthorize(connectorId, tagId);
if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
logger.info(self.logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
// Start transaction
- return await self.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId);
+ return self.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId);
}
return authorizeResponse;
}
logger.info(self.logPrefix(connectorId) + ' start transaction for tagID ' + tagId);
// Start transaction
- return await self.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId);
+ return self.chargingStation.ocppRequestService.sendStartTransaction(connectorId, tagId);
}
logger.info(self.logPrefix(connectorId) + ' start transaction without a tagID');
- return await self.chargingStation.ocppRequestService.sendStartTransaction(connectorId);
+ return self.chargingStation.ocppRequestService.sendStartTransaction(connectorId);
}
// eslint-disable-next-line consistent-this
private async stopTransaction(connectorId: number, self: AutomaticTransactionGenerator): Promise<StopTransactionResponse> {
const transactionId = self.chargingStation.getConnector(connectorId).transactionId;
- return await self.chargingStation.ocppRequestService.sendStopTransaction(transactionId, self.chargingStation.getTransactionMeterStop(transactionId), self.chargingStation.getTransactionIdTag(transactionId));
+ return self.chargingStation.ocppRequestService.sendStopTransaction(transactionId, self.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
+ self.chargingStation.getTransactionIdTag(transactionId));
}
- private logPrefix(connectorId: number = null): string {
+ private logPrefix(connectorId?: number): string {
if (connectorId) {
- return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG on connector #' + connectorId.toString() + ':');
+ return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG on connector #' + connectorId.toString() + ':');
}
- return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG:');
+ return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' | ATG:');
}
}