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 { millisecondsToSeconds
, secondsToMilliseconds
} from
'date-fns';
18 import merge from
'just-merge';
19 import { type RawData
, WebSocket
} from
'ws';
21 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator';
22 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel';
25 deleteConfigurationKey
,
27 setConfigurationKeyValue
,
28 } from
'./ChargingStationConfigurationUtils';
31 checkConnectorsConfiguration
,
32 checkStationInfoConnectorStatus
,
34 countReservableConnectors
,
35 createBootNotificationRequest
,
37 getAmperageLimitationUnitDivider
,
38 getBootConnectorStatus
,
39 getChargingStationConnectorChargingProfilesPowerLimit
,
45 getPhaseRotationValue
,
46 initializeConnectorsMapStatus
,
47 propagateSerialNumber
,
48 stationTemplateToStationInfo
,
49 warnTemplateKeysDeprecation
,
50 } from
'./ChargingStationUtils';
51 import { IdTagsCache
} from
'./IdTagsCache';
53 OCPP16IncomingRequestService
,
55 OCPP16ResponseService
,
57 OCPP20IncomingRequestService
,
59 OCPP20ResponseService
,
60 type OCPPIncomingRequestService
,
61 type OCPPRequestService
,
64 import { SharedLRUCache
} from
'./SharedLRUCache';
65 import { BaseError
, OCPPError
} from
'../exception';
66 import { PerformanceStatistics
} from
'../performance';
68 type AutomaticTransactionGeneratorConfiguration
,
70 type BootNotificationRequest
,
71 type BootNotificationResponse
,
73 type ChargingStationConfiguration
,
74 type ChargingStationInfo
,
75 type ChargingStationOcppConfiguration
,
76 type ChargingStationTemplate
,
84 type EvseStatusConfiguration
,
87 type FirmwareStatusNotificationRequest
,
88 type FirmwareStatusNotificationResponse
,
90 type HeartbeatRequest
,
91 type HeartbeatResponse
,
93 type IncomingRequestCommand
,
98 type MeterValuesRequest
,
99 type MeterValuesResponse
,
101 type OutgoingRequest
,
103 RegistrationStatusEnumType
,
106 ReservationFilterKey
,
107 ReservationTerminationReason
,
109 StandardParametersKey
,
111 type StatusNotificationRequest
,
112 type StatusNotificationResponse
,
113 StopTransactionReason
,
114 type StopTransactionRequest
,
115 type StopTransactionResponse
,
116 SupervisionUrlDistribution
,
117 SupportedFeatureProfiles
,
120 WebSocketCloseEventStatusCode
,
130 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
131 buildConnectorsStatus
,
140 formatDurationMilliSeconds
,
141 formatDurationSeconds
,
143 getWebSocketCloseEventStatusString
,
157 export class ChargingStation
{
158 public readonly index
: number;
159 public readonly templateFile
: string;
160 public stationInfo
!: ChargingStationInfo
;
161 public started
: boolean;
162 public starting
: boolean;
163 public idTagsCache
: IdTagsCache
;
164 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
165 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
166 public wsConnection
!: WebSocket
| null;
167 public readonly connectors
: Map
<number, ConnectorStatus
>;
168 public readonly evses
: Map
<number, EvseStatus
>;
169 public readonly requests
: Map
<string, CachedRequest
>;
170 public performanceStatistics
!: PerformanceStatistics
| undefined;
171 public heartbeatSetInterval
?: NodeJS
.Timeout
;
172 public ocppRequestService
!: OCPPRequestService
;
173 public bootNotificationRequest
!: BootNotificationRequest
;
174 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
175 public powerDivider
!: number;
176 private stopping
: boolean;
177 private configurationFile
!: string;
178 private configurationFileHash
!: string;
179 private connectorsConfigurationHash
!: string;
180 private evsesConfigurationHash
!: string;
181 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
;
182 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
183 private readonly messageBuffer
: Set
<string>;
184 private configuredSupervisionUrl
!: URL
;
185 private wsConnectionRestarted
: boolean;
186 private autoReconnectRetryCount
: number;
187 private templateFileWatcher
!: FSWatcher
| undefined;
188 private templateFileHash
!: string;
189 private readonly sharedLRUCache
: SharedLRUCache
;
190 private webSocketPingSetInterval
?: NodeJS
.Timeout
;
191 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
192 private reservationExpirationSetInterval
?: NodeJS
.Timeout
;
194 constructor(index
: number, templateFile
: string) {
195 this.started
= false;
196 this.starting
= false;
197 this.stopping
= false;
198 this.wsConnectionRestarted
= false;
199 this.autoReconnectRetryCount
= 0;
201 this.templateFile
= templateFile
;
202 this.connectors
= new Map
<number, ConnectorStatus
>();
203 this.evses
= new Map
<number, EvseStatus
>();
204 this.requests
= new Map
<string, CachedRequest
>();
205 this.messageBuffer
= new Set
<string>();
206 this.sharedLRUCache
= SharedLRUCache
.getInstance();
207 this.idTagsCache
= IdTagsCache
.getInstance();
208 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
213 public get
hasEvses(): boolean {
214 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
217 private get
wsConnectionUrl(): URL
{
220 this.getSupervisionUrlOcppConfiguration() &&
221 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
222 isNotEmptyString(getConfigurationKey(this, this.getSupervisionUrlOcppKey())?.value)
223 ? getConfigurationKey(this, this.getSupervisionUrlOcppKey())!.value
224 : this.configuredSupervisionUrl.href
225 }/${this.stationInfo.chargingStationId}`,
229 public logPrefix
= (): string => {
232 (isNotEmptyString(this?.stationInfo?.chargingStationId)
233 ? this?.stationInfo?.chargingStationId
234 : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
235 'Error at building log prefix'
240 public hasIdTags(): boolean {
241 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
244 public getEnableStatistics(): boolean {
245 return this.stationInfo
.enableStatistics
?? false;
248 public getMustAuthorizeAtRemoteStart(): boolean {
249 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
252 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
253 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
254 switch (this.getCurrentOutType(stationInfo
)) {
256 return !isUndefined(localStationInfo
.numberOfPhases
) ? localStationInfo
.numberOfPhases
! : 3;
262 public isWebSocketConnectionOpened(): boolean {
263 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
266 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
267 return this?.bootNotificationResponse
?.status;
270 public inUnknownState(): boolean {
271 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
274 public inPendingState(): boolean {
275 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
278 public inAcceptedState(): boolean {
279 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
282 public inRejectedState(): boolean {
283 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
286 public isRegistered(): boolean {
288 this.inUnknownState() === false &&
289 (this.inAcceptedState() === true || this.inPendingState() === true)
293 public isChargingStationAvailable(): boolean {
294 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
297 public hasConnector(connectorId
: number): boolean {
299 for (const evseStatus
of this.evses
.values()) {
300 if (evseStatus
.connectors
.has(connectorId
)) {
306 return this.connectors
.has(connectorId
);
309 public isConnectorAvailable(connectorId
: number): boolean {
312 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
316 public getNumberOfConnectors(): number {
318 let numberOfConnectors
= 0;
319 for (const [evseId
, evseStatus
] of this.evses
) {
321 numberOfConnectors
+= evseStatus
.connectors
.size
;
324 return numberOfConnectors
;
326 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
329 public getNumberOfEvses(): number {
330 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
333 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
335 for (const evseStatus
of this.evses
.values()) {
336 if (evseStatus
.connectors
.has(connectorId
)) {
337 return evseStatus
.connectors
.get(connectorId
);
342 return this.connectors
.get(connectorId
);
345 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
346 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
349 public getOcppStrictCompliance(): boolean {
350 return this.stationInfo
?.ocppStrictCompliance
?? true;
353 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number {
354 const defaultVoltageOut
= getDefaultVoltageOut(
355 this.getCurrentOutType(stationInfo
),
359 return (stationInfo
?? this.stationInfo
).voltageOut
?? defaultVoltageOut
;
362 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
363 const localStationInfo
= stationInfo
?? this.stationInfo
;
364 // eslint-disable-next-line @typescript-eslint/dot-notation
366 (localStationInfo
['maxPower' as keyof ChargingStationInfo
] as number) ??
367 localStationInfo
.maximumPower
371 public getConnectorMaximumAvailablePower(connectorId
: number): number {
372 let connectorAmperageLimitationPowerLimit
: number | undefined;
374 !isNullOrUndefined(this.getAmperageLimitation()) &&
375 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
377 connectorAmperageLimitationPowerLimit
=
378 (this.getCurrentOutType() === CurrentType
.AC
379 ? ACElectricUtils
.powerTotal(
380 this.getNumberOfPhases(),
381 this.getVoltageOut(),
382 this.getAmperageLimitation()! *
383 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
385 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation()!)) /
388 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
389 const connectorChargingProfilesPowerLimit
=
390 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
392 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
393 isNaN(connectorAmperageLimitationPowerLimit
!)
395 : connectorAmperageLimitationPowerLimit
!,
396 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
400 public getTransactionIdTag(transactionId
: number): string | undefined {
402 for (const evseStatus
of this.evses
.values()) {
403 for (const connectorStatus
of evseStatus
.connectors
.values()) {
404 if (connectorStatus
.transactionId
=== transactionId
) {
405 return connectorStatus
.transactionIdTag
;
410 for (const connectorId
of this.connectors
.keys()) {
411 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
412 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
418 public getNumberOfRunningTransactions(): number {
421 for (const [evseId
, evseStatus
] of this.evses
) {
425 for (const connectorStatus
of evseStatus
.connectors
.values()) {
426 if (connectorStatus
.transactionStarted
=== true) {
432 for (const connectorId
of this.connectors
.keys()) {
433 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
441 public getOutOfOrderEndMeterValues(): boolean {
442 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
445 public getBeginEndMeterValues(): boolean {
446 return this.stationInfo
?.beginEndMeterValues
?? false;
449 public getMeteringPerTransaction(): boolean {
450 return this.stationInfo
?.meteringPerTransaction
?? true;
453 public getTransactionDataMeterValues(): boolean {
454 return this.stationInfo
?.transactionDataMeterValues
?? false;
457 public getMainVoltageMeterValues(): boolean {
458 return this.stationInfo
?.mainVoltageMeterValues
?? true;
461 public getPhaseLineToLineVoltageMeterValues(): boolean {
462 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
465 public getCustomValueLimitationMeterValues(): boolean {
466 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
469 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
471 for (const evseStatus
of this.evses
.values()) {
472 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
473 if (connectorStatus
.transactionId
=== transactionId
) {
479 for (const connectorId
of this.connectors
.keys()) {
480 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
487 public getEnergyActiveImportRegisterByTransactionId(
488 transactionId
: number,
491 return this.getEnergyActiveImportRegister(
492 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
497 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
498 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
501 public getAuthorizeRemoteTxRequests(): boolean {
502 const authorizeRemoteTxRequests
= getConfigurationKey(
504 StandardParametersKey
.AuthorizeRemoteTxRequests
,
506 return authorizeRemoteTxRequests
? convertToBoolean(authorizeRemoteTxRequests
.value
) : false;
509 public getLocalAuthListEnabled(): boolean {
510 const localAuthListEnabled
= getConfigurationKey(
512 StandardParametersKey
.LocalAuthListEnabled
,
514 return localAuthListEnabled
? convertToBoolean(localAuthListEnabled
.value
) : false;
517 public getHeartbeatInterval(): number {
518 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
);
519 if (HeartbeatInterval
) {
520 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
));
522 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
);
523 if (HeartBeatInterval
) {
524 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
));
526 this.stationInfo
?.autoRegister
=== false &&
528 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
529 Constants.DEFAULT_HEARTBEAT_INTERVAL
532 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
535 public setSupervisionUrl(url
: string): void {
537 this.getSupervisionUrlOcppConfiguration() &&
538 isNotEmptyString(this.getSupervisionUrlOcppKey())
540 setConfigurationKeyValue(this, this.getSupervisionUrlOcppKey(), url
);
542 this.stationInfo
.supervisionUrls
= url
;
543 this.saveStationInfo();
544 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
548 public startHeartbeat(): void {
549 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
550 this.heartbeatSetInterval
= setInterval(() => {
551 this.ocppRequestService
552 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
555 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
559 }, this.getHeartbeatInterval());
561 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
562 this.getHeartbeatInterval(),
565 } else if (this.heartbeatSetInterval
) {
567 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
568 this.getHeartbeatInterval(),
573 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()},
574 not starting the heartbeat`,
579 public restartHeartbeat(): void {
581 this.stopHeartbeat();
583 this.startHeartbeat();
586 public restartWebSocketPing(): void {
587 // Stop WebSocket ping
588 this.stopWebSocketPing();
589 // Start WebSocket ping
590 this.startWebSocketPing();
593 public startMeterValues(connectorId
: number, interval
: number): void {
594 if (connectorId
=== 0) {
596 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
600 if (!this.getConnectorStatus(connectorId
)) {
602 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
607 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
609 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}
610 with no transaction started`,
614 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
615 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
618 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}
619 with no transaction id`,
624 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
625 // FIXME: Implement OCPP version agnostic helpers
626 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
629 this.getConnectorStatus(connectorId
)!.transactionId
!,
632 this.ocppRequestService
633 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
635 RequestCommand
.METER_VALUES
,
638 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
639 meterValue
: [meterValue
],
644 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
651 `${this.logPrefix()} Charging station ${
652 StandardParametersKey.MeterValueSampleInterval
653 } configuration set to ${interval}, not sending MeterValues`,
658 public stopMeterValues(connectorId
: number) {
659 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
660 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
664 public start(): void {
665 if (this.started
=== false) {
666 if (this.starting
=== false) {
667 this.starting
= true;
668 if (this.getEnableStatistics() === true) {
669 this.performanceStatistics
?.start();
671 if (this.hasFeatureProfile(SupportedFeatureProfiles
.Reservation
)) {
672 this.startReservationExpirationSetInterval();
674 this.openWSConnection();
675 // Monitor charging station template file
676 this.templateFileWatcher
= watchJsonFile(
678 FileType
.ChargingStationTemplate
,
681 (event
, filename
): void => {
682 if (isNotEmptyString(filename
) && event
=== 'change') {
685 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
687 } file have changed, reload`,
689 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
692 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
)!);
694 this.stopAutomaticTransactionGenerator();
695 delete this.automaticTransactionGeneratorConfiguration
;
696 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
697 this.startAutomaticTransactionGenerator();
699 if (this.getEnableStatistics() === true) {
700 this.performanceStatistics
?.restart();
702 this.performanceStatistics
?.stop();
704 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
707 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
715 parentPort
?.postMessage(buildStartedMessage(this));
716 this.starting
= false;
718 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
721 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
725 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
726 if (this.started
=== true) {
727 if (this.stopping
=== false) {
728 this.stopping
= true;
729 await this.stopMessageSequence(reason
);
730 this.closeWSConnection();
731 if (this.getEnableStatistics() === true) {
732 this.performanceStatistics
?.stop();
734 if (this.hasFeatureProfile(SupportedFeatureProfiles
.Reservation
)) {
735 this.stopReservationExpirationSetInterval();
737 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
738 this.templateFileWatcher
?.close();
739 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
740 delete this.bootNotificationResponse
;
741 this.started
= false;
742 this.saveConfiguration();
743 parentPort
?.postMessage(buildStoppedMessage(this));
744 this.stopping
= false;
746 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
749 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
753 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
754 await this.stop(reason
);
755 await sleep(this.stationInfo
.resetTime
!);
760 public saveOcppConfiguration(): void {
761 if (this.getOcppPersistentConfiguration()) {
762 this.saveConfiguration();
766 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
767 return getConfigurationKey(
769 StandardParametersKey
.SupportedFeatureProfiles
,
770 )?.value
?.includes(featureProfile
);
773 public bufferMessage(message
: string): void {
774 this.messageBuffer
.add(message
);
777 public openWSConnection(
778 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
779 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
781 terminateOpened
: false,
784 options
= { handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()), ...options
};
785 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
786 if (this.started
=== false && this.starting
=== false) {
788 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()}
789 on stopped charging station`,
794 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
795 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
797 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
799 if (params
?.closeOpened
) {
800 this.closeWSConnection();
802 if (params
?.terminateOpened
) {
803 this.terminateWSConnection();
806 if (this.isWebSocketConnectionOpened() === true) {
808 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()}
815 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
818 this.wsConnection
= new WebSocket(
819 this.wsConnectionUrl
,
820 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
824 // Handle WebSocket message
825 this.wsConnection
.on(
827 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
829 // Handle WebSocket error
830 this.wsConnection
.on(
832 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
834 // Handle WebSocket close
835 this.wsConnection
.on(
837 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
839 // Handle WebSocket open
840 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
841 // Handle WebSocket ping
842 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
843 // Handle WebSocket pong
844 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
847 public closeWSConnection(): void {
848 if (this.isWebSocketConnectionOpened() === true) {
849 this.wsConnection
?.close();
850 this.wsConnection
= null;
854 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
855 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration
)) {
856 let automaticTransactionGeneratorConfiguration
:
857 | AutomaticTransactionGeneratorConfiguration
859 const automaticTransactionGeneratorConfigurationFromFile
=
860 this.getConfigurationFromFile()?.automaticTransactionGenerator
;
862 this.getAutomaticTransactionGeneratorPersistentConfiguration() &&
863 automaticTransactionGeneratorConfigurationFromFile
865 automaticTransactionGeneratorConfiguration
=
866 automaticTransactionGeneratorConfigurationFromFile
;
868 automaticTransactionGeneratorConfiguration
=
869 this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
871 this.automaticTransactionGeneratorConfiguration
= {
872 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
873 ...automaticTransactionGeneratorConfiguration
,
876 return this.automaticTransactionGeneratorConfiguration
!;
879 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
880 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
883 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
884 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
885 if (isNotEmptyArray(connectorIds
)) {
886 for (const connectorId
of connectorIds
!) {
887 this.automaticTransactionGenerator
?.startConnector(connectorId
);
890 this.automaticTransactionGenerator
?.start();
892 this.saveAutomaticTransactionGeneratorConfiguration();
893 parentPort
?.postMessage(buildUpdatedMessage(this));
896 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
897 if (isNotEmptyArray(connectorIds
)) {
898 for (const connectorId
of connectorIds
!) {
899 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
902 this.automaticTransactionGenerator
?.stop();
904 this.saveAutomaticTransactionGeneratorConfiguration();
905 parentPort
?.postMessage(buildUpdatedMessage(this));
908 public async stopTransactionOnConnector(
910 reason
= StopTransactionReason
.NONE
,
911 ): Promise
<StopTransactionResponse
> {
912 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
914 this.getBeginEndMeterValues() === true &&
915 this.getOcppStrictCompliance() === true &&
916 this.getOutOfOrderEndMeterValues() === false
918 // FIXME: Implement OCPP version agnostic helpers
919 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
922 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
924 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
926 RequestCommand
.METER_VALUES
,
930 meterValue
: [transactionEndMeterValue
],
934 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
936 RequestCommand
.STOP_TRANSACTION
,
939 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
945 public getReservationOnConnectorId0Enabled(): boolean {
946 return convertToBoolean(
947 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
,
951 public async addReservation(reservation
: Reservation
): Promise
<void> {
952 const [exists
, reservationFound
] = this.doesReservationExists(reservation
);
954 await this.removeReservation(
956 ReservationTerminationReason
.REPLACE_EXISTING
,
959 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
960 await OCPPServiceUtils
.sendAndSetConnectorStatus(
962 reservation
.connectorId
,
963 ConnectorStatusEnum
.Reserved
,
965 { send
: reservation
.connectorId
!== 0 },
969 public async removeReservation(
970 reservation
: Reservation
,
971 reason
?: ReservationTerminationReason
,
973 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
975 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
976 delete connector
.reservation
;
978 case ReservationTerminationReason
.TRANSACTION_STARTED
:
979 delete connector
.reservation
;
981 case ReservationTerminationReason
.RESERVATION_CANCELED
:
982 case ReservationTerminationReason
.REPLACE_EXISTING
:
983 case ReservationTerminationReason
.EXPIRED
:
984 await OCPPServiceUtils
.sendAndSetConnectorStatus(
986 reservation
.connectorId
,
987 ConnectorStatusEnum
.Available
,
989 { send
: reservation
.connectorId
!== 0 },
991 delete connector
.reservation
;
998 public getReservationBy(
999 filterKey
: ReservationFilterKey
,
1000 value
: number | string,
1001 ): Reservation
| undefined {
1002 if (this.hasEvses
) {
1003 for (const evseStatus
of this.evses
.values()) {
1004 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1005 if (connectorStatus
?.reservation
?.[filterKey
as keyof Reservation
] === value
) {
1006 return connectorStatus
.reservation
;
1011 for (const connectorStatus
of this.connectors
.values()) {
1012 if (connectorStatus
?.reservation
?.[filterKey
as keyof Reservation
] === value
) {
1013 return connectorStatus
.reservation
;
1019 public doesReservationExists(
1020 reservation
: Partial
<Reservation
>,
1021 ): [boolean, Reservation
| undefined] {
1022 const foundReservation
= this.getReservationBy(
1023 ReservationFilterKey
.RESERVATION_ID
,
1026 return isUndefined(foundReservation
) ? [false, undefined] : [true, foundReservation
];
1029 public startReservationExpirationSetInterval(customInterval
?: number): void {
1031 customInterval
?? Constants
.DEFAULT_RESERVATION_EXPIRATION_OBSERVATION_INTERVAL
;
1034 `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds(
1038 this.reservationExpirationSetInterval
= setInterval((): void => {
1039 const currentDate
= new Date();
1040 if (this.hasEvses
) {
1041 for (const evseStatus
of this.evses
.values()) {
1042 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1044 connectorStatus
.reservation
&&
1045 connectorStatus
.reservation
.expiryDate
< currentDate
1047 this.removeReservation(
1048 connectorStatus
.reservation
,
1049 ReservationTerminationReason
.EXPIRED
,
1050 ).catch(Constants
.EMPTY_FUNCTION
);
1055 for (const connectorStatus
of this.connectors
.values()) {
1057 connectorStatus
.reservation
&&
1058 connectorStatus
.reservation
.expiryDate
< currentDate
1060 this.removeReservation(
1061 connectorStatus
.reservation
,
1062 ReservationTerminationReason
.EXPIRED
,
1063 ).catch(Constants
.EMPTY_FUNCTION
);
1071 public restartReservationExpiryDateSetInterval(): void {
1072 this.stopReservationExpirationSetInterval();
1073 this.startReservationExpirationSetInterval();
1076 public validateIncomingRequestWithReservation(connectorId
: number, idTag
: string): boolean {
1077 return this.getReservationBy(ReservationFilterKey
.CONNECTOR_ID
, connectorId
)?.idTag
=== idTag
;
1080 public isConnectorReservable(
1081 reservationId
: number,
1083 connectorId
?: number,
1085 const [alreadyExists
] = this.doesReservationExists({ id
: reservationId
});
1086 if (alreadyExists
) {
1087 return alreadyExists
;
1089 const userReservedAlready
= isUndefined(
1090 this.getReservationBy(ReservationFilterKey
.ID_TAG
, idTag
!),
1094 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
1095 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
1096 return !alreadyExists
&& !userReservedAlready
&& notConnectorZero
&& freeConnectorsAvailable
;
1099 private getNumberOfReservableConnectors(): number {
1100 let reservableConnectors
= 0;
1101 if (this.hasEvses
) {
1102 for (const evseStatus
of this.evses
.values()) {
1103 reservableConnectors
+= countReservableConnectors(evseStatus
.connectors
);
1106 reservableConnectors
= countReservableConnectors(this.connectors
);
1108 return reservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
1111 private getNumberOfReservationsOnConnectorZero(): number {
1112 let numberOfReservations
= 0;
1114 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1115 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) ||
1116 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
)
1118 ++numberOfReservations
;
1120 return numberOfReservations
;
1123 private flushMessageBuffer(): void {
1124 if (this.messageBuffer
.size
> 0) {
1125 for (const message
of this.messageBuffer
.values()) {
1126 let beginId
: string | undefined;
1127 let commandName
: RequestCommand
| undefined;
1128 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1129 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1131 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1132 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1134 this.wsConnection
?.send(message
);
1135 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1137 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
1139 )} payload sent: ${message}`,
1141 this.messageBuffer
.delete(message
);
1146 private getSupervisionUrlOcppConfiguration(): boolean {
1147 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
1150 private stopReservationExpirationSetInterval(): void {
1151 if (this.reservationExpirationSetInterval
) {
1152 clearInterval(this.reservationExpirationSetInterval
);
1156 private getSupervisionUrlOcppKey(): string {
1157 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
1160 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1161 let template
: ChargingStationTemplate
| undefined;
1163 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1164 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1166 const measureId
= `${FileType.ChargingStationTemplate} read`;
1167 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1168 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1169 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1170 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1171 .update(JSON
.stringify(template
))
1173 this.sharedLRUCache
.setChargingStationTemplate(template
);
1174 this.templateFileHash
= template
.templateHash
;
1177 handleFileException(
1179 FileType
.ChargingStationTemplate
,
1180 error
as NodeJS
.ErrnoException
,
1187 private getStationInfoFromTemplate(): ChargingStationInfo
{
1188 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1189 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1190 warnTemplateKeysDeprecation(stationTemplate
, this.logPrefix(), this.templateFile
);
1191 if (stationTemplate
?.Connectors
) {
1192 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1194 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1195 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1196 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1197 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1198 createSerialNumber(stationTemplate
, stationInfo
);
1199 if (isNotEmptyArray(stationTemplate
?.power
)) {
1200 stationTemplate
.power
= stationTemplate
.power
as number[];
1201 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1202 stationInfo
.maximumPower
=
1203 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1204 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1205 : stationTemplate
.power
[powerArrayRandomIndex
];
1207 stationTemplate
.power
= stationTemplate
?.power
as number;
1208 stationInfo
.maximumPower
=
1209 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1210 ? stationTemplate
.power
* 1000
1211 : stationTemplate
.power
;
1213 stationInfo
.firmwareVersionPattern
=
1214 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1216 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1217 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1220 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1222 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1225 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1232 stationTemplate
?.firmwareUpgrade
?? {},
1234 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1235 ? secondsToMilliseconds(stationTemplate
.resetTime
!)
1236 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1237 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1241 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
1242 let stationInfo
: ChargingStationInfo
| undefined;
1243 if (this.getStationInfoPersistentConfiguration()) {
1244 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1246 delete stationInfo
?.infoHash
;
1252 private getStationInfo(): ChargingStationInfo
{
1253 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1254 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
1256 // 1. charging station info from template
1257 // 2. charging station info from configuration file
1258 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1259 return stationInfoFromFile
!;
1261 stationInfoFromFile
&&
1262 propagateSerialNumber(
1263 this.getTemplateFromFile()!,
1264 stationInfoFromFile
,
1265 stationInfoFromTemplate
,
1267 return stationInfoFromTemplate
;
1270 private saveStationInfo(): void {
1271 if (this.getStationInfoPersistentConfiguration()) {
1272 this.saveConfiguration();
1276 private getOcppPersistentConfiguration(): boolean {
1277 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1280 private getStationInfoPersistentConfiguration(): boolean {
1281 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1284 private getAutomaticTransactionGeneratorPersistentConfiguration(): boolean {
1285 return this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
?? true;
1288 private handleUnsupportedVersion(version
: OCPPVersion
) {
1289 const errorMsg
= `Unsupported protocol version '${version}' configured
1290 in template file ${this.templateFile}`;
1291 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1292 throw new BaseError(errorMsg
);
1295 private initialize(): void {
1296 const stationTemplate
= this.getTemplateFromFile()!;
1297 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1298 this.configurationFile
= join(
1299 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1300 `${getHashId(this.index, stationTemplate)}.json`,
1302 const chargingStationConfiguration
= this.getConfigurationFromFile();
1304 chargingStationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1305 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1306 (chargingStationConfiguration
?.connectorsStatus
|| chargingStationConfiguration
?.evsesStatus
)
1308 this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration
);
1310 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1312 this.stationInfo
= this.getStationInfo();
1314 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1315 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1316 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1318 const patternGroup
: number | undefined =
1319 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1320 this.stationInfo
.firmwareVersion
?.split('.').length
;
1321 const match
= this.stationInfo
1322 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1323 .slice(1, patternGroup
! + 1);
1324 const patchLevelIndex
= match
.length
- 1;
1325 match
[patchLevelIndex
] = (
1326 convertToInt(match
[patchLevelIndex
]) +
1327 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1329 this.stationInfo
.firmwareVersion
= match
?.join('.');
1331 this.saveStationInfo();
1332 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1333 if (this.getEnableStatistics() === true) {
1334 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1335 this.stationInfo
.hashId
,
1336 this.stationInfo
.chargingStationId
!,
1337 this.configuredSupervisionUrl
,
1340 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1341 this.powerDivider
= this.getPowerDivider();
1342 // OCPP configuration
1343 this.ocppConfiguration
= this.getOcppConfiguration();
1344 this.initializeOcppConfiguration();
1345 this.initializeOcppServices();
1346 if (this.stationInfo
?.autoRegister
=== true) {
1347 this.bootNotificationResponse
= {
1348 currentTime
: new Date(),
1349 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1350 status: RegistrationStatusEnumType
.ACCEPTED
,
1355 private initializeOcppServices(): void {
1356 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1357 switch (ocppVersion
) {
1358 case OCPPVersion
.VERSION_16
:
1359 this.ocppIncomingRequestService
=
1360 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1361 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1362 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1365 case OCPPVersion
.VERSION_20
:
1366 case OCPPVersion
.VERSION_201
:
1367 this.ocppIncomingRequestService
=
1368 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1369 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1370 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1374 this.handleUnsupportedVersion(ocppVersion
);
1379 private initializeOcppConfiguration(): void {
1380 if (!getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)) {
1381 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0');
1383 if (!getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)) {
1384 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false });
1387 this.getSupervisionUrlOcppConfiguration() &&
1388 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1389 !getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1391 addConfigurationKey(
1393 this.getSupervisionUrlOcppKey(),
1394 this.configuredSupervisionUrl
.href
,
1398 !this.getSupervisionUrlOcppConfiguration() &&
1399 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1400 getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1402 deleteConfigurationKey(this, this.getSupervisionUrlOcppKey(), { save
: false });
1405 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1406 !getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)
1408 addConfigurationKey(
1410 this.stationInfo
.amperageLimitationOcppKey
!,
1412 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1416 if (!getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)) {
1417 addConfigurationKey(
1419 StandardParametersKey
.SupportedFeatureProfiles
,
1420 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1423 addConfigurationKey(
1425 StandardParametersKey
.NumberOfConnectors
,
1426 this.getNumberOfConnectors().toString(),
1428 { overwrite
: true },
1430 if (!getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
)) {
1431 addConfigurationKey(
1433 StandardParametersKey
.MeterValuesSampledData
,
1434 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1437 if (!getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
)) {
1438 const connectorsPhaseRotation
: string[] = [];
1439 if (this.hasEvses
) {
1440 for (const evseStatus
of this.evses
.values()) {
1441 for (const connectorId
of evseStatus
.connectors
.keys()) {
1442 connectorsPhaseRotation
.push(
1443 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1448 for (const connectorId
of this.connectors
.keys()) {
1449 connectorsPhaseRotation
.push(
1450 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1454 addConfigurationKey(
1456 StandardParametersKey
.ConnectorPhaseRotation
,
1457 connectorsPhaseRotation
.toString(),
1460 if (!getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
)) {
1461 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
1464 !getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) &&
1465 getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)?.value
?.includes(
1466 SupportedFeatureProfiles
.LocalAuthListManagement
,
1469 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false');
1471 if (!getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)) {
1472 addConfigurationKey(
1474 StandardParametersKey
.ConnectionTimeOut
,
1475 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1478 this.saveOcppConfiguration();
1481 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1482 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1483 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1484 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1486 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1487 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1488 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1489 delete evseStatus
.connectorsStatus
;
1490 this.evses
.set(evseId
, {
1491 ...(evseStatus
as EvseStatus
),
1492 connectors
: new Map
<number, ConnectorStatus
>(
1493 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1500 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1501 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1502 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1503 throw new BaseError(errorMsg
);
1505 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1506 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1507 throw new BaseError(errorMsg
);
1511 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1512 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1513 this.initializeConnectorsFromTemplate(stationTemplate
);
1514 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1515 this.initializeEvsesFromTemplate(stationTemplate
);
1516 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1517 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1518 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1519 throw new BaseError(errorMsg
);
1521 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1522 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1523 throw new BaseError(errorMsg
);
1527 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1528 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1529 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1530 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1531 throw new BaseError(errorMsg
);
1533 if (!stationTemplate
?.Connectors
?.[0]) {
1535 `${this.logPrefix()} Charging station information from template ${
1537 } with no connector id 0 configuration`,
1540 if (stationTemplate
?.Connectors
) {
1541 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1542 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1543 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1545 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1548 const connectorsConfigChanged
=
1549 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1550 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1551 connectorsConfigChanged
&& this.connectors
.clear();
1552 this.connectorsConfigurationHash
= connectorsConfigHash
;
1553 if (templateMaxConnectors
> 0) {
1554 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1556 connectorId
=== 0 &&
1557 (!stationTemplate
?.Connectors
[connectorId
] ||
1558 this.getUseConnectorId0(stationTemplate
) === false)
1562 const templateConnectorId
=
1563 connectorId
> 0 && stationTemplate
?.randomConnectors
1564 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1566 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1567 checkStationInfoConnectorStatus(
1568 templateConnectorId
,
1573 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1575 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1576 this.saveConnectorsStatus();
1579 `${this.logPrefix()} Charging station information from template ${
1581 } with no connectors configuration defined, cannot create connectors`,
1587 `${this.logPrefix()} Charging station information from template ${
1589 } with no connectors configuration defined, using already defined connectors`,
1594 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1595 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1596 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1597 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1598 throw new BaseError(errorMsg
);
1600 if (!stationTemplate
?.Evses
?.[0]) {
1602 `${this.logPrefix()} Charging station information from template ${
1604 } with no evse id 0 configuration`,
1607 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1609 `${this.logPrefix()} Charging station information from template ${
1611 } with evse id 0 with no connector id 0 configuration`,
1614 if (stationTemplate
?.Evses
) {
1615 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1616 .update(JSON
.stringify(stationTemplate
?.Evses
))
1618 const evsesConfigChanged
=
1619 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1620 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1621 evsesConfigChanged
&& this.evses
.clear();
1622 this.evsesConfigurationHash
= evsesConfigHash
;
1623 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1624 if (templateMaxEvses
> 0) {
1625 for (const evse
in stationTemplate
.Evses
) {
1626 const evseId
= convertToInt(evse
);
1627 this.evses
.set(evseId
, {
1628 connectors
: buildConnectorsMap(
1629 stationTemplate
?.Evses
[evse
]?.Connectors
,
1633 availability
: AvailabilityType
.Operative
,
1635 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1637 this.saveEvsesStatus();
1640 `${this.logPrefix()} Charging station information from template ${
1642 } with no evses configuration defined, cannot create evses`,
1648 `${this.logPrefix()} Charging station information from template ${
1650 } with no evses configuration defined, using already defined evses`,
1655 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1656 let configuration
: ChargingStationConfiguration
| undefined;
1657 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1659 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1660 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1661 this.configurationFileHash
,
1664 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1665 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1666 configuration
= JSON
.parse(
1667 readFileSync(this.configurationFile
, 'utf8'),
1668 ) as ChargingStationConfiguration
;
1669 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1670 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1671 this.configurationFileHash
= configuration
.configurationHash
!;
1674 handleFileException(
1675 this.configurationFile
,
1676 FileType
.ChargingStationConfiguration
,
1677 error
as NodeJS
.ErrnoException
,
1682 return configuration
;
1685 private saveAutomaticTransactionGeneratorConfiguration(): void {
1686 if (this.getAutomaticTransactionGeneratorPersistentConfiguration()) {
1687 this.saveConfiguration();
1691 private saveConnectorsStatus() {
1692 this.saveConfiguration();
1695 private saveEvsesStatus() {
1696 this.saveConfiguration();
1699 private saveConfiguration(): void {
1700 if (isNotEmptyString(this.configurationFile
)) {
1702 if (!existsSync(dirname(this.configurationFile
))) {
1703 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1705 let configurationData
: ChargingStationConfiguration
= this.getConfigurationFromFile()
1706 ? cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!)
1708 if (this.getStationInfoPersistentConfiguration() && this.stationInfo
) {
1709 configurationData
.stationInfo
= this.stationInfo
;
1711 delete configurationData
.stationInfo
;
1713 if (this.getOcppPersistentConfiguration() && this.ocppConfiguration
?.configurationKey
) {
1714 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1716 delete configurationData
.configurationKey
;
1718 configurationData
= merge
<ChargingStationConfiguration
>(
1720 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1723 !this.getAutomaticTransactionGeneratorPersistentConfiguration() ||
1724 !this.getAutomaticTransactionGeneratorConfiguration()
1726 delete configurationData
.automaticTransactionGenerator
;
1728 if (this.connectors
.size
> 0) {
1729 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1731 delete configurationData
.connectorsStatus
;
1733 if (this.evses
.size
> 0) {
1734 configurationData
.evsesStatus
= buildEvsesStatus(this);
1736 delete configurationData
.evsesStatus
;
1738 delete configurationData
.configurationHash
;
1739 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1742 stationInfo
: configurationData
.stationInfo
,
1743 configurationKey
: configurationData
.configurationKey
,
1744 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1745 } as ChargingStationConfiguration
),
1748 if (this.configurationFileHash
!== configurationHash
) {
1749 AsyncLock
.acquire(AsyncLockType
.configuration
)
1751 configurationData
.configurationHash
= configurationHash
;
1752 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1753 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1754 const fileDescriptor
= openSync(this.configurationFile
, 'w');
1755 writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1756 closeSync(fileDescriptor
);
1757 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1758 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1759 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1760 this.configurationFileHash
= configurationHash
;
1763 handleFileException(
1764 this.configurationFile
,
1765 FileType
.ChargingStationConfiguration
,
1766 error
as NodeJS
.ErrnoException
,
1771 AsyncLock
.release(AsyncLockType
.configuration
).catch(Constants
.EMPTY_FUNCTION
);
1775 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1776 this.configurationFile
1781 handleFileException(
1782 this.configurationFile
,
1783 FileType
.ChargingStationConfiguration
,
1784 error
as NodeJS
.ErrnoException
,
1790 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1795 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1796 return this.getTemplateFromFile()?.Configuration
;
1799 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1800 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1801 if (this.getOcppPersistentConfiguration() === true && configurationKey
) {
1802 return { configurationKey
};
1807 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1808 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1809 this.getOcppConfigurationFromFile();
1810 if (!ocppConfiguration
) {
1811 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1813 return ocppConfiguration
;
1816 private async onOpen(): Promise
<void> {
1817 if (this.isWebSocketConnectionOpened() === true) {
1819 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1821 if (this.isRegistered() === false) {
1822 // Send BootNotification
1823 let registrationRetryCount
= 0;
1825 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1826 BootNotificationRequest
,
1827 BootNotificationResponse
1828 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1829 skipBufferingOnError
: true,
1831 if (this.isRegistered() === false) {
1832 this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount
;
1834 this?.bootNotificationResponse
?.interval
1835 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1836 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1840 this.isRegistered() === false &&
1841 (registrationRetryCount
<= this.getRegistrationMaxRetries()! ||
1842 this.getRegistrationMaxRetries() === -1)
1845 if (this.isRegistered() === true) {
1846 if (this.inAcceptedState() === true) {
1847 await this.startMessageSequence();
1851 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`,
1854 this.wsConnectionRestarted
= false;
1855 this.autoReconnectRetryCount
= 0;
1856 parentPort
?.postMessage(buildUpdatedMessage(this));
1859 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1864 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1867 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1868 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1870 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1872 )}' and reason '${reason.toString()}'`,
1874 this.autoReconnectRetryCount
= 0;
1879 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1881 )}' and reason '${reason.toString()}'`,
1883 this.started
=== true && (await this.reconnect());
1886 parentPort
?.postMessage(buildUpdatedMessage(this));
1889 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1890 const cachedRequest
= this.requests
.get(messageId
);
1891 if (Array.isArray(cachedRequest
) === true) {
1892 return cachedRequest
;
1894 throw new OCPPError(
1895 ErrorType
.PROTOCOL_ERROR
,
1896 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1898 )} is not an array`,
1900 cachedRequest
as JsonType
,
1904 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1905 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1906 if (this.getEnableStatistics() === true) {
1907 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1910 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1914 // Process the message
1915 await this.ocppIncomingRequestService
.incomingRequestHandler(
1923 private handleResponseMessage(response
: Response
): void {
1924 const [messageType
, messageId
, commandPayload
] = response
;
1925 if (this.requests
.has(messageId
) === false) {
1927 throw new OCPPError(
1928 ErrorType
.INTERNAL_ERROR
,
1929 `Response for unknown message id ${messageId}`,
1935 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1940 `${this.logPrefix()} << Command '${
1941 requestCommandName ?? Constants.UNKNOWN_COMMAND
1942 }' received response payload: ${JSON.stringify(response)}`,
1944 responseCallback(commandPayload
, requestPayload
);
1947 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1948 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1949 if (this.requests
.has(messageId
) === false) {
1951 throw new OCPPError(
1952 ErrorType
.INTERNAL_ERROR
,
1953 `Error response for unknown message id ${messageId}`,
1955 { errorType
, errorMessage
, errorDetails
},
1958 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1960 `${this.logPrefix()} << Command '${
1961 requestCommandName ?? Constants.UNKNOWN_COMMAND
1962 }' received error response payload: ${JSON.stringify(errorResponse)}`,
1964 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1967 private async onMessage(data
: RawData
): Promise
<void> {
1968 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
1969 let messageType
: number | undefined;
1970 let errorMsg
: string;
1972 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1973 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1974 if (Array.isArray(request
) === true) {
1975 [messageType
] = request
;
1976 // Check the type of message
1977 switch (messageType
) {
1979 case MessageType
.CALL_MESSAGE
:
1980 await this.handleIncomingMessage(request
as IncomingRequest
);
1983 case MessageType
.CALL_RESULT_MESSAGE
:
1984 this.handleResponseMessage(request
as Response
);
1987 case MessageType
.CALL_ERROR_MESSAGE
:
1988 this.handleErrorMessage(request
as ErrorResponse
);
1992 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1993 errorMsg
= `Wrong message type ${messageType}`;
1994 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1995 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1997 parentPort
?.postMessage(buildUpdatedMessage(this));
1999 throw new OCPPError(
2000 ErrorType
.PROTOCOL_ERROR
,
2001 'Incoming message is not an array',
2009 let commandName
: IncomingRequestCommand
| undefined;
2010 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
2011 let errorCallback
: ErrorCallback
;
2012 const [, messageId
] = request
!;
2013 switch (messageType
) {
2014 case MessageType
.CALL_MESSAGE
:
2015 [, , commandName
] = request
as IncomingRequest
;
2017 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
2019 case MessageType
.CALL_RESULT_MESSAGE
:
2020 case MessageType
.CALL_ERROR_MESSAGE
:
2021 if (this.requests
.has(messageId
) === true) {
2022 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
2023 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
2024 errorCallback(error
as OCPPError
, false);
2026 // Remove the request from the cache in case of error at response handling
2027 this.requests
.delete(messageId
);
2031 if (error
instanceof OCPPError
=== false) {
2033 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2034 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2035 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2036 }' message '${data.toString()}' handling is not an OCPPError:`,
2041 `${this.logPrefix()} Incoming OCPP command '${
2042 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2043 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2044 }' message '${data.toString()}'${
2045 messageType !== MessageType.CALL_MESSAGE
2046 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
2048 } processing error:`,
2054 private onPing(): void {
2055 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
2058 private onPong(): void {
2059 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
2062 private onError(error
: WSError
): void {
2063 this.closeWSConnection();
2064 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
2067 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
2068 if (this.getMeteringPerTransaction() === true) {
2071 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
2072 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2077 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
2078 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2082 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
2083 return stationTemplate
?.useConnectorId0
?? true;
2086 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
2087 if (this.hasEvses
) {
2088 for (const [evseId
, evseStatus
] of this.evses
) {
2092 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2093 if (connectorStatus
.transactionStarted
=== true) {
2094 await this.stopTransactionOnConnector(connectorId
, reason
);
2099 for (const connectorId
of this.connectors
.keys()) {
2100 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2101 await this.stopTransactionOnConnector(connectorId
, reason
);
2108 private getConnectionTimeout(): number {
2109 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)) {
2111 parseInt(getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)!.value
!) ??
2112 Constants
.DEFAULT_CONNECTION_TIMEOUT
2115 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2118 // -1 for unlimited, 0 for disabling
2119 private getAutoReconnectMaxRetries(): number | undefined {
2121 this.stationInfo
.autoReconnectMaxRetries
?? Configuration
.getAutoReconnectMaxRetries() ?? -1
2126 private getRegistrationMaxRetries(): number | undefined {
2127 return this.stationInfo
.registrationMaxRetries
?? -1;
2130 private getPowerDivider(): number {
2131 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2132 if (this.stationInfo
?.powerSharedByConnectors
) {
2133 powerDivider
= this.getNumberOfRunningTransactions();
2135 return powerDivider
;
2138 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
2139 const maximumPower
= this.getMaximumPower(stationInfo
);
2140 switch (this.getCurrentOutType(stationInfo
)) {
2141 case CurrentType
.AC
:
2142 return ACElectricUtils
.amperagePerPhaseFromPower(
2143 this.getNumberOfPhases(stationInfo
),
2144 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2145 this.getVoltageOut(stationInfo
),
2147 case CurrentType
.DC
:
2148 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2152 private getAmperageLimitation(): number | undefined {
2154 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2155 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)
2159 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)?.value
,
2160 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2165 private async startMessageSequence(): Promise
<void> {
2166 if (this.stationInfo
?.autoRegister
=== true) {
2167 await this.ocppRequestService
.requestHandler
<
2168 BootNotificationRequest
,
2169 BootNotificationResponse
2170 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2171 skipBufferingOnError
: true,
2174 // Start WebSocket ping
2175 this.startWebSocketPing();
2177 this.startHeartbeat();
2178 // Initialize connectors status
2179 if (this.hasEvses
) {
2180 for (const [evseId
, evseStatus
] of this.evses
) {
2182 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2183 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2184 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2187 connectorBootStatus
,
2194 for (const connectorId
of this.connectors
.keys()) {
2195 if (connectorId
> 0) {
2196 const connectorBootStatus
= getBootConnectorStatus(
2199 this.getConnectorStatus(connectorId
)!,
2201 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2205 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2206 await this.ocppRequestService
.requestHandler
<
2207 FirmwareStatusNotificationRequest
,
2208 FirmwareStatusNotificationResponse
2209 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2210 status: FirmwareStatus
.Installed
,
2212 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2216 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2217 this.startAutomaticTransactionGenerator();
2219 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2222 private async stopMessageSequence(
2223 reason
: StopTransactionReason
= StopTransactionReason
.NONE
,
2225 // Stop WebSocket ping
2226 this.stopWebSocketPing();
2228 this.stopHeartbeat();
2229 // Stop ongoing transactions
2230 if (this.automaticTransactionGenerator
?.started
=== true) {
2231 this.stopAutomaticTransactionGenerator();
2233 await this.stopRunningTransactions(reason
);
2235 if (this.hasEvses
) {
2236 for (const [evseId
, evseStatus
] of this.evses
) {
2238 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2239 await this.ocppRequestService
.requestHandler
<
2240 StatusNotificationRequest
,
2241 StatusNotificationResponse
2244 RequestCommand
.STATUS_NOTIFICATION
,
2245 OCPPServiceUtils
.buildStatusNotificationRequest(
2248 ConnectorStatusEnum
.Unavailable
,
2252 delete connectorStatus
?.status;
2257 for (const connectorId
of this.connectors
.keys()) {
2258 if (connectorId
> 0) {
2259 await this.ocppRequestService
.requestHandler
<
2260 StatusNotificationRequest
,
2261 StatusNotificationResponse
2264 RequestCommand
.STATUS_NOTIFICATION
,
2265 OCPPServiceUtils
.buildStatusNotificationRequest(
2268 ConnectorStatusEnum
.Unavailable
,
2271 delete this.getConnectorStatus(connectorId
)?.status;
2277 private startWebSocketPing(): void {
2278 const webSocketPingInterval
: number = getConfigurationKey(
2280 StandardParametersKey
.WebSocketPingInterval
,
2282 ? convertToInt(getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
)
2284 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2285 this.webSocketPingSetInterval
= setInterval(() => {
2286 if (this.isWebSocketConnectionOpened() === true) {
2287 this.wsConnection
?.ping();
2289 }, secondsToMilliseconds(webSocketPingInterval
));
2291 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2292 webSocketPingInterval,
2295 } else if (this.webSocketPingSetInterval
) {
2297 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2298 webSocketPingInterval,
2303 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2308 private stopWebSocketPing(): void {
2309 if (this.webSocketPingSetInterval
) {
2310 clearInterval(this.webSocketPingSetInterval
);
2311 delete this.webSocketPingSetInterval
;
2315 private getConfiguredSupervisionUrl(): URL
{
2316 let configuredSupervisionUrl
: string;
2317 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2318 if (isNotEmptyArray(supervisionUrls
)) {
2319 let configuredSupervisionUrlIndex
: number;
2320 switch (Configuration
.getSupervisionUrlDistribution()) {
2321 case SupervisionUrlDistribution
.RANDOM
:
2322 configuredSupervisionUrlIndex
= Math.floor(
2323 secureRandom() * (supervisionUrls
as string[]).length
,
2326 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2327 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2329 Object.values(SupervisionUrlDistribution
).includes(
2330 Configuration
.getSupervisionUrlDistribution()!,
2333 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2334 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2335 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2338 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2341 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2343 configuredSupervisionUrl
= supervisionUrls
as string;
2345 if (isNotEmptyString(configuredSupervisionUrl
)) {
2346 return new URL(configuredSupervisionUrl
);
2348 const errorMsg
= 'No supervision url(s) configured';
2349 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2350 throw new BaseError(`${errorMsg}`);
2353 private stopHeartbeat(): void {
2354 if (this.heartbeatSetInterval
) {
2355 clearInterval(this.heartbeatSetInterval
);
2356 delete this.heartbeatSetInterval
;
2360 private terminateWSConnection(): void {
2361 if (this.isWebSocketConnectionOpened() === true) {
2362 this.wsConnection
?.terminate();
2363 this.wsConnection
= null;
2367 private getReconnectExponentialDelay(): boolean {
2368 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2371 private async reconnect(): Promise
<void> {
2372 // Stop WebSocket ping
2373 this.stopWebSocketPing();
2375 this.stopHeartbeat();
2376 // Stop the ATG if needed
2377 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2378 this.stopAutomaticTransactionGenerator();
2381 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries()! ||
2382 this.getAutoReconnectMaxRetries() === -1
2384 ++this.autoReconnectRetryCount
;
2385 const reconnectDelay
= this.getReconnectExponentialDelay()
2386 ? exponentialDelay(this.autoReconnectRetryCount
)
2387 : secondsToMilliseconds(this.getConnectionTimeout());
2388 const reconnectDelayWithdraw
= 1000;
2389 const reconnectTimeout
=
2390 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2391 ? reconnectDelay
- reconnectDelayWithdraw
2394 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2397 )}ms, timeout ${reconnectTimeout}ms`,
2399 await sleep(reconnectDelay
);
2401 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2403 this.openWSConnection(
2405 ...(this.stationInfo
?.wsOptions
?? {}),
2406 handshakeTimeout
: reconnectTimeout
,
2408 { closeOpened
: true },
2410 this.wsConnectionRestarted
= true;
2411 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2413 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2414 this.autoReconnectRetryCount
2415 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`,