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 { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
14 import { ChargingStationUtils
} from
'./ChargingStationUtils';
15 import { ChargingStationWorkerBroadcastChannel
} from
'./ChargingStationWorkerBroadcastChannel';
16 import { IdTagsCache
} from
'./IdTagsCache';
17 import { MessageChannelUtils
} from
'./MessageChannelUtils';
19 OCPP16IncomingRequestService
,
21 OCPP16ResponseService
,
23 OCPP20IncomingRequestService
,
25 OCPP20ResponseService
,
26 type OCPPIncomingRequestService
,
27 type OCPPRequestService
,
30 import { SharedLRUCache
} from
'./SharedLRUCache';
31 import { BaseError
, OCPPError
} from
'../exception';
32 // import { PerformanceStatistics } from '../performance';
33 import { PerformanceStatistics
} from
'../performance/PerformanceStatistics';
35 type AutomaticTransactionGeneratorConfiguration
,
37 type BootNotificationRequest
,
38 type BootNotificationResponse
,
40 type ChargingStationAutomaticTransactionGeneratorConfiguration
,
41 type ChargingStationConfiguration
,
42 type ChargingStationInfo
,
43 type ChargingStationOcppConfiguration
,
44 type ChargingStationTemplate
,
52 type EvseStatusConfiguration
,
55 type FirmwareStatusNotificationRequest
,
56 type FirmwareStatusNotificationResponse
,
58 type HeartbeatRequest
,
59 type HeartbeatResponse
,
61 type IncomingRequestCommand
,
66 type MeterValuesRequest
,
67 type MeterValuesResponse
,
71 RegistrationStatusEnumType
,
74 StandardParametersKey
,
75 type StatusNotificationRequest
,
76 type StatusNotificationResponse
,
77 StopTransactionReason
,
78 type StopTransactionRequest
,
79 type StopTransactionResponse
,
80 SupervisionUrlDistribution
,
81 SupportedFeatureProfiles
,
84 WebSocketCloseEventStatusCode
,
100 export class ChargingStation
{
101 public readonly index
: number;
102 public readonly templateFile
: string;
103 public stationInfo
!: ChargingStationInfo
;
104 public started
: boolean;
105 public starting
: boolean;
106 public idTagsCache
: IdTagsCache
;
107 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
108 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
109 public wsConnection
!: WebSocket
| null;
110 public readonly connectors
: Map
<number, ConnectorStatus
>;
111 public readonly evses
: Map
<number, EvseStatus
>;
112 public readonly requests
: Map
<string, CachedRequest
>;
113 public performanceStatistics
!: PerformanceStatistics
| undefined;
114 public heartbeatSetInterval
!: NodeJS
.Timeout
;
115 public ocppRequestService
!: OCPPRequestService
;
116 public bootNotificationRequest
!: BootNotificationRequest
;
117 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
118 public powerDivider
!: number;
119 private stopping
: boolean;
120 private configurationFile
!: string;
121 private configurationFileHash
!: string;
122 private connectorsConfigurationHash
!: string;
123 private evsesConfigurationHash
!: string;
124 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
125 private readonly messageBuffer
: Set
<string>;
126 private configuredSupervisionUrl
!: URL
;
127 private wsConnectionRestarted
: boolean;
128 private autoReconnectRetryCount
: number;
129 private templateFileWatcher
!: fs
.FSWatcher
| undefined;
130 private templateFileHash
!: string;
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 public get
hasEvses(): boolean {
155 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
158 private get
wsConnectionUrl(): URL
{
161 this.getSupervisionUrlOcppConfiguration() &&
162 Utils.isNotEmptyString(this.getSupervisionUrlOcppKey())
163 ? ChargingStationConfigurationUtils.getConfigurationKey(
165 this.getSupervisionUrlOcppKey()
167 : this.configuredSupervisionUrl.href
168 }/${this.stationInfo.chargingStationId}`
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 return Utils
.isNotEmptyArray(
185 this.idTagsCache
.getIdTags(ChargingStationUtils
.getIdTagsFile(this.stationInfo
))
189 public getEnableStatistics(): boolean {
190 return this.stationInfo
.enableStatistics
?? false;
193 public getMustAuthorizeAtRemoteStart(): boolean {
194 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
197 public getPayloadSchemaValidation(): boolean {
198 return this.stationInfo
.payloadSchemaValidation
?? true;
201 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
202 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
203 switch (this.getCurrentOutType(stationInfo
)) {
205 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
206 ? localStationInfo
.numberOfPhases
213 public isWebSocketConnectionOpened(): boolean {
214 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
217 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
218 return this?.bootNotificationResponse
?.status;
221 public inUnknownState(): boolean {
222 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
225 public inPendingState(): boolean {
226 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
229 public inAcceptedState(): boolean {
230 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
233 public inRejectedState(): boolean {
234 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
237 public isRegistered(): boolean {
239 this.inUnknownState() === false &&
240 (this.inAcceptedState() === true || this.inPendingState() === true)
244 public isChargingStationAvailable(): boolean {
245 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
248 public hasConnector(connectorId
: number): boolean {
250 for (const evseStatus
of this.evses
.values()) {
251 if (evseStatus
.connectors
.has(connectorId
)) {
257 return this.connectors
.has(connectorId
);
260 public isConnectorAvailable(connectorId
: number): boolean {
263 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
267 public getNumberOfConnectors(): number {
269 if (this.evses
.size
=== 0) {
270 throw new BaseError('Evses not initialized, cannot get number of connectors');
272 let numberOfConnectors
= 0;
273 for (const [evseId
, evseStatus
] of this.evses
) {
275 numberOfConnectors
+= evseStatus
.connectors
.size
;
278 return numberOfConnectors
;
280 if (this.connectors
.size
=== 0) {
281 throw new BaseError('Connectors not initialized, cannot get number of connectors');
283 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
286 public getNumberOfEvses(): number {
287 if (this.evses
.size
=== 0) {
288 throw new BaseError('Evses not initialized, cannot get number of evses');
290 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
293 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
295 for (const evseStatus
of this.evses
.values()) {
296 if (evseStatus
.connectors
.has(connectorId
)) {
297 return evseStatus
.connectors
.get(connectorId
);
302 return this.connectors
.get(connectorId
);
305 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
306 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
309 public getOcppStrictCompliance(): boolean {
310 return this.stationInfo
?.ocppStrictCompliance
?? false;
313 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
314 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
315 this.getCurrentOutType(stationInfo
),
319 return (stationInfo
?? this.stationInfo
).voltageOut
?? defaultVoltageOut
;
322 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
323 const localStationInfo
= stationInfo
?? this.stationInfo
;
324 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
327 public getConnectorMaximumAvailablePower(connectorId
: number): number {
328 let connectorAmperageLimitationPowerLimit
: number;
330 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
331 this.getAmperageLimitation() < this.stationInfo
?.maximumAmperage
333 connectorAmperageLimitationPowerLimit
=
334 (this.getCurrentOutType() === CurrentType
.AC
335 ? ACElectricUtils
.powerTotal(
336 this.getNumberOfPhases(),
337 this.getVoltageOut(),
338 this.getAmperageLimitation() *
339 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
341 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
344 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
345 const connectorChargingProfilesPowerLimit
=
346 ChargingStationUtils
.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
348 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
349 isNaN(connectorAmperageLimitationPowerLimit
)
351 : connectorAmperageLimitationPowerLimit
,
352 isNaN(connectorChargingProfilesPowerLimit
) ? Infinity : connectorChargingProfilesPowerLimit
356 public getTransactionIdTag(transactionId
: number): string | undefined {
358 for (const evseStatus
of this.evses
.values()) {
359 for (const connectorStatus
of evseStatus
.connectors
.values()) {
360 if (connectorStatus
.transactionId
=== transactionId
) {
361 return connectorStatus
.transactionIdTag
;
366 for (const connectorId
of this.connectors
.keys()) {
369 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
371 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
377 public getNumberOfRunningTransactions(): number {
380 for (const evseStatus
of this.evses
.values()) {
381 for (const connectorStatus
of evseStatus
.connectors
.values()) {
382 if (connectorStatus
.transactionStarted
=== true) {
388 for (const connectorId
of this.connectors
.keys()) {
389 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
397 public getOutOfOrderEndMeterValues(): boolean {
398 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
401 public getBeginEndMeterValues(): boolean {
402 return this.stationInfo
?.beginEndMeterValues
?? false;
405 public getMeteringPerTransaction(): boolean {
406 return this.stationInfo
?.meteringPerTransaction
?? true;
409 public getTransactionDataMeterValues(): boolean {
410 return this.stationInfo
?.transactionDataMeterValues
?? false;
413 public getMainVoltageMeterValues(): boolean {
414 return this.stationInfo
?.mainVoltageMeterValues
?? true;
417 public getPhaseLineToLineVoltageMeterValues(): boolean {
418 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
421 public getCustomValueLimitationMeterValues(): boolean {
422 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
425 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
427 for (const evseStatus
of this.evses
.values()) {
428 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
429 if (connectorStatus
.transactionId
=== transactionId
) {
435 for (const connectorId
of this.connectors
.keys()) {
438 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
446 public getEnergyActiveImportRegisterByTransactionId(
447 transactionId
: number,
450 return this.getEnergyActiveImportRegister(
451 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
456 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
457 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
);
460 public getAuthorizeRemoteTxRequests(): boolean {
461 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
463 StandardParametersKey
.AuthorizeRemoteTxRequests
465 return authorizeRemoteTxRequests
466 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
470 public getLocalAuthListEnabled(): boolean {
471 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
473 StandardParametersKey
.LocalAuthListEnabled
475 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
478 public getHeartbeatInterval(): number {
479 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
481 StandardParametersKey
.HeartbeatInterval
483 if (HeartbeatInterval
) {
484 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
486 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
488 StandardParametersKey
.HeartBeatInterval
490 if (HeartBeatInterval
) {
491 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
493 this.stationInfo
?.autoRegister
=== false &&
495 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
496 Constants.DEFAULT_HEARTBEAT_INTERVAL
499 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
502 public setSupervisionUrl(url
: string): void {
504 this.getSupervisionUrlOcppConfiguration() &&
505 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey())
507 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
509 this.getSupervisionUrlOcppKey(),
513 this.stationInfo
.supervisionUrls
= url
;
514 this.saveStationInfo();
515 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
519 public startHeartbeat(): void {
520 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
521 this.heartbeatSetInterval
= setInterval(() => {
522 this.ocppRequestService
523 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
526 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
530 }, this.getHeartbeatInterval());
532 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
533 this.getHeartbeatInterval()
536 } else if (this.heartbeatSetInterval
) {
538 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
539 this.getHeartbeatInterval()
544 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
549 public restartHeartbeat(): void {
551 this.stopHeartbeat();
553 this.startHeartbeat();
556 public restartWebSocketPing(): void {
557 // Stop WebSocket ping
558 this.stopWebSocketPing();
559 // Start WebSocket ping
560 this.startWebSocketPing();
563 public startMeterValues(connectorId
: number, interval
: number): void {
564 if (connectorId
=== 0) {
566 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`
570 if (!this.getConnectorStatus(connectorId
)) {
572 `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}`
576 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
578 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
582 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
583 Utils
.isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
586 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
591 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(() => {
592 // FIXME: Implement OCPP version agnostic helpers
593 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
596 this.getConnectorStatus(connectorId
).transactionId
,
599 this.ocppRequestService
600 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
602 RequestCommand
.METER_VALUES
,
605 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
606 meterValue
: [meterValue
],
611 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
618 `${this.logPrefix()} Charging station ${
619 StandardParametersKey.MeterValueSampleInterval
620 } configuration set to ${interval}, not sending MeterValues`
625 public stopMeterValues(connectorId
: number) {
626 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
627 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
631 public start(): void {
632 if (this.started
=== false) {
633 if (this.starting
=== false) {
634 this.starting
= true;
635 if (this.getEnableStatistics() === true) {
636 this.performanceStatistics
?.start();
638 this.openWSConnection();
639 // Monitor charging station template file
640 this.templateFileWatcher
= FileUtils
.watchJsonFile(
642 FileType
.ChargingStationTemplate
,
645 (event
, filename
): void => {
646 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
649 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
651 } file have changed, reload`
653 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
657 this.stopAutomaticTransactionGenerator();
658 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
659 this.startAutomaticTransactionGenerator();
661 if (this.getEnableStatistics() === true) {
662 this.performanceStatistics
?.restart();
664 this.performanceStatistics
?.stop();
666 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
669 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
677 parentPort
?.postMessage(MessageChannelUtils
.buildStartedMessage(this));
678 this.starting
= false;
680 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
683 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
687 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
688 if (this.started
=== true) {
689 if (this.stopping
=== false) {
690 this.stopping
= true;
691 await this.stopMessageSequence(reason
);
692 this.closeWSConnection();
693 if (this.getEnableStatistics() === true) {
694 this.performanceStatistics
?.stop();
696 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
697 this.templateFileWatcher
?.close();
698 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
699 delete this.bootNotificationResponse
;
700 this.started
= false;
701 this.saveConfiguration();
702 parentPort
?.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
703 this.stopping
= false;
705 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
708 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
712 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
713 await this.stop(reason
);
714 await Utils
.sleep(this.stationInfo
.resetTime
);
719 public saveOcppConfiguration(): void {
720 if (this.getOcppPersistentConfiguration()) {
721 this.saveConfiguration();
725 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
726 return ChargingStationConfigurationUtils
.getConfigurationKey(
728 StandardParametersKey
.SupportedFeatureProfiles
729 )?.value
?.includes(featureProfile
);
732 public bufferMessage(message
: string): void {
733 this.messageBuffer
.add(message
);
736 public openWSConnection(
737 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
738 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
740 terminateOpened
: false,
743 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
744 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
745 if (this.started
=== false && this.starting
=== false) {
747 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
752 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
753 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
755 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
757 if (params
?.closeOpened
) {
758 this.closeWSConnection();
760 if (params
?.terminateOpened
) {
761 this.terminateWSConnection();
764 if (this.isWebSocketConnectionOpened() === true) {
766 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
772 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
775 this.wsConnection
= new WebSocket(
776 this.wsConnectionUrl
,
777 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
781 // Handle WebSocket message
782 this.wsConnection
.on(
784 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
786 // Handle WebSocket error
787 this.wsConnection
.on(
789 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
791 // Handle WebSocket close
792 this.wsConnection
.on(
794 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
796 // Handle WebSocket open
797 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
798 // Handle WebSocket ping
799 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
800 // Handle WebSocket pong
801 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
804 public closeWSConnection(): void {
805 if (this.isWebSocketConnectionOpened() === true) {
806 this.wsConnection
?.close();
807 this.wsConnection
= null;
811 public getAutomaticTransactionGeneratorConfiguration():
812 | AutomaticTransactionGeneratorConfiguration
814 const automaticTransactionGeneratorConfigurationFromFile
=
815 this.getConfigurationFromFile()?.automaticTransactionGenerator
;
816 if (automaticTransactionGeneratorConfigurationFromFile
) {
817 return automaticTransactionGeneratorConfigurationFromFile
;
819 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
822 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
823 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
824 if (Utils
.isNotEmptyArray(connectorIds
)) {
825 for (const connectorId
of connectorIds
) {
826 this.automaticTransactionGenerator
?.startConnector(connectorId
);
829 this.automaticTransactionGenerator
?.start();
831 this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
832 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
835 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
836 if (Utils
.isNotEmptyArray(connectorIds
)) {
837 for (const connectorId
of connectorIds
) {
838 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
841 this.automaticTransactionGenerator
?.stop();
843 this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
844 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
847 public async stopTransactionOnConnector(
849 reason
= StopTransactionReason
.NONE
850 ): Promise
<StopTransactionResponse
> {
851 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
853 this.getBeginEndMeterValues() === true &&
854 this.getOcppStrictCompliance() === true &&
855 this.getOutOfOrderEndMeterValues() === false
857 // FIXME: Implement OCPP version agnostic helpers
858 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
861 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
863 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
865 RequestCommand
.METER_VALUES
,
869 meterValue
: [transactionEndMeterValue
],
873 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
875 RequestCommand
.STOP_TRANSACTION
,
878 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
884 private flushMessageBuffer(): void {
885 if (this.messageBuffer
.size
> 0) {
886 for (const message
of this.messageBuffer
.values()) {
888 let commandName
: RequestCommand
;
889 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
890 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
892 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
893 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
895 this.wsConnection
?.send(message
);
896 isRequest
&& PerformanceStatistics
.endMeasure(commandName
, beginId
);
898 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
900 )} payload sent: ${message}`
902 this.messageBuffer
.delete(message
);
907 private getSupervisionUrlOcppConfiguration(): boolean {
908 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
911 private getSupervisionUrlOcppKey(): string {
912 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
915 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
916 let template
: ChargingStationTemplate
;
918 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
919 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
921 const measureId
= `${FileType.ChargingStationTemplate} read`;
922 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
923 template
= JSON
.parse(
924 fs
.readFileSync(this.templateFile
, 'utf8')
925 ) as ChargingStationTemplate
;
926 PerformanceStatistics
.endMeasure(measureId
, beginId
);
927 template
.templateHash
= crypto
928 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
929 .update(JSON
.stringify(template
))
931 this.sharedLRUCache
.setChargingStationTemplate(template
);
932 this.templateFileHash
= template
.templateHash
;
935 ErrorUtils
.handleFileException(
937 FileType
.ChargingStationTemplate
,
938 error
as NodeJS
.ErrnoException
,
945 private getStationInfoFromTemplate(): ChargingStationInfo
{
946 const stationTemplate
: ChargingStationTemplate
| undefined = this.getTemplateFromFile();
947 ChargingStationUtils
.checkTemplateFile(stationTemplate
, this.logPrefix(), this.templateFile
);
948 ChargingStationUtils
.warnTemplateKeysDeprecation(
953 if (stationTemplate
?.Connectors
) {
954 ChargingStationUtils
.checkConnectorsConfiguration(
960 const stationInfo
: ChargingStationInfo
=
961 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
962 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
963 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
967 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
968 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
969 if (Utils
.isNotEmptyArray(stationTemplate
?.power
)) {
970 stationTemplate
.power
= stationTemplate
.power
as number[];
971 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
972 stationInfo
.maximumPower
=
973 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
974 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
975 : stationTemplate
.power
[powerArrayRandomIndex
];
977 stationTemplate
.power
= stationTemplate
?.power
as number;
978 stationInfo
.maximumPower
=
979 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
980 ? stationTemplate
.power
* 1000
981 : stationTemplate
.power
;
983 stationInfo
.firmwareVersionPattern
=
984 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
986 Utils
.isNotEmptyString(stationInfo
.firmwareVersion
) &&
987 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
990 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
992 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
995 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1002 stationTemplate
?.firmwareUpgrade
?? {}
1004 stationInfo
.resetTime
= !Utils
.isNullOrUndefined(stationTemplate
?.resetTime
)
1005 ? stationTemplate
.resetTime
* 1000
1006 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1007 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1011 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
1012 let stationInfo
: ChargingStationInfo
| undefined;
1013 if (this.getStationInfoPersistentConfiguration()) {
1014 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1016 delete stationInfo
?.infoHash
;
1022 private getStationInfo(): ChargingStationInfo
{
1023 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1024 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
1026 // 1. charging station info from template
1027 // 2. charging station info from configuration file
1028 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1029 return stationInfoFromFile
;
1031 stationInfoFromFile
&&
1032 ChargingStationUtils
.propagateSerialNumber(
1033 this.getTemplateFromFile(),
1034 stationInfoFromFile
,
1035 stationInfoFromTemplate
1037 return stationInfoFromTemplate
;
1040 private saveStationInfo(): void {
1041 if (this.getStationInfoPersistentConfiguration()) {
1042 this.saveConfiguration();
1046 private getOcppPersistentConfiguration(): boolean {
1047 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1050 private getStationInfoPersistentConfiguration(): boolean {
1051 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1054 private handleUnsupportedVersion(version
: OCPPVersion
) {
1055 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1056 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1057 throw new BaseError(errorMsg
);
1060 private initialize(): void {
1061 const stationTemplate
= this.getTemplateFromFile();
1062 ChargingStationUtils
.checkTemplateFile(stationTemplate
, this.logPrefix(), this.templateFile
);
1063 this.configurationFile
= path
.join(
1064 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
1065 `${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json`
1067 const chargingStationConfiguration
= this.getConfigurationFromFile();
1068 const featureFlag
= false;
1071 (chargingStationConfiguration
?.connectorsStatus
|| chargingStationConfiguration
?.evsesStatus
)
1073 this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration
);
1075 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1077 this.stationInfo
= this.getStationInfo();
1079 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1080 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1081 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1083 const patternGroup
: number | undefined =
1084 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1085 this.stationInfo
.firmwareVersion
?.split('.').length
;
1086 const match
= this.stationInfo
?.firmwareVersion
1087 ?.match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1088 ?.slice(1, patternGroup
+ 1);
1089 const patchLevelIndex
= match
.length
- 1;
1090 match
[patchLevelIndex
] = (
1091 Utils
.convertToInt(match
[patchLevelIndex
]) +
1092 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
1094 this.stationInfo
.firmwareVersion
= match
?.join('.');
1096 this.saveStationInfo();
1097 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1098 if (this.getEnableStatistics() === true) {
1099 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1100 this.stationInfo
.hashId
,
1101 this.stationInfo
.chargingStationId
,
1102 this.configuredSupervisionUrl
1105 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
1108 this.powerDivider
= this.getPowerDivider();
1109 // OCPP configuration
1110 this.ocppConfiguration
= this.getOcppConfiguration();
1111 this.initializeOcppConfiguration();
1112 this.initializeOcppServices();
1113 if (this.stationInfo
?.autoRegister
=== true) {
1114 this.bootNotificationResponse
= {
1115 currentTime
: new Date(),
1116 interval
: this.getHeartbeatInterval() / 1000,
1117 status: RegistrationStatusEnumType
.ACCEPTED
,
1122 private initializeOcppServices(): void {
1123 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1124 switch (ocppVersion
) {
1125 case OCPPVersion
.VERSION_16
:
1126 this.ocppIncomingRequestService
=
1127 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1128 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1129 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1132 case OCPPVersion
.VERSION_20
:
1133 case OCPPVersion
.VERSION_201
:
1134 this.ocppIncomingRequestService
=
1135 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1136 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1137 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1141 this.handleUnsupportedVersion(ocppVersion
);
1146 private initializeOcppConfiguration(): void {
1148 !ChargingStationConfigurationUtils
.getConfigurationKey(
1150 StandardParametersKey
.HeartbeatInterval
1153 ChargingStationConfigurationUtils
.addConfigurationKey(
1155 StandardParametersKey
.HeartbeatInterval
,
1160 !ChargingStationConfigurationUtils
.getConfigurationKey(
1162 StandardParametersKey
.HeartBeatInterval
1165 ChargingStationConfigurationUtils
.addConfigurationKey(
1167 StandardParametersKey
.HeartBeatInterval
,
1173 this.getSupervisionUrlOcppConfiguration() &&
1174 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1175 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1177 ChargingStationConfigurationUtils
.addConfigurationKey(
1179 this.getSupervisionUrlOcppKey(),
1180 this.configuredSupervisionUrl
.href
,
1184 !this.getSupervisionUrlOcppConfiguration() &&
1185 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1186 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1188 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1190 this.getSupervisionUrlOcppKey(),
1195 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1196 !ChargingStationConfigurationUtils
.getConfigurationKey(
1198 this.stationInfo
.amperageLimitationOcppKey
1201 ChargingStationConfigurationUtils
.addConfigurationKey(
1203 this.stationInfo
.amperageLimitationOcppKey
,
1205 this.stationInfo
.maximumAmperage
*
1206 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1211 !ChargingStationConfigurationUtils
.getConfigurationKey(
1213 StandardParametersKey
.SupportedFeatureProfiles
1216 ChargingStationConfigurationUtils
.addConfigurationKey(
1218 StandardParametersKey
.SupportedFeatureProfiles
,
1219 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1222 ChargingStationConfigurationUtils
.addConfigurationKey(
1224 StandardParametersKey
.NumberOfConnectors
,
1225 this.getNumberOfConnectors().toString(),
1230 !ChargingStationConfigurationUtils
.getConfigurationKey(
1232 StandardParametersKey
.MeterValuesSampledData
1235 ChargingStationConfigurationUtils
.addConfigurationKey(
1237 StandardParametersKey
.MeterValuesSampledData
,
1238 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1242 !ChargingStationConfigurationUtils
.getConfigurationKey(
1244 StandardParametersKey
.ConnectorPhaseRotation
1247 const connectorsPhaseRotation
: string[] = [];
1248 if (this.hasEvses
) {
1249 for (const evseStatus
of this.evses
.values()) {
1250 for (const connectorId
of evseStatus
.connectors
.keys()) {
1251 connectorsPhaseRotation
.push(
1252 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1257 for (const connectorId
of this.connectors
.keys()) {
1258 connectorsPhaseRotation
.push(
1259 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1263 ChargingStationConfigurationUtils
.addConfigurationKey(
1265 StandardParametersKey
.ConnectorPhaseRotation
,
1266 connectorsPhaseRotation
.toString()
1270 !ChargingStationConfigurationUtils
.getConfigurationKey(
1272 StandardParametersKey
.AuthorizeRemoteTxRequests
1275 ChargingStationConfigurationUtils
.addConfigurationKey(
1277 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1282 !ChargingStationConfigurationUtils
.getConfigurationKey(
1284 StandardParametersKey
.LocalAuthListEnabled
1286 ChargingStationConfigurationUtils
.getConfigurationKey(
1288 StandardParametersKey
.SupportedFeatureProfiles
1289 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1291 ChargingStationConfigurationUtils
.addConfigurationKey(
1293 StandardParametersKey
.LocalAuthListEnabled
,
1298 !ChargingStationConfigurationUtils
.getConfigurationKey(
1300 StandardParametersKey
.ConnectionTimeOut
1303 ChargingStationConfigurationUtils
.addConfigurationKey(
1305 StandardParametersKey
.ConnectionTimeOut
,
1306 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1309 this.saveOcppConfiguration();
1312 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1313 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1314 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1315 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1317 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1318 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1319 const evseStatus
= Utils
.cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1320 delete evseStatus
.connectorsStatus
;
1321 this.evses
.set(evseId
, {
1322 ...(evseStatus
as EvseStatus
),
1323 connectors
: new Map
<number, ConnectorStatus
>(
1324 evseStatusConfiguration
.connectorsStatus
.map((connectorStatus
, connectorId
) => [
1331 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1332 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1333 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1334 throw new BaseError(errorMsg
);
1336 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1337 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1338 throw new BaseError(errorMsg
);
1342 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1343 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1344 this.initializeConnectorsFromTemplate(stationTemplate
);
1345 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1346 this.initializeEvsesFromTemplate(stationTemplate
);
1347 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1348 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1349 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1350 throw new BaseError(errorMsg
);
1352 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1353 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1354 throw new BaseError(errorMsg
);
1358 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1359 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1360 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1361 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1362 throw new BaseError(errorMsg
);
1364 if (!stationTemplate
?.Connectors
[0]) {
1366 `${this.logPrefix()} Charging station information from template ${
1368 } with no connector id 0 configuration`
1371 if (stationTemplate
?.Connectors
) {
1372 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1373 ChargingStationUtils
.checkConnectorsConfiguration(
1378 const connectorsConfigHash
= crypto
1379 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1381 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`
1384 const connectorsConfigChanged
=
1385 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1386 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1387 connectorsConfigChanged
&& this.connectors
.clear();
1388 this.connectorsConfigurationHash
= connectorsConfigHash
;
1389 if (templateMaxConnectors
> 0) {
1390 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1392 connectorId
=== 0 &&
1393 (!stationTemplate
?.Connectors
[connectorId
] ||
1394 this.getUseConnectorId0(stationTemplate
) === false)
1398 const templateConnectorId
=
1399 connectorId
> 0 && stationTemplate
?.randomConnectors
1400 ? Utils
.getRandomInteger(templateMaxAvailableConnectors
, 1)
1402 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1403 ChargingStationUtils
.checkStationInfoConnectorStatus(
1404 templateConnectorId
,
1409 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1411 ChargingStationUtils
.initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1412 this.saveConnectorsStatus();
1415 `${this.logPrefix()} Charging station information from template ${
1417 } with no connectors configuration defined, cannot create connectors`
1423 `${this.logPrefix()} Charging station information from template ${
1425 } with no connectors configuration defined, using already defined connectors`
1430 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1431 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1432 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1433 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1434 throw new BaseError(errorMsg
);
1436 if (!stationTemplate
?.Evses
[0]) {
1438 `${this.logPrefix()} Charging station information from template ${
1440 } with no evse id 0 configuration`
1443 if (!stationTemplate
?.Evses
[0]?.Connectors
[0]) {
1445 `${this.logPrefix()} Charging station information from template ${
1447 } with evse id 0 with no connector id 0 configuration`
1450 if (stationTemplate
?.Evses
) {
1451 const evsesConfigHash
= crypto
1452 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1453 .update(`${JSON.stringify(stationTemplate?.Evses)}`)
1455 const evsesConfigChanged
=
1456 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1457 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1458 evsesConfigChanged
&& this.evses
.clear();
1459 this.evsesConfigurationHash
= evsesConfigHash
;
1460 const templateMaxEvses
= ChargingStationUtils
.getMaxNumberOfEvses(stationTemplate
?.Evses
);
1461 if (templateMaxEvses
> 0) {
1462 for (const evse
in stationTemplate
.Evses
) {
1463 const evseId
= Utils
.convertToInt(evse
);
1464 this.evses
.set(evseId
, {
1465 connectors
: ChargingStationUtils
.buildConnectorsMap(
1466 stationTemplate
?.Evses
[evse
]?.Connectors
,
1470 availability
: AvailabilityType
.Operative
,
1472 ChargingStationUtils
.initializeConnectorsMapStatus(
1473 this.evses
.get(evseId
)?.connectors
,
1477 this.saveEvsesStatus();
1480 `${this.logPrefix()} Charging station information from template ${
1482 } with no evses configuration defined, cannot create evses`
1488 `${this.logPrefix()} Charging station information from template ${
1490 } with no evses configuration defined, using already defined evses`
1495 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1496 let configuration
: ChargingStationConfiguration
| undefined;
1497 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1499 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1500 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1501 this.configurationFileHash
1504 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1505 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1506 configuration
= JSON
.parse(
1507 fs
.readFileSync(this.configurationFile
, 'utf8')
1508 ) as ChargingStationConfiguration
;
1509 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1510 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1511 this.configurationFileHash
= configuration
.configurationHash
;
1514 ErrorUtils
.handleFileException(
1515 this.configurationFile
,
1516 FileType
.ChargingStationConfiguration
,
1517 error
as NodeJS
.ErrnoException
,
1522 return configuration
;
1525 private saveChargingStationAutomaticTransactionGeneratorConfiguration(
1526 stationTemplate
?: ChargingStationTemplate
1528 this.saveConfiguration({
1529 automaticTransactionGenerator
: (stationTemplate
?? this.getTemplateFromFile())
1530 .AutomaticTransactionGenerator
,
1531 ...(!Utils
.isNullOrUndefined(this.automaticTransactionGenerator
?.connectorsStatus
) && {
1532 automaticTransactionGeneratorStatuses
: [
1533 ...this.automaticTransactionGenerator
.connectorsStatus
.values(),
1539 private saveConnectorsStatus() {
1540 this.saveConfiguration();
1543 private saveEvsesStatus() {
1544 this.saveConfiguration();
1547 private saveConfiguration(
1548 chargingStationAutomaticTransactionGeneratorConfiguration
?: ChargingStationAutomaticTransactionGeneratorConfiguration
1550 if (this.configurationFile
) {
1552 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1553 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1555 let configurationData
: ChargingStationConfiguration
=
1556 Utils
.cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()) ?? {};
1557 if (this.getStationInfoPersistentConfiguration() && this.stationInfo
) {
1558 configurationData
.stationInfo
= this.stationInfo
;
1560 if (this.getOcppPersistentConfiguration() && this.ocppConfiguration
?.configurationKey
) {
1561 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1563 if (chargingStationAutomaticTransactionGeneratorConfiguration
) {
1564 configurationData
= merge
<ChargingStationConfiguration
>(
1566 chargingStationAutomaticTransactionGeneratorConfiguration
1569 if (this.connectors
.size
> 0) {
1570 configurationData
.connectorsStatus
= [...this.connectors
.values()].map(
1571 // eslint-disable-next-line @typescript-eslint/no-unused-vars
1572 ({ transactionSetInterval
, ...connectorStatusRest
}) => connectorStatusRest
1575 if (this.evses
.size
> 0) {
1576 configurationData
.evsesStatus
= [...this.evses
.values()].map((evseStatus
) => {
1579 connectorsStatus
: [...evseStatus
.connectors
.values()].map(
1580 // eslint-disable-next-line @typescript-eslint/no-unused-vars
1581 ({ transactionSetInterval
, ...connectorStatusRest
}) => connectorStatusRest
1584 delete status.connectors
;
1585 return status as EvseStatusConfiguration
;
1588 delete configurationData
.configurationHash
;
1589 const configurationHash
= crypto
1590 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1591 .update(JSON
.stringify(configurationData
))
1593 if (this.configurationFileHash
!== configurationHash
) {
1594 AsyncLock
.acquire(AsyncLockType
.configuration
)
1596 configurationData
.configurationHash
= configurationHash
;
1597 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1598 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1599 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1600 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1601 fs
.closeSync(fileDescriptor
);
1602 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1603 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1604 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1605 this.configurationFileHash
= configurationHash
;
1608 ErrorUtils
.handleFileException(
1609 this.configurationFile
,
1610 FileType
.ChargingStationConfiguration
,
1611 error
as NodeJS
.ErrnoException
,
1616 AsyncLock
.release(AsyncLockType
.configuration
).catch(Constants
.EMPTY_FUNCTION
);
1620 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1621 this.configurationFile
1626 ErrorUtils
.handleFileException(
1627 this.configurationFile
,
1628 FileType
.ChargingStationConfiguration
,
1629 error
as NodeJS
.ErrnoException
,
1635 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1640 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1641 return this.getTemplateFromFile()?.Configuration
;
1644 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1645 if (this.getOcppPersistentConfiguration() === true) {
1646 return { configurationKey
: this.getConfigurationFromFile()?.configurationKey
};
1650 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1651 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1652 this.getOcppConfigurationFromFile();
1653 if (!ocppConfiguration
) {
1654 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1656 return ocppConfiguration
;
1659 private async onOpen(): Promise
<void> {
1660 if (this.isWebSocketConnectionOpened() === true) {
1662 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1664 if (this.isRegistered() === false) {
1665 // Send BootNotification
1666 let registrationRetryCount
= 0;
1668 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1669 BootNotificationRequest
,
1670 BootNotificationResponse
1671 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1672 skipBufferingOnError
: true,
1674 if (this.isRegistered() === false) {
1675 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1677 this?.bootNotificationResponse
?.interval
1678 ? this.bootNotificationResponse
.interval
* 1000
1679 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1683 this.isRegistered() === false &&
1684 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1685 this.getRegistrationMaxRetries() === -1)
1688 if (this.isRegistered() === true) {
1689 if (this.inAcceptedState() === true) {
1690 await this.startMessageSequence();
1694 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1697 this.wsConnectionRestarted
= false;
1698 this.autoReconnectRetryCount
= 0;
1699 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1702 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1707 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1710 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1711 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1713 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1715 )}' and reason '${reason.toString()}'`
1717 this.autoReconnectRetryCount
= 0;
1722 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1724 )}' and reason '${reason.toString()}'`
1726 this.started
=== true && (await this.reconnect());
1729 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1732 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1733 const cachedRequest
= this.requests
.get(messageId
);
1734 if (Array.isArray(cachedRequest
) === true) {
1735 return cachedRequest
;
1737 throw new OCPPError(
1738 ErrorType
.PROTOCOL_ERROR
,
1739 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1741 )} is not an array`,
1743 cachedRequest
as JsonType
1747 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1748 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1749 if (this.getEnableStatistics() === true) {
1750 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1753 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1757 // Process the message
1758 await this.ocppIncomingRequestService
.incomingRequestHandler(
1766 private handleResponseMessage(response
: Response
): void {
1767 const [messageType
, messageId
, commandPayload
] = response
;
1768 if (this.requests
.has(messageId
) === false) {
1770 throw new OCPPError(
1771 ErrorType
.INTERNAL_ERROR
,
1772 `Response for unknown message id ${messageId}`,
1778 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1783 `${this.logPrefix()} << Command '${
1784 requestCommandName ?? Constants.UNKNOWN_COMMAND
1785 }' received response payload: ${JSON.stringify(response)}`
1787 responseCallback(commandPayload
, requestPayload
);
1790 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1791 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1792 if (this.requests
.has(messageId
) === false) {
1794 throw new OCPPError(
1795 ErrorType
.INTERNAL_ERROR
,
1796 `Error response for unknown message id ${messageId}`,
1798 { errorType
, errorMessage
, errorDetails
}
1801 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1803 `${this.logPrefix()} << Command '${
1804 requestCommandName ?? Constants.UNKNOWN_COMMAND
1805 }' received error response payload: ${JSON.stringify(errorResponse)}`
1807 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1810 private async onMessage(data
: RawData
): Promise
<void> {
1811 let request
: IncomingRequest
| Response
| ErrorResponse
;
1812 let messageType
: number;
1813 let errorMsg
: string;
1815 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1816 if (Array.isArray(request
) === true) {
1817 [messageType
] = request
;
1818 // Check the type of message
1819 switch (messageType
) {
1821 case MessageType
.CALL_MESSAGE
:
1822 await this.handleIncomingMessage(request
as IncomingRequest
);
1825 case MessageType
.CALL_RESULT_MESSAGE
:
1826 this.handleResponseMessage(request
as Response
);
1829 case MessageType
.CALL_ERROR_MESSAGE
:
1830 this.handleErrorMessage(request
as ErrorResponse
);
1834 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1835 errorMsg
= `Wrong message type ${messageType}`;
1836 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1837 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1839 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1841 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1846 let commandName
: IncomingRequestCommand
;
1847 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1848 let errorCallback
: ErrorCallback
;
1849 const [, messageId
] = request
;
1850 switch (messageType
) {
1851 case MessageType
.CALL_MESSAGE
:
1852 [, , commandName
] = request
as IncomingRequest
;
1854 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1856 case MessageType
.CALL_RESULT_MESSAGE
:
1857 case MessageType
.CALL_ERROR_MESSAGE
:
1858 if (this.requests
.has(messageId
) === true) {
1859 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1860 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1861 errorCallback(error
as OCPPError
, false);
1863 // Remove the request from the cache in case of error at response handling
1864 this.requests
.delete(messageId
);
1868 if (error
instanceof OCPPError
=== false) {
1870 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1871 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1872 }' message '${data.toString()}' handling is not an OCPPError:`,
1877 `${this.logPrefix()} Incoming OCPP command '${
1878 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1879 }' message '${data.toString()}'${
1880 messageType !== MessageType.CALL_MESSAGE
1881 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1883 } processing error:`,
1889 private onPing(): void {
1890 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1893 private onPong(): void {
1894 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1897 private onError(error
: WSError
): void {
1898 this.closeWSConnection();
1899 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1902 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1903 if (this.getMeteringPerTransaction() === true) {
1906 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1907 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1912 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1913 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1917 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
1918 return stationTemplate
?.useConnectorId0
?? true;
1921 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1922 if (this.hasEvses
) {
1923 for (const evseStatus
of this.evses
.values()) {
1924 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1925 if (connectorStatus
.transactionStarted
=== true) {
1926 await this.stopTransactionOnConnector(connectorId
, reason
);
1931 for (const connectorId
of this.connectors
.keys()) {
1932 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1933 await this.stopTransactionOnConnector(connectorId
, reason
);
1940 private getConnectionTimeout(): number {
1942 ChargingStationConfigurationUtils
.getConfigurationKey(
1944 StandardParametersKey
.ConnectionTimeOut
1949 ChargingStationConfigurationUtils
.getConfigurationKey(
1951 StandardParametersKey
.ConnectionTimeOut
1953 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1956 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1959 // -1 for unlimited, 0 for disabling
1960 private getAutoReconnectMaxRetries(): number | undefined {
1962 this.stationInfo
.autoReconnectMaxRetries
?? Configuration
.getAutoReconnectMaxRetries() ?? -1
1967 private getRegistrationMaxRetries(): number | undefined {
1968 return this.stationInfo
.registrationMaxRetries
?? -1;
1971 private getPowerDivider(): number {
1972 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
1973 if (this.stationInfo
?.powerSharedByConnectors
) {
1974 powerDivider
= this.getNumberOfRunningTransactions();
1976 return powerDivider
;
1979 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1980 const maximumPower
= this.getMaximumPower(stationInfo
);
1981 switch (this.getCurrentOutType(stationInfo
)) {
1982 case CurrentType
.AC
:
1983 return ACElectricUtils
.amperagePerPhaseFromPower(
1984 this.getNumberOfPhases(stationInfo
),
1985 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
1986 this.getVoltageOut(stationInfo
)
1988 case CurrentType
.DC
:
1989 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1993 private getAmperageLimitation(): number | undefined {
1995 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1996 ChargingStationConfigurationUtils
.getConfigurationKey(
1998 this.stationInfo
.amperageLimitationOcppKey
2003 ChargingStationConfigurationUtils
.getConfigurationKey(
2005 this.stationInfo
.amperageLimitationOcppKey
2007 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
2012 private async startMessageSequence(): Promise
<void> {
2013 if (this.stationInfo
?.autoRegister
=== true) {
2014 await this.ocppRequestService
.requestHandler
<
2015 BootNotificationRequest
,
2016 BootNotificationResponse
2017 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2018 skipBufferingOnError
: true,
2021 // Start WebSocket ping
2022 this.startWebSocketPing();
2024 this.startHeartbeat();
2025 // Initialize connectors status
2026 if (this.hasEvses
) {
2027 for (const [evseId
, evseStatus
] of this.evses
) {
2029 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2030 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2035 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2038 connectorBootStatus
,
2045 for (const connectorId
of this.connectors
.keys()) {
2046 if (connectorId
> 0) {
2047 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2050 this.getConnectorStatus(connectorId
)
2052 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2056 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2057 await this.ocppRequestService
.requestHandler
<
2058 FirmwareStatusNotificationRequest
,
2059 FirmwareStatusNotificationResponse
2060 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2061 status: FirmwareStatus
.Installed
,
2063 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2067 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2068 this.startAutomaticTransactionGenerator();
2070 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2073 private async stopMessageSequence(
2074 reason
: StopTransactionReason
= StopTransactionReason
.NONE
2076 // Stop WebSocket ping
2077 this.stopWebSocketPing();
2079 this.stopHeartbeat();
2080 // Stop ongoing transactions
2081 if (this.automaticTransactionGenerator
?.started
=== true) {
2082 this.stopAutomaticTransactionGenerator();
2084 await this.stopRunningTransactions(reason
);
2086 if (this.hasEvses
) {
2087 for (const [evseId
, evseStatus
] of this.evses
) {
2089 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2090 await this.ocppRequestService
.requestHandler
<
2091 StatusNotificationRequest
,
2092 StatusNotificationResponse
2095 RequestCommand
.STATUS_NOTIFICATION
,
2096 OCPPServiceUtils
.buildStatusNotificationRequest(
2099 ConnectorStatusEnum
.Unavailable
,
2103 delete connectorStatus
?.status;
2108 for (const connectorId
of this.connectors
.keys()) {
2109 if (connectorId
> 0) {
2110 await this.ocppRequestService
.requestHandler
<
2111 StatusNotificationRequest
,
2112 StatusNotificationResponse
2115 RequestCommand
.STATUS_NOTIFICATION
,
2116 OCPPServiceUtils
.buildStatusNotificationRequest(
2119 ConnectorStatusEnum
.Unavailable
2122 delete this.getConnectorStatus(connectorId
)?.status;
2128 private startWebSocketPing(): void {
2129 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
2131 StandardParametersKey
.WebSocketPingInterval
2133 ? Utils
.convertToInt(
2134 ChargingStationConfigurationUtils
.getConfigurationKey(
2136 StandardParametersKey
.WebSocketPingInterval
2140 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2141 this.webSocketPingSetInterval
= setInterval(() => {
2142 if (this.isWebSocketConnectionOpened() === true) {
2143 this.wsConnection
?.ping();
2145 }, webSocketPingInterval
* 1000);
2147 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
2148 webSocketPingInterval
2151 } else if (this.webSocketPingSetInterval
) {
2153 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
2154 webSocketPingInterval
2159 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2164 private stopWebSocketPing(): void {
2165 if (this.webSocketPingSetInterval
) {
2166 clearInterval(this.webSocketPingSetInterval
);
2167 delete this.webSocketPingSetInterval
;
2171 private getConfiguredSupervisionUrl(): URL
{
2172 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2173 if (Utils
.isNotEmptyArray(supervisionUrls
)) {
2174 let configuredSupervisionUrlIndex
: number;
2175 switch (Configuration
.getSupervisionUrlDistribution()) {
2176 case SupervisionUrlDistribution
.RANDOM
:
2177 configuredSupervisionUrlIndex
= Math.floor(Utils
.secureRandom() * supervisionUrls
.length
);
2179 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2180 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2182 Object.values(SupervisionUrlDistribution
).includes(
2183 Configuration
.getSupervisionUrlDistribution()
2186 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2187 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2190 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
2193 return new URL(supervisionUrls
[configuredSupervisionUrlIndex
]);
2195 return new URL(supervisionUrls
as string);
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()})`