1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import crypto from
'node:crypto';
4 import fs from
'node:fs';
5 import path from
'node:path';
6 import { URL
} from
'node:url';
7 import { parentPort
} from
'node:worker_threads';
9 import merge from
'just-merge';
10 import WebSocket
, { type RawData
} from
'ws';
13 AutomaticTransactionGenerator
,
14 ChargingStationConfigurationUtils
,
16 ChargingStationWorkerBroadcastChannel
,
22 // OCPP16IncomingRequestService,
24 // OCPP16ResponseService,
26 OCPP20IncomingRequestService
,
28 // OCPP20ResponseService,
29 type OCPPIncomingRequestService
,
30 type OCPPRequestService
,
33 import { OCPP16IncomingRequestService
} from
'./ocpp/1.6/OCPP16IncomingRequestService';
34 import { OCPP16ResponseService
} from
'./ocpp/1.6/OCPP16ResponseService';
35 import { OCPP20ResponseService
} from
'./ocpp/2.0/OCPP20ResponseService';
36 import { OCPPServiceUtils
} from
'./ocpp/OCPPServiceUtils';
37 import { BaseError
, OCPPError
} from
'../exception';
38 import { PerformanceStatistics
} from
'../performance';
40 type AutomaticTransactionGeneratorConfiguration
,
42 type BootNotificationRequest
,
43 type BootNotificationResponse
,
45 type ChargingStationConfiguration
,
46 type ChargingStationInfo
,
47 type ChargingStationOcppConfiguration
,
48 type ChargingStationTemplate
,
56 type EvseStatusConfiguration
,
59 type FirmwareStatusNotificationRequest
,
60 type FirmwareStatusNotificationResponse
,
62 type HeartbeatRequest
,
63 type HeartbeatResponse
,
65 type IncomingRequestCommand
,
70 type MeterValuesRequest
,
71 type MeterValuesResponse
,
75 RegistrationStatusEnumType
,
78 StandardParametersKey
,
79 type StatusNotificationRequest
,
80 type StatusNotificationResponse
,
81 StopTransactionReason
,
82 type StopTransactionRequest
,
83 type StopTransactionResponse
,
84 SupervisionUrlDistribution
,
85 SupportedFeatureProfiles
,
88 WebSocketCloseEventStatusCode
,
101 export class ChargingStation
{
102 public readonly index
: number;
103 public readonly templateFile
: string;
104 public stationInfo
!: ChargingStationInfo
;
105 public started
: boolean;
106 public starting
: boolean;
107 public idTagsCache
: IdTagsCache
;
108 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
109 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
110 public wsConnection
!: WebSocket
| null;
111 public readonly connectors
: Map
<number, ConnectorStatus
>;
112 public readonly evses
: Map
<number, EvseStatus
>;
113 public readonly requests
: Map
<string, CachedRequest
>;
114 public performanceStatistics
!: PerformanceStatistics
| undefined;
115 public heartbeatSetInterval
!: NodeJS
.Timeout
;
116 public ocppRequestService
!: OCPPRequestService
;
117 public bootNotificationRequest
!: BootNotificationRequest
;
118 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
119 public powerDivider
!: number;
120 private stopping
: boolean;
121 private configurationFile
!: string;
122 private configurationFileHash
!: string;
123 private connectorsConfigurationHash
!: string;
124 private evsesConfigurationHash
!: string;
125 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
126 private readonly messageBuffer
: Set
<string>;
127 private configuredSupervisionUrl
!: URL
;
128 private wsConnectionRestarted
: boolean;
129 private autoReconnectRetryCount
: number;
130 private templateFileWatcher
!: fs
.FSWatcher
| undefined;
131 private readonly sharedLRUCache
: SharedLRUCache
;
132 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
133 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
135 constructor(index
: number, templateFile
: string) {
136 this.started
= false;
137 this.starting
= false;
138 this.stopping
= false;
139 this.wsConnectionRestarted
= false;
140 this.autoReconnectRetryCount
= 0;
142 this.templateFile
= templateFile
;
143 this.connectors
= new Map
<number, ConnectorStatus
>();
144 this.evses
= new Map
<number, EvseStatus
>();
145 this.requests
= new Map
<string, CachedRequest
>();
146 this.messageBuffer
= new Set
<string>();
147 this.sharedLRUCache
= SharedLRUCache
.getInstance();
148 this.idTagsCache
= IdTagsCache
.getInstance();
149 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
154 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 const idTagsFile
= ChargingStationUtils
.getIdTagsFile(this.stationInfo
);
185 return Utils
.isNotEmptyArray(this.idTagsCache
.getIdTags(idTagsFile
));
188 public getEnableStatistics(): boolean {
189 return this.stationInfo
.enableStatistics
?? false;
192 public getMustAuthorizeAtRemoteStart(): boolean {
193 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
196 public getPayloadSchemaValidation(): boolean {
197 return this.stationInfo
.payloadSchemaValidation
?? true;
200 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
201 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
202 switch (this.getCurrentOutType(stationInfo
)) {
204 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
205 ? localStationInfo
.numberOfPhases
212 public isWebSocketConnectionOpened(): boolean {
213 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
216 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
217 return this?.bootNotificationResponse
?.status;
220 public isInUnknownState(): boolean {
221 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
224 public isInPendingState(): boolean {
225 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
228 public isInAcceptedState(): boolean {
229 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
232 public isInRejectedState(): boolean {
233 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
236 public isRegistered(): boolean {
238 this.isInUnknownState() === false &&
239 (this.isInAcceptedState() === true || this.isInPendingState() === true)
243 public isChargingStationAvailable(): boolean {
244 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
247 public hasConnector(connectorId
: number): boolean {
249 for (const evseStatus
of this.evses
.values()) {
250 if (evseStatus
.connectors
.has(connectorId
)) {
256 return this.connectors
.has(connectorId
);
259 public isConnectorAvailable(connectorId
: number): boolean {
262 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
266 public getNumberOfConnectors(): number {
268 let numberOfConnectors
= 0;
269 for (const [evseId
, evseStatus
] of this.evses
) {
271 numberOfConnectors
+= evseStatus
.connectors
.size
;
274 return numberOfConnectors
;
276 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
279 public getNumberOfEvses(): number {
280 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
283 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
285 for (const evseStatus
of this.evses
.values()) {
286 if (evseStatus
.connectors
.has(connectorId
)) {
287 return evseStatus
.connectors
.get(connectorId
);
292 return this.connectors
.get(connectorId
);
295 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
296 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
299 public getOcppStrictCompliance(): boolean {
300 return this.stationInfo
?.ocppStrictCompliance
?? false;
303 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
304 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
305 this.getCurrentOutType(stationInfo
),
309 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
310 return !Utils
.isUndefined(localStationInfo
.voltageOut
)
311 ? localStationInfo
.voltageOut
315 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
316 const localStationInfo
= stationInfo
?? this.stationInfo
;
317 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
320 public getConnectorMaximumAvailablePower(connectorId
: number): number {
321 let connectorAmperageLimitationPowerLimit
: number;
323 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
324 this.getAmperageLimitation() < this.stationInfo
?.maximumAmperage
326 connectorAmperageLimitationPowerLimit
=
327 (this.getCurrentOutType() === CurrentType
.AC
328 ? ACElectricUtils
.powerTotal(
329 this.getNumberOfPhases(),
330 this.getVoltageOut(),
331 this.getAmperageLimitation() * this.getNumberOfConnectors()
333 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
336 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
337 const connectorChargingProfilesPowerLimit
=
338 ChargingStationUtils
.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
340 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
341 isNaN(connectorAmperageLimitationPowerLimit
)
343 : connectorAmperageLimitationPowerLimit
,
344 isNaN(connectorChargingProfilesPowerLimit
) ? Infinity : connectorChargingProfilesPowerLimit
348 public getTransactionIdTag(transactionId
: number): string | undefined {
350 for (const evseStatus
of this.evses
.values()) {
351 for (const connectorStatus
of evseStatus
.connectors
.values()) {
352 if (connectorStatus
.transactionId
=== transactionId
) {
353 return connectorStatus
.transactionIdTag
;
358 for (const connectorId
of this.connectors
.keys()) {
361 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
363 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
369 public getOutOfOrderEndMeterValues(): boolean {
370 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
373 public getBeginEndMeterValues(): boolean {
374 return this.stationInfo
?.beginEndMeterValues
?? false;
377 public getMeteringPerTransaction(): boolean {
378 return this.stationInfo
?.meteringPerTransaction
?? true;
381 public getTransactionDataMeterValues(): boolean {
382 return this.stationInfo
?.transactionDataMeterValues
?? false;
385 public getMainVoltageMeterValues(): boolean {
386 return this.stationInfo
?.mainVoltageMeterValues
?? true;
389 public getPhaseLineToLineVoltageMeterValues(): boolean {
390 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
393 public getCustomValueLimitationMeterValues(): boolean {
394 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
397 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
399 for (const evseStatus
of this.evses
.values()) {
400 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
401 if (connectorStatus
.transactionId
=== transactionId
) {
407 for (const connectorId
of this.connectors
.keys()) {
410 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
418 public getEnergyActiveImportRegisterByTransactionId(
419 transactionId
: number,
422 return this.getEnergyActiveImportRegister(
423 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
428 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
429 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
);
432 public getAuthorizeRemoteTxRequests(): boolean {
433 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
435 StandardParametersKey
.AuthorizeRemoteTxRequests
437 return authorizeRemoteTxRequests
438 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
442 public getLocalAuthListEnabled(): boolean {
443 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
445 StandardParametersKey
.LocalAuthListEnabled
447 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
450 public getHeartbeatInterval(): number {
451 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
453 StandardParametersKey
.HeartbeatInterval
455 if (HeartbeatInterval
) {
456 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
458 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
460 StandardParametersKey
.HeartBeatInterval
462 if (HeartBeatInterval
) {
463 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
465 this.stationInfo
?.autoRegister
=== false &&
467 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
468 Constants.DEFAULT_HEARTBEAT_INTERVAL
471 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
474 public setSupervisionUrl(url
: string): void {
476 this.getSupervisionUrlOcppConfiguration() &&
477 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey())
479 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
481 this.getSupervisionUrlOcppKey(),
485 this.stationInfo
.supervisionUrls
= url
;
486 this.saveStationInfo();
487 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
491 public startHeartbeat(): void {
492 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
493 this.heartbeatSetInterval
= setInterval(() => {
494 this.ocppRequestService
495 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
498 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
502 }, this.getHeartbeatInterval());
504 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
505 this.getHeartbeatInterval()
508 } else if (this.heartbeatSetInterval
) {
510 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
511 this.getHeartbeatInterval()
516 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
521 public restartHeartbeat(): void {
523 this.stopHeartbeat();
525 this.startHeartbeat();
528 public restartWebSocketPing(): void {
529 // Stop WebSocket ping
530 this.stopWebSocketPing();
531 // Start WebSocket ping
532 this.startWebSocketPing();
535 public startMeterValues(connectorId
: number, interval
: number): void {
536 if (connectorId
=== 0) {
538 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`
542 if (!this.getConnectorStatus(connectorId
)) {
544 `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}`
548 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
550 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
554 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
555 Utils
.isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
558 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
563 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(() => {
564 // FIXME: Implement OCPP version agnostic helpers
565 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
568 this.getConnectorStatus(connectorId
).transactionId
,
571 this.ocppRequestService
572 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
574 RequestCommand
.METER_VALUES
,
577 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
578 meterValue
: [meterValue
],
583 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
590 `${this.logPrefix()} Charging station ${
591 StandardParametersKey.MeterValueSampleInterval
592 } configuration set to ${interval}, not sending MeterValues`
597 public stopMeterValues(connectorId
: number) {
598 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
599 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
603 public start(): void {
604 if (this.started
=== false) {
605 if (this.starting
=== false) {
606 this.starting
= true;
607 if (this.getEnableStatistics() === true) {
608 this.performanceStatistics
?.start();
610 this.openWSConnection();
611 // Monitor charging station template file
612 this.templateFileWatcher
= FileUtils
.watchJsonFile(
614 FileType
.ChargingStationTemplate
,
617 (event
, filename
): void => {
618 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
621 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
623 } file have changed, reload`
625 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
629 this.stopAutomaticTransactionGenerator();
631 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true
633 this.startAutomaticTransactionGenerator();
635 if (this.getEnableStatistics() === true) {
636 this.performanceStatistics
?.restart();
638 this.performanceStatistics
?.stop();
640 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
643 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
651 parentPort
?.postMessage(MessageChannelUtils
.buildStartedMessage(this));
652 this.starting
= false;
654 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
657 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
661 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
662 if (this.started
=== true) {
663 if (this.stopping
=== false) {
664 this.stopping
= true;
665 await this.stopMessageSequence(reason
);
666 this.closeWSConnection();
667 if (this.getEnableStatistics() === true) {
668 this.performanceStatistics
?.stop();
670 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
671 this.templateFileWatcher
?.close();
672 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
673 delete this.bootNotificationResponse
;
674 this.started
= false;
675 parentPort
?.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
676 this.stopping
= false;
678 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
681 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
685 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
686 await this.stop(reason
);
687 await Utils
.sleep(this.stationInfo
.resetTime
);
692 public saveOcppConfiguration(): void {
693 if (this.getOcppPersistentConfiguration()) {
694 this.saveConfiguration({ stationInfo
: false, connectors
: false, evses
: false });
698 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
699 return ChargingStationConfigurationUtils
.getConfigurationKey(
701 StandardParametersKey
.SupportedFeatureProfiles
702 )?.value
?.includes(featureProfile
);
705 public bufferMessage(message
: string): void {
706 this.messageBuffer
.add(message
);
709 public openWSConnection(
710 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
711 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
713 terminateOpened
: false,
716 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
717 params
.closeOpened
= params
?.closeOpened
?? false;
718 params
.terminateOpened
= params
?.terminateOpened
?? false;
719 if (this.started
=== false && this.starting
=== false) {
721 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
726 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
727 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
729 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
731 if (params
?.closeOpened
) {
732 this.closeWSConnection();
734 if (params
?.terminateOpened
) {
735 this.terminateWSConnection();
738 if (this.isWebSocketConnectionOpened() === true) {
740 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
746 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
749 this.wsConnection
= new WebSocket(
750 this.wsConnectionUrl
,
751 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
755 // Handle WebSocket message
756 this.wsConnection
.on(
758 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
760 // Handle WebSocket error
761 this.wsConnection
.on(
763 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
765 // Handle WebSocket close
766 this.wsConnection
.on(
768 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
770 // Handle WebSocket open
771 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
772 // Handle WebSocket ping
773 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
774 // Handle WebSocket pong
775 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
778 public closeWSConnection(): void {
779 if (this.isWebSocketConnectionOpened() === true) {
780 this.wsConnection
?.close();
781 this.wsConnection
= null;
785 public startAutomaticTransactionGenerator(
786 connectorIds
?: number[],
787 automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
789 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(
790 automaticTransactionGeneratorConfiguration
??
791 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
794 if (Utils
.isNotEmptyArray(connectorIds
)) {
795 for (const connectorId
of connectorIds
) {
796 this.automaticTransactionGenerator
?.startConnector(connectorId
);
799 this.automaticTransactionGenerator
?.start();
801 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
804 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
805 if (Utils
.isNotEmptyArray(connectorIds
)) {
806 for (const connectorId
of connectorIds
) {
807 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
810 this.automaticTransactionGenerator
?.stop();
812 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
815 public async stopTransactionOnConnector(
817 reason
= StopTransactionReason
.NONE
818 ): Promise
<StopTransactionResponse
> {
819 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
821 this.getBeginEndMeterValues() === true &&
822 this.getOcppStrictCompliance() === true &&
823 this.getOutOfOrderEndMeterValues() === false
825 // FIXME: Implement OCPP version agnostic helpers
826 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
829 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
831 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
833 RequestCommand
.METER_VALUES
,
837 meterValue
: [transactionEndMeterValue
],
841 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
843 RequestCommand
.STOP_TRANSACTION
,
846 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
852 private flushMessageBuffer(): void {
853 if (this.messageBuffer
.size
> 0) {
854 for (const message
of this.messageBuffer
.values()) {
856 let commandName
: RequestCommand
;
857 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
858 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
860 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
861 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
863 this.wsConnection
?.send(message
);
864 isRequest
&& PerformanceStatistics
.endMeasure(commandName
, beginId
);
866 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
868 )} payload sent: ${message}`
870 this.messageBuffer
.delete(message
);
875 private getSupervisionUrlOcppConfiguration(): boolean {
876 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
879 private getSupervisionUrlOcppKey(): string {
880 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
883 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
884 let template
: ChargingStationTemplate
;
886 if (this.sharedLRUCache
.hasChargingStationTemplate(this.stationInfo
?.templateHash
)) {
887 template
= this.sharedLRUCache
.getChargingStationTemplate(this.stationInfo
.templateHash
);
889 const measureId
= `${FileType.ChargingStationTemplate} read`;
890 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
891 template
= JSON
.parse(
892 fs
.readFileSync(this.templateFile
, 'utf8')
893 ) as ChargingStationTemplate
;
894 PerformanceStatistics
.endMeasure(measureId
, beginId
);
895 template
.templateHash
= crypto
896 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
897 .update(JSON
.stringify(template
))
899 this.sharedLRUCache
.setChargingStationTemplate(template
);
902 FileUtils
.handleFileException(
904 FileType
.ChargingStationTemplate
,
905 error
as NodeJS
.ErrnoException
,
912 private getStationInfoFromTemplate(): ChargingStationInfo
{
913 const stationTemplate
: ChargingStationTemplate
| undefined = this.getTemplateFromFile();
914 if (Utils
.isNullOrUndefined(stationTemplate
)) {
915 const errorMsg
= `Failed to read charging station template file ${this.templateFile}`;
916 logger
.error(`${this.logPrefix()} ${errorMsg}`);
917 throw new BaseError(errorMsg
);
919 if (Utils
.isEmptyObject(stationTemplate
)) {
920 const errorMsg
= `Empty charging station information from template file ${this.templateFile}`;
921 logger
.error(`${this.logPrefix()} ${errorMsg}`);
922 throw new BaseError(errorMsg
);
924 ChargingStationUtils
.warnTemplateKeysDeprecation(
929 const stationInfo
: ChargingStationInfo
=
930 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
931 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
932 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
936 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
937 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
938 if (Utils
.isNotEmptyArray(stationTemplate
?.power
)) {
939 stationTemplate
.power
= stationTemplate
.power
as number[];
940 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
941 stationInfo
.maximumPower
=
942 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
943 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
944 : stationTemplate
.power
[powerArrayRandomIndex
];
946 stationTemplate
.power
= stationTemplate
?.power
as number;
947 stationInfo
.maximumPower
=
948 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
949 ? stationTemplate
.power
* 1000
950 : stationTemplate
.power
;
952 stationInfo
.firmwareVersionPattern
=
953 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
955 Utils
.isNotEmptyString(stationInfo
.firmwareVersion
) &&
956 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
959 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
961 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
964 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
971 stationTemplate
?.firmwareUpgrade
?? {}
973 stationInfo
.resetTime
= !Utils
.isNullOrUndefined(stationTemplate
?.resetTime
)
974 ? stationTemplate
.resetTime
* 1000
975 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
976 // Initialize evses or connectors if needed (FIXME: should be factored out)
977 this.initializeConnectorsOrEvses(stationInfo
);
978 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
979 ChargingStationUtils
.createStationInfoHash(stationInfo
);
983 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
984 let stationInfo
: ChargingStationInfo
| undefined;
985 this.getStationInfoPersistentConfiguration() &&
986 (stationInfo
= this.getConfigurationFromFile()?.stationInfo
);
987 stationInfo
&& ChargingStationUtils
.createStationInfoHash(stationInfo
);
991 private getStationInfo(): ChargingStationInfo
{
992 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
993 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
995 // 1. charging station info from template
996 // 2. charging station info from configuration file
997 // 3. charging station info attribute
998 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
999 if (this.stationInfo
?.infoHash
=== stationInfoFromFile
?.infoHash
) {
1000 return this.stationInfo
;
1002 return stationInfoFromFile
;
1004 stationInfoFromFile
&&
1005 ChargingStationUtils
.propagateSerialNumber(
1006 this.getTemplateFromFile(),
1007 stationInfoFromFile
,
1008 stationInfoFromTemplate
1010 return stationInfoFromTemplate
;
1013 private saveStationInfo(): void {
1014 if (this.getStationInfoPersistentConfiguration()) {
1015 this.saveConfiguration({ ocppConfiguration
: false, connectors
: false, evses
: false });
1019 private getOcppPersistentConfiguration(): boolean {
1020 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1023 private getStationInfoPersistentConfiguration(): boolean {
1024 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1027 private handleUnsupportedVersion(version
: OCPPVersion
) {
1028 const errMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1029 logger
.error(`${this.logPrefix()} ${errMsg}`);
1030 throw new BaseError(errMsg
);
1033 private initialize(): void {
1034 this.configurationFile
= path
.join(
1035 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
1036 `${ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile())}.json`
1038 this.stationInfo
= this.getStationInfo();
1040 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1041 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1042 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1044 const patternGroup
: number | undefined =
1045 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1046 this.stationInfo
.firmwareVersion
?.split('.').length
;
1047 const match
= this.stationInfo
?.firmwareVersion
1048 ?.match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1049 ?.slice(1, patternGroup
+ 1);
1050 const patchLevelIndex
= match
.length
- 1;
1051 match
[patchLevelIndex
] = (
1052 Utils
.convertToInt(match
[patchLevelIndex
]) +
1053 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
1055 this.stationInfo
.firmwareVersion
= match
?.join('.');
1057 this.saveStationInfo();
1058 // Avoid duplication of connectors or evses related information in RAM
1059 delete this.stationInfo
?.Connectors
;
1060 delete this.stationInfo
?.Evses
;
1061 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1062 if (this.getEnableStatistics() === true) {
1063 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1064 this.stationInfo
.hashId
,
1065 this.stationInfo
.chargingStationId
,
1066 this.configuredSupervisionUrl
1069 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
1072 this.powerDivider
= this.getPowerDivider();
1073 // OCPP configuration
1074 this.ocppConfiguration
= this.getOcppConfiguration();
1075 this.initializeOcppConfiguration();
1076 this.initializeOcppServices();
1077 if (this.stationInfo
?.autoRegister
=== true) {
1078 this.bootNotificationResponse
= {
1079 currentTime
: new Date(),
1080 interval
: this.getHeartbeatInterval() / 1000,
1081 status: RegistrationStatusEnumType
.ACCEPTED
,
1086 private initializeOcppServices(): void {
1087 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1088 switch (ocppVersion
) {
1089 case OCPPVersion
.VERSION_16
:
1090 this.ocppIncomingRequestService
=
1091 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1092 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1093 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1096 case OCPPVersion
.VERSION_20
:
1097 case OCPPVersion
.VERSION_201
:
1098 this.ocppIncomingRequestService
=
1099 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1100 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1101 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1105 this.handleUnsupportedVersion(ocppVersion
);
1110 private initializeOcppConfiguration(): void {
1112 !ChargingStationConfigurationUtils
.getConfigurationKey(
1114 StandardParametersKey
.HeartbeatInterval
1117 ChargingStationConfigurationUtils
.addConfigurationKey(
1119 StandardParametersKey
.HeartbeatInterval
,
1124 !ChargingStationConfigurationUtils
.getConfigurationKey(
1126 StandardParametersKey
.HeartBeatInterval
1129 ChargingStationConfigurationUtils
.addConfigurationKey(
1131 StandardParametersKey
.HeartBeatInterval
,
1137 this.getSupervisionUrlOcppConfiguration() &&
1138 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1139 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1141 ChargingStationConfigurationUtils
.addConfigurationKey(
1143 this.getSupervisionUrlOcppKey(),
1144 this.configuredSupervisionUrl
.href
,
1148 !this.getSupervisionUrlOcppConfiguration() &&
1149 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1150 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1152 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1154 this.getSupervisionUrlOcppKey(),
1159 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1160 !ChargingStationConfigurationUtils
.getConfigurationKey(
1162 this.stationInfo
.amperageLimitationOcppKey
1165 ChargingStationConfigurationUtils
.addConfigurationKey(
1167 this.stationInfo
.amperageLimitationOcppKey
,
1169 this.stationInfo
.maximumAmperage
*
1170 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1175 !ChargingStationConfigurationUtils
.getConfigurationKey(
1177 StandardParametersKey
.SupportedFeatureProfiles
1180 ChargingStationConfigurationUtils
.addConfigurationKey(
1182 StandardParametersKey
.SupportedFeatureProfiles
,
1183 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1186 ChargingStationConfigurationUtils
.addConfigurationKey(
1188 StandardParametersKey
.NumberOfConnectors
,
1189 this.getNumberOfConnectors().toString(),
1194 !ChargingStationConfigurationUtils
.getConfigurationKey(
1196 StandardParametersKey
.MeterValuesSampledData
1199 ChargingStationConfigurationUtils
.addConfigurationKey(
1201 StandardParametersKey
.MeterValuesSampledData
,
1202 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1206 !ChargingStationConfigurationUtils
.getConfigurationKey(
1208 StandardParametersKey
.ConnectorPhaseRotation
1211 const connectorsPhaseRotation
: string[] = [];
1212 if (this.hasEvses
) {
1213 for (const evseStatus
of this.evses
.values()) {
1214 for (const connectorId
of evseStatus
.connectors
.keys()) {
1215 connectorsPhaseRotation
.push(
1216 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1221 for (const connectorId
of this.connectors
.keys()) {
1222 connectorsPhaseRotation
.push(
1223 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1227 ChargingStationConfigurationUtils
.addConfigurationKey(
1229 StandardParametersKey
.ConnectorPhaseRotation
,
1230 connectorsPhaseRotation
.toString()
1234 !ChargingStationConfigurationUtils
.getConfigurationKey(
1236 StandardParametersKey
.AuthorizeRemoteTxRequests
1239 ChargingStationConfigurationUtils
.addConfigurationKey(
1241 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1246 !ChargingStationConfigurationUtils
.getConfigurationKey(
1248 StandardParametersKey
.LocalAuthListEnabled
1250 ChargingStationConfigurationUtils
.getConfigurationKey(
1252 StandardParametersKey
.SupportedFeatureProfiles
1253 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1255 ChargingStationConfigurationUtils
.addConfigurationKey(
1257 StandardParametersKey
.LocalAuthListEnabled
,
1262 !ChargingStationConfigurationUtils
.getConfigurationKey(
1264 StandardParametersKey
.ConnectionTimeOut
1267 ChargingStationConfigurationUtils
.addConfigurationKey(
1269 StandardParametersKey
.ConnectionTimeOut
,
1270 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1273 this.saveOcppConfiguration();
1276 private initializeConnectorsOrEvses(stationInfo
: ChargingStationInfo
) {
1277 if (stationInfo
?.Connectors
&& !stationInfo
?.Evses
) {
1278 this.initializeConnectors(stationInfo
);
1279 } else if (stationInfo
?.Evses
&& !stationInfo
?.Connectors
) {
1280 this.initializeEvses(stationInfo
);
1281 } else if (stationInfo
?.Evses
&& stationInfo
?.Connectors
) {
1282 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1283 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1284 throw new BaseError(errorMsg
);
1286 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1287 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1288 throw new BaseError(errorMsg
);
1292 private initializeConnectors(stationInfo
: ChargingStationInfo
): void {
1293 if (!stationInfo
?.Connectors
&& this.connectors
.size
=== 0) {
1294 const logMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1295 logger
.error(`${this.logPrefix()} ${logMsg}`);
1296 throw new BaseError(logMsg
);
1298 if (!stationInfo
?.Connectors
[0]) {
1300 `${this.logPrefix()} Charging station information from template ${
1302 } with no connector id 0 configuration`
1305 if (stationInfo
?.Connectors
) {
1306 const configuredMaxConnectors
=
1307 ChargingStationUtils
.getConfiguredNumberOfConnectors(stationInfo
);
1308 ChargingStationUtils
.checkConfiguredMaxConnectors(
1309 configuredMaxConnectors
,
1313 const connectorsConfigHash
= crypto
1314 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1315 .update(`${JSON.stringify(stationInfo?.Connectors)}${configuredMaxConnectors.toString()}`)
1317 const connectorsConfigChanged
=
1318 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1319 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1320 connectorsConfigChanged
&& this.connectors
.clear();
1321 this.connectorsConfigurationHash
= connectorsConfigHash
;
1322 const templateMaxConnectors
= ChargingStationUtils
.getMaxNumberOfConnectors(
1323 stationInfo
.Connectors
1325 ChargingStationUtils
.checkTemplateMaxConnectors(
1326 templateMaxConnectors
,
1330 const templateMaxAvailableConnectors
= stationInfo
?.Connectors
[0]
1331 ? templateMaxConnectors
- 1
1332 : templateMaxConnectors
;
1334 configuredMaxConnectors
> templateMaxAvailableConnectors
&&
1335 !stationInfo
?.randomConnectors
1338 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
1340 }, forcing random connector configurations affectation`
1342 stationInfo
.randomConnectors
= true;
1344 if (templateMaxConnectors
> 0) {
1345 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1347 connectorId
=== 0 &&
1348 (!stationInfo
?.Connectors
[connectorId
] ||
1349 this.getUseConnectorId0(stationInfo
) === false)
1353 const templateConnectorId
=
1354 connectorId
> 0 && stationInfo
?.randomConnectors
1355 ? Utils
.getRandomInteger(templateMaxAvailableConnectors
, 1)
1357 const connectorStatus
= stationInfo
?.Connectors
[templateConnectorId
];
1358 ChargingStationUtils
.checkStationInfoConnectorStatus(
1359 templateConnectorId
,
1364 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1366 ChargingStationUtils
.initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1367 this.saveConnectorsStatus();
1370 `${this.logPrefix()} Charging station information from template ${
1372 } with no connectors configuration defined, cannot create connectors`
1378 `${this.logPrefix()} Charging station information from template ${
1380 } with no connectors configuration defined, using already defined connectors`
1385 private initializeEvses(stationInfo
: ChargingStationInfo
): void {
1386 if (!stationInfo
?.Evses
&& this.evses
.size
=== 0) {
1387 const logMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1388 logger
.error(`${this.logPrefix()} ${logMsg}`);
1389 throw new BaseError(logMsg
);
1391 if (!stationInfo
?.Evses
[0]) {
1393 `${this.logPrefix()} Charging station information from template ${
1395 } with no evse id 0 configuration`
1398 if (!stationInfo
?.Evses
[0]?.Connectors
[0]) {
1400 `${this.logPrefix()} Charging station information from template ${
1402 } with evse id 0 with no connector id 0 configuration`
1405 if (stationInfo
?.Evses
) {
1406 const evsesConfigHash
= crypto
1407 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1408 .update(`${JSON.stringify(stationInfo?.Evses)}`)
1410 const evsesConfigChanged
=
1411 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1412 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1413 evsesConfigChanged
&& this.evses
.clear();
1414 this.evsesConfigurationHash
= evsesConfigHash
;
1415 const templateMaxEvses
= ChargingStationUtils
.getMaxNumberOfEvses(stationInfo
?.Evses
);
1416 if (templateMaxEvses
> 0) {
1417 for (const evse
in stationInfo
.Evses
) {
1418 const evseId
= Utils
.convertToInt(evse
);
1419 this.evses
.set(evseId
, {
1420 connectors
: ChargingStationUtils
.buildConnectorsMap(
1421 stationInfo
?.Evses
[evse
]?.Connectors
,
1425 availability
: AvailabilityType
.Operative
,
1427 ChargingStationUtils
.initializeConnectorsMapStatus(
1428 this.evses
.get(evseId
)?.connectors
,
1432 this.saveEvsesStatus();
1435 `${this.logPrefix()} Charging station information from template ${
1437 } with no evses configuration defined, cannot create evses`
1443 `${this.logPrefix()} Charging station information from template ${
1445 } with no evses configuration defined, using already defined evses`
1450 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1451 let configuration
: ChargingStationConfiguration
| undefined;
1452 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1454 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1455 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1456 this.configurationFileHash
1459 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1460 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1461 configuration
= JSON
.parse(
1462 fs
.readFileSync(this.configurationFile
, 'utf8')
1463 ) as ChargingStationConfiguration
;
1464 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1465 this.configurationFileHash
= configuration
.configurationHash
;
1466 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1469 FileUtils
.handleFileException(
1470 this.configurationFile
,
1471 FileType
.ChargingStationConfiguration
,
1472 error
as NodeJS
.ErrnoException
,
1477 return configuration
;
1480 private saveConnectorsStatus() {
1481 if (this.getOcppPersistentConfiguration()) {
1482 this.saveConfiguration({ stationInfo
: false, ocppConfiguration
: false, evses
: false });
1486 private saveEvsesStatus() {
1487 if (this.getOcppPersistentConfiguration()) {
1488 this.saveConfiguration({ stationInfo
: false, ocppConfiguration
: false, connectors
: false });
1492 private saveConfiguration(
1494 stationInfo
?: boolean;
1495 ocppConfiguration
?: boolean;
1496 connectors
?: boolean;
1498 } = { stationInfo
: true, ocppConfiguration
: true, connectors
: true, evses
: true }
1500 if (this.configurationFile
) {
1503 ...{ stationInfo
: true, ocppConfiguration
: true, connectors
: true, evses
: true },
1506 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1507 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1509 const configurationData
: ChargingStationConfiguration
=
1510 Utils
.cloneObject(this.getConfigurationFromFile()) ?? {};
1511 if (params
.stationInfo
&& this.stationInfo
) {
1512 configurationData
.stationInfo
= this.stationInfo
;
1514 if (params
.ocppConfiguration
&& this.ocppConfiguration
?.configurationKey
) {
1515 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1517 if (params
.connectors
&& this.connectors
.size
> 0) {
1518 configurationData
.connectorsStatus
= [...this.connectors
.values()].map(
1519 // eslint-disable-next-line @typescript-eslint/no-unused-vars
1520 ({ transactionSetInterval
, ...connectorStatusRest
}) => connectorStatusRest
1523 if (params
.evses
&& this.evses
.size
> 0) {
1524 configurationData
.evsesStatus
= [...this.evses
.values()].map((evseStatus
) => {
1527 connectorsStatus
: [...evseStatus
.connectors
.values()].map(
1528 // eslint-disable-next-line @typescript-eslint/no-unused-vars
1529 ({ transactionSetInterval
, ...connectorStatusRest
}) => connectorStatusRest
1532 delete status.connectors
;
1533 return status as EvseStatusConfiguration
;
1536 delete configurationData
.configurationHash
;
1537 const configurationHash
= crypto
1538 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1539 .update(JSON
.stringify(configurationData
))
1541 if (this.configurationFileHash
!== configurationHash
) {
1542 configurationData
.configurationHash
= configurationHash
;
1543 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1544 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1545 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1546 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1547 fs
.closeSync(fileDescriptor
);
1548 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1549 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1550 this.configurationFileHash
= configurationHash
;
1551 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1554 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1555 this.configurationFile
1560 FileUtils
.handleFileException(
1561 this.configurationFile
,
1562 FileType
.ChargingStationConfiguration
,
1563 error
as NodeJS
.ErrnoException
,
1569 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1574 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1575 return this.getTemplateFromFile()?.Configuration
;
1578 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1579 let configuration
: ChargingStationConfiguration
| undefined;
1580 if (this.getOcppPersistentConfiguration() === true) {
1581 const configurationFromFile
= this.getConfigurationFromFile();
1582 configuration
= configurationFromFile
?.configurationKey
&& configurationFromFile
;
1584 if (!Utils
.isNullOrUndefined(configuration
)) {
1585 delete configuration
.stationInfo
;
1586 delete configuration
.configurationHash
;
1588 return configuration
;
1591 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1592 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1593 this.getOcppConfigurationFromFile();
1594 if (!ocppConfiguration
) {
1595 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1597 return ocppConfiguration
;
1600 private async onOpen(): Promise
<void> {
1601 if (this.isWebSocketConnectionOpened() === true) {
1603 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1605 if (this.isRegistered() === false) {
1606 // Send BootNotification
1607 let registrationRetryCount
= 0;
1609 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1610 BootNotificationRequest
,
1611 BootNotificationResponse
1612 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1613 skipBufferingOnError
: true,
1615 if (this.isRegistered() === false) {
1616 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1618 this?.bootNotificationResponse
?.interval
1619 ? this.bootNotificationResponse
.interval
* 1000
1620 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1624 this.isRegistered() === false &&
1625 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1626 this.getRegistrationMaxRetries() === -1)
1629 if (this.isRegistered() === true) {
1630 if (this.isInAcceptedState() === true) {
1631 await this.startMessageSequence();
1635 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1638 this.wsConnectionRestarted
= false;
1639 this.autoReconnectRetryCount
= 0;
1640 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1643 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1648 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1651 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1652 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1654 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1656 )}' and reason '${reason.toString()}'`
1658 this.autoReconnectRetryCount
= 0;
1663 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1665 )}' and reason '${reason.toString()}'`
1667 this.started
=== true && (await this.reconnect());
1670 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1673 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1674 const cachedRequest
= this.requests
.get(messageId
);
1675 if (Array.isArray(cachedRequest
) === true) {
1676 return cachedRequest
;
1678 throw new OCPPError(
1679 ErrorType
.PROTOCOL_ERROR
,
1680 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1682 )} is not an array`,
1684 cachedRequest
as JsonType
1688 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1689 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1690 if (this.getEnableStatistics() === true) {
1691 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1694 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1698 // Process the message
1699 await this.ocppIncomingRequestService
.incomingRequestHandler(
1707 private handleResponseMessage(response
: Response
): void {
1708 const [messageType
, messageId
, commandPayload
] = response
;
1709 if (this.requests
.has(messageId
) === false) {
1711 throw new OCPPError(
1712 ErrorType
.INTERNAL_ERROR
,
1713 `Response for unknown message id ${messageId}`,
1719 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1724 `${this.logPrefix()} << Command '${
1725 requestCommandName ?? Constants.UNKNOWN_COMMAND
1726 }' received response payload: ${JSON.stringify(response)}`
1728 responseCallback(commandPayload
, requestPayload
);
1731 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1732 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1733 if (this.requests
.has(messageId
) === false) {
1735 throw new OCPPError(
1736 ErrorType
.INTERNAL_ERROR
,
1737 `Error response for unknown message id ${messageId}`,
1739 { errorType
, errorMessage
, errorDetails
}
1742 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1744 `${this.logPrefix()} << Command '${
1745 requestCommandName ?? Constants.UNKNOWN_COMMAND
1746 }' received error response payload: ${JSON.stringify(errorResponse)}`
1748 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1751 private async onMessage(data
: RawData
): Promise
<void> {
1752 let request
: IncomingRequest
| Response
| ErrorResponse
;
1753 let messageType
: number;
1756 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1757 if (Array.isArray(request
) === true) {
1758 [messageType
] = request
;
1759 // Check the type of message
1760 switch (messageType
) {
1762 case MessageType
.CALL_MESSAGE
:
1763 await this.handleIncomingMessage(request
as IncomingRequest
);
1766 case MessageType
.CALL_RESULT_MESSAGE
:
1767 this.handleResponseMessage(request
as Response
);
1770 case MessageType
.CALL_ERROR_MESSAGE
:
1771 this.handleErrorMessage(request
as ErrorResponse
);
1775 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1776 errMsg
= `Wrong message type ${messageType}`;
1777 logger
.error(`${this.logPrefix()} ${errMsg}`);
1778 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1780 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1782 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1787 let commandName
: IncomingRequestCommand
;
1788 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1789 let errorCallback
: ErrorCallback
;
1790 const [, messageId
] = request
;
1791 switch (messageType
) {
1792 case MessageType
.CALL_MESSAGE
:
1793 [, , commandName
] = request
as IncomingRequest
;
1795 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1797 case MessageType
.CALL_RESULT_MESSAGE
:
1798 case MessageType
.CALL_ERROR_MESSAGE
:
1799 if (this.requests
.has(messageId
) === true) {
1800 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1801 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1802 errorCallback(error
as OCPPError
, false);
1804 // Remove the request from the cache in case of error at response handling
1805 this.requests
.delete(messageId
);
1809 if (error
instanceof OCPPError
=== false) {
1811 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1812 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1813 }' message '${data.toString()}' handling is not an OCPPError:`,
1818 `${this.logPrefix()} Incoming OCPP command '${
1819 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1820 }' message '${data.toString()}'${
1821 messageType !== MessageType.CALL_MESSAGE
1822 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1824 } processing error:`,
1830 private onPing(): void {
1831 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1834 private onPong(): void {
1835 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1838 private onError(error
: WSError
): void {
1839 this.closeWSConnection();
1840 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1843 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1844 if (this.getMeteringPerTransaction() === true) {
1847 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1848 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1853 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1854 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1858 private getUseConnectorId0(stationInfo
?: ChargingStationInfo
): boolean {
1859 const localStationInfo
= stationInfo
?? this.stationInfo
;
1860 return localStationInfo
?.useConnectorId0
?? true;
1863 private getNumberOfRunningTransactions(): number {
1865 if (this.hasEvses
) {
1866 for (const evseStatus
of this.evses
.values()) {
1867 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1868 if (connectorStatus
.transactionStarted
=== true) {
1874 for (const connectorId
of this.connectors
.keys()) {
1875 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1883 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1884 if (this.hasEvses
) {
1885 for (const evseStatus
of this.evses
.values()) {
1886 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1887 if (connectorStatus
.transactionStarted
=== true) {
1888 await this.stopTransactionOnConnector(connectorId
, reason
);
1893 for (const connectorId
of this.connectors
.keys()) {
1894 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1895 await this.stopTransactionOnConnector(connectorId
, reason
);
1902 private getConnectionTimeout(): number {
1904 ChargingStationConfigurationUtils
.getConfigurationKey(
1906 StandardParametersKey
.ConnectionTimeOut
1911 ChargingStationConfigurationUtils
.getConfigurationKey(
1913 StandardParametersKey
.ConnectionTimeOut
1915 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1918 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1921 // -1 for unlimited, 0 for disabling
1922 private getAutoReconnectMaxRetries(): number | undefined {
1923 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1924 return this.stationInfo
.autoReconnectMaxRetries
;
1926 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1927 return Configuration
.getAutoReconnectMaxRetries();
1933 private getRegistrationMaxRetries(): number | undefined {
1934 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1935 return this.stationInfo
.registrationMaxRetries
;
1940 private getPowerDivider(): number {
1941 let powerDivider
= this.getNumberOfConnectors();
1942 if (this.stationInfo
?.powerSharedByConnectors
) {
1943 powerDivider
= this.getNumberOfRunningTransactions();
1945 return powerDivider
;
1948 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1949 const maximumPower
= this.getMaximumPower(stationInfo
);
1950 switch (this.getCurrentOutType(stationInfo
)) {
1951 case CurrentType
.AC
:
1952 return ACElectricUtils
.amperagePerPhaseFromPower(
1953 this.getNumberOfPhases(stationInfo
),
1954 maximumPower
/ this.getNumberOfConnectors(),
1955 this.getVoltageOut(stationInfo
)
1957 case CurrentType
.DC
:
1958 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1962 private getAmperageLimitation(): number | undefined {
1964 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1965 ChargingStationConfigurationUtils
.getConfigurationKey(
1967 this.stationInfo
.amperageLimitationOcppKey
1972 ChargingStationConfigurationUtils
.getConfigurationKey(
1974 this.stationInfo
.amperageLimitationOcppKey
1976 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1981 private async startMessageSequence(): Promise
<void> {
1982 if (this.stationInfo
?.autoRegister
=== true) {
1983 await this.ocppRequestService
.requestHandler
<
1984 BootNotificationRequest
,
1985 BootNotificationResponse
1986 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1987 skipBufferingOnError
: true,
1990 // Start WebSocket ping
1991 this.startWebSocketPing();
1993 this.startHeartbeat();
1994 // Initialize connectors status
1995 if (this.hasEvses
) {
1996 for (const [evseId
, evseStatus
] of this.evses
) {
1998 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1999 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2004 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2013 for (const connectorId
of this.connectors
.keys()) {
2014 if (connectorId
> 0) {
2015 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2018 this.getConnectorStatus(connectorId
)
2020 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2024 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2025 await this.ocppRequestService
.requestHandler
<
2026 FirmwareStatusNotificationRequest
,
2027 FirmwareStatusNotificationResponse
2028 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2029 status: FirmwareStatus
.Installed
,
2031 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2035 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true) {
2036 this.startAutomaticTransactionGenerator();
2038 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2041 private async stopMessageSequence(
2042 reason
: StopTransactionReason
= StopTransactionReason
.NONE
2044 // Stop WebSocket ping
2045 this.stopWebSocketPing();
2047 this.stopHeartbeat();
2048 // Stop ongoing transactions
2049 if (this.automaticTransactionGenerator
?.started
=== true) {
2050 this.stopAutomaticTransactionGenerator();
2052 await this.stopRunningTransactions(reason
);
2054 if (this.hasEvses
) {
2055 for (const [evseId
, evseStatus
] of this.evses
) {
2057 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2058 await this.ocppRequestService
.requestHandler
<
2059 StatusNotificationRequest
,
2060 StatusNotificationResponse
2063 RequestCommand
.STATUS_NOTIFICATION
,
2064 OCPPServiceUtils
.buildStatusNotificationRequest(
2067 ConnectorStatusEnum
.Unavailable
2070 delete connectorStatus
?.status;
2075 for (const connectorId
of this.connectors
.keys()) {
2076 if (connectorId
> 0) {
2077 await this.ocppRequestService
.requestHandler
<
2078 StatusNotificationRequest
,
2079 StatusNotificationResponse
2082 RequestCommand
.STATUS_NOTIFICATION
,
2083 OCPPServiceUtils
.buildStatusNotificationRequest(
2086 ConnectorStatusEnum
.Unavailable
2089 delete this.getConnectorStatus(connectorId
)?.status;
2095 private startWebSocketPing(): void {
2096 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
2098 StandardParametersKey
.WebSocketPingInterval
2100 ? Utils
.convertToInt(
2101 ChargingStationConfigurationUtils
.getConfigurationKey(
2103 StandardParametersKey
.WebSocketPingInterval
2107 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2108 this.webSocketPingSetInterval
= setInterval(() => {
2109 if (this.isWebSocketConnectionOpened() === true) {
2110 this.wsConnection
?.ping();
2112 }, webSocketPingInterval
* 1000);
2114 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
2115 webSocketPingInterval
2118 } else if (this.webSocketPingSetInterval
) {
2120 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
2121 webSocketPingInterval
2126 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2131 private stopWebSocketPing(): void {
2132 if (this.webSocketPingSetInterval
) {
2133 clearInterval(this.webSocketPingSetInterval
);
2134 delete this.webSocketPingSetInterval
;
2138 private getConfiguredSupervisionUrl(): URL
{
2139 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2140 if (Utils
.isNotEmptyArray(supervisionUrls
)) {
2141 let configuredSupervisionUrlIndex
: number;
2142 switch (Configuration
.getSupervisionUrlDistribution()) {
2143 case SupervisionUrlDistribution
.RANDOM
:
2144 configuredSupervisionUrlIndex
= Math.floor(Utils
.secureRandom() * supervisionUrls
.length
);
2146 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2147 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2149 Object.values(SupervisionUrlDistribution
).includes(
2150 Configuration
.getSupervisionUrlDistribution()
2153 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2154 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2157 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
2160 return new URL(supervisionUrls
[configuredSupervisionUrlIndex
]);
2162 return new URL(supervisionUrls
as string);
2165 private stopHeartbeat(): void {
2166 if (this.heartbeatSetInterval
) {
2167 clearInterval(this.heartbeatSetInterval
);
2168 delete this.heartbeatSetInterval
;
2172 private terminateWSConnection(): void {
2173 if (this.isWebSocketConnectionOpened() === true) {
2174 this.wsConnection
?.terminate();
2175 this.wsConnection
= null;
2179 private getReconnectExponentialDelay(): boolean {
2180 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2183 private async reconnect(): Promise
<void> {
2184 // Stop WebSocket ping
2185 this.stopWebSocketPing();
2187 this.stopHeartbeat();
2188 // Stop the ATG if needed
2189 if (this.automaticTransactionGenerator
?.configuration
?.stopOnConnectionFailure
=== true) {
2190 this.stopAutomaticTransactionGenerator();
2193 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2194 this.getAutoReconnectMaxRetries() === -1
2196 this.autoReconnectRetryCount
++;
2197 const reconnectDelay
= this.getReconnectExponentialDelay()
2198 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2199 : this.getConnectionTimeout() * 1000;
2200 const reconnectDelayWithdraw
= 1000;
2201 const reconnectTimeout
=
2202 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2203 ? reconnectDelay
- reconnectDelayWithdraw
2206 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2209 )}ms, timeout ${reconnectTimeout}ms`
2211 await Utils
.sleep(reconnectDelay
);
2213 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2215 this.openWSConnection(
2217 ...(this.stationInfo
?.wsOptions
?? {}),
2218 handshakeTimeout
: reconnectTimeout
,
2220 { closeOpened
: true }
2222 this.wsConnectionRestarted
= true;
2223 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2225 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2226 this.autoReconnectRetryCount
2227 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2232 private getAutomaticTransactionGeneratorConfigurationFromTemplate():
2233 | AutomaticTransactionGeneratorConfiguration
2235 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
;