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