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 => {
239 if (isNotEmptyString(this?.stationInfo
?.chargingStationId
)) {
240 return logPrefix(` ${this?.stationInfo?.chargingStationId} |`);
242 let stationTemplate
: ChargingStationTemplate
| undefined;
244 stationTemplate
= JSON
.parse(
245 readFileSync(this.templateFile
, 'utf8'),
246 ) as ChargingStationTemplate
;
248 stationTemplate
= undefined;
250 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`);
253 public hasIdTags(): boolean {
254 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
257 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
258 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
259 switch (this.getCurrentOutType(stationInfo
)) {
261 return localStationInfo
.numberOfPhases
?? 3;
267 public isWebSocketConnectionOpened(): boolean {
268 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
271 public inUnknownState(): boolean {
272 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
275 public inPendingState(): boolean {
276 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
279 public inAcceptedState(): boolean {
280 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
283 public inRejectedState(): boolean {
284 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
287 public isRegistered(): boolean {
289 this.inUnknownState() === false &&
290 (this.inAcceptedState() === true || this.inPendingState() === true)
294 public isChargingStationAvailable(): boolean {
295 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
298 public hasConnector(connectorId
: number): boolean {
300 for (const evseStatus
of this.evses
.values()) {
301 if (evseStatus
.connectors
.has(connectorId
)) {
307 return this.connectors
.has(connectorId
);
310 public isConnectorAvailable(connectorId
: number): boolean {
313 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
317 public getNumberOfConnectors(): number {
319 let numberOfConnectors
= 0;
320 for (const [evseId
, evseStatus
] of this.evses
) {
322 numberOfConnectors
+= evseStatus
.connectors
.size
;
325 return numberOfConnectors
;
327 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
330 public getNumberOfEvses(): number {
331 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
334 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
336 for (const evseStatus
of this.evses
.values()) {
337 if (evseStatus
.connectors
.has(connectorId
)) {
338 return evseStatus
.connectors
.get(connectorId
);
343 return this.connectors
.get(connectorId
);
346 public getConnectorMaximumAvailablePower(connectorId
: number): number {
347 let connectorAmperageLimitationPowerLimit
: number | undefined;
349 !isNullOrUndefined(this.getAmperageLimitation()) &&
350 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
352 connectorAmperageLimitationPowerLimit
=
353 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
354 ? ACElectricUtils
.powerTotal(
355 this.getNumberOfPhases(),
356 this.stationInfo
.voltageOut
!,
357 this.getAmperageLimitation()! *
358 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
360 : DCElectricUtils
.power(this.stationInfo
.voltageOut
!, this.getAmperageLimitation()!)) /
363 const connectorMaximumPower
= this.stationInfo
.maximumPower
! / this.powerDivider
;
364 const connectorChargingProfilesPowerLimit
=
365 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
367 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
368 isNaN(connectorAmperageLimitationPowerLimit
!)
370 : connectorAmperageLimitationPowerLimit
!,
371 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
375 public getTransactionIdTag(transactionId
: number): string | undefined {
377 for (const evseStatus
of this.evses
.values()) {
378 for (const connectorStatus
of evseStatus
.connectors
.values()) {
379 if (connectorStatus
.transactionId
=== transactionId
) {
380 return connectorStatus
.transactionIdTag
;
385 for (const connectorId
of this.connectors
.keys()) {
386 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
387 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
393 public getNumberOfRunningTransactions(): number {
394 let numberOfRunningTransactions
= 0;
396 for (const [evseId
, evseStatus
] of this.evses
) {
400 for (const connectorStatus
of evseStatus
.connectors
.values()) {
401 if (connectorStatus
.transactionStarted
=== true) {
402 ++numberOfRunningTransactions
;
407 for (const connectorId
of this.connectors
.keys()) {
408 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
409 ++numberOfRunningTransactions
;
413 return numberOfRunningTransactions
;
416 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
418 for (const evseStatus
of this.evses
.values()) {
419 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
420 if (connectorStatus
.transactionId
=== transactionId
) {
426 for (const connectorId
of this.connectors
.keys()) {
427 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
434 public getEnergyActiveImportRegisterByTransactionId(
435 transactionId
: number,
438 return this.getEnergyActiveImportRegister(
439 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
444 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
445 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
448 public getAuthorizeRemoteTxRequests(): boolean {
449 const authorizeRemoteTxRequests
= getConfigurationKey(
451 StandardParametersKey
.AuthorizeRemoteTxRequests
,
453 return authorizeRemoteTxRequests
!== undefined
454 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
458 public getLocalAuthListEnabled(): boolean {
459 const localAuthListEnabled
= getConfigurationKey(
461 StandardParametersKey
.LocalAuthListEnabled
,
463 return localAuthListEnabled
!== undefined
464 ? convertToBoolean(localAuthListEnabled
.value
)
468 public getHeartbeatInterval(): number {
469 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
);
470 if (HeartbeatInterval
!== undefined) {
471 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
));
473 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
);
474 if (HeartBeatInterval
!== undefined) {
475 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
));
477 this.stationInfo
?.autoRegister
=== false &&
479 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
480 Constants.DEFAULT_HEARTBEAT_INTERVAL
483 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
486 public setSupervisionUrl(url
: string): void {
488 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
489 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
)
491 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
!, url
);
493 this.stationInfo
.supervisionUrls
= url
;
494 this.saveStationInfo();
495 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
499 public startHeartbeat(): void {
500 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
501 this.heartbeatSetInterval
= setInterval(() => {
502 this.ocppRequestService
503 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
506 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
510 }, this.getHeartbeatInterval());
512 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
513 this.getHeartbeatInterval(),
516 } else if (this.heartbeatSetInterval
) {
518 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
519 this.getHeartbeatInterval(),
524 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`,
529 public restartHeartbeat(): void {
531 this.stopHeartbeat();
533 this.startHeartbeat();
536 public restartWebSocketPing(): void {
537 // Stop WebSocket ping
538 this.stopWebSocketPing();
539 // Start WebSocket ping
540 this.startWebSocketPing();
543 public startMeterValues(connectorId
: number, interval
: number): void {
544 if (connectorId
=== 0) {
546 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
550 if (!this.getConnectorStatus(connectorId
)) {
552 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
557 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
559 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`,
563 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
564 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
567 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`,
572 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
573 // FIXME: Implement OCPP version agnostic helpers
574 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
577 this.getConnectorStatus(connectorId
)!.transactionId
!,
580 this.ocppRequestService
581 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
583 RequestCommand
.METER_VALUES
,
586 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
587 meterValue
: [meterValue
],
592 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
599 `${this.logPrefix()} Charging station ${
600 StandardParametersKey.MeterValueSampleInterval
601 } configuration set to ${interval}, not sending MeterValues`,
606 public stopMeterValues(connectorId
: number) {
607 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
608 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
612 public start(): void {
613 if (this.started
=== false) {
614 if (this.starting
=== false) {
615 this.starting
= true;
616 if (this.stationInfo
?.enableStatistics
=== true) {
617 this.performanceStatistics
?.start();
619 this.openWSConnection();
620 // Monitor charging station template file
621 this.templateFileWatcher
= watchJsonFile(
623 FileType
.ChargingStationTemplate
,
626 (event
, filename
): void => {
627 if (isNotEmptyString(filename
) && event
=== 'change') {
630 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
632 } file have changed, reload`,
634 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
637 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
)!);
639 this.stopAutomaticTransactionGenerator();
640 delete this.automaticTransactionGeneratorConfiguration
;
641 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
642 this.startAutomaticTransactionGenerator();
644 if (this.stationInfo
?.enableStatistics
=== true) {
645 this.performanceStatistics
?.restart();
647 this.performanceStatistics
?.stop();
649 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
652 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
660 this.emit(ChargingStationEvents
.started
);
661 this.starting
= false;
663 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
666 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
670 public async stop(reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
671 if (this.started
=== true) {
672 if (this.stopping
=== false) {
673 this.stopping
= true;
674 await this.stopMessageSequence(reason
, stopTransactions
);
675 this.closeWSConnection();
676 if (this.stationInfo
?.enableStatistics
=== true) {
677 this.performanceStatistics
?.stop();
679 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
680 this.templateFileWatcher
?.close();
681 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
682 delete this.bootNotificationResponse
;
683 this.started
= false;
684 this.saveConfiguration();
685 this.emit(ChargingStationEvents
.stopped
);
686 this.stopping
= false;
688 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
691 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
695 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
696 await this.stop(reason
);
697 await sleep(this.stationInfo
.resetTime
!);
702 public saveOcppConfiguration(): void {
703 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
704 this.saveConfiguration();
708 public bufferMessage(message
: string): void {
709 this.messageBuffer
.add(message
);
710 if (this.flushMessageBufferSetInterval
=== undefined) {
711 this.flushMessageBufferSetInterval
= setInterval(() => {
712 if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) {
713 this.flushMessageBuffer();
715 if (this.flushMessageBufferSetInterval
!== undefined && this.messageBuffer
.size
=== 0) {
716 clearInterval(this.flushMessageBufferSetInterval
);
717 delete this.flushMessageBufferSetInterval
;
719 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
);
723 public openWSConnection(
725 params
?: { closeOpened
?: boolean; terminateOpened
?: boolean },
728 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
729 ...this.stationInfo
?.wsOptions
,
732 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
733 if (!checkChargingStation(this, this.logPrefix())) {
737 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
738 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
740 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
742 if (params
?.closeOpened
) {
743 this.closeWSConnection();
745 if (params
?.terminateOpened
) {
746 this.terminateWSConnection();
749 if (this.isWebSocketConnectionOpened() === true) {
751 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`,
757 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
760 this.wsConnection
= new WebSocket(
761 this.wsConnectionUrl
,
762 `ocpp${this.stationInfo?.ocppVersion}`,
766 // Handle WebSocket message
767 this.wsConnection
.on(
769 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
771 // Handle WebSocket error
772 this.wsConnection
.on(
774 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
776 // Handle WebSocket close
777 this.wsConnection
.on(
779 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
781 // Handle WebSocket open
782 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
783 // Handle WebSocket ping
784 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
785 // Handle WebSocket pong
786 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
789 public closeWSConnection(): void {
790 if (this.isWebSocketConnectionOpened() === true) {
791 this.wsConnection
?.close();
792 this.wsConnection
= null;
796 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
797 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration
)) {
798 let automaticTransactionGeneratorConfiguration
:
799 | AutomaticTransactionGeneratorConfiguration
801 const stationTemplate
= this.getTemplateFromFile();
802 const stationConfiguration
= this.getConfigurationFromFile();
804 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
805 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
806 stationConfiguration
?.automaticTransactionGenerator
808 automaticTransactionGeneratorConfiguration
=
809 stationConfiguration
?.automaticTransactionGenerator
;
811 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
;
813 this.automaticTransactionGeneratorConfiguration
= {
814 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
815 ...automaticTransactionGeneratorConfiguration
,
818 return this.automaticTransactionGeneratorConfiguration
!;
821 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
822 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
825 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
826 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
827 if (isNotEmptyArray(connectorIds
)) {
828 for (const connectorId
of connectorIds
!) {
829 this.automaticTransactionGenerator
?.startConnector(connectorId
);
832 this.automaticTransactionGenerator
?.start();
834 this.saveAutomaticTransactionGeneratorConfiguration();
835 this.emit(ChargingStationEvents
.updated
);
838 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
839 if (isNotEmptyArray(connectorIds
)) {
840 for (const connectorId
of connectorIds
!) {
841 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
844 this.automaticTransactionGenerator
?.stop();
846 this.saveAutomaticTransactionGeneratorConfiguration();
847 this.emit(ChargingStationEvents
.updated
);
850 public async stopTransactionOnConnector(
852 reason
?: StopTransactionReason
,
853 ): Promise
<StopTransactionResponse
> {
854 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
856 this.stationInfo
?.beginEndMeterValues
=== true &&
857 this.stationInfo
?.ocppStrictCompliance
=== true &&
858 this.stationInfo
?.outOfOrderEndMeterValues
=== false
860 // FIXME: Implement OCPP version agnostic helpers
861 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
864 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
866 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
868 RequestCommand
.METER_VALUES
,
872 meterValue
: [transactionEndMeterValue
],
876 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
878 RequestCommand
.STOP_TRANSACTION
,
881 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
882 ...(isNullOrUndefined(reason
) && { reason
}),
887 public getReserveConnectorZeroSupported(): boolean {
888 return convertToBoolean(
889 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
,
893 public async addReservation(reservation
: Reservation
): Promise
<void> {
894 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
);
895 if (reservationFound
!== undefined) {
896 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
);
898 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
899 await OCPPServiceUtils
.sendAndSetConnectorStatus(
901 reservation
.connectorId
,
902 ConnectorStatusEnum
.Reserved
,
904 { send
: reservation
.connectorId
!== 0 },
908 public async removeReservation(
909 reservation
: Reservation
,
910 reason
: ReservationTerminationReason
,
912 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
914 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
915 case ReservationTerminationReason
.TRANSACTION_STARTED
:
916 delete connector
.reservation
;
918 case ReservationTerminationReason
.RESERVATION_CANCELED
:
919 case ReservationTerminationReason
.REPLACE_EXISTING
:
920 case ReservationTerminationReason
.EXPIRED
:
921 await OCPPServiceUtils
.sendAndSetConnectorStatus(
923 reservation
.connectorId
,
924 ConnectorStatusEnum
.Available
,
926 { send
: reservation
.connectorId
!== 0 },
928 delete connector
.reservation
;
931 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
932 throw new BaseError(`Unknown reservation termination reason '${reason}'`);
936 public getReservationBy(
937 filterKey
: ReservationKey
,
938 value
: number | string,
939 ): Reservation
| undefined {
941 for (const evseStatus
of this.evses
.values()) {
942 for (const connectorStatus
of evseStatus
.connectors
.values()) {
943 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
944 return connectorStatus
.reservation
;
949 for (const connectorStatus
of this.connectors
.values()) {
950 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
951 return connectorStatus
.reservation
;
957 public isConnectorReservable(
958 reservationId
: number,
960 connectorId
?: number,
962 const reservation
= this.getReservationBy('reservationId', reservationId
);
963 const reservationExists
= !isUndefined(reservation
) && !hasReservationExpired(reservation
!);
964 if (arguments.length
=== 1) {
965 return !reservationExists
;
966 } else if (arguments.length
> 1) {
967 const userReservation
= !isUndefined(idTag
)
968 ? this.getReservationBy('idTag', idTag
!)
970 const userReservationExists
=
971 !isUndefined(userReservation
) && !hasReservationExpired(userReservation
!);
972 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
973 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
975 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
981 private getNumberOfReservableConnectors(): number {
982 let numberOfReservableConnectors
= 0;
984 for (const evseStatus
of this.evses
.values()) {
985 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
);
988 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
);
990 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
993 private getNumberOfReservationsOnConnectorZero(): number {
995 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
996 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) ||
997 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
)
1004 private flushMessageBuffer(): void {
1005 if (this.messageBuffer
.size
> 0) {
1006 for (const message
of this.messageBuffer
.values()) {
1007 let beginId
: string | undefined;
1008 let commandName
: RequestCommand
| undefined;
1009 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1010 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1012 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1013 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1015 this.wsConnection
?.send(message
, (error
?: Error) => {
1016 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1017 if (isNullOrUndefined(error
)) {
1019 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
1021 )} payload sent: ${message}`,
1023 this.messageBuffer
.delete(message
);
1030 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1031 let template
: ChargingStationTemplate
| undefined;
1033 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1034 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1036 const measureId
= `${FileType.ChargingStationTemplate} read`;
1037 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1038 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1039 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1040 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1041 .update(JSON
.stringify(template
))
1043 this.sharedLRUCache
.setChargingStationTemplate(template
);
1044 this.templateFileHash
= template
.templateHash
;
1047 handleFileException(
1049 FileType
.ChargingStationTemplate
,
1050 error
as NodeJS
.ErrnoException
,
1057 private getStationInfoFromTemplate(): ChargingStationInfo
{
1058 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1059 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1060 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this);
1061 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
);
1062 if (stationTemplate
?.Connectors
) {
1063 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1065 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1066 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1067 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1068 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1069 createSerialNumber(stationTemplate
, stationInfo
);
1070 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
);
1071 if (isNotEmptyArray(stationTemplate
?.power
)) {
1072 stationTemplate
.power
= stationTemplate
.power
as number[];
1073 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1074 stationInfo
.maximumPower
=
1075 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1076 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1077 : stationTemplate
.power
[powerArrayRandomIndex
];
1079 stationTemplate
.power
= stationTemplate
?.power
as number;
1080 stationInfo
.maximumPower
=
1081 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1082 ? stationTemplate
.power
* 1000
1083 : stationTemplate
.power
;
1085 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1086 stationInfo
.firmwareVersionPattern
=
1087 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1089 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1090 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1093 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1095 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1098 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1105 stationTemplate
?.firmwareUpgrade
?? {},
1107 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1108 ? secondsToMilliseconds(stationTemplate
.resetTime
!)
1109 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1113 private getStationInfoFromFile(
1114 stationInfoPersistentConfiguration
= true,
1115 ): ChargingStationInfo
| undefined {
1116 let stationInfo
: ChargingStationInfo
| undefined;
1117 if (stationInfoPersistentConfiguration
=== true) {
1118 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1120 delete stationInfo
?.infoHash
;
1126 private getStationInfo(): ChargingStationInfo
{
1127 const defaultStationInfo
: Partial
<ChargingStationInfo
> = {
1128 enableStatistics
: false,
1129 remoteAuthorization
: true,
1130 currentOutType
: CurrentType
.AC
,
1131 mainVoltageMeterValues
: true,
1132 phaseLineToLineVoltageMeterValues
: false,
1133 customValueLimitationMeterValues
: true,
1134 ocppStrictCompliance
: true,
1135 outOfOrderEndMeterValues
: false,
1136 beginEndMeterValues
: false,
1137 meteringPerTransaction
: true,
1138 transactionDataMeterValues
: false,
1139 supervisionUrlOcppConfiguration
: false,
1140 supervisionUrlOcppKey
: VendorParametersKey
.ConnectionUrl
,
1141 ocppVersion
: OCPPVersion
.VERSION_16
,
1142 ocppPersistentConfiguration
: true,
1143 stationInfoPersistentConfiguration
: true,
1144 automaticTransactionGeneratorPersistentConfiguration
: true,
1145 autoReconnectMaxRetries
: -1,
1146 registrationMaxRetries
: -1,
1147 reconnectExponentialDelay
: false,
1148 stopTransactionsOnStopped
: true,
1150 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1151 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile(
1152 stationInfoFromTemplate
?.stationInfoPersistentConfiguration
,
1155 // 1. charging station info from template
1156 // 2. charging station info from configuration file
1157 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1158 return { ...defaultStationInfo
, ...stationInfoFromFile
! };
1160 stationInfoFromFile
&&
1161 propagateSerialNumber(
1162 this.getTemplateFromFile()!,
1163 stationInfoFromFile
,
1164 stationInfoFromTemplate
,
1166 return { ...defaultStationInfo
, ...stationInfoFromTemplate
};
1169 private saveStationInfo(): void {
1170 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1171 this.saveConfiguration();
1175 private handleUnsupportedVersion(version
: OCPPVersion
| undefined) {
1176 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1177 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1178 throw new BaseError(errorMsg
);
1181 private initialize(): void {
1182 const stationTemplate
= this.getTemplateFromFile()!;
1183 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1184 this.configurationFile
= join(
1185 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1186 `${getHashId(this.index, stationTemplate)}.json`,
1188 const stationConfiguration
= this.getConfigurationFromFile();
1190 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1191 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1192 (stationConfiguration
?.connectorsStatus
|| stationConfiguration
?.evsesStatus
)
1194 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
);
1196 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1198 this.stationInfo
= this.getStationInfo();
1200 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1201 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1202 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1204 const patternGroup
: number | undefined =
1205 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1206 this.stationInfo
.firmwareVersion
?.split('.').length
;
1207 const match
= this.stationInfo
1208 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1209 .slice(1, patternGroup
! + 1);
1210 const patchLevelIndex
= match
.length
- 1;
1211 match
[patchLevelIndex
] = (
1212 convertToInt(match
[patchLevelIndex
]) +
1213 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1215 this.stationInfo
.firmwareVersion
= match
?.join('.');
1217 this.saveStationInfo();
1218 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1219 if (this.stationInfo
?.enableStatistics
=== true) {
1220 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1221 this.stationInfo
.hashId
,
1222 this.stationInfo
.chargingStationId
!,
1223 this.configuredSupervisionUrl
,
1226 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1227 this.powerDivider
= this.getPowerDivider();
1228 // OCPP configuration
1229 this.ocppConfiguration
= this.getOcppConfiguration();
1230 this.initializeOcppConfiguration();
1231 this.initializeOcppServices();
1232 this.once(ChargingStationEvents
.accepted
, () => {
1233 this.startMessageSequence().catch((error
) => {
1234 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
);
1237 if (this.stationInfo
?.autoRegister
=== true) {
1238 this.bootNotificationResponse
= {
1239 currentTime
: new Date(),
1240 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1241 status: RegistrationStatusEnumType
.ACCEPTED
,
1246 private initializeOcppServices(): void {
1247 const ocppVersion
= this.stationInfo
?.ocppVersion
;
1248 switch (ocppVersion
) {
1249 case OCPPVersion
.VERSION_16
:
1250 this.ocppIncomingRequestService
=
1251 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1252 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1253 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1256 case OCPPVersion
.VERSION_20
:
1257 case OCPPVersion
.VERSION_201
:
1258 this.ocppIncomingRequestService
=
1259 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1260 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1261 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1265 this.handleUnsupportedVersion(ocppVersion
);
1270 private initializeOcppConfiguration(): void {
1271 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
))) {
1272 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0');
1274 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
))) {
1275 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false });
1278 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1279 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1280 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1282 addConfigurationKey(
1284 this.stationInfo
.supervisionUrlOcppKey
!,
1285 this.configuredSupervisionUrl
.href
,
1289 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1290 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1291 !isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1293 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!, { save
: false });
1296 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1297 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!))
1299 addConfigurationKey(
1301 this.stationInfo
.amperageLimitationOcppKey
!,
1303 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1308 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
))
1310 addConfigurationKey(
1312 StandardParametersKey
.SupportedFeatureProfiles
,
1313 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1316 addConfigurationKey(
1318 StandardParametersKey
.NumberOfConnectors
,
1319 this.getNumberOfConnectors().toString(),
1321 { overwrite
: true },
1324 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
))
1326 addConfigurationKey(
1328 StandardParametersKey
.MeterValuesSampledData
,
1329 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1333 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
))
1335 const connectorsPhaseRotation
: string[] = [];
1336 if (this.hasEvses
) {
1337 for (const evseStatus
of this.evses
.values()) {
1338 for (const connectorId
of evseStatus
.connectors
.keys()) {
1339 connectorsPhaseRotation
.push(
1340 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1345 for (const connectorId
of this.connectors
.keys()) {
1346 connectorsPhaseRotation
.push(
1347 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1351 addConfigurationKey(
1353 StandardParametersKey
.ConnectorPhaseRotation
,
1354 connectorsPhaseRotation
.toString(),
1358 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
))
1360 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
1363 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
)) &&
1364 getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)?.value
?.includes(
1365 SupportedFeatureProfiles
.LocalAuthListManagement
,
1368 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false');
1370 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
))) {
1371 addConfigurationKey(
1373 StandardParametersKey
.ConnectionTimeOut
,
1374 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1377 this.saveOcppConfiguration();
1380 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1381 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1382 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1383 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1385 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1386 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1387 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1388 delete evseStatus
.connectorsStatus
;
1389 this.evses
.set(evseId
, {
1390 ...(evseStatus
as EvseStatus
),
1391 connectors
: new Map
<number, ConnectorStatus
>(
1392 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1399 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1400 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1401 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1402 throw new BaseError(errorMsg
);
1404 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1405 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1406 throw new BaseError(errorMsg
);
1410 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1411 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1412 this.initializeConnectorsFromTemplate(stationTemplate
);
1413 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1414 this.initializeEvsesFromTemplate(stationTemplate
);
1415 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1416 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1417 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1418 throw new BaseError(errorMsg
);
1420 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1421 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1422 throw new BaseError(errorMsg
);
1426 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1427 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1428 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1429 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1430 throw new BaseError(errorMsg
);
1432 if (!stationTemplate
?.Connectors
?.[0]) {
1434 `${this.logPrefix()} Charging station information from template ${
1436 } with no connector id 0 configuration`,
1439 if (stationTemplate
?.Connectors
) {
1440 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1441 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1442 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1444 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1447 const connectorsConfigChanged
=
1448 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1449 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1450 connectorsConfigChanged
&& this.connectors
.clear();
1451 this.connectorsConfigurationHash
= connectorsConfigHash
;
1452 if (templateMaxConnectors
> 0) {
1453 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1455 connectorId
=== 0 &&
1456 (!stationTemplate
?.Connectors
?.[connectorId
] ||
1457 this.getUseConnectorId0(stationTemplate
) === false)
1461 const templateConnectorId
=
1462 connectorId
> 0 && stationTemplate
?.randomConnectors
1463 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1465 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1466 checkStationInfoConnectorStatus(
1467 templateConnectorId
,
1472 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1474 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1475 this.saveConnectorsStatus();
1478 `${this.logPrefix()} Charging station information from template ${
1480 } with no connectors configuration defined, cannot create connectors`,
1486 `${this.logPrefix()} Charging station information from template ${
1488 } with no connectors configuration defined, using already defined connectors`,
1493 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1494 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1495 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1496 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1497 throw new BaseError(errorMsg
);
1499 if (!stationTemplate
?.Evses
?.[0]) {
1501 `${this.logPrefix()} Charging station information from template ${
1503 } with no evse id 0 configuration`,
1506 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1508 `${this.logPrefix()} Charging station information from template ${
1510 } with evse id 0 with no connector id 0 configuration`,
1513 if (Object.keys(stationTemplate
?.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1515 `${this.logPrefix()} Charging station information from template ${
1517 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`,
1520 if (stationTemplate
?.Evses
) {
1521 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1522 .update(JSON
.stringify(stationTemplate
?.Evses
))
1524 const evsesConfigChanged
=
1525 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1526 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1527 evsesConfigChanged
&& this.evses
.clear();
1528 this.evsesConfigurationHash
= evsesConfigHash
;
1529 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1530 if (templateMaxEvses
> 0) {
1531 for (const evseKey
in stationTemplate
.Evses
) {
1532 const evseId
= convertToInt(evseKey
);
1533 this.evses
.set(evseId
, {
1534 connectors
: buildConnectorsMap(
1535 stationTemplate
?.Evses
[evseKey
]?.Connectors
,
1539 availability
: AvailabilityType
.Operative
,
1541 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1543 this.saveEvsesStatus();
1546 `${this.logPrefix()} Charging station information from template ${
1548 } with no evses configuration defined, cannot create evses`,
1554 `${this.logPrefix()} Charging station information from template ${
1556 } with no evses configuration defined, using already defined evses`,
1561 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1562 let configuration
: ChargingStationConfiguration
| undefined;
1563 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1565 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1566 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1567 this.configurationFileHash
,
1570 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1571 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1572 configuration
= JSON
.parse(
1573 readFileSync(this.configurationFile
, 'utf8'),
1574 ) as ChargingStationConfiguration
;
1575 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1576 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1577 this.configurationFileHash
= configuration
.configurationHash
!;
1580 handleFileException(
1581 this.configurationFile
,
1582 FileType
.ChargingStationConfiguration
,
1583 error
as NodeJS
.ErrnoException
,
1588 return configuration
;
1591 private saveAutomaticTransactionGeneratorConfiguration(): void {
1592 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1593 this.saveConfiguration();
1597 private saveConnectorsStatus() {
1598 this.saveConfiguration();
1601 private saveEvsesStatus() {
1602 this.saveConfiguration();
1605 private saveConfiguration(): void {
1606 if (isNotEmptyString(this.configurationFile
)) {
1608 if (!existsSync(dirname(this.configurationFile
))) {
1609 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1611 let configurationData
: ChargingStationConfiguration
= this.getConfigurationFromFile()
1612 ? cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!)
1614 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true && this.stationInfo
) {
1615 configurationData
.stationInfo
= this.stationInfo
;
1617 delete configurationData
.stationInfo
;
1620 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1621 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1623 configurationData
.configurationKey
= this.ocppConfiguration
?.configurationKey
;
1625 delete configurationData
.configurationKey
;
1627 configurationData
= merge
<ChargingStationConfiguration
>(
1629 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1632 !this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
||
1633 !this.getAutomaticTransactionGeneratorConfiguration()
1635 delete configurationData
.automaticTransactionGenerator
;
1637 if (this.connectors
.size
> 0) {
1638 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1640 delete configurationData
.connectorsStatus
;
1642 if (this.evses
.size
> 0) {
1643 configurationData
.evsesStatus
= buildEvsesStatus(this);
1645 delete configurationData
.evsesStatus
;
1647 delete configurationData
.configurationHash
;
1648 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1651 stationInfo
: configurationData
.stationInfo
,
1652 configurationKey
: configurationData
.configurationKey
,
1653 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1654 ...(this.connectors
.size
> 0 && {
1655 connectorsStatus
: configurationData
.connectorsStatus
,
1657 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
}),
1658 } as ChargingStationConfiguration
),
1661 if (this.configurationFileHash
!== configurationHash
) {
1662 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1663 configurationData
.configurationHash
= configurationHash
;
1664 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1665 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1667 this.configurationFile
,
1668 JSON
.stringify(configurationData
, undefined, 2),
1671 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1672 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1673 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1674 this.configurationFileHash
= configurationHash
;
1675 }).catch((error
) => {
1676 handleFileException(
1677 this.configurationFile
,
1678 FileType
.ChargingStationConfiguration
,
1679 error
as NodeJS
.ErrnoException
,
1685 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1686 this.configurationFile
1691 handleFileException(
1692 this.configurationFile
,
1693 FileType
.ChargingStationConfiguration
,
1694 error
as NodeJS
.ErrnoException
,
1700 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1705 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1706 return this.getTemplateFromFile()?.Configuration
;
1709 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1710 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1711 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1712 return { configurationKey
};
1717 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1718 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1719 this.getOcppConfigurationFromFile();
1720 if (!ocppConfiguration
) {
1721 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1723 return ocppConfiguration
;
1726 private async onOpen(): Promise
<void> {
1727 if (this.isWebSocketConnectionOpened() === true) {
1729 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1731 let registrationRetryCount
= 0;
1732 if (this.isRegistered() === false) {
1733 // Send BootNotification
1735 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1736 BootNotificationRequest
,
1737 BootNotificationResponse
1738 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1739 skipBufferingOnError
: true,
1741 if (this.isRegistered() === false) {
1742 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
;
1744 this?.bootNotificationResponse
?.interval
1745 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1746 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1750 this.isRegistered() === false &&
1751 (registrationRetryCount
<= this.stationInfo
.registrationMaxRetries
! ||
1752 this.stationInfo
?.registrationMaxRetries
=== -1)
1755 if (this.isRegistered() === true) {
1756 this.emit(ChargingStationEvents
.registered
);
1757 if (this.inAcceptedState() === true) {
1758 this.emit(ChargingStationEvents
.accepted
);
1762 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this
1763 .stationInfo?.registrationMaxRetries})`,
1766 this.autoReconnectRetryCount
= 0;
1767 this.emit(ChargingStationEvents
.updated
);
1770 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1775 private async onClose(code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): Promise
<void> {
1778 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1779 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1781 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1783 )}' and reason '${reason.toString()}'`,
1785 this.autoReconnectRetryCount
= 0;
1790 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1792 )}' and reason '${reason.toString()}'`,
1794 this.started
=== true && (await this.reconnect());
1797 this.emit(ChargingStationEvents
.updated
);
1800 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1801 const cachedRequest
= this.requests
.get(messageId
);
1802 if (Array.isArray(cachedRequest
) === true) {
1803 return cachedRequest
;
1805 throw new OCPPError(
1806 ErrorType
.PROTOCOL_ERROR
,
1807 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1809 )} is not an array`,
1811 cachedRequest
as JsonType
,
1815 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1816 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1817 if (this.stationInfo
?.enableStatistics
=== true) {
1818 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1821 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1825 // Process the message
1826 await this.ocppIncomingRequestService
.incomingRequestHandler(
1832 this.emit(ChargingStationEvents
.updated
);
1835 private handleResponseMessage(response
: Response
): void {
1836 const [messageType
, messageId
, commandPayload
] = response
;
1837 if (this.requests
.has(messageId
) === false) {
1839 throw new OCPPError(
1840 ErrorType
.INTERNAL_ERROR
,
1841 `Response for unknown message id ${messageId}`,
1847 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1852 `${this.logPrefix()} << Command '${
1853 requestCommandName ?? Constants.UNKNOWN_COMMAND
1854 }' received response payload: ${JSON.stringify(response)}`,
1856 responseCallback(commandPayload
, requestPayload
);
1859 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1860 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1861 if (this.requests
.has(messageId
) === false) {
1863 throw new OCPPError(
1864 ErrorType
.INTERNAL_ERROR
,
1865 `Error response for unknown message id ${messageId}`,
1867 { errorType
, errorMessage
, errorDetails
},
1870 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1872 `${this.logPrefix()} << Command '${
1873 requestCommandName ?? Constants.UNKNOWN_COMMAND
1874 }' received error response payload: ${JSON.stringify(errorResponse)}`,
1876 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1879 private async onMessage(data
: RawData
): Promise
<void> {
1880 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
1881 let messageType
: MessageType
| undefined;
1882 let errorMsg
: string;
1884 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1885 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1886 if (Array.isArray(request
) === true) {
1887 [messageType
] = request
;
1888 // Check the type of message
1889 switch (messageType
) {
1891 case MessageType
.CALL_MESSAGE
:
1892 await this.handleIncomingMessage(request
as IncomingRequest
);
1895 case MessageType
.CALL_RESULT_MESSAGE
:
1896 this.handleResponseMessage(request
as Response
);
1899 case MessageType
.CALL_ERROR_MESSAGE
:
1900 this.handleErrorMessage(request
as ErrorResponse
);
1904 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1905 errorMsg
= `Wrong message type ${messageType}`;
1906 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1907 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1910 throw new OCPPError(
1911 ErrorType
.PROTOCOL_ERROR
,
1912 'Incoming message is not an array',
1920 let commandName
: IncomingRequestCommand
| undefined;
1921 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
1922 let errorCallback
: ErrorCallback
;
1923 const [, messageId
] = request
!;
1924 switch (messageType
) {
1925 case MessageType
.CALL_MESSAGE
:
1926 [, , commandName
] = request
as IncomingRequest
;
1928 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1930 case MessageType
.CALL_RESULT_MESSAGE
:
1931 case MessageType
.CALL_ERROR_MESSAGE
:
1932 if (this.requests
.has(messageId
) === true) {
1933 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1934 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1935 errorCallback(error
as OCPPError
, false);
1937 // Remove the request from the cache in case of error at response handling
1938 this.requests
.delete(messageId
);
1942 if (error
instanceof OCPPError
=== false) {
1944 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1945 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1946 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1947 }' message '${data.toString()}' handling is not an OCPPError:`,
1952 `${this.logPrefix()} Incoming OCPP command '${
1953 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1954 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1955 }' message '${data.toString()}'${
1956 messageType !== MessageType.CALL_MESSAGE
1957 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1959 } processing error:`,
1965 private onPing(): void {
1966 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1969 private onPong(): void {
1970 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1973 private onError(error
: WSError
): void {
1974 this.closeWSConnection();
1975 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1978 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1979 if (this.stationInfo
?.meteringPerTransaction
=== true) {
1982 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
1983 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1988 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
1989 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1993 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
1994 return stationTemplate
?.useConnectorId0
?? true;
1997 private async stopRunningTransactions(reason
?: StopTransactionReason
): Promise
<void> {
1998 if (this.hasEvses
) {
1999 for (const [evseId
, evseStatus
] of this.evses
) {
2003 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2004 if (connectorStatus
.transactionStarted
=== true) {
2005 await this.stopTransactionOnConnector(connectorId
, reason
);
2010 for (const connectorId
of this.connectors
.keys()) {
2011 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2012 await this.stopTransactionOnConnector(connectorId
, reason
);
2019 private getConnectionTimeout(): number {
2020 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) !== undefined) {
2021 return convertToInt(
2022 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)!.value
! ??
2023 Constants
.DEFAULT_CONNECTION_TIMEOUT
,
2026 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2029 private getPowerDivider(): number {
2030 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2031 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2032 powerDivider
= this.getNumberOfRunningTransactions();
2034 return powerDivider
;
2037 private getMaximumAmperage(stationInfo
?: ChargingStationInfo
): number | undefined {
2038 const maximumPower
= (stationInfo
?? this.stationInfo
).maximumPower
!;
2039 switch (this.getCurrentOutType(stationInfo
)) {
2040 case CurrentType
.AC
:
2041 return ACElectricUtils
.amperagePerPhaseFromPower(
2042 this.getNumberOfPhases(stationInfo
),
2043 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2044 this.getVoltageOut(stationInfo
),
2046 case CurrentType
.DC
:
2047 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2051 private getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
2052 return (stationInfo
?? this.stationInfo
).currentOutType
?? CurrentType
.AC
;
2055 private getVoltageOut(stationInfo
?: ChargingStationInfo
): Voltage
{
2057 (stationInfo
?? this.stationInfo
).voltageOut
??
2058 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2062 private getAmperageLimitation(): number | undefined {
2064 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2065 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!) !== undefined
2069 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)?.value
,
2070 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2075 private async startMessageSequence(): Promise
<void> {
2076 if (this.stationInfo
?.autoRegister
=== true) {
2077 await this.ocppRequestService
.requestHandler
<
2078 BootNotificationRequest
,
2079 BootNotificationResponse
2080 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2081 skipBufferingOnError
: true,
2084 // Start WebSocket ping
2085 this.startWebSocketPing();
2087 this.startHeartbeat();
2088 // Initialize connectors status
2089 if (this.hasEvses
) {
2090 for (const [evseId
, evseStatus
] of this.evses
) {
2092 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2093 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2094 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2097 connectorBootStatus
,
2104 for (const connectorId
of this.connectors
.keys()) {
2105 if (connectorId
> 0) {
2106 const connectorBootStatus
= getBootConnectorStatus(
2109 this.getConnectorStatus(connectorId
)!,
2111 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2115 if (this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
) {
2116 await this.ocppRequestService
.requestHandler
<
2117 FirmwareStatusNotificationRequest
,
2118 FirmwareStatusNotificationResponse
2119 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2120 status: FirmwareStatus
.Installed
,
2122 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2126 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
2127 this.startAutomaticTransactionGenerator();
2129 this.flushMessageBuffer();
2132 private async stopMessageSequence(
2133 reason
?: StopTransactionReason
,
2134 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
,
2136 // Stop WebSocket ping
2137 this.stopWebSocketPing();
2139 this.stopHeartbeat();
2141 if (this.automaticTransactionGenerator
?.started
=== true) {
2142 this.stopAutomaticTransactionGenerator();
2144 // Stop ongoing transactions
2145 stopTransactions
&& (await this.stopRunningTransactions(reason
));
2146 if (this.hasEvses
) {
2147 for (const [evseId
, evseStatus
] of this.evses
) {
2149 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2150 await this.ocppRequestService
.requestHandler
<
2151 StatusNotificationRequest
,
2152 StatusNotificationResponse
2155 RequestCommand
.STATUS_NOTIFICATION
,
2156 OCPPServiceUtils
.buildStatusNotificationRequest(
2159 ConnectorStatusEnum
.Unavailable
,
2163 delete connectorStatus
?.status;
2168 for (const connectorId
of this.connectors
.keys()) {
2169 if (connectorId
> 0) {
2170 await this.ocppRequestService
.requestHandler
<
2171 StatusNotificationRequest
,
2172 StatusNotificationResponse
2175 RequestCommand
.STATUS_NOTIFICATION
,
2176 OCPPServiceUtils
.buildStatusNotificationRequest(
2179 ConnectorStatusEnum
.Unavailable
,
2182 delete this.getConnectorStatus(connectorId
)?.status;
2188 private startWebSocketPing(): void {
2189 const webSocketPingInterval
: number =
2190 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) !== undefined
2192 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
,
2195 if (webSocketPingInterval
> 0 && this.webSocketPingSetInterval
=== undefined) {
2196 this.webSocketPingSetInterval
= setInterval(() => {
2197 if (this.isWebSocketConnectionOpened() === true) {
2198 this.wsConnection
?.ping();
2200 }, secondsToMilliseconds(webSocketPingInterval
));
2202 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2203 webSocketPingInterval,
2206 } else if (this.webSocketPingSetInterval
!== undefined) {
2208 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2209 webSocketPingInterval,
2214 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2219 private stopWebSocketPing(): void {
2220 if (this.webSocketPingSetInterval
!== undefined) {
2221 clearInterval(this.webSocketPingSetInterval
);
2222 delete this.webSocketPingSetInterval
;
2226 private getConfiguredSupervisionUrl(): URL
{
2227 let configuredSupervisionUrl
: string;
2228 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2229 if (isNotEmptyArray(supervisionUrls
)) {
2230 let configuredSupervisionUrlIndex
: number;
2231 switch (Configuration
.getSupervisionUrlDistribution()) {
2232 case SupervisionUrlDistribution
.RANDOM
:
2233 configuredSupervisionUrlIndex
= Math.floor(
2234 secureRandom() * (supervisionUrls
as string[]).length
,
2237 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2238 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2240 Object.values(SupervisionUrlDistribution
).includes(
2241 Configuration
.getSupervisionUrlDistribution()!,
2244 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2245 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2246 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2249 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2252 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2254 configuredSupervisionUrl
= supervisionUrls
as string;
2256 if (isNotEmptyString(configuredSupervisionUrl
)) {
2257 return new URL(configuredSupervisionUrl
);
2259 const errorMsg
= 'No supervision url(s) configured';
2260 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2261 throw new BaseError(`${errorMsg}`);
2264 private stopHeartbeat(): void {
2265 if (this.heartbeatSetInterval
) {
2266 clearInterval(this.heartbeatSetInterval
);
2267 delete this.heartbeatSetInterval
;
2271 private terminateWSConnection(): void {
2272 if (this.isWebSocketConnectionOpened() === true) {
2273 this.wsConnection
?.terminate();
2274 this.wsConnection
= null;
2278 private async reconnect(): Promise
<void> {
2279 // Stop WebSocket ping
2280 this.stopWebSocketPing();
2282 this.stopHeartbeat();
2283 // Stop the ATG if needed
2284 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2285 this.stopAutomaticTransactionGenerator();
2288 this.autoReconnectRetryCount
< this.stationInfo
.autoReconnectMaxRetries
! ||
2289 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2291 ++this.autoReconnectRetryCount
;
2292 const reconnectDelay
=
2293 this.stationInfo
?.reconnectExponentialDelay
=== true
2294 ? exponentialDelay(this.autoReconnectRetryCount
)
2295 : secondsToMilliseconds(this.getConnectionTimeout());
2296 const reconnectDelayWithdraw
= 1000;
2297 const reconnectTimeout
=
2298 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2299 ? reconnectDelay
- reconnectDelayWithdraw
2302 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2305 )}ms, timeout ${reconnectTimeout}ms`,
2307 await sleep(reconnectDelay
);
2309 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2311 this.openWSConnection(
2313 handshakeTimeout
: reconnectTimeout
,
2315 { closeOpened
: true },
2317 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2319 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2320 this.autoReconnectRetryCount
2321 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`,