Fix build
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
CommitLineData
edd13439 1// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
b4d34251 2
8114d10e 3import fs from 'fs';
43ee4373 4import crypto from 'node:crypto';
8114d10e 5import path from 'path';
ee5f26a2 6import { URL } from 'url';
8114d10e
JB
7import { parentPort } from 'worker_threads';
8
5d280aae 9import merge from 'just-merge';
ef7d8c21 10import WebSocket, { type RawData } from 'ws';
8114d10e 11
78202038
JB
12import AuthorizedTagsCache from './AuthorizedTagsCache';
13import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
14import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
15import { ChargingStationUtils } from './ChargingStationUtils';
16import ChargingStationWorkerBroadcastChannel from './ChargingStationWorkerBroadcastChannel';
17import { MessageChannelUtils } from './MessageChannelUtils';
18import OCPP16IncomingRequestService from './ocpp/1.6/OCPP16IncomingRequestService';
19import OCPP16RequestService from './ocpp/1.6/OCPP16RequestService';
20import OCPP16ResponseService from './ocpp/1.6/OCPP16ResponseService';
21import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
22import OCPP20IncomingRequestService from './ocpp/2.0/OCPP20IncomingRequestService';
23import OCPP20RequestService from './ocpp/2.0/OCPP20RequestService';
24import OCPP20ResponseService from './ocpp/2.0/OCPP20ResponseService';
25import type OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService';
26import type OCPPRequestService from './ocpp/OCPPRequestService';
27import { OCPPServiceUtils } from './ocpp/OCPPServiceUtils';
28import SharedLRUCache from './SharedLRUCache';
8114d10e
JB
29import BaseError from '../exception/BaseError';
30import OCPPError from '../exception/OCPPError';
31import PerformanceStatistics from '../performance/PerformanceStatistics';
6c1761d4 32import type { AutomaticTransactionGeneratorConfiguration } from '../types/AutomaticTransactionGenerator';
981ebfbe
JB
33import type { ChargingStationConfiguration } from '../types/ChargingStationConfiguration';
34import type { ChargingStationInfo } from '../types/ChargingStationInfo';
83e00df1 35import type { ChargingStationOcppConfiguration } from '../types/ChargingStationOcppConfiguration';
981ebfbe
JB
36import {
37 type ChargingStationTemplate,
8114d10e
JB
38 CurrentType,
39 PowerUnits,
981ebfbe 40 type WsOptions,
8114d10e 41} from '../types/ChargingStationTemplate';
8114d10e 42import { SupervisionUrlDistribution } from '../types/ConfigurationData';
6c1761d4 43import type { ConnectorStatus } from '../types/ConnectorStatus';
8114d10e 44import { FileType } from '../types/FileType';
6c1761d4 45import type { JsonType } from '../types/JsonType';
8114d10e
JB
46import {
47 ConnectorPhaseRotation,
48 StandardParametersKey,
49 SupportedFeatureProfiles,
50 VendorDefaultParametersKey,
51} from '../types/ocpp/Configuration';
6e939d9e 52import { ConnectorStatusEnum } from '../types/ocpp/ConnectorStatusEnum';
8114d10e
JB
53import { ErrorType } from '../types/ocpp/ErrorType';
54import { MessageType } from '../types/ocpp/MessageType';
55import { MeterValue, MeterValueMeasurand } from '../types/ocpp/MeterValues';
56import { OCPPVersion } from '../types/ocpp/OCPPVersion';
e7aeea18
JB
57import {
58 AvailabilityType,
e0b0ee21
JB
59 type BootNotificationRequest,
60 type CachedRequest,
61 type ErrorCallback,
c9a4f9ea
JB
62 FirmwareStatus,
63 type FirmwareStatusNotificationRequest,
e0b0ee21
JB
64 type HeartbeatRequest,
65 type IncomingRequest,
e7aeea18 66 IncomingRequestCommand,
e0b0ee21 67 type MeterValuesRequest,
8ca6874c 68 type OutgoingRequest,
e7aeea18 69 RequestCommand,
e0b0ee21
JB
70 type ResponseCallback,
71 type StatusNotificationRequest,
e7aeea18 72} from '../types/ocpp/Requests';
f22266fd 73import {
e0b0ee21
JB
74 type BootNotificationResponse,
75 type ErrorResponse,
c9a4f9ea 76 type FirmwareStatusNotificationResponse,
e0b0ee21
JB
77 type HeartbeatResponse,
78 type MeterValuesResponse,
d270cc87 79 RegistrationStatusEnumType,
e0b0ee21
JB
80 type Response,
81 type StatusNotificationResponse,
f22266fd 82} from '../types/ocpp/Responses';
ef6fa3fb
JB
83import {
84 StopTransactionReason,
e0b0ee21
JB
85 type StopTransactionRequest,
86 type StopTransactionResponse,
ef6fa3fb 87} from '../types/ocpp/Transaction';
16b0d4e7 88import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
8114d10e
JB
89import Configuration from '../utils/Configuration';
90import Constants from '../utils/Constants';
91import { ACElectricUtils, DCElectricUtils } from '../utils/ElectricUtils';
92import FileUtils from '../utils/FileUtils';
93import logger from '../utils/Logger';
94import Utils from '../utils/Utils';
3f40bc9c
JB
95
96export default class ChargingStation {
c72f6634 97 public readonly index: number;
2484ac1e 98 public readonly templateFile: string;
6e0964c8 99 public stationInfo!: ChargingStationInfo;
452a82ca 100 public started: boolean;
cbf9b878 101 public starting: boolean;
a5e9befc
JB
102 public authorizedTagsCache: AuthorizedTagsCache;
103 public automaticTransactionGenerator!: AutomaticTransactionGenerator;
72092cfc
JB
104 public ocppConfiguration!: ChargingStationOcppConfiguration | null;
105 public wsConnection!: WebSocket | null;
a5e9befc 106 public readonly connectors: Map<number, ConnectorStatus>;
9e23580d 107 public readonly requests: Map<string, CachedRequest>;
6e0964c8
JB
108 public performanceStatistics!: PerformanceStatistics;
109 public heartbeatSetInterval!: NodeJS.Timeout;
6e0964c8 110 public ocppRequestService!: OCPPRequestService;
0a03f36c 111 public bootNotificationRequest!: BootNotificationRequest;
1895299d 112 public bootNotificationResponse!: BootNotificationResponse | undefined;
fa7bccf4 113 public powerDivider!: number;
950b1349 114 private stopping: boolean;
073bd098 115 private configurationFile!: string;
7c72977b 116 private configurationFileHash!: string;
6e0964c8 117 private connectorsConfigurationHash!: string;
a472cf2b 118 private ocppIncomingRequestService!: OCPPIncomingRequestService;
8e242273 119 private readonly messageBuffer: Set<string>;
fa7bccf4 120 private configuredSupervisionUrl!: URL;
c72f6634 121 private configuredSupervisionUrlIndex!: number;
265e4266 122 private wsConnectionRestarted: boolean;
ad2f27c3 123 private autoReconnectRetryCount: number;
72092cfc 124 private templateFileWatcher!: fs.FSWatcher | undefined;
57adbebc 125 private readonly sharedLRUCache: SharedLRUCache;
6e0964c8 126 private webSocketPingSetInterval!: NodeJS.Timeout;
89b7a234 127 private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel;
6af9012e 128
2484ac1e 129 constructor(index: number, templateFile: string) {
aa428a31 130 this.started = false;
950b1349
JB
131 this.starting = false;
132 this.stopping = false;
aa428a31
JB
133 this.wsConnectionRestarted = false;
134 this.autoReconnectRetryCount = 0;
ad2f27c3 135 this.index = index;
2484ac1e 136 this.templateFile = templateFile;
9f2e3130 137 this.connectors = new Map<number, ConnectorStatus>();
32b02249 138 this.requests = new Map<string, CachedRequest>();
8e242273 139 this.messageBuffer = new Set<string>();
b44b779a
JB
140 this.sharedLRUCache = SharedLRUCache.getInstance();
141 this.authorizedTagsCache = AuthorizedTagsCache.getInstance();
89b7a234 142 this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this);
32de5a57 143
9f2e3130 144 this.initialize();
c0560973
JB
145 }
146
25f5a959 147 private get wsConnectionUrl(): URL {
fa7bccf4 148 return new URL(
44eb6026
JB
149 `${
150 this.getSupervisionUrlOcppConfiguration()
151 ? ChargingStationConfigurationUtils.getConfigurationKey(
152 this,
153 this.getSupervisionUrlOcppKey()
72092cfc 154 )?.value
44eb6026
JB
155 : this.configuredSupervisionUrl.href
156 }/${this.stationInfo.chargingStationId}`
fa7bccf4 157 );
12fc74d6
JB
158 }
159
c0560973 160 public logPrefix(): string {
ccb1d6e9
JB
161 return Utils.logPrefix(
162 ` ${
163 this?.stationInfo?.chargingStationId ??
164 ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())
165 } |`
166 );
c0560973
JB
167 }
168
c0560973 169 public hasAuthorizedTags(): boolean {
9d7484a4
JB
170 return !Utils.isEmptyArray(
171 this.authorizedTagsCache.getAuthorizedTags(
172 ChargingStationUtils.getAuthorizationFile(this.stationInfo)
173 )
174 );
c0560973
JB
175 }
176
ad774cec
JB
177 public getEnableStatistics(): boolean {
178 return this.stationInfo.enableStatistics ?? false;
c0560973
JB
179 }
180
ad774cec 181 public getMustAuthorizeAtRemoteStart(): boolean {
03ebf4c1 182 return this.stationInfo.mustAuthorizeAtRemoteStart ?? true;
a7fc8211
JB
183 }
184
ad774cec 185 public getPayloadSchemaValidation(): boolean {
e3018bc4
JB
186 return this.stationInfo.payloadSchemaValidation ?? true;
187 }
188
fa7bccf4
JB
189 public getNumberOfPhases(stationInfo?: ChargingStationInfo): number | undefined {
190 const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo;
191 switch (this.getCurrentOutType(stationInfo)) {
4c2b4904 192 case CurrentType.AC:
fa7bccf4
JB
193 return !Utils.isUndefined(localStationInfo.numberOfPhases)
194 ? localStationInfo.numberOfPhases
e7aeea18 195 : 3;
4c2b4904 196 case CurrentType.DC:
c0560973
JB
197 return 0;
198 }
199 }
200
d5bff457 201 public isWebSocketConnectionOpened(): boolean {
0d8140bd 202 return this?.wsConnection?.readyState === WebSocket.OPEN;
c0560973
JB
203 }
204
1895299d 205 public getRegistrationStatus(): RegistrationStatusEnumType | undefined {
672fed6e
JB
206 return this?.bootNotificationResponse?.status;
207 }
208
73c4266d
JB
209 public isInUnknownState(): boolean {
210 return Utils.isNullOrUndefined(this?.bootNotificationResponse?.status);
211 }
212
16cd35ad 213 public isInPendingState(): boolean {
d270cc87 214 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING;
16cd35ad
JB
215 }
216
217 public isInAcceptedState(): boolean {
d270cc87 218 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED;
c0560973
JB
219 }
220
16cd35ad 221 public isInRejectedState(): boolean {
d270cc87 222 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED;
16cd35ad
JB
223 }
224
225 public isRegistered(): boolean {
ed6cfcff
JB
226 return (
227 this.isInUnknownState() === false &&
228 (this.isInAcceptedState() === true || this.isInPendingState() === true)
229 );
16cd35ad
JB
230 }
231
c0560973 232 public isChargingStationAvailable(): boolean {
1895299d 233 return this.getConnectorStatus(0)?.availability === AvailabilityType.OPERATIVE;
c0560973
JB
234 }
235
236 public isConnectorAvailable(id: number): boolean {
1895299d 237 return id > 0 && this.getConnectorStatus(id)?.availability === AvailabilityType.OPERATIVE;
c0560973
JB
238 }
239
54544ef1
JB
240 public getNumberOfConnectors(): number {
241 return this.connectors.get(0) ? this.connectors.size - 1 : this.connectors.size;
242 }
243
6d9876e7 244 public getConnectorStatus(id: number): ConnectorStatus | undefined {
734d790d 245 return this.connectors.get(id);
c0560973
JB
246 }
247
fa7bccf4 248 public getCurrentOutType(stationInfo?: ChargingStationInfo): CurrentType {
72092cfc 249 return (stationInfo ?? this.stationInfo)?.currentOutType ?? CurrentType.AC;
c0560973
JB
250 }
251
672fed6e 252 public getOcppStrictCompliance(): boolean {
ccb1d6e9 253 return this.stationInfo?.ocppStrictCompliance ?? false;
672fed6e
JB
254 }
255
fa7bccf4 256 public getVoltageOut(stationInfo?: ChargingStationInfo): number | undefined {
492cf6ab 257 const defaultVoltageOut = ChargingStationUtils.getDefaultVoltageOut(
fa7bccf4 258 this.getCurrentOutType(stationInfo),
492cf6ab
JB
259 this.templateFile,
260 this.logPrefix()
261 );
fa7bccf4
JB
262 const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo;
263 return !Utils.isUndefined(localStationInfo.voltageOut)
264 ? localStationInfo.voltageOut
e7aeea18 265 : defaultVoltageOut;
c0560973
JB
266 }
267
15068be9
JB
268 public getMaximumPower(stationInfo?: ChargingStationInfo): number {
269 const localStationInfo = stationInfo ?? this.stationInfo;
270 return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower;
271 }
272
ad8537a7 273 public getConnectorMaximumAvailablePower(connectorId: number): number {
d20f43b5 274 let connectorAmperageLimitationPowerLimit: number;
b47d68d7
JB
275 if (
276 !Utils.isNullOrUndefined(this.getAmperageLimitation()) &&
72092cfc 277 this.getAmperageLimitation() < this.stationInfo?.maximumAmperage
b47d68d7 278 ) {
4160ae28
JB
279 connectorAmperageLimitationPowerLimit =
280 (this.getCurrentOutType() === CurrentType.AC
cc6e8ab5
JB
281 ? ACElectricUtils.powerTotal(
282 this.getNumberOfPhases(),
283 this.getVoltageOut(),
da57964c 284 this.getAmperageLimitation() * this.getNumberOfConnectors()
cc6e8ab5 285 )
4160ae28 286 : DCElectricUtils.power(this.getVoltageOut(), this.getAmperageLimitation())) /
fa7bccf4 287 this.powerDivider;
cc6e8ab5 288 }
fa7bccf4 289 const connectorMaximumPower = this.getMaximumPower() / this.powerDivider;
15068be9
JB
290 const connectorChargingProfilesPowerLimit =
291 ChargingStationUtils.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId);
ad8537a7
JB
292 return Math.min(
293 isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower,
294 isNaN(connectorAmperageLimitationPowerLimit)
295 ? Infinity
296 : connectorAmperageLimitationPowerLimit,
15068be9 297 isNaN(connectorChargingProfilesPowerLimit) ? Infinity : connectorChargingProfilesPowerLimit
ad8537a7 298 );
cc6e8ab5
JB
299 }
300
6e0964c8 301 public getTransactionIdTag(transactionId: number): string | undefined {
734d790d 302 for (const connectorId of this.connectors.keys()) {
1895299d
JB
303 if (
304 connectorId > 0 &&
305 this.getConnectorStatus(connectorId)?.transactionId === transactionId
306 ) {
72092cfc 307 return this.getConnectorStatus(connectorId)?.transactionIdTag;
c0560973
JB
308 }
309 }
310 }
311
6ed92bc1 312 public getOutOfOrderEndMeterValues(): boolean {
ccb1d6e9 313 return this.stationInfo?.outOfOrderEndMeterValues ?? false;
6ed92bc1
JB
314 }
315
316 public getBeginEndMeterValues(): boolean {
ccb1d6e9 317 return this.stationInfo?.beginEndMeterValues ?? false;
6ed92bc1
JB
318 }
319
320 public getMeteringPerTransaction(): boolean {
ccb1d6e9 321 return this.stationInfo?.meteringPerTransaction ?? true;
6ed92bc1
JB
322 }
323
fd0c36fa 324 public getTransactionDataMeterValues(): boolean {
ccb1d6e9 325 return this.stationInfo?.transactionDataMeterValues ?? false;
fd0c36fa
JB
326 }
327
9ccca265 328 public getMainVoltageMeterValues(): boolean {
ccb1d6e9 329 return this.stationInfo?.mainVoltageMeterValues ?? true;
9ccca265
JB
330 }
331
6b10669b 332 public getPhaseLineToLineVoltageMeterValues(): boolean {
ccb1d6e9 333 return this.stationInfo?.phaseLineToLineVoltageMeterValues ?? false;
9bd87386
JB
334 }
335
7bc31f9c 336 public getCustomValueLimitationMeterValues(): boolean {
ccb1d6e9 337 return this.stationInfo?.customValueLimitationMeterValues ?? true;
7bc31f9c
JB
338 }
339
f479a792 340 public getConnectorIdByTransactionId(transactionId: number): number | undefined {
734d790d 341 for (const connectorId of this.connectors.keys()) {
f479a792
JB
342 if (
343 connectorId > 0 &&
344 this.getConnectorStatus(connectorId)?.transactionId === transactionId
345 ) {
346 return connectorId;
c0560973
JB
347 }
348 }
349 }
350
07989fad
JB
351 public getEnergyActiveImportRegisterByTransactionId(
352 transactionId: number,
18bf8274 353 rounded = false
07989fad
JB
354 ): number {
355 return this.getEnergyActiveImportRegister(
356 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)),
18bf8274 357 rounded
cbad1217 358 );
cbad1217
JB
359 }
360
18bf8274
JB
361 public getEnergyActiveImportRegisterByConnectorId(connectorId: number, rounded = false): number {
362 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId), rounded);
6ed92bc1
JB
363 }
364
c0560973 365 public getAuthorizeRemoteTxRequests(): boolean {
17ac262c
JB
366 const authorizeRemoteTxRequests = ChargingStationConfigurationUtils.getConfigurationKey(
367 this,
e7aeea18
JB
368 StandardParametersKey.AuthorizeRemoteTxRequests
369 );
370 return authorizeRemoteTxRequests
371 ? Utils.convertToBoolean(authorizeRemoteTxRequests.value)
372 : false;
c0560973
JB
373 }
374
375 public getLocalAuthListEnabled(): boolean {
17ac262c
JB
376 const localAuthListEnabled = ChargingStationConfigurationUtils.getConfigurationKey(
377 this,
e7aeea18
JB
378 StandardParametersKey.LocalAuthListEnabled
379 );
c0560973
JB
380 return localAuthListEnabled ? Utils.convertToBoolean(localAuthListEnabled.value) : false;
381 }
382
c0560973 383 public startHeartbeat(): void {
e7aeea18
JB
384 if (
385 this.getHeartbeatInterval() &&
386 this.getHeartbeatInterval() > 0 &&
387 !this.heartbeatSetInterval
388 ) {
6a8329b4
JB
389 this.heartbeatSetInterval = setInterval(() => {
390 this.ocppRequestService
391 .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
72092cfc 392 .catch((error) => {
6a8329b4
JB
393 logger.error(
394 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
395 error
396 );
397 });
c0560973 398 }, this.getHeartbeatInterval());
e7aeea18 399 logger.info(
44eb6026
JB
400 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
401 this.getHeartbeatInterval()
402 )}`
e7aeea18 403 );
c0560973 404 } else if (this.heartbeatSetInterval) {
e7aeea18 405 logger.info(
44eb6026
JB
406 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
407 this.getHeartbeatInterval()
408 )}`
e7aeea18 409 );
c0560973 410 } else {
e7aeea18
JB
411 logger.error(
412 `${this.logPrefix()} Heartbeat interval set to ${
413 this.getHeartbeatInterval()
414 ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
415 : this.getHeartbeatInterval()
416 }, not starting the heartbeat`
417 );
c0560973
JB
418 }
419 }
420
421 public restartHeartbeat(): void {
422 // Stop heartbeat
423 this.stopHeartbeat();
424 // Start heartbeat
425 this.startHeartbeat();
426 }
427
17ac262c
JB
428 public restartWebSocketPing(): void {
429 // Stop WebSocket ping
430 this.stopWebSocketPing();
431 // Start WebSocket ping
432 this.startWebSocketPing();
433 }
434
c0560973
JB
435 public startMeterValues(connectorId: number, interval: number): void {
436 if (connectorId === 0) {
e7aeea18
JB
437 logger.error(
438 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`
439 );
c0560973
JB
440 return;
441 }
734d790d 442 if (!this.getConnectorStatus(connectorId)) {
e7aeea18
JB
443 logger.error(
444 `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`
445 );
c0560973
JB
446 return;
447 }
5e3cb728 448 if (this.getConnectorStatus(connectorId)?.transactionStarted === false) {
e7aeea18
JB
449 logger.error(
450 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
451 );
c0560973 452 return;
e7aeea18 453 } else if (
5e3cb728 454 this.getConnectorStatus(connectorId)?.transactionStarted === true &&
e7aeea18
JB
455 !this.getConnectorStatus(connectorId)?.transactionId
456 ) {
457 logger.error(
458 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`
459 );
c0560973
JB
460 return;
461 }
462 if (interval > 0) {
6a8329b4
JB
463 this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(() => {
464 // FIXME: Implement OCPP version agnostic helpers
465 const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue(
466 this,
467 connectorId,
468 this.getConnectorStatus(connectorId).transactionId,
469 interval
470 );
471 this.ocppRequestService
472 .requestHandler<MeterValuesRequest, MeterValuesResponse>(
08f130a0 473 this,
f22266fd
JB
474 RequestCommand.METER_VALUES,
475 {
476 connectorId,
72092cfc 477 transactionId: this.getConnectorStatus(connectorId)?.transactionId,
f22266fd
JB
478 meterValue: [meterValue],
479 }
6a8329b4 480 )
72092cfc 481 .catch((error) => {
6a8329b4
JB
482 logger.error(
483 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
484 error
485 );
486 });
487 }, interval);
c0560973 488 } else {
e7aeea18
JB
489 logger.error(
490 `${this.logPrefix()} Charging station ${
491 StandardParametersKey.MeterValueSampleInterval
492 } configuration set to ${
493 interval ? Utils.formatDurationMilliSeconds(interval) : interval
494 }, not sending MeterValues`
495 );
c0560973
JB
496 }
497 }
498
499 public start(): void {
0d8852a5
JB
500 if (this.started === false) {
501 if (this.starting === false) {
502 this.starting = true;
ad774cec 503 if (this.getEnableStatistics() === true) {
0d8852a5
JB
504 this.performanceStatistics.start();
505 }
506 this.openWSConnection();
507 // Monitor charging station template file
508 this.templateFileWatcher = FileUtils.watchJsonFile(
509 this.logPrefix(),
510 FileType.ChargingStationTemplate,
511 this.templateFile,
512 null,
513 (event, filename): void => {
514 if (filename && event === 'change') {
515 try {
516 logger.debug(
517 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
518 this.templateFile
519 } file have changed, reload`
520 );
521 this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
522 // Initialize
523 this.initialize();
524 // Restart the ATG
525 this.stopAutomaticTransactionGenerator();
526 if (
527 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable === true
528 ) {
529 this.startAutomaticTransactionGenerator();
530 }
ad774cec 531 if (this.getEnableStatistics() === true) {
0d8852a5
JB
532 this.performanceStatistics.restart();
533 } else {
534 this.performanceStatistics.stop();
535 }
536 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
537 } catch (error) {
538 logger.error(
539 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
540 error
541 );
950b1349 542 }
a95873d8 543 }
a95873d8 544 }
0d8852a5 545 );
56eb297e 546 this.started = true;
1895299d 547 parentPort?.postMessage(MessageChannelUtils.buildStartedMessage(this));
0d8852a5
JB
548 this.starting = false;
549 } else {
550 logger.warn(`${this.logPrefix()} Charging station is already starting...`);
551 }
950b1349 552 } else {
0d8852a5 553 logger.warn(`${this.logPrefix()} Charging station is already started...`);
950b1349 554 }
c0560973
JB
555 }
556
60ddad53 557 public async stop(reason?: StopTransactionReason): Promise<void> {
0d8852a5
JB
558 if (this.started === true) {
559 if (this.stopping === false) {
560 this.stopping = true;
561 await this.stopMessageSequence(reason);
0d8852a5 562 this.closeWSConnection();
ad774cec 563 if (this.getEnableStatistics() === true) {
0d8852a5
JB
564 this.performanceStatistics.stop();
565 }
566 this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
72092cfc 567 this.templateFileWatcher?.close();
0d8852a5 568 this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
1895299d 569 this.bootNotificationResponse = undefined;
0d8852a5 570 this.started = false;
1895299d 571 parentPort?.postMessage(MessageChannelUtils.buildStoppedMessage(this));
0d8852a5
JB
572 this.stopping = false;
573 } else {
574 logger.warn(`${this.logPrefix()} Charging station is already stopping...`);
c0560973 575 }
950b1349 576 } else {
0d8852a5 577 logger.warn(`${this.logPrefix()} Charging station is already stopped...`);
c0560973 578 }
c0560973
JB
579 }
580
60ddad53
JB
581 public async reset(reason?: StopTransactionReason): Promise<void> {
582 await this.stop(reason);
94ec7e96 583 await Utils.sleep(this.stationInfo.resetTime);
fa7bccf4 584 this.initialize();
94ec7e96
JB
585 this.start();
586 }
587
17ac262c
JB
588 public saveOcppConfiguration(): void {
589 if (this.getOcppPersistentConfiguration()) {
7c72977b 590 this.saveConfiguration();
e6895390
JB
591 }
592 }
593
a2653482
JB
594 public resetConnectorStatus(connectorId: number): void {
595 this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
596 this.getConnectorStatus(connectorId).idTagAuthorized = false;
597 this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
734d790d 598 this.getConnectorStatus(connectorId).transactionStarted = false;
a2653482 599 delete this.getConnectorStatus(connectorId).localAuthorizeIdTag;
734d790d
JB
600 delete this.getConnectorStatus(connectorId).authorizeIdTag;
601 delete this.getConnectorStatus(connectorId).transactionId;
602 delete this.getConnectorStatus(connectorId).transactionIdTag;
603 this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
604 delete this.getConnectorStatus(connectorId).transactionBeginMeterValue;
dd119a6b 605 this.stopMeterValues(connectorId);
1895299d 606 parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
2e6f5966
JB
607 }
608
72092cfc 609 public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean | undefined {
17ac262c
JB
610 return ChargingStationConfigurationUtils.getConfigurationKey(
611 this,
612 StandardParametersKey.SupportedFeatureProfiles
72092cfc 613 )?.value?.includes(featureProfile);
68cb8b91
JB
614 }
615
8e242273
JB
616 public bufferMessage(message: string): void {
617 this.messageBuffer.add(message);
3ba2381e
JB
618 }
619
db2336d9
JB
620 public openWSConnection(
621 options: WsOptions = this.stationInfo?.wsOptions ?? {},
622 params: { closeOpened?: boolean; terminateOpened?: boolean } = {
623 closeOpened: false,
624 terminateOpened: false,
625 }
626 ): void {
627 options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
628 params.closeOpened = params?.closeOpened ?? false;
629 params.terminateOpened = params?.terminateOpened ?? false;
cbf9b878 630 if (this.started === false && this.starting === false) {
d1c6c833
JB
631 logger.warn(
632 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
633 );
634 return;
635 }
db2336d9
JB
636 if (
637 !Utils.isNullOrUndefined(this.stationInfo.supervisionUser) &&
638 !Utils.isNullOrUndefined(this.stationInfo.supervisionPassword)
639 ) {
640 options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
641 }
642 if (params?.closeOpened) {
643 this.closeWSConnection();
644 }
645 if (params?.terminateOpened) {
646 this.terminateWSConnection();
647 }
cda96260 648 const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
db2336d9 649 let protocol: string;
edd13439 650 switch (ocppVersion) {
db2336d9 651 case OCPPVersion.VERSION_16:
edd13439
JB
652 case OCPPVersion.VERSION_20:
653 case OCPPVersion.VERSION_201:
44eb6026 654 protocol = `ocpp${ocppVersion}`;
db2336d9
JB
655 break;
656 default:
edd13439 657 this.handleUnsupportedVersion(ocppVersion);
db2336d9
JB
658 break;
659 }
660
56eb297e 661 if (this.isWebSocketConnectionOpened() === true) {
0a03f36c
JB
662 logger.warn(
663 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
664 );
665 return;
666 }
667
db2336d9 668 logger.info(
0a03f36c 669 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
db2336d9
JB
670 );
671
672 this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options);
673
674 // Handle WebSocket message
675 this.wsConnection.on(
676 'message',
677 this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void
678 );
679 // Handle WebSocket error
680 this.wsConnection.on(
681 'error',
682 this.onError.bind(this) as (this: WebSocket, error: Error) => void
683 );
684 // Handle WebSocket close
685 this.wsConnection.on(
686 'close',
687 this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void
688 );
689 // Handle WebSocket open
690 this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void);
691 // Handle WebSocket ping
692 this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void);
693 // Handle WebSocket pong
694 this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void);
695 }
696
697 public closeWSConnection(): void {
56eb297e 698 if (this.isWebSocketConnectionOpened() === true) {
72092cfc 699 this.wsConnection?.close();
db2336d9
JB
700 this.wsConnection = null;
701 }
702 }
703
8f879946
JB
704 public startAutomaticTransactionGenerator(
705 connectorIds?: number[],
706 automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration
707 ): void {
708 this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(
709 automaticTransactionGeneratorConfiguration ??
4f69be04 710 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
8f879946
JB
711 this
712 );
a5e9befc
JB
713 if (!Utils.isEmptyArray(connectorIds)) {
714 for (const connectorId of connectorIds) {
715 this.automaticTransactionGenerator.startConnector(connectorId);
716 }
717 } else {
4f69be04
JB
718 this.automaticTransactionGenerator.start();
719 }
1895299d 720 parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
4f69be04
JB
721 }
722
a5e9befc
JB
723 public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
724 if (!Utils.isEmptyArray(connectorIds)) {
725 for (const connectorId of connectorIds) {
726 this.automaticTransactionGenerator?.stopConnector(connectorId);
727 }
728 } else {
729 this.automaticTransactionGenerator?.stop();
4f69be04 730 }
1895299d 731 parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
4f69be04
JB
732 }
733
5e3cb728
JB
734 public async stopTransactionOnConnector(
735 connectorId: number,
736 reason = StopTransactionReason.NONE
737 ): Promise<StopTransactionResponse> {
72092cfc 738 const transactionId = this.getConnectorStatus(connectorId)?.transactionId;
5e3cb728 739 if (
c7e8e0a2
JB
740 this.getBeginEndMeterValues() === true &&
741 this.getOcppStrictCompliance() === true &&
742 this.getOutOfOrderEndMeterValues() === false
5e3cb728
JB
743 ) {
744 // FIXME: Implement OCPP version agnostic helpers
745 const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
746 this,
747 connectorId,
748 this.getEnergyActiveImportRegisterByTransactionId(transactionId)
749 );
750 await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
751 this,
752 RequestCommand.METER_VALUES,
753 {
754 connectorId,
755 transactionId,
756 meterValue: [transactionEndMeterValue],
757 }
758 );
759 }
760 return this.ocppRequestService.requestHandler<StopTransactionRequest, StopTransactionResponse>(
761 this,
762 RequestCommand.STOP_TRANSACTION,
763 {
764 transactionId,
765 meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId, true),
5e3cb728
JB
766 reason,
767 }
768 );
769 }
770
f90c1757 771 private flushMessageBuffer(): void {
8e242273 772 if (this.messageBuffer.size > 0) {
72092cfc 773 this.messageBuffer.forEach((message) => {
1431af78
JB
774 let beginId: string;
775 let commandName: RequestCommand;
8ca6874c 776 const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse;
1431af78
JB
777 const isRequest = messageType === MessageType.CALL_MESSAGE;
778 if (isRequest) {
779 [, , commandName] = JSON.parse(message) as OutgoingRequest;
780 beginId = PerformanceStatistics.beginMeasure(commandName);
781 }
72092cfc 782 this.wsConnection?.send(message);
1431af78 783 isRequest && PerformanceStatistics.endMeasure(commandName, beginId);
8ca6874c
JB
784 logger.debug(
785 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
786 messageType
787 )} payload sent: ${message}`
788 );
8e242273 789 this.messageBuffer.delete(message);
77f00f84
JB
790 });
791 }
792 }
793
1f5df42a
JB
794 private getSupervisionUrlOcppConfiguration(): boolean {
795 return this.stationInfo.supervisionUrlOcppConfiguration ?? false;
12fc74d6
JB
796 }
797
e8e865ea
JB
798 private getSupervisionUrlOcppKey(): string {
799 return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
800 }
801
72092cfc
JB
802 private getTemplateFromFile(): ChargingStationTemplate | undefined {
803 let template: ChargingStationTemplate;
5ad8570f 804 try {
57adbebc
JB
805 if (this.sharedLRUCache.hasChargingStationTemplate(this.stationInfo?.templateHash)) {
806 template = this.sharedLRUCache.getChargingStationTemplate(this.stationInfo.templateHash);
7c72977b
JB
807 } else {
808 const measureId = `${FileType.ChargingStationTemplate} read`;
809 const beginId = PerformanceStatistics.beginMeasure(measureId);
810 template = JSON.parse(
811 fs.readFileSync(this.templateFile, 'utf8')
812 ) as ChargingStationTemplate;
813 PerformanceStatistics.endMeasure(measureId, beginId);
814 template.templateHash = crypto
815 .createHash(Constants.DEFAULT_HASH_ALGORITHM)
816 .update(JSON.stringify(template))
817 .digest('hex');
57adbebc 818 this.sharedLRUCache.setChargingStationTemplate(template);
7c72977b 819 }
5ad8570f 820 } catch (error) {
e7aeea18
JB
821 FileUtils.handleFileException(
822 this.logPrefix(),
a95873d8 823 FileType.ChargingStationTemplate,
2484ac1e 824 this.templateFile,
e7aeea18
JB
825 error as NodeJS.ErrnoException
826 );
5ad8570f 827 }
2484ac1e
JB
828 return template;
829 }
830
7a3a2ebb 831 private getStationInfoFromTemplate(): ChargingStationInfo {
72092cfc 832 const stationTemplate: ChargingStationTemplate | undefined = this.getTemplateFromFile();
fa7bccf4 833 if (Utils.isNullOrUndefined(stationTemplate)) {
eaad6e5c 834 const errorMsg = `Failed to read charging station template file ${this.templateFile}`;
ccb1d6e9
JB
835 logger.error(`${this.logPrefix()} ${errorMsg}`);
836 throw new BaseError(errorMsg);
94ec7e96 837 }
fa7bccf4 838 if (Utils.isEmptyObject(stationTemplate)) {
ccb1d6e9
JB
839 const errorMsg = `Empty charging station information from template file ${this.templateFile}`;
840 logger.error(`${this.logPrefix()} ${errorMsg}`);
841 throw new BaseError(errorMsg);
94ec7e96 842 }
2dcfe98e 843 // Deprecation template keys section
17ac262c 844 ChargingStationUtils.warnDeprecatedTemplateKey(
fa7bccf4 845 stationTemplate,
e7aeea18 846 'supervisionUrl',
17ac262c 847 this.templateFile,
ccb1d6e9 848 this.logPrefix(),
e7aeea18
JB
849 "Use 'supervisionUrls' instead"
850 );
17ac262c 851 ChargingStationUtils.convertDeprecatedTemplateKey(
fa7bccf4 852 stationTemplate,
17ac262c
JB
853 'supervisionUrl',
854 'supervisionUrls'
855 );
fa7bccf4
JB
856 const stationInfo: ChargingStationInfo =
857 ChargingStationUtils.stationTemplateToStationInfo(stationTemplate);
51c83d6f 858 stationInfo.hashId = ChargingStationUtils.getHashId(this.index, stationTemplate);
fa7bccf4
JB
859 stationInfo.chargingStationId = ChargingStationUtils.getChargingStationId(
860 this.index,
861 stationTemplate
862 );
72092cfc 863 stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16;
fa7bccf4 864 ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo);
72092cfc
JB
865 if (!Utils.isEmptyArray(stationTemplate?.power)) {
866 stationTemplate.power = stationTemplate?.power as number[];
fa7bccf4 867 const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplate.power.length);
cc6e8ab5 868 stationInfo.maximumPower =
72092cfc 869 stationTemplate?.powerUnit === PowerUnits.KILO_WATT
fa7bccf4
JB
870 ? stationTemplate.power[powerArrayRandomIndex] * 1000
871 : stationTemplate.power[powerArrayRandomIndex];
5ad8570f 872 } else {
fa7bccf4 873 stationTemplate.power = stationTemplate.power as number;
cc6e8ab5 874 stationInfo.maximumPower =
72092cfc 875 stationTemplate?.powerUnit === PowerUnits.KILO_WATT
fa7bccf4
JB
876 ? stationTemplate.power * 1000
877 : stationTemplate.power;
878 }
3637ca2c 879 stationInfo.firmwareVersionPattern =
72092cfc 880 stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN;
3637ca2c
JB
881 if (
882 stationInfo.firmwareVersion &&
883 new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion) === false
884 ) {
885 logger.warn(
886 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
887 this.templateFile
888 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
889 );
890 }
15748260
JB
891 stationInfo.firmwareUpgrade = merge(
892 {
893 reset: true,
894 },
72092cfc 895 stationTemplate?.firmwareUpgrade ?? {}
15748260 896 );
72092cfc 897 stationInfo.resetTime = stationTemplate?.resetTime
fa7bccf4 898 ? stationTemplate.resetTime * 1000
e7aeea18 899 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
c72f6634
JB
900 const configuredMaxConnectors =
901 ChargingStationUtils.getConfiguredNumberOfConnectors(stationTemplate);
fa7bccf4
JB
902 ChargingStationUtils.checkConfiguredMaxConnectors(
903 configuredMaxConnectors,
904 this.templateFile,
fc040c43 905 this.logPrefix()
fa7bccf4
JB
906 );
907 const templateMaxConnectors =
908 ChargingStationUtils.getTemplateMaxNumberOfConnectors(stationTemplate);
909 ChargingStationUtils.checkTemplateMaxConnectors(
910 templateMaxConnectors,
911 this.templateFile,
fc040c43 912 this.logPrefix()
fa7bccf4
JB
913 );
914 if (
915 configuredMaxConnectors >
916 (stationTemplate?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) &&
917 !stationTemplate?.randomConnectors
918 ) {
919 logger.warn(
920 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
921 this.templateFile
922 }, forcing random connector configurations affectation`
923 );
924 stationInfo.randomConnectors = true;
925 }
926 // Build connectors if needed (FIXME: should be factored out)
927 this.initializeConnectors(stationInfo, configuredMaxConnectors, templateMaxConnectors);
928 stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
929 ChargingStationUtils.createStationInfoHash(stationInfo);
9ac86a7e 930 return stationInfo;
5ad8570f
JB
931 }
932
ccb1d6e9 933 private getStationInfoFromFile(): ChargingStationInfo | null {
72092cfc 934 let stationInfo: ChargingStationInfo | null = null;
fa7bccf4
JB
935 this.getStationInfoPersistentConfiguration() &&
936 (stationInfo = this.getConfigurationFromFile()?.stationInfo ?? null);
937 stationInfo && ChargingStationUtils.createStationInfoHash(stationInfo);
f765beaa 938 return stationInfo;
2484ac1e
JB
939 }
940
941 private getStationInfo(): ChargingStationInfo {
942 const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate();
72092cfc 943 const stationInfoFromFile: ChargingStationInfo | null = this.getStationInfoFromFile();
aca53a1a 944 // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
f765beaa 945 if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) {
01efc60a
JB
946 if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) {
947 return this.stationInfo;
948 }
2484ac1e 949 return stationInfoFromFile;
f765beaa 950 }
fec4d204
JB
951 stationInfoFromFile &&
952 ChargingStationUtils.propagateSerialNumber(
953 this.getTemplateFromFile(),
954 stationInfoFromFile,
955 stationInfoFromTemplate
956 );
01efc60a 957 return stationInfoFromTemplate;
2484ac1e
JB
958 }
959
960 private saveStationInfo(): void {
ccb1d6e9 961 if (this.getStationInfoPersistentConfiguration()) {
7c72977b 962 this.saveConfiguration();
ccb1d6e9 963 }
2484ac1e
JB
964 }
965
e8e865ea 966 private getOcppPersistentConfiguration(): boolean {
ccb1d6e9
JB
967 return this.stationInfo?.ocppPersistentConfiguration ?? true;
968 }
969
970 private getStationInfoPersistentConfiguration(): boolean {
971 return this.stationInfo?.stationInfoPersistentConfiguration ?? true;
e8e865ea
JB
972 }
973
c0560973 974 private handleUnsupportedVersion(version: OCPPVersion) {
fc040c43
JB
975 const errMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
976 logger.error(`${this.logPrefix()} ${errMsg}`);
6c8f5d90 977 throw new BaseError(errMsg);
c0560973
JB
978 }
979
2484ac1e 980 private initialize(): void {
fa7bccf4 981 this.configurationFile = path.join(
ee5f26a2 982 path.dirname(this.templateFile.replace('station-templates', 'configurations')),
44eb6026 983 `${ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile())}.json`
0642c3d2 984 );
b44b779a
JB
985 this.stationInfo = this.getStationInfo();
986 this.saveStationInfo();
7a3a2ebb 987 // Avoid duplication of connectors related information in RAM
94ec7e96 988 this.stationInfo?.Connectors && delete this.stationInfo.Connectors;
fa7bccf4 989 this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl();
ad774cec 990 if (this.getEnableStatistics() === true) {
0642c3d2 991 this.performanceStatistics = PerformanceStatistics.getInstance(
51c83d6f 992 this.stationInfo.hashId,
0642c3d2 993 this.stationInfo.chargingStationId,
fa7bccf4 994 this.configuredSupervisionUrl
0642c3d2
JB
995 );
996 }
fa7bccf4
JB
997 this.bootNotificationRequest = ChargingStationUtils.createBootNotificationRequest(
998 this.stationInfo
999 );
fa7bccf4
JB
1000 this.powerDivider = this.getPowerDivider();
1001 // OCPP configuration
1002 this.ocppConfiguration = this.getOcppConfiguration();
1003 this.initializeOcppConfiguration();
cda96260
JB
1004 const ocppVersion = this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
1005 switch (ocppVersion) {
c0560973 1006 case OCPPVersion.VERSION_16:
e7aeea18 1007 this.ocppIncomingRequestService =
08f130a0 1008 OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>();
e7aeea18 1009 this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(
08f130a0 1010 OCPP16ResponseService.getInstance<OCPP16ResponseService>()
e7aeea18 1011 );
c0560973 1012 break;
edd13439
JB
1013 case OCPPVersion.VERSION_20:
1014 case OCPPVersion.VERSION_201:
1015 this.ocppIncomingRequestService =
1016 OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>();
1017 this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>(
1018 OCPP20ResponseService.getInstance<OCPP20ResponseService>()
1019 );
1020 break;
c0560973 1021 default:
cda96260 1022 this.handleUnsupportedVersion(ocppVersion);
c0560973
JB
1023 break;
1024 }
b7f9e41d 1025 if (this.stationInfo?.autoRegister === true) {
47e22477 1026 this.bootNotificationResponse = {
d270cc87 1027 currentTime: new Date(),
47e22477 1028 interval: this.getHeartbeatInterval() / 1000,
d270cc87 1029 status: RegistrationStatusEnumType.ACCEPTED,
47e22477
JB
1030 };
1031 }
3637ca2c
JB
1032 if (
1033 this.stationInfo.firmwareStatus === FirmwareStatus.Installing &&
1034 this.stationInfo.firmwareVersion &&
1035 this.stationInfo.firmwareVersionPattern
1036 ) {
15748260 1037 const versionStep = this.stationInfo.firmwareUpgrade?.versionUpgrade?.step ?? 1;
5d280aae 1038 const patternGroup: number =
15748260 1039 this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ??
5d280aae 1040 this.stationInfo.firmwareVersion.split('.').length;
72092cfc
JB
1041 const match = this.stationInfo?.firmwareVersion
1042 ?.match(new RegExp(this.stationInfo.firmwareVersionPattern))
1043 ?.slice(1, patternGroup + 1);
3637ca2c 1044 const patchLevelIndex = match.length - 1;
5d280aae
JB
1045 match[patchLevelIndex] = (
1046 Utils.convertToInt(match[patchLevelIndex]) + versionStep
1047 ).toString();
72092cfc 1048 this.stationInfo.firmwareVersion = match?.join('.');
3637ca2c 1049 }
147d0e0f
JB
1050 }
1051
2484ac1e 1052 private initializeOcppConfiguration(): void {
17ac262c
JB
1053 if (
1054 !ChargingStationConfigurationUtils.getConfigurationKey(
1055 this,
1056 StandardParametersKey.HeartbeatInterval
1057 )
1058 ) {
1059 ChargingStationConfigurationUtils.addConfigurationKey(
1060 this,
1061 StandardParametersKey.HeartbeatInterval,
1062 '0'
1063 );
f0f65a62 1064 }
17ac262c
JB
1065 if (
1066 !ChargingStationConfigurationUtils.getConfigurationKey(
1067 this,
1068 StandardParametersKey.HeartBeatInterval
1069 )
1070 ) {
1071 ChargingStationConfigurationUtils.addConfigurationKey(
1072 this,
1073 StandardParametersKey.HeartBeatInterval,
1074 '0',
1075 { visible: false }
1076 );
f0f65a62 1077 }
e7aeea18
JB
1078 if (
1079 this.getSupervisionUrlOcppConfiguration() &&
17ac262c 1080 !ChargingStationConfigurationUtils.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
e7aeea18 1081 ) {
17ac262c
JB
1082 ChargingStationConfigurationUtils.addConfigurationKey(
1083 this,
a59737e3 1084 this.getSupervisionUrlOcppKey(),
fa7bccf4 1085 this.configuredSupervisionUrl.href,
e7aeea18
JB
1086 { reboot: true }
1087 );
e6895390
JB
1088 } else if (
1089 !this.getSupervisionUrlOcppConfiguration() &&
17ac262c 1090 ChargingStationConfigurationUtils.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
e6895390 1091 ) {
17ac262c
JB
1092 ChargingStationConfigurationUtils.deleteConfigurationKey(
1093 this,
1094 this.getSupervisionUrlOcppKey(),
1095 { save: false }
1096 );
12fc74d6 1097 }
cc6e8ab5
JB
1098 if (
1099 this.stationInfo.amperageLimitationOcppKey &&
17ac262c
JB
1100 !ChargingStationConfigurationUtils.getConfigurationKey(
1101 this,
1102 this.stationInfo.amperageLimitationOcppKey
1103 )
cc6e8ab5 1104 ) {
17ac262c
JB
1105 ChargingStationConfigurationUtils.addConfigurationKey(
1106 this,
cc6e8ab5 1107 this.stationInfo.amperageLimitationOcppKey,
17ac262c
JB
1108 (
1109 this.stationInfo.maximumAmperage *
1110 ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
1111 ).toString()
cc6e8ab5
JB
1112 );
1113 }
17ac262c
JB
1114 if (
1115 !ChargingStationConfigurationUtils.getConfigurationKey(
1116 this,
1117 StandardParametersKey.SupportedFeatureProfiles
1118 )
1119 ) {
1120 ChargingStationConfigurationUtils.addConfigurationKey(
1121 this,
e7aeea18 1122 StandardParametersKey.SupportedFeatureProfiles,
b22787b4 1123 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
e7aeea18
JB
1124 );
1125 }
17ac262c
JB
1126 ChargingStationConfigurationUtils.addConfigurationKey(
1127 this,
e7aeea18
JB
1128 StandardParametersKey.NumberOfConnectors,
1129 this.getNumberOfConnectors().toString(),
a95873d8
JB
1130 { readonly: true },
1131 { overwrite: true }
e7aeea18 1132 );
17ac262c
JB
1133 if (
1134 !ChargingStationConfigurationUtils.getConfigurationKey(
1135 this,
1136 StandardParametersKey.MeterValuesSampledData
1137 )
1138 ) {
1139 ChargingStationConfigurationUtils.addConfigurationKey(
1140 this,
e7aeea18
JB
1141 StandardParametersKey.MeterValuesSampledData,
1142 MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
1143 );
7abfea5f 1144 }
17ac262c
JB
1145 if (
1146 !ChargingStationConfigurationUtils.getConfigurationKey(
1147 this,
1148 StandardParametersKey.ConnectorPhaseRotation
1149 )
1150 ) {
7e1dc878 1151 const connectorPhaseRotation = [];
734d790d 1152 for (const connectorId of this.connectors.keys()) {
7e1dc878 1153 // AC/DC
734d790d
JB
1154 if (connectorId === 0 && this.getNumberOfPhases() === 0) {
1155 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1156 } else if (connectorId > 0 && this.getNumberOfPhases() === 0) {
1157 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
e7aeea18 1158 // AC
734d790d
JB
1159 } else if (connectorId > 0 && this.getNumberOfPhases() === 1) {
1160 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1161 } else if (connectorId > 0 && this.getNumberOfPhases() === 3) {
1162 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
7e1dc878
JB
1163 }
1164 }
17ac262c
JB
1165 ChargingStationConfigurationUtils.addConfigurationKey(
1166 this,
e7aeea18
JB
1167 StandardParametersKey.ConnectorPhaseRotation,
1168 connectorPhaseRotation.toString()
1169 );
7e1dc878 1170 }
e7aeea18 1171 if (
17ac262c
JB
1172 !ChargingStationConfigurationUtils.getConfigurationKey(
1173 this,
1174 StandardParametersKey.AuthorizeRemoteTxRequests
e7aeea18
JB
1175 )
1176 ) {
17ac262c
JB
1177 ChargingStationConfigurationUtils.addConfigurationKey(
1178 this,
1179 StandardParametersKey.AuthorizeRemoteTxRequests,
1180 'true'
1181 );
36f6a92e 1182 }
17ac262c
JB
1183 if (
1184 !ChargingStationConfigurationUtils.getConfigurationKey(
1185 this,
1186 StandardParametersKey.LocalAuthListEnabled
1187 ) &&
1188 ChargingStationConfigurationUtils.getConfigurationKey(
1189 this,
1190 StandardParametersKey.SupportedFeatureProfiles
72092cfc 1191 )?.value?.includes(SupportedFeatureProfiles.LocalAuthListManagement)
17ac262c
JB
1192 ) {
1193 ChargingStationConfigurationUtils.addConfigurationKey(
1194 this,
1195 StandardParametersKey.LocalAuthListEnabled,
1196 'false'
1197 );
1198 }
1199 if (
1200 !ChargingStationConfigurationUtils.getConfigurationKey(
1201 this,
1202 StandardParametersKey.ConnectionTimeOut
1203 )
1204 ) {
1205 ChargingStationConfigurationUtils.addConfigurationKey(
1206 this,
e7aeea18
JB
1207 StandardParametersKey.ConnectionTimeOut,
1208 Constants.DEFAULT_CONNECTION_TIMEOUT.toString()
1209 );
8bce55bf 1210 }
2484ac1e 1211 this.saveOcppConfiguration();
073bd098
JB
1212 }
1213
3d25cc86
JB
1214 private initializeConnectors(
1215 stationInfo: ChargingStationInfo,
fa7bccf4 1216 configuredMaxConnectors: number,
3d25cc86
JB
1217 templateMaxConnectors: number
1218 ): void {
1219 if (!stationInfo?.Connectors && this.connectors.size === 0) {
fc040c43
JB
1220 const logMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1221 logger.error(`${this.logPrefix()} ${logMsg}`);
3d25cc86
JB
1222 throw new BaseError(logMsg);
1223 }
1224 if (!stationInfo?.Connectors[0]) {
1225 logger.warn(
1226 `${this.logPrefix()} Charging station information from template ${
1227 this.templateFile
1228 } with no connector Id 0 configuration`
1229 );
1230 }
1231 if (stationInfo?.Connectors) {
1232 const connectorsConfigHash = crypto
1233 .createHash(Constants.DEFAULT_HASH_ALGORITHM)
14ecae6a 1234 .update(`${JSON.stringify(stationInfo?.Connectors)}${configuredMaxConnectors.toString()}`)
3d25cc86
JB
1235 .digest('hex');
1236 const connectorsConfigChanged =
1237 this.connectors?.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash;
1238 if (this.connectors?.size === 0 || connectorsConfigChanged) {
1239 connectorsConfigChanged && this.connectors.clear();
1240 this.connectorsConfigurationHash = connectorsConfigHash;
1241 // Add connector Id 0
1242 let lastConnector = '0';
1243 for (lastConnector in stationInfo?.Connectors) {
56eb297e 1244 const connectorStatus = stationInfo?.Connectors[lastConnector];
3d25cc86
JB
1245 const lastConnectorId = Utils.convertToInt(lastConnector);
1246 if (
1247 lastConnectorId === 0 &&
bb83b5ed 1248 this.getUseConnectorId0(stationInfo) === true &&
56eb297e 1249 connectorStatus
3d25cc86 1250 ) {
56eb297e 1251 this.checkStationInfoConnectorStatus(lastConnectorId, connectorStatus);
3d25cc86
JB
1252 this.connectors.set(
1253 lastConnectorId,
56eb297e 1254 Utils.cloneObject<ConnectorStatus>(connectorStatus)
3d25cc86
JB
1255 );
1256 this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.OPERATIVE;
1257 if (Utils.isUndefined(this.getConnectorStatus(lastConnectorId)?.chargingProfiles)) {
1258 this.getConnectorStatus(lastConnectorId).chargingProfiles = [];
1259 }
1260 }
1261 }
1262 // Generate all connectors
1263 if ((stationInfo?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) > 0) {
fa7bccf4 1264 for (let index = 1; index <= configuredMaxConnectors; index++) {
ccb1d6e9 1265 const randConnectorId = stationInfo?.randomConnectors
3d25cc86
JB
1266 ? Utils.getRandomInteger(Utils.convertToInt(lastConnector), 1)
1267 : index;
56eb297e
JB
1268 const connectorStatus = stationInfo?.Connectors[randConnectorId.toString()];
1269 this.checkStationInfoConnectorStatus(randConnectorId, connectorStatus);
1270 this.connectors.set(index, Utils.cloneObject<ConnectorStatus>(connectorStatus));
3d25cc86
JB
1271 this.getConnectorStatus(index).availability = AvailabilityType.OPERATIVE;
1272 if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) {
1273 this.getConnectorStatus(index).chargingProfiles = [];
1274 }
1275 }
1276 }
1277 }
1278 } else {
1279 logger.warn(
1280 `${this.logPrefix()} Charging station information from template ${
1281 this.templateFile
1282 } with no connectors configuration defined, using already defined connectors`
1283 );
1284 }
1285 // Initialize transaction attributes on connectors
1286 for (const connectorId of this.connectors.keys()) {
72092cfc 1287 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
f5b51071
JB
1288 logger.warn(
1289 `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
72092cfc 1290 this.getConnectorStatus(connectorId)?.transactionId
f5b51071
JB
1291 }`
1292 );
1293 }
1984f194
JB
1294 if (
1295 connectorId > 0 &&
72092cfc
JB
1296 (this.getConnectorStatus(connectorId)?.transactionStarted === undefined ||
1297 this.getConnectorStatus(connectorId)?.transactionStarted === null)
1984f194 1298 ) {
3d25cc86
JB
1299 this.initializeConnectorStatus(connectorId);
1300 }
1301 }
1302 }
1303
56eb297e
JB
1304 private checkStationInfoConnectorStatus(
1305 connectorId: number,
1306 connectorStatus: ConnectorStatus
1307 ): void {
1308 if (!Utils.isNullOrUndefined(connectorStatus?.status)) {
1309 logger.warn(
1310 `${this.logPrefix()} Charging station information from template ${
1311 this.templateFile
1312 } with connector ${connectorId} status configuration defined, undefine it`
1313 );
1314 connectorStatus.status = undefined;
1315 }
1316 }
1317
7f7b65ca 1318 private getConfigurationFromFile(): ChargingStationConfiguration | null {
72092cfc 1319 let configuration: ChargingStationConfiguration | null = null;
2484ac1e 1320 if (this.configurationFile && fs.existsSync(this.configurationFile)) {
073bd098 1321 try {
57adbebc
JB
1322 if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) {
1323 configuration = this.sharedLRUCache.getChargingStationConfiguration(
1324 this.configurationFileHash
1325 );
7c72977b
JB
1326 } else {
1327 const measureId = `${FileType.ChargingStationConfiguration} read`;
1328 const beginId = PerformanceStatistics.beginMeasure(measureId);
1329 configuration = JSON.parse(
1330 fs.readFileSync(this.configurationFile, 'utf8')
1331 ) as ChargingStationConfiguration;
1332 PerformanceStatistics.endMeasure(measureId, beginId);
1333 this.configurationFileHash = configuration.configurationHash;
57adbebc 1334 this.sharedLRUCache.setChargingStationConfiguration(configuration);
7c72977b 1335 }
073bd098
JB
1336 } catch (error) {
1337 FileUtils.handleFileException(
1338 this.logPrefix(),
a95873d8 1339 FileType.ChargingStationConfiguration,
073bd098
JB
1340 this.configurationFile,
1341 error as NodeJS.ErrnoException
1342 );
1343 }
1344 }
1345 return configuration;
1346 }
1347
7c72977b 1348 private saveConfiguration(): void {
2484ac1e
JB
1349 if (this.configurationFile) {
1350 try {
2484ac1e
JB
1351 if (!fs.existsSync(path.dirname(this.configurationFile))) {
1352 fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
073bd098 1353 }
ccb1d6e9
JB
1354 const configurationData: ChargingStationConfiguration =
1355 this.getConfigurationFromFile() ?? {};
7c72977b
JB
1356 this.ocppConfiguration?.configurationKey &&
1357 (configurationData.configurationKey = this.ocppConfiguration.configurationKey);
1358 this.stationInfo && (configurationData.stationInfo = this.stationInfo);
1359 delete configurationData.configurationHash;
1360 const configurationHash = crypto
1361 .createHash(Constants.DEFAULT_HASH_ALGORITHM)
1362 .update(JSON.stringify(configurationData))
1363 .digest('hex');
1364 if (this.configurationFileHash !== configurationHash) {
1365 configurationData.configurationHash = configurationHash;
1366 const measureId = `${FileType.ChargingStationConfiguration} write`;
1367 const beginId = PerformanceStatistics.beginMeasure(measureId);
1368 const fileDescriptor = fs.openSync(this.configurationFile, 'w');
1369 fs.writeFileSync(fileDescriptor, JSON.stringify(configurationData, null, 2), 'utf8');
1370 fs.closeSync(fileDescriptor);
1371 PerformanceStatistics.endMeasure(measureId, beginId);
57adbebc 1372 this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
7c72977b 1373 this.configurationFileHash = configurationHash;
57adbebc 1374 this.sharedLRUCache.setChargingStationConfiguration(configurationData);
7c72977b
JB
1375 } else {
1376 logger.debug(
1377 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1378 this.configurationFile
1379 }`
1380 );
2484ac1e 1381 }
2484ac1e
JB
1382 } catch (error) {
1383 FileUtils.handleFileException(
1384 this.logPrefix(),
1385 FileType.ChargingStationConfiguration,
1386 this.configurationFile,
1387 error as NodeJS.ErrnoException
073bd098
JB
1388 );
1389 }
2484ac1e
JB
1390 } else {
1391 logger.error(
01efc60a 1392 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
2484ac1e 1393 );
073bd098
JB
1394 }
1395 }
1396
ccb1d6e9
JB
1397 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration | null {
1398 return this.getTemplateFromFile()?.Configuration ?? null;
2484ac1e
JB
1399 }
1400
1401 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | null {
72092cfc 1402 let configuration: ChargingStationConfiguration | null = null;
23290150 1403 if (this.getOcppPersistentConfiguration() === true) {
7a3a2ebb
JB
1404 const configurationFromFile = this.getConfigurationFromFile();
1405 configuration = configurationFromFile?.configurationKey && configurationFromFile;
073bd098 1406 }
2484ac1e 1407 configuration && delete configuration.stationInfo;
073bd098 1408 return configuration;
7dde0b73
JB
1409 }
1410
ccb1d6e9 1411 private getOcppConfiguration(): ChargingStationOcppConfiguration | null {
72092cfc
JB
1412 let ocppConfiguration: ChargingStationOcppConfiguration | null =
1413 this.getOcppConfigurationFromFile();
2484ac1e
JB
1414 if (!ocppConfiguration) {
1415 ocppConfiguration = this.getOcppConfigurationFromTemplate();
1416 }
1417 return ocppConfiguration;
1418 }
1419
c0560973 1420 private async onOpen(): Promise<void> {
56eb297e 1421 if (this.isWebSocketConnectionOpened() === true) {
5144f4d1
JB
1422 logger.info(
1423 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1424 );
ed6cfcff 1425 if (this.isRegistered() === false) {
5144f4d1
JB
1426 // Send BootNotification
1427 let registrationRetryCount = 0;
1428 do {
f7f98c68 1429 this.bootNotificationResponse = await this.ocppRequestService.requestHandler<
5144f4d1
JB
1430 BootNotificationRequest,
1431 BootNotificationResponse
8bfbc743
JB
1432 >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
1433 skipBufferingOnError: true,
1434 });
ed6cfcff 1435 if (this.isRegistered() === false) {
5144f4d1
JB
1436 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
1437 await Utils.sleep(
1895299d 1438 this?.bootNotificationResponse?.interval
5144f4d1
JB
1439 ? this.bootNotificationResponse.interval * 1000
1440 : Constants.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1441 );
1442 }
1443 } while (
ed6cfcff 1444 this.isRegistered() === false &&
5144f4d1
JB
1445 (registrationRetryCount <= this.getRegistrationMaxRetries() ||
1446 this.getRegistrationMaxRetries() === -1)
1447 );
1448 }
ed6cfcff 1449 if (this.isRegistered() === true) {
23290150 1450 if (this.isInAcceptedState() === true) {
94bb24d5 1451 await this.startMessageSequence();
c0560973 1452 }
5144f4d1
JB
1453 } else {
1454 logger.error(
1455 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1456 );
caad9d6b 1457 }
5144f4d1 1458 this.wsConnectionRestarted = false;
aa428a31 1459 this.autoReconnectRetryCount = 0;
1895299d 1460 parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
2e6f5966 1461 } else {
5144f4d1
JB
1462 logger.warn(
1463 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
e7aeea18 1464 );
2e6f5966 1465 }
2e6f5966
JB
1466 }
1467
ef7d8c21 1468 private async onClose(code: number, reason: Buffer): Promise<void> {
d09085e9 1469 switch (code) {
6c65a295
JB
1470 // Normal close
1471 case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
c0560973 1472 case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS:
e7aeea18 1473 logger.info(
5e3cb728 1474 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
e7aeea18 1475 code
ef7d8c21 1476 )}' and reason '${reason.toString()}'`
e7aeea18 1477 );
c0560973
JB
1478 this.autoReconnectRetryCount = 0;
1479 break;
6c65a295
JB
1480 // Abnormal close
1481 default:
e7aeea18 1482 logger.error(
5e3cb728 1483 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
e7aeea18 1484 code
ef7d8c21 1485 )}' and reason '${reason.toString()}'`
e7aeea18 1486 );
56eb297e 1487 this.started === true && (await this.reconnect());
c0560973
JB
1488 break;
1489 }
1895299d 1490 parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
2e6f5966
JB
1491 }
1492
ef7d8c21 1493 private async onMessage(data: RawData): Promise<void> {
b3ec7bc1
JB
1494 let messageType: number;
1495 let messageId: string;
1496 let commandName: IncomingRequestCommand;
1497 let commandPayload: JsonType;
1498 let errorType: ErrorType;
1499 let errorMessage: string;
1500 let errorDetails: JsonType;
d900c8d7
JB
1501 let responseCallback: ResponseCallback;
1502 let errorCallback: ErrorCallback;
32b02249 1503 let requestCommandName: RequestCommand | IncomingRequestCommand;
b3ec7bc1 1504 let requestPayload: JsonType;
32b02249 1505 let cachedRequest: CachedRequest;
c0560973
JB
1506 let errMsg: string;
1507 try {
b3ec7bc1 1508 const request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse;
53e5fd67 1509 if (Array.isArray(request) === true) {
9934652c 1510 [messageType, messageId] = request;
b3ec7bc1
JB
1511 // Check the type of message
1512 switch (messageType) {
1513 // Incoming Message
1514 case MessageType.CALL_MESSAGE:
9934652c 1515 [, , commandName, commandPayload] = request as IncomingRequest;
0638ddd2 1516 if (this.getEnableStatistics() === true) {
b3ec7bc1
JB
1517 this.performanceStatistics.addRequestStatistic(commandName, messageType);
1518 }
1519 logger.debug(
1520 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1521 request
1522 )}`
1523 );
1524 // Process the message
1525 await this.ocppIncomingRequestService.incomingRequestHandler(
08f130a0 1526 this,
b3ec7bc1
JB
1527 messageId,
1528 commandName,
1529 commandPayload
1530 );
1531 break;
1532 // Outcome Message
1533 case MessageType.CALL_RESULT_MESSAGE:
9934652c 1534 [, , commandPayload] = request as Response;
ba7965c4 1535 if (this.requests.has(messageId) === false) {
a2d1c0f1
JB
1536 // Error
1537 throw new OCPPError(
1538 ErrorType.INTERNAL_ERROR,
1539 `Response for unknown message id ${messageId}`,
72092cfc 1540 undefined,
a2d1c0f1
JB
1541 commandPayload
1542 );
1543 }
b3ec7bc1
JB
1544 // Respond
1545 cachedRequest = this.requests.get(messageId);
53e5fd67 1546 if (Array.isArray(cachedRequest) === true) {
53e07f94 1547 [responseCallback, errorCallback, requestCommandName, requestPayload] = cachedRequest;
b3ec7bc1
JB
1548 } else {
1549 throw new OCPPError(
1550 ErrorType.PROTOCOL_ERROR,
53e5fd67 1551 `Cached request for message id ${messageId} response is not an array`,
72092cfc 1552 undefined,
c2bc716f 1553 cachedRequest as unknown as JsonType
b3ec7bc1
JB
1554 );
1555 }
1556 logger.debug(
7ec6c5c9 1557 `${this.logPrefix()} << Command '${
2a232a18 1558 requestCommandName ?? Constants.UNKNOWN_COMMAND
7ec6c5c9 1559 }' received response payload: ${JSON.stringify(request)}`
b3ec7bc1 1560 );
a2d1c0f1
JB
1561 responseCallback(commandPayload, requestPayload);
1562 break;
1563 // Error Message
1564 case MessageType.CALL_ERROR_MESSAGE:
1565 [, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
ba7965c4 1566 if (this.requests.has(messageId) === false) {
b3ec7bc1
JB
1567 // Error
1568 throw new OCPPError(
1569 ErrorType.INTERNAL_ERROR,
a2d1c0f1 1570 `Error response for unknown message id ${messageId}`,
72092cfc 1571 undefined,
a2d1c0f1 1572 { errorType, errorMessage, errorDetails }
b3ec7bc1
JB
1573 );
1574 }
b3ec7bc1 1575 cachedRequest = this.requests.get(messageId);
53e5fd67 1576 if (Array.isArray(cachedRequest) === true) {
a2d1c0f1 1577 [, errorCallback, requestCommandName] = cachedRequest;
b3ec7bc1
JB
1578 } else {
1579 throw new OCPPError(
1580 ErrorType.PROTOCOL_ERROR,
53e5fd67 1581 `Cached request for message id ${messageId} error response is not an array`,
72092cfc 1582 undefined,
c2bc716f 1583 cachedRequest as unknown as JsonType
b3ec7bc1
JB
1584 );
1585 }
1586 logger.debug(
7ec6c5c9 1587 `${this.logPrefix()} << Command '${
2a232a18 1588 requestCommandName ?? Constants.UNKNOWN_COMMAND
2cc5d5ec 1589 }' received error response payload: ${JSON.stringify(request)}`
b3ec7bc1 1590 );
a2d1c0f1 1591 errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
b3ec7bc1
JB
1592 break;
1593 // Error
1594 default:
1595 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
fc040c43
JB
1596 errMsg = `Wrong message type ${messageType}`;
1597 logger.error(`${this.logPrefix()} ${errMsg}`);
b3ec7bc1
JB
1598 throw new OCPPError(ErrorType.PROTOCOL_ERROR, errMsg);
1599 }
1895299d 1600 parentPort?.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
47e22477 1601 } else {
53e5fd67 1602 throw new OCPPError(ErrorType.PROTOCOL_ERROR, 'Incoming message is not an array', null, {
ba7965c4 1603 request,
ac54a9bb 1604 });
47e22477 1605 }
c0560973
JB
1606 } catch (error) {
1607 // Log
e7aeea18 1608 logger.error(
91a4f151 1609 `${this.logPrefix()} Incoming OCPP command '${
2a232a18 1610 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
9c5d9fa4
JB
1611 }' message '${data.toString()}'${
1612 messageType !== MessageType.CALL_MESSAGE
1613 ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'`
1614 : ''
1615 } processing error:`,
e7aeea18
JB
1616 error
1617 );
ba7965c4 1618 if (error instanceof OCPPError === false) {
247659af 1619 logger.warn(
91a4f151 1620 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2a232a18 1621 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
fc040c43 1622 }' message '${data.toString()}' handling is not an OCPPError:`,
247659af
JB
1623 error
1624 );
1625 }
13701f69
JB
1626 switch (messageType) {
1627 case MessageType.CALL_MESSAGE:
1628 // Send error
1629 await this.ocppRequestService.sendError(
1630 this,
1631 messageId,
1632 error as OCPPError,
1633 commandName ?? requestCommandName ?? null
1634 );
1635 break;
1636 case MessageType.CALL_RESULT_MESSAGE:
1637 case MessageType.CALL_ERROR_MESSAGE:
1638 if (errorCallback) {
1639 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1640 errorCallback(error as OCPPError, false);
1641 } else {
1642 // Remove the request from the cache in case of error at response handling
1643 this.requests.delete(messageId);
1644 }
de4cb8b6 1645 break;
ba7965c4 1646 }
c0560973 1647 }
2328be1e
JB
1648 }
1649
c0560973 1650 private onPing(): void {
44eb6026 1651 logger.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
c0560973
JB
1652 }
1653
1654 private onPong(): void {
44eb6026 1655 logger.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
c0560973
JB
1656 }
1657
9534e74e 1658 private onError(error: WSError): void {
bcc9c3c0 1659 this.closeWSConnection();
44eb6026 1660 logger.error(`${this.logPrefix()} WebSocket error:`, error);
c0560973
JB
1661 }
1662
18bf8274 1663 private getEnergyActiveImportRegister(connectorStatus: ConnectorStatus, rounded = false): number {
95bdbf12 1664 if (this.getMeteringPerTransaction() === true) {
07989fad 1665 return (
18bf8274 1666 (rounded === true
07989fad
JB
1667 ? Math.round(connectorStatus?.transactionEnergyActiveImportRegisterValue)
1668 : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0
1669 );
1670 }
1671 return (
18bf8274 1672 (rounded === true
07989fad
JB
1673 ? Math.round(connectorStatus?.energyActiveImportRegisterValue)
1674 : connectorStatus?.energyActiveImportRegisterValue) ?? 0
1675 );
1676 }
1677
bb83b5ed 1678 private getUseConnectorId0(stationInfo?: ChargingStationInfo): boolean {
fa7bccf4
JB
1679 const localStationInfo = stationInfo ?? this.stationInfo;
1680 return !Utils.isUndefined(localStationInfo.useConnectorId0)
1681 ? localStationInfo.useConnectorId0
e7aeea18 1682 : true;
8bce55bf
JB
1683 }
1684
60ddad53
JB
1685 private getNumberOfRunningTransactions(): number {
1686 let trxCount = 0;
1687 for (const connectorId of this.connectors.keys()) {
1688 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
1689 trxCount++;
1690 }
1691 }
1692 return trxCount;
1693 }
1694
1695 private async stopRunningTransactions(reason = StopTransactionReason.NONE): Promise<void> {
1696 for (const connectorId of this.connectors.keys()) {
1697 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
1698 await this.stopTransactionOnConnector(connectorId, reason);
1699 }
1700 }
1701 }
1702
1f761b9a 1703 // 0 for disabling
c72f6634 1704 private getConnectionTimeout(): number {
17ac262c
JB
1705 if (
1706 ChargingStationConfigurationUtils.getConfigurationKey(
1707 this,
1708 StandardParametersKey.ConnectionTimeOut
1709 )
1710 ) {
e7aeea18 1711 return (
17ac262c
JB
1712 parseInt(
1713 ChargingStationConfigurationUtils.getConfigurationKey(
1714 this,
1715 StandardParametersKey.ConnectionTimeOut
1716 ).value
1717 ) ?? Constants.DEFAULT_CONNECTION_TIMEOUT
e7aeea18 1718 );
291cb255 1719 }
291cb255 1720 return Constants.DEFAULT_CONNECTION_TIMEOUT;
3574dfd3
JB
1721 }
1722
1f761b9a 1723 // -1 for unlimited, 0 for disabling
72092cfc 1724 private getAutoReconnectMaxRetries(): number | undefined {
ad2f27c3
JB
1725 if (!Utils.isUndefined(this.stationInfo.autoReconnectMaxRetries)) {
1726 return this.stationInfo.autoReconnectMaxRetries;
3574dfd3
JB
1727 }
1728 if (!Utils.isUndefined(Configuration.getAutoReconnectMaxRetries())) {
1729 return Configuration.getAutoReconnectMaxRetries();
1730 }
1731 return -1;
1732 }
1733
ec977daf 1734 // 0 for disabling
72092cfc 1735 private getRegistrationMaxRetries(): number | undefined {
ad2f27c3
JB
1736 if (!Utils.isUndefined(this.stationInfo.registrationMaxRetries)) {
1737 return this.stationInfo.registrationMaxRetries;
32a1eb7a
JB
1738 }
1739 return -1;
1740 }
1741
c0560973
JB
1742 private getPowerDivider(): number {
1743 let powerDivider = this.getNumberOfConnectors();
fa7bccf4 1744 if (this.stationInfo?.powerSharedByConnectors) {
c0560973 1745 powerDivider = this.getNumberOfRunningTransactions();
6ecb15e4
JB
1746 }
1747 return powerDivider;
1748 }
1749
fa7bccf4
JB
1750 private getMaximumAmperage(stationInfo: ChargingStationInfo): number | undefined {
1751 const maximumPower = this.getMaximumPower(stationInfo);
1752 switch (this.getCurrentOutType(stationInfo)) {
cc6e8ab5
JB
1753 case CurrentType.AC:
1754 return ACElectricUtils.amperagePerPhaseFromPower(
fa7bccf4 1755 this.getNumberOfPhases(stationInfo),
ad8537a7 1756 maximumPower / this.getNumberOfConnectors(),
fa7bccf4 1757 this.getVoltageOut(stationInfo)
cc6e8ab5
JB
1758 );
1759 case CurrentType.DC:
fa7bccf4 1760 return DCElectricUtils.amperage(maximumPower, this.getVoltageOut(stationInfo));
cc6e8ab5
JB
1761 }
1762 }
1763
cc6e8ab5
JB
1764 private getAmperageLimitation(): number | undefined {
1765 if (
1766 this.stationInfo.amperageLimitationOcppKey &&
17ac262c
JB
1767 ChargingStationConfigurationUtils.getConfigurationKey(
1768 this,
1769 this.stationInfo.amperageLimitationOcppKey
1770 )
cc6e8ab5
JB
1771 ) {
1772 return (
1773 Utils.convertToInt(
17ac262c
JB
1774 ChargingStationConfigurationUtils.getConfigurationKey(
1775 this,
1776 this.stationInfo.amperageLimitationOcppKey
72092cfc 1777 )?.value
17ac262c 1778 ) / ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
cc6e8ab5
JB
1779 );
1780 }
1781 }
1782
c0560973 1783 private async startMessageSequence(): Promise<void> {
b7f9e41d 1784 if (this.stationInfo?.autoRegister === true) {
f7f98c68 1785 await this.ocppRequestService.requestHandler<
ef6fa3fb
JB
1786 BootNotificationRequest,
1787 BootNotificationResponse
8bfbc743
JB
1788 >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
1789 skipBufferingOnError: true,
1790 });
6114e6f1 1791 }
136c90ba 1792 // Start WebSocket ping
c0560973 1793 this.startWebSocketPing();
5ad8570f 1794 // Start heartbeat
c0560973 1795 this.startHeartbeat();
0a60c33c 1796 // Initialize connectors status
734d790d 1797 for (const connectorId of this.connectors.keys()) {
72092cfc 1798 let connectorStatus: ConnectorStatusEnum | undefined;
734d790d 1799 if (connectorId === 0) {
593cf3f9 1800 continue;
e7aeea18 1801 } else if (
56eb297e
JB
1802 !this.getConnectorStatus(connectorId)?.status &&
1803 (this.isChargingStationAvailable() === false ||
1789ba2c 1804 this.isConnectorAvailable(connectorId) === false)
e7aeea18 1805 ) {
6e939d9e 1806 connectorStatus = ConnectorStatusEnum.UNAVAILABLE;
45c0ae82
JB
1807 } else if (
1808 !this.getConnectorStatus(connectorId)?.status &&
1809 this.getConnectorStatus(connectorId)?.bootStatus
1810 ) {
1811 // Set boot status in template at startup
72092cfc 1812 connectorStatus = this.getConnectorStatus(connectorId)?.bootStatus;
56eb297e
JB
1813 } else if (this.getConnectorStatus(connectorId)?.status) {
1814 // Set previous status at startup
72092cfc 1815 connectorStatus = this.getConnectorStatus(connectorId)?.status;
5ad8570f 1816 } else {
56eb297e 1817 // Set default status
6e939d9e 1818 connectorStatus = ConnectorStatusEnum.AVAILABLE;
5ad8570f 1819 }
56eb297e
JB
1820 await this.ocppRequestService.requestHandler<
1821 StatusNotificationRequest,
1822 StatusNotificationResponse
6e939d9e
JB
1823 >(
1824 this,
1825 RequestCommand.STATUS_NOTIFICATION,
1826 OCPPServiceUtils.buildStatusNotificationRequest(this, connectorId, connectorStatus)
1827 );
1828 this.getConnectorStatus(connectorId).status = connectorStatus;
5ad8570f 1829 }
c9a4f9ea
JB
1830 if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) {
1831 await this.ocppRequestService.requestHandler<
1832 FirmwareStatusNotificationRequest,
1833 FirmwareStatusNotificationResponse
1834 >(this, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, {
1835 status: FirmwareStatus.Installed,
1836 });
1837 this.stationInfo.firmwareStatus = FirmwareStatus.Installed;
c9a4f9ea 1838 }
3637ca2c 1839
0a60c33c 1840 // Start the ATG
60ddad53 1841 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable === true) {
4f69be04 1842 this.startAutomaticTransactionGenerator();
fa7bccf4 1843 }
aa428a31 1844 this.wsConnectionRestarted === true && this.flushMessageBuffer();
fa7bccf4
JB
1845 }
1846
e7aeea18
JB
1847 private async stopMessageSequence(
1848 reason: StopTransactionReason = StopTransactionReason.NONE
1849 ): Promise<void> {
136c90ba 1850 // Stop WebSocket ping
c0560973 1851 this.stopWebSocketPing();
79411696 1852 // Stop heartbeat
c0560973 1853 this.stopHeartbeat();
fa7bccf4 1854 // Stop ongoing transactions
b20eb107 1855 if (this.automaticTransactionGenerator?.started === true) {
60ddad53
JB
1856 this.stopAutomaticTransactionGenerator();
1857 } else {
1858 await this.stopRunningTransactions(reason);
79411696 1859 }
45c0ae82
JB
1860 for (const connectorId of this.connectors.keys()) {
1861 if (connectorId > 0) {
1862 await this.ocppRequestService.requestHandler<
1863 StatusNotificationRequest,
1864 StatusNotificationResponse
6e939d9e
JB
1865 >(
1866 this,
1867 RequestCommand.STATUS_NOTIFICATION,
1868 OCPPServiceUtils.buildStatusNotificationRequest(
1869 this,
1870 connectorId,
1871 ConnectorStatusEnum.UNAVAILABLE
1872 )
1873 );
72092cfc 1874 this.getConnectorStatus(connectorId).status = undefined;
45c0ae82
JB
1875 }
1876 }
79411696
JB
1877 }
1878
c0560973 1879 private startWebSocketPing(): void {
17ac262c
JB
1880 const webSocketPingInterval: number = ChargingStationConfigurationUtils.getConfigurationKey(
1881 this,
e7aeea18
JB
1882 StandardParametersKey.WebSocketPingInterval
1883 )
1884 ? Utils.convertToInt(
17ac262c
JB
1885 ChargingStationConfigurationUtils.getConfigurationKey(
1886 this,
1887 StandardParametersKey.WebSocketPingInterval
72092cfc 1888 )?.value
e7aeea18 1889 )
9cd3dfb0 1890 : 0;
ad2f27c3
JB
1891 if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
1892 this.webSocketPingSetInterval = setInterval(() => {
56eb297e 1893 if (this.isWebSocketConnectionOpened() === true) {
72092cfc 1894 this.wsConnection?.ping();
136c90ba
JB
1895 }
1896 }, webSocketPingInterval * 1000);
e7aeea18 1897 logger.info(
44eb6026
JB
1898 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
1899 webSocketPingInterval
1900 )}`
e7aeea18 1901 );
ad2f27c3 1902 } else if (this.webSocketPingSetInterval) {
e7aeea18 1903 logger.info(
44eb6026
JB
1904 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
1905 webSocketPingInterval
1906 )}`
e7aeea18 1907 );
136c90ba 1908 } else {
e7aeea18
JB
1909 logger.error(
1910 `${this.logPrefix()} WebSocket ping interval set to ${
1911 webSocketPingInterval
1912 ? Utils.formatDurationSeconds(webSocketPingInterval)
1913 : webSocketPingInterval
1914 }, not starting the WebSocket ping`
1915 );
136c90ba
JB
1916 }
1917 }
1918
c0560973 1919 private stopWebSocketPing(): void {
ad2f27c3
JB
1920 if (this.webSocketPingSetInterval) {
1921 clearInterval(this.webSocketPingSetInterval);
136c90ba
JB
1922 }
1923 }
1924
1f5df42a 1925 private getConfiguredSupervisionUrl(): URL {
72092cfc 1926 const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls();
c0560973 1927 if (!Utils.isEmptyArray(supervisionUrls)) {
2dcfe98e
JB
1928 switch (Configuration.getSupervisionUrlDistribution()) {
1929 case SupervisionUrlDistribution.ROUND_ROBIN:
c72f6634
JB
1930 // FIXME
1931 this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
2dcfe98e
JB
1932 break;
1933 case SupervisionUrlDistribution.RANDOM:
c72f6634
JB
1934 this.configuredSupervisionUrlIndex = Math.floor(
1935 Utils.secureRandom() * supervisionUrls.length
1936 );
2dcfe98e 1937 break;
c72f6634
JB
1938 case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY:
1939 this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
2dcfe98e
JB
1940 break;
1941 default:
e7aeea18
JB
1942 logger.error(
1943 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
c72f6634 1944 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
e7aeea18
JB
1945 }`
1946 );
c72f6634 1947 this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
2dcfe98e 1948 break;
c0560973 1949 }
c72f6634 1950 return new URL(supervisionUrls[this.configuredSupervisionUrlIndex]);
c0560973 1951 }
57939a9d 1952 return new URL(supervisionUrls as string);
136c90ba
JB
1953 }
1954
c72f6634 1955 private getHeartbeatInterval(): number {
17ac262c
JB
1956 const HeartbeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
1957 this,
1958 StandardParametersKey.HeartbeatInterval
1959 );
c0560973
JB
1960 if (HeartbeatInterval) {
1961 return Utils.convertToInt(HeartbeatInterval.value) * 1000;
1962 }
17ac262c
JB
1963 const HeartBeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
1964 this,
1965 StandardParametersKey.HeartBeatInterval
1966 );
c0560973
JB
1967 if (HeartBeatInterval) {
1968 return Utils.convertToInt(HeartBeatInterval.value) * 1000;
0a60c33c 1969 }
b7f9e41d 1970 this.stationInfo?.autoRegister === false &&
e7aeea18
JB
1971 logger.warn(
1972 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
1973 Constants.DEFAULT_HEARTBEAT_INTERVAL
1974 }`
1975 );
47e22477 1976 return Constants.DEFAULT_HEARTBEAT_INTERVAL;
0a60c33c
JB
1977 }
1978
c0560973 1979 private stopHeartbeat(): void {
ad2f27c3
JB
1980 if (this.heartbeatSetInterval) {
1981 clearInterval(this.heartbeatSetInterval);
7dde0b73 1982 }
5ad8570f
JB
1983 }
1984
55516218 1985 private terminateWSConnection(): void {
56eb297e 1986 if (this.isWebSocketConnectionOpened() === true) {
72092cfc 1987 this.wsConnection?.terminate();
55516218
JB
1988 this.wsConnection = null;
1989 }
1990 }
1991
dd119a6b 1992 private stopMeterValues(connectorId: number) {
734d790d 1993 if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
72092cfc 1994 clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
dd119a6b
JB
1995 }
1996 }
1997
c72f6634 1998 private getReconnectExponentialDelay(): boolean {
e7aeea18
JB
1999 return !Utils.isUndefined(this.stationInfo.reconnectExponentialDelay)
2000 ? this.stationInfo.reconnectExponentialDelay
2001 : false;
5ad8570f
JB
2002 }
2003
aa428a31 2004 private async reconnect(): Promise<void> {
7874b0b1
JB
2005 // Stop WebSocket ping
2006 this.stopWebSocketPing();
136c90ba 2007 // Stop heartbeat
c0560973 2008 this.stopHeartbeat();
5ad8570f 2009 // Stop the ATG if needed
6d9876e7 2010 if (this.automaticTransactionGenerator?.configuration?.stopOnConnectionFailure === true) {
fa7bccf4 2011 this.stopAutomaticTransactionGenerator();
ad2f27c3 2012 }
e7aeea18
JB
2013 if (
2014 this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() ||
2015 this.getAutoReconnectMaxRetries() === -1
2016 ) {
ad2f27c3 2017 this.autoReconnectRetryCount++;
e7aeea18
JB
2018 const reconnectDelay = this.getReconnectExponentialDelay()
2019 ? Utils.exponentialDelay(this.autoReconnectRetryCount)
2020 : this.getConnectionTimeout() * 1000;
1e080116
JB
2021 const reconnectDelayWithdraw = 1000;
2022 const reconnectTimeout =
2023 reconnectDelay && reconnectDelay - reconnectDelayWithdraw > 0
2024 ? reconnectDelay - reconnectDelayWithdraw
2025 : 0;
e7aeea18 2026 logger.error(
d56ea27c 2027 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
e7aeea18
JB
2028 reconnectDelay,
2029 2
2030 )}ms, timeout ${reconnectTimeout}ms`
2031 );
032d6efc 2032 await Utils.sleep(reconnectDelay);
e7aeea18 2033 logger.error(
44eb6026 2034 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
e7aeea18
JB
2035 );
2036 this.openWSConnection(
ccb1d6e9 2037 { ...(this.stationInfo?.wsOptions ?? {}), handshakeTimeout: reconnectTimeout },
1e080116 2038 { closeOpened: true }
e7aeea18 2039 );
265e4266 2040 this.wsConnectionRestarted = true;
c0560973 2041 } else if (this.getAutoReconnectMaxRetries() !== -1) {
e7aeea18 2042 logger.error(
d56ea27c 2043 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
e7aeea18 2044 this.autoReconnectRetryCount
d56ea27c 2045 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
e7aeea18 2046 );
5ad8570f
JB
2047 }
2048 }
2049
fa7bccf4
JB
2050 private getAutomaticTransactionGeneratorConfigurationFromTemplate(): AutomaticTransactionGeneratorConfiguration | null {
2051 return this.getTemplateFromFile()?.AutomaticTransactionGenerator ?? null;
2052 }
2053
a2653482
JB
2054 private initializeConnectorStatus(connectorId: number): void {
2055 this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
2056 this.getConnectorStatus(connectorId).idTagAuthorized = false;
2057 this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
734d790d
JB
2058 this.getConnectorStatus(connectorId).transactionStarted = false;
2059 this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0;
2060 this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
0a60c33c 2061 }
7dde0b73 2062}