1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import PerformanceStatistics from
'../performance/PerformanceStatistics';
5 AutomaticTransactionGeneratorConfiguration
,
7 } from
'../types/AutomaticTransactionGenerator';
8 import { RequestCommand
} from
'../types/ocpp/Requests';
11 type AuthorizeRequest
,
12 type AuthorizeResponse
,
13 type StartTransactionRequest
,
14 type StartTransactionResponse
,
15 StopTransactionReason
,
16 type StopTransactionResponse
,
17 } from
'../types/ocpp/Transaction';
18 import Constants from
'../utils/Constants';
19 import logger from
'../utils/Logger';
20 import Utils from
'../utils/Utils';
21 import type ChargingStation from
'./ChargingStation';
23 export default class AutomaticTransactionGenerator
{
24 private static readonly instances
: Map
<string, AutomaticTransactionGenerator
> = new Map
<
26 AutomaticTransactionGenerator
29 public readonly connectorsStatus
: Map
<number, Status
>;
30 public readonly configuration
: AutomaticTransactionGeneratorConfiguration
;
31 public started
: boolean;
32 private readonly chargingStation
: ChargingStation
;
35 automaticTransactionGeneratorConfiguration
: AutomaticTransactionGeneratorConfiguration
,
36 chargingStation
: ChargingStation
39 this.configuration
= automaticTransactionGeneratorConfiguration
;
40 this.chargingStation
= chargingStation
;
41 this.connectorsStatus
= new Map
<number, Status
>();
42 this.stopConnectors();
45 public static getInstance(
46 automaticTransactionGeneratorConfiguration
: AutomaticTransactionGeneratorConfiguration
,
47 chargingStation
: ChargingStation
48 ): AutomaticTransactionGenerator
{
49 if (!AutomaticTransactionGenerator
.instances
.has(chargingStation
.stationInfo
.hashId
)) {
50 AutomaticTransactionGenerator
.instances
.set(
51 chargingStation
.stationInfo
.hashId
,
52 new AutomaticTransactionGenerator(
53 automaticTransactionGeneratorConfiguration
,
58 return AutomaticTransactionGenerator
.instances
.get(chargingStation
.stationInfo
.hashId
);
61 public start(): void {
62 if (this.started
=== true) {
63 logger
.warn(`${this.logPrefix()} is already started`);
66 this.startConnectors();
71 if (this.started
=== false) {
72 logger
.warn(`${this.logPrefix()} is already stopped`);
75 this.stopConnectors();
79 public startConnector(connectorId
: number): void {
80 if (this.chargingStation
.connectors
.has(connectorId
) === false) {
81 logger
.warn(`${this.logPrefix(connectorId)} starting on non existing connector`);
84 if (this.connectorsStatus
.get(connectorId
)?.start
=== false) {
85 // Avoid hogging the event loop with a busy loop
87 this.internalStartConnector(connectorId
).catch(() => {
88 /* This is intentional */
91 } else if (this.connectorsStatus
.get(connectorId
)?.start
=== true) {
92 logger
.warn(`${this.logPrefix(connectorId)} is already started on connector`);
96 public stopConnector(connectorId
: number): void {
97 if (this.chargingStation
.connectors
.has(connectorId
) === false) {
98 logger
.warn(`${this.logPrefix(connectorId)} stopping on non existing connector`);
101 if (this.connectorsStatus
.get(connectorId
)?.start
=== true) {
102 this.connectorsStatus
.set(connectorId
, {
103 ...this.connectorsStatus
.get(connectorId
),
106 } else if (this.connectorsStatus
.get(connectorId
)?.start
=== false) {
107 logger
.warn(`${this.logPrefix(connectorId)} is already stopped on connector`);
111 private startConnectors(): void {
113 this.connectorsStatus
?.size
> 0 &&
114 this.connectorsStatus
.size
!== this.chargingStation
.getNumberOfConnectors()
116 this.connectorsStatus
.clear();
118 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
119 if (connectorId
> 0) {
120 this.startConnector(connectorId
);
125 private stopConnectors(): void {
126 for (const connectorId
of this.chargingStation
.connectors
.keys()) {
127 if (connectorId
> 0) {
128 this.stopConnector(connectorId
);
133 private async internalStartConnector(connectorId
: number): Promise
<void> {
134 this.initializeConnectorStatus(connectorId
);
136 this.logPrefix(connectorId
) +
137 ' started on connector and will run for ' +
138 Utils
.formatDurationMilliSeconds(
139 this.connectorsStatus
.get(connectorId
).stopDate
.getTime() -
140 this.connectorsStatus
.get(connectorId
).startDate
.getTime()
143 while (this.connectorsStatus
.get(connectorId
).start
=== true) {
144 if (new Date() > this.connectorsStatus
.get(connectorId
).stopDate
) {
145 this.stopConnector(connectorId
);
148 if (!this.chargingStation
.isInAcceptedState()) {
150 this.logPrefix(connectorId
) +
151 ' entered in transaction loop while the charging station is not in accepted state'
153 this.stopConnector(connectorId
);
156 if (!this.chargingStation
.isChargingStationAvailable()) {
158 this.logPrefix(connectorId
) +
159 ' entered in transaction loop while the charging station is unavailable'
161 this.stopConnector(connectorId
);
164 if (!this.chargingStation
.isConnectorAvailable(connectorId
)) {
168 )} entered in transaction loop while the connector ${connectorId} is unavailable`
170 this.stopConnector(connectorId
);
173 if (!this.chargingStation
?.ocppRequestService
) {
177 )} transaction loop waiting for charging station service to be initialized`
180 await Utils
.sleep(Constants
.CHARGING_STATION_ATG_INITIALIZATION_TIME
);
181 } while (!this.chargingStation
?.ocppRequestService
);
184 Utils
.getRandomInteger(
185 this.configuration
.maxDelayBetweenTwoTransactions
,
186 this.configuration
.minDelayBetweenTwoTransactions
189 this.logPrefix(connectorId
) + ' waiting for ' + Utils
.formatDurationMilliSeconds(wait
)
191 await Utils
.sleep(wait
);
192 const start
= Utils
.secureRandom();
193 if (start
< this.configuration
.probabilityOfStart
) {
194 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
= 0;
196 const startResponse
= await this.startTransaction(connectorId
);
197 if (startResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
198 // Wait until end of transaction
200 Utils
.getRandomInteger(this.configuration
.maxDuration
, this.configuration
.minDuration
) *
203 this.logPrefix(connectorId
) +
205 this.chargingStation
.getConnectorStatus(connectorId
).transactionId
.toString() +
206 ' started and will stop in ' +
207 Utils
.formatDurationMilliSeconds(waitTrxEnd
)
209 await Utils
.sleep(waitTrxEnd
);
212 this.logPrefix(connectorId
) +
213 ' stop transaction ' +
214 this.chargingStation
.getConnectorStatus(connectorId
).transactionId
.toString()
216 await this.stopTransaction(connectorId
);
219 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
++;
220 this.connectorsStatus
.get(connectorId
).skippedTransactions
++;
222 this.logPrefix(connectorId
) +
223 ' skipped consecutively ' +
224 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
.toString() +
226 this.connectorsStatus
.get(connectorId
).skippedTransactions
.toString() +
230 this.connectorsStatus
.get(connectorId
).lastRunDate
= new Date();
232 this.connectorsStatus
.get(connectorId
).stoppedDate
= new Date();
234 this.logPrefix(connectorId
) +
235 ' stopped on connector and lasted for ' +
236 Utils
.formatDurationMilliSeconds(
237 this.connectorsStatus
.get(connectorId
).stoppedDate
.getTime() -
238 this.connectorsStatus
.get(connectorId
).startDate
.getTime()
242 `${this.logPrefix(connectorId)} connector status: %j`,
243 this.connectorsStatus
.get(connectorId
)
247 private initializeConnectorStatus(connectorId
: number): void {
248 this.connectorsStatus
.get(connectorId
).authorizeRequests
=
249 this?.connectorsStatus
.get(connectorId
)?.authorizeRequests
?? 0;
250 this.connectorsStatus
.get(connectorId
).acceptedAuthorizeRequests
=
251 this?.connectorsStatus
.get(connectorId
)?.acceptedAuthorizeRequests
?? 0;
252 this.connectorsStatus
.get(connectorId
).rejectedAuthorizeRequests
=
253 this?.connectorsStatus
.get(connectorId
)?.rejectedAuthorizeRequests
?? 0;
254 this.connectorsStatus
.get(connectorId
).startTransactionRequests
=
255 this?.connectorsStatus
.get(connectorId
)?.startTransactionRequests
?? 0;
256 this.connectorsStatus
.get(connectorId
).acceptedStartTransactionRequests
=
257 this?.connectorsStatus
.get(connectorId
)?.acceptedStartTransactionRequests
?? 0;
258 this.connectorsStatus
.get(connectorId
).rejectedStartTransactionRequests
=
259 this?.connectorsStatus
.get(connectorId
)?.rejectedStartTransactionRequests
?? 0;
260 this.connectorsStatus
.get(connectorId
).stopTransactionRequests
=
261 this?.connectorsStatus
.get(connectorId
)?.stopTransactionRequests
?? 0;
262 this.connectorsStatus
.get(connectorId
).acceptedStopTransactionRequests
=
263 this?.connectorsStatus
.get(connectorId
)?.acceptedStopTransactionRequests
?? 0;
264 this.connectorsStatus
.get(connectorId
).rejectedStopTransactionRequests
=
265 this?.connectorsStatus
.get(connectorId
)?.rejectedStopTransactionRequests
?? 0;
266 this.connectorsStatus
.get(connectorId
).skippedConsecutiveTransactions
= 0;
267 this.connectorsStatus
.get(connectorId
).skippedTransactions
=
268 this?.connectorsStatus
.get(connectorId
)?.skippedTransactions
?? 0;
269 const previousRunDuration
=
270 this?.connectorsStatus
.get(connectorId
)?.startDate
&&
271 this?.connectorsStatus
.get(connectorId
)?.lastRunDate
272 ? this.connectorsStatus
.get(connectorId
).lastRunDate
.getTime() -
273 this.connectorsStatus
.get(connectorId
).startDate
.getTime()
275 this.connectorsStatus
.get(connectorId
).startDate
= new Date();
276 this.connectorsStatus
.get(connectorId
).stopDate
= new Date(
277 this.connectorsStatus
.get(connectorId
).startDate
.getTime() +
278 (this.configuration
.stopAfterHours
??
279 Constants
.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS
) *
284 this.connectorsStatus
.get(connectorId
).start
= true;
287 private async startTransaction(
289 ): Promise
<StartTransactionResponse
| undefined> {
290 const measureId
= 'StartTransaction with ATG';
291 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
292 let startResponse
: StartTransactionResponse
;
293 if (this.chargingStation
.hasAuthorizedTags()) {
294 const idTag
= this.chargingStation
.getRandomIdTag();
295 const startTransactionLogMsg
= `${this.logPrefix(
297 )} start transaction with an idTag '${idTag}'`;
298 if (this.getRequireAuthorize()) {
299 this.chargingStation
.getConnectorStatus(connectorId
).authorizeIdTag
= idTag
;
301 const authorizeResponse
: AuthorizeResponse
=
302 await this.chargingStation
.ocppRequestService
.requestHandler
<
305 >(this.chargingStation
, RequestCommand
.AUTHORIZE
, {
308 this.connectorsStatus
.get(connectorId
).authorizeRequests
++;
309 if (authorizeResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
310 this.connectorsStatus
.get(connectorId
).acceptedAuthorizeRequests
++;
311 logger
.info(startTransactionLogMsg
);
313 startResponse
= await this.chargingStation
.ocppRequestService
.requestHandler
<
314 StartTransactionRequest
,
315 StartTransactionResponse
316 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
320 this.handleStartTransactionResponse(connectorId
, startResponse
);
321 PerformanceStatistics
.endMeasure(measureId
, beginId
);
322 return startResponse
;
324 this.connectorsStatus
.get(connectorId
).rejectedAuthorizeRequests
++;
325 PerformanceStatistics
.endMeasure(measureId
, beginId
);
326 return startResponse
;
328 logger
.info(startTransactionLogMsg
);
330 startResponse
= await this.chargingStation
.ocppRequestService
.requestHandler
<
331 StartTransactionRequest
,
332 StartTransactionResponse
333 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, {
337 this.handleStartTransactionResponse(connectorId
, startResponse
);
338 PerformanceStatistics
.endMeasure(measureId
, beginId
);
339 return startResponse
;
341 logger
.info(`${this.logPrefix(connectorId)} start transaction without an idTag`);
342 startResponse
= await this.chargingStation
.ocppRequestService
.requestHandler
<
343 StartTransactionRequest
,
344 StartTransactionResponse
345 >(this.chargingStation
, RequestCommand
.START_TRANSACTION
, { connectorId
});
346 this.handleStartTransactionResponse(connectorId
, startResponse
);
347 PerformanceStatistics
.endMeasure(measureId
, beginId
);
348 return startResponse
;
351 private async stopTransaction(
353 reason
: StopTransactionReason
= StopTransactionReason
.LOCAL
354 ): Promise
<StopTransactionResponse
> {
355 const measureId
= 'StopTransaction with ATG';
356 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
357 let stopResponse
: StopTransactionResponse
;
358 if (this.chargingStation
.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
359 stopResponse
= await this.chargingStation
.stopTransactionOnConnector(connectorId
, reason
);
360 this.connectorsStatus
.get(connectorId
).stopTransactionRequests
++;
361 if (stopResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
362 this.connectorsStatus
.get(connectorId
).acceptedStopTransactionRequests
++;
364 this.connectorsStatus
.get(connectorId
).rejectedStopTransactionRequests
++;
367 const transactionId
= this.chargingStation
.getConnectorStatus(connectorId
).transactionId
;
369 `${this.logPrefix(connectorId)} stopping a not started transaction${
370 transactionId ? ' ' + transactionId.toString() : ''
374 PerformanceStatistics
.endMeasure(measureId
, beginId
);
378 private getRequireAuthorize(): boolean {
379 return this.configuration
?.requireAuthorize
?? true;
382 private logPrefix(connectorId
?: number): string {
383 return Utils
.logPrefix(
384 ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
385 connectorId !== undefined ? ` on connector #${connectorId.toString()}
` : ''
390 private handleStartTransactionResponse(
392 startResponse
: StartTransactionResponse
394 this.connectorsStatus
.get(connectorId
).startTransactionRequests
++;
395 if (startResponse
?.idTagInfo
?.status === AuthorizationStatus
.ACCEPTED
) {
396 this.connectorsStatus
.get(connectorId
).acceptedStartTransactionRequests
++;
398 logger
.warn(this.logPrefix(connectorId
) + ' start transaction rejected');
399 this.connectorsStatus
.get(connectorId
).rejectedStartTransactionRequests
++;