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