1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { createHash
} from
'node:crypto';
4 import { EventEmitter
} from
'node:events';
5 import { type FSWatcher
, existsSync
, mkdirSync
, readFileSync
, writeFileSync
} from
'node:fs';
6 import { dirname
, join
} from
'node:path';
7 import { URL
} from
'node:url';
8 import { parentPort
} from
'node:worker_threads';
10 import { millisecondsToSeconds
, secondsToMilliseconds
} from
'date-fns';
11 import merge from
'just-merge';
12 import { type RawData
, WebSocket
} from
'ws';
14 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator';
15 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel';
18 deleteConfigurationKey
,
20 setConfigurationKeyValue
,
21 } from
'./ConfigurationKeyUtils';
25 checkConnectorsConfiguration
,
26 checkStationInfoConnectorStatus
,
28 createBootNotificationRequest
,
30 getAmperageLimitationUnitDivider
,
31 getBootConnectorStatus
,
32 getChargingStationConnectorChargingProfilesPowerLimit
,
38 getNumberOfReservableConnectors
,
39 getPhaseRotationValue
,
41 hasReservationExpired
,
42 initializeConnectorsMapStatus
,
43 propagateSerialNumber
,
44 removeExpiredReservations
,
45 stationTemplateToStationInfo
,
46 warnTemplateKeysDeprecation
,
48 import { IdTagsCache
} from
'./IdTagsCache';
50 OCPP16IncomingRequestService
,
52 OCPP16ResponseService
,
54 OCPP20IncomingRequestService
,
56 OCPP20ResponseService
,
57 type OCPPIncomingRequestService
,
58 type OCPPRequestService
,
61 import { SharedLRUCache
} from
'./SharedLRUCache';
62 import { BaseError
, OCPPError
} from
'../exception';
63 import { PerformanceStatistics
} from
'../performance';
65 type AutomaticTransactionGeneratorConfiguration
,
67 type BootNotificationRequest
,
68 type BootNotificationResponse
,
70 type ChargingStationConfiguration
,
71 ChargingStationEvents
,
72 type ChargingStationInfo
,
73 type ChargingStationOcppConfiguration
,
74 type ChargingStationTemplate
,
82 type EvseStatusConfiguration
,
85 type FirmwareStatusNotificationRequest
,
86 type FirmwareStatusNotificationResponse
,
88 type HeartbeatRequest
,
89 type HeartbeatResponse
,
91 type IncomingRequestCommand
,
96 type MeterValuesRequest
,
97 type MeterValuesResponse
,
101 RegistrationStatusEnumType
,
105 ReservationTerminationReason
,
107 StandardParametersKey
,
109 type StatusNotificationRequest
,
110 type StatusNotificationResponse
,
111 StopTransactionReason
,
112 type StopTransactionRequest
,
113 type StopTransactionResponse
,
114 SupervisionUrlDistribution
,
115 SupportedFeatureProfiles
,
119 WebSocketCloseEventStatusCode
,
129 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
130 buildConnectorsStatus
,
139 formatDurationMilliSeconds
,
140 formatDurationSeconds
,
142 getWebSocketCloseEventStatusString
,
158 export class ChargingStation
extends EventEmitter
{
159 public readonly index
: number;
160 public readonly templateFile
: string;
161 public stationInfo
!: ChargingStationInfo
;
162 public started
: boolean;
163 public starting
: boolean;
164 public idTagsCache
: IdTagsCache
;
165 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
166 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
167 public wsConnection
: WebSocket
| null;
168 public readonly connectors
: Map
<number, ConnectorStatus
>;
169 public readonly evses
: Map
<number, EvseStatus
>;
170 public readonly requests
: Map
<string, CachedRequest
>;
171 public performanceStatistics
!: PerformanceStatistics
| undefined;
172 public heartbeatSetInterval
?: NodeJS
.Timeout
;
173 public ocppRequestService
!: OCPPRequestService
;
174 public bootNotificationRequest
!: BootNotificationRequest
;
175 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
176 public powerDivider
!: number;
177 private stopping
: boolean;
178 private configurationFile
!: string;
179 private configurationFileHash
!: string;
180 private connectorsConfigurationHash
!: string;
181 private evsesConfigurationHash
!: string;
182 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
;
183 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
184 private readonly messageBuffer
: Set
<string>;
185 private configuredSupervisionUrl
!: URL
;
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) {
196 this.started
= false;
197 this.starting
= false;
198 this.stopping
= false;
199 this.wsConnection
= null;
200 this.autoReconnectRetryCount
= 0;
202 this.templateFile
= templateFile
;
203 this.connectors
= new Map
<number, ConnectorStatus
>();
204 this.evses
= new Map
<number, EvseStatus
>();
205 this.requests
= new Map
<string, CachedRequest
>();
206 this.messageBuffer
= new Set
<string>();
207 this.sharedLRUCache
= SharedLRUCache
.getInstance();
208 this.idTagsCache
= IdTagsCache
.getInstance();
209 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
211 this.on(ChargingStationEvents
.started
, () => {
212 parentPort
?.postMessage(buildStartedMessage(this));
214 this.on(ChargingStationEvents
.stopped
, () => {
215 parentPort
?.postMessage(buildStoppedMessage(this));
217 this.on(ChargingStationEvents
.updated
, () => {
218 parentPort
?.postMessage(buildUpdatedMessage(this));
224 public get
hasEvses(): boolean {
225 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
228 private get
wsConnectionUrl(): URL
{
231 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
232 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
233 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value)
234 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value
235 : this.configuredSupervisionUrl.href
236 }/${this.stationInfo.chargingStationId}`,
240 public logPrefix
= (): string => {
243 (isNotEmptyString(this?.stationInfo?.chargingStationId)
244 ? this?.stationInfo?.chargingStationId
245 : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
246 'Error at building log prefix'
251 public hasIdTags(): boolean {
252 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
255 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
256 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
257 switch (this.getCurrentOutType(stationInfo
)) {
259 return localStationInfo
.numberOfPhases
?? 3;
265 public isWebSocketConnectionOpened(): boolean {
266 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
269 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
270 return this?.bootNotificationResponse
?.status;
273 public inUnknownState(): boolean {
274 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
277 public inPendingState(): boolean {
278 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
281 public inAcceptedState(): boolean {
282 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
285 public inRejectedState(): boolean {
286 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
289 public isRegistered(): boolean {
291 this.inUnknownState() === false &&
292 (this.inAcceptedState() === true || this.inPendingState() === true)
296 public isChargingStationAvailable(): boolean {
297 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
300 public hasConnector(connectorId
: number): boolean {
302 for (const evseStatus
of this.evses
.values()) {
303 if (evseStatus
.connectors
.has(connectorId
)) {
309 return this.connectors
.has(connectorId
);
312 public isConnectorAvailable(connectorId
: number): boolean {
315 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
319 public getNumberOfConnectors(): number {
321 let numberOfConnectors
= 0;
322 for (const [evseId
, evseStatus
] of this.evses
) {
324 numberOfConnectors
+= evseStatus
.connectors
.size
;
327 return numberOfConnectors
;
329 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
332 public getNumberOfEvses(): number {
333 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
336 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
338 for (const evseStatus
of this.evses
.values()) {
339 if (evseStatus
.connectors
.has(connectorId
)) {
340 return evseStatus
.connectors
.get(connectorId
);
345 return this.connectors
.get(connectorId
);
348 public getConnectorMaximumAvailablePower(connectorId
: number): number {
349 let connectorAmperageLimitationPowerLimit
: number | undefined;
351 !isNullOrUndefined(this.getAmperageLimitation()) &&
352 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
354 connectorAmperageLimitationPowerLimit
=
355 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
356 ? ACElectricUtils
.powerTotal(
357 this.getNumberOfPhases(),
358 this.stationInfo
.voltageOut
!,
359 this.getAmperageLimitation()! *
360 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
362 : DCElectricUtils
.power(this.stationInfo
.voltageOut
!, this.getAmperageLimitation()!)) /
365 const connectorMaximumPower
= this.stationInfo
.maximumPower
! / this.powerDivider
;
366 const connectorChargingProfilesPowerLimit
=
367 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
369 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
370 isNaN(connectorAmperageLimitationPowerLimit
!)
372 : connectorAmperageLimitationPowerLimit
!,
373 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
377 public getTransactionIdTag(transactionId
: number): string | undefined {
379 for (const evseStatus
of this.evses
.values()) {
380 for (const connectorStatus
of evseStatus
.connectors
.values()) {
381 if (connectorStatus
.transactionId
=== transactionId
) {
382 return connectorStatus
.transactionIdTag
;
387 for (const connectorId
of this.connectors
.keys()) {
388 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
389 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
395 public getNumberOfRunningTransactions(): number {
396 let numberOfRunningTransactions
= 0;
398 for (const [evseId
, evseStatus
] of this.evses
) {
402 for (const connectorStatus
of evseStatus
.connectors
.values()) {
403 if (connectorStatus
.transactionStarted
=== true) {
404 ++numberOfRunningTransactions
;
409 for (const connectorId
of this.connectors
.keys()) {
410 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
411 ++numberOfRunningTransactions
;
415 return numberOfRunningTransactions
;
418 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
420 for (const evseStatus
of this.evses
.values()) {
421 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
422 if (connectorStatus
.transactionId
=== transactionId
) {
428 for (const connectorId
of this.connectors
.keys()) {
429 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
436 public getEnergyActiveImportRegisterByTransactionId(
437 transactionId
: number,
440 return this.getEnergyActiveImportRegister(
441 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
446 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
447 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
450 public getAuthorizeRemoteTxRequests(): boolean {
451 const authorizeRemoteTxRequests
= getConfigurationKey(
453 StandardParametersKey
.AuthorizeRemoteTxRequests
,
455 return authorizeRemoteTxRequests
!== undefined
456 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
460 public getLocalAuthListEnabled(): boolean {
461 const localAuthListEnabled
= getConfigurationKey(
463 StandardParametersKey
.LocalAuthListEnabled
,
465 return localAuthListEnabled
!== undefined
466 ? convertToBoolean(localAuthListEnabled
.value
)
470 public getHeartbeatInterval(): number {
471 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
);
472 if (HeartbeatInterval
!== undefined) {
473 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
));
475 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
);
476 if (HeartBeatInterval
!== undefined) {
477 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
));
479 this.stationInfo
?.autoRegister
=== false &&
481 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
482 Constants.DEFAULT_HEARTBEAT_INTERVAL
485 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
488 public setSupervisionUrl(url
: string): void {
490 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
491 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
)
493 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
!, url
);
495 this.stationInfo
.supervisionUrls
= url
;
496 this.saveStationInfo();
497 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
501 public startHeartbeat(): void {
502 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
503 this.heartbeatSetInterval
= setInterval(() => {
504 this.ocppRequestService
505 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
508 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
512 }, this.getHeartbeatInterval());
514 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
515 this.getHeartbeatInterval(),
518 } else if (this.heartbeatSetInterval
) {
520 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
521 this.getHeartbeatInterval(),
526 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`,
531 public restartHeartbeat(): void {
533 this.stopHeartbeat();
535 this.startHeartbeat();
538 public restartWebSocketPing(): void {
539 // Stop WebSocket ping
540 this.stopWebSocketPing();
541 // Start WebSocket ping
542 this.startWebSocketPing();
545 public startMeterValues(connectorId
: number, interval
: number): void {
546 if (connectorId
=== 0) {
548 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
552 if (!this.getConnectorStatus(connectorId
)) {
554 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
559 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
561 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`,
565 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
566 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
569 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`,
574 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
575 // FIXME: Implement OCPP version agnostic helpers
576 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
579 this.getConnectorStatus(connectorId
)!.transactionId
!,
582 this.ocppRequestService
583 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
585 RequestCommand
.METER_VALUES
,
588 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
589 meterValue
: [meterValue
],
594 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
601 `${this.logPrefix()} Charging station ${
602 StandardParametersKey.MeterValueSampleInterval
603 } configuration set to ${interval}, not sending MeterValues`,
608 public stopMeterValues(connectorId
: number) {
609 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
610 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
614 public start(): void {
615 if (this.started
=== false) {
616 if (this.starting
=== false) {
617 this.starting
= true;
618 if (this.stationInfo
?.enableStatistics
=== true) {
619 this.performanceStatistics
?.start();
621 if (hasFeatureProfile(this, SupportedFeatureProfiles
.Reservation
)) {
622 this.startReservationExpirationSetInterval();
624 this.openWSConnection();
625 // Monitor charging station template file
626 this.templateFileWatcher
= watchJsonFile(
628 FileType
.ChargingStationTemplate
,
631 (event
, filename
): void => {
632 if (isNotEmptyString(filename
) && event
=== 'change') {
635 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
637 } file have changed, reload`,
639 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
642 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
)!);
644 this.stopAutomaticTransactionGenerator();
645 delete this.automaticTransactionGeneratorConfiguration
;
646 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
647 this.startAutomaticTransactionGenerator();
649 if (this.stationInfo
?.enableStatistics
=== true) {
650 this.performanceStatistics
?.restart();
652 this.performanceStatistics
?.stop();
654 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
657 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
665 this.emit(ChargingStationEvents
.started
);
666 this.starting
= false;
668 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
671 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
675 public async stop(reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
676 if (this.started
=== true) {
677 if (this.stopping
=== false) {
678 this.stopping
= true;
679 await this.stopMessageSequence(reason
, stopTransactions
);
680 this.closeWSConnection();
681 if (this.stationInfo
?.enableStatistics
=== true) {
682 this.performanceStatistics
?.stop();
684 if (hasFeatureProfile(this, SupportedFeatureProfiles
.Reservation
)) {
685 this.stopReservationExpirationSetInterval();
687 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
688 this.templateFileWatcher
?.close();
689 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
690 delete this.bootNotificationResponse
;
691 this.started
= false;
692 this.saveConfiguration();
693 this.emit(ChargingStationEvents
.stopped
);
694 this.stopping
= false;
696 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
699 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
703 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
704 await this.stop(reason
);
705 await sleep(this.stationInfo
.resetTime
!);
710 public saveOcppConfiguration(): void {
711 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
712 this.saveConfiguration();
716 public bufferMessage(message
: string): void {
717 this.messageBuffer
.add(message
);
720 public openWSConnection(
722 params
?: { closeOpened
?: boolean; terminateOpened
?: boolean },
725 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
726 ...this.stationInfo
?.wsOptions
,
729 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
730 if (!checkChargingStation(this, this.logPrefix())) {
734 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
735 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
737 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
739 if (params
?.closeOpened
) {
740 this.closeWSConnection();
742 if (params
?.terminateOpened
) {
743 this.terminateWSConnection();
746 if (this.isWebSocketConnectionOpened() === true) {
748 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`,
754 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
757 this.wsConnection
= new WebSocket(
758 this.wsConnectionUrl
,
759 `ocpp${this.stationInfo?.ocppVersion}`,
763 // Handle WebSocket message
764 this.wsConnection
.on(
766 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
768 // Handle WebSocket error
769 this.wsConnection
.on(
771 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
773 // Handle WebSocket close
774 this.wsConnection
.on(
776 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
778 // Handle WebSocket open
779 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
780 // Handle WebSocket ping
781 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
782 // Handle WebSocket pong
783 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
786 public closeWSConnection(): void {
787 if (this.isWebSocketConnectionOpened() === true) {
788 this.wsConnection
?.close();
789 this.wsConnection
= null;
793 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
794 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration
)) {
795 let automaticTransactionGeneratorConfiguration
:
796 | AutomaticTransactionGeneratorConfiguration
798 const stationTemplate
= this.getTemplateFromFile();
799 const stationConfiguration
= this.getConfigurationFromFile();
801 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
802 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
803 stationConfiguration
?.automaticTransactionGenerator
805 automaticTransactionGeneratorConfiguration
=
806 stationConfiguration
?.automaticTransactionGenerator
;
808 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
;
810 this.automaticTransactionGeneratorConfiguration
= {
811 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
812 ...automaticTransactionGeneratorConfiguration
,
815 return this.automaticTransactionGeneratorConfiguration
!;
818 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
819 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
822 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
823 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
824 if (isNotEmptyArray(connectorIds
)) {
825 for (const connectorId
of connectorIds
!) {
826 this.automaticTransactionGenerator
?.startConnector(connectorId
);
829 this.automaticTransactionGenerator
?.start();
831 this.saveAutomaticTransactionGeneratorConfiguration();
832 this.emit(ChargingStationEvents
.updated
);
835 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
836 if (isNotEmptyArray(connectorIds
)) {
837 for (const connectorId
of connectorIds
!) {
838 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
841 this.automaticTransactionGenerator
?.stop();
843 this.saveAutomaticTransactionGeneratorConfiguration();
844 this.emit(ChargingStationEvents
.updated
);
847 public async stopTransactionOnConnector(
849 reason
?: StopTransactionReason
,
850 ): Promise
<StopTransactionResponse
> {
851 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
853 this.stationInfo
?.beginEndMeterValues
=== true &&
854 this.stationInfo
?.ocppStrictCompliance
=== true &&
855 this.stationInfo
?.outOfOrderEndMeterValues
=== false
857 // FIXME: Implement OCPP version agnostic helpers
858 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
861 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
863 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
865 RequestCommand
.METER_VALUES
,
869 meterValue
: [transactionEndMeterValue
],
873 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
875 RequestCommand
.STOP_TRANSACTION
,
878 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
879 ...(isNullOrUndefined(reason
) && { reason
}),
884 public getReserveConnectorZeroSupported(): boolean {
885 return convertToBoolean(
886 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
,
890 public async addReservation(reservation
: Reservation
): Promise
<void> {
891 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
);
892 if (reservationFound
!== undefined) {
893 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
);
895 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
896 await OCPPServiceUtils
.sendAndSetConnectorStatus(
898 reservation
.connectorId
,
899 ConnectorStatusEnum
.Reserved
,
901 { send
: reservation
.connectorId
!== 0 },
905 public async removeReservation(
906 reservation
: Reservation
,
907 reason
: ReservationTerminationReason
,
909 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
911 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
912 case ReservationTerminationReason
.TRANSACTION_STARTED
:
913 delete connector
.reservation
;
915 case ReservationTerminationReason
.RESERVATION_CANCELED
:
916 case ReservationTerminationReason
.REPLACE_EXISTING
:
917 case ReservationTerminationReason
.EXPIRED
:
918 await OCPPServiceUtils
.sendAndSetConnectorStatus(
920 reservation
.connectorId
,
921 ConnectorStatusEnum
.Available
,
923 { send
: reservation
.connectorId
!== 0 },
925 delete connector
.reservation
;
928 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
929 throw new BaseError(`Unknown reservation termination reason '${reason}'`);
933 public getReservationBy(
934 filterKey
: ReservationKey
,
935 value
: number | string,
936 ): Reservation
| undefined {
938 for (const evseStatus
of this.evses
.values()) {
939 for (const connectorStatus
of evseStatus
.connectors
.values()) {
940 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
941 return connectorStatus
.reservation
;
946 for (const connectorStatus
of this.connectors
.values()) {
947 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
948 return connectorStatus
.reservation
;
954 public isConnectorReservable(
955 reservationId
: number,
957 connectorId
?: number,
959 const reservation
= this.getReservationBy('reservationId', reservationId
);
960 const reservationExists
= !isUndefined(reservation
) && !hasReservationExpired(reservation
!);
961 if (arguments.length
=== 1) {
962 return !reservationExists
;
963 } else if (arguments.length
> 1) {
964 const userReservation
= !isUndefined(idTag
)
965 ? this.getReservationBy('idTag', idTag
!)
967 const userReservationExists
=
968 !isUndefined(userReservation
) && !hasReservationExpired(userReservation
!);
969 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
970 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
972 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
978 private startReservationExpirationSetInterval(customInterval
?: number): void {
979 const interval
= customInterval
?? Constants
.DEFAULT_RESERVATION_EXPIRATION_INTERVAL
;
982 `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds(
986 this.reservationExpirationSetInterval
= setInterval((): void => {
987 removeExpiredReservations(this).catch(Constants
.EMPTY_FUNCTION
);
992 private stopReservationExpirationSetInterval(): void {
993 if (!isNullOrUndefined(this.reservationExpirationSetInterval
)) {
994 clearInterval(this.reservationExpirationSetInterval
);
998 // private restartReservationExpiryDateSetInterval(): void {
999 // this.stopReservationExpirationSetInterval();
1000 // this.startReservationExpirationSetInterval();
1003 private getNumberOfReservableConnectors(): number {
1004 let numberOfReservableConnectors
= 0;
1005 if (this.hasEvses
) {
1006 for (const evseStatus
of this.evses
.values()) {
1007 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
);
1010 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
);
1012 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
1015 private getNumberOfReservationsOnConnectorZero(): number {
1017 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1018 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) ||
1019 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
)
1026 private flushMessageBuffer(): void {
1027 if (this.messageBuffer
.size
> 0) {
1028 for (const message
of this.messageBuffer
.values()) {
1029 let beginId
: string | undefined;
1030 let commandName
: RequestCommand
| undefined;
1031 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1032 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1034 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1035 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1037 this.wsConnection
?.send(message
, (error
?: Error) => {
1038 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1039 if (isNullOrUndefined(error
)) {
1041 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
1043 )} payload sent: ${message}`,
1045 this.messageBuffer
.delete(message
);
1052 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1053 let template
: ChargingStationTemplate
| undefined;
1055 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1056 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1058 const measureId
= `${FileType.ChargingStationTemplate} read`;
1059 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1060 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1061 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1062 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1063 .update(JSON
.stringify(template
))
1065 this.sharedLRUCache
.setChargingStationTemplate(template
);
1066 this.templateFileHash
= template
.templateHash
;
1069 handleFileException(
1071 FileType
.ChargingStationTemplate
,
1072 error
as NodeJS
.ErrnoException
,
1079 private getStationInfoFromTemplate(): ChargingStationInfo
{
1080 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1081 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1082 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this);
1083 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
);
1084 if (stationTemplate
?.Connectors
) {
1085 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1087 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1088 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1089 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1090 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1091 createSerialNumber(stationTemplate
, stationInfo
);
1092 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
);
1093 if (isNotEmptyArray(stationTemplate
?.power
)) {
1094 stationTemplate
.power
= stationTemplate
.power
as number[];
1095 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1096 stationInfo
.maximumPower
=
1097 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1098 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1099 : stationTemplate
.power
[powerArrayRandomIndex
];
1101 stationTemplate
.power
= stationTemplate
?.power
as number;
1102 stationInfo
.maximumPower
=
1103 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1104 ? stationTemplate
.power
* 1000
1105 : stationTemplate
.power
;
1107 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1108 stationInfo
.firmwareVersionPattern
=
1109 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1111 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1112 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1115 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1117 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1120 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1127 stationTemplate
?.firmwareUpgrade
?? {},
1129 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1130 ? secondsToMilliseconds(stationTemplate
.resetTime
!)
1131 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1135 private getStationInfoFromFile(
1136 stationInfoPersistentConfiguration
: boolean,
1137 ): ChargingStationInfo
| undefined {
1138 let stationInfo
: ChargingStationInfo
| undefined;
1139 if (stationInfoPersistentConfiguration
=== true) {
1140 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1142 delete stationInfo
?.infoHash
;
1148 private getStationInfo(): ChargingStationInfo
{
1149 const defaultStationInfo
: Partial
<ChargingStationInfo
> = {
1150 enableStatistics
: false,
1151 remoteAuthorization
: true,
1152 currentOutType
: CurrentType
.AC
,
1153 mainVoltageMeterValues
: true,
1154 phaseLineToLineVoltageMeterValues
: false,
1155 customValueLimitationMeterValues
: true,
1156 ocppStrictCompliance
: true,
1157 outOfOrderEndMeterValues
: false,
1158 beginEndMeterValues
: false,
1159 meteringPerTransaction
: true,
1160 transactionDataMeterValues
: false,
1161 supervisionUrlOcppConfiguration
: false,
1162 supervisionUrlOcppKey
: VendorParametersKey
.ConnectionUrl
,
1163 ocppVersion
: OCPPVersion
.VERSION_16
,
1164 ocppPersistentConfiguration
: true,
1165 stationInfoPersistentConfiguration
: true,
1166 automaticTransactionGeneratorPersistentConfiguration
: true,
1167 autoReconnectMaxRetries
: -1,
1168 registrationMaxRetries
: -1,
1169 reconnectExponentialDelay
: false,
1170 stopTransactionsOnStopped
: true,
1172 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1173 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile(
1174 stationInfoFromTemplate
?.stationInfoPersistentConfiguration
?? true,
1177 // 1. charging station info from template
1178 // 2. charging station info from configuration file
1179 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1180 return { ...defaultStationInfo
, ...stationInfoFromFile
! };
1182 stationInfoFromFile
&&
1183 propagateSerialNumber(
1184 this.getTemplateFromFile()!,
1185 stationInfoFromFile
,
1186 stationInfoFromTemplate
,
1188 return { ...defaultStationInfo
, ...stationInfoFromTemplate
};
1191 private saveStationInfo(): void {
1192 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1193 this.saveConfiguration();
1197 private handleUnsupportedVersion(version
: OCPPVersion
| undefined) {
1198 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1199 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1200 throw new BaseError(errorMsg
);
1203 private initialize(): void {
1204 const stationTemplate
= this.getTemplateFromFile()!;
1205 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1206 this.configurationFile
= join(
1207 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1208 `${getHashId(this.index, stationTemplate)}.json`,
1210 const stationConfiguration
= this.getConfigurationFromFile();
1212 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1213 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1214 (stationConfiguration
?.connectorsStatus
|| stationConfiguration
?.evsesStatus
)
1216 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
);
1218 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1220 this.stationInfo
= this.getStationInfo();
1222 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1223 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1224 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1226 const patternGroup
: number | undefined =
1227 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1228 this.stationInfo
.firmwareVersion
?.split('.').length
;
1229 const match
= this.stationInfo
1230 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1231 .slice(1, patternGroup
! + 1);
1232 const patchLevelIndex
= match
.length
- 1;
1233 match
[patchLevelIndex
] = (
1234 convertToInt(match
[patchLevelIndex
]) +
1235 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1237 this.stationInfo
.firmwareVersion
= match
?.join('.');
1239 this.saveStationInfo();
1240 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1241 if (this.stationInfo
?.enableStatistics
=== true) {
1242 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1243 this.stationInfo
.hashId
,
1244 this.stationInfo
.chargingStationId
!,
1245 this.configuredSupervisionUrl
,
1248 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1249 this.powerDivider
= this.getPowerDivider();
1250 // OCPP configuration
1251 this.ocppConfiguration
= this.getOcppConfiguration();
1252 this.initializeOcppConfiguration();
1253 this.initializeOcppServices();
1254 this.once(ChargingStationEvents
.accepted
, () => {
1255 this.startMessageSequence().catch((error
) => {
1256 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
);
1259 if (this.stationInfo
?.autoRegister
=== true) {
1260 this.bootNotificationResponse
= {
1261 currentTime
: new Date(),
1262 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1263 status: RegistrationStatusEnumType
.ACCEPTED
,
1268 private initializeOcppServices(): void {
1269 const ocppVersion
= this.stationInfo
?.ocppVersion
;
1270 switch (ocppVersion
) {
1271 case OCPPVersion
.VERSION_16
:
1272 this.ocppIncomingRequestService
=
1273 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1274 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1275 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1278 case OCPPVersion
.VERSION_20
:
1279 case OCPPVersion
.VERSION_201
:
1280 this.ocppIncomingRequestService
=
1281 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1282 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1283 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1287 this.handleUnsupportedVersion(ocppVersion
);
1292 private initializeOcppConfiguration(): void {
1293 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
))) {
1294 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0');
1296 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
))) {
1297 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false });
1300 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1301 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1302 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1304 addConfigurationKey(
1306 this.stationInfo
.supervisionUrlOcppKey
!,
1307 this.configuredSupervisionUrl
.href
,
1311 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1312 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1313 !isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1315 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!, { save
: false });
1318 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1319 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!))
1321 addConfigurationKey(
1323 this.stationInfo
.amperageLimitationOcppKey
!,
1325 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1330 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
))
1332 addConfigurationKey(
1334 StandardParametersKey
.SupportedFeatureProfiles
,
1335 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1338 addConfigurationKey(
1340 StandardParametersKey
.NumberOfConnectors
,
1341 this.getNumberOfConnectors().toString(),
1343 { overwrite
: true },
1346 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
))
1348 addConfigurationKey(
1350 StandardParametersKey
.MeterValuesSampledData
,
1351 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1355 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
))
1357 const connectorsPhaseRotation
: string[] = [];
1358 if (this.hasEvses
) {
1359 for (const evseStatus
of this.evses
.values()) {
1360 for (const connectorId
of evseStatus
.connectors
.keys()) {
1361 connectorsPhaseRotation
.push(
1362 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1367 for (const connectorId
of this.connectors
.keys()) {
1368 connectorsPhaseRotation
.push(
1369 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1373 addConfigurationKey(
1375 StandardParametersKey
.ConnectorPhaseRotation
,
1376 connectorsPhaseRotation
.toString(),
1380 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
))
1382 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
1385 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
)) &&
1386 getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)?.value
?.includes(
1387 SupportedFeatureProfiles
.LocalAuthListManagement
,
1390 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false');
1392 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
))) {
1393 addConfigurationKey(
1395 StandardParametersKey
.ConnectionTimeOut
,
1396 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1399 this.saveOcppConfiguration();
1402 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1403 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1404 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1405 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1407 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1408 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1409 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1410 delete evseStatus
.connectorsStatus
;
1411 this.evses
.set(evseId
, {
1412 ...(evseStatus
as EvseStatus
),
1413 connectors
: new Map
<number, ConnectorStatus
>(
1414 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1421 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1422 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1423 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1424 throw new BaseError(errorMsg
);
1426 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1427 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1428 throw new BaseError(errorMsg
);
1432 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1433 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1434 this.initializeConnectorsFromTemplate(stationTemplate
);
1435 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1436 this.initializeEvsesFromTemplate(stationTemplate
);
1437 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1438 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1439 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1440 throw new BaseError(errorMsg
);
1442 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1443 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1444 throw new BaseError(errorMsg
);
1448 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1449 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1450 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1451 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1452 throw new BaseError(errorMsg
);
1454 if (!stationTemplate
?.Connectors
?.[0]) {
1456 `${this.logPrefix()} Charging station information from template ${
1458 } with no connector id 0 configuration`,
1461 if (stationTemplate
?.Connectors
) {
1462 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1463 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1464 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1466 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1469 const connectorsConfigChanged
=
1470 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1471 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1472 connectorsConfigChanged
&& this.connectors
.clear();
1473 this.connectorsConfigurationHash
= connectorsConfigHash
;
1474 if (templateMaxConnectors
> 0) {
1475 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1477 connectorId
=== 0 &&
1478 (!stationTemplate
?.Connectors
?.[connectorId
] ||
1479 this.getUseConnectorId0(stationTemplate
) === false)
1483 const templateConnectorId
=
1484 connectorId
> 0 && stationTemplate
?.randomConnectors
1485 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1487 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1488 checkStationInfoConnectorStatus(
1489 templateConnectorId
,
1494 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1496 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1497 this.saveConnectorsStatus();
1500 `${this.logPrefix()} Charging station information from template ${
1502 } with no connectors configuration defined, cannot create connectors`,
1508 `${this.logPrefix()} Charging station information from template ${
1510 } with no connectors configuration defined, using already defined connectors`,
1515 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1516 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1517 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1518 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1519 throw new BaseError(errorMsg
);
1521 if (!stationTemplate
?.Evses
?.[0]) {
1523 `${this.logPrefix()} Charging station information from template ${
1525 } with no evse id 0 configuration`,
1528 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1530 `${this.logPrefix()} Charging station information from template ${
1532 } with evse id 0 with no connector id 0 configuration`,
1535 if (Object.keys(stationTemplate
?.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1537 `${this.logPrefix()} Charging station information from template ${
1539 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`,
1542 if (stationTemplate
?.Evses
) {
1543 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1544 .update(JSON
.stringify(stationTemplate
?.Evses
))
1546 const evsesConfigChanged
=
1547 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1548 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1549 evsesConfigChanged
&& this.evses
.clear();
1550 this.evsesConfigurationHash
= evsesConfigHash
;
1551 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1552 if (templateMaxEvses
> 0) {
1553 for (const evseKey
in stationTemplate
.Evses
) {
1554 const evseId
= convertToInt(evseKey
);
1555 this.evses
.set(evseId
, {
1556 connectors
: buildConnectorsMap(
1557 stationTemplate
?.Evses
[evseKey
]?.Connectors
,
1561 availability
: AvailabilityType
.Operative
,
1563 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1565 this.saveEvsesStatus();
1568 `${this.logPrefix()} Charging station information from template ${
1570 } with no evses configuration defined, cannot create evses`,
1576 `${this.logPrefix()} Charging station information from template ${
1578 } with no evses configuration defined, using already defined evses`,
1583 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1584 let configuration
: ChargingStationConfiguration
| undefined;
1585 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1587 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1588 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1589 this.configurationFileHash
,
1592 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1593 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1594 configuration
= JSON
.parse(
1595 readFileSync(this.configurationFile
, 'utf8'),
1596 ) as ChargingStationConfiguration
;
1597 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1598 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1599 this.configurationFileHash
= configuration
.configurationHash
!;
1602 handleFileException(
1603 this.configurationFile
,
1604 FileType
.ChargingStationConfiguration
,
1605 error
as NodeJS
.ErrnoException
,
1610 return configuration
;
1613 private saveAutomaticTransactionGeneratorConfiguration(): void {
1614 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1615 this.saveConfiguration();
1619 private saveConnectorsStatus() {
1620 this.saveConfiguration();
1623 private saveEvsesStatus() {
1624 this.saveConfiguration();
1627 private saveConfiguration(): void {
1628 if (isNotEmptyString(this.configurationFile
)) {
1630 if (!existsSync(dirname(this.configurationFile
))) {
1631 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1633 let configurationData
: ChargingStationConfiguration
= this.getConfigurationFromFile()
1634 ? cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!)
1636 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true && this.stationInfo
) {
1637 configurationData
.stationInfo
= this.stationInfo
;
1639 delete configurationData
.stationInfo
;
1642 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1643 this.ocppConfiguration
?.configurationKey
1645 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1647 delete configurationData
.configurationKey
;
1649 configurationData
= merge
<ChargingStationConfiguration
>(
1651 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1654 !this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
||
1655 !this.getAutomaticTransactionGeneratorConfiguration()
1657 delete configurationData
.automaticTransactionGenerator
;
1659 if (this.connectors
.size
> 0) {
1660 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1662 delete configurationData
.connectorsStatus
;
1664 if (this.evses
.size
> 0) {
1665 configurationData
.evsesStatus
= buildEvsesStatus(this);
1667 delete configurationData
.evsesStatus
;
1669 delete configurationData
.configurationHash
;
1670 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1673 stationInfo
: configurationData
.stationInfo
,
1674 configurationKey
: configurationData
.configurationKey
,
1675 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1676 ...(this.connectors
.size
> 0 && {
1677 connectorsStatus
: configurationData
.connectorsStatus
,
1679 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
}),
1680 } as ChargingStationConfiguration
),
1683 if (this.configurationFileHash
!== configurationHash
) {
1684 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1685 configurationData
.configurationHash
= configurationHash
;
1686 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1687 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1689 this.configurationFile
,
1690 JSON
.stringify(configurationData
, undefined, 2),
1693 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1694 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1695 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1696 this.configurationFileHash
= configurationHash
;
1697 }).catch((error
) => {
1698 handleFileException(
1699 this.configurationFile
,
1700 FileType
.ChargingStationConfiguration
,
1701 error
as NodeJS
.ErrnoException
,
1707 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1708 this.configurationFile
1713 handleFileException(
1714 this.configurationFile
,
1715 FileType
.ChargingStationConfiguration
,
1716 error
as NodeJS
.ErrnoException
,
1722 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1727 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1728 return this.getTemplateFromFile()?.Configuration
;
1731 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1732 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1733 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1734 return { configurationKey
};
1739 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1740 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1741 this.getOcppConfigurationFromFile();
1742 if (!ocppConfiguration
) {
1743 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1745 return ocppConfiguration
;
1748 private async onOpen(): Promise
<void> {
1749 if (this.isWebSocketConnectionOpened() === true) {
1751 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1753 let registrationRetryCount
= 0;
1754 if (this.isRegistered() === false) {
1755 // Send BootNotification
1757 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1758 BootNotificationRequest
,
1759 BootNotificationResponse
1760 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1761 skipBufferingOnError
: true,
1763 if (this.isRegistered() === false) {
1764 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
;
1766 this?.bootNotificationResponse
?.interval
1767 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1768 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1772 this.isRegistered() === false &&
1773 (registrationRetryCount
<= this.stationInfo
.registrationMaxRetries
! ||
1774 this.stationInfo
?.registrationMaxRetries
=== -1)
1777 if (this.isRegistered() === true) {
1778 this.emit(ChargingStationEvents
.registered
);
1779 if (this.inAcceptedState() === true) {
1780 this.emit(ChargingStationEvents
.accepted
);
1784 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this
1785 .stationInfo?.registrationMaxRetries})`,
1788 this.autoReconnectRetryCount
= 0;
1789 this.emit(ChargingStationEvents
.updated
);
1792 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1797 private async onClose(code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): Promise
<void> {
1800 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1801 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1803 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1805 )}' and reason '${reason.toString()}'`,
1807 this.autoReconnectRetryCount
= 0;
1812 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1814 )}' and reason '${reason.toString()}'`,
1816 this.started
=== true && (await this.reconnect());
1819 this.emit(ChargingStationEvents
.updated
);
1822 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1823 const cachedRequest
= this.requests
.get(messageId
);
1824 if (Array.isArray(cachedRequest
) === true) {
1825 return cachedRequest
;
1827 throw new OCPPError(
1828 ErrorType
.PROTOCOL_ERROR
,
1829 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1831 )} is not an array`,
1833 cachedRequest
as JsonType
,
1837 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1838 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1839 if (this.stationInfo
?.enableStatistics
=== true) {
1840 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1843 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1847 // Process the message
1848 await this.ocppIncomingRequestService
.incomingRequestHandler(
1854 this.emit(ChargingStationEvents
.updated
);
1857 private handleResponseMessage(response
: Response
): void {
1858 const [messageType
, messageId
, commandPayload
] = response
;
1859 if (this.requests
.has(messageId
) === false) {
1861 throw new OCPPError(
1862 ErrorType
.INTERNAL_ERROR
,
1863 `Response for unknown message id ${messageId}`,
1869 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1874 `${this.logPrefix()} << Command '${
1875 requestCommandName ?? Constants.UNKNOWN_COMMAND
1876 }' received response payload: ${JSON.stringify(response)}`,
1878 responseCallback(commandPayload
, requestPayload
);
1881 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1882 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1883 if (this.requests
.has(messageId
) === false) {
1885 throw new OCPPError(
1886 ErrorType
.INTERNAL_ERROR
,
1887 `Error response for unknown message id ${messageId}`,
1889 { errorType
, errorMessage
, errorDetails
},
1892 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1894 `${this.logPrefix()} << Command '${
1895 requestCommandName ?? Constants.UNKNOWN_COMMAND
1896 }' received error response payload: ${JSON.stringify(errorResponse)}`,
1898 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1901 private async onMessage(data
: RawData
): Promise
<void> {
1902 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
1903 let messageType
: MessageType
| undefined;
1904 let errorMsg
: string;
1906 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1907 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1908 if (Array.isArray(request
) === true) {
1909 [messageType
] = request
;
1910 // Check the type of message
1911 switch (messageType
) {
1913 case MessageType
.CALL_MESSAGE
:
1914 await this.handleIncomingMessage(request
as IncomingRequest
);
1917 case MessageType
.CALL_RESULT_MESSAGE
:
1918 this.handleResponseMessage(request
as Response
);
1921 case MessageType
.CALL_ERROR_MESSAGE
:
1922 this.handleErrorMessage(request
as ErrorResponse
);
1926 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1927 errorMsg
= `Wrong message type ${messageType}`;
1928 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1929 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1932 throw new OCPPError(
1933 ErrorType
.PROTOCOL_ERROR
,
1934 'Incoming message is not an array',
1942 let commandName
: IncomingRequestCommand
| undefined;
1943 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
1944 let errorCallback
: ErrorCallback
;
1945 const [, messageId
] = request
!;
1946 switch (messageType
) {
1947 case MessageType
.CALL_MESSAGE
:
1948 [, , commandName
] = request
as IncomingRequest
;
1950 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1952 case MessageType
.CALL_RESULT_MESSAGE
:
1953 case MessageType
.CALL_ERROR_MESSAGE
:
1954 if (this.requests
.has(messageId
) === true) {
1955 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1956 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1957 errorCallback(error
as OCPPError
, false);
1959 // Remove the request from the cache in case of error at response handling
1960 this.requests
.delete(messageId
);
1964 if (error
instanceof OCPPError
=== false) {
1966 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1967 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1968 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1969 }' message '${data.toString()}' handling is not an OCPPError:`,
1974 `${this.logPrefix()} Incoming OCPP command '${
1975 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1976 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1977 }' message '${data.toString()}'${
1978 messageType !== MessageType.CALL_MESSAGE
1979 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1981 } processing error:`,
1987 private onPing(): void {
1988 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1991 private onPong(): void {
1992 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1995 private onError(error
: WSError
): void {
1996 this.closeWSConnection();
1997 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
2000 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
2001 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2004 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
2005 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2010 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
2011 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2015 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
2016 return stationTemplate
?.useConnectorId0
?? true;
2019 private async stopRunningTransactions(reason
?: StopTransactionReason
): Promise
<void> {
2020 if (this.hasEvses
) {
2021 for (const [evseId
, evseStatus
] of this.evses
) {
2025 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2026 if (connectorStatus
.transactionStarted
=== true) {
2027 await this.stopTransactionOnConnector(connectorId
, reason
);
2032 for (const connectorId
of this.connectors
.keys()) {
2033 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2034 await this.stopTransactionOnConnector(connectorId
, reason
);
2041 private getConnectionTimeout(): number {
2042 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) !== undefined) {
2043 return convertToInt(
2044 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)!.value
! ??
2045 Constants
.DEFAULT_CONNECTION_TIMEOUT
,
2048 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2051 private getPowerDivider(): number {
2052 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2053 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2054 powerDivider
= this.getNumberOfRunningTransactions();
2056 return powerDivider
;
2059 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
2060 const maximumPower
= this.getMaximumPower(stationInfo
);
2061 switch (this.getCurrentOutType(stationInfo
)) {
2062 case CurrentType
.AC
:
2063 return ACElectricUtils
.amperagePerPhaseFromPower(
2064 this.getNumberOfPhases(stationInfo
),
2065 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2066 this.getVoltageOut(stationInfo
),
2068 case CurrentType
.DC
:
2069 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2073 private getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
2074 return (stationInfo
?? this.stationInfo
).maximumPower
!;
2077 private getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
2078 return (stationInfo
?? this.stationInfo
).currentOutType
?? CurrentType
.AC
;
2081 private getVoltageOut(stationInfo
?: ChargingStationInfo
): Voltage
{
2083 (stationInfo
?? this.stationInfo
).voltageOut
??
2084 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2088 private getAmperageLimitation(): number | undefined {
2090 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2091 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!) !== undefined
2095 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)?.value
,
2096 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2101 private async startMessageSequence(): Promise
<void> {
2102 if (this.stationInfo
?.autoRegister
=== true) {
2103 await this.ocppRequestService
.requestHandler
<
2104 BootNotificationRequest
,
2105 BootNotificationResponse
2106 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2107 skipBufferingOnError
: true,
2110 // Start WebSocket ping
2111 this.startWebSocketPing();
2113 this.startHeartbeat();
2114 // Initialize connectors status
2115 if (this.hasEvses
) {
2116 for (const [evseId
, evseStatus
] of this.evses
) {
2118 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2119 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2120 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2123 connectorBootStatus
,
2130 for (const connectorId
of this.connectors
.keys()) {
2131 if (connectorId
> 0) {
2132 const connectorBootStatus
= getBootConnectorStatus(
2135 this.getConnectorStatus(connectorId
)!,
2137 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2141 if (this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
) {
2142 await this.ocppRequestService
.requestHandler
<
2143 FirmwareStatusNotificationRequest
,
2144 FirmwareStatusNotificationResponse
2145 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2146 status: FirmwareStatus
.Installed
,
2148 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2152 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
2153 this.startAutomaticTransactionGenerator();
2155 this.flushMessageBuffer();
2158 private async stopMessageSequence(
2159 reason
?: StopTransactionReason
,
2160 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
,
2162 // Stop WebSocket ping
2163 this.stopWebSocketPing();
2165 this.stopHeartbeat();
2167 if (this.automaticTransactionGenerator
?.started
=== true) {
2168 this.stopAutomaticTransactionGenerator();
2170 // Stop ongoing transactions
2171 stopTransactions
&& (await this.stopRunningTransactions(reason
));
2172 if (this.hasEvses
) {
2173 for (const [evseId
, evseStatus
] of this.evses
) {
2175 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2176 await this.ocppRequestService
.requestHandler
<
2177 StatusNotificationRequest
,
2178 StatusNotificationResponse
2181 RequestCommand
.STATUS_NOTIFICATION
,
2182 OCPPServiceUtils
.buildStatusNotificationRequest(
2185 ConnectorStatusEnum
.Unavailable
,
2189 delete connectorStatus
?.status;
2194 for (const connectorId
of this.connectors
.keys()) {
2195 if (connectorId
> 0) {
2196 await this.ocppRequestService
.requestHandler
<
2197 StatusNotificationRequest
,
2198 StatusNotificationResponse
2201 RequestCommand
.STATUS_NOTIFICATION
,
2202 OCPPServiceUtils
.buildStatusNotificationRequest(
2205 ConnectorStatusEnum
.Unavailable
,
2208 delete this.getConnectorStatus(connectorId
)?.status;
2214 private startWebSocketPing(): void {
2215 const webSocketPingInterval
: number =
2216 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) !== undefined
2218 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
,
2221 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2222 this.webSocketPingSetInterval
= setInterval(() => {
2223 if (this.isWebSocketConnectionOpened() === true) {
2224 this.wsConnection
?.ping();
2226 }, secondsToMilliseconds(webSocketPingInterval
));
2228 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2229 webSocketPingInterval,
2232 } else if (this.webSocketPingSetInterval
) {
2234 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2235 webSocketPingInterval,
2240 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2245 private stopWebSocketPing(): void {
2246 if (this.webSocketPingSetInterval
) {
2247 clearInterval(this.webSocketPingSetInterval
);
2248 delete this.webSocketPingSetInterval
;
2252 private getConfiguredSupervisionUrl(): URL
{
2253 let configuredSupervisionUrl
: string;
2254 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2255 if (isNotEmptyArray(supervisionUrls
)) {
2256 let configuredSupervisionUrlIndex
: number;
2257 switch (Configuration
.getSupervisionUrlDistribution()) {
2258 case SupervisionUrlDistribution
.RANDOM
:
2259 configuredSupervisionUrlIndex
= Math.floor(
2260 secureRandom() * (supervisionUrls
as string[]).length
,
2263 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2264 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2266 Object.values(SupervisionUrlDistribution
).includes(
2267 Configuration
.getSupervisionUrlDistribution()!,
2270 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2271 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2272 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2275 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2278 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2280 configuredSupervisionUrl
= supervisionUrls
as string;
2282 if (isNotEmptyString(configuredSupervisionUrl
)) {
2283 return new URL(configuredSupervisionUrl
);
2285 const errorMsg
= 'No supervision url(s) configured';
2286 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2287 throw new BaseError(`${errorMsg}`);
2290 private stopHeartbeat(): void {
2291 if (this.heartbeatSetInterval
) {
2292 clearInterval(this.heartbeatSetInterval
);
2293 delete this.heartbeatSetInterval
;
2297 private terminateWSConnection(): void {
2298 if (this.isWebSocketConnectionOpened() === true) {
2299 this.wsConnection
?.terminate();
2300 this.wsConnection
= null;
2304 private async reconnect(): Promise
<void> {
2305 // Stop WebSocket ping
2306 this.stopWebSocketPing();
2308 this.stopHeartbeat();
2309 // Stop the ATG if needed
2310 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2311 this.stopAutomaticTransactionGenerator();
2314 this.autoReconnectRetryCount
< this.stationInfo
.autoReconnectMaxRetries
! ||
2315 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2317 ++this.autoReconnectRetryCount
;
2318 const reconnectDelay
=
2319 this.stationInfo
?.reconnectExponentialDelay
=== true
2320 ? exponentialDelay(this.autoReconnectRetryCount
)
2321 : secondsToMilliseconds(this.getConnectionTimeout());
2322 const reconnectDelayWithdraw
= 1000;
2323 const reconnectTimeout
=
2324 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2325 ? reconnectDelay
- reconnectDelayWithdraw
2328 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2331 )}ms, timeout ${reconnectTimeout}ms`,
2333 await sleep(reconnectDelay
);
2335 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2337 this.openWSConnection(
2339 handshakeTimeout
: reconnectTimeout
,
2341 { closeOpened
: true },
2343 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2345 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2346 this.autoReconnectRetryCount
2347 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`,