1 import { AuthorizationStatus
, AuthorizeResponse
, StartTransactionResponse
, StopTransactionReason
, StopTransactionResponse
} from
'../types/ocpp/Transaction';
2 import { PerformanceObserver
, performance
} from
'perf_hooks';
4 import ChargingStation from
'./ChargingStation';
5 import Constants from
'../utils/Constants';
6 import Utils from
'../utils/Utils';
7 import logger from
'../utils/Logger';
9 export default class AutomaticTransactionGenerator
{
10 public timeToStop
: boolean;
11 private chargingStation
: ChargingStation
;
12 private performanceObserver
!: PerformanceObserver
;
14 constructor(chargingStation
: ChargingStation
) {
15 this.chargingStation
= chargingStation
;
16 this.timeToStop
= true;
17 if (this.chargingStation
.getEnableStatistics()) {
18 this.performanceObserver
= new PerformanceObserver((list
) => {
19 const entry
= list
.getEntries()[0];
20 this.chargingStation
.performanceStatistics
.logPerformance(entry
, Constants
.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR
);
21 this.performanceObserver
.disconnect();
26 public async start(): Promise
<void> {
27 this.timeToStop
= false;
28 if (this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
&&
29 this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
> 0) {
30 // eslint-disable-next-line @typescript-eslint/no-misused-promises
31 setTimeout(async (): Promise
<void> => {
33 }, this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600 * 1000);
35 for (const connector
in this.chargingStation
.connectors
) {
36 if (Utils
.convertToInt(connector
) > 0) {
37 await this.startConnector(Utils
.convertToInt(connector
));
40 logger
.info(this.logPrefix() + ' ATG started and will stop in ' + Utils
.secondsToHHMMSS(this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600));
43 public async stop(reason
: StopTransactionReason
= StopTransactionReason
.NONE
): Promise
<void> {
44 logger
.info(this.logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
45 for (const connector
in this.chargingStation
.connectors
) {
46 const transactionId
= this.chargingStation
.getConnector(Utils
.convertToInt(connector
)).transactionId
;
47 if (this.chargingStation
.getConnector(Utils
.convertToInt(connector
)).transactionStarted
) {
48 logger
.info(this.logPrefix(Utils
.convertToInt(connector
)) + ' ATG OVER. Stop transaction ' + transactionId
.toString());
49 await this.chargingStation
.ocppRequestService
.sendStopTransaction(transactionId
, this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
50 this.chargingStation
.getTransactionIdTag(transactionId
), reason
);
53 this.timeToStop
= true;
56 private async startConnector(connectorId
: number): Promise
<void> {
58 if (this.timeToStop
) {
59 logger
.error(this.logPrefix(connectorId
) + ' Entered in transaction loop while a request to stop it was made');
62 if (!this.chargingStation
.isRegistered()) {
63 logger
.error(this.logPrefix(connectorId
) + ' Entered in transaction loop while the charging station is not registered');
66 if (!this.chargingStation
.isChargingStationAvailable()) {
67 logger
.info(this.logPrefix(connectorId
) + ' Entered in transaction loop while the charging station is unavailable');
71 if (!this.chargingStation
.isConnectorAvailable(connectorId
)) {
72 logger
.info(`${this.logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`);
75 if (!this.chargingStation
?.ocppRequestService
) {
76 logger
.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`);
78 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_INITIALIZATION_TIME
);
79 } while (!this.chargingStation
?.ocppRequestService
);
81 const wait
= Utils
.getRandomInt(this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.maxDelayBetweenTwoTransactions
,
82 this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.minDelayBetweenTwoTransactions
) * 1000;
83 logger
.info(this.logPrefix(connectorId
) + ' wait for ' + Utils
.milliSecondsToHHMMSS(wait
));
84 await Utils
.sleep(wait
);
85 const start
= Math.random();
87 if (start
< this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.probabilityOfStart
) {
90 let startResponse
: StartTransactionResponse
| AuthorizeResponse
;
91 if (this.chargingStation
.getEnableStatistics()) {
92 const startTransaction
= performance
.timerify(this.startTransaction
);
93 this.performanceObserver
.observe({ entryTypes
: ['function'] });
94 startResponse
= await startTransaction(connectorId
, this);
96 startResponse
= await this.startTransaction(connectorId
, this);
98 if (startResponse
?.idTagInfo
?.status !== AuthorizationStatus
.ACCEPTED
) {
99 logger
.warn(this.logPrefix(connectorId
) + ' transaction rejected');
100 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_WAIT_TIME
);
102 // Wait until end of transaction
103 const waitTrxEnd
= Utils
.getRandomInt(this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.maxDuration
,
104 this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.minDuration
) * 1000;
105 logger
.info(this.logPrefix(connectorId
) + ' transaction ' + this.chargingStation
.getConnector(connectorId
).transactionId
.toString() + ' will stop in ' + Utils
.milliSecondsToHHMMSS(waitTrxEnd
));
106 await Utils
.sleep(waitTrxEnd
);
108 if (this.chargingStation
.getConnector(connectorId
)?.transactionStarted
) {
109 logger
.info(this.logPrefix(connectorId
) + ' stop transaction ' + this.chargingStation
.getConnector(connectorId
).transactionId
.toString());
110 if (this.chargingStation
.getEnableStatistics()) {
111 const stopTransaction
= performance
.timerify(this.stopTransaction
);
112 this.performanceObserver
.observe({ entryTypes
: ['function'] });
113 await stopTransaction(connectorId
, this);
115 await this.stopTransaction(connectorId
, this);
121 logger
.info(this.logPrefix(connectorId
) + ' transaction skipped ' + skip
.toString());
123 } while (!this.timeToStop
);
124 logger
.info(this.logPrefix(connectorId
) + ' ATG STOPPED on the connector');
127 // eslint-disable-next-line consistent-this
128 private async startTransaction(connectorId
: number, self: AutomaticTransactionGenerator
): Promise
<StartTransactionResponse
| AuthorizeResponse
> {
129 if (self.chargingStation
.hasAuthorizedTags()) {
130 const tagId
= self.chargingStation
.getRandomTagId();
131 if (self.chargingStation
.getAutomaticTransactionGeneratorRequireAuthorize()) {
133 const authorizeResponse
= await self.chargingStation
.ocppRequestService
.sendAuthorize(connectorId
, tagId
);
134 if (authorizeResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
135 logger
.info(self.logPrefix(connectorId
) + ' start transaction for tagID ' + tagId
);
137 return await self.chargingStation
.ocppRequestService
.sendStartTransaction(connectorId
, tagId
);
139 return authorizeResponse
;
141 logger
.info(self.logPrefix(connectorId
) + ' start transaction for tagID ' + tagId
);
143 return await self.chargingStation
.ocppRequestService
.sendStartTransaction(connectorId
, tagId
);
145 logger
.info(self.logPrefix(connectorId
) + ' start transaction without a tagID');
146 return await self.chargingStation
.ocppRequestService
.sendStartTransaction(connectorId
);
149 // eslint-disable-next-line consistent-this
150 private async stopTransaction(connectorId
: number, self: AutomaticTransactionGenerator
): Promise
<StopTransactionResponse
> {
151 const transactionId
= self.chargingStation
.getConnector(connectorId
).transactionId
;
152 return await self.chargingStation
.ocppRequestService
.sendStopTransaction(transactionId
, self.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
),
153 self.chargingStation
.getTransactionIdTag(transactionId
));
156 private logPrefix(connectorId
?: number): string {
158 return Utils
.logPrefix(' ' + this.chargingStation
.stationInfo
.chargingStationId
+ ' | ATG on connector #' + connectorId
.toString() + ':');
160 return Utils
.logPrefix(' ' + this.chargingStation
.stationInfo
.chargingStationId
+ ' | ATG:');