1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import crypto from
'node:crypto';
4 import fs from
'node:fs';
5 import path from
'node:path';
6 import { URL
} from
'node:url';
7 import { parentPort
} from
'node:worker_threads';
9 import merge from
'just-merge';
10 import WebSocket
, { type RawData
} from
'ws';
13 AutomaticTransactionGenerator
,
14 ChargingStationConfigurationUtils
,
16 ChargingStationWorkerBroadcastChannel
,
22 // OCPP16IncomingRequestService,
24 // OCPP16ResponseService,
26 OCPP20IncomingRequestService
,
28 // OCPP20ResponseService,
29 type OCPPIncomingRequestService
,
30 type OCPPRequestService
,
33 import { OCPP16IncomingRequestService
} from
'./ocpp/1.6/OCPP16IncomingRequestService';
34 import { OCPP16ResponseService
} from
'./ocpp/1.6/OCPP16ResponseService';
35 import { OCPP20ResponseService
} from
'./ocpp/2.0/OCPP20ResponseService';
36 import { OCPPServiceUtils
} from
'./ocpp/OCPPServiceUtils';
37 import { BaseError
, OCPPError
} from
'../exception';
38 import { PerformanceStatistics
} from
'../performance';
40 type AutomaticTransactionGeneratorConfiguration
,
42 type BootNotificationRequest
,
43 type BootNotificationResponse
,
45 type ChargingStationConfiguration
,
46 type ChargingStationInfo
,
47 type ChargingStationOcppConfiguration
,
48 type ChargingStationTemplate
,
49 ConnectorPhaseRotation
,
59 type FirmwareStatusNotificationRequest
,
60 type FirmwareStatusNotificationResponse
,
62 type HeartbeatRequest
,
63 type HeartbeatResponse
,
65 type IncomingRequestCommand
,
70 type MeterValuesRequest
,
71 type MeterValuesResponse
,
75 RegistrationStatusEnumType
,
78 StandardParametersKey
,
79 type StatusNotificationRequest
,
80 type StatusNotificationResponse
,
81 StopTransactionReason
,
82 type StopTransactionRequest
,
83 type StopTransactionResponse
,
84 SupervisionUrlDistribution
,
85 SupportedFeatureProfiles
,
88 WebSocketCloseEventStatusCode
,
101 export class ChargingStation
{
102 public readonly index
: number;
103 public readonly templateFile
: string;
104 public stationInfo
!: ChargingStationInfo
;
105 public started
: boolean;
106 public starting
: boolean;
107 public idTagsCache
: IdTagsCache
;
108 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
109 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
110 public wsConnection
!: WebSocket
| null;
111 public readonly connectors
: Map
<number, ConnectorStatus
>;
112 public readonly evses
: Map
<number, EvseStatus
>;
113 public readonly requests
: Map
<string, CachedRequest
>;
114 public performanceStatistics
!: PerformanceStatistics
| undefined;
115 public heartbeatSetInterval
!: NodeJS
.Timeout
;
116 public ocppRequestService
!: OCPPRequestService
;
117 public bootNotificationRequest
!: BootNotificationRequest
;
118 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
119 public powerDivider
!: number;
120 private stopping
: boolean;
121 private configurationFile
!: string;
122 private configurationFileHash
!: string;
123 private connectorsConfigurationHash
!: string;
124 private evsesConfigurationHash
!: string;
125 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
126 private readonly messageBuffer
: Set
<string>;
127 private configuredSupervisionUrl
!: URL
;
128 private wsConnectionRestarted
: boolean;
129 private autoReconnectRetryCount
: number;
130 private templateFileWatcher
!: fs
.FSWatcher
| undefined;
131 private readonly sharedLRUCache
: SharedLRUCache
;
132 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
133 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
135 constructor(index
: number, templateFile
: string) {
136 this.started
= false;
137 this.starting
= false;
138 this.stopping
= false;
139 this.wsConnectionRestarted
= false;
140 this.autoReconnectRetryCount
= 0;
142 this.templateFile
= templateFile
;
143 this.connectors
= new Map
<number, ConnectorStatus
>();
144 this.evses
= new Map
<number, EvseStatus
>();
145 this.requests
= new Map
<string, CachedRequest
>();
146 this.messageBuffer
= new Set
<string>();
147 this.sharedLRUCache
= SharedLRUCache
.getInstance();
148 this.idTagsCache
= IdTagsCache
.getInstance();
149 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
154 private get
wsConnectionUrl(): URL
{
157 this.getSupervisionUrlOcppConfiguration() &&
158 Utils.isNotEmptyString(this.getSupervisionUrlOcppKey())
159 ? ChargingStationConfigurationUtils.getConfigurationKey(
161 this.getSupervisionUrlOcppKey()
163 : this.configuredSupervisionUrl.href
164 }/${this.stationInfo.chargingStationId}`
168 private get
hasEvses(): boolean {
169 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
172 public logPrefix
= (): string => {
173 return Utils
.logPrefix(
175 (Utils.isNotEmptyString(this?.stationInfo?.chargingStationId)
176 ? this?.stationInfo?.chargingStationId
177 : ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())) ??
178 'Error at building log prefix'
183 public hasIdTags(): boolean {
184 const idTagsFile
= ChargingStationUtils
.getIdTagsFile(this.stationInfo
);
185 return Utils
.isNotEmptyArray(this.idTagsCache
.getIdTags(idTagsFile
));
188 public getEnableStatistics(): boolean {
189 return this.stationInfo
.enableStatistics
?? false;
192 public getMustAuthorizeAtRemoteStart(): boolean {
193 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
196 public getPayloadSchemaValidation(): boolean {
197 return this.stationInfo
.payloadSchemaValidation
?? true;
200 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
201 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
202 switch (this.getCurrentOutType(stationInfo
)) {
204 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
205 ? localStationInfo
.numberOfPhases
212 public isWebSocketConnectionOpened(): boolean {
213 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
216 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
217 return this?.bootNotificationResponse
?.status;
220 public isInUnknownState(): boolean {
221 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
224 public isInPendingState(): boolean {
225 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
228 public isInAcceptedState(): boolean {
229 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
232 public isInRejectedState(): boolean {
233 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
236 public isRegistered(): boolean {
238 this.isInUnknownState() === false &&
239 (this.isInAcceptedState() === true || this.isInPendingState() === true)
243 public isChargingStationAvailable(): boolean {
244 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
247 public isConnectorAvailable(connectorId
: number): boolean {
250 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
254 public getNumberOfConnectors(): number {
256 let numberOfConnectors
= 0;
257 for (const [evseId
, evseStatus
] of this.evses
) {
261 numberOfConnectors
+= evseStatus
.connectors
.size
;
263 return numberOfConnectors
;
265 return this.connectors
.get(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
268 public getNumberOfEvses(): number {
269 return this.evses
.size
;
272 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
274 for (const evseStatus
of this.evses
.values()) {
275 if (evseStatus
.connectors
.has(connectorId
)) {
276 return evseStatus
.connectors
.get(connectorId
);
280 return this.connectors
.get(connectorId
);
283 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
284 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
287 public getOcppStrictCompliance(): boolean {
288 return this.stationInfo
?.ocppStrictCompliance
?? false;
291 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
292 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
293 this.getCurrentOutType(stationInfo
),
297 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
298 return !Utils
.isUndefined(localStationInfo
.voltageOut
)
299 ? localStationInfo
.voltageOut
303 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
304 const localStationInfo
= stationInfo
?? this.stationInfo
;
305 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
308 public getConnectorMaximumAvailablePower(connectorId
: number): number {
309 let connectorAmperageLimitationPowerLimit
: number;
311 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
312 this.getAmperageLimitation() < this.stationInfo
?.maximumAmperage
314 connectorAmperageLimitationPowerLimit
=
315 (this.getCurrentOutType() === CurrentType
.AC
316 ? ACElectricUtils
.powerTotal(
317 this.getNumberOfPhases(),
318 this.getVoltageOut(),
319 this.getAmperageLimitation() * this.getNumberOfConnectors()
321 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
324 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
325 const connectorChargingProfilesPowerLimit
=
326 ChargingStationUtils
.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
328 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
329 isNaN(connectorAmperageLimitationPowerLimit
)
331 : connectorAmperageLimitationPowerLimit
,
332 isNaN(connectorChargingProfilesPowerLimit
) ? Infinity : connectorChargingProfilesPowerLimit
336 public getTransactionIdTag(transactionId
: number): string | undefined {
338 for (const evseStatus
of this.evses
.values()) {
339 for (const connectorStatus
of evseStatus
.connectors
.values()) {
340 if (connectorStatus
.transactionId
=== transactionId
) {
341 return connectorStatus
.transactionIdTag
;
346 for (const connectorId
of this.connectors
.keys()) {
349 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
351 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
357 public getOutOfOrderEndMeterValues(): boolean {
358 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
361 public getBeginEndMeterValues(): boolean {
362 return this.stationInfo
?.beginEndMeterValues
?? false;
365 public getMeteringPerTransaction(): boolean {
366 return this.stationInfo
?.meteringPerTransaction
?? true;
369 public getTransactionDataMeterValues(): boolean {
370 return this.stationInfo
?.transactionDataMeterValues
?? false;
373 public getMainVoltageMeterValues(): boolean {
374 return this.stationInfo
?.mainVoltageMeterValues
?? true;
377 public getPhaseLineToLineVoltageMeterValues(): boolean {
378 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
381 public getCustomValueLimitationMeterValues(): boolean {
382 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
385 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
387 for (const evseStatus
of this.evses
.values()) {
388 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
389 if (connectorStatus
.transactionId
=== transactionId
) {
395 for (const connectorId
of this.connectors
.keys()) {
398 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
406 public getEnergyActiveImportRegisterByTransactionId(
407 transactionId
: number,
410 return this.getEnergyActiveImportRegister(
411 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
416 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
417 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
);
420 public getAuthorizeRemoteTxRequests(): boolean {
421 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
423 StandardParametersKey
.AuthorizeRemoteTxRequests
425 return authorizeRemoteTxRequests
426 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
430 public getLocalAuthListEnabled(): boolean {
431 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
433 StandardParametersKey
.LocalAuthListEnabled
435 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
438 public getHeartbeatInterval(): number {
439 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
441 StandardParametersKey
.HeartbeatInterval
443 if (HeartbeatInterval
) {
444 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
446 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
448 StandardParametersKey
.HeartBeatInterval
450 if (HeartBeatInterval
) {
451 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
453 this.stationInfo
?.autoRegister
=== false &&
455 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
456 Constants.DEFAULT_HEARTBEAT_INTERVAL
459 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
462 public setSupervisionUrl(url
: string): void {
464 this.getSupervisionUrlOcppConfiguration() &&
465 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey())
467 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
469 this.getSupervisionUrlOcppKey(),
473 this.stationInfo
.supervisionUrls
= url
;
474 this.saveStationInfo();
475 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
479 public startHeartbeat(): void {
480 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
481 this.heartbeatSetInterval
= setInterval(() => {
482 this.ocppRequestService
483 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
486 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
490 }, this.getHeartbeatInterval());
492 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
493 this.getHeartbeatInterval()
496 } else if (this.heartbeatSetInterval
) {
498 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
499 this.getHeartbeatInterval()
504 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
509 public restartHeartbeat(): void {
511 this.stopHeartbeat();
513 this.startHeartbeat();
516 public restartWebSocketPing(): void {
517 // Stop WebSocket ping
518 this.stopWebSocketPing();
519 // Start WebSocket ping
520 this.startWebSocketPing();
523 public startMeterValues(connectorId
: number, interval
: number): void {
524 if (connectorId
=== 0) {
526 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`
530 if (!this.getConnectorStatus(connectorId
)) {
532 `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}`
536 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
538 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
542 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
543 Utils
.isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
546 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
551 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(() => {
552 // FIXME: Implement OCPP version agnostic helpers
553 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
556 this.getConnectorStatus(connectorId
).transactionId
,
559 this.ocppRequestService
560 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
562 RequestCommand
.METER_VALUES
,
565 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
566 meterValue
: [meterValue
],
571 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
578 `${this.logPrefix()} Charging station ${
579 StandardParametersKey.MeterValueSampleInterval
580 } configuration set to ${interval}, not sending MeterValues`
585 public stopMeterValues(connectorId
: number) {
586 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
587 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
591 public start(): void {
592 if (this.started
=== false) {
593 if (this.starting
=== false) {
594 this.starting
= true;
595 if (this.getEnableStatistics() === true) {
596 this.performanceStatistics
?.start();
598 this.openWSConnection();
599 // Monitor charging station template file
600 this.templateFileWatcher
= FileUtils
.watchJsonFile(
602 FileType
.ChargingStationTemplate
,
605 (event
, filename
): void => {
606 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
609 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
611 } file have changed, reload`
613 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
617 this.stopAutomaticTransactionGenerator();
619 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true
621 this.startAutomaticTransactionGenerator();
623 if (this.getEnableStatistics() === true) {
624 this.performanceStatistics
?.restart();
626 this.performanceStatistics
?.stop();
628 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
631 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
639 parentPort
?.postMessage(MessageChannelUtils
.buildStartedMessage(this));
640 this.starting
= false;
642 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
645 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
649 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
650 if (this.started
=== true) {
651 if (this.stopping
=== false) {
652 this.stopping
= true;
653 await this.stopMessageSequence(reason
);
654 this.closeWSConnection();
655 if (this.getEnableStatistics() === true) {
656 this.performanceStatistics
?.stop();
658 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
659 this.templateFileWatcher
?.close();
660 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
661 delete this.bootNotificationResponse
;
662 this.started
= false;
663 parentPort
?.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
664 this.stopping
= false;
666 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
669 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
673 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
674 await this.stop(reason
);
675 await Utils
.sleep(this.stationInfo
.resetTime
);
680 public saveOcppConfiguration(): void {
681 if (this.getOcppPersistentConfiguration()) {
682 this.saveConfiguration();
686 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
687 return ChargingStationConfigurationUtils
.getConfigurationKey(
689 StandardParametersKey
.SupportedFeatureProfiles
690 )?.value
?.includes(featureProfile
);
693 public bufferMessage(message
: string): void {
694 this.messageBuffer
.add(message
);
697 public openWSConnection(
698 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
699 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
701 terminateOpened
: false,
704 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
705 params
.closeOpened
= params
?.closeOpened
?? false;
706 params
.terminateOpened
= params
?.terminateOpened
?? false;
707 if (this.started
=== false && this.starting
=== false) {
709 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
714 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
715 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
717 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
719 if (params
?.closeOpened
) {
720 this.closeWSConnection();
722 if (params
?.terminateOpened
) {
723 this.terminateWSConnection();
726 if (this.isWebSocketConnectionOpened() === true) {
728 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
734 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
737 this.wsConnection
= new WebSocket(
738 this.wsConnectionUrl
,
739 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
743 // Handle WebSocket message
744 this.wsConnection
.on(
746 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
748 // Handle WebSocket error
749 this.wsConnection
.on(
751 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
753 // Handle WebSocket close
754 this.wsConnection
.on(
756 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
758 // Handle WebSocket open
759 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
760 // Handle WebSocket ping
761 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
762 // Handle WebSocket pong
763 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
766 public closeWSConnection(): void {
767 if (this.isWebSocketConnectionOpened() === true) {
768 this.wsConnection
?.close();
769 this.wsConnection
= null;
773 public startAutomaticTransactionGenerator(
774 connectorIds
?: number[],
775 automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
777 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(
778 automaticTransactionGeneratorConfiguration
??
779 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
782 if (Utils
.isNotEmptyArray(connectorIds
)) {
783 for (const connectorId
of connectorIds
) {
784 this.automaticTransactionGenerator
?.startConnector(connectorId
);
787 this.automaticTransactionGenerator
?.start();
789 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
792 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
793 if (Utils
.isNotEmptyArray(connectorIds
)) {
794 for (const connectorId
of connectorIds
) {
795 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
798 this.automaticTransactionGenerator
?.stop();
800 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
803 public async stopTransactionOnConnector(
805 reason
= StopTransactionReason
.NONE
806 ): Promise
<StopTransactionResponse
> {
807 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
809 this.getBeginEndMeterValues() === true &&
810 this.getOcppStrictCompliance() === true &&
811 this.getOutOfOrderEndMeterValues() === false
813 // FIXME: Implement OCPP version agnostic helpers
814 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
817 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
819 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
821 RequestCommand
.METER_VALUES
,
825 meterValue
: [transactionEndMeterValue
],
829 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
831 RequestCommand
.STOP_TRANSACTION
,
834 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
840 private flushMessageBuffer(): void {
841 if (this.messageBuffer
.size
> 0) {
842 for (const message
of this.messageBuffer
.values()) {
844 let commandName
: RequestCommand
;
845 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
846 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
848 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
849 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
851 this.wsConnection
?.send(message
);
852 isRequest
&& PerformanceStatistics
.endMeasure(commandName
, beginId
);
854 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
856 )} payload sent: ${message}`
858 this.messageBuffer
.delete(message
);
863 private getSupervisionUrlOcppConfiguration(): boolean {
864 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
867 private getSupervisionUrlOcppKey(): string {
868 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
871 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
872 let template
: ChargingStationTemplate
;
874 if (this.sharedLRUCache
.hasChargingStationTemplate(this.stationInfo
?.templateHash
)) {
875 template
= this.sharedLRUCache
.getChargingStationTemplate(this.stationInfo
.templateHash
);
877 const measureId
= `${FileType.ChargingStationTemplate} read`;
878 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
879 template
= JSON
.parse(
880 fs
.readFileSync(this.templateFile
, 'utf8')
881 ) as ChargingStationTemplate
;
882 PerformanceStatistics
.endMeasure(measureId
, beginId
);
883 template
.templateHash
= crypto
884 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
885 .update(JSON
.stringify(template
))
887 this.sharedLRUCache
.setChargingStationTemplate(template
);
890 FileUtils
.handleFileException(
892 FileType
.ChargingStationTemplate
,
893 error
as NodeJS
.ErrnoException
,
900 private getStationInfoFromTemplate(): ChargingStationInfo
{
901 const stationTemplate
: ChargingStationTemplate
| undefined = this.getTemplateFromFile();
902 if (Utils
.isNullOrUndefined(stationTemplate
)) {
903 const errorMsg
= `Failed to read charging station template file ${this.templateFile}`;
904 logger
.error(`${this.logPrefix()} ${errorMsg}`);
905 throw new BaseError(errorMsg
);
907 if (Utils
.isEmptyObject(stationTemplate
)) {
908 const errorMsg
= `Empty charging station information from template file ${this.templateFile}`;
909 logger
.error(`${this.logPrefix()} ${errorMsg}`);
910 throw new BaseError(errorMsg
);
912 ChargingStationUtils
.warnTemplateKeysDeprecation(
917 const stationInfo
: ChargingStationInfo
=
918 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
919 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
920 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
924 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
925 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
926 if (Utils
.isNotEmptyArray(stationTemplate
?.power
)) {
927 stationTemplate
.power
= stationTemplate
.power
as number[];
928 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
929 stationInfo
.maximumPower
=
930 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
931 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
932 : stationTemplate
.power
[powerArrayRandomIndex
];
934 stationTemplate
.power
= stationTemplate
?.power
as number;
935 stationInfo
.maximumPower
=
936 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
937 ? stationTemplate
.power
* 1000
938 : stationTemplate
.power
;
940 stationInfo
.firmwareVersionPattern
=
941 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
943 Utils
.isNotEmptyString(stationInfo
.firmwareVersion
) &&
944 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
947 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
949 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
952 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
959 stationTemplate
?.firmwareUpgrade
?? {}
961 stationInfo
.resetTime
= !Utils
.isNullOrUndefined(stationTemplate
?.resetTime
)
962 ? stationTemplate
.resetTime
* 1000
963 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
964 // Initialize evses or connectors if needed (FIXME: should be factored out)
965 this.initializeConnectorsOrEvses(stationInfo
);
966 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
967 ChargingStationUtils
.createStationInfoHash(stationInfo
);
971 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
972 let stationInfo
: ChargingStationInfo
| undefined;
973 this.getStationInfoPersistentConfiguration() &&
974 (stationInfo
= this.getConfigurationFromFile()?.stationInfo
);
975 stationInfo
&& ChargingStationUtils
.createStationInfoHash(stationInfo
);
979 private getStationInfo(): ChargingStationInfo
{
980 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
981 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
983 // 1. charging station info from template
984 // 2. charging station info from configuration file
985 // 3. charging station info attribute
986 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
987 if (this.stationInfo
?.infoHash
=== stationInfoFromFile
?.infoHash
) {
988 return this.stationInfo
;
990 return stationInfoFromFile
;
992 stationInfoFromFile
&&
993 ChargingStationUtils
.propagateSerialNumber(
994 this.getTemplateFromFile(),
996 stationInfoFromTemplate
998 return stationInfoFromTemplate
;
1001 private saveStationInfo(): void {
1002 if (this.getStationInfoPersistentConfiguration()) {
1003 this.saveConfiguration();
1007 private getOcppPersistentConfiguration(): boolean {
1008 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1011 private getStationInfoPersistentConfiguration(): boolean {
1012 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1015 private handleUnsupportedVersion(version
: OCPPVersion
) {
1016 const errMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1017 logger
.error(`${this.logPrefix()} ${errMsg}`);
1018 throw new BaseError(errMsg
);
1021 private initialize(): void {
1022 this.configurationFile
= path
.join(
1023 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
1024 `${ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile())}.json`
1026 this.stationInfo
= this.getStationInfo();
1028 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1029 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1030 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1032 const patternGroup
: number | undefined =
1033 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1034 this.stationInfo
.firmwareVersion
?.split('.').length
;
1035 const match
= this.stationInfo
?.firmwareVersion
1036 ?.match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1037 ?.slice(1, patternGroup
+ 1);
1038 const patchLevelIndex
= match
.length
- 1;
1039 match
[patchLevelIndex
] = (
1040 Utils
.convertToInt(match
[patchLevelIndex
]) +
1041 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
1043 this.stationInfo
.firmwareVersion
= match
?.join('.');
1045 this.saveStationInfo();
1046 // Avoid duplication of connectors or evses related information in RAM
1047 delete this.stationInfo
?.Connectors
;
1048 delete this.stationInfo
?.Evses
;
1049 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1050 if (this.getEnableStatistics() === true) {
1051 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1052 this.stationInfo
.hashId
,
1053 this.stationInfo
.chargingStationId
,
1054 this.configuredSupervisionUrl
1057 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
1060 this.powerDivider
= this.getPowerDivider();
1061 // OCPP configuration
1062 this.ocppConfiguration
= this.getOcppConfiguration();
1063 this.initializeOcppConfiguration();
1064 this.initializeOcppServices();
1065 if (this.stationInfo
?.autoRegister
=== true) {
1066 this.bootNotificationResponse
= {
1067 currentTime
: new Date(),
1068 interval
: this.getHeartbeatInterval() / 1000,
1069 status: RegistrationStatusEnumType
.ACCEPTED
,
1074 private initializeOcppServices(): void {
1075 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1076 switch (ocppVersion
) {
1077 case OCPPVersion
.VERSION_16
:
1078 this.ocppIncomingRequestService
=
1079 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1080 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1081 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1084 case OCPPVersion
.VERSION_20
:
1085 case OCPPVersion
.VERSION_201
:
1086 this.ocppIncomingRequestService
=
1087 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1088 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1089 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1093 this.handleUnsupportedVersion(ocppVersion
);
1098 private initializeOcppConfiguration(): void {
1100 !ChargingStationConfigurationUtils
.getConfigurationKey(
1102 StandardParametersKey
.HeartbeatInterval
1105 ChargingStationConfigurationUtils
.addConfigurationKey(
1107 StandardParametersKey
.HeartbeatInterval
,
1112 !ChargingStationConfigurationUtils
.getConfigurationKey(
1114 StandardParametersKey
.HeartBeatInterval
1117 ChargingStationConfigurationUtils
.addConfigurationKey(
1119 StandardParametersKey
.HeartBeatInterval
,
1125 this.getSupervisionUrlOcppConfiguration() &&
1126 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1127 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1129 ChargingStationConfigurationUtils
.addConfigurationKey(
1131 this.getSupervisionUrlOcppKey(),
1132 this.configuredSupervisionUrl
.href
,
1136 !this.getSupervisionUrlOcppConfiguration() &&
1137 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1138 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1140 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1142 this.getSupervisionUrlOcppKey(),
1147 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1148 !ChargingStationConfigurationUtils
.getConfigurationKey(
1150 this.stationInfo
.amperageLimitationOcppKey
1153 ChargingStationConfigurationUtils
.addConfigurationKey(
1155 this.stationInfo
.amperageLimitationOcppKey
,
1157 this.stationInfo
.maximumAmperage
*
1158 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1163 !ChargingStationConfigurationUtils
.getConfigurationKey(
1165 StandardParametersKey
.SupportedFeatureProfiles
1168 ChargingStationConfigurationUtils
.addConfigurationKey(
1170 StandardParametersKey
.SupportedFeatureProfiles
,
1171 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1174 ChargingStationConfigurationUtils
.addConfigurationKey(
1176 StandardParametersKey
.NumberOfConnectors
,
1177 this.getNumberOfConnectors().toString(),
1182 !ChargingStationConfigurationUtils
.getConfigurationKey(
1184 StandardParametersKey
.MeterValuesSampledData
1187 ChargingStationConfigurationUtils
.addConfigurationKey(
1189 StandardParametersKey
.MeterValuesSampledData
,
1190 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1194 !ChargingStationConfigurationUtils
.getConfigurationKey(
1196 StandardParametersKey
.ConnectorPhaseRotation
1199 const connectorPhaseRotation
= [];
1200 if (this.hasEvses
) {
1201 for (const evseStatus
of this.evses
.values()) {
1202 for (const connectorId
of evseStatus
.connectors
.keys()) {
1204 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
1205 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1206 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
1207 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1209 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
1210 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1211 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
1212 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1217 for (const connectorId
of this.connectors
.keys()) {
1219 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
1220 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1221 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
1222 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1224 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
1225 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1226 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
1227 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1231 ChargingStationConfigurationUtils
.addConfigurationKey(
1233 StandardParametersKey
.ConnectorPhaseRotation
,
1234 connectorPhaseRotation
.toString()
1238 !ChargingStationConfigurationUtils
.getConfigurationKey(
1240 StandardParametersKey
.AuthorizeRemoteTxRequests
1243 ChargingStationConfigurationUtils
.addConfigurationKey(
1245 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1250 !ChargingStationConfigurationUtils
.getConfigurationKey(
1252 StandardParametersKey
.LocalAuthListEnabled
1254 ChargingStationConfigurationUtils
.getConfigurationKey(
1256 StandardParametersKey
.SupportedFeatureProfiles
1257 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1259 ChargingStationConfigurationUtils
.addConfigurationKey(
1261 StandardParametersKey
.LocalAuthListEnabled
,
1266 !ChargingStationConfigurationUtils
.getConfigurationKey(
1268 StandardParametersKey
.ConnectionTimeOut
1271 ChargingStationConfigurationUtils
.addConfigurationKey(
1273 StandardParametersKey
.ConnectionTimeOut
,
1274 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1277 this.saveOcppConfiguration();
1280 private initializeConnectorsOrEvses(stationInfo
: ChargingStationInfo
) {
1281 if (stationInfo
?.Connectors
&& !stationInfo
?.Evses
) {
1282 this.initializeConnectors(stationInfo
);
1283 } else if (stationInfo
?.Evses
&& !stationInfo
?.Connectors
) {
1284 this.initializeEvses(stationInfo
);
1285 } else if (stationInfo
?.Evses
&& stationInfo
?.Connectors
) {
1286 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1287 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1288 throw new BaseError(errorMsg
);
1290 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1291 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1292 throw new BaseError(errorMsg
);
1296 private initializeConnectors(stationInfo
: ChargingStationInfo
): void {
1297 if (!stationInfo
?.Connectors
&& this.connectors
.size
=== 0) {
1298 const logMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1299 logger
.error(`${this.logPrefix()} ${logMsg}`);
1300 throw new BaseError(logMsg
);
1302 if (!stationInfo
?.Connectors
[0]) {
1304 `${this.logPrefix()} Charging station information from template ${
1306 } with no connector id 0 configuration`
1309 if (stationInfo
?.Connectors
) {
1310 const configuredMaxConnectors
=
1311 ChargingStationUtils
.getConfiguredNumberOfConnectors(stationInfo
);
1312 ChargingStationUtils
.checkConfiguredMaxConnectors(
1313 configuredMaxConnectors
,
1317 const connectorsConfigHash
= crypto
1318 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1319 .update(`${JSON.stringify(stationInfo?.Connectors)}${configuredMaxConnectors.toString()}`)
1321 const connectorsConfigChanged
=
1322 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1323 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1324 connectorsConfigChanged
&& this.connectors
.clear();
1325 this.connectorsConfigurationHash
= connectorsConfigHash
;
1326 const connectorZeroStatus
= stationInfo
?.Connectors
[0];
1327 // Add connector id 0
1328 if (connectorZeroStatus
&& this.getUseConnectorId0(stationInfo
) === true) {
1329 ChargingStationUtils
.checkStationInfoConnectorStatus(
1331 connectorZeroStatus
,
1335 this.connectors
.set(0, Utils
.cloneObject
<ConnectorStatus
>(connectorZeroStatus
));
1336 this.getConnectorStatus(0).availability
= AvailabilityType
.Operative
;
1337 if (Utils
.isUndefined(this.getConnectorStatus(0)?.chargingProfiles
)) {
1338 this.getConnectorStatus(0).chargingProfiles
= [];
1341 // Add remaining connectors
1342 const templateMaxConnectors
= ChargingStationUtils
.getMaxNumberOfConnectors(
1343 stationInfo
.Connectors
1345 ChargingStationUtils
.checkTemplateMaxConnectors(
1346 templateMaxConnectors
,
1351 configuredMaxConnectors
>
1352 (stationInfo
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) &&
1353 !stationInfo
?.randomConnectors
1356 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
1358 }, forcing random connector configurations affectation`
1360 stationInfo
.randomConnectors
= true;
1362 const templateMaxAvailableConnectors
= stationInfo
?.Connectors
[0]
1363 ? templateMaxConnectors
- 1
1364 : templateMaxConnectors
;
1365 if (templateMaxAvailableConnectors
> 0) {
1366 for (let connectorId
= 1; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1367 const templateConnectorId
= stationInfo
?.randomConnectors
1368 ? Utils
.getRandomInteger(templateMaxAvailableConnectors
, 1)
1370 const connectorStatus
= stationInfo
?.Connectors
[templateConnectorId
];
1371 ChargingStationUtils
.checkStationInfoConnectorStatus(
1372 templateConnectorId
,
1377 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1378 this.getConnectorStatus(connectorId
).availability
= AvailabilityType
.Operative
;
1379 if (Utils
.isUndefined(this.getConnectorStatus(connectorId
)?.chargingProfiles
)) {
1380 this.getConnectorStatus(connectorId
).chargingProfiles
= [];
1382 ChargingStationUtils
.initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1386 `${this.logPrefix()} Charging station information from template ${
1388 } with no connectors configuration defined, cannot create connectors`
1394 `${this.logPrefix()} Charging station information from template ${
1396 } with no connectors configuration defined, using already defined connectors`
1401 private initializeEvses(stationInfo
: ChargingStationInfo
): void {
1402 if (!stationInfo
?.Evses
&& this.evses
.size
=== 0) {
1403 const logMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1404 logger
.error(`${this.logPrefix()} ${logMsg}`);
1405 throw new BaseError(logMsg
);
1407 if (!stationInfo
?.Evses
[0]) {
1409 `${this.logPrefix()} Charging station information from template ${
1411 } with no evse id 0 configuration`
1414 if (stationInfo
?.Evses
) {
1415 const evsesConfigHash
= crypto
1416 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1417 .update(`${JSON.stringify(stationInfo?.Evses)}`)
1419 const evsesConfigChanged
=
1420 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1421 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1422 evsesConfigChanged
&& this.evses
.clear();
1423 this.evsesConfigurationHash
= evsesConfigHash
;
1424 const templateMaxEvses
= ChargingStationUtils
.getMaxNumberOfEvses(stationInfo
?.Evses
);
1425 if (templateMaxEvses
> 0) {
1426 for (const evse
in stationInfo
.Evses
) {
1427 this.evses
.set(Utils
.convertToInt(evse
), {
1428 connectors
: ChargingStationUtils
.buildConnectorsMap(
1429 stationInfo
?.Evses
[evse
]?.Connectors
,
1433 availability
: AvailabilityType
.Operative
,
1435 ChargingStationUtils
.initializeConnectorsMapStatus(
1436 this.evses
.get(Utils
.convertToInt(evse
))?.connectors
,
1442 `${this.logPrefix()} Charging station information from template ${
1444 } with no evses configuration defined, cannot create evses`
1450 `${this.logPrefix()} Charging station information from template ${
1452 } with no evses configuration defined, using already defined evses`
1457 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1458 let configuration
: ChargingStationConfiguration
| undefined;
1459 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1461 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1462 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1463 this.configurationFileHash
1466 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1467 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1468 configuration
= JSON
.parse(
1469 fs
.readFileSync(this.configurationFile
, 'utf8')
1470 ) as ChargingStationConfiguration
;
1471 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1472 this.configurationFileHash
= configuration
.configurationHash
;
1473 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1476 FileUtils
.handleFileException(
1477 this.configurationFile
,
1478 FileType
.ChargingStationConfiguration
,
1479 error
as NodeJS
.ErrnoException
,
1484 return configuration
;
1487 private saveConfiguration(): void {
1488 if (this.configurationFile
) {
1490 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1491 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1493 const configurationData
: ChargingStationConfiguration
=
1494 Utils
.cloneObject(this.getConfigurationFromFile()) ?? {};
1495 this.ocppConfiguration
?.configurationKey
&&
1496 (configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
);
1497 this.stationInfo
&& (configurationData
.stationInfo
= this.stationInfo
);
1498 delete configurationData
.configurationHash
;
1499 const configurationHash
= crypto
1500 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1501 .update(JSON
.stringify(configurationData
))
1503 if (this.configurationFileHash
!== configurationHash
) {
1504 configurationData
.configurationHash
= configurationHash
;
1505 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1506 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1507 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1508 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1509 fs
.closeSync(fileDescriptor
);
1510 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1511 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1512 this.configurationFileHash
= configurationHash
;
1513 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1516 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1517 this.configurationFile
1522 FileUtils
.handleFileException(
1523 this.configurationFile
,
1524 FileType
.ChargingStationConfiguration
,
1525 error
as NodeJS
.ErrnoException
,
1531 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1536 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1537 return this.getTemplateFromFile()?.Configuration
;
1540 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1541 let configuration
: ChargingStationConfiguration
| undefined;
1542 if (this.getOcppPersistentConfiguration() === true) {
1543 const configurationFromFile
= this.getConfigurationFromFile();
1544 configuration
= configurationFromFile
?.configurationKey
&& configurationFromFile
;
1546 if (!Utils
.isNullOrUndefined(configuration
)) {
1547 delete configuration
.stationInfo
;
1548 delete configuration
.configurationHash
;
1550 return configuration
;
1553 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1554 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1555 this.getOcppConfigurationFromFile();
1556 if (!ocppConfiguration
) {
1557 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1559 return ocppConfiguration
;
1562 private async onOpen(): Promise
<void> {
1563 if (this.isWebSocketConnectionOpened() === true) {
1565 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1567 if (this.isRegistered() === false) {
1568 // Send BootNotification
1569 let registrationRetryCount
= 0;
1571 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1572 BootNotificationRequest
,
1573 BootNotificationResponse
1574 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1575 skipBufferingOnError
: true,
1577 if (this.isRegistered() === false) {
1578 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1580 this?.bootNotificationResponse
?.interval
1581 ? this.bootNotificationResponse
.interval
* 1000
1582 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1586 this.isRegistered() === false &&
1587 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1588 this.getRegistrationMaxRetries() === -1)
1591 if (this.isRegistered() === true) {
1592 if (this.isInAcceptedState() === true) {
1593 await this.startMessageSequence();
1597 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1600 this.wsConnectionRestarted
= false;
1601 this.autoReconnectRetryCount
= 0;
1602 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1605 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1610 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1613 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1614 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1616 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1618 )}' and reason '${reason.toString()}'`
1620 this.autoReconnectRetryCount
= 0;
1625 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1627 )}' and reason '${reason.toString()}'`
1629 this.started
=== true && (await this.reconnect());
1632 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1635 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1636 const cachedRequest
= this.requests
.get(messageId
);
1637 if (Array.isArray(cachedRequest
) === true) {
1638 return cachedRequest
;
1640 throw new OCPPError(
1641 ErrorType
.PROTOCOL_ERROR
,
1642 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1644 )} is not an array`,
1646 cachedRequest
as JsonType
1650 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1651 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1652 if (this.getEnableStatistics() === true) {
1653 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1656 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1660 // Process the message
1661 await this.ocppIncomingRequestService
.incomingRequestHandler(
1669 private handleResponseMessage(response
: Response
): void {
1670 const [messageType
, messageId
, commandPayload
] = response
;
1671 if (this.requests
.has(messageId
) === false) {
1673 throw new OCPPError(
1674 ErrorType
.INTERNAL_ERROR
,
1675 `Response for unknown message id ${messageId}`,
1681 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1686 `${this.logPrefix()} << Command '${
1687 requestCommandName ?? Constants.UNKNOWN_COMMAND
1688 }' received response payload: ${JSON.stringify(response)}`
1690 responseCallback(commandPayload
, requestPayload
);
1693 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1694 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1695 if (this.requests
.has(messageId
) === false) {
1697 throw new OCPPError(
1698 ErrorType
.INTERNAL_ERROR
,
1699 `Error response for unknown message id ${messageId}`,
1701 { errorType
, errorMessage
, errorDetails
}
1704 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1706 `${this.logPrefix()} << Command '${
1707 requestCommandName ?? Constants.UNKNOWN_COMMAND
1708 }' received error response payload: ${JSON.stringify(errorResponse)}`
1710 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1713 private async onMessage(data
: RawData
): Promise
<void> {
1714 let request
: IncomingRequest
| Response
| ErrorResponse
;
1715 let messageType
: number;
1718 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1719 if (Array.isArray(request
) === true) {
1720 [messageType
] = request
;
1721 // Check the type of message
1722 switch (messageType
) {
1724 case MessageType
.CALL_MESSAGE
:
1725 await this.handleIncomingMessage(request
as IncomingRequest
);
1728 case MessageType
.CALL_RESULT_MESSAGE
:
1729 this.handleResponseMessage(request
as Response
);
1732 case MessageType
.CALL_ERROR_MESSAGE
:
1733 this.handleErrorMessage(request
as ErrorResponse
);
1737 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1738 errMsg
= `Wrong message type ${messageType}`;
1739 logger
.error(`${this.logPrefix()} ${errMsg}`);
1740 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1742 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1744 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1749 let commandName
: IncomingRequestCommand
;
1750 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1751 let errorCallback
: ErrorCallback
;
1752 const [, messageId
] = request
;
1753 switch (messageType
) {
1754 case MessageType
.CALL_MESSAGE
:
1755 [, , commandName
] = request
as IncomingRequest
;
1757 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1759 case MessageType
.CALL_RESULT_MESSAGE
:
1760 case MessageType
.CALL_ERROR_MESSAGE
:
1761 if (this.requests
.has(messageId
) === true) {
1762 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1763 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1764 errorCallback(error
as OCPPError
, false);
1766 // Remove the request from the cache in case of error at response handling
1767 this.requests
.delete(messageId
);
1771 if (error
instanceof OCPPError
=== false) {
1773 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1774 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1775 }' message '${data.toString()}' handling is not an OCPPError:`,
1780 `${this.logPrefix()} Incoming OCPP command '${
1781 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1782 }' message '${data.toString()}'${
1783 messageType !== MessageType.CALL_MESSAGE
1784 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1786 } processing error:`,
1792 private onPing(): void {
1793 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1796 private onPong(): void {
1797 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1800 private onError(error
: WSError
): void {
1801 this.closeWSConnection();
1802 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1805 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1806 if (this.getMeteringPerTransaction() === true) {
1809 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1810 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1815 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1816 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1820 private getUseConnectorId0(stationInfo
?: ChargingStationInfo
): boolean {
1821 const localStationInfo
= stationInfo
?? this.stationInfo
;
1822 return localStationInfo
?.useConnectorId0
?? true;
1825 private getNumberOfRunningTransactions(): number {
1827 if (this.hasEvses
) {
1828 for (const evseStatus
of this.evses
.values()) {
1829 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1830 if (connectorStatus
.transactionStarted
=== true) {
1836 for (const connectorId
of this.connectors
.keys()) {
1837 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1845 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1846 if (this.hasEvses
) {
1847 for (const evseStatus
of this.evses
.values()) {
1848 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1849 if (connectorStatus
.transactionStarted
=== true) {
1850 await this.stopTransactionOnConnector(connectorId
, reason
);
1855 for (const connectorId
of this.connectors
.keys()) {
1856 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1857 await this.stopTransactionOnConnector(connectorId
, reason
);
1864 private getConnectionTimeout(): number {
1866 ChargingStationConfigurationUtils
.getConfigurationKey(
1868 StandardParametersKey
.ConnectionTimeOut
1873 ChargingStationConfigurationUtils
.getConfigurationKey(
1875 StandardParametersKey
.ConnectionTimeOut
1877 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1880 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1883 // -1 for unlimited, 0 for disabling
1884 private getAutoReconnectMaxRetries(): number | undefined {
1885 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1886 return this.stationInfo
.autoReconnectMaxRetries
;
1888 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1889 return Configuration
.getAutoReconnectMaxRetries();
1895 private getRegistrationMaxRetries(): number | undefined {
1896 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1897 return this.stationInfo
.registrationMaxRetries
;
1902 private getPowerDivider(): number {
1903 let powerDivider
= this.getNumberOfConnectors();
1904 if (this.stationInfo
?.powerSharedByConnectors
) {
1905 powerDivider
= this.getNumberOfRunningTransactions();
1907 return powerDivider
;
1910 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1911 const maximumPower
= this.getMaximumPower(stationInfo
);
1912 switch (this.getCurrentOutType(stationInfo
)) {
1913 case CurrentType
.AC
:
1914 return ACElectricUtils
.amperagePerPhaseFromPower(
1915 this.getNumberOfPhases(stationInfo
),
1916 maximumPower
/ this.getNumberOfConnectors(),
1917 this.getVoltageOut(stationInfo
)
1919 case CurrentType
.DC
:
1920 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1924 private getAmperageLimitation(): number | undefined {
1926 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1927 ChargingStationConfigurationUtils
.getConfigurationKey(
1929 this.stationInfo
.amperageLimitationOcppKey
1934 ChargingStationConfigurationUtils
.getConfigurationKey(
1936 this.stationInfo
.amperageLimitationOcppKey
1938 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1943 private async startMessageSequence(): Promise
<void> {
1944 if (this.stationInfo
?.autoRegister
=== true) {
1945 await this.ocppRequestService
.requestHandler
<
1946 BootNotificationRequest
,
1947 BootNotificationResponse
1948 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1949 skipBufferingOnError
: true,
1952 // Start WebSocket ping
1953 this.startWebSocketPing();
1955 this.startHeartbeat();
1956 // Initialize connectors status
1957 for (const connectorId
of this.connectors
.keys()) {
1958 let connectorStatus
: ConnectorStatusEnum
| undefined;
1959 if (connectorId
=== 0) {
1962 !this.getConnectorStatus(connectorId
)?.status &&
1963 (this.isChargingStationAvailable() === false ||
1964 this.isConnectorAvailable(connectorId
) === false)
1966 connectorStatus
= ConnectorStatusEnum
.Unavailable
;
1968 !this.getConnectorStatus(connectorId
)?.status &&
1969 this.getConnectorStatus(connectorId
)?.bootStatus
1971 // Set boot status in template at startup
1972 connectorStatus
= this.getConnectorStatus(connectorId
)?.bootStatus
;
1973 } else if (this.getConnectorStatus(connectorId
)?.status) {
1974 // Set previous status at startup
1975 connectorStatus
= this.getConnectorStatus(connectorId
)?.status;
1977 // Set default status
1978 connectorStatus
= ConnectorStatusEnum
.Available
;
1980 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorStatus
);
1982 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
1983 await this.ocppRequestService
.requestHandler
<
1984 FirmwareStatusNotificationRequest
,
1985 FirmwareStatusNotificationResponse
1986 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1987 status: FirmwareStatus
.Installed
,
1989 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
1993 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true) {
1994 this.startAutomaticTransactionGenerator();
1996 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
1999 private async stopMessageSequence(
2000 reason
: StopTransactionReason
= StopTransactionReason
.NONE
2002 // Stop WebSocket ping
2003 this.stopWebSocketPing();
2005 this.stopHeartbeat();
2006 // Stop ongoing transactions
2007 if (this.automaticTransactionGenerator
?.started
=== true) {
2008 this.stopAutomaticTransactionGenerator();
2010 await this.stopRunningTransactions(reason
);
2012 for (const connectorId
of this.connectors
.keys()) {
2013 if (connectorId
> 0) {
2014 await this.ocppRequestService
.requestHandler
<
2015 StatusNotificationRequest
,
2016 StatusNotificationResponse
2019 RequestCommand
.STATUS_NOTIFICATION
,
2020 OCPPServiceUtils
.buildStatusNotificationRequest(
2023 ConnectorStatusEnum
.Unavailable
2026 delete this.getConnectorStatus(connectorId
)?.status;
2031 private startWebSocketPing(): void {
2032 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
2034 StandardParametersKey
.WebSocketPingInterval
2036 ? Utils
.convertToInt(
2037 ChargingStationConfigurationUtils
.getConfigurationKey(
2039 StandardParametersKey
.WebSocketPingInterval
2043 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2044 this.webSocketPingSetInterval
= setInterval(() => {
2045 if (this.isWebSocketConnectionOpened() === true) {
2046 this.wsConnection
?.ping();
2048 }, webSocketPingInterval
* 1000);
2050 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
2051 webSocketPingInterval
2054 } else if (this.webSocketPingSetInterval
) {
2056 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
2057 webSocketPingInterval
2062 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2067 private stopWebSocketPing(): void {
2068 if (this.webSocketPingSetInterval
) {
2069 clearInterval(this.webSocketPingSetInterval
);
2070 delete this.webSocketPingSetInterval
;
2074 private getConfiguredSupervisionUrl(): URL
{
2075 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2076 if (Utils
.isNotEmptyArray(supervisionUrls
)) {
2077 let configuredSupervisionUrlIndex
: number;
2078 switch (Configuration
.getSupervisionUrlDistribution()) {
2079 case SupervisionUrlDistribution
.RANDOM
:
2080 configuredSupervisionUrlIndex
= Math.floor(Utils
.secureRandom() * supervisionUrls
.length
);
2082 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2083 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2085 Object.values(SupervisionUrlDistribution
).includes(
2086 Configuration
.getSupervisionUrlDistribution()
2089 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2090 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2093 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
2096 return new URL(supervisionUrls
[configuredSupervisionUrlIndex
]);
2098 return new URL(supervisionUrls
as string);
2101 private stopHeartbeat(): void {
2102 if (this.heartbeatSetInterval
) {
2103 clearInterval(this.heartbeatSetInterval
);
2104 delete this.heartbeatSetInterval
;
2108 private terminateWSConnection(): void {
2109 if (this.isWebSocketConnectionOpened() === true) {
2110 this.wsConnection
?.terminate();
2111 this.wsConnection
= null;
2115 private getReconnectExponentialDelay(): boolean {
2116 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2119 private async reconnect(): Promise
<void> {
2120 // Stop WebSocket ping
2121 this.stopWebSocketPing();
2123 this.stopHeartbeat();
2124 // Stop the ATG if needed
2125 if (this.automaticTransactionGenerator
?.configuration
?.stopOnConnectionFailure
=== true) {
2126 this.stopAutomaticTransactionGenerator();
2129 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2130 this.getAutoReconnectMaxRetries() === -1
2132 this.autoReconnectRetryCount
++;
2133 const reconnectDelay
= this.getReconnectExponentialDelay()
2134 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2135 : this.getConnectionTimeout() * 1000;
2136 const reconnectDelayWithdraw
= 1000;
2137 const reconnectTimeout
=
2138 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2139 ? reconnectDelay
- reconnectDelayWithdraw
2142 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2145 )}ms, timeout ${reconnectTimeout}ms`
2147 await Utils
.sleep(reconnectDelay
);
2149 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2151 this.openWSConnection(
2153 ...(this.stationInfo
?.wsOptions
?? {}),
2154 handshakeTimeout
: reconnectTimeout
,
2156 { closeOpened
: true }
2158 this.wsConnectionRestarted
= true;
2159 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2161 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2162 this.autoReconnectRetryCount
2163 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2168 private getAutomaticTransactionGeneratorConfigurationFromTemplate():
2169 | AutomaticTransactionGeneratorConfiguration
2171 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
;