1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { createHash
} from
'node:crypto';
13 import { dirname
, join
} from
'node:path';
14 import { URL
} from
'node:url';
15 import { parentPort
} from
'node:worker_threads';
17 import merge from
'just-merge';
18 import { type RawData
, WebSocket
} from
'ws';
20 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator';
21 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel';
22 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
25 checkConnectorsConfiguration
,
26 checkStationInfoConnectorStatus
,
28 countReservableConnectors
,
29 createBootNotificationRequest
,
31 getAmperageLimitationUnitDivider
,
32 getBootConnectorStatus
,
33 getChargingStationConnectorChargingProfilesPowerLimit
,
39 getPhaseRotationValue
,
40 initializeConnectorsMapStatus
,
41 propagateSerialNumber
,
42 stationTemplateToStationInfo
,
43 warnTemplateKeysDeprecation
,
44 } from
'./ChargingStationUtils';
45 import { IdTagsCache
} from
'./IdTagsCache';
47 OCPP16IncomingRequestService
,
49 OCPP16ResponseService
,
51 OCPP20IncomingRequestService
,
53 OCPP20ResponseService
,
54 type OCPPIncomingRequestService
,
55 type OCPPRequestService
,
58 import { SharedLRUCache
} from
'./SharedLRUCache';
59 import { BaseError
, OCPPError
} from
'../exception';
60 import { PerformanceStatistics
} from
'../performance';
62 type AutomaticTransactionGeneratorConfiguration
,
64 type BootNotificationRequest
,
65 type BootNotificationResponse
,
67 type ChargingStationConfiguration
,
68 type ChargingStationInfo
,
69 type ChargingStationOcppConfiguration
,
70 type ChargingStationTemplate
,
78 type EvseStatusConfiguration
,
81 type FirmwareStatusNotificationRequest
,
82 type FirmwareStatusNotificationResponse
,
84 type HeartbeatRequest
,
85 type HeartbeatResponse
,
87 type IncomingRequestCommand
,
92 type MeterValuesRequest
,
93 type MeterValuesResponse
,
97 RegistrationStatusEnumType
,
100 ReservationFilterKey
,
101 ReservationTerminationReason
,
103 StandardParametersKey
,
105 type StatusNotificationRequest
,
106 type StatusNotificationResponse
,
107 StopTransactionReason
,
108 type StopTransactionRequest
,
109 type StopTransactionResponse
,
110 SupervisionUrlDistribution
,
111 SupportedFeatureProfiles
,
114 WebSocketCloseEventStatusCode
,
124 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
125 buildConnectorsStatus
,
134 formatDurationMilliSeconds
,
135 formatDurationSeconds
,
137 getWebSocketCloseEventStatusString
,
151 export class ChargingStation
{
152 public readonly index
: number;
153 public readonly templateFile
: string;
154 public stationInfo
!: ChargingStationInfo
;
155 public started
: boolean;
156 public starting
: boolean;
157 public idTagsCache
: IdTagsCache
;
158 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
159 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
160 public wsConnection
!: WebSocket
| null;
161 public readonly connectors
: Map
<number, ConnectorStatus
>;
162 public readonly evses
: Map
<number, EvseStatus
>;
163 public readonly requests
: Map
<string, CachedRequest
>;
164 public performanceStatistics
!: PerformanceStatistics
| undefined;
165 public heartbeatSetInterval
?: NodeJS
.Timeout
;
166 public ocppRequestService
!: OCPPRequestService
;
167 public bootNotificationRequest
!: BootNotificationRequest
;
168 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
169 public powerDivider
!: number;
170 private stopping
: boolean;
171 private configurationFile
!: string;
172 private configurationFileHash
!: string;
173 private connectorsConfigurationHash
!: string;
174 private evsesConfigurationHash
!: string;
175 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
176 private readonly messageBuffer
: Set
<string>;
177 private configuredSupervisionUrl
!: URL
;
178 private wsConnectionRestarted
: boolean;
179 private autoReconnectRetryCount
: number;
180 private templateFileWatcher
!: FSWatcher
| undefined;
181 private templateFileHash
!: string;
182 private readonly sharedLRUCache
: SharedLRUCache
;
183 private webSocketPingSetInterval
?: NodeJS
.Timeout
;
184 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
185 private reservationExpirationSetInterval
?: NodeJS
.Timeout
;
187 constructor(index
: number, templateFile
: string) {
188 this.started
= false;
189 this.starting
= false;
190 this.stopping
= false;
191 this.wsConnectionRestarted
= false;
192 this.autoReconnectRetryCount
= 0;
194 this.templateFile
= templateFile
;
195 this.connectors
= new Map
<number, ConnectorStatus
>();
196 this.evses
= new Map
<number, EvseStatus
>();
197 this.requests
= new Map
<string, CachedRequest
>();
198 this.messageBuffer
= new Set
<string>();
199 this.sharedLRUCache
= SharedLRUCache
.getInstance();
200 this.idTagsCache
= IdTagsCache
.getInstance();
201 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
206 public get
hasEvses(): boolean {
207 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
210 private get
wsConnectionUrl(): URL
{
213 this.getSupervisionUrlOcppConfiguration() &&
214 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
216 ChargingStationConfigurationUtils.getConfigurationKey(
218 this.getSupervisionUrlOcppKey(),
221 ? ChargingStationConfigurationUtils.getConfigurationKey(
223 this.getSupervisionUrlOcppKey(),
225 : this.configuredSupervisionUrl.href
226 }/${this.stationInfo.chargingStationId}`,
230 public logPrefix
= (): string => {
233 (isNotEmptyString(this?.stationInfo?.chargingStationId)
234 ? this?.stationInfo?.chargingStationId
235 : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
236 'Error at building log prefix'
241 public hasIdTags(): boolean {
242 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
245 public getEnableStatistics(): boolean {
246 return this.stationInfo
.enableStatistics
?? false;
249 public getMustAuthorizeAtRemoteStart(): boolean {
250 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
253 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
254 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
255 switch (this.getCurrentOutType(stationInfo
)) {
257 return !isUndefined(localStationInfo
.numberOfPhases
) ? localStationInfo
.numberOfPhases
! : 3;
263 public isWebSocketConnectionOpened(): boolean {
264 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
267 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
268 return this?.bootNotificationResponse
?.status;
271 public inUnknownState(): boolean {
272 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
275 public inPendingState(): boolean {
276 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
279 public inAcceptedState(): boolean {
280 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
283 public inRejectedState(): boolean {
284 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
287 public isRegistered(): boolean {
289 this.inUnknownState() === false &&
290 (this.inAcceptedState() === true || this.inPendingState() === true)
294 public isChargingStationAvailable(): boolean {
295 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
298 public hasConnector(connectorId
: number): boolean {
300 for (const evseStatus
of this.evses
.values()) {
301 if (evseStatus
.connectors
.has(connectorId
)) {
307 return this.connectors
.has(connectorId
);
310 public isConnectorAvailable(connectorId
: number): boolean {
313 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
317 public getNumberOfConnectors(): number {
319 let numberOfConnectors
= 0;
320 for (const [evseId
, evseStatus
] of this.evses
) {
322 numberOfConnectors
+= evseStatus
.connectors
.size
;
325 return numberOfConnectors
;
327 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
330 public getNumberOfEvses(): number {
331 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
334 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
336 for (const evseStatus
of this.evses
.values()) {
337 if (evseStatus
.connectors
.has(connectorId
)) {
338 return evseStatus
.connectors
.get(connectorId
);
343 return this.connectors
.get(connectorId
);
346 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
347 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
350 public getOcppStrictCompliance(): boolean {
351 return this.stationInfo
?.ocppStrictCompliance
?? true;
354 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number {
355 const defaultVoltageOut
= getDefaultVoltageOut(
356 this.getCurrentOutType(stationInfo
),
360 return (stationInfo
?? this.stationInfo
).voltageOut
?? defaultVoltageOut
;
363 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
364 const localStationInfo
= stationInfo
?? this.stationInfo
;
365 // eslint-disable-next-line @typescript-eslint/dot-notation
367 (localStationInfo
['maxPower' as keyof ChargingStationInfo
] as number) ??
368 localStationInfo
.maximumPower
372 public getConnectorMaximumAvailablePower(connectorId
: number): number {
373 let connectorAmperageLimitationPowerLimit
: number | undefined;
375 !isNullOrUndefined(this.getAmperageLimitation()) &&
376 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
378 connectorAmperageLimitationPowerLimit
=
379 (this.getCurrentOutType() === CurrentType
.AC
380 ? ACElectricUtils
.powerTotal(
381 this.getNumberOfPhases(),
382 this.getVoltageOut(),
383 this.getAmperageLimitation()! *
384 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
386 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation()!)) /
389 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
390 const connectorChargingProfilesPowerLimit
=
391 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
393 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
394 isNaN(connectorAmperageLimitationPowerLimit
!)
396 : connectorAmperageLimitationPowerLimit
!,
397 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
401 public getTransactionIdTag(transactionId
: number): string | undefined {
403 for (const evseStatus
of this.evses
.values()) {
404 for (const connectorStatus
of evseStatus
.connectors
.values()) {
405 if (connectorStatus
.transactionId
=== transactionId
) {
406 return connectorStatus
.transactionIdTag
;
411 for (const connectorId
of this.connectors
.keys()) {
412 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
413 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
419 public getNumberOfRunningTransactions(): number {
422 for (const [evseId
, evseStatus
] of this.evses
) {
426 for (const connectorStatus
of evseStatus
.connectors
.values()) {
427 if (connectorStatus
.transactionStarted
=== true) {
433 for (const connectorId
of this.connectors
.keys()) {
434 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
442 public getOutOfOrderEndMeterValues(): boolean {
443 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
446 public getBeginEndMeterValues(): boolean {
447 return this.stationInfo
?.beginEndMeterValues
?? false;
450 public getMeteringPerTransaction(): boolean {
451 return this.stationInfo
?.meteringPerTransaction
?? true;
454 public getTransactionDataMeterValues(): boolean {
455 return this.stationInfo
?.transactionDataMeterValues
?? false;
458 public getMainVoltageMeterValues(): boolean {
459 return this.stationInfo
?.mainVoltageMeterValues
?? true;
462 public getPhaseLineToLineVoltageMeterValues(): boolean {
463 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
466 public getCustomValueLimitationMeterValues(): boolean {
467 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
470 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
472 for (const evseStatus
of this.evses
.values()) {
473 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
474 if (connectorStatus
.transactionId
=== transactionId
) {
480 for (const connectorId
of this.connectors
.keys()) {
481 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
488 public getEnergyActiveImportRegisterByTransactionId(
489 transactionId
: number,
492 return this.getEnergyActiveImportRegister(
493 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
498 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
499 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
502 public getAuthorizeRemoteTxRequests(): boolean {
503 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
505 StandardParametersKey
.AuthorizeRemoteTxRequests
,
507 return authorizeRemoteTxRequests
? convertToBoolean(authorizeRemoteTxRequests
.value
) : false;
510 public getLocalAuthListEnabled(): boolean {
511 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
513 StandardParametersKey
.LocalAuthListEnabled
,
515 return localAuthListEnabled
? convertToBoolean(localAuthListEnabled
.value
) : false;
518 public getHeartbeatInterval(): number {
519 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
521 StandardParametersKey
.HeartbeatInterval
,
523 if (HeartbeatInterval
) {
524 return convertToInt(HeartbeatInterval
.value
) * 1000;
526 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
528 StandardParametersKey
.HeartBeatInterval
,
530 if (HeartBeatInterval
) {
531 return convertToInt(HeartBeatInterval
.value
) * 1000;
533 this.stationInfo
?.autoRegister
=== false &&
535 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
536 Constants.DEFAULT_HEARTBEAT_INTERVAL
539 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
542 public setSupervisionUrl(url
: string): void {
544 this.getSupervisionUrlOcppConfiguration() &&
545 isNotEmptyString(this.getSupervisionUrlOcppKey())
547 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
549 this.getSupervisionUrlOcppKey(),
553 this.stationInfo
.supervisionUrls
= url
;
554 this.saveStationInfo();
555 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
559 public startHeartbeat(): void {
560 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
561 this.heartbeatSetInterval
= setInterval(() => {
562 this.ocppRequestService
563 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
566 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
570 }, this.getHeartbeatInterval());
572 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
573 this.getHeartbeatInterval(),
576 } else if (this.heartbeatSetInterval
) {
578 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
579 this.getHeartbeatInterval(),
584 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()},
585 not starting the heartbeat`,
590 public restartHeartbeat(): void {
592 this.stopHeartbeat();
594 this.startHeartbeat();
597 public restartWebSocketPing(): void {
598 // Stop WebSocket ping
599 this.stopWebSocketPing();
600 // Start WebSocket ping
601 this.startWebSocketPing();
604 public startMeterValues(connectorId
: number, interval
: number): void {
605 if (connectorId
=== 0) {
607 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`,
611 if (!this.getConnectorStatus(connectorId
)) {
613 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
614 ${connectorId.toString()}`,
618 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
620 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}
621 with no transaction started`,
625 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
626 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
629 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}
630 with no transaction id`,
635 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
636 // FIXME: Implement OCPP version agnostic helpers
637 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
640 this.getConnectorStatus(connectorId
)!.transactionId
!,
643 this.ocppRequestService
644 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
646 RequestCommand
.METER_VALUES
,
649 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
650 meterValue
: [meterValue
],
655 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
662 `${this.logPrefix()} Charging station ${
663 StandardParametersKey.MeterValueSampleInterval
664 } configuration set to ${interval}, not sending MeterValues`,
669 public stopMeterValues(connectorId
: number) {
670 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
671 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
675 public start(): void {
676 if (this.started
=== false) {
677 if (this.starting
=== false) {
678 this.starting
= true;
679 if (this.getEnableStatistics() === true) {
680 this.performanceStatistics
?.start();
682 if (this.hasFeatureProfile(SupportedFeatureProfiles
.Reservation
)) {
683 this.startReservationExpirationSetInterval();
685 this.openWSConnection();
686 // Monitor charging station template file
687 this.templateFileWatcher
= watchJsonFile(
689 FileType
.ChargingStationTemplate
,
692 (event
, filename
): void => {
693 if (isNotEmptyString(filename
) && event
=== 'change') {
696 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
698 } file have changed, reload`,
700 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
703 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
)!);
705 this.stopAutomaticTransactionGenerator();
706 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
707 this.startAutomaticTransactionGenerator();
709 if (this.getEnableStatistics() === true) {
710 this.performanceStatistics
?.restart();
712 this.performanceStatistics
?.stop();
714 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
717 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
725 parentPort
?.postMessage(buildStartedMessage(this));
726 this.starting
= false;
728 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
731 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
735 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
736 if (this.started
=== true) {
737 if (this.stopping
=== false) {
738 this.stopping
= true;
739 await this.stopMessageSequence(reason
);
740 this.closeWSConnection();
741 if (this.getEnableStatistics() === true) {
742 this.performanceStatistics
?.stop();
744 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
745 this.templateFileWatcher
?.close();
746 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
747 delete this.bootNotificationResponse
;
748 this.started
= false;
749 this.saveConfiguration();
750 parentPort
?.postMessage(buildStoppedMessage(this));
751 this.stopping
= false;
753 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
756 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
760 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
761 await this.stop(reason
);
762 await sleep(this.stationInfo
.resetTime
!);
767 public saveOcppConfiguration(): void {
768 if (this.getOcppPersistentConfiguration()) {
769 this.saveConfiguration();
773 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
774 return ChargingStationConfigurationUtils
.getConfigurationKey(
776 StandardParametersKey
.SupportedFeatureProfiles
,
777 )?.value
?.includes(featureProfile
);
780 public bufferMessage(message
: string): void {
781 this.messageBuffer
.add(message
);
784 public openWSConnection(
785 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
786 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
788 terminateOpened
: false,
791 options
= { handshakeTimeout
: this.getConnectionTimeout() * 1000, ...options
};
792 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
793 if (this.started
=== false && this.starting
=== false) {
795 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()}
796 on stopped charging station`,
801 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
802 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
804 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
806 if (params
?.closeOpened
) {
807 this.closeWSConnection();
809 if (params
?.terminateOpened
) {
810 this.terminateWSConnection();
813 if (this.isWebSocketConnectionOpened() === true) {
815 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()}
822 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
825 this.wsConnection
= new WebSocket(
826 this.wsConnectionUrl
,
827 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
831 // Handle WebSocket message
832 this.wsConnection
.on(
834 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
836 // Handle WebSocket error
837 this.wsConnection
.on(
839 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
841 // Handle WebSocket close
842 this.wsConnection
.on(
844 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
846 // Handle WebSocket open
847 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
848 // Handle WebSocket ping
849 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
850 // Handle WebSocket pong
851 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
854 public closeWSConnection(): void {
855 if (this.isWebSocketConnectionOpened() === true) {
856 this.wsConnection
?.close();
857 this.wsConnection
= null;
861 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
862 let automaticTransactionGeneratorConfiguration
:
863 | AutomaticTransactionGeneratorConfiguration
865 const automaticTransactionGeneratorConfigurationFromFile
=
866 this.getConfigurationFromFile()?.automaticTransactionGenerator
;
868 this.getAutomaticTransactionGeneratorPersistentConfiguration() &&
869 automaticTransactionGeneratorConfigurationFromFile
871 automaticTransactionGeneratorConfiguration
=
872 automaticTransactionGeneratorConfigurationFromFile
;
874 automaticTransactionGeneratorConfiguration
=
875 this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
878 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
879 ...automaticTransactionGeneratorConfiguration
,
883 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
884 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
887 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
888 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
889 if (isNotEmptyArray(connectorIds
)) {
890 for (const connectorId
of connectorIds
!) {
891 this.automaticTransactionGenerator
?.startConnector(connectorId
);
894 this.automaticTransactionGenerator
?.start();
896 this.saveAutomaticTransactionGeneratorConfiguration();
897 parentPort
?.postMessage(buildUpdatedMessage(this));
900 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
901 if (isNotEmptyArray(connectorIds
)) {
902 for (const connectorId
of connectorIds
!) {
903 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
906 this.automaticTransactionGenerator
?.stop();
908 this.saveAutomaticTransactionGeneratorConfiguration();
909 parentPort
?.postMessage(buildUpdatedMessage(this));
912 public async stopTransactionOnConnector(
914 reason
= StopTransactionReason
.NONE
,
915 ): Promise
<StopTransactionResponse
> {
916 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
918 this.getBeginEndMeterValues() === true &&
919 this.getOcppStrictCompliance() === true &&
920 this.getOutOfOrderEndMeterValues() === false
922 // FIXME: Implement OCPP version agnostic helpers
923 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
926 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
928 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
930 RequestCommand
.METER_VALUES
,
934 meterValue
: [transactionEndMeterValue
],
938 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
940 RequestCommand
.STOP_TRANSACTION
,
943 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
949 public getReservationOnConnectorId0Enabled(): boolean {
950 return convertToBoolean(
951 ChargingStationConfigurationUtils
.getConfigurationKey(
953 StandardParametersKey
.ReserveConnectorZeroSupported
,
958 public async addReservation(reservation
: Reservation
): Promise
<void> {
959 const [exists
, reservationFound
] = this.doesReservationExists(reservation
);
961 await this.removeReservation(
963 ReservationTerminationReason
.REPLACE_EXISTING
,
966 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
967 await OCPPServiceUtils
.sendAndSetConnectorStatus(
969 reservation
.connectorId
,
970 ConnectorStatusEnum
.Reserved
,
972 { send
: reservation
.connectorId
!== 0 },
976 public async removeReservation(
977 reservation
: Reservation
,
978 reason
?: ReservationTerminationReason
,
980 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
982 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
983 delete connector
.reservation
;
985 case ReservationTerminationReason
.TRANSACTION_STARTED
:
986 delete connector
.reservation
;
988 case ReservationTerminationReason
.RESERVATION_CANCELED
:
989 case ReservationTerminationReason
.REPLACE_EXISTING
:
990 case ReservationTerminationReason
.EXPIRED
:
991 await OCPPServiceUtils
.sendAndSetConnectorStatus(
993 reservation
.connectorId
,
994 ConnectorStatusEnum
.Available
,
996 { send
: reservation
.connectorId
!== 0 },
998 delete connector
.reservation
;
1005 public getReservationBy(
1006 filterKey
: ReservationFilterKey
,
1007 value
: number | string,
1008 ): Reservation
| undefined {
1009 if (this.hasEvses
) {
1010 for (const evseStatus
of this.evses
.values()) {
1011 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1012 if (connectorStatus
?.reservation
?.[filterKey
as keyof Reservation
] === value
) {
1013 return connectorStatus
.reservation
;
1018 for (const connectorStatus
of this.connectors
.values()) {
1019 if (connectorStatus
?.reservation
?.[filterKey
as keyof Reservation
] === value
) {
1020 return connectorStatus
.reservation
;
1026 public doesReservationExists(
1027 reservation
: Partial
<Reservation
>,
1028 ): [boolean, Reservation
| undefined] {
1029 const foundReservation
= this.getReservationBy(
1030 ReservationFilterKey
.RESERVATION_ID
,
1033 return isUndefined(foundReservation
) ? [false, undefined] : [true, foundReservation
];
1036 public startReservationExpirationSetInterval(customInterval
?: number): void {
1038 customInterval
?? Constants
.DEFAULT_RESERVATION_EXPIRATION_OBSERVATION_INTERVAL
;
1040 `${this.logPrefix()} Reservation expiration date interval is set to ${interval}
1041 and starts on charging station now`,
1044 // eslint-disable-next-line @typescript-eslint/no-misused-promises
1045 this.reservationExpirationSetInterval
= setInterval(async (): Promise
<void> => {
1046 const now
= new Date();
1047 if (this.hasEvses
) {
1048 for (const evseStatus
of this.evses
.values()) {
1049 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1050 if (connectorStatus
.reservation
!.expiryDate
< now
) {
1051 await this.removeReservation(
1052 connectorStatus
.reservation
!,
1053 ReservationTerminationReason
.EXPIRED
,
1059 for (const connectorStatus
of this.connectors
.values()) {
1060 if (connectorStatus
.reservation
!.expiryDate
< now
) {
1061 await this.removeReservation(
1062 connectorStatus
.reservation
!,
1063 ReservationTerminationReason
.EXPIRED
,
1072 public restartReservationExpiryDateSetInterval(): void {
1073 this.stopReservationExpirationSetInterval();
1074 this.startReservationExpirationSetInterval();
1077 public validateIncomingRequestWithReservation(connectorId
: number, idTag
: string): boolean {
1078 return this.getReservationBy(ReservationFilterKey
.CONNECTOR_ID
, connectorId
)?.idTag
=== idTag
;
1081 public isConnectorReservable(
1082 reservationId
: number,
1084 connectorId
?: number,
1086 const [alreadyExists
] = this.doesReservationExists({ id
: reservationId
});
1087 if (alreadyExists
) {
1088 return alreadyExists
;
1090 const userReservedAlready
= isUndefined(
1091 this.getReservationBy(ReservationFilterKey
.ID_TAG
, idTag
!),
1095 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
1096 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
1097 return !alreadyExists
&& !userReservedAlready
&& notConnectorZero
&& freeConnectorsAvailable
;
1100 private getNumberOfReservableConnectors(): number {
1101 let reservableConnectors
= 0;
1102 if (this.hasEvses
) {
1103 for (const evseStatus
of this.evses
.values()) {
1104 reservableConnectors
+= countReservableConnectors(evseStatus
.connectors
);
1107 reservableConnectors
= countReservableConnectors(this.connectors
);
1109 return reservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
1112 private getNumberOfReservationsOnConnectorZero(): number {
1113 let numberOfReservations
= 0;
1114 if (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) {
1115 ++numberOfReservations
;
1116 } else if (this.connectors
.get(0)?.reservation
) {
1117 ++numberOfReservations
;
1119 return numberOfReservations
;
1122 private flushMessageBuffer(): void {
1123 if (this.messageBuffer
.size
> 0) {
1124 for (const message
of this.messageBuffer
.values()) {
1125 let beginId
: string | undefined;
1126 let commandName
: RequestCommand
| undefined;
1127 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1128 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1130 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1131 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1133 this.wsConnection
?.send(message
);
1134 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1136 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
1138 )} payload sent: ${message}`,
1140 this.messageBuffer
.delete(message
);
1145 private getSupervisionUrlOcppConfiguration(): boolean {
1146 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
1149 private stopReservationExpirationSetInterval(): void {
1150 if (this.reservationExpirationSetInterval
) {
1151 clearInterval(this.reservationExpirationSetInterval
);
1155 private getSupervisionUrlOcppKey(): string {
1156 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
1159 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1160 let template
: ChargingStationTemplate
| undefined;
1162 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1163 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1165 const measureId
= `${FileType.ChargingStationTemplate} read`;
1166 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1167 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1168 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1169 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1170 .update(JSON
.stringify(template
))
1172 this.sharedLRUCache
.setChargingStationTemplate(template
);
1173 this.templateFileHash
= template
.templateHash
;
1176 handleFileException(
1178 FileType
.ChargingStationTemplate
,
1179 error
as NodeJS
.ErrnoException
,
1186 private getStationInfoFromTemplate(): ChargingStationInfo
{
1187 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1188 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1189 warnTemplateKeysDeprecation(stationTemplate
, this.logPrefix(), this.templateFile
);
1190 if (stationTemplate
?.Connectors
) {
1191 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1193 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1194 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1195 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1196 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1197 createSerialNumber(stationTemplate
, stationInfo
);
1198 if (isNotEmptyArray(stationTemplate
?.power
)) {
1199 stationTemplate
.power
= stationTemplate
.power
as number[];
1200 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1201 stationInfo
.maximumPower
=
1202 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1203 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1204 : stationTemplate
.power
[powerArrayRandomIndex
];
1206 stationTemplate
.power
= stationTemplate
?.power
as number;
1207 stationInfo
.maximumPower
=
1208 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1209 ? stationTemplate
.power
* 1000
1210 : stationTemplate
.power
;
1212 stationInfo
.firmwareVersionPattern
=
1213 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1215 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1216 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1219 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1221 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1224 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1231 stationTemplate
?.firmwareUpgrade
?? {},
1233 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1234 ? stationTemplate
.resetTime
! * 1000
1235 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1236 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1240 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
1241 let stationInfo
: ChargingStationInfo
| undefined;
1242 if (this.getStationInfoPersistentConfiguration()) {
1243 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1245 delete stationInfo
?.infoHash
;
1251 private getStationInfo(): ChargingStationInfo
{
1252 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1253 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
1255 // 1. charging station info from template
1256 // 2. charging station info from configuration file
1257 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1258 return stationInfoFromFile
!;
1260 stationInfoFromFile
&&
1261 propagateSerialNumber(
1262 this.getTemplateFromFile()!,
1263 stationInfoFromFile
,
1264 stationInfoFromTemplate
,
1266 return stationInfoFromTemplate
;
1269 private saveStationInfo(): void {
1270 if (this.getStationInfoPersistentConfiguration()) {
1271 this.saveConfiguration();
1275 private getOcppPersistentConfiguration(): boolean {
1276 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1279 private getStationInfoPersistentConfiguration(): boolean {
1280 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1283 private getAutomaticTransactionGeneratorPersistentConfiguration(): boolean {
1284 return this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
?? true;
1287 private handleUnsupportedVersion(version
: OCPPVersion
) {
1288 const errorMsg
= `Unsupported protocol version '${version}' configured
1289 in template file ${this.templateFile}`;
1290 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1291 throw new BaseError(errorMsg
);
1294 private initialize(): void {
1295 const stationTemplate
= this.getTemplateFromFile()!;
1296 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1297 this.configurationFile
= join(
1298 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1299 `${getHashId(this.index, stationTemplate)}.json`,
1301 const chargingStationConfiguration
= this.getConfigurationFromFile();
1303 chargingStationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1304 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1305 (chargingStationConfiguration
?.connectorsStatus
|| chargingStationConfiguration
?.evsesStatus
)
1307 this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration
);
1309 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1311 this.stationInfo
= this.getStationInfo();
1313 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1314 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1315 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1317 const patternGroup
: number | undefined =
1318 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1319 this.stationInfo
.firmwareVersion
?.split('.').length
;
1320 const match
= this.stationInfo
1321 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1322 .slice(1, patternGroup
! + 1);
1323 const patchLevelIndex
= match
.length
- 1;
1324 match
[patchLevelIndex
] = (
1325 convertToInt(match
[patchLevelIndex
]) +
1326 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1328 this.stationInfo
.firmwareVersion
= match
?.join('.');
1330 this.saveStationInfo();
1331 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1332 if (this.getEnableStatistics() === true) {
1333 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1334 this.stationInfo
.hashId
,
1335 this.stationInfo
.chargingStationId
!,
1336 this.configuredSupervisionUrl
,
1339 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1340 this.powerDivider
= this.getPowerDivider();
1341 // OCPP configuration
1342 this.ocppConfiguration
= this.getOcppConfiguration();
1343 this.initializeOcppConfiguration();
1344 this.initializeOcppServices();
1345 if (this.stationInfo
?.autoRegister
=== true) {
1346 this.bootNotificationResponse
= {
1347 currentTime
: new Date(),
1348 interval
: this.getHeartbeatInterval() / 1000,
1349 status: RegistrationStatusEnumType
.ACCEPTED
,
1354 private initializeOcppServices(): void {
1355 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1356 switch (ocppVersion
) {
1357 case OCPPVersion
.VERSION_16
:
1358 this.ocppIncomingRequestService
=
1359 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1360 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1361 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1364 case OCPPVersion
.VERSION_20
:
1365 case OCPPVersion
.VERSION_201
:
1366 this.ocppIncomingRequestService
=
1367 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1368 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1369 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1373 this.handleUnsupportedVersion(ocppVersion
);
1378 private initializeOcppConfiguration(): void {
1380 !ChargingStationConfigurationUtils
.getConfigurationKey(
1382 StandardParametersKey
.HeartbeatInterval
,
1385 ChargingStationConfigurationUtils
.addConfigurationKey(
1387 StandardParametersKey
.HeartbeatInterval
,
1392 !ChargingStationConfigurationUtils
.getConfigurationKey(
1394 StandardParametersKey
.HeartBeatInterval
,
1397 ChargingStationConfigurationUtils
.addConfigurationKey(
1399 StandardParametersKey
.HeartBeatInterval
,
1405 this.getSupervisionUrlOcppConfiguration() &&
1406 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1407 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1409 ChargingStationConfigurationUtils
.addConfigurationKey(
1411 this.getSupervisionUrlOcppKey(),
1412 this.configuredSupervisionUrl
.href
,
1416 !this.getSupervisionUrlOcppConfiguration() &&
1417 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1418 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1420 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1422 this.getSupervisionUrlOcppKey(),
1427 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1428 !ChargingStationConfigurationUtils
.getConfigurationKey(
1430 this.stationInfo
.amperageLimitationOcppKey
!,
1433 ChargingStationConfigurationUtils
.addConfigurationKey(
1435 this.stationInfo
.amperageLimitationOcppKey
!,
1437 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1442 !ChargingStationConfigurationUtils
.getConfigurationKey(
1444 StandardParametersKey
.SupportedFeatureProfiles
,
1447 ChargingStationConfigurationUtils
.addConfigurationKey(
1449 StandardParametersKey
.SupportedFeatureProfiles
,
1450 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1453 ChargingStationConfigurationUtils
.addConfigurationKey(
1455 StandardParametersKey
.NumberOfConnectors
,
1456 this.getNumberOfConnectors().toString(),
1458 { overwrite
: true },
1461 !ChargingStationConfigurationUtils
.getConfigurationKey(
1463 StandardParametersKey
.MeterValuesSampledData
,
1466 ChargingStationConfigurationUtils
.addConfigurationKey(
1468 StandardParametersKey
.MeterValuesSampledData
,
1469 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1473 !ChargingStationConfigurationUtils
.getConfigurationKey(
1475 StandardParametersKey
.ConnectorPhaseRotation
,
1478 const connectorsPhaseRotation
: string[] = [];
1479 if (this.hasEvses
) {
1480 for (const evseStatus
of this.evses
.values()) {
1481 for (const connectorId
of evseStatus
.connectors
.keys()) {
1482 connectorsPhaseRotation
.push(
1483 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1488 for (const connectorId
of this.connectors
.keys()) {
1489 connectorsPhaseRotation
.push(
1490 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1494 ChargingStationConfigurationUtils
.addConfigurationKey(
1496 StandardParametersKey
.ConnectorPhaseRotation
,
1497 connectorsPhaseRotation
.toString(),
1501 !ChargingStationConfigurationUtils
.getConfigurationKey(
1503 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1506 ChargingStationConfigurationUtils
.addConfigurationKey(
1508 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1513 !ChargingStationConfigurationUtils
.getConfigurationKey(
1515 StandardParametersKey
.LocalAuthListEnabled
,
1517 ChargingStationConfigurationUtils
.getConfigurationKey(
1519 StandardParametersKey
.SupportedFeatureProfiles
,
1520 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1522 ChargingStationConfigurationUtils
.addConfigurationKey(
1524 StandardParametersKey
.LocalAuthListEnabled
,
1529 !ChargingStationConfigurationUtils
.getConfigurationKey(
1531 StandardParametersKey
.ConnectionTimeOut
,
1534 ChargingStationConfigurationUtils
.addConfigurationKey(
1536 StandardParametersKey
.ConnectionTimeOut
,
1537 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1540 this.saveOcppConfiguration();
1543 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1544 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1545 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1546 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1548 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1549 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1550 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1551 delete evseStatus
.connectorsStatus
;
1552 this.evses
.set(evseId
, {
1553 ...(evseStatus
as EvseStatus
),
1554 connectors
: new Map
<number, ConnectorStatus
>(
1555 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1562 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1563 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1564 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1565 throw new BaseError(errorMsg
);
1567 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1568 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1569 throw new BaseError(errorMsg
);
1573 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1574 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1575 this.initializeConnectorsFromTemplate(stationTemplate
);
1576 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1577 this.initializeEvsesFromTemplate(stationTemplate
);
1578 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1579 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1580 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1581 throw new BaseError(errorMsg
);
1583 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1584 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1585 throw new BaseError(errorMsg
);
1589 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1590 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1591 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1592 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1593 throw new BaseError(errorMsg
);
1595 if (!stationTemplate
?.Connectors
?.[0]) {
1597 `${this.logPrefix()} Charging station information from template ${
1599 } with no connector id 0 configuration`,
1602 if (stationTemplate
?.Connectors
) {
1603 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1604 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1605 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1607 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1610 const connectorsConfigChanged
=
1611 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1612 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1613 connectorsConfigChanged
&& this.connectors
.clear();
1614 this.connectorsConfigurationHash
= connectorsConfigHash
;
1615 if (templateMaxConnectors
> 0) {
1616 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1618 connectorId
=== 0 &&
1619 (!stationTemplate
?.Connectors
[connectorId
] ||
1620 this.getUseConnectorId0(stationTemplate
) === false)
1624 const templateConnectorId
=
1625 connectorId
> 0 && stationTemplate
?.randomConnectors
1626 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1628 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1629 checkStationInfoConnectorStatus(
1630 templateConnectorId
,
1635 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1637 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1638 this.saveConnectorsStatus();
1641 `${this.logPrefix()} Charging station information from template ${
1643 } with no connectors configuration defined, cannot create connectors`,
1649 `${this.logPrefix()} Charging station information from template ${
1651 } with no connectors configuration defined, using already defined connectors`,
1656 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1657 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1658 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1659 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1660 throw new BaseError(errorMsg
);
1662 if (!stationTemplate
?.Evses
?.[0]) {
1664 `${this.logPrefix()} Charging station information from template ${
1666 } with no evse id 0 configuration`,
1669 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1671 `${this.logPrefix()} Charging station information from template ${
1673 } with evse id 0 with no connector id 0 configuration`,
1676 if (stationTemplate
?.Evses
) {
1677 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1678 .update(JSON
.stringify(stationTemplate
?.Evses
))
1680 const evsesConfigChanged
=
1681 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1682 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1683 evsesConfigChanged
&& this.evses
.clear();
1684 this.evsesConfigurationHash
= evsesConfigHash
;
1685 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1686 if (templateMaxEvses
> 0) {
1687 for (const evse
in stationTemplate
.Evses
) {
1688 const evseId
= convertToInt(evse
);
1689 this.evses
.set(evseId
, {
1690 connectors
: buildConnectorsMap(
1691 stationTemplate
?.Evses
[evse
]?.Connectors
,
1695 availability
: AvailabilityType
.Operative
,
1697 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1699 this.saveEvsesStatus();
1702 `${this.logPrefix()} Charging station information from template ${
1704 } with no evses configuration defined, cannot create evses`,
1710 `${this.logPrefix()} Charging station information from template ${
1712 } with no evses configuration defined, using already defined evses`,
1717 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1718 let configuration
: ChargingStationConfiguration
| undefined;
1719 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1721 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1722 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1723 this.configurationFileHash
,
1726 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1727 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1728 configuration
= JSON
.parse(
1729 readFileSync(this.configurationFile
, 'utf8'),
1730 ) as ChargingStationConfiguration
;
1731 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1732 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1733 this.configurationFileHash
= configuration
.configurationHash
!;
1736 handleFileException(
1737 this.configurationFile
,
1738 FileType
.ChargingStationConfiguration
,
1739 error
as NodeJS
.ErrnoException
,
1744 return configuration
;
1747 private saveAutomaticTransactionGeneratorConfiguration(): void {
1748 if (this.getAutomaticTransactionGeneratorPersistentConfiguration()) {
1749 this.saveConfiguration();
1753 private saveConnectorsStatus() {
1754 this.saveConfiguration();
1757 private saveEvsesStatus() {
1758 this.saveConfiguration();
1761 private saveConfiguration(): void {
1762 if (isNotEmptyString(this.configurationFile
)) {
1764 if (!existsSync(dirname(this.configurationFile
))) {
1765 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1767 let configurationData
: ChargingStationConfiguration
=
1768 cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!) ?? {};
1769 if (this.getStationInfoPersistentConfiguration() && this.stationInfo
) {
1770 configurationData
.stationInfo
= this.stationInfo
;
1772 delete configurationData
.stationInfo
;
1774 if (this.getOcppPersistentConfiguration() && this.ocppConfiguration
?.configurationKey
) {
1775 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1777 delete configurationData
.configurationKey
;
1779 configurationData
= merge
<ChargingStationConfiguration
>(
1781 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1784 !this.getAutomaticTransactionGeneratorPersistentConfiguration() ||
1785 !this.getAutomaticTransactionGeneratorConfiguration()
1787 delete configurationData
.automaticTransactionGenerator
;
1789 if (this.connectors
.size
> 0) {
1790 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1792 delete configurationData
.connectorsStatus
;
1794 if (this.evses
.size
> 0) {
1795 configurationData
.evsesStatus
= buildEvsesStatus(this);
1797 delete configurationData
.evsesStatus
;
1799 delete configurationData
.configurationHash
;
1800 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1803 stationInfo
: configurationData
.stationInfo
,
1804 configurationKey
: configurationData
.configurationKey
,
1805 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1806 } as ChargingStationConfiguration
),
1809 if (this.configurationFileHash
!== configurationHash
) {
1810 AsyncLock
.acquire(AsyncLockType
.configuration
)
1812 configurationData
.configurationHash
= configurationHash
;
1813 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1814 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1815 const fileDescriptor
= openSync(this.configurationFile
, 'w');
1816 writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1817 closeSync(fileDescriptor
);
1818 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1819 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1820 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1821 this.configurationFileHash
= configurationHash
;
1824 handleFileException(
1825 this.configurationFile
,
1826 FileType
.ChargingStationConfiguration
,
1827 error
as NodeJS
.ErrnoException
,
1832 AsyncLock
.release(AsyncLockType
.configuration
).catch(Constants
.EMPTY_FUNCTION
);
1836 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1837 this.configurationFile
1842 handleFileException(
1843 this.configurationFile
,
1844 FileType
.ChargingStationConfiguration
,
1845 error
as NodeJS
.ErrnoException
,
1851 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1856 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1857 return this.getTemplateFromFile()?.Configuration
;
1860 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1861 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1862 if (this.getOcppPersistentConfiguration() === true && configurationKey
) {
1863 return { configurationKey
};
1868 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1869 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1870 this.getOcppConfigurationFromFile();
1871 if (!ocppConfiguration
) {
1872 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1874 return ocppConfiguration
;
1877 private async onOpen(): Promise
<void> {
1878 if (this.isWebSocketConnectionOpened() === true) {
1880 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1882 if (this.isRegistered() === false) {
1883 // Send BootNotification
1884 let registrationRetryCount
= 0;
1886 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1887 BootNotificationRequest
,
1888 BootNotificationResponse
1889 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1890 skipBufferingOnError
: true,
1892 if (this.isRegistered() === false) {
1893 this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount
;
1895 this?.bootNotificationResponse
?.interval
1896 ? this.bootNotificationResponse
.interval
* 1000
1897 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1901 this.isRegistered() === false &&
1902 (registrationRetryCount
<= this.getRegistrationMaxRetries()! ||
1903 this.getRegistrationMaxRetries() === -1)
1906 if (this.isRegistered() === true) {
1907 if (this.inAcceptedState() === true) {
1908 await this.startMessageSequence();
1912 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`,
1915 this.wsConnectionRestarted
= false;
1916 this.autoReconnectRetryCount
= 0;
1917 parentPort
?.postMessage(buildUpdatedMessage(this));
1920 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1925 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1928 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1929 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1931 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1933 )}' and reason '${reason.toString()}'`,
1935 this.autoReconnectRetryCount
= 0;
1940 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1942 )}' and reason '${reason.toString()}'`,
1944 this.started
=== true && (await this.reconnect());
1947 parentPort
?.postMessage(buildUpdatedMessage(this));
1950 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1951 const cachedRequest
= this.requests
.get(messageId
);
1952 if (Array.isArray(cachedRequest
) === true) {
1953 return cachedRequest
;
1955 throw new OCPPError(
1956 ErrorType
.PROTOCOL_ERROR
,
1957 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1959 )} is not an array`,
1961 cachedRequest
as JsonType
,
1965 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1966 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1967 if (this.getEnableStatistics() === true) {
1968 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1971 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1975 // Process the message
1976 await this.ocppIncomingRequestService
.incomingRequestHandler(
1984 private handleResponseMessage(response
: Response
): void {
1985 const [messageType
, messageId
, commandPayload
] = response
;
1986 if (this.requests
.has(messageId
) === false) {
1988 throw new OCPPError(
1989 ErrorType
.INTERNAL_ERROR
,
1990 `Response for unknown message id ${messageId}`,
1996 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
2001 `${this.logPrefix()} << Command '${
2002 requestCommandName ?? Constants.UNKNOWN_COMMAND
2003 }' received response payload: ${JSON.stringify(response)}`,
2005 responseCallback(commandPayload
, requestPayload
);
2008 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
2009 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
2010 if (this.requests
.has(messageId
) === false) {
2012 throw new OCPPError(
2013 ErrorType
.INTERNAL_ERROR
,
2014 `Error response for unknown message id ${messageId}`,
2016 { errorType
, errorMessage
, errorDetails
},
2019 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
2021 `${this.logPrefix()} << Command '${
2022 requestCommandName ?? Constants.UNKNOWN_COMMAND
2023 }' received error response payload: ${JSON.stringify(errorResponse)}`,
2025 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
2028 private async onMessage(data
: RawData
): Promise
<void> {
2029 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
2030 let messageType
: number | undefined;
2031 let errorMsg
: string;
2033 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2034 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
2035 if (Array.isArray(request
) === true) {
2036 [messageType
] = request
;
2037 // Check the type of message
2038 switch (messageType
) {
2040 case MessageType
.CALL_MESSAGE
:
2041 await this.handleIncomingMessage(request
as IncomingRequest
);
2044 case MessageType
.CALL_RESULT_MESSAGE
:
2045 this.handleResponseMessage(request
as Response
);
2048 case MessageType
.CALL_ERROR_MESSAGE
:
2049 this.handleErrorMessage(request
as ErrorResponse
);
2053 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
2054 errorMsg
= `Wrong message type ${messageType}`;
2055 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2056 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
2058 parentPort
?.postMessage(buildUpdatedMessage(this));
2060 throw new OCPPError(
2061 ErrorType
.PROTOCOL_ERROR
,
2062 'Incoming message is not an array',
2070 let commandName
: IncomingRequestCommand
| undefined;
2071 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
2072 let errorCallback
: ErrorCallback
;
2073 const [, messageId
] = request
!;
2074 switch (messageType
) {
2075 case MessageType
.CALL_MESSAGE
:
2076 [, , commandName
] = request
as IncomingRequest
;
2078 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
2080 case MessageType
.CALL_RESULT_MESSAGE
:
2081 case MessageType
.CALL_ERROR_MESSAGE
:
2082 if (this.requests
.has(messageId
) === true) {
2083 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
2084 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2085 errorCallback(error
as OCPPError
, false);
2087 // Remove the request from the cache in case of error at response handling
2088 this.requests
.delete(messageId
);
2092 if (error
instanceof OCPPError
=== false) {
2094 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2095 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2096 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2097 }' message '${data.toString()}' handling is not an OCPPError:`,
2102 `${this.logPrefix()} Incoming OCPP command '${
2103 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2104 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2105 }' message '${data.toString()}'${
2106 messageType !== MessageType.CALL_MESSAGE
2107 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
2109 } processing error:`,
2115 private onPing(): void {
2116 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
2119 private onPong(): void {
2120 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
2123 private onError(error
: WSError
): void {
2124 this.closeWSConnection();
2125 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
2128 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
2129 if (this.getMeteringPerTransaction() === true) {
2132 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
2133 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2138 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
2139 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2143 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
2144 return stationTemplate
?.useConnectorId0
?? true;
2147 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
2148 if (this.hasEvses
) {
2149 for (const [evseId
, evseStatus
] of this.evses
) {
2153 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2154 if (connectorStatus
.transactionStarted
=== true) {
2155 await this.stopTransactionOnConnector(connectorId
, reason
);
2160 for (const connectorId
of this.connectors
.keys()) {
2161 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2162 await this.stopTransactionOnConnector(connectorId
, reason
);
2169 private getConnectionTimeout(): number {
2171 ChargingStationConfigurationUtils
.getConfigurationKey(
2173 StandardParametersKey
.ConnectionTimeOut
,
2178 ChargingStationConfigurationUtils
.getConfigurationKey(
2180 StandardParametersKey
.ConnectionTimeOut
,
2182 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
2185 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2188 // -1 for unlimited, 0 for disabling
2189 private getAutoReconnectMaxRetries(): number | undefined {
2191 this.stationInfo
.autoReconnectMaxRetries
?? Configuration
.getAutoReconnectMaxRetries() ?? -1
2196 private getRegistrationMaxRetries(): number | undefined {
2197 return this.stationInfo
.registrationMaxRetries
?? -1;
2200 private getPowerDivider(): number {
2201 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2202 if (this.stationInfo
?.powerSharedByConnectors
) {
2203 powerDivider
= this.getNumberOfRunningTransactions();
2205 return powerDivider
;
2208 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
2209 const maximumPower
= this.getMaximumPower(stationInfo
);
2210 switch (this.getCurrentOutType(stationInfo
)) {
2211 case CurrentType
.AC
:
2212 return ACElectricUtils
.amperagePerPhaseFromPower(
2213 this.getNumberOfPhases(stationInfo
),
2214 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2215 this.getVoltageOut(stationInfo
),
2217 case CurrentType
.DC
:
2218 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2222 private getAmperageLimitation(): number | undefined {
2224 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2225 ChargingStationConfigurationUtils
.getConfigurationKey(
2227 this.stationInfo
.amperageLimitationOcppKey
!,
2232 ChargingStationConfigurationUtils
.getConfigurationKey(
2234 this.stationInfo
.amperageLimitationOcppKey
!,
2236 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2241 private async startMessageSequence(): Promise
<void> {
2242 if (this.stationInfo
?.autoRegister
=== true) {
2243 await this.ocppRequestService
.requestHandler
<
2244 BootNotificationRequest
,
2245 BootNotificationResponse
2246 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2247 skipBufferingOnError
: true,
2250 // Start WebSocket ping
2251 this.startWebSocketPing();
2253 this.startHeartbeat();
2254 // Initialize connectors status
2255 if (this.hasEvses
) {
2256 for (const [evseId
, evseStatus
] of this.evses
) {
2258 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2259 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2260 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2263 connectorBootStatus
,
2270 for (const connectorId
of this.connectors
.keys()) {
2271 if (connectorId
> 0) {
2272 const connectorBootStatus
= getBootConnectorStatus(
2275 this.getConnectorStatus(connectorId
)!,
2277 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2281 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2282 await this.ocppRequestService
.requestHandler
<
2283 FirmwareStatusNotificationRequest
,
2284 FirmwareStatusNotificationResponse
2285 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2286 status: FirmwareStatus
.Installed
,
2288 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2292 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2293 this.startAutomaticTransactionGenerator();
2295 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2298 private async stopMessageSequence(
2299 reason
: StopTransactionReason
= StopTransactionReason
.NONE
,
2301 // Stop WebSocket ping
2302 this.stopWebSocketPing();
2304 this.stopHeartbeat();
2305 // Stop ongoing transactions
2306 if (this.automaticTransactionGenerator
?.started
=== true) {
2307 this.stopAutomaticTransactionGenerator();
2309 await this.stopRunningTransactions(reason
);
2311 if (this.hasEvses
) {
2312 for (const [evseId
, evseStatus
] of this.evses
) {
2314 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2315 await this.ocppRequestService
.requestHandler
<
2316 StatusNotificationRequest
,
2317 StatusNotificationResponse
2320 RequestCommand
.STATUS_NOTIFICATION
,
2321 OCPPServiceUtils
.buildStatusNotificationRequest(
2324 ConnectorStatusEnum
.Unavailable
,
2328 delete connectorStatus
?.status;
2333 for (const connectorId
of this.connectors
.keys()) {
2334 if (connectorId
> 0) {
2335 await this.ocppRequestService
.requestHandler
<
2336 StatusNotificationRequest
,
2337 StatusNotificationResponse
2340 RequestCommand
.STATUS_NOTIFICATION
,
2341 OCPPServiceUtils
.buildStatusNotificationRequest(
2344 ConnectorStatusEnum
.Unavailable
,
2347 delete this.getConnectorStatus(connectorId
)?.status;
2353 private startWebSocketPing(): void {
2354 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
2356 StandardParametersKey
.WebSocketPingInterval
,
2359 ChargingStationConfigurationUtils
.getConfigurationKey(
2361 StandardParametersKey
.WebSocketPingInterval
,
2365 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2366 this.webSocketPingSetInterval
= setInterval(() => {
2367 if (this.isWebSocketConnectionOpened() === true) {
2368 this.wsConnection
?.ping();
2370 }, webSocketPingInterval
* 1000);
2372 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2373 webSocketPingInterval,
2376 } else if (this.webSocketPingSetInterval
) {
2378 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2379 webSocketPingInterval,
2384 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2389 private stopWebSocketPing(): void {
2390 if (this.webSocketPingSetInterval
) {
2391 clearInterval(this.webSocketPingSetInterval
);
2392 delete this.webSocketPingSetInterval
;
2396 private getConfiguredSupervisionUrl(): URL
{
2397 let configuredSupervisionUrl
: string;
2398 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2399 if (isNotEmptyArray(supervisionUrls
)) {
2400 let configuredSupervisionUrlIndex
: number;
2401 switch (Configuration
.getSupervisionUrlDistribution()) {
2402 case SupervisionUrlDistribution
.RANDOM
:
2403 configuredSupervisionUrlIndex
= Math.floor(
2404 secureRandom() * (supervisionUrls
as string[]).length
,
2407 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2408 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2410 Object.values(SupervisionUrlDistribution
).includes(
2411 Configuration
.getSupervisionUrlDistribution()!,
2414 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2415 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2416 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2419 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2422 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2424 configuredSupervisionUrl
= supervisionUrls
as string;
2426 if (isNotEmptyString(configuredSupervisionUrl
)) {
2427 return new URL(configuredSupervisionUrl
);
2429 const errorMsg
= 'No supervision url(s) configured';
2430 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2431 throw new BaseError(`${errorMsg}`);
2434 private stopHeartbeat(): void {
2435 if (this.heartbeatSetInterval
) {
2436 clearInterval(this.heartbeatSetInterval
);
2437 delete this.heartbeatSetInterval
;
2441 private terminateWSConnection(): void {
2442 if (this.isWebSocketConnectionOpened() === true) {
2443 this.wsConnection
?.terminate();
2444 this.wsConnection
= null;
2448 private getReconnectExponentialDelay(): boolean {
2449 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2452 private async reconnect(): Promise
<void> {
2453 // Stop WebSocket ping
2454 this.stopWebSocketPing();
2456 this.stopHeartbeat();
2457 // Stop the ATG if needed
2458 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2459 this.stopAutomaticTransactionGenerator();
2462 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries()! ||
2463 this.getAutoReconnectMaxRetries() === -1
2465 ++this.autoReconnectRetryCount
;
2466 const reconnectDelay
= this.getReconnectExponentialDelay()
2467 ? exponentialDelay(this.autoReconnectRetryCount
)
2468 : this.getConnectionTimeout() * 1000;
2469 const reconnectDelayWithdraw
= 1000;
2470 const reconnectTimeout
=
2471 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2472 ? reconnectDelay
- reconnectDelayWithdraw
2475 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2478 )}ms, timeout ${reconnectTimeout}ms`,
2480 await sleep(reconnectDelay
);
2482 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2484 this.openWSConnection(
2486 ...(this.stationInfo
?.wsOptions
?? {}),
2487 handshakeTimeout
: reconnectTimeout
,
2489 { closeOpened
: true },
2491 this.wsConnectionRestarted
= true;
2492 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2494 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2495 this.autoReconnectRetryCount
2496 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`,