fix: compute the ATG stop date only once
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
CommitLineData
edd13439 1// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
c8eeb62b 2
66a7748d 3import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns'
be4c6702 4
66a7748d
JB
5import type { ChargingStation } from './ChargingStation.js'
6import { checkChargingStation } from './Helpers.js'
7import { IdTagsCache } from './IdTagsCache.js'
8import { isIdTagAuthorized } from './ocpp/index.js'
9import { BaseError } from '../exception/index.js'
10import { PerformanceStatistics } from '../performance/index.js'
e7aeea18
JB
11import {
12 AuthorizationStatus,
268a74bb 13 RequestCommand,
976d11ec
JB
14 type StartTransactionRequest,
15 type StartTransactionResponse,
268a74bb 16 type Status,
e7aeea18 17 StopTransactionReason,
66a7748d
JB
18 type StopTransactionResponse
19} from '../types/index.js'
9bf0ef23
JB
20import {
21 Constants,
22 cloneObject,
6dde6c5f 23 convertToDate,
9bf0ef23
JB
24 formatDurationMilliSeconds,
25 getRandomInteger,
0a1dd746 26 isValidTime,
9bf0ef23
JB
27 logPrefix,
28 logger,
29 secureRandom,
66a7748d
JB
30 sleep
31} from '../utils/index.js'
6af9012e 32
d1ff8599 33export class AutomaticTransactionGenerator {
e7aeea18 34 private static readonly instances: Map<string, AutomaticTransactionGenerator> = new Map<
66a7748d
JB
35 string,
36 AutomaticTransactionGenerator
37 >()
10068088 38
66a7748d
JB
39 public readonly connectorsStatus: Map<number, Status>
40 public started: boolean
41 private starting: boolean
42 private stopping: boolean
43 private readonly chargingStation: ChargingStation
6af9012e 44
66a7748d
JB
45 private constructor (chargingStation: ChargingStation) {
46 this.started = false
47 this.starting = false
48 this.stopping = false
49 this.chargingStation = chargingStation
50 this.connectorsStatus = new Map<number, Status>()
51 this.initializeConnectorsStatus()
6af9012e
JB
52 }
53
66a7748d
JB
54 public static getInstance (
55 chargingStation: ChargingStation
1895299d 56 ): AutomaticTransactionGenerator | undefined {
5199f9fd
JB
57 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
58 if (!AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo!.hashId)) {
e7aeea18 59 AutomaticTransactionGenerator.instances.set(
5199f9fd
JB
60 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61 chargingStation.stationInfo!.hashId,
66a7748d
JB
62 new AutomaticTransactionGenerator(chargingStation)
63 )
73b9adec 64 }
5199f9fd
JB
65 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66 return AutomaticTransactionGenerator.instances.get(chargingStation.stationInfo!.hashId)
73b9adec
JB
67 }
68
66a7748d
JB
69 public start (): void {
70 if (!checkChargingStation(this.chargingStation, this.logPrefix())) {
71 return
d1c6c833 72 }
66a7748d
JB
73 if (this.started) {
74 logger.warn(`${this.logPrefix()} is already started`)
75 return
b809adf1 76 }
66a7748d
JB
77 if (this.starting) {
78 logger.warn(`${this.logPrefix()} is already starting`)
79 return
11353865 80 }
66a7748d
JB
81 this.starting = true
82 this.startConnectors()
83 this.started = true
84 this.starting = false
6af9012e
JB
85 }
86
66a7748d
JB
87 public stop (): void {
88 if (!this.started) {
89 logger.warn(`${this.logPrefix()} is already stopped`)
90 return
265e4266 91 }
66a7748d
JB
92 if (this.stopping) {
93 logger.warn(`${this.logPrefix()} is already stopping`)
94 return
11353865 95 }
66a7748d
JB
96 this.stopping = true
97 this.stopConnectors()
98 this.started = false
99 this.stopping = false
6af9012e
JB
100 }
101
66a7748d
JB
102 public startConnector (connectorId: number): void {
103 if (!checkChargingStation(this.chargingStation, this.logPrefix(connectorId))) {
104 return
d1c6c833 105 }
66a7748d
JB
106 if (!this.connectorsStatus.has(connectorId)) {
107 logger.error(`${this.logPrefix(connectorId)} starting on non existing connector`)
108 throw new BaseError(`Connector ${connectorId} does not exist`)
a5e9befc
JB
109 }
110 if (this.connectorsStatus.get(connectorId)?.start === false) {
66a7748d 111 this.internalStartConnector(connectorId).catch(Constants.EMPTY_FUNCTION)
ecb3869d 112 } else if (this.connectorsStatus.get(connectorId)?.start === true) {
66a7748d 113 logger.warn(`${this.logPrefix(connectorId)} is already started on connector`)
a5e9befc
JB
114 }
115 }
116
66a7748d
JB
117 public stopConnector (connectorId: number): void {
118 if (!this.connectorsStatus.has(connectorId)) {
119 logger.error(`${this.logPrefix(connectorId)} stopping on non existing connector`)
120 throw new BaseError(`Connector ${connectorId} does not exist`)
ba7965c4
JB
121 }
122 if (this.connectorsStatus.get(connectorId)?.start === true) {
66a7748d
JB
123 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
124 this.connectorsStatus.get(connectorId)!.start = false
ba7965c4 125 } else if (this.connectorsStatus.get(connectorId)?.start === false) {
66a7748d 126 logger.warn(`${this.logPrefix(connectorId)} is already stopped on connector`)
ba7965c4 127 }
a5e9befc
JB
128 }
129
66a7748d 130 private startConnectors (): void {
e7aeea18 131 if (
5199f9fd 132 this.connectorsStatus.size > 0 &&
e7aeea18
JB
133 this.connectorsStatus.size !== this.chargingStation.getNumberOfConnectors()
134 ) {
66a7748d
JB
135 this.connectorsStatus.clear()
136 this.initializeConnectorsStatus()
54544ef1 137 }
4334db72
JB
138 if (this.chargingStation.hasEvses) {
139 for (const [evseId, evseStatus] of this.chargingStation.evses) {
140 if (evseId > 0) {
141 for (const connectorId of evseStatus.connectors.keys()) {
66a7748d 142 this.startConnector(connectorId)
4334db72
JB
143 }
144 }
145 }
146 } else {
147 for (const connectorId of this.chargingStation.connectors.keys()) {
148 if (connectorId > 0) {
66a7748d 149 this.startConnector(connectorId)
4334db72 150 }
72740232
JB
151 }
152 }
153 }
154
66a7748d 155 private stopConnectors (): void {
4334db72
JB
156 if (this.chargingStation.hasEvses) {
157 for (const [evseId, evseStatus] of this.chargingStation.evses) {
158 if (evseId > 0) {
159 for (const connectorId of evseStatus.connectors.keys()) {
66a7748d 160 this.stopConnector(connectorId)
4334db72
JB
161 }
162 }
163 }
164 } else {
165 for (const connectorId of this.chargingStation.connectors.keys()) {
166 if (connectorId > 0) {
66a7748d 167 this.stopConnector(connectorId)
4334db72 168 }
72740232
JB
169 }
170 }
171 }
172
66a7748d
JB
173 private async internalStartConnector (connectorId: number): Promise<void> {
174 this.setStartConnectorStatus(connectorId)
e7aeea18 175 logger.info(
44eb6026 176 `${this.logPrefix(
66a7748d 177 connectorId
9bf0ef23 178 )} started on connector and will run for ${formatDurationMilliSeconds(
66a7748d 179 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
e1d9a0f4 180 this.connectorsStatus.get(connectorId)!.stopDate!.getTime() -
66a7748d
JB
181 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
182 this.connectorsStatus.get(connectorId)!.startDate!.getTime()
183 )}`
184 )
1895299d 185 while (this.connectorsStatus.get(connectorId)?.start === true) {
66a7748d
JB
186 await this.waitChargingStationAvailable(connectorId)
187 await this.waitConnectorAvailable(connectorId)
0bd926c1 188 if (!this.canStartConnector(connectorId)) {
66a7748d
JB
189 this.stopConnector(connectorId)
190 break
9b4d0c70 191 }
be4c6702 192 const wait = secondsToMilliseconds(
9bf0ef23 193 getRandomInteger(
ac7f79af 194 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
5199f9fd 195 ?.maxDelayBetweenTwoTransactions,
ac7f79af 196 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
5199f9fd 197 ?.minDelayBetweenTwoTransactions
66a7748d
JB
198 )
199 )
200 logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`)
201 await sleep(wait)
202 const start = secureRandom()
ac7f79af
JB
203 if (
204 start <
5199f9fd
JB
205 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
206 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.probabilityOfStart
ac7f79af 207 ) {
66a7748d
JB
208 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
209 this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
6af9012e 210 // Start transaction
66a7748d 211 const startResponse = await this.startTransaction(connectorId)
5199f9fd 212 if (startResponse?.idTagInfo.status === AuthorizationStatus.ACCEPTED) {
6af9012e 213 // Wait until end of transaction
be4c6702 214 const waitTrxEnd = secondsToMilliseconds(
9bf0ef23 215 getRandomInteger(
5199f9fd
JB
216 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration,
217 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration
66a7748d
JB
218 )
219 )
e7aeea18 220 logger.info(
1c9de2b9 221 `${this.logPrefix(
66a7748d 222 connectorId
1c9de2b9 223 )} transaction started with id ${this.chargingStation.getConnectorStatus(connectorId)
66a7748d
JB
224 ?.transactionId} and will stop in ${formatDurationMilliSeconds(waitTrxEnd)}`
225 )
226 await sleep(waitTrxEnd)
227 await this.stopTransaction(connectorId)
6af9012e
JB
228 }
229 } else {
66a7748d 230 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 231 ++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions
66a7748d 232 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 233 ++this.connectorsStatus.get(connectorId)!.skippedTransactions
e7aeea18 234 logger.info(
1c9de2b9 235 `${this.logPrefix(connectorId)} skipped consecutively ${this.connectorsStatus.get(
66a7748d 236 connectorId
1c9de2b9 237 )?.skippedConsecutiveTransactions}/${this.connectorsStatus.get(connectorId)
66a7748d
JB
238 ?.skippedTransactions} transaction(s)`
239 )
6af9012e 240 }
66a7748d
JB
241 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
242 this.connectorsStatus.get(connectorId)!.lastRunDate = new Date()
7d75bee1 243 }
66a7748d
JB
244 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
245 this.connectorsStatus.get(connectorId)!.stoppedDate = new Date()
e7aeea18 246 logger.info(
44eb6026 247 `${this.logPrefix(
66a7748d 248 connectorId
9bf0ef23 249 )} stopped on connector and lasted for ${formatDurationMilliSeconds(
66a7748d 250 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
e1d9a0f4 251 this.connectorsStatus.get(connectorId)!.stoppedDate!.getTime() -
66a7748d
JB
252 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
253 this.connectorsStatus.get(connectorId)!.startDate!.getTime()
254 )}`
255 )
e7aeea18 256 logger.debug(
be9ee554 257 `${this.logPrefix(connectorId)} connector status: %j`,
66a7748d
JB
258 this.connectorsStatus.get(connectorId)
259 )
6af9012e
JB
260 }
261
66a7748d 262 private setStartConnectorStatus (connectorId: number): void {
66a7748d
JB
263 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
264 this.connectorsStatus.get(connectorId)!.startDate = new Date()
265 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0193fdd3 266 if (!isValidTime(this.connectorsStatus.get(connectorId)!.stopDate)) {
66a7748d 267 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0193fdd3
JB
268 this.connectorsStatus.get(connectorId)!.stopDate = new Date(
269 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
270 this.connectorsStatus.get(connectorId)!.startDate!.getTime() +
271 hoursToMilliseconds(
272 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
273 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.stopAfterHours
274 )
275 )
276 }
0a1dd746
JB
277 delete this.connectorsStatus.get(connectorId)?.stoppedDate
278 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
279 this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
66a7748d
JB
280 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
281 this.connectorsStatus.get(connectorId)!.start = true
4dff3039
JB
282 }
283
66a7748d
JB
284 private canStartConnector (connectorId: number): boolean {
285 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0bd926c1 286 if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) {
0a1dd746
JB
287 logger.info(
288 `${this.logPrefix(
289 connectorId
290 )} entered in transaction loop while the ATG stop date has been reached`
291 )
66a7748d 292 return false
0bd926c1 293 }
66a7748d 294 if (!this.chargingStation.inAcceptedState()) {
0bd926c1
JB
295 logger.error(
296 `${this.logPrefix(
66a7748d
JB
297 connectorId
298 )} entered in transaction loop while the charging station is not in accepted state`
299 )
300 return false
0bd926c1 301 }
66a7748d 302 if (!this.chargingStation.isChargingStationAvailable()) {
0bd926c1
JB
303 logger.info(
304 `${this.logPrefix(
66a7748d
JB
305 connectorId
306 )} entered in transaction loop while the charging station is unavailable`
307 )
308 return false
0bd926c1 309 }
66a7748d 310 if (!this.chargingStation.isConnectorAvailable(connectorId)) {
0bd926c1
JB
311 logger.info(
312 `${this.logPrefix(
66a7748d
JB
313 connectorId
314 )} entered in transaction loop while the connector ${connectorId} is unavailable`
315 )
316 return false
0bd926c1 317 }
66a7748d 318 return true
0bd926c1
JB
319 }
320
66a7748d
JB
321 private async waitChargingStationAvailable (connectorId: number): Promise<void> {
322 let logged = false
60400e23
JB
323 while (!this.chargingStation.isChargingStationAvailable()) {
324 if (!logged) {
325 logger.info(
326 `${this.logPrefix(
66a7748d
JB
327 connectorId
328 )} transaction loop waiting for charging station to be available`
329 )
330 logged = true
60400e23 331 }
66a7748d 332 await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
3e888c65
JB
333 }
334 }
335
66a7748d
JB
336 private async waitConnectorAvailable (connectorId: number): Promise<void> {
337 let logged = false
60400e23
JB
338 while (!this.chargingStation.isConnectorAvailable(connectorId)) {
339 if (!logged) {
340 logger.info(
341 `${this.logPrefix(
66a7748d
JB
342 connectorId
343 )} transaction loop waiting for connector ${connectorId} to be available`
344 )
345 logged = true
60400e23 346 }
66a7748d 347 await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
3e888c65
JB
348 }
349 }
350
66a7748d 351 private initializeConnectorsStatus (): void {
4334db72
JB
352 if (this.chargingStation.hasEvses) {
353 for (const [evseId, evseStatus] of this.chargingStation.evses) {
354 if (evseId > 0) {
355 for (const connectorId of evseStatus.connectors.keys()) {
66a7748d 356 this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId))
4334db72
JB
357 }
358 }
359 }
360 } else {
361 for (const connectorId of this.chargingStation.connectors.keys()) {
362 if (connectorId > 0) {
66a7748d 363 this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId))
4334db72 364 }
4dff3039
JB
365 }
366 }
72740232
JB
367 }
368
66a7748d
JB
369 private getConnectorStatus (connectorId: number): Status {
370 const connectorStatus =
0a1dd746 371 this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId - 1] != null
66a7748d
JB
372 ? cloneObject<Status>(
373 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 374 this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1]
a82d0329 375 )
66a7748d 376 : undefined
6dde6c5f
JB
377 if (connectorStatus != null) {
378 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
379 connectorStatus.startDate = convertToDate(connectorStatus.startDate)!
380 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
381 connectorStatus.lastRunDate = convertToDate(connectorStatus.lastRunDate)!
382 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
383 connectorStatus.stopDate = convertToDate(connectorStatus.stopDate)!
384 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
385 connectorStatus.stoppedDate = convertToDate(connectorStatus.stoppedDate)!
386 if (
387 !this.started &&
388 (connectorStatus.start ||
389 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.enable !== true)
390 ) {
391 connectorStatus.start = false
392 }
393 }
5ced7e80
JB
394 return (
395 connectorStatus ?? {
396 start: false,
397 authorizeRequests: 0,
398 acceptedAuthorizeRequests: 0,
399 rejectedAuthorizeRequests: 0,
400 startTransactionRequests: 0,
401 acceptedStartTransactionRequests: 0,
402 rejectedStartTransactionRequests: 0,
403 stopTransactionRequests: 0,
404 acceptedStopTransactionRequests: 0,
405 rejectedStopTransactionRequests: 0,
406 skippedConsecutiveTransactions: 0,
66a7748d 407 skippedTransactions: 0
5ced7e80 408 }
66a7748d 409 )
5ced7e80
JB
410 }
411
66a7748d
JB
412 private async startTransaction (
413 connectorId: number
0afed85f 414 ): Promise<StartTransactionResponse | undefined> {
66a7748d
JB
415 const measureId = 'StartTransaction with ATG'
416 const beginId = PerformanceStatistics.beginMeasure(measureId)
417 let startResponse: StartTransactionResponse | undefined
f911a4af
JB
418 if (this.chargingStation.hasIdTags()) {
419 const idTag = IdTagsCache.getInstance().getIdTag(
66a7748d 420 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
5199f9fd 421 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.idTagDistribution!,
aaf2bf9c 422 this.chargingStation,
66a7748d
JB
423 connectorId
424 )
5cf9050d 425 const startTransactionLogMsg = `${this.logPrefix(
66a7748d
JB
426 connectorId
427 )} start transaction with an idTag '${idTag}'`
ccb1d6e9 428 if (this.getRequireAuthorize()) {
66a7748d 429 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 430 ++this.connectorsStatus.get(connectorId)!.authorizeRequests
041365be 431 if (await isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
66a7748d 432 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 433 ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests
66a7748d 434 logger.info(startTransactionLogMsg)
5fdab605 435 // Start transaction
f7f98c68 436 startResponse = await this.chargingStation.ocppRequestService.requestHandler<
66a7748d
JB
437 StartTransactionRequest,
438 StartTransactionResponse
08f130a0 439 >(this.chargingStation, RequestCommand.START_TRANSACTION, {
ef6fa3fb 440 connectorId,
66a7748d
JB
441 idTag
442 })
443 this.handleStartTransactionResponse(connectorId, startResponse)
444 PerformanceStatistics.endMeasure(measureId, beginId)
445 return startResponse
5fdab605 446 }
66a7748d 447 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 448 ++this.connectorsStatus.get(connectorId)!.rejectedAuthorizeRequests
66a7748d
JB
449 PerformanceStatistics.endMeasure(measureId, beginId)
450 return startResponse
ef6076c1 451 }
66a7748d 452 logger.info(startTransactionLogMsg)
5fdab605 453 // Start transaction
f7f98c68 454 startResponse = await this.chargingStation.ocppRequestService.requestHandler<
66a7748d
JB
455 StartTransactionRequest,
456 StartTransactionResponse
08f130a0 457 >(this.chargingStation, RequestCommand.START_TRANSACTION, {
ef6fa3fb 458 connectorId,
66a7748d
JB
459 idTag
460 })
461 this.handleStartTransactionResponse(connectorId, startResponse)
462 PerformanceStatistics.endMeasure(measureId, beginId)
463 return startResponse
6af9012e 464 }
66a7748d 465 logger.info(`${this.logPrefix(connectorId)} start transaction without an idTag`)
f7f98c68 466 startResponse = await this.chargingStation.ocppRequestService.requestHandler<
66a7748d
JB
467 StartTransactionRequest,
468 StartTransactionResponse
469 >(this.chargingStation, RequestCommand.START_TRANSACTION, { connectorId })
470 this.handleStartTransactionResponse(connectorId, startResponse)
471 PerformanceStatistics.endMeasure(measureId, beginId)
472 return startResponse
6af9012e
JB
473 }
474
66a7748d 475 private async stopTransaction (
e7aeea18 476 connectorId: number,
66a7748d 477 reason = StopTransactionReason.LOCAL
e1d9a0f4 478 ): Promise<StopTransactionResponse | undefined> {
66a7748d
JB
479 const measureId = 'StopTransaction with ATG'
480 const beginId = PerformanceStatistics.beginMeasure(measureId)
481 let stopResponse: StopTransactionResponse | undefined
6d9876e7 482 if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
49563992
JB
483 logger.info(
484 `${this.logPrefix(
66a7748d 485 connectorId
49563992 486 )} stop transaction with id ${this.chargingStation.getConnectorStatus(connectorId)
66a7748d
JB
487 ?.transactionId}`
488 )
489 stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason)
490 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 491 ++this.connectorsStatus.get(connectorId)!.stopTransactionRequests
5199f9fd 492 if (stopResponse.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
66a7748d 493 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 494 ++this.connectorsStatus.get(connectorId)!.acceptedStopTransactionRequests
6d9876e7 495 } else {
66a7748d 496 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 497 ++this.connectorsStatus.get(connectorId)!.rejectedStopTransactionRequests
6d9876e7 498 }
0045cef5 499 } else {
66a7748d 500 const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId
ff581359 501 logger.debug(
ba7965c4 502 `${this.logPrefix(connectorId)} stopping a not started transaction${
401fa922 503 transactionId != null ? ` with id ${transactionId}` : ''
66a7748d
JB
504 }`
505 )
0045cef5 506 }
66a7748d
JB
507 PerformanceStatistics.endMeasure(measureId, beginId)
508 return stopResponse
c0560973
JB
509 }
510
66a7748d 511 private getRequireAuthorize (): boolean {
ac7f79af
JB
512 return (
513 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.requireAuthorize ?? true
66a7748d 514 )
ccb1d6e9
JB
515 }
516
66a7748d 517 private readonly logPrefix = (connectorId?: number): string => {
9bf0ef23 518 return logPrefix(
5199f9fd 519 ` ${this.chargingStation.stationInfo?.chargingStationId} | ATG${
401fa922 520 connectorId != null ? ` on connector #${connectorId}` : ''
66a7748d
JB
521 }:`
522 )
523 }
d9ac47ef 524
66a7748d 525 private handleStartTransactionResponse (
d9ac47ef 526 connectorId: number,
66a7748d 527 startResponse: StartTransactionResponse
d9ac47ef 528 ): void {
66a7748d 529 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 530 ++this.connectorsStatus.get(connectorId)!.startTransactionRequests
5199f9fd 531 if (startResponse.idTagInfo.status === AuthorizationStatus.ACCEPTED) {
66a7748d 532 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 533 ++this.connectorsStatus.get(connectorId)!.acceptedStartTransactionRequests
d9ac47ef 534 } else {
66a7748d
JB
535 logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`)
536 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 537 ++this.connectorsStatus.get(connectorId)!.rejectedStartTransactionRequests
d9ac47ef
JB
538 }
539 }
6af9012e 540}