Commit | Line | Data |
---|---|---|
c8eeb62b JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
8114d10e | 3 | import PerformanceStatistics from '../performance/PerformanceStatistics'; |
6c1761d4 | 4 | import type { |
8114d10e JB |
5 | AutomaticTransactionGeneratorConfiguration, |
6 | Status, | |
7 | } from '../types/AutomaticTransactionGenerator'; | |
5e3cb728 | 8 | import { RequestCommand } from '../types/ocpp/Requests'; |
e7aeea18 JB |
9 | import { |
10 | AuthorizationStatus, | |
976d11ec JB |
11 | type AuthorizeRequest, |
12 | type AuthorizeResponse, | |
13 | type StartTransactionRequest, | |
14 | type StartTransactionResponse, | |
e7aeea18 | 15 | StopTransactionReason, |
976d11ec | 16 | type StopTransactionResponse, |
e7aeea18 | 17 | } from '../types/ocpp/Transaction'; |
6af9012e | 18 | import Constants from '../utils/Constants'; |
9f2e3130 | 19 | import logger from '../utils/Logger'; |
8114d10e JB |
20 | import Utils from '../utils/Utils'; |
21 | import type ChargingStation from './ChargingStation'; | |
6af9012e JB |
22 | |
23 | export default class AutomaticTransactionGenerator { | |
e7aeea18 JB |
24 | private static readonly instances: Map<string, AutomaticTransactionGenerator> = new Map< |
25 | string, | |
26 | AutomaticTransactionGenerator | |
27 | >(); | |
10068088 | 28 | |
5e3cb728 | 29 | public readonly connectorsStatus: Map<number, Status>; |
fa7bccf4 | 30 | public readonly configuration: AutomaticTransactionGeneratorConfiguration; |
265e4266 | 31 | public started: boolean; |
9e23580d | 32 | private readonly chargingStation: ChargingStation; |
6af9012e | 33 | |
fa7bccf4 JB |
34 | private constructor( |
35 | automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration, | |
36 | chargingStation: ChargingStation | |
37 | ) { | |
aa428a31 | 38 | this.started = false; |
fa7bccf4 | 39 | this.configuration = automaticTransactionGeneratorConfiguration; |
ad2f27c3 | 40 | this.chargingStation = chargingStation; |
9664ec50 | 41 | this.connectorsStatus = new Map<number, Status>(); |
72740232 | 42 | this.stopConnectors(); |
6af9012e JB |
43 | } |
44 | ||
fa7bccf4 JB |
45 | public static getInstance( |
46 | automaticTransactionGeneratorConfiguration: AutomaticTransactionGeneratorConfiguration, | |
47 | chargingStation: ChargingStation | |
48 | ): AutomaticTransactionGenerator { | |
51c83d6f | 49 | if (!AutomaticTransactionGenerator.instances.has(chargingStation.stationInfo.hashId)) { |
e7aeea18 | 50 | AutomaticTransactionGenerator.instances.set( |
51c83d6f | 51 | chargingStation.stationInfo.hashId, |
fa7bccf4 JB |
52 | new AutomaticTransactionGenerator( |
53 | automaticTransactionGeneratorConfiguration, | |
54 | chargingStation | |
55 | ) | |
e7aeea18 | 56 | ); |
73b9adec | 57 | } |
51c83d6f | 58 | return AutomaticTransactionGenerator.instances.get(chargingStation.stationInfo.hashId); |
73b9adec JB |
59 | } |
60 | ||
7d75bee1 | 61 | public start(): void { |
a5e9befc | 62 | if (this.started === true) { |
ba7965c4 | 63 | logger.warn(`${this.logPrefix()} is already started`); |
b809adf1 JB |
64 | return; |
65 | } | |
72740232 | 66 | this.startConnectors(); |
265e4266 | 67 | this.started = true; |
6af9012e JB |
68 | } |
69 | ||
0045cef5 | 70 | public stop(): void { |
a5e9befc | 71 | if (this.started === false) { |
ba7965c4 | 72 | logger.warn(`${this.logPrefix()} is already stopped`); |
265e4266 JB |
73 | return; |
74 | } | |
72740232 | 75 | this.stopConnectors(); |
265e4266 | 76 | this.started = false; |
6af9012e JB |
77 | } |
78 | ||
a5e9befc | 79 | public startConnector(connectorId: number): void { |
ecb3869d | 80 | if (this.chargingStation.connectors.has(connectorId) === false) { |
ba7965c4 | 81 | logger.warn(`${this.logPrefix(connectorId)} starting on non existing connector`); |
a5e9befc JB |
82 | return; |
83 | } | |
84 | if (this.connectorsStatus.get(connectorId)?.start === false) { | |
85 | // Avoid hogging the event loop with a busy loop | |
86 | setImmediate(() => { | |
87 | this.internalStartConnector(connectorId).catch(() => { | |
88 | /* This is intentional */ | |
89 | }); | |
90 | }); | |
ecb3869d | 91 | } else if (this.connectorsStatus.get(connectorId)?.start === true) { |
ba7965c4 | 92 | logger.warn(`${this.logPrefix(connectorId)} is already started on connector`); |
a5e9befc JB |
93 | } |
94 | } | |
95 | ||
96 | public stopConnector(connectorId: number): void { | |
ba7965c4 JB |
97 | if (this.chargingStation.connectors.has(connectorId) === false) { |
98 | logger.warn(`${this.logPrefix(connectorId)} stopping on non existing connector`); | |
99 | return; | |
100 | } | |
101 | if (this.connectorsStatus.get(connectorId)?.start === true) { | |
102 | this.connectorsStatus.set(connectorId, { | |
103 | ...this.connectorsStatus.get(connectorId), | |
104 | start: false, | |
105 | }); | |
106 | } else if (this.connectorsStatus.get(connectorId)?.start === false) { | |
107 | logger.warn(`${this.logPrefix(connectorId)} is already stopped on connector`); | |
108 | } | |
a5e9befc JB |
109 | } |
110 | ||
72740232 | 111 | private startConnectors(): void { |
e7aeea18 JB |
112 | if ( |
113 | this.connectorsStatus?.size > 0 && | |
114 | this.connectorsStatus.size !== this.chargingStation.getNumberOfConnectors() | |
115 | ) { | |
54544ef1 JB |
116 | this.connectorsStatus.clear(); |
117 | } | |
734d790d | 118 | for (const connectorId of this.chargingStation.connectors.keys()) { |
72740232 | 119 | if (connectorId > 0) { |
83a3286a | 120 | this.startConnector(connectorId); |
72740232 JB |
121 | } |
122 | } | |
123 | } | |
124 | ||
125 | private stopConnectors(): void { | |
734d790d | 126 | for (const connectorId of this.chargingStation.connectors.keys()) { |
72740232 JB |
127 | if (connectorId > 0) { |
128 | this.stopConnector(connectorId); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
83a3286a | 133 | private async internalStartConnector(connectorId: number): Promise<void> { |
6cd85def | 134 | this.initializeConnectorStatus(connectorId); |
e7aeea18 JB |
135 | logger.info( |
136 | this.logPrefix(connectorId) + | |
137 | ' started on connector and will run for ' + | |
138 | Utils.formatDurationMilliSeconds( | |
139 | this.connectorsStatus.get(connectorId).stopDate.getTime() - | |
140 | this.connectorsStatus.get(connectorId).startDate.getTime() | |
141 | ) | |
142 | ); | |
a5e9befc | 143 | while (this.connectorsStatus.get(connectorId).start === true) { |
e7aeea18 | 144 | if (new Date() > this.connectorsStatus.get(connectorId).stopDate) { |
9664ec50 | 145 | this.stopConnector(connectorId); |
17991e8c JB |
146 | break; |
147 | } | |
16cd35ad | 148 | if (!this.chargingStation.isInAcceptedState()) { |
e7aeea18 JB |
149 | logger.error( |
150 | this.logPrefix(connectorId) + | |
151 | ' entered in transaction loop while the charging station is not in accepted state' | |
152 | ); | |
9664ec50 | 153 | this.stopConnector(connectorId); |
17991e8c JB |
154 | break; |
155 | } | |
c0560973 | 156 | if (!this.chargingStation.isChargingStationAvailable()) { |
e7aeea18 JB |
157 | logger.info( |
158 | this.logPrefix(connectorId) + | |
159 | ' entered in transaction loop while the charging station is unavailable' | |
160 | ); | |
9664ec50 | 161 | this.stopConnector(connectorId); |
ab5f4b03 JB |
162 | break; |
163 | } | |
c0560973 | 164 | if (!this.chargingStation.isConnectorAvailable(connectorId)) { |
e7aeea18 JB |
165 | logger.info( |
166 | `${this.logPrefix( | |
167 | connectorId | |
168 | )} entered in transaction loop while the connector ${connectorId} is unavailable` | |
169 | ); | |
9c7195b2 | 170 | this.stopConnector(connectorId); |
17991e8c JB |
171 | break; |
172 | } | |
c0560973 | 173 | if (!this.chargingStation?.ocppRequestService) { |
e7aeea18 JB |
174 | logger.info( |
175 | `${this.logPrefix( | |
176 | connectorId | |
177 | )} transaction loop waiting for charging station service to be initialized` | |
178 | ); | |
c0560973 | 179 | do { |
a4cc42ea | 180 | await Utils.sleep(Constants.CHARGING_STATION_ATG_INITIALIZATION_TIME); |
c0560973 JB |
181 | } while (!this.chargingStation?.ocppRequestService); |
182 | } | |
e7aeea18 JB |
183 | const wait = |
184 | Utils.getRandomInteger( | |
fa7bccf4 JB |
185 | this.configuration.maxDelayBetweenTwoTransactions, |
186 | this.configuration.minDelayBetweenTwoTransactions | |
e7aeea18 JB |
187 | ) * 1000; |
188 | logger.info( | |
189 | this.logPrefix(connectorId) + ' waiting for ' + Utils.formatDurationMilliSeconds(wait) | |
190 | ); | |
6af9012e | 191 | await Utils.sleep(wait); |
c37528f1 | 192 | const start = Utils.secureRandom(); |
fa7bccf4 | 193 | if (start < this.configuration.probabilityOfStart) { |
9664ec50 | 194 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; |
6af9012e | 195 | // Start transaction |
aef1b33a | 196 | const startResponse = await this.startTransaction(connectorId); |
0afed85f | 197 | if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
6af9012e | 198 | // Wait until end of transaction |
e7aeea18 | 199 | const waitTrxEnd = |
fa7bccf4 JB |
200 | Utils.getRandomInteger(this.configuration.maxDuration, this.configuration.minDuration) * |
201 | 1000; | |
e7aeea18 JB |
202 | logger.info( |
203 | this.logPrefix(connectorId) + | |
204 | ' transaction ' + | |
205 | this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() + | |
206 | ' started and will stop in ' + | |
207 | Utils.formatDurationMilliSeconds(waitTrxEnd) | |
208 | ); | |
6af9012e JB |
209 | await Utils.sleep(waitTrxEnd); |
210 | // Stop transaction | |
e7aeea18 JB |
211 | logger.info( |
212 | this.logPrefix(connectorId) + | |
213 | ' stop transaction ' + | |
214 | this.chargingStation.getConnectorStatus(connectorId).transactionId.toString() | |
215 | ); | |
85d20667 | 216 | await this.stopTransaction(connectorId); |
6af9012e JB |
217 | } |
218 | } else { | |
9664ec50 JB |
219 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions++; |
220 | this.connectorsStatus.get(connectorId).skippedTransactions++; | |
e7aeea18 JB |
221 | logger.info( |
222 | this.logPrefix(connectorId) + | |
223 | ' skipped consecutively ' + | |
224 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions.toString() + | |
225 | '/' + | |
226 | this.connectorsStatus.get(connectorId).skippedTransactions.toString() + | |
227 | ' transaction(s)' | |
228 | ); | |
6af9012e | 229 | } |
9664ec50 | 230 | this.connectorsStatus.get(connectorId).lastRunDate = new Date(); |
7d75bee1 | 231 | } |
9664ec50 | 232 | this.connectorsStatus.get(connectorId).stoppedDate = new Date(); |
e7aeea18 JB |
233 | logger.info( |
234 | this.logPrefix(connectorId) + | |
235 | ' stopped on connector and lasted for ' + | |
236 | Utils.formatDurationMilliSeconds( | |
237 | this.connectorsStatus.get(connectorId).stoppedDate.getTime() - | |
238 | this.connectorsStatus.get(connectorId).startDate.getTime() | |
239 | ) | |
240 | ); | |
241 | logger.debug( | |
be9ee554 | 242 | `${this.logPrefix(connectorId)} connector status: %j`, |
e7aeea18 JB |
243 | this.connectorsStatus.get(connectorId) |
244 | ); | |
6af9012e JB |
245 | } |
246 | ||
6cd85def | 247 | private initializeConnectorStatus(connectorId: number): void { |
e7aeea18 JB |
248 | this.connectorsStatus.get(connectorId).authorizeRequests = |
249 | this?.connectorsStatus.get(connectorId)?.authorizeRequests ?? 0; | |
250 | this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests = | |
251 | this?.connectorsStatus.get(connectorId)?.acceptedAuthorizeRequests ?? 0; | |
252 | this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests = | |
253 | this?.connectorsStatus.get(connectorId)?.rejectedAuthorizeRequests ?? 0; | |
254 | this.connectorsStatus.get(connectorId).startTransactionRequests = | |
255 | this?.connectorsStatus.get(connectorId)?.startTransactionRequests ?? 0; | |
256 | this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests = | |
257 | this?.connectorsStatus.get(connectorId)?.acceptedStartTransactionRequests ?? 0; | |
258 | this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests = | |
259 | this?.connectorsStatus.get(connectorId)?.rejectedStartTransactionRequests ?? 0; | |
260 | this.connectorsStatus.get(connectorId).stopTransactionRequests = | |
261 | this?.connectorsStatus.get(connectorId)?.stopTransactionRequests ?? 0; | |
6d9876e7 JB |
262 | this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests = |
263 | this?.connectorsStatus.get(connectorId)?.acceptedStopTransactionRequests ?? 0; | |
264 | this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests = | |
265 | this?.connectorsStatus.get(connectorId)?.rejectedStopTransactionRequests ?? 0; | |
9664ec50 | 266 | this.connectorsStatus.get(connectorId).skippedConsecutiveTransactions = 0; |
e7aeea18 JB |
267 | this.connectorsStatus.get(connectorId).skippedTransactions = |
268 | this?.connectorsStatus.get(connectorId)?.skippedTransactions ?? 0; | |
269 | const previousRunDuration = | |
270 | this?.connectorsStatus.get(connectorId)?.startDate && | |
271 | this?.connectorsStatus.get(connectorId)?.lastRunDate | |
272 | ? this.connectorsStatus.get(connectorId).lastRunDate.getTime() - | |
273 | this.connectorsStatus.get(connectorId).startDate.getTime() | |
274 | : 0; | |
9664ec50 | 275 | this.connectorsStatus.get(connectorId).startDate = new Date(); |
e7aeea18 JB |
276 | this.connectorsStatus.get(connectorId).stopDate = new Date( |
277 | this.connectorsStatus.get(connectorId).startDate.getTime() + | |
fa7bccf4 | 278 | (this.configuration.stopAfterHours ?? |
e7aeea18 JB |
279 | Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * |
280 | 3600 * | |
281 | 1000 - | |
282 | previousRunDuration | |
283 | ); | |
9664ec50 | 284 | this.connectorsStatus.get(connectorId).start = true; |
72740232 JB |
285 | } |
286 | ||
e7aeea18 JB |
287 | private async startTransaction( |
288 | connectorId: number | |
0afed85f | 289 | ): Promise<StartTransactionResponse | undefined> { |
aef1b33a JB |
290 | const measureId = 'StartTransaction with ATG'; |
291 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
292 | let startResponse: StartTransactionResponse; | |
293 | if (this.chargingStation.hasAuthorizedTags()) { | |
f4bf2abd | 294 | const idTag = this.chargingStation.getRandomIdTag(); |
5cf9050d JB |
295 | const startTransactionLogMsg = `${this.logPrefix( |
296 | connectorId | |
ba7965c4 | 297 | )} start transaction with an idTag '${idTag}'`; |
ccb1d6e9 | 298 | if (this.getRequireAuthorize()) { |
2e3d65ae | 299 | this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag; |
f4bf2abd | 300 | // Authorize idTag |
2e3d65ae | 301 | const authorizeResponse: AuthorizeResponse = |
f7f98c68 | 302 | await this.chargingStation.ocppRequestService.requestHandler< |
ef6fa3fb JB |
303 | AuthorizeRequest, |
304 | AuthorizeResponse | |
08f130a0 | 305 | >(this.chargingStation, RequestCommand.AUTHORIZE, { |
ef6fa3fb JB |
306 | idTag, |
307 | }); | |
071a9315 | 308 | this.connectorsStatus.get(connectorId).authorizeRequests++; |
5fdab605 | 309 | if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
071a9315 | 310 | this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++; |
5cf9050d | 311 | logger.info(startTransactionLogMsg); |
5fdab605 | 312 | // Start transaction |
f7f98c68 | 313 | startResponse = await this.chargingStation.ocppRequestService.requestHandler< |
ef6fa3fb JB |
314 | StartTransactionRequest, |
315 | StartTransactionResponse | |
08f130a0 | 316 | >(this.chargingStation, RequestCommand.START_TRANSACTION, { |
ef6fa3fb JB |
317 | connectorId, |
318 | idTag, | |
319 | }); | |
d9ac47ef | 320 | this.handleStartTransactionResponse(connectorId, startResponse); |
aef1b33a JB |
321 | PerformanceStatistics.endMeasure(measureId, beginId); |
322 | return startResponse; | |
5fdab605 | 323 | } |
071a9315 | 324 | this.connectorsStatus.get(connectorId).rejectedAuthorizeRequests++; |
aef1b33a | 325 | PerformanceStatistics.endMeasure(measureId, beginId); |
0afed85f | 326 | return startResponse; |
ef6076c1 | 327 | } |
5cf9050d | 328 | logger.info(startTransactionLogMsg); |
5fdab605 | 329 | // Start transaction |
f7f98c68 | 330 | startResponse = await this.chargingStation.ocppRequestService.requestHandler< |
ef6fa3fb JB |
331 | StartTransactionRequest, |
332 | StartTransactionResponse | |
08f130a0 | 333 | >(this.chargingStation, RequestCommand.START_TRANSACTION, { |
ef6fa3fb JB |
334 | connectorId, |
335 | idTag, | |
336 | }); | |
d9ac47ef | 337 | this.handleStartTransactionResponse(connectorId, startResponse); |
aef1b33a JB |
338 | PerformanceStatistics.endMeasure(measureId, beginId); |
339 | return startResponse; | |
6af9012e | 340 | } |
5cf9050d | 341 | logger.info(`${this.logPrefix(connectorId)} start transaction without an idTag`); |
f7f98c68 | 342 | startResponse = await this.chargingStation.ocppRequestService.requestHandler< |
ef6fa3fb JB |
343 | StartTransactionRequest, |
344 | StartTransactionResponse | |
08f130a0 | 345 | >(this.chargingStation, RequestCommand.START_TRANSACTION, { connectorId }); |
431b6bd5 | 346 | this.handleStartTransactionResponse(connectorId, startResponse); |
aef1b33a JB |
347 | PerformanceStatistics.endMeasure(measureId, beginId); |
348 | return startResponse; | |
6af9012e JB |
349 | } |
350 | ||
e7aeea18 JB |
351 | private async stopTransaction( |
352 | connectorId: number, | |
5e3cb728 | 353 | reason: StopTransactionReason = StopTransactionReason.LOCAL |
e7aeea18 | 354 | ): Promise<StopTransactionResponse> { |
aef1b33a JB |
355 | const measureId = 'StopTransaction with ATG'; |
356 | const beginId = PerformanceStatistics.beginMeasure(measureId); | |
0045cef5 | 357 | let stopResponse: StopTransactionResponse; |
6d9876e7 | 358 | if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { |
5e3cb728 | 359 | stopResponse = await this.chargingStation.stopTransactionOnConnector(connectorId, reason); |
071a9315 | 360 | this.connectorsStatus.get(connectorId).stopTransactionRequests++; |
0afed85f | 361 | if (stopResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
6d9876e7 JB |
362 | this.connectorsStatus.get(connectorId).acceptedStopTransactionRequests++; |
363 | } else { | |
364 | this.connectorsStatus.get(connectorId).rejectedStopTransactionRequests++; | |
365 | } | |
0045cef5 | 366 | } else { |
5e3cb728 | 367 | const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId; |
e7aeea18 | 368 | logger.warn( |
ba7965c4 | 369 | `${this.logPrefix(connectorId)} stopping a not started transaction${ |
e7aeea18 JB |
370 | transactionId ? ' ' + transactionId.toString() : '' |
371 | }` | |
372 | ); | |
0045cef5 | 373 | } |
aef1b33a JB |
374 | PerformanceStatistics.endMeasure(measureId, beginId); |
375 | return stopResponse; | |
c0560973 JB |
376 | } |
377 | ||
ccb1d6e9 | 378 | private getRequireAuthorize(): boolean { |
fa7bccf4 | 379 | return this.configuration?.requireAuthorize ?? true; |
ccb1d6e9 JB |
380 | } |
381 | ||
6e0964c8 | 382 | private logPrefix(connectorId?: number): string { |
6cd85def JB |
383 | return Utils.logPrefix( |
384 | ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${ | |
a5e9befc | 385 | connectorId !== undefined ? ` on connector #${connectorId.toString()}` : '' |
6cd85def JB |
386 | }:` |
387 | ); | |
6af9012e | 388 | } |
d9ac47ef JB |
389 | |
390 | private handleStartTransactionResponse( | |
391 | connectorId: number, | |
392 | startResponse: StartTransactionResponse | |
393 | ): void { | |
394 | this.connectorsStatus.get(connectorId).startTransactionRequests++; | |
395 | if (startResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { | |
396 | this.connectorsStatus.get(connectorId).acceptedStartTransactionRequests++; | |
397 | } else { | |
398 | logger.warn(this.logPrefix(connectorId) + ' start transaction rejected'); | |
399 | this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; | |
400 | } | |
401 | } | |
6af9012e | 402 | } |