fix: ensure the ATG is properly restored after disconnection to CSMS
[e-mobility-charging-stations-simulator.git] / src / charging-station / AutomaticTransactionGenerator.ts
CommitLineData
a19b897d 1// Partial Copyright Jerome Benoit. 2021-2024. 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,
40615072 22 clone,
6dde6c5f 23 convertToDate,
9bf0ef23
JB
24 formatDurationMilliSeconds,
25 getRandomInteger,
5dc7c990 26 isValidDate,
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
e054fc1c 69 public start (stopAbsoluteDuration?: boolean): void {
66a7748d
JB
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 81 this.starting = true
e054fc1c 82 this.startConnectors(stopAbsoluteDuration)
66a7748d
JB
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
e054fc1c 102 public startConnector (connectorId: number, stopAbsoluteDuration?: boolean): void {
66a7748d
JB
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) {
e054fc1c 111 this.internalStartConnector(connectorId, stopAbsoluteDuration).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
e054fc1c 130 private startConnectors (stopAbsoluteDuration?: boolean): 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()) {
e054fc1c 142 this.startConnector(connectorId, stopAbsoluteDuration)
4334db72
JB
143 }
144 }
145 }
146 } else {
147 for (const connectorId of this.chargingStation.connectors.keys()) {
148 if (connectorId > 0) {
e054fc1c 149 this.startConnector(connectorId, stopAbsoluteDuration)
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
e054fc1c
JB
173 private async internalStartConnector (
174 connectorId: number,
175 stopAbsoluteDuration?: boolean
176 ): Promise<void> {
177 this.setStartConnectorStatus(connectorId, stopAbsoluteDuration)
e7aeea18 178 logger.info(
44eb6026 179 `${this.logPrefix(
66a7748d 180 connectorId
9bf0ef23 181 )} started on connector and will run for ${formatDurationMilliSeconds(
66a7748d 182 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
e1d9a0f4 183 this.connectorsStatus.get(connectorId)!.stopDate!.getTime() -
66a7748d
JB
184 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
185 this.connectorsStatus.get(connectorId)!.startDate!.getTime()
186 )}`
187 )
1895299d 188 while (this.connectorsStatus.get(connectorId)?.start === true) {
66a7748d
JB
189 await this.waitChargingStationAvailable(connectorId)
190 await this.waitConnectorAvailable(connectorId)
0bd926c1 191 if (!this.canStartConnector(connectorId)) {
66a7748d
JB
192 this.stopConnector(connectorId)
193 break
9b4d0c70 194 }
be4c6702 195 const wait = secondsToMilliseconds(
9bf0ef23 196 getRandomInteger(
ac7f79af 197 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
5199f9fd 198 ?.maxDelayBetweenTwoTransactions,
ac7f79af 199 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
5199f9fd 200 ?.minDelayBetweenTwoTransactions
66a7748d
JB
201 )
202 )
203 logger.info(`${this.logPrefix(connectorId)} waiting for ${formatDurationMilliSeconds(wait)}`)
204 await sleep(wait)
205 const start = secureRandom()
ac7f79af
JB
206 if (
207 start <
5199f9fd
JB
208 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
209 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.probabilityOfStart
ac7f79af 210 ) {
66a7748d
JB
211 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
212 this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
6af9012e 213 // Start transaction
66a7748d 214 const startResponse = await this.startTransaction(connectorId)
5199f9fd 215 if (startResponse?.idTagInfo.status === AuthorizationStatus.ACCEPTED) {
6af9012e 216 // Wait until end of transaction
be4c6702 217 const waitTrxEnd = secondsToMilliseconds(
9bf0ef23 218 getRandomInteger(
5199f9fd
JB
219 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.maxDuration,
220 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.minDuration
66a7748d
JB
221 )
222 )
e7aeea18 223 logger.info(
a223d9be
JB
224 `${this.logPrefix(connectorId)} transaction started with id ${
225 this.chargingStation.getConnectorStatus(connectorId)?.transactionId
226 } and will stop in ${formatDurationMilliSeconds(waitTrxEnd)}`
66a7748d
JB
227 )
228 await sleep(waitTrxEnd)
229 await this.stopTransaction(connectorId)
6af9012e
JB
230 }
231 } else {
66a7748d 232 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 233 ++this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions
66a7748d 234 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 235 ++this.connectorsStatus.get(connectorId)!.skippedTransactions
e7aeea18 236 logger.info(
a223d9be
JB
237 `${this.logPrefix(connectorId)} skipped consecutively ${
238 this.connectorsStatus.get(connectorId)?.skippedConsecutiveTransactions
239 }/${this.connectorsStatus.get(connectorId)?.skippedTransactions} transaction(s)`
66a7748d 240 )
6af9012e 241 }
66a7748d
JB
242 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
243 this.connectorsStatus.get(connectorId)!.lastRunDate = new Date()
7d75bee1 244 }
66a7748d
JB
245 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
246 this.connectorsStatus.get(connectorId)!.stoppedDate = new Date()
e7aeea18 247 logger.info(
44eb6026 248 `${this.logPrefix(
66a7748d 249 connectorId
9bf0ef23 250 )} stopped on connector and lasted for ${formatDurationMilliSeconds(
66a7748d 251 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
e1d9a0f4 252 this.connectorsStatus.get(connectorId)!.stoppedDate!.getTime() -
66a7748d
JB
253 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
254 this.connectorsStatus.get(connectorId)!.startDate!.getTime()
255 )}`
256 )
e7aeea18 257 logger.debug(
be9ee554 258 `${this.logPrefix(connectorId)} connector status: %j`,
66a7748d
JB
259 this.connectorsStatus.get(connectorId)
260 )
6af9012e
JB
261 }
262
e054fc1c
JB
263 private setStartConnectorStatus (
264 connectorId: number,
265 stopAbsoluteDuration = this.chargingStation.getAutomaticTransactionGeneratorConfiguration()
266 ?.stopAbsoluteDuration
267 ): void {
66a7748d
JB
268 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
269 this.connectorsStatus.get(connectorId)!.startDate = new Date()
46a830d2 270 if (
e054fc1c 271 stopAbsoluteDuration === false ||
46a830d2 272 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
5dc7c990 273 !isValidDate(this.connectorsStatus.get(connectorId)!.stopDate)
46a830d2 274 ) {
66a7748d 275 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0193fdd3
JB
276 this.connectorsStatus.get(connectorId)!.stopDate = new Date(
277 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
278 this.connectorsStatus.get(connectorId)!.startDate!.getTime() +
279 hoursToMilliseconds(
280 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
281 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.stopAfterHours
282 )
283 )
284 }
0a1dd746
JB
285 delete this.connectorsStatus.get(connectorId)?.stoppedDate
286 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
287 this.connectorsStatus.get(connectorId)!.skippedConsecutiveTransactions = 0
66a7748d
JB
288 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
289 this.connectorsStatus.get(connectorId)!.start = true
4dff3039
JB
290 }
291
66a7748d
JB
292 private canStartConnector (connectorId: number): boolean {
293 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0bd926c1 294 if (new Date() > this.connectorsStatus.get(connectorId)!.stopDate!) {
0a1dd746
JB
295 logger.info(
296 `${this.logPrefix(
297 connectorId
298 )} entered in transaction loop while the ATG stop date has been reached`
299 )
66a7748d 300 return false
0bd926c1 301 }
66a7748d 302 if (!this.chargingStation.inAcceptedState()) {
0bd926c1
JB
303 logger.error(
304 `${this.logPrefix(
66a7748d
JB
305 connectorId
306 )} entered in transaction loop while the charging station is not in accepted state`
307 )
308 return false
0bd926c1 309 }
66a7748d 310 if (!this.chargingStation.isChargingStationAvailable()) {
0bd926c1
JB
311 logger.info(
312 `${this.logPrefix(
66a7748d
JB
313 connectorId
314 )} entered in transaction loop while the charging station is unavailable`
315 )
316 return false
0bd926c1 317 }
66a7748d 318 if (!this.chargingStation.isConnectorAvailable(connectorId)) {
0bd926c1
JB
319 logger.info(
320 `${this.logPrefix(
66a7748d
JB
321 connectorId
322 )} entered in transaction loop while the connector ${connectorId} is unavailable`
323 )
324 return false
0bd926c1 325 }
66a7748d 326 return true
0bd926c1
JB
327 }
328
66a7748d
JB
329 private async waitChargingStationAvailable (connectorId: number): Promise<void> {
330 let logged = false
60400e23
JB
331 while (!this.chargingStation.isChargingStationAvailable()) {
332 if (!logged) {
333 logger.info(
334 `${this.logPrefix(
66a7748d
JB
335 connectorId
336 )} transaction loop waiting for charging station to be available`
337 )
338 logged = true
60400e23 339 }
66a7748d 340 await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
3e888c65
JB
341 }
342 }
343
66a7748d
JB
344 private async waitConnectorAvailable (connectorId: number): Promise<void> {
345 let logged = false
60400e23
JB
346 while (!this.chargingStation.isConnectorAvailable(connectorId)) {
347 if (!logged) {
348 logger.info(
349 `${this.logPrefix(
66a7748d
JB
350 connectorId
351 )} transaction loop waiting for connector ${connectorId} to be available`
352 )
353 logged = true
60400e23 354 }
66a7748d 355 await sleep(Constants.CHARGING_STATION_ATG_AVAILABILITY_TIME)
3e888c65
JB
356 }
357 }
358
66a7748d 359 private initializeConnectorsStatus (): void {
4334db72
JB
360 if (this.chargingStation.hasEvses) {
361 for (const [evseId, evseStatus] of this.chargingStation.evses) {
362 if (evseId > 0) {
363 for (const connectorId of evseStatus.connectors.keys()) {
66a7748d 364 this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId))
4334db72
JB
365 }
366 }
367 }
368 } else {
369 for (const connectorId of this.chargingStation.connectors.keys()) {
370 if (connectorId > 0) {
66a7748d 371 this.connectorsStatus.set(connectorId, this.getConnectorStatus(connectorId))
4334db72 372 }
4dff3039
JB
373 }
374 }
72740232
JB
375 }
376
66a7748d 377 private getConnectorStatus (connectorId: number): Status {
e3fbf1af
JB
378 const statusIndex = connectorId - 1
379 let connectorStatus: Status | undefined
380 if (this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[statusIndex] != null) {
381 connectorStatus = clone<Status>(
382 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
383 this.chargingStation.getAutomaticTransactionGeneratorStatuses()![statusIndex]
384 )
385 } else if (this.chargingStation.getAutomaticTransactionGeneratorStatuses() != null) {
703d80dd
JB
386 logger.warn(
387 `${this.logPrefix(connectorId)} no status found for connector #${connectorId} in charging station configuration file. New status will be created`
388 )
e3fbf1af 389 }
6dde6c5f 390 if (connectorStatus != null) {
3423c8a5
JB
391 connectorStatus.startDate = convertToDate(connectorStatus.startDate)
392 connectorStatus.lastRunDate = convertToDate(connectorStatus.lastRunDate)
393 connectorStatus.stopDate = convertToDate(connectorStatus.stopDate)
394 connectorStatus.stoppedDate = convertToDate(connectorStatus.stoppedDate)
6dde6c5f
JB
395 if (
396 !this.started &&
397 (connectorStatus.start ||
398 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.enable !== true)
399 ) {
400 connectorStatus.start = false
401 }
402 }
5ced7e80
JB
403 return (
404 connectorStatus ?? {
405 start: false,
406 authorizeRequests: 0,
407 acceptedAuthorizeRequests: 0,
408 rejectedAuthorizeRequests: 0,
409 startTransactionRequests: 0,
410 acceptedStartTransactionRequests: 0,
411 rejectedStartTransactionRequests: 0,
412 stopTransactionRequests: 0,
413 acceptedStopTransactionRequests: 0,
414 rejectedStopTransactionRequests: 0,
415 skippedConsecutiveTransactions: 0,
66a7748d 416 skippedTransactions: 0
5ced7e80 417 }
66a7748d 418 )
5ced7e80
JB
419 }
420
66a7748d
JB
421 private async startTransaction (
422 connectorId: number
0afed85f 423 ): Promise<StartTransactionResponse | undefined> {
66a7748d
JB
424 const measureId = 'StartTransaction with ATG'
425 const beginId = PerformanceStatistics.beginMeasure(measureId)
426 let startResponse: StartTransactionResponse | undefined
f911a4af
JB
427 if (this.chargingStation.hasIdTags()) {
428 const idTag = IdTagsCache.getInstance().getIdTag(
66a7748d 429 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
5199f9fd 430 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()!.idTagDistribution!,
aaf2bf9c 431 this.chargingStation,
66a7748d
JB
432 connectorId
433 )
5cf9050d 434 const startTransactionLogMsg = `${this.logPrefix(
66a7748d
JB
435 connectorId
436 )} start transaction with an idTag '${idTag}'`
ccb1d6e9 437 if (this.getRequireAuthorize()) {
66a7748d 438 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 439 ++this.connectorsStatus.get(connectorId)!.authorizeRequests
041365be 440 if (await isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
66a7748d 441 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 442 ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests
66a7748d 443 logger.info(startTransactionLogMsg)
5fdab605 444 // Start transaction
f7f98c68 445 startResponse = await this.chargingStation.ocppRequestService.requestHandler<
66a7748d
JB
446 StartTransactionRequest,
447 StartTransactionResponse
08f130a0 448 >(this.chargingStation, RequestCommand.START_TRANSACTION, {
ef6fa3fb 449 connectorId,
66a7748d
JB
450 idTag
451 })
452 this.handleStartTransactionResponse(connectorId, startResponse)
453 PerformanceStatistics.endMeasure(measureId, beginId)
454 return startResponse
5fdab605 455 }
66a7748d 456 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 457 ++this.connectorsStatus.get(connectorId)!.rejectedAuthorizeRequests
66a7748d
JB
458 PerformanceStatistics.endMeasure(measureId, beginId)
459 return startResponse
ef6076c1 460 }
66a7748d 461 logger.info(startTransactionLogMsg)
5fdab605 462 // Start transaction
f7f98c68 463 startResponse = await this.chargingStation.ocppRequestService.requestHandler<
66a7748d
JB
464 StartTransactionRequest,
465 StartTransactionResponse
08f130a0 466 >(this.chargingStation, RequestCommand.START_TRANSACTION, {
ef6fa3fb 467 connectorId,
66a7748d
JB
468 idTag
469 })
470 this.handleStartTransactionResponse(connectorId, startResponse)
471 PerformanceStatistics.endMeasure(measureId, beginId)
472 return startResponse
6af9012e 473 }
66a7748d 474 logger.info(`${this.logPrefix(connectorId)} start transaction without an idTag`)
f7f98c68 475 startResponse = await this.chargingStation.ocppRequestService.requestHandler<
66a7748d
JB
476 StartTransactionRequest,
477 StartTransactionResponse
478 >(this.chargingStation, RequestCommand.START_TRANSACTION, { connectorId })
479 this.handleStartTransactionResponse(connectorId, startResponse)
480 PerformanceStatistics.endMeasure(measureId, beginId)
481 return startResponse
6af9012e
JB
482 }
483
66a7748d 484 private async stopTransaction (
e7aeea18 485 connectorId: number,
66a7748d 486 reason = StopTransactionReason.LOCAL
e1d9a0f4 487 ): Promise<StopTransactionResponse | undefined> {
66a7748d
JB
488 const measureId = 'StopTransaction with ATG'
489 const beginId = PerformanceStatistics.beginMeasure(measureId)
490 let stopResponse: StopTransactionResponse | undefined
6d9876e7 491 if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
49563992 492 logger.info(
a223d9be
JB
493 `${this.logPrefix(connectorId)} stop transaction with id ${
494 this.chargingStation.getConnectorStatus(connectorId)?.transactionId
495 }`
66a7748d
JB
496 )
497 stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason)
498 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 499 ++this.connectorsStatus.get(connectorId)!.stopTransactionRequests
5199f9fd 500 if (stopResponse.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
66a7748d 501 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 502 ++this.connectorsStatus.get(connectorId)!.acceptedStopTransactionRequests
6d9876e7 503 } else {
66a7748d 504 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 505 ++this.connectorsStatus.get(connectorId)!.rejectedStopTransactionRequests
6d9876e7 506 }
0045cef5 507 } else {
66a7748d 508 const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId
ff581359 509 logger.debug(
ba7965c4 510 `${this.logPrefix(connectorId)} stopping a not started transaction${
401fa922 511 transactionId != null ? ` with id ${transactionId}` : ''
66a7748d
JB
512 }`
513 )
0045cef5 514 }
66a7748d
JB
515 PerformanceStatistics.endMeasure(measureId, beginId)
516 return stopResponse
c0560973
JB
517 }
518
66a7748d 519 private getRequireAuthorize (): boolean {
ac7f79af
JB
520 return (
521 this.chargingStation.getAutomaticTransactionGeneratorConfiguration()?.requireAuthorize ?? true
66a7748d 522 )
ccb1d6e9
JB
523 }
524
66a7748d 525 private readonly logPrefix = (connectorId?: number): string => {
9bf0ef23 526 return logPrefix(
5199f9fd 527 ` ${this.chargingStation.stationInfo?.chargingStationId} | ATG${
401fa922 528 connectorId != null ? ` on connector #${connectorId}` : ''
66a7748d
JB
529 }:`
530 )
531 }
d9ac47ef 532
66a7748d 533 private handleStartTransactionResponse (
d9ac47ef 534 connectorId: number,
66a7748d 535 startResponse: StartTransactionResponse
d9ac47ef 536 ): void {
66a7748d 537 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 538 ++this.connectorsStatus.get(connectorId)!.startTransactionRequests
5199f9fd 539 if (startResponse.idTagInfo.status === AuthorizationStatus.ACCEPTED) {
66a7748d 540 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 541 ++this.connectorsStatus.get(connectorId)!.acceptedStartTransactionRequests
d9ac47ef 542 } else {
66a7748d
JB
543 logger.warn(`${this.logPrefix(connectorId)} start transaction rejected`)
544 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
0a1dd746 545 ++this.connectorsStatus.get(connectorId)!.rejectedStartTransactionRequests
d9ac47ef
JB
546 }
547 }
6af9012e 548}