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