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';
12 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator';
13 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel';
14 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
15 import { ChargingStationUtils
} from
'./ChargingStationUtils';
16 import { IdTagsCache
} from
'./IdTagsCache';
18 OCPP16IncomingRequestService
,
20 OCPP16ResponseService
,
22 OCPP20IncomingRequestService
,
24 OCPP20ResponseService
,
25 type OCPPIncomingRequestService
,
26 type OCPPRequestService
,
29 import { SharedLRUCache
} from
'./SharedLRUCache';
30 import { BaseError
, OCPPError
} from
'../exception';
31 import { PerformanceStatistics
} from
'../performance';
33 type AutomaticTransactionGeneratorConfiguration
,
35 type BootNotificationRequest
,
36 type BootNotificationResponse
,
38 type ChargingStationConfiguration
,
39 type ChargingStationInfo
,
40 type ChargingStationOcppConfiguration
,
41 type ChargingStationTemplate
,
49 type EvseStatusConfiguration
,
52 type FirmwareStatusNotificationRequest
,
53 type FirmwareStatusNotificationResponse
,
55 type HeartbeatRequest
,
56 type HeartbeatResponse
,
58 type IncomingRequestCommand
,
63 type MeterValuesRequest
,
64 type MeterValuesResponse
,
68 RegistrationStatusEnumType
,
71 StandardParametersKey
,
72 type StatusNotificationRequest
,
73 type StatusNotificationResponse
,
74 StopTransactionReason
,
75 type StopTransactionRequest
,
76 type StopTransactionResponse
,
77 SupervisionUrlDistribution
,
78 SupportedFeatureProfiles
,
81 WebSocketCloseEventStatusCode
,
95 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
96 buildConnectorsStatus
,
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 templateFileHash
!: string;
132 private readonly sharedLRUCache
: SharedLRUCache
;
133 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
134 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
136 constructor(index
: number, templateFile
: string) {
137 this.started
= false;
138 this.starting
= false;
139 this.stopping
= false;
140 this.wsConnectionRestarted
= false;
141 this.autoReconnectRetryCount
= 0;
143 this.templateFile
= templateFile
;
144 this.connectors
= new Map
<number, ConnectorStatus
>();
145 this.evses
= new Map
<number, EvseStatus
>();
146 this.requests
= new Map
<string, CachedRequest
>();
147 this.messageBuffer
= new Set
<string>();
148 this.sharedLRUCache
= SharedLRUCache
.getInstance();
149 this.idTagsCache
= IdTagsCache
.getInstance();
150 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
155 public get
hasEvses(): boolean {
156 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
159 private get
wsConnectionUrl(): URL
{
162 this.getSupervisionUrlOcppConfiguration() &&
163 Utils.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
164 Utils.isNotEmptyString(
165 ChargingStationConfigurationUtils.getConfigurationKey(
167 this.getSupervisionUrlOcppKey()
170 ? ChargingStationConfigurationUtils.getConfigurationKey(
172 this.getSupervisionUrlOcppKey()
174 : this.configuredSupervisionUrl.href
175 }/${this.stationInfo.chargingStationId}`
179 public logPrefix
= (): string => {
180 return Utils
.logPrefix(
182 (Utils.isNotEmptyString(this?.stationInfo?.chargingStationId)
183 ? this?.stationInfo?.chargingStationId
184 : ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())) ??
185 'Error at building log prefix'
190 public hasIdTags(): boolean {
191 return Utils
.isNotEmptyArray(
192 this.idTagsCache
.getIdTags(ChargingStationUtils
.getIdTagsFile(this.stationInfo
))
196 public getEnableStatistics(): boolean {
197 return this.stationInfo
.enableStatistics
?? false;
200 public getMustAuthorizeAtRemoteStart(): boolean {
201 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
204 public getPayloadSchemaValidation(): boolean {
205 return this.stationInfo
.payloadSchemaValidation
?? true;
208 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
209 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
210 switch (this.getCurrentOutType(stationInfo
)) {
212 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
213 ? localStationInfo
.numberOfPhases
220 public isWebSocketConnectionOpened(): boolean {
221 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
224 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
225 return this?.bootNotificationResponse
?.status;
228 public inUnknownState(): boolean {
229 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
232 public inPendingState(): boolean {
233 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
236 public inAcceptedState(): boolean {
237 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
240 public inRejectedState(): boolean {
241 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
244 public isRegistered(): boolean {
246 this.inUnknownState() === false &&
247 (this.inAcceptedState() === true || this.inPendingState() === true)
251 public isChargingStationAvailable(): boolean {
252 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
255 public hasConnector(connectorId
: number): boolean {
257 for (const evseStatus
of this.evses
.values()) {
258 if (evseStatus
.connectors
.has(connectorId
)) {
264 return this.connectors
.has(connectorId
);
267 public isConnectorAvailable(connectorId
: number): boolean {
270 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
274 public getNumberOfConnectors(): number {
276 if (this.evses
.size
=== 0) {
277 throw new BaseError('Evses not initialized, cannot get number of connectors');
279 let numberOfConnectors
= 0;
280 for (const [evseId
, evseStatus
] of this.evses
) {
282 numberOfConnectors
+= evseStatus
.connectors
.size
;
285 return numberOfConnectors
;
287 if (this.connectors
.size
=== 0) {
288 throw new BaseError('Connectors not initialized, cannot get number of connectors');
290 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
293 public getNumberOfEvses(): number {
294 if (this.evses
.size
=== 0) {
295 throw new BaseError('Evses not initialized, cannot get number of evses');
297 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
300 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
302 for (const evseStatus
of this.evses
.values()) {
303 if (evseStatus
.connectors
.has(connectorId
)) {
304 return evseStatus
.connectors
.get(connectorId
);
309 return this.connectors
.get(connectorId
);
312 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
313 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
316 public getOcppStrictCompliance(): boolean {
317 return this.stationInfo
?.ocppStrictCompliance
?? false;
320 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
321 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
322 this.getCurrentOutType(stationInfo
),
326 return (stationInfo
?? this.stationInfo
).voltageOut
?? defaultVoltageOut
;
329 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
330 const localStationInfo
= stationInfo
?? this.stationInfo
;
331 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
334 public getConnectorMaximumAvailablePower(connectorId
: number): number {
335 let connectorAmperageLimitationPowerLimit
: number;
337 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
338 this.getAmperageLimitation() < this.stationInfo
?.maximumAmperage
340 connectorAmperageLimitationPowerLimit
=
341 (this.getCurrentOutType() === CurrentType
.AC
342 ? ACElectricUtils
.powerTotal(
343 this.getNumberOfPhases(),
344 this.getVoltageOut(),
345 this.getAmperageLimitation() *
346 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
348 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
351 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
352 const connectorChargingProfilesPowerLimit
=
353 ChargingStationUtils
.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
355 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
356 isNaN(connectorAmperageLimitationPowerLimit
)
358 : connectorAmperageLimitationPowerLimit
,
359 isNaN(connectorChargingProfilesPowerLimit
) ? Infinity : connectorChargingProfilesPowerLimit
363 public getTransactionIdTag(transactionId
: number): string | undefined {
365 for (const evseStatus
of this.evses
.values()) {
366 for (const connectorStatus
of evseStatus
.connectors
.values()) {
367 if (connectorStatus
.transactionId
=== transactionId
) {
368 return connectorStatus
.transactionIdTag
;
373 for (const connectorId
of this.connectors
.keys()) {
376 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
378 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
384 public getNumberOfRunningTransactions(): number {
387 for (const evseStatus
of this.evses
.values()) {
388 for (const connectorStatus
of evseStatus
.connectors
.values()) {
389 if (connectorStatus
.transactionStarted
=== true) {
395 for (const connectorId
of this.connectors
.keys()) {
396 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
404 public getOutOfOrderEndMeterValues(): boolean {
405 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
408 public getBeginEndMeterValues(): boolean {
409 return this.stationInfo
?.beginEndMeterValues
?? false;
412 public getMeteringPerTransaction(): boolean {
413 return this.stationInfo
?.meteringPerTransaction
?? true;
416 public getTransactionDataMeterValues(): boolean {
417 return this.stationInfo
?.transactionDataMeterValues
?? false;
420 public getMainVoltageMeterValues(): boolean {
421 return this.stationInfo
?.mainVoltageMeterValues
?? true;
424 public getPhaseLineToLineVoltageMeterValues(): boolean {
425 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
428 public getCustomValueLimitationMeterValues(): boolean {
429 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
432 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
434 for (const evseStatus
of this.evses
.values()) {
435 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
436 if (connectorStatus
.transactionId
=== transactionId
) {
442 for (const connectorId
of this.connectors
.keys()) {
445 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
453 public getEnergyActiveImportRegisterByTransactionId(
454 transactionId
: number,
457 return this.getEnergyActiveImportRegister(
458 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
463 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
464 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
);
467 public getAuthorizeRemoteTxRequests(): boolean {
468 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
470 StandardParametersKey
.AuthorizeRemoteTxRequests
472 return authorizeRemoteTxRequests
473 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
477 public getLocalAuthListEnabled(): boolean {
478 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
480 StandardParametersKey
.LocalAuthListEnabled
482 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
485 public getHeartbeatInterval(): number {
486 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
488 StandardParametersKey
.HeartbeatInterval
490 if (HeartbeatInterval
) {
491 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
493 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
495 StandardParametersKey
.HeartBeatInterval
497 if (HeartBeatInterval
) {
498 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
500 this.stationInfo
?.autoRegister
=== false &&
502 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
503 Constants.DEFAULT_HEARTBEAT_INTERVAL
506 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
509 public setSupervisionUrl(url
: string): void {
511 this.getSupervisionUrlOcppConfiguration() &&
512 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey())
514 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
516 this.getSupervisionUrlOcppKey(),
520 this.stationInfo
.supervisionUrls
= url
;
521 this.saveStationInfo();
522 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
526 public startHeartbeat(): void {
527 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
528 this.heartbeatSetInterval
= setInterval(() => {
529 this.ocppRequestService
530 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
533 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
537 }, this.getHeartbeatInterval());
539 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
540 this.getHeartbeatInterval()
543 } else if (this.heartbeatSetInterval
) {
545 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
546 this.getHeartbeatInterval()
551 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
556 public restartHeartbeat(): void {
558 this.stopHeartbeat();
560 this.startHeartbeat();
563 public restartWebSocketPing(): void {
564 // Stop WebSocket ping
565 this.stopWebSocketPing();
566 // Start WebSocket ping
567 this.startWebSocketPing();
570 public startMeterValues(connectorId
: number, interval
: number): void {
571 if (connectorId
=== 0) {
573 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`
577 if (!this.getConnectorStatus(connectorId
)) {
579 `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}`
583 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
585 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
589 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
590 Utils
.isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
593 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
598 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(() => {
599 // FIXME: Implement OCPP version agnostic helpers
600 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
603 this.getConnectorStatus(connectorId
).transactionId
,
606 this.ocppRequestService
607 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
609 RequestCommand
.METER_VALUES
,
612 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
613 meterValue
: [meterValue
],
618 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
625 `${this.logPrefix()} Charging station ${
626 StandardParametersKey.MeterValueSampleInterval
627 } configuration set to ${interval}, not sending MeterValues`
632 public stopMeterValues(connectorId
: number) {
633 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
634 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
638 public start(): void {
639 if (this.started
=== false) {
640 if (this.starting
=== false) {
641 this.starting
= true;
642 if (this.getEnableStatistics() === true) {
643 this.performanceStatistics
?.start();
645 this.openWSConnection();
646 // Monitor charging station template file
647 this.templateFileWatcher
= FileUtils
.watchJsonFile(
649 FileType
.ChargingStationTemplate
,
652 (event
, filename
): void => {
653 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
656 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
658 } file have changed, reload`
660 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
664 this.stopAutomaticTransactionGenerator();
665 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
666 this.startAutomaticTransactionGenerator();
668 if (this.getEnableStatistics() === true) {
669 this.performanceStatistics
?.restart();
671 this.performanceStatistics
?.stop();
673 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
676 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
684 parentPort
?.postMessage(MessageChannelUtils
.buildStartedMessage(this));
685 this.starting
= false;
687 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
690 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
694 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
695 if (this.started
=== true) {
696 if (this.stopping
=== false) {
697 this.stopping
= true;
698 await this.stopMessageSequence(reason
);
699 this.closeWSConnection();
700 if (this.getEnableStatistics() === true) {
701 this.performanceStatistics
?.stop();
703 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
704 this.templateFileWatcher
?.close();
705 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
706 delete this.bootNotificationResponse
;
707 this.started
= false;
708 this.saveConfiguration();
709 parentPort
?.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
710 this.stopping
= false;
712 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
715 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
719 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
720 await this.stop(reason
);
721 await Utils
.sleep(this.stationInfo
.resetTime
);
726 public saveOcppConfiguration(): void {
727 if (this.getOcppPersistentConfiguration()) {
728 this.saveConfiguration();
732 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
733 return ChargingStationConfigurationUtils
.getConfigurationKey(
735 StandardParametersKey
.SupportedFeatureProfiles
736 )?.value
?.includes(featureProfile
);
739 public bufferMessage(message
: string): void {
740 this.messageBuffer
.add(message
);
743 public openWSConnection(
744 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
745 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
747 terminateOpened
: false,
750 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
751 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
752 if (this.started
=== false && this.starting
=== false) {
754 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
759 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
760 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
762 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
764 if (params
?.closeOpened
) {
765 this.closeWSConnection();
767 if (params
?.terminateOpened
) {
768 this.terminateWSConnection();
771 if (this.isWebSocketConnectionOpened() === true) {
773 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
779 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
782 this.wsConnection
= new WebSocket(
783 this.wsConnectionUrl
,
784 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
788 // Handle WebSocket message
789 this.wsConnection
.on(
791 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
793 // Handle WebSocket error
794 this.wsConnection
.on(
796 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
798 // Handle WebSocket close
799 this.wsConnection
.on(
801 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
803 // Handle WebSocket open
804 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
805 // Handle WebSocket ping
806 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
807 // Handle WebSocket pong
808 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
811 public closeWSConnection(): void {
812 if (this.isWebSocketConnectionOpened() === true) {
813 this.wsConnection
?.close();
814 this.wsConnection
= null;
818 public getAutomaticTransactionGeneratorConfiguration():
819 | AutomaticTransactionGeneratorConfiguration
821 let automaticTransactionGeneratorConfiguration
:
822 | AutomaticTransactionGeneratorConfiguration
824 const automaticTransactionGeneratorConfigurationFromFile
=
825 this.getConfigurationFromFile()?.automaticTransactionGenerator
;
826 if (automaticTransactionGeneratorConfigurationFromFile
) {
827 automaticTransactionGeneratorConfiguration
=
828 automaticTransactionGeneratorConfigurationFromFile
;
830 automaticTransactionGeneratorConfiguration
=
831 this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
834 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
835 ...automaticTransactionGeneratorConfiguration
,
839 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
840 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
841 if (Utils
.isNotEmptyArray(connectorIds
)) {
842 for (const connectorId
of connectorIds
) {
843 this.automaticTransactionGenerator
?.startConnector(connectorId
);
846 this.automaticTransactionGenerator
?.start();
848 this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
849 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
852 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
853 if (Utils
.isNotEmptyArray(connectorIds
)) {
854 for (const connectorId
of connectorIds
) {
855 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
858 this.automaticTransactionGenerator
?.stop();
860 this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
861 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
864 public async stopTransactionOnConnector(
866 reason
= StopTransactionReason
.NONE
867 ): Promise
<StopTransactionResponse
> {
868 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
870 this.getBeginEndMeterValues() === true &&
871 this.getOcppStrictCompliance() === true &&
872 this.getOutOfOrderEndMeterValues() === false
874 // FIXME: Implement OCPP version agnostic helpers
875 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
878 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
880 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
882 RequestCommand
.METER_VALUES
,
886 meterValue
: [transactionEndMeterValue
],
890 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
892 RequestCommand
.STOP_TRANSACTION
,
895 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
901 private flushMessageBuffer(): void {
902 if (this.messageBuffer
.size
> 0) {
903 for (const message
of this.messageBuffer
.values()) {
905 let commandName
: RequestCommand
;
906 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
907 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
909 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
910 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
912 this.wsConnection
?.send(message
);
913 isRequest
&& PerformanceStatistics
.endMeasure(commandName
, beginId
);
915 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
917 )} payload sent: ${message}`
919 this.messageBuffer
.delete(message
);
924 private getSupervisionUrlOcppConfiguration(): boolean {
925 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
928 private getSupervisionUrlOcppKey(): string {
929 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
932 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
933 let template
: ChargingStationTemplate
;
935 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
936 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
938 const measureId
= `${FileType.ChargingStationTemplate} read`;
939 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
940 template
= JSON
.parse(
941 fs
.readFileSync(this.templateFile
, 'utf8')
942 ) as ChargingStationTemplate
;
943 PerformanceStatistics
.endMeasure(measureId
, beginId
);
944 template
.templateHash
= crypto
945 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
946 .update(JSON
.stringify(template
))
948 this.sharedLRUCache
.setChargingStationTemplate(template
);
949 this.templateFileHash
= template
.templateHash
;
952 ErrorUtils
.handleFileException(
954 FileType
.ChargingStationTemplate
,
955 error
as NodeJS
.ErrnoException
,
962 private getStationInfoFromTemplate(): ChargingStationInfo
{
963 const stationTemplate
: ChargingStationTemplate
| undefined = this.getTemplateFromFile();
964 ChargingStationUtils
.checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
965 ChargingStationUtils
.warnTemplateKeysDeprecation(
970 if (stationTemplate
?.Connectors
) {
971 ChargingStationUtils
.checkConnectorsConfiguration(
977 const stationInfo
: ChargingStationInfo
=
978 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
979 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
980 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
984 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
985 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
986 if (Utils
.isNotEmptyArray(stationTemplate
?.power
)) {
987 stationTemplate
.power
= stationTemplate
.power
as number[];
988 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
989 stationInfo
.maximumPower
=
990 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
991 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
992 : stationTemplate
.power
[powerArrayRandomIndex
];
994 stationTemplate
.power
= stationTemplate
?.power
as number;
995 stationInfo
.maximumPower
=
996 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
997 ? stationTemplate
.power
* 1000
998 : stationTemplate
.power
;
1000 stationInfo
.firmwareVersionPattern
=
1001 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1003 Utils
.isNotEmptyString(stationInfo
.firmwareVersion
) &&
1004 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
1007 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1009 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1012 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1019 stationTemplate
?.firmwareUpgrade
?? {}
1021 stationInfo
.resetTime
= !Utils
.isNullOrUndefined(stationTemplate
?.resetTime
)
1022 ? stationTemplate
.resetTime
* 1000
1023 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1024 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1028 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
1029 let stationInfo
: ChargingStationInfo
| undefined;
1030 if (this.getStationInfoPersistentConfiguration()) {
1031 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1033 delete stationInfo
?.infoHash
;
1039 private getStationInfo(): ChargingStationInfo
{
1040 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1041 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
1043 // 1. charging station info from template
1044 // 2. charging station info from configuration file
1045 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1046 return stationInfoFromFile
;
1048 stationInfoFromFile
&&
1049 ChargingStationUtils
.propagateSerialNumber(
1050 this.getTemplateFromFile(),
1051 stationInfoFromFile
,
1052 stationInfoFromTemplate
1054 return stationInfoFromTemplate
;
1057 private saveStationInfo(): void {
1058 if (this.getStationInfoPersistentConfiguration()) {
1059 this.saveConfiguration();
1063 private getOcppPersistentConfiguration(): boolean {
1064 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1067 private getStationInfoPersistentConfiguration(): boolean {
1068 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1071 private handleUnsupportedVersion(version
: OCPPVersion
) {
1072 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1073 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1074 throw new BaseError(errorMsg
);
1077 private initialize(): void {
1078 const stationTemplate
= this.getTemplateFromFile();
1079 ChargingStationUtils
.checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1080 this.configurationFile
= path
.join(
1081 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
1082 `${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json`
1084 const chargingStationConfiguration
= this.getConfigurationFromFile();
1085 const featureFlag
= false;
1088 (chargingStationConfiguration
?.connectorsStatus
|| chargingStationConfiguration
?.evsesStatus
)
1090 this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration
);
1092 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1094 this.stationInfo
= this.getStationInfo();
1096 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1097 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1098 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1100 const patternGroup
: number | undefined =
1101 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1102 this.stationInfo
.firmwareVersion
?.split('.').length
;
1103 const match
= this.stationInfo
?.firmwareVersion
1104 ?.match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1105 ?.slice(1, patternGroup
+ 1);
1106 const patchLevelIndex
= match
.length
- 1;
1107 match
[patchLevelIndex
] = (
1108 Utils
.convertToInt(match
[patchLevelIndex
]) +
1109 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
1111 this.stationInfo
.firmwareVersion
= match
?.join('.');
1113 this.saveStationInfo();
1114 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1115 if (this.getEnableStatistics() === true) {
1116 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1117 this.stationInfo
.hashId
,
1118 this.stationInfo
.chargingStationId
,
1119 this.configuredSupervisionUrl
1122 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
1125 this.powerDivider
= this.getPowerDivider();
1126 // OCPP configuration
1127 this.ocppConfiguration
= this.getOcppConfiguration();
1128 this.initializeOcppConfiguration();
1129 this.initializeOcppServices();
1130 if (this.stationInfo
?.autoRegister
=== true) {
1131 this.bootNotificationResponse
= {
1132 currentTime
: new Date(),
1133 interval
: this.getHeartbeatInterval() / 1000,
1134 status: RegistrationStatusEnumType
.ACCEPTED
,
1139 private initializeOcppServices(): void {
1140 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1141 switch (ocppVersion
) {
1142 case OCPPVersion
.VERSION_16
:
1143 this.ocppIncomingRequestService
=
1144 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1145 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1146 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1149 case OCPPVersion
.VERSION_20
:
1150 case OCPPVersion
.VERSION_201
:
1151 this.ocppIncomingRequestService
=
1152 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1153 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1154 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1158 this.handleUnsupportedVersion(ocppVersion
);
1163 private initializeOcppConfiguration(): void {
1165 !ChargingStationConfigurationUtils
.getConfigurationKey(
1167 StandardParametersKey
.HeartbeatInterval
1170 ChargingStationConfigurationUtils
.addConfigurationKey(
1172 StandardParametersKey
.HeartbeatInterval
,
1177 !ChargingStationConfigurationUtils
.getConfigurationKey(
1179 StandardParametersKey
.HeartBeatInterval
1182 ChargingStationConfigurationUtils
.addConfigurationKey(
1184 StandardParametersKey
.HeartBeatInterval
,
1190 this.getSupervisionUrlOcppConfiguration() &&
1191 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1192 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1194 ChargingStationConfigurationUtils
.addConfigurationKey(
1196 this.getSupervisionUrlOcppKey(),
1197 this.configuredSupervisionUrl
.href
,
1201 !this.getSupervisionUrlOcppConfiguration() &&
1202 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1203 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1205 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1207 this.getSupervisionUrlOcppKey(),
1212 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1213 !ChargingStationConfigurationUtils
.getConfigurationKey(
1215 this.stationInfo
.amperageLimitationOcppKey
1218 ChargingStationConfigurationUtils
.addConfigurationKey(
1220 this.stationInfo
.amperageLimitationOcppKey
,
1222 this.stationInfo
.maximumAmperage
*
1223 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1228 !ChargingStationConfigurationUtils
.getConfigurationKey(
1230 StandardParametersKey
.SupportedFeatureProfiles
1233 ChargingStationConfigurationUtils
.addConfigurationKey(
1235 StandardParametersKey
.SupportedFeatureProfiles
,
1236 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1239 ChargingStationConfigurationUtils
.addConfigurationKey(
1241 StandardParametersKey
.NumberOfConnectors
,
1242 this.getNumberOfConnectors().toString(),
1247 !ChargingStationConfigurationUtils
.getConfigurationKey(
1249 StandardParametersKey
.MeterValuesSampledData
1252 ChargingStationConfigurationUtils
.addConfigurationKey(
1254 StandardParametersKey
.MeterValuesSampledData
,
1255 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1259 !ChargingStationConfigurationUtils
.getConfigurationKey(
1261 StandardParametersKey
.ConnectorPhaseRotation
1264 const connectorsPhaseRotation
: string[] = [];
1265 if (this.hasEvses
) {
1266 for (const evseStatus
of this.evses
.values()) {
1267 for (const connectorId
of evseStatus
.connectors
.keys()) {
1268 connectorsPhaseRotation
.push(
1269 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1274 for (const connectorId
of this.connectors
.keys()) {
1275 connectorsPhaseRotation
.push(
1276 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1280 ChargingStationConfigurationUtils
.addConfigurationKey(
1282 StandardParametersKey
.ConnectorPhaseRotation
,
1283 connectorsPhaseRotation
.toString()
1287 !ChargingStationConfigurationUtils
.getConfigurationKey(
1289 StandardParametersKey
.AuthorizeRemoteTxRequests
1292 ChargingStationConfigurationUtils
.addConfigurationKey(
1294 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1299 !ChargingStationConfigurationUtils
.getConfigurationKey(
1301 StandardParametersKey
.LocalAuthListEnabled
1303 ChargingStationConfigurationUtils
.getConfigurationKey(
1305 StandardParametersKey
.SupportedFeatureProfiles
1306 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1308 ChargingStationConfigurationUtils
.addConfigurationKey(
1310 StandardParametersKey
.LocalAuthListEnabled
,
1315 !ChargingStationConfigurationUtils
.getConfigurationKey(
1317 StandardParametersKey
.ConnectionTimeOut
1320 ChargingStationConfigurationUtils
.addConfigurationKey(
1322 StandardParametersKey
.ConnectionTimeOut
,
1323 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1326 this.saveOcppConfiguration();
1329 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1330 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1331 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1332 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1334 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1335 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1336 const evseStatus
= Utils
.cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1337 delete evseStatus
.connectorsStatus
;
1338 this.evses
.set(evseId
, {
1339 ...(evseStatus
as EvseStatus
),
1340 connectors
: new Map
<number, ConnectorStatus
>(
1341 evseStatusConfiguration
.connectorsStatus
.map((connectorStatus
, connectorId
) => [
1348 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1349 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1350 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1351 throw new BaseError(errorMsg
);
1353 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1354 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1355 throw new BaseError(errorMsg
);
1359 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1360 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1361 this.initializeConnectorsFromTemplate(stationTemplate
);
1362 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1363 this.initializeEvsesFromTemplate(stationTemplate
);
1364 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1365 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1366 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1367 throw new BaseError(errorMsg
);
1369 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1370 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1371 throw new BaseError(errorMsg
);
1375 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1376 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1377 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1378 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1379 throw new BaseError(errorMsg
);
1381 if (!stationTemplate
?.Connectors
[0]) {
1383 `${this.logPrefix()} Charging station information from template ${
1385 } with no connector id 0 configuration`
1388 if (stationTemplate
?.Connectors
) {
1389 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1390 ChargingStationUtils
.checkConnectorsConfiguration(
1395 const connectorsConfigHash
= crypto
1396 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1398 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`
1401 const connectorsConfigChanged
=
1402 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1403 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1404 connectorsConfigChanged
&& this.connectors
.clear();
1405 this.connectorsConfigurationHash
= connectorsConfigHash
;
1406 if (templateMaxConnectors
> 0) {
1407 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1409 connectorId
=== 0 &&
1410 (!stationTemplate
?.Connectors
[connectorId
] ||
1411 this.getUseConnectorId0(stationTemplate
) === false)
1415 const templateConnectorId
=
1416 connectorId
> 0 && stationTemplate
?.randomConnectors
1417 ? Utils
.getRandomInteger(templateMaxAvailableConnectors
, 1)
1419 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1420 ChargingStationUtils
.checkStationInfoConnectorStatus(
1421 templateConnectorId
,
1426 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1428 ChargingStationUtils
.initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1429 this.saveConnectorsStatus();
1432 `${this.logPrefix()} Charging station information from template ${
1434 } with no connectors configuration defined, cannot create connectors`
1440 `${this.logPrefix()} Charging station information from template ${
1442 } with no connectors configuration defined, using already defined connectors`
1447 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1448 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1449 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1450 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1451 throw new BaseError(errorMsg
);
1453 if (!stationTemplate
?.Evses
[0]) {
1455 `${this.logPrefix()} Charging station information from template ${
1457 } with no evse id 0 configuration`
1460 if (!stationTemplate
?.Evses
[0]?.Connectors
[0]) {
1462 `${this.logPrefix()} Charging station information from template ${
1464 } with evse id 0 with no connector id 0 configuration`
1467 if (stationTemplate
?.Evses
) {
1468 const evsesConfigHash
= crypto
1469 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1470 .update(`${JSON.stringify(stationTemplate?.Evses)}`)
1472 const evsesConfigChanged
=
1473 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1474 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1475 evsesConfigChanged
&& this.evses
.clear();
1476 this.evsesConfigurationHash
= evsesConfigHash
;
1477 const templateMaxEvses
= ChargingStationUtils
.getMaxNumberOfEvses(stationTemplate
?.Evses
);
1478 if (templateMaxEvses
> 0) {
1479 for (const evse
in stationTemplate
.Evses
) {
1480 const evseId
= Utils
.convertToInt(evse
);
1481 this.evses
.set(evseId
, {
1482 connectors
: ChargingStationUtils
.buildConnectorsMap(
1483 stationTemplate
?.Evses
[evse
]?.Connectors
,
1487 availability
: AvailabilityType
.Operative
,
1489 ChargingStationUtils
.initializeConnectorsMapStatus(
1490 this.evses
.get(evseId
)?.connectors
,
1494 this.saveEvsesStatus();
1497 `${this.logPrefix()} Charging station information from template ${
1499 } with no evses configuration defined, cannot create evses`
1505 `${this.logPrefix()} Charging station information from template ${
1507 } with no evses configuration defined, using already defined evses`
1512 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1513 let configuration
: ChargingStationConfiguration
| undefined;
1514 if (Utils
.isNotEmptyString(this.configurationFile
) && fs
.existsSync(this.configurationFile
)) {
1516 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1517 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1518 this.configurationFileHash
1521 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1522 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1523 configuration
= JSON
.parse(
1524 fs
.readFileSync(this.configurationFile
, 'utf8')
1525 ) as ChargingStationConfiguration
;
1526 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1527 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1528 this.configurationFileHash
= configuration
.configurationHash
;
1531 ErrorUtils
.handleFileException(
1532 this.configurationFile
,
1533 FileType
.ChargingStationConfiguration
,
1534 error
as NodeJS
.ErrnoException
,
1539 return configuration
;
1542 private saveChargingStationAutomaticTransactionGeneratorConfiguration(): void {
1543 this.saveConfiguration();
1546 private saveConnectorsStatus() {
1547 this.saveConfiguration();
1550 private saveEvsesStatus() {
1551 this.saveConfiguration();
1554 private saveConfiguration(): void {
1555 if (Utils
.isNotEmptyString(this.configurationFile
)) {
1557 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1558 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1560 let configurationData
: ChargingStationConfiguration
=
1561 Utils
.cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()) ?? {};
1562 if (this.getStationInfoPersistentConfiguration() && this.stationInfo
) {
1563 configurationData
.stationInfo
= this.stationInfo
;
1565 if (this.getOcppPersistentConfiguration() && this.ocppConfiguration
?.configurationKey
) {
1566 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1568 configurationData
= merge
<ChargingStationConfiguration
>(
1570 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1572 if (this.connectors
.size
> 0) {
1573 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1575 if (this.evses
.size
> 0) {
1576 configurationData
.evsesStatus
= buildEvsesStatus(this);
1578 delete configurationData
.configurationHash
;
1579 const configurationHash
= crypto
1580 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1581 .update(JSON
.stringify(configurationData
))
1583 if (this.configurationFileHash
!== configurationHash
) {
1584 AsyncLock
.acquire(AsyncLockType
.configuration
)
1586 configurationData
.configurationHash
= configurationHash
;
1587 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1588 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1589 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1590 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1591 fs
.closeSync(fileDescriptor
);
1592 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1593 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1594 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1595 this.configurationFileHash
= configurationHash
;
1598 ErrorUtils
.handleFileException(
1599 this.configurationFile
,
1600 FileType
.ChargingStationConfiguration
,
1601 error
as NodeJS
.ErrnoException
,
1606 AsyncLock
.release(AsyncLockType
.configuration
).catch(Constants
.EMPTY_FUNCTION
);
1610 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1611 this.configurationFile
1616 ErrorUtils
.handleFileException(
1617 this.configurationFile
,
1618 FileType
.ChargingStationConfiguration
,
1619 error
as NodeJS
.ErrnoException
,
1625 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1630 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1631 return this.getTemplateFromFile()?.Configuration
;
1634 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1635 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1636 if (this.getOcppPersistentConfiguration() === true && configurationKey
) {
1637 return { configurationKey
};
1642 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1643 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1644 this.getOcppConfigurationFromFile();
1645 if (!ocppConfiguration
) {
1646 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1648 return ocppConfiguration
;
1651 private async onOpen(): Promise
<void> {
1652 if (this.isWebSocketConnectionOpened() === true) {
1654 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1656 if (this.isRegistered() === false) {
1657 // Send BootNotification
1658 let registrationRetryCount
= 0;
1660 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1661 BootNotificationRequest
,
1662 BootNotificationResponse
1663 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1664 skipBufferingOnError
: true,
1666 if (this.isRegistered() === false) {
1667 this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount
;
1669 this?.bootNotificationResponse
?.interval
1670 ? this.bootNotificationResponse
.interval
* 1000
1671 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1675 this.isRegistered() === false &&
1676 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1677 this.getRegistrationMaxRetries() === -1)
1680 if (this.isRegistered() === true) {
1681 if (this.inAcceptedState() === true) {
1682 await this.startMessageSequence();
1686 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1689 this.wsConnectionRestarted
= false;
1690 this.autoReconnectRetryCount
= 0;
1691 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1694 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1699 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1702 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1703 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1705 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1707 )}' and reason '${reason.toString()}'`
1709 this.autoReconnectRetryCount
= 0;
1714 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1716 )}' and reason '${reason.toString()}'`
1718 this.started
=== true && (await this.reconnect());
1721 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1724 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1725 const cachedRequest
= this.requests
.get(messageId
);
1726 if (Array.isArray(cachedRequest
) === true) {
1727 return cachedRequest
;
1729 throw new OCPPError(
1730 ErrorType
.PROTOCOL_ERROR
,
1731 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1733 )} is not an array`,
1735 cachedRequest
as JsonType
1739 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1740 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1741 if (this.getEnableStatistics() === true) {
1742 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1745 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1749 // Process the message
1750 await this.ocppIncomingRequestService
.incomingRequestHandler(
1758 private handleResponseMessage(response
: Response
): void {
1759 const [messageType
, messageId
, commandPayload
] = response
;
1760 if (this.requests
.has(messageId
) === false) {
1762 throw new OCPPError(
1763 ErrorType
.INTERNAL_ERROR
,
1764 `Response for unknown message id ${messageId}`,
1770 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1775 `${this.logPrefix()} << Command '${
1776 requestCommandName ?? Constants.UNKNOWN_COMMAND
1777 }' received response payload: ${JSON.stringify(response)}`
1779 responseCallback(commandPayload
, requestPayload
);
1782 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1783 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1784 if (this.requests
.has(messageId
) === false) {
1786 throw new OCPPError(
1787 ErrorType
.INTERNAL_ERROR
,
1788 `Error response for unknown message id ${messageId}`,
1790 { errorType
, errorMessage
, errorDetails
}
1793 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1795 `${this.logPrefix()} << Command '${
1796 requestCommandName ?? Constants.UNKNOWN_COMMAND
1797 }' received error response payload: ${JSON.stringify(errorResponse)}`
1799 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1802 private async onMessage(data
: RawData
): Promise
<void> {
1803 let request
: IncomingRequest
| Response
| ErrorResponse
;
1804 let messageType
: number;
1805 let errorMsg
: string;
1807 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1808 if (Array.isArray(request
) === true) {
1809 [messageType
] = request
;
1810 // Check the type of message
1811 switch (messageType
) {
1813 case MessageType
.CALL_MESSAGE
:
1814 await this.handleIncomingMessage(request
as IncomingRequest
);
1817 case MessageType
.CALL_RESULT_MESSAGE
:
1818 this.handleResponseMessage(request
as Response
);
1821 case MessageType
.CALL_ERROR_MESSAGE
:
1822 this.handleErrorMessage(request
as ErrorResponse
);
1826 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1827 errorMsg
= `Wrong message type ${messageType}`;
1828 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1829 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1831 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1833 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1838 let commandName
: IncomingRequestCommand
;
1839 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1840 let errorCallback
: ErrorCallback
;
1841 const [, messageId
] = request
;
1842 switch (messageType
) {
1843 case MessageType
.CALL_MESSAGE
:
1844 [, , commandName
] = request
as IncomingRequest
;
1846 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1848 case MessageType
.CALL_RESULT_MESSAGE
:
1849 case MessageType
.CALL_ERROR_MESSAGE
:
1850 if (this.requests
.has(messageId
) === true) {
1851 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1852 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1853 errorCallback(error
as OCPPError
, false);
1855 // Remove the request from the cache in case of error at response handling
1856 this.requests
.delete(messageId
);
1860 if (error
instanceof OCPPError
=== false) {
1862 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1863 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1864 }' message '${data.toString()}' handling is not an OCPPError:`,
1869 `${this.logPrefix()} Incoming OCPP command '${
1870 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1871 }' message '${data.toString()}'${
1872 messageType !== MessageType.CALL_MESSAGE
1873 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1875 } processing error:`,
1881 private onPing(): void {
1882 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1885 private onPong(): void {
1886 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1889 private onError(error
: WSError
): void {
1890 this.closeWSConnection();
1891 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1894 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1895 if (this.getMeteringPerTransaction() === true) {
1898 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1899 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1904 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1905 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1909 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
1910 return stationTemplate
?.useConnectorId0
?? true;
1913 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1914 if (this.hasEvses
) {
1915 for (const evseStatus
of this.evses
.values()) {
1916 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1917 if (connectorStatus
.transactionStarted
=== true) {
1918 await this.stopTransactionOnConnector(connectorId
, reason
);
1923 for (const connectorId
of this.connectors
.keys()) {
1924 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1925 await this.stopTransactionOnConnector(connectorId
, reason
);
1932 private getConnectionTimeout(): number {
1934 ChargingStationConfigurationUtils
.getConfigurationKey(
1936 StandardParametersKey
.ConnectionTimeOut
1941 ChargingStationConfigurationUtils
.getConfigurationKey(
1943 StandardParametersKey
.ConnectionTimeOut
1945 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1948 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1951 // -1 for unlimited, 0 for disabling
1952 private getAutoReconnectMaxRetries(): number | undefined {
1954 this.stationInfo
.autoReconnectMaxRetries
?? Configuration
.getAutoReconnectMaxRetries() ?? -1
1959 private getRegistrationMaxRetries(): number | undefined {
1960 return this.stationInfo
.registrationMaxRetries
?? -1;
1963 private getPowerDivider(): number {
1964 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
1965 if (this.stationInfo
?.powerSharedByConnectors
) {
1966 powerDivider
= this.getNumberOfRunningTransactions();
1968 return powerDivider
;
1971 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1972 const maximumPower
= this.getMaximumPower(stationInfo
);
1973 switch (this.getCurrentOutType(stationInfo
)) {
1974 case CurrentType
.AC
:
1975 return ACElectricUtils
.amperagePerPhaseFromPower(
1976 this.getNumberOfPhases(stationInfo
),
1977 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
1978 this.getVoltageOut(stationInfo
)
1980 case CurrentType
.DC
:
1981 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1985 private getAmperageLimitation(): number | undefined {
1987 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1988 ChargingStationConfigurationUtils
.getConfigurationKey(
1990 this.stationInfo
.amperageLimitationOcppKey
1995 ChargingStationConfigurationUtils
.getConfigurationKey(
1997 this.stationInfo
.amperageLimitationOcppKey
1999 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
2004 private async startMessageSequence(): Promise
<void> {
2005 if (this.stationInfo
?.autoRegister
=== true) {
2006 await this.ocppRequestService
.requestHandler
<
2007 BootNotificationRequest
,
2008 BootNotificationResponse
2009 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2010 skipBufferingOnError
: true,
2013 // Start WebSocket ping
2014 this.startWebSocketPing();
2016 this.startHeartbeat();
2017 // Initialize connectors status
2018 if (this.hasEvses
) {
2019 for (const [evseId
, evseStatus
] of this.evses
) {
2021 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2022 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2027 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2030 connectorBootStatus
,
2037 for (const connectorId
of this.connectors
.keys()) {
2038 if (connectorId
> 0) {
2039 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2042 this.getConnectorStatus(connectorId
)
2044 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2048 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2049 await this.ocppRequestService
.requestHandler
<
2050 FirmwareStatusNotificationRequest
,
2051 FirmwareStatusNotificationResponse
2052 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2053 status: FirmwareStatus
.Installed
,
2055 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2059 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2060 this.startAutomaticTransactionGenerator();
2062 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2065 private async stopMessageSequence(
2066 reason
: StopTransactionReason
= StopTransactionReason
.NONE
2068 // Stop WebSocket ping
2069 this.stopWebSocketPing();
2071 this.stopHeartbeat();
2072 // Stop ongoing transactions
2073 if (this.automaticTransactionGenerator
?.started
=== true) {
2074 this.stopAutomaticTransactionGenerator();
2076 await this.stopRunningTransactions(reason
);
2078 if (this.hasEvses
) {
2079 for (const [evseId
, evseStatus
] of this.evses
) {
2081 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2082 await this.ocppRequestService
.requestHandler
<
2083 StatusNotificationRequest
,
2084 StatusNotificationResponse
2087 RequestCommand
.STATUS_NOTIFICATION
,
2088 OCPPServiceUtils
.buildStatusNotificationRequest(
2091 ConnectorStatusEnum
.Unavailable
,
2095 delete connectorStatus
?.status;
2100 for (const connectorId
of this.connectors
.keys()) {
2101 if (connectorId
> 0) {
2102 await this.ocppRequestService
.requestHandler
<
2103 StatusNotificationRequest
,
2104 StatusNotificationResponse
2107 RequestCommand
.STATUS_NOTIFICATION
,
2108 OCPPServiceUtils
.buildStatusNotificationRequest(
2111 ConnectorStatusEnum
.Unavailable
2114 delete this.getConnectorStatus(connectorId
)?.status;
2120 private startWebSocketPing(): void {
2121 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
2123 StandardParametersKey
.WebSocketPingInterval
2125 ? Utils
.convertToInt(
2126 ChargingStationConfigurationUtils
.getConfigurationKey(
2128 StandardParametersKey
.WebSocketPingInterval
2132 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2133 this.webSocketPingSetInterval
= setInterval(() => {
2134 if (this.isWebSocketConnectionOpened() === true) {
2135 this.wsConnection
?.ping();
2137 }, webSocketPingInterval
* 1000);
2139 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
2140 webSocketPingInterval
2143 } else if (this.webSocketPingSetInterval
) {
2145 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
2146 webSocketPingInterval
2151 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2156 private stopWebSocketPing(): void {
2157 if (this.webSocketPingSetInterval
) {
2158 clearInterval(this.webSocketPingSetInterval
);
2159 delete this.webSocketPingSetInterval
;
2163 private getConfiguredSupervisionUrl(): URL
{
2164 let configuredSupervisionUrl
: string;
2165 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2166 if (Utils
.isNotEmptyArray(supervisionUrls
)) {
2167 let configuredSupervisionUrlIndex
: number;
2168 switch (Configuration
.getSupervisionUrlDistribution()) {
2169 case SupervisionUrlDistribution
.RANDOM
:
2170 configuredSupervisionUrlIndex
= Math.floor(Utils
.secureRandom() * supervisionUrls
.length
);
2172 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2173 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2175 Object.values(SupervisionUrlDistribution
).includes(
2176 Configuration
.getSupervisionUrlDistribution()
2179 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2180 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2183 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
2186 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
];
2188 configuredSupervisionUrl
= supervisionUrls
as string;
2190 if (Utils
.isNotEmptyString(configuredSupervisionUrl
)) {
2191 return new URL(configuredSupervisionUrl
);
2193 const errorMsg
= 'No supervision url(s) configured';
2194 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2195 throw new BaseError(`${errorMsg}`);
2198 private stopHeartbeat(): void {
2199 if (this.heartbeatSetInterval
) {
2200 clearInterval(this.heartbeatSetInterval
);
2201 delete this.heartbeatSetInterval
;
2205 private terminateWSConnection(): void {
2206 if (this.isWebSocketConnectionOpened() === true) {
2207 this.wsConnection
?.terminate();
2208 this.wsConnection
= null;
2212 private getReconnectExponentialDelay(): boolean {
2213 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2216 private async reconnect(): Promise
<void> {
2217 // Stop WebSocket ping
2218 this.stopWebSocketPing();
2220 this.stopHeartbeat();
2221 // Stop the ATG if needed
2222 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2223 this.stopAutomaticTransactionGenerator();
2226 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2227 this.getAutoReconnectMaxRetries() === -1
2229 ++this.autoReconnectRetryCount
;
2230 const reconnectDelay
= this.getReconnectExponentialDelay()
2231 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2232 : this.getConnectionTimeout() * 1000;
2233 const reconnectDelayWithdraw
= 1000;
2234 const reconnectTimeout
=
2235 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2236 ? reconnectDelay
- reconnectDelayWithdraw
2239 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2242 )}ms, timeout ${reconnectTimeout}ms`
2244 await Utils
.sleep(reconnectDelay
);
2246 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2248 this.openWSConnection(
2250 ...(this.stationInfo
?.wsOptions
?? {}),
2251 handshakeTimeout
: reconnectTimeout
,
2253 { closeOpened
: true }
2255 this.wsConnectionRestarted
= true;
2256 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2258 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2259 this.autoReconnectRetryCount
2260 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`