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
.statistics
.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 setTimeout(async (): Promise
<void> => {
32 }, this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600 * 1000);
34 for (const connector
in this.chargingStation
.connectors
) {
35 if (Utils
.convertToInt(connector
) > 0) {
36 await this.startConnector(Utils
.convertToInt(connector
));
39 logger
.info(this.logPrefix() + ' ATG started and will stop in ' + Utils
.secondsToHHMMSS(this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.stopAfterHours
* 3600));
42 public async stop(reason
: StopTransactionReason
= StopTransactionReason
.NONE
): Promise
<void> {
43 logger
.info(this.logPrefix() + ' ATG OVER => STOPPING ALL TRANSACTIONS');
44 for (const connector
in this.chargingStation
.connectors
) {
45 const transactionId
= this.chargingStation
.getConnector(Utils
.convertToInt(connector
)).transactionId
;
46 if (this.chargingStation
.getConnector(Utils
.convertToInt(connector
)).transactionStarted
) {
47 logger
.info(this.logPrefix(Utils
.convertToInt(connector
)) + ' ATG OVER. Stop transaction ' + transactionId
.toString());
48 await this.chargingStation
.ocppRequestService
.sendStopTransaction(transactionId
, this.chargingStation
.getTransactionMeterStop(transactionId
), this.chargingStation
.getTransactionIdTag(transactionId
), reason
);
51 this.timeToStop
= true;
54 public async startConnector(connectorId
: number): Promise
<void> {
56 if (this.timeToStop
) {
57 logger
.error(this.logPrefix(connectorId
) + ' Entered in transaction loop while a request to stop it was made');
60 if (!this.chargingStation
.isRegistered()) {
61 logger
.error(this.logPrefix(connectorId
) + ' Entered in transaction loop while the charging station is not registered');
64 if (!this.chargingStation
.isChargingStationAvailable()) {
65 logger
.info(this.logPrefix(connectorId
) + ' Entered in transaction loop while the charging station is unavailable');
69 if (!this.chargingStation
.isConnectorAvailable(connectorId
)) {
70 logger
.info(`${this.logPrefix(connectorId)} Entered in transaction loop while the connector ${connectorId} is unavailable, stop it`);
73 if (!this.chargingStation
?.ocppRequestService
) {
74 logger
.info(`${this.logPrefix(connectorId)} Transaction loop waiting for charging station service to be initialized`);
76 await Utils
.sleep(500);
77 } while (!this.chargingStation
?.ocppRequestService
);
79 const wait
= Utils
.getRandomInt(this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.maxDelayBetweenTwoTransactions
,
80 this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.minDelayBetweenTwoTransactions
) * 1000;
81 logger
.info(this.logPrefix(connectorId
) + ' wait for ' + Utils
.milliSecondsToHHMMSS(wait
));
82 await Utils
.sleep(wait
);
83 const start
= Math.random();
85 if (start
< this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.probabilityOfStart
) {
88 let startResponse
: StartTransactionResponse
| AuthorizeResponse
;
89 if (this.chargingStation
.getEnableStatistics()) {
90 const startTransaction
= performance
.timerify(this.startTransaction
);
91 this.performanceObserver
.observe({ entryTypes
: ['function'] });
92 startResponse
= await startTransaction(connectorId
, this);
94 startResponse
= await this.startTransaction(connectorId
, this);
96 if (startResponse
?.idTagInfo
?.status !== AuthorizationStatus
.ACCEPTED
) {
97 logger
.info(this.logPrefix(connectorId
) + ' transaction rejected');
98 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_WAIT_TIME
);
100 // Wait until end of transaction
101 const waitTrxEnd
= Utils
.getRandomInt(this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.maxDuration
,
102 this.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.minDuration
) * 1000;
103 logger
.info(this.logPrefix(connectorId
) + ' transaction ' + this.chargingStation
.getConnector(connectorId
).transactionId
.toString() + ' will stop in ' + Utils
.milliSecondsToHHMMSS(waitTrxEnd
));
104 await Utils
.sleep(waitTrxEnd
);
106 if (this.chargingStation
.getConnector(connectorId
)?.transactionStarted
) {
107 logger
.info(this.logPrefix(connectorId
) + ' stop transaction ' + this.chargingStation
.getConnector(connectorId
).transactionId
.toString());
108 if (this.chargingStation
.getEnableStatistics()) {
109 const stopTransaction
= performance
.timerify(this.stopTransaction
);
110 this.performanceObserver
.observe({ entryTypes
: ['function'] });
111 await stopTransaction(connectorId
, this);
113 await this.stopTransaction(connectorId
, this);
119 logger
.info(this.logPrefix(connectorId
) + ' transaction skipped ' + skip
.toString());
121 } while (!this.timeToStop
);
122 logger
.info(this.logPrefix(connectorId
) + ' ATG STOPPED on the connector');
125 // eslint-disable-next-line consistent-this
126 private async startTransaction(connectorId
: number, self: AutomaticTransactionGenerator
): Promise
<StartTransactionResponse
| AuthorizeResponse
> {
127 if (self.chargingStation
.hasAuthorizedTags()) {
128 const tagId
= self.chargingStation
.getRandomTagId();
129 if (self.chargingStation
.stationInfo
.AutomaticTransactionGenerator
.requireAuthorize
) {
131 const authorizeResponse
= await self.chargingStation
.ocppRequestService
.sendAuthorize(tagId
);
132 if (authorizeResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
133 logger
.info(self.logPrefix(connectorId
) + ' start transaction for tagID ' + tagId
);
135 return await self.chargingStation
.ocppRequestService
.sendStartTransaction(connectorId
, tagId
);
137 return authorizeResponse
;
139 logger
.info(self.logPrefix(connectorId
) + ' start transaction for tagID ' + tagId
);
141 return await self.chargingStation
.ocppRequestService
.sendStartTransaction(connectorId
, tagId
);
143 logger
.info(self.logPrefix(connectorId
) + ' start transaction without a tagID');
144 return await self.chargingStation
.ocppRequestService
.sendStartTransaction(connectorId
);
147 // eslint-disable-next-line consistent-this
148 private async stopTransaction(connectorId
: number, self: AutomaticTransactionGenerator
): Promise
<StopTransactionResponse
> {
149 const transactionId
= self.chargingStation
.getConnector(connectorId
).transactionId
;
150 return await self.chargingStation
.ocppRequestService
.sendStopTransaction(transactionId
, self.chargingStation
.getTransactionMeterStop(transactionId
), self.chargingStation
.getTransactionIdTag(transactionId
));
153 private logPrefix(connectorId
: number = null): string {
155 return Utils
.logPrefix(' ' + this.chargingStation
.stationInfo
.chargingStationId
+ ' ATG on connector #' + connectorId
.toString() + ':');
157 return Utils
.logPrefix(' ' + this.chargingStation
.stationInfo
.chargingStationId
+ ' ATG:');