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
,
40 hasReservationExpired
,
41 initializeConnectorsMapStatus
,
42 propagateSerialNumber
,
43 stationTemplateToStationInfo
,
44 warnTemplateKeysDeprecation
,
46 import { IdTagsCache
} from
'./IdTagsCache';
48 OCPP16IncomingRequestService
,
50 OCPP16ResponseService
,
52 OCPP20IncomingRequestService
,
54 OCPP20ResponseService
,
55 type OCPPIncomingRequestService
,
56 type OCPPRequestService
,
59 import { SharedLRUCache
} from
'./SharedLRUCache';
60 import { BaseError
, OCPPError
} from
'../exception';
61 import { PerformanceStatistics
} from
'../performance';
63 type AutomaticTransactionGeneratorConfiguration
,
65 type BootNotificationRequest
,
66 type BootNotificationResponse
,
68 type ChargingStationConfiguration
,
69 ChargingStationEvents
,
70 type ChargingStationInfo
,
71 type ChargingStationOcppConfiguration
,
72 type ChargingStationTemplate
,
80 type EvseStatusConfiguration
,
83 type FirmwareStatusNotificationRequest
,
84 type FirmwareStatusNotificationResponse
,
86 type HeartbeatRequest
,
87 type HeartbeatResponse
,
89 type IncomingRequestCommand
,
94 type MeterValuesRequest
,
95 type MeterValuesResponse
,
99 RegistrationStatusEnumType
,
103 ReservationTerminationReason
,
105 StandardParametersKey
,
107 type StatusNotificationRequest
,
108 type StatusNotificationResponse
,
109 StopTransactionReason
,
110 type StopTransactionRequest
,
111 type StopTransactionResponse
,
112 SupervisionUrlDistribution
,
113 SupportedFeatureProfiles
,
117 WebSocketCloseEventStatusCode
,
127 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
128 buildConnectorsStatus
,
137 formatDurationMilliSeconds
,
138 formatDurationSeconds
,
140 getWebSocketCloseEventStatusString
,
156 export class ChargingStation
extends EventEmitter
{
157 public readonly index
: number;
158 public readonly templateFile
: string;
159 public stationInfo
!: ChargingStationInfo
;
160 public started
: boolean;
161 public starting
: boolean;
162 public idTagsCache
: IdTagsCache
;
163 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
164 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
165 public wsConnection
: WebSocket
| null;
166 public readonly connectors
: Map
<number, ConnectorStatus
>;
167 public readonly evses
: Map
<number, EvseStatus
>;
168 public readonly requests
: Map
<string, CachedRequest
>;
169 public performanceStatistics
!: PerformanceStatistics
| undefined;
170 public heartbeatSetInterval
?: NodeJS
.Timeout
;
171 public ocppRequestService
!: OCPPRequestService
;
172 public bootNotificationRequest
!: BootNotificationRequest
;
173 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
174 public powerDivider
!: number;
175 private stopping
: boolean;
176 private configurationFile
!: string;
177 private configurationFileHash
!: string;
178 private connectorsConfigurationHash
!: string;
179 private evsesConfigurationHash
!: string;
180 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
;
181 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
182 private readonly messageBuffer
: Set
<string>;
183 private configuredSupervisionUrl
!: URL
;
184 private autoReconnectRetryCount
: number;
185 private templateFileWatcher
!: FSWatcher
| undefined;
186 private templateFileHash
!: string;
187 private readonly sharedLRUCache
: SharedLRUCache
;
188 private webSocketPingSetInterval
?: NodeJS
.Timeout
;
189 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
190 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
;
192 constructor(index
: number, templateFile
: string) {
194 this.started
= false;
195 this.starting
= false;
196 this.stopping
= false;
197 this.wsConnection
= null;
198 this.autoReconnectRetryCount
= 0;
200 this.templateFile
= templateFile
;
201 this.connectors
= new Map
<number, ConnectorStatus
>();
202 this.evses
= new Map
<number, EvseStatus
>();
203 this.requests
= new Map
<string, CachedRequest
>();
204 this.messageBuffer
= new Set
<string>();
205 this.sharedLRUCache
= SharedLRUCache
.getInstance();
206 this.idTagsCache
= IdTagsCache
.getInstance();
207 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
209 this.on(ChargingStationEvents
.started
, () => {
210 parentPort
?.postMessage(buildStartedMessage(this));
212 this.on(ChargingStationEvents
.stopped
, () => {
213 parentPort
?.postMessage(buildStoppedMessage(this));
215 this.on(ChargingStationEvents
.updated
, () => {
216 parentPort
?.postMessage(buildUpdatedMessage(this));
222 public get
hasEvses(): boolean {
223 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
226 private get
wsConnectionUrl(): URL
{
229 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
230 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
231 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value)
232 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value
233 : this.configuredSupervisionUrl.href
234 }/${this.stationInfo.chargingStationId}`,
238 public logPrefix
= (): string => {
241 (isNotEmptyString(this?.stationInfo?.chargingStationId)
242 ? this?.stationInfo?.chargingStationId
243 : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
244 'Error at building log prefix'
249 public hasIdTags(): boolean {
250 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
253 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
254 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
255 switch (this.getCurrentOutType(stationInfo
)) {
257 return localStationInfo
.numberOfPhases
?? 3;
263 public isWebSocketConnectionOpened(): boolean {
264 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
267 public inUnknownState(): boolean {
268 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
271 public inPendingState(): boolean {
272 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
275 public inAcceptedState(): boolean {
276 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
279 public inRejectedState(): boolean {
280 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
283 public isRegistered(): boolean {
285 this.inUnknownState() === false &&
286 (this.inAcceptedState() === true || this.inPendingState() === true)
290 public isChargingStationAvailable(): boolean {
291 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
294 public hasConnector(connectorId
: number): boolean {
296 for (const evseStatus
of this.evses
.values()) {
297 if (evseStatus
.connectors
.has(connectorId
)) {
303 return this.connectors
.has(connectorId
);
306 public isConnectorAvailable(connectorId
: number): boolean {
309 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
313 public getNumberOfConnectors(): number {
315 let numberOfConnectors
= 0;
316 for (const [evseId
, evseStatus
] of this.evses
) {
318 numberOfConnectors
+= evseStatus
.connectors
.size
;
321 return numberOfConnectors
;
323 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
326 public getNumberOfEvses(): number {
327 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
330 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
332 for (const evseStatus
of this.evses
.values()) {
333 if (evseStatus
.connectors
.has(connectorId
)) {
334 return evseStatus
.connectors
.get(connectorId
);
339 return this.connectors
.get(connectorId
);
342 public getConnectorMaximumAvailablePower(connectorId
: number): number {
343 let connectorAmperageLimitationPowerLimit
: number | undefined;
345 !isNullOrUndefined(this.getAmperageLimitation()) &&
346 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
348 connectorAmperageLimitationPowerLimit
=
349 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
350 ? ACElectricUtils
.powerTotal(
351 this.getNumberOfPhases(),
352 this.stationInfo
.voltageOut
!,
353 this.getAmperageLimitation()! *
354 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
356 : DCElectricUtils
.power(this.stationInfo
.voltageOut
!, this.getAmperageLimitation()!)) /
359 const connectorMaximumPower
= this.stationInfo
.maximumPower
! / this.powerDivider
;
360 const connectorChargingProfilesPowerLimit
=
361 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
363 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
364 isNaN(connectorAmperageLimitationPowerLimit
!)
366 : connectorAmperageLimitationPowerLimit
!,
367 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
371 public getTransactionIdTag(transactionId
: number): string | undefined {
373 for (const evseStatus
of this.evses
.values()) {
374 for (const connectorStatus
of evseStatus
.connectors
.values()) {
375 if (connectorStatus
.transactionId
=== transactionId
) {
376 return connectorStatus
.transactionIdTag
;
381 for (const connectorId
of this.connectors
.keys()) {
382 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
383 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
389 public getNumberOfRunningTransactions(): number {
390 let numberOfRunningTransactions
= 0;
392 for (const [evseId
, evseStatus
] of this.evses
) {
396 for (const connectorStatus
of evseStatus
.connectors
.values()) {
397 if (connectorStatus
.transactionStarted
=== true) {
398 ++numberOfRunningTransactions
;
403 for (const connectorId
of this.connectors
.keys()) {
404 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
405 ++numberOfRunningTransactions
;
409 return numberOfRunningTransactions
;
412 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
414 for (const evseStatus
of this.evses
.values()) {
415 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
416 if (connectorStatus
.transactionId
=== transactionId
) {
422 for (const connectorId
of this.connectors
.keys()) {
423 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
430 public getEnergyActiveImportRegisterByTransactionId(
431 transactionId
: number,
434 return this.getEnergyActiveImportRegister(
435 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
440 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
441 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
444 public getAuthorizeRemoteTxRequests(): boolean {
445 const authorizeRemoteTxRequests
= getConfigurationKey(
447 StandardParametersKey
.AuthorizeRemoteTxRequests
,
449 return authorizeRemoteTxRequests
!== undefined
450 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
454 public getLocalAuthListEnabled(): boolean {
455 const localAuthListEnabled
= getConfigurationKey(
457 StandardParametersKey
.LocalAuthListEnabled
,
459 return localAuthListEnabled
!== undefined
460 ? convertToBoolean(localAuthListEnabled
.value
)
464 public getHeartbeatInterval(): number {
465 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
);
466 if (HeartbeatInterval
!== undefined) {
467 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
));
469 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
);
470 if (HeartBeatInterval
!== undefined) {
471 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
));
473 this.stationInfo
?.autoRegister
=== false &&
475 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
476 Constants.DEFAULT_HEARTBEAT_INTERVAL
479 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
482 public setSupervisionUrl(url
: string): void {
484 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
485 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
)
487 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
!, url
);
489 this.stationInfo
.supervisionUrls
= url
;
490 this.saveStationInfo();
491 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
495 public startHeartbeat(): void {
496 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
497 this.heartbeatSetInterval
= setInterval(() => {
498 this.ocppRequestService
499 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
502 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
506 }, this.getHeartbeatInterval());
508 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
509 this.getHeartbeatInterval(),
512 } else if (this.heartbeatSetInterval
) {
514 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
515 this.getHeartbeatInterval(),
520 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`,
525 public restartHeartbeat(): void {
527 this.stopHeartbeat();
529 this.startHeartbeat();
532 public restartWebSocketPing(): void {
533 // Stop WebSocket ping
534 this.stopWebSocketPing();
535 // Start WebSocket ping
536 this.startWebSocketPing();
539 public startMeterValues(connectorId
: number, interval
: number): void {
540 if (connectorId
=== 0) {
542 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
546 if (!this.getConnectorStatus(connectorId
)) {
548 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
553 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
555 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`,
559 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
560 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
563 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`,
568 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
569 // FIXME: Implement OCPP version agnostic helpers
570 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
573 this.getConnectorStatus(connectorId
)!.transactionId
!,
576 this.ocppRequestService
577 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
579 RequestCommand
.METER_VALUES
,
582 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
583 meterValue
: [meterValue
],
588 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
595 `${this.logPrefix()} Charging station ${
596 StandardParametersKey.MeterValueSampleInterval
597 } configuration set to ${interval}, not sending MeterValues`,
602 public stopMeterValues(connectorId
: number) {
603 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
604 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
608 public start(): void {
609 if (this.started
=== false) {
610 if (this.starting
=== false) {
611 this.starting
= true;
612 if (this.stationInfo
?.enableStatistics
=== true) {
613 this.performanceStatistics
?.start();
615 this.openWSConnection();
616 // Monitor charging station template file
617 this.templateFileWatcher
= watchJsonFile(
619 FileType
.ChargingStationTemplate
,
622 (event
, filename
): void => {
623 if (isNotEmptyString(filename
) && event
=== 'change') {
626 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
628 } file have changed, reload`,
630 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
633 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
)!);
635 this.stopAutomaticTransactionGenerator();
636 delete this.automaticTransactionGeneratorConfiguration
;
637 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
638 this.startAutomaticTransactionGenerator();
640 if (this.stationInfo
?.enableStatistics
=== true) {
641 this.performanceStatistics
?.restart();
643 this.performanceStatistics
?.stop();
645 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
648 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
656 this.emit(ChargingStationEvents
.started
);
657 this.starting
= false;
659 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
662 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
666 public async stop(reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
667 if (this.started
=== true) {
668 if (this.stopping
=== false) {
669 this.stopping
= true;
670 await this.stopMessageSequence(reason
, stopTransactions
);
671 this.closeWSConnection();
672 if (this.stationInfo
?.enableStatistics
=== true) {
673 this.performanceStatistics
?.stop();
675 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
676 this.templateFileWatcher
?.close();
677 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
678 delete this.bootNotificationResponse
;
679 this.started
= false;
680 this.saveConfiguration();
681 this.emit(ChargingStationEvents
.stopped
);
682 this.stopping
= false;
684 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
687 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
691 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
692 await this.stop(reason
);
693 await sleep(this.stationInfo
.resetTime
!);
698 public saveOcppConfiguration(): void {
699 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
700 this.saveConfiguration();
704 public bufferMessage(message
: string): void {
705 this.messageBuffer
.add(message
);
706 if (this.flushMessageBufferSetInterval
=== undefined) {
707 this.flushMessageBufferSetInterval
= setInterval(() => {
708 if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) {
709 this.flushMessageBuffer();
711 if (this.flushMessageBufferSetInterval
!== undefined && this.messageBuffer
.size
=== 0) {
712 clearInterval(this.flushMessageBufferSetInterval
);
713 delete this.flushMessageBufferSetInterval
;
715 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
);
719 public openWSConnection(
721 params
?: { closeOpened
?: boolean; terminateOpened
?: boolean },
724 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
725 ...this.stationInfo
?.wsOptions
,
728 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
729 if (!checkChargingStation(this, this.logPrefix())) {
733 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
734 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
736 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
738 if (params
?.closeOpened
) {
739 this.closeWSConnection();
741 if (params
?.terminateOpened
) {
742 this.terminateWSConnection();
745 if (this.isWebSocketConnectionOpened() === true) {
747 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`,
753 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
756 this.wsConnection
= new WebSocket(
757 this.wsConnectionUrl
,
758 `ocpp${this.stationInfo?.ocppVersion}`,
762 // Handle WebSocket message
763 this.wsConnection
.on(
765 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
767 // Handle WebSocket error
768 this.wsConnection
.on(
770 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
772 // Handle WebSocket close
773 this.wsConnection
.on(
775 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
777 // Handle WebSocket open
778 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
779 // Handle WebSocket ping
780 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
781 // Handle WebSocket pong
782 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
785 public closeWSConnection(): void {
786 if (this.isWebSocketConnectionOpened() === true) {
787 this.wsConnection
?.close();
788 this.wsConnection
= null;
792 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
793 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration
)) {
794 let automaticTransactionGeneratorConfiguration
:
795 | AutomaticTransactionGeneratorConfiguration
797 const stationTemplate
= this.getTemplateFromFile();
798 const stationConfiguration
= this.getConfigurationFromFile();
800 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
801 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
802 stationConfiguration
?.automaticTransactionGenerator
804 automaticTransactionGeneratorConfiguration
=
805 stationConfiguration
?.automaticTransactionGenerator
;
807 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
;
809 this.automaticTransactionGeneratorConfiguration
= {
810 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
811 ...automaticTransactionGeneratorConfiguration
,
814 return this.automaticTransactionGeneratorConfiguration
!;
817 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
818 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
821 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
822 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
823 if (isNotEmptyArray(connectorIds
)) {
824 for (const connectorId
of connectorIds
!) {
825 this.automaticTransactionGenerator
?.startConnector(connectorId
);
828 this.automaticTransactionGenerator
?.start();
830 this.saveAutomaticTransactionGeneratorConfiguration();
831 this.emit(ChargingStationEvents
.updated
);
834 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
835 if (isNotEmptyArray(connectorIds
)) {
836 for (const connectorId
of connectorIds
!) {
837 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
840 this.automaticTransactionGenerator
?.stop();
842 this.saveAutomaticTransactionGeneratorConfiguration();
843 this.emit(ChargingStationEvents
.updated
);
846 public async stopTransactionOnConnector(
848 reason
?: StopTransactionReason
,
849 ): Promise
<StopTransactionResponse
> {
850 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
852 this.stationInfo
?.beginEndMeterValues
=== true &&
853 this.stationInfo
?.ocppStrictCompliance
=== true &&
854 this.stationInfo
?.outOfOrderEndMeterValues
=== false
856 // FIXME: Implement OCPP version agnostic helpers
857 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
860 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
862 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
864 RequestCommand
.METER_VALUES
,
868 meterValue
: [transactionEndMeterValue
],
872 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
874 RequestCommand
.STOP_TRANSACTION
,
877 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
878 ...(isNullOrUndefined(reason
) && { reason
}),
883 public getReserveConnectorZeroSupported(): boolean {
884 return convertToBoolean(
885 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
,
889 public async addReservation(reservation
: Reservation
): Promise
<void> {
890 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
);
891 if (reservationFound
!== undefined) {
892 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
);
894 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
895 await OCPPServiceUtils
.sendAndSetConnectorStatus(
897 reservation
.connectorId
,
898 ConnectorStatusEnum
.Reserved
,
900 { send
: reservation
.connectorId
!== 0 },
904 public async removeReservation(
905 reservation
: Reservation
,
906 reason
: ReservationTerminationReason
,
908 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
910 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
911 case ReservationTerminationReason
.TRANSACTION_STARTED
:
912 delete connector
.reservation
;
914 case ReservationTerminationReason
.RESERVATION_CANCELED
:
915 case ReservationTerminationReason
.REPLACE_EXISTING
:
916 case ReservationTerminationReason
.EXPIRED
:
917 await OCPPServiceUtils
.sendAndSetConnectorStatus(
919 reservation
.connectorId
,
920 ConnectorStatusEnum
.Available
,
922 { send
: reservation
.connectorId
!== 0 },
924 delete connector
.reservation
;
927 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
928 throw new BaseError(`Unknown reservation termination reason '${reason}'`);
932 public getReservationBy(
933 filterKey
: ReservationKey
,
934 value
: number | string,
935 ): Reservation
| undefined {
937 for (const evseStatus
of this.evses
.values()) {
938 for (const connectorStatus
of evseStatus
.connectors
.values()) {
939 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
940 return connectorStatus
.reservation
;
945 for (const connectorStatus
of this.connectors
.values()) {
946 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
947 return connectorStatus
.reservation
;
953 public isConnectorReservable(
954 reservationId
: number,
956 connectorId
?: number,
958 const reservation
= this.getReservationBy('reservationId', reservationId
);
959 const reservationExists
= !isUndefined(reservation
) && !hasReservationExpired(reservation
!);
960 if (arguments.length
=== 1) {
961 return !reservationExists
;
962 } else if (arguments.length
> 1) {
963 const userReservation
= !isUndefined(idTag
)
964 ? this.getReservationBy('idTag', idTag
!)
966 const userReservationExists
=
967 !isUndefined(userReservation
) && !hasReservationExpired(userReservation
!);
968 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
969 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
971 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
977 private getNumberOfReservableConnectors(): number {
978 let numberOfReservableConnectors
= 0;
980 for (const evseStatus
of this.evses
.values()) {
981 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
);
984 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
);
986 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
989 private getNumberOfReservationsOnConnectorZero(): number {
991 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
992 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) ||
993 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
)
1000 private flushMessageBuffer(): void {
1001 if (this.messageBuffer
.size
> 0) {
1002 for (const message
of this.messageBuffer
.values()) {
1003 let beginId
: string | undefined;
1004 let commandName
: RequestCommand
| undefined;
1005 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1006 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1008 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1009 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1011 this.wsConnection
?.send(message
, (error
?: Error) => {
1012 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1013 if (isNullOrUndefined(error
)) {
1015 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
1017 )} payload sent: ${message}`,
1019 this.messageBuffer
.delete(message
);
1026 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1027 let template
: ChargingStationTemplate
| undefined;
1029 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1030 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1032 const measureId
= `${FileType.ChargingStationTemplate} read`;
1033 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1034 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1035 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1036 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1037 .update(JSON
.stringify(template
))
1039 this.sharedLRUCache
.setChargingStationTemplate(template
);
1040 this.templateFileHash
= template
.templateHash
;
1043 handleFileException(
1045 FileType
.ChargingStationTemplate
,
1046 error
as NodeJS
.ErrnoException
,
1053 private getStationInfoFromTemplate(): ChargingStationInfo
{
1054 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1055 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1056 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this);
1057 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
);
1058 if (stationTemplate
?.Connectors
) {
1059 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1061 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1062 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1063 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1064 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1065 createSerialNumber(stationTemplate
, stationInfo
);
1066 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
);
1067 if (isNotEmptyArray(stationTemplate
?.power
)) {
1068 stationTemplate
.power
= stationTemplate
.power
as number[];
1069 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1070 stationInfo
.maximumPower
=
1071 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1072 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1073 : stationTemplate
.power
[powerArrayRandomIndex
];
1075 stationTemplate
.power
= stationTemplate
?.power
as number;
1076 stationInfo
.maximumPower
=
1077 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1078 ? stationTemplate
.power
* 1000
1079 : stationTemplate
.power
;
1081 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1082 stationInfo
.firmwareVersionPattern
=
1083 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1085 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1086 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1089 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1091 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1094 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1101 stationTemplate
?.firmwareUpgrade
?? {},
1103 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1104 ? secondsToMilliseconds(stationTemplate
.resetTime
!)
1105 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1109 private getStationInfoFromFile(
1110 stationInfoPersistentConfiguration
= true,
1111 ): ChargingStationInfo
| undefined {
1112 let stationInfo
: ChargingStationInfo
| undefined;
1113 if (stationInfoPersistentConfiguration
=== true) {
1114 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1116 delete stationInfo
?.infoHash
;
1122 private getStationInfo(): ChargingStationInfo
{
1123 const defaultStationInfo
: Partial
<ChargingStationInfo
> = {
1124 enableStatistics
: false,
1125 remoteAuthorization
: true,
1126 currentOutType
: CurrentType
.AC
,
1127 mainVoltageMeterValues
: true,
1128 phaseLineToLineVoltageMeterValues
: false,
1129 customValueLimitationMeterValues
: true,
1130 ocppStrictCompliance
: true,
1131 outOfOrderEndMeterValues
: false,
1132 beginEndMeterValues
: false,
1133 meteringPerTransaction
: true,
1134 transactionDataMeterValues
: false,
1135 supervisionUrlOcppConfiguration
: false,
1136 supervisionUrlOcppKey
: VendorParametersKey
.ConnectionUrl
,
1137 ocppVersion
: OCPPVersion
.VERSION_16
,
1138 ocppPersistentConfiguration
: true,
1139 stationInfoPersistentConfiguration
: true,
1140 automaticTransactionGeneratorPersistentConfiguration
: true,
1141 autoReconnectMaxRetries
: -1,
1142 registrationMaxRetries
: -1,
1143 reconnectExponentialDelay
: false,
1144 stopTransactionsOnStopped
: true,
1146 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1147 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile(
1148 stationInfoFromTemplate
?.stationInfoPersistentConfiguration
,
1151 // 1. charging station info from template
1152 // 2. charging station info from configuration file
1153 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1154 return { ...defaultStationInfo
, ...stationInfoFromFile
! };
1156 stationInfoFromFile
&&
1157 propagateSerialNumber(
1158 this.getTemplateFromFile()!,
1159 stationInfoFromFile
,
1160 stationInfoFromTemplate
,
1162 return { ...defaultStationInfo
, ...stationInfoFromTemplate
};
1165 private saveStationInfo(): void {
1166 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1167 this.saveConfiguration();
1171 private handleUnsupportedVersion(version
: OCPPVersion
| undefined) {
1172 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1173 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1174 throw new BaseError(errorMsg
);
1177 private initialize(): void {
1178 const stationTemplate
= this.getTemplateFromFile()!;
1179 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1180 this.configurationFile
= join(
1181 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1182 `${getHashId(this.index, stationTemplate)}.json`,
1184 const stationConfiguration
= this.getConfigurationFromFile();
1186 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1187 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1188 (stationConfiguration
?.connectorsStatus
|| stationConfiguration
?.evsesStatus
)
1190 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
);
1192 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1194 this.stationInfo
= this.getStationInfo();
1196 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1197 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1198 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1200 const patternGroup
: number | undefined =
1201 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1202 this.stationInfo
.firmwareVersion
?.split('.').length
;
1203 const match
= this.stationInfo
1204 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1205 .slice(1, patternGroup
! + 1);
1206 const patchLevelIndex
= match
.length
- 1;
1207 match
[patchLevelIndex
] = (
1208 convertToInt(match
[patchLevelIndex
]) +
1209 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1211 this.stationInfo
.firmwareVersion
= match
?.join('.');
1213 this.saveStationInfo();
1214 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1215 if (this.stationInfo
?.enableStatistics
=== true) {
1216 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1217 this.stationInfo
.hashId
,
1218 this.stationInfo
.chargingStationId
!,
1219 this.configuredSupervisionUrl
,
1222 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1223 this.powerDivider
= this.getPowerDivider();
1224 // OCPP configuration
1225 this.ocppConfiguration
= this.getOcppConfiguration();
1226 this.initializeOcppConfiguration();
1227 this.initializeOcppServices();
1228 this.once(ChargingStationEvents
.accepted
, () => {
1229 this.startMessageSequence().catch((error
) => {
1230 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
);
1233 if (this.stationInfo
?.autoRegister
=== true) {
1234 this.bootNotificationResponse
= {
1235 currentTime
: new Date(),
1236 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1237 status: RegistrationStatusEnumType
.ACCEPTED
,
1242 private initializeOcppServices(): void {
1243 const ocppVersion
= this.stationInfo
?.ocppVersion
;
1244 switch (ocppVersion
) {
1245 case OCPPVersion
.VERSION_16
:
1246 this.ocppIncomingRequestService
=
1247 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1248 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1249 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1252 case OCPPVersion
.VERSION_20
:
1253 case OCPPVersion
.VERSION_201
:
1254 this.ocppIncomingRequestService
=
1255 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1256 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1257 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1261 this.handleUnsupportedVersion(ocppVersion
);
1266 private initializeOcppConfiguration(): void {
1267 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
))) {
1268 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0');
1270 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
))) {
1271 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false });
1274 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1275 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1276 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1278 addConfigurationKey(
1280 this.stationInfo
.supervisionUrlOcppKey
!,
1281 this.configuredSupervisionUrl
.href
,
1285 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1286 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1287 !isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1289 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!, { save
: false });
1292 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1293 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!))
1295 addConfigurationKey(
1297 this.stationInfo
.amperageLimitationOcppKey
!,
1299 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1304 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
))
1306 addConfigurationKey(
1308 StandardParametersKey
.SupportedFeatureProfiles
,
1309 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1312 addConfigurationKey(
1314 StandardParametersKey
.NumberOfConnectors
,
1315 this.getNumberOfConnectors().toString(),
1317 { overwrite
: true },
1320 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
))
1322 addConfigurationKey(
1324 StandardParametersKey
.MeterValuesSampledData
,
1325 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1329 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
))
1331 const connectorsPhaseRotation
: string[] = [];
1332 if (this.hasEvses
) {
1333 for (const evseStatus
of this.evses
.values()) {
1334 for (const connectorId
of evseStatus
.connectors
.keys()) {
1335 connectorsPhaseRotation
.push(
1336 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1341 for (const connectorId
of this.connectors
.keys()) {
1342 connectorsPhaseRotation
.push(
1343 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1347 addConfigurationKey(
1349 StandardParametersKey
.ConnectorPhaseRotation
,
1350 connectorsPhaseRotation
.toString(),
1354 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
))
1356 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
1359 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
)) &&
1360 getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)?.value
?.includes(
1361 SupportedFeatureProfiles
.LocalAuthListManagement
,
1364 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false');
1366 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
))) {
1367 addConfigurationKey(
1369 StandardParametersKey
.ConnectionTimeOut
,
1370 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1373 this.saveOcppConfiguration();
1376 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1377 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1378 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1379 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1381 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1382 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1383 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1384 delete evseStatus
.connectorsStatus
;
1385 this.evses
.set(evseId
, {
1386 ...(evseStatus
as EvseStatus
),
1387 connectors
: new Map
<number, ConnectorStatus
>(
1388 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1395 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1396 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1397 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1398 throw new BaseError(errorMsg
);
1400 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1401 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1402 throw new BaseError(errorMsg
);
1406 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1407 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1408 this.initializeConnectorsFromTemplate(stationTemplate
);
1409 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1410 this.initializeEvsesFromTemplate(stationTemplate
);
1411 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1412 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1413 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1414 throw new BaseError(errorMsg
);
1416 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1417 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1418 throw new BaseError(errorMsg
);
1422 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1423 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1424 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1425 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1426 throw new BaseError(errorMsg
);
1428 if (!stationTemplate
?.Connectors
?.[0]) {
1430 `${this.logPrefix()} Charging station information from template ${
1432 } with no connector id 0 configuration`,
1435 if (stationTemplate
?.Connectors
) {
1436 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1437 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1438 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1440 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1443 const connectorsConfigChanged
=
1444 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1445 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1446 connectorsConfigChanged
&& this.connectors
.clear();
1447 this.connectorsConfigurationHash
= connectorsConfigHash
;
1448 if (templateMaxConnectors
> 0) {
1449 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1451 connectorId
=== 0 &&
1452 (!stationTemplate
?.Connectors
?.[connectorId
] ||
1453 this.getUseConnectorId0(stationTemplate
) === false)
1457 const templateConnectorId
=
1458 connectorId
> 0 && stationTemplate
?.randomConnectors
1459 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1461 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1462 checkStationInfoConnectorStatus(
1463 templateConnectorId
,
1468 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1470 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1471 this.saveConnectorsStatus();
1474 `${this.logPrefix()} Charging station information from template ${
1476 } with no connectors configuration defined, cannot create connectors`,
1482 `${this.logPrefix()} Charging station information from template ${
1484 } with no connectors configuration defined, using already defined connectors`,
1489 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1490 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1491 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1492 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1493 throw new BaseError(errorMsg
);
1495 if (!stationTemplate
?.Evses
?.[0]) {
1497 `${this.logPrefix()} Charging station information from template ${
1499 } with no evse id 0 configuration`,
1502 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1504 `${this.logPrefix()} Charging station information from template ${
1506 } with evse id 0 with no connector id 0 configuration`,
1509 if (Object.keys(stationTemplate
?.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1511 `${this.logPrefix()} Charging station information from template ${
1513 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`,
1516 if (stationTemplate
?.Evses
) {
1517 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1518 .update(JSON
.stringify(stationTemplate
?.Evses
))
1520 const evsesConfigChanged
=
1521 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1522 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1523 evsesConfigChanged
&& this.evses
.clear();
1524 this.evsesConfigurationHash
= evsesConfigHash
;
1525 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1526 if (templateMaxEvses
> 0) {
1527 for (const evseKey
in stationTemplate
.Evses
) {
1528 const evseId
= convertToInt(evseKey
);
1529 this.evses
.set(evseId
, {
1530 connectors
: buildConnectorsMap(
1531 stationTemplate
?.Evses
[evseKey
]?.Connectors
,
1535 availability
: AvailabilityType
.Operative
,
1537 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1539 this.saveEvsesStatus();
1542 `${this.logPrefix()} Charging station information from template ${
1544 } with no evses configuration defined, cannot create evses`,
1550 `${this.logPrefix()} Charging station information from template ${
1552 } with no evses configuration defined, using already defined evses`,
1557 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1558 let configuration
: ChargingStationConfiguration
| undefined;
1559 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1561 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1562 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1563 this.configurationFileHash
,
1566 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1567 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1568 configuration
= JSON
.parse(
1569 readFileSync(this.configurationFile
, 'utf8'),
1570 ) as ChargingStationConfiguration
;
1571 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1572 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1573 this.configurationFileHash
= configuration
.configurationHash
!;
1576 handleFileException(
1577 this.configurationFile
,
1578 FileType
.ChargingStationConfiguration
,
1579 error
as NodeJS
.ErrnoException
,
1584 return configuration
;
1587 private saveAutomaticTransactionGeneratorConfiguration(): void {
1588 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1589 this.saveConfiguration();
1593 private saveConnectorsStatus() {
1594 this.saveConfiguration();
1597 private saveEvsesStatus() {
1598 this.saveConfiguration();
1601 private saveConfiguration(): void {
1602 if (isNotEmptyString(this.configurationFile
)) {
1604 if (!existsSync(dirname(this.configurationFile
))) {
1605 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1607 let configurationData
: ChargingStationConfiguration
= this.getConfigurationFromFile()
1608 ? cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!)
1610 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true && this.stationInfo
) {
1611 configurationData
.stationInfo
= this.stationInfo
;
1613 delete configurationData
.stationInfo
;
1616 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1617 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1619 configurationData
.configurationKey
= this.ocppConfiguration
?.configurationKey
;
1621 delete configurationData
.configurationKey
;
1623 configurationData
= merge
<ChargingStationConfiguration
>(
1625 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1628 !this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
||
1629 !this.getAutomaticTransactionGeneratorConfiguration()
1631 delete configurationData
.automaticTransactionGenerator
;
1633 if (this.connectors
.size
> 0) {
1634 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1636 delete configurationData
.connectorsStatus
;
1638 if (this.evses
.size
> 0) {
1639 configurationData
.evsesStatus
= buildEvsesStatus(this);
1641 delete configurationData
.evsesStatus
;
1643 delete configurationData
.configurationHash
;
1644 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1647 stationInfo
: configurationData
.stationInfo
,
1648 configurationKey
: configurationData
.configurationKey
,
1649 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1650 ...(this.connectors
.size
> 0 && {
1651 connectorsStatus
: configurationData
.connectorsStatus
,
1653 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
}),
1654 } as ChargingStationConfiguration
),
1657 if (this.configurationFileHash
!== configurationHash
) {
1658 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1659 configurationData
.configurationHash
= configurationHash
;
1660 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1661 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1663 this.configurationFile
,
1664 JSON
.stringify(configurationData
, undefined, 2),
1667 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1668 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1669 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1670 this.configurationFileHash
= configurationHash
;
1671 }).catch((error
) => {
1672 handleFileException(
1673 this.configurationFile
,
1674 FileType
.ChargingStationConfiguration
,
1675 error
as NodeJS
.ErrnoException
,
1681 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1682 this.configurationFile
1687 handleFileException(
1688 this.configurationFile
,
1689 FileType
.ChargingStationConfiguration
,
1690 error
as NodeJS
.ErrnoException
,
1696 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1701 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1702 return this.getTemplateFromFile()?.Configuration
;
1705 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1706 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1707 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1708 return { configurationKey
};
1713 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1714 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1715 this.getOcppConfigurationFromFile();
1716 if (!ocppConfiguration
) {
1717 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1719 return ocppConfiguration
;
1722 private async onOpen(): Promise
<void> {
1723 if (this.isWebSocketConnectionOpened() === true) {
1725 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1727 let registrationRetryCount
= 0;
1728 if (this.isRegistered() === false) {
1729 // Send BootNotification
1731 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1732 BootNotificationRequest
,
1733 BootNotificationResponse
1734 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1735 skipBufferingOnError
: true,
1737 if (this.isRegistered() === false) {
1738 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
;
1740 this?.bootNotificationResponse
?.interval
1741 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1742 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1746 this.isRegistered() === false &&
1747 (registrationRetryCount
<= this.stationInfo
.registrationMaxRetries
! ||
1748 this.stationInfo
?.registrationMaxRetries
=== -1)
1751 if (this.isRegistered() === true) {
1752 this.emit(ChargingStationEvents
.registered
);
1753 if (this.inAcceptedState() === true) {
1754 this.emit(ChargingStationEvents
.accepted
);
1758 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this
1759 .stationInfo?.registrationMaxRetries})`,
1762 this.autoReconnectRetryCount
= 0;
1763 this.emit(ChargingStationEvents
.updated
);
1766 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1771 private async onClose(code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): Promise
<void> {
1774 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1775 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1777 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1779 )}' and reason '${reason.toString()}'`,
1781 this.autoReconnectRetryCount
= 0;
1786 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1788 )}' and reason '${reason.toString()}'`,
1790 this.started
=== true && (await this.reconnect());
1793 this.emit(ChargingStationEvents
.updated
);
1796 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1797 const cachedRequest
= this.requests
.get(messageId
);
1798 if (Array.isArray(cachedRequest
) === true) {
1799 return cachedRequest
;
1801 throw new OCPPError(
1802 ErrorType
.PROTOCOL_ERROR
,
1803 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1805 )} is not an array`,
1807 cachedRequest
as JsonType
,
1811 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1812 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1813 if (this.stationInfo
?.enableStatistics
=== true) {
1814 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1817 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1821 // Process the message
1822 await this.ocppIncomingRequestService
.incomingRequestHandler(
1828 this.emit(ChargingStationEvents
.updated
);
1831 private handleResponseMessage(response
: Response
): void {
1832 const [messageType
, messageId
, commandPayload
] = response
;
1833 if (this.requests
.has(messageId
) === false) {
1835 throw new OCPPError(
1836 ErrorType
.INTERNAL_ERROR
,
1837 `Response for unknown message id ${messageId}`,
1843 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1848 `${this.logPrefix()} << Command '${
1849 requestCommandName ?? Constants.UNKNOWN_COMMAND
1850 }' received response payload: ${JSON.stringify(response)}`,
1852 responseCallback(commandPayload
, requestPayload
);
1855 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1856 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1857 if (this.requests
.has(messageId
) === false) {
1859 throw new OCPPError(
1860 ErrorType
.INTERNAL_ERROR
,
1861 `Error response for unknown message id ${messageId}`,
1863 { errorType
, errorMessage
, errorDetails
},
1866 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1868 `${this.logPrefix()} << Command '${
1869 requestCommandName ?? Constants.UNKNOWN_COMMAND
1870 }' received error response payload: ${JSON.stringify(errorResponse)}`,
1872 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1875 private async onMessage(data
: RawData
): Promise
<void> {
1876 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
1877 let messageType
: MessageType
| undefined;
1878 let errorMsg
: string;
1880 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1881 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1882 if (Array.isArray(request
) === true) {
1883 [messageType
] = request
;
1884 // Check the type of message
1885 switch (messageType
) {
1887 case MessageType
.CALL_MESSAGE
:
1888 await this.handleIncomingMessage(request
as IncomingRequest
);
1891 case MessageType
.CALL_RESULT_MESSAGE
:
1892 this.handleResponseMessage(request
as Response
);
1895 case MessageType
.CALL_ERROR_MESSAGE
:
1896 this.handleErrorMessage(request
as ErrorResponse
);
1900 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1901 errorMsg
= `Wrong message type ${messageType}`;
1902 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1903 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1906 throw new OCPPError(
1907 ErrorType
.PROTOCOL_ERROR
,
1908 'Incoming message is not an array',
1916 let commandName
: IncomingRequestCommand
| undefined;
1917 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
1918 let errorCallback
: ErrorCallback
;
1919 const [, messageId
] = request
!;
1920 switch (messageType
) {
1921 case MessageType
.CALL_MESSAGE
:
1922 [, , commandName
] = request
as IncomingRequest
;
1924 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1926 case MessageType
.CALL_RESULT_MESSAGE
:
1927 case MessageType
.CALL_ERROR_MESSAGE
:
1928 if (this.requests
.has(messageId
) === true) {
1929 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1930 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1931 errorCallback(error
as OCPPError
, false);
1933 // Remove the request from the cache in case of error at response handling
1934 this.requests
.delete(messageId
);
1938 if (error
instanceof OCPPError
=== false) {
1940 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1941 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1942 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1943 }' message '${data.toString()}' handling is not an OCPPError:`,
1948 `${this.logPrefix()} Incoming OCPP command '${
1949 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1950 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1951 }' message '${data.toString()}'${
1952 messageType !== MessageType.CALL_MESSAGE
1953 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1955 } processing error:`,
1961 private onPing(): void {
1962 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1965 private onPong(): void {
1966 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1969 private onError(error
: WSError
): void {
1970 this.closeWSConnection();
1971 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1974 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1975 if (this.stationInfo
?.meteringPerTransaction
=== true) {
1978 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
1979 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1984 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
1985 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1989 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
1990 return stationTemplate
?.useConnectorId0
?? true;
1993 private async stopRunningTransactions(reason
?: StopTransactionReason
): Promise
<void> {
1994 if (this.hasEvses
) {
1995 for (const [evseId
, evseStatus
] of this.evses
) {
1999 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2000 if (connectorStatus
.transactionStarted
=== true) {
2001 await this.stopTransactionOnConnector(connectorId
, reason
);
2006 for (const connectorId
of this.connectors
.keys()) {
2007 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2008 await this.stopTransactionOnConnector(connectorId
, reason
);
2015 private getConnectionTimeout(): number {
2016 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) !== undefined) {
2017 return convertToInt(
2018 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)!.value
! ??
2019 Constants
.DEFAULT_CONNECTION_TIMEOUT
,
2022 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2025 private getPowerDivider(): number {
2026 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2027 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2028 powerDivider
= this.getNumberOfRunningTransactions();
2030 return powerDivider
;
2033 private getMaximumAmperage(stationInfo
?: ChargingStationInfo
): number | undefined {
2034 const maximumPower
= (stationInfo
?? this.stationInfo
).maximumPower
!;
2035 switch (this.getCurrentOutType(stationInfo
)) {
2036 case CurrentType
.AC
:
2037 return ACElectricUtils
.amperagePerPhaseFromPower(
2038 this.getNumberOfPhases(stationInfo
),
2039 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2040 this.getVoltageOut(stationInfo
),
2042 case CurrentType
.DC
:
2043 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2047 private getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
2048 return (stationInfo
?? this.stationInfo
).currentOutType
?? CurrentType
.AC
;
2051 private getVoltageOut(stationInfo
?: ChargingStationInfo
): Voltage
{
2053 (stationInfo
?? this.stationInfo
).voltageOut
??
2054 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2058 private getAmperageLimitation(): number | undefined {
2060 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2061 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!) !== undefined
2065 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)?.value
,
2066 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2071 private async startMessageSequence(): Promise
<void> {
2072 if (this.stationInfo
?.autoRegister
=== true) {
2073 await this.ocppRequestService
.requestHandler
<
2074 BootNotificationRequest
,
2075 BootNotificationResponse
2076 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2077 skipBufferingOnError
: true,
2080 // Start WebSocket ping
2081 this.startWebSocketPing();
2083 this.startHeartbeat();
2084 // Initialize connectors status
2085 if (this.hasEvses
) {
2086 for (const [evseId
, evseStatus
] of this.evses
) {
2088 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2089 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2090 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2093 connectorBootStatus
,
2100 for (const connectorId
of this.connectors
.keys()) {
2101 if (connectorId
> 0) {
2102 const connectorBootStatus
= getBootConnectorStatus(
2105 this.getConnectorStatus(connectorId
)!,
2107 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2111 if (this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
) {
2112 await this.ocppRequestService
.requestHandler
<
2113 FirmwareStatusNotificationRequest
,
2114 FirmwareStatusNotificationResponse
2115 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2116 status: FirmwareStatus
.Installed
,
2118 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2122 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
2123 this.startAutomaticTransactionGenerator();
2125 this.flushMessageBuffer();
2128 private async stopMessageSequence(
2129 reason
?: StopTransactionReason
,
2130 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
,
2132 // Stop WebSocket ping
2133 this.stopWebSocketPing();
2135 this.stopHeartbeat();
2137 if (this.automaticTransactionGenerator
?.started
=== true) {
2138 this.stopAutomaticTransactionGenerator();
2140 // Stop ongoing transactions
2141 stopTransactions
&& (await this.stopRunningTransactions(reason
));
2142 if (this.hasEvses
) {
2143 for (const [evseId
, evseStatus
] of this.evses
) {
2145 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2146 await this.ocppRequestService
.requestHandler
<
2147 StatusNotificationRequest
,
2148 StatusNotificationResponse
2151 RequestCommand
.STATUS_NOTIFICATION
,
2152 OCPPServiceUtils
.buildStatusNotificationRequest(
2155 ConnectorStatusEnum
.Unavailable
,
2159 delete connectorStatus
?.status;
2164 for (const connectorId
of this.connectors
.keys()) {
2165 if (connectorId
> 0) {
2166 await this.ocppRequestService
.requestHandler
<
2167 StatusNotificationRequest
,
2168 StatusNotificationResponse
2171 RequestCommand
.STATUS_NOTIFICATION
,
2172 OCPPServiceUtils
.buildStatusNotificationRequest(
2175 ConnectorStatusEnum
.Unavailable
,
2178 delete this.getConnectorStatus(connectorId
)?.status;
2184 private startWebSocketPing(): void {
2185 const webSocketPingInterval
: number =
2186 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) !== undefined
2188 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
,
2191 if (webSocketPingInterval
> 0 && this.webSocketPingSetInterval
=== undefined) {
2192 this.webSocketPingSetInterval
= setInterval(() => {
2193 if (this.isWebSocketConnectionOpened() === true) {
2194 this.wsConnection
?.ping();
2196 }, secondsToMilliseconds(webSocketPingInterval
));
2198 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2199 webSocketPingInterval,
2202 } else if (this.webSocketPingSetInterval
!== undefined) {
2204 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2205 webSocketPingInterval,
2210 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2215 private stopWebSocketPing(): void {
2216 if (this.webSocketPingSetInterval
!== undefined) {
2217 clearInterval(this.webSocketPingSetInterval
);
2218 delete this.webSocketPingSetInterval
;
2222 private getConfiguredSupervisionUrl(): URL
{
2223 let configuredSupervisionUrl
: string;
2224 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2225 if (isNotEmptyArray(supervisionUrls
)) {
2226 let configuredSupervisionUrlIndex
: number;
2227 switch (Configuration
.getSupervisionUrlDistribution()) {
2228 case SupervisionUrlDistribution
.RANDOM
:
2229 configuredSupervisionUrlIndex
= Math.floor(
2230 secureRandom() * (supervisionUrls
as string[]).length
,
2233 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2234 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2236 Object.values(SupervisionUrlDistribution
).includes(
2237 Configuration
.getSupervisionUrlDistribution()!,
2240 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2241 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2242 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2245 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2248 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2250 configuredSupervisionUrl
= supervisionUrls
as string;
2252 if (isNotEmptyString(configuredSupervisionUrl
)) {
2253 return new URL(configuredSupervisionUrl
);
2255 const errorMsg
= 'No supervision url(s) configured';
2256 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2257 throw new BaseError(`${errorMsg}`);
2260 private stopHeartbeat(): void {
2261 if (this.heartbeatSetInterval
) {
2262 clearInterval(this.heartbeatSetInterval
);
2263 delete this.heartbeatSetInterval
;
2267 private terminateWSConnection(): void {
2268 if (this.isWebSocketConnectionOpened() === true) {
2269 this.wsConnection
?.terminate();
2270 this.wsConnection
= null;
2274 private async reconnect(): Promise
<void> {
2275 // Stop WebSocket ping
2276 this.stopWebSocketPing();
2278 this.stopHeartbeat();
2279 // Stop the ATG if needed
2280 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2281 this.stopAutomaticTransactionGenerator();
2284 this.autoReconnectRetryCount
< this.stationInfo
.autoReconnectMaxRetries
! ||
2285 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2287 ++this.autoReconnectRetryCount
;
2288 const reconnectDelay
=
2289 this.stationInfo
?.reconnectExponentialDelay
=== true
2290 ? exponentialDelay(this.autoReconnectRetryCount
)
2291 : secondsToMilliseconds(this.getConnectionTimeout());
2292 const reconnectDelayWithdraw
= 1000;
2293 const reconnectTimeout
=
2294 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2295 ? reconnectDelay
- reconnectDelayWithdraw
2298 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2301 )}ms, timeout ${reconnectTimeout}ms`,
2303 await sleep(reconnectDelay
);
2305 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2307 this.openWSConnection(
2309 handshakeTimeout
: reconnectTimeout
,
2311 { closeOpened
: true },
2313 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2315 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2316 this.autoReconnectRetryCount
2317 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`,