1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { AsyncResource
} from
'node:async_hooks';
5 import { type ChargingStation
, ChargingStationUtils
} from
'./internal';
6 import { BaseError
} from
'../exception';
7 // import { PerformanceStatistics } from '../performance';
8 import { PerformanceStatistics
} from
'../performance/PerformanceStatistics';
11 type AuthorizeRequest
,
12 type AuthorizeResponse
,
13 type AutomaticTransactionGeneratorConfiguration
,
16 type StartTransactionRequest
,
17 type StartTransactionResponse
,
19 StopTransactionReason
,
20 type StopTransactionResponse
,
22 import { Constants
, Utils
, logger
} from
'../utils';
24 const moduleName
= 'AutomaticTransactionGenerator';
26 export class AutomaticTransactionGenerator
extends AsyncResource
{
27 private static readonly instances
: Map
<string, AutomaticTransactionGenerator
> = new Map
<
29 AutomaticTransactionGenerator
32 public readonly connectorsStatus
: Map
<number, Status
>;
33 public readonly configuration
: AutomaticTransactionGeneratorConfiguration
;
34 public started
: boolean;
35 private readonly chargingStation
: ChargingStation
;
36 private idTagIndex
: number;
39 automaticTransactionGeneratorConfiguration
: AutomaticTransactionGeneratorConfiguration
,
40 chargingStation
: ChargingStation
44 this.configuration
= automaticTransactionGeneratorConfiguration
;
45 this.chargingStation
= chargingStation
;
47 this.connectorsStatus
= new Map
<number, Status
>();
48 this.initializeConnectorsStatus();
51 public static getInstance(
52 automaticTransactionGeneratorConfiguration
: AutomaticTransactionGeneratorConfiguration
,
53 chargingStation
: ChargingStation
54 ): AutomaticTransactionGenerator
| undefined {
55 if (AutomaticTransactionGenerator
.instances
.has(chargingStation
.stationInfo
.hashId
) === false) {
56 AutomaticTransactionGenerator
.instances
.set(
57 chargingStation
.stationInfo
.hashId
,
58 new AutomaticTransactionGenerator(
59 automaticTransactionGeneratorConfiguration
,
64 return AutomaticTransactionGenerator
.instances
.get(chargingStation
.stationInfo
.hashId
);
67 public start(): void {
68 if (this.checkChargingStation() === false) {
71 if (this.started
=== true) {
72 logger
.warn(`${this.logPrefix()} is already started`);
75 this.startConnectors();
80 if (this.started
=== false) {
81 logger
.warn(`${this.logPrefix()} is already stopped`);
84 this.stopConnectors();
88 public startConnector(connectorId
: number): void {
89 if (this.checkChargingStation(connectorId
) === false) {
92 if (this.connectorsStatus
.has(connectorId
) === false) {
93 logger
.error(`${this.logPrefix(connectorId)} starting on non existing connector`);
94 throw new BaseError(`Connector ${connectorId} does not exist`);
96 if (this.connectorsStatus
.get(connectorId
)?.start
=== false) {
98 this.internalStartConnector
.bind(this) as (
99 this: AutomaticTransactionGenerator
,
104 ).catch(Constants
.EMPTY_FUNCTION
);
105 } else if (this.connectorsStatus
.get(connectorId
)?.start
=== true) {
106 logger
.warn(`${this.logPrefix(connectorId)} is already started on connector`);
110 public stopConnector(connectorId
: number): void {
111 if (this.connectorsStatus
.has(connectorId
) === false) {
112 logger
.error(`${this.logPrefix(connectorId)} stopping on non existing connector`);
113 throw new BaseError(`Connector ${connectorId} does not exist`);
115 if (this.connectorsStatus
.get(connectorId
)?.start
=== true) {
116 this.connectorsStatus
.get(connectorId
).start
= false;
117 } else if (this.connectorsStatus
.get(connectorId
)?.start
=== false) {
118 logger
.warn(`${this.logPrefix(connectorId)} is already stopped on connector`);
122 private startConnectors(): void {
124 this.connectorsStatus
?.size
> 0 &&
125 this.connectorsStatus
.size
!== this.chargingStation
.getNumberOfConnectors()
127 this.connectorsStatus
.clear();
128 this.initializeConnectorsStatus();
130 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
131 if (connectorId
> 0) {
132 this.startConnector(connectorId
);
137 private stopConnectors(): void {
138 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
139 if (connectorId
> 0) {
140 this.stopConnector(connectorId
);
145 private async internalStartConnector(connectorId
: number): Promise
<void> {
146 this.setStartConnectorStatus(connectorId
);
150 )} started on connector and will run for ${Utils.formatDurationMilliSeconds(
151 this.connectorsStatus.get(connectorId).stopDate.getTime() -
152 this.connectorsStatus.get(connectorId).startDate.getTime()
155 while (this.connectorsStatus
.get(connectorId
)?.start
=== true) {
156 if (new Date() > this.connectorsStatus
.get(connectorId
).stopDate
) {
157 this.stopConnector(connectorId
);
160 if (this.chargingStation
.isInAcceptedState() === false) {
164 )} entered in transaction loop while the charging station is not in accepted state`
166 this.stopConnector(connectorId
);
169 if (this.chargingStation
.isChargingStationAvailable() === false) {
173 )} entered in transaction loop while the charging station is unavailable`
175 this.stopConnector(connectorId
);
178 if (this.chargingStation
.isConnectorAvailable(connectorId
) === false) {
182 )} entered in transaction loop while the connector ${connectorId} is unavailable`
184 this.stopConnector(connectorId
);
187 if (!this.chargingStation
?.ocppRequestService
) {
191 )} transaction loop waiting for charging station service to be initialized`
194 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_INITIALIZATION_TIME
);
195 } while (!this.chargingStation
?.ocppRequestService
);
198 Utils
.getRandomInteger(
199 this.configuration
.maxDelayBetweenTwoTransactions
,
200 this.configuration
.minDelayBetweenTwoTransactions
203 `${this.logPrefix(connectorId)} waiting for ${Utils.formatDurationMilliSeconds(wait)}`
205 await Utils
.sleep(wait
);
206 const start
= Utils
.secureRandom();
207 if (start
< this.configuration
.probabilityOfStart
) {
208 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
= 0;
210 const startResponse
= await this.startTransaction(connectorId
);
211 if (startResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
212 // Wait until end of transaction
214 Utils
.getRandomInteger(this.configuration
.maxDuration
, this.configuration
.minDuration
) *
217 `${this.logPrefix(connectorId)} transaction ${this.chargingStation
218 .getConnectorStatus(connectorId)
219 ?.transactionId?.toString()} started and will stop in ${Utils.formatDurationMilliSeconds(
223 await Utils
.sleep(waitTrxEnd
);
226 `${this.logPrefix(connectorId)} stop transaction ${this.chargingStation
227 .getConnectorStatus(connectorId)
228 ?.transactionId?.toString()}`
230 await this.stopTransaction(connectorId
);
233 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
++;
234 this.connectorsStatus
.get(connectorId
).skippedTransactions
++;
236 `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus
238 ?.skippedConsecutiveTransactions?.toString()}/${this.connectorsStatus
240 ?.skippedTransactions?.toString()} transaction(s)`
243 this.connectorsStatus
.get(connectorId
).lastRunDate
= new Date();
245 this.connectorsStatus
.get(connectorId
).stoppedDate
= new Date();
249 )} stopped on connector and lasted for ${Utils.formatDurationMilliSeconds(
250 this.connectorsStatus.get(connectorId).stoppedDate.getTime() -
251 this.connectorsStatus.get(connectorId).startDate.getTime()
255 `${this.logPrefix(connectorId)} connector status: %j`,
256 this.connectorsStatus
.get(connectorId
)
260 private setStartConnectorStatus(connectorId
: number): void {
261 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
= 0;
262 const previousRunDuration
=
263 this.connectorsStatus
.get(connectorId
)?.startDate
&&
264 this.connectorsStatus
.get(connectorId
)?.lastRunDate
265 ? this.connectorsStatus
.get(connectorId
).lastRunDate
.getTime() -
266 this.connectorsStatus
.get(connectorId
).startDate
.getTime()
268 this.connectorsStatus
.get(connectorId
).startDate
= new Date();
269 this.connectorsStatus
.get(connectorId
).stopDate
= new Date(
270 this.connectorsStatus
.get(connectorId
).startDate
.getTime() +
271 (this.configuration
.stopAfterHours
??
272 Constants
.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS
) *
277 this.connectorsStatus
.get(connectorId
).start
= true;
280 private initializeConnectorsStatus(): void {
281 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
282 if (connectorId
> 0) {
283 this.connectorsStatus
.set(connectorId
, {
285 authorizeRequests
: 0,
286 acceptedAuthorizeRequests
: 0,
287 rejectedAuthorizeRequests
: 0,
288 startTransactionRequests
: 0,
289 acceptedStartTransactionRequests
: 0,
290 rejectedStartTransactionRequests
: 0,
291 stopTransactionRequests
: 0,
292 acceptedStopTransactionRequests
: 0,
293 rejectedStopTransactionRequests
: 0,
294 skippedConsecutiveTransactions
: 0,
295 skippedTransactions
: 0,
301 private async startTransaction(
303 ): Promise
<StartTransactionResponse
| undefined> {
304 const measureId
= 'StartTransaction with ATG';
305 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
306 let startResponse
: StartTransactionResponse
;
307 if (this.chargingStation
.hasAuthorizedTags()) {
308 const idTag
= this.getIdTag(connectorId
);
309 const startTransactionLogMsg
= `${this.logPrefix(
311 )} start transaction with an idTag '${idTag}'`;
312 if (this.getRequireAuthorize()) {
313 this.chargingStation
.getConnectorStatus(connectorId
).authorizeIdTag
= idTag
;
315 const authorizeResponse
: AuthorizeResponse
=
316 await this.chargingStation
.ocppRequestService
.requestHandler
<
319 >(this.chargingStation
, RequestCommand
.AUTHORIZE
, {
322 this.connectorsStatus
.get(connectorId
).authorizeRequests
++;
323 if (authorizeResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
324 this.connectorsStatus
.get(connectorId
).acceptedAuthorizeRequests
++;
325 logger
.info(startTransactionLogMsg
);
327 startResponse
= await this.chargingStation
.ocppRequestService
.requestHandler
<
328 StartTransactionRequest
,
329 StartTransactionResponse
330 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
334 this.handleStartTransactionResponse(connectorId
, startResponse
);
335 PerformanceStatistics
.endMeasure(measureId
, beginId
);
336 return startResponse
;
338 this.connectorsStatus
.get(connectorId
).rejectedAuthorizeRequests
++;
339 PerformanceStatistics
.endMeasure(measureId
, beginId
);
340 return startResponse
;
342 logger
.info(startTransactionLogMsg
);
344 startResponse
= await this.chargingStation
.ocppRequestService
.requestHandler
<
345 StartTransactionRequest
,
346 StartTransactionResponse
347 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
351 this.handleStartTransactionResponse(connectorId
, startResponse
);
352 PerformanceStatistics
.endMeasure(measureId
, beginId
);
353 return startResponse
;
355 logger
.info(`${this.logPrefix(connectorId)} start transaction without an idTag`);
356 startResponse
= await this.chargingStation
.ocppRequestService
.requestHandler
<
357 StartTransactionRequest
,
358 StartTransactionResponse
359 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, { connectorId
});
360 this.handleStartTransactionResponse(connectorId
, startResponse
);
361 PerformanceStatistics
.endMeasure(measureId
, beginId
);
362 return startResponse
;
365 private async stopTransaction(
367 reason
: StopTransactionReason
= StopTransactionReason
.LOCAL
368 ): Promise
<StopTransactionResponse
> {
369 const measureId
= 'StopTransaction with ATG';
370 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
371 let stopResponse
: StopTransactionResponse
;
372 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
373 stopResponse
= await this.chargingStation
.stopTransactionOnConnector(connectorId
, reason
);
374 this.connectorsStatus
.get(connectorId
).stopTransactionRequests
++;
375 if (stopResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
376 this.connectorsStatus
.get(connectorId
).acceptedStopTransactionRequests
++;
378 this.connectorsStatus
.get(connectorId
).rejectedStopTransactionRequests
++;
381 const transactionId
= this.chargingStation
.getConnectorStatus(connectorId
)?.transactionId
;
383 `${this.logPrefix(connectorId)} stopping a not started transaction${
384 !Utils.isNullOrUndefined(transactionId) ? ` ${transactionId?.toString()}
` : ''
388 PerformanceStatistics
.endMeasure(measureId
, beginId
);
392 private getRequireAuthorize(): boolean {
393 return this.configuration
?.requireAuthorize
?? true;
396 private getRandomIdTag(authorizationFile
: string): string {
397 const tags
= this.chargingStation
.authorizedTagsCache
.getAuthorizedTags(authorizationFile
);
398 this.idTagIndex
= Math.floor(Utils
.secureRandom() * tags
.length
);
399 return tags
[this.idTagIndex
];
402 private getRoundRobinIdTag(authorizationFile
: string): string {
403 const tags
= this.chargingStation
.authorizedTagsCache
.getAuthorizedTags(authorizationFile
);
404 const idTag
= tags
[this.idTagIndex
];
405 this.idTagIndex
= this.idTagIndex
=== tags
.length
- 1 ? 0 : this.idTagIndex
+ 1;
409 private getConnectorAffinityIdTag(authorizationFile
: string, connectorId
: number): string {
410 const tags
= this.chargingStation
.authorizedTagsCache
.getAuthorizedTags(authorizationFile
);
411 this.idTagIndex
= (this.chargingStation
.index
- 1 + (connectorId
- 1)) % tags
.length
;
412 return tags
[this.idTagIndex
];
415 private getIdTag(connectorId
: number): string {
416 const authorizationFile
= ChargingStationUtils
.getAuthorizationFile(
417 this.chargingStation
.stationInfo
419 switch (this.configuration
?.idTagDistribution
) {
420 case IdTagDistribution
.RANDOM
:
421 return this.getRandomIdTag(authorizationFile
);
422 case IdTagDistribution
.ROUND_ROBIN
:
423 return this.getRoundRobinIdTag(authorizationFile
);
424 case IdTagDistribution
.CONNECTOR_AFFINITY
:
425 return this.getConnectorAffinityIdTag(authorizationFile
, connectorId
);
427 return this.getRoundRobinIdTag(authorizationFile
);
431 private logPrefix
= (connectorId
?: number): string => {
432 return Utils
.logPrefix(
433 ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
434 connectorId !== undefined ? ` on connector #${connectorId.toString()}
` : ''
439 private handleStartTransactionResponse(
441 startResponse
: StartTransactionResponse
443 this.connectorsStatus
.get(connectorId
).startTransactionRequests
++;
444 if (startResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
445 this.connectorsStatus
.get(connectorId
).acceptedStartTransactionRequests
++;
447 logger
.warn(`${this.logPrefix(connectorId)} start transaction rejected`);
448 this.connectorsStatus
.get(connectorId
).rejectedStartTransactionRequests
++;
452 private checkChargingStation(connectorId
?: number): boolean {
453 if (this.chargingStation
.started
=== false && this.chargingStation
.starting
=== false) {
454 logger
.warn(`${this.logPrefix(connectorId)} charging station is stopped, cannot proceed`);