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';
26 checkConnectorsConfiguration
,
27 checkStationInfoConnectorStatus
,
29 createBootNotificationRequest
,
31 getAmperageLimitationUnitDivider
,
32 getBootConnectorStatus
,
33 getChargingStationConnectorChargingProfilesPowerLimit
,
39 getNumberOfReservableConnectors
,
40 getPhaseRotationValue
,
41 hasReservationExpired
,
42 initializeConnectorsMapStatus
,
43 propagateSerialNumber
,
44 stationTemplateToStationInfo
,
45 warnTemplateKeysDeprecation
,
47 import { IdTagsCache
} from
'./IdTagsCache';
49 OCPP16IncomingRequestService
,
51 OCPP16ResponseService
,
52 OCPP20IncomingRequestService
,
54 OCPP20ResponseService
,
55 type OCPPIncomingRequestService
,
56 type OCPPRequestService
,
58 buildStatusNotificationRequest
,
59 buildTransactionEndMeterValue
,
61 sendAndSetConnectorStatus
,
63 import { SharedLRUCache
} from
'./SharedLRUCache';
64 import { BaseError
, OCPPError
} from
'../exception';
65 import { PerformanceStatistics
} from
'../performance';
67 type AutomaticTransactionGeneratorConfiguration
,
69 type BootNotificationRequest
,
70 type BootNotificationResponse
,
72 type ChargingStationConfiguration
,
73 ChargingStationEvents
,
74 type ChargingStationInfo
,
75 type ChargingStationOcppConfiguration
,
76 type ChargingStationTemplate
,
84 type EvseStatusConfiguration
,
87 type FirmwareStatusNotificationRequest
,
88 type FirmwareStatusNotificationResponse
,
90 type HeartbeatRequest
,
91 type HeartbeatResponse
,
93 type IncomingRequestCommand
,
96 type MeterValuesRequest
,
97 type MeterValuesResponse
,
101 RegistrationStatusEnumType
,
105 ReservationTerminationReason
,
107 StandardParametersKey
,
109 type StatusNotificationRequest
,
110 type StatusNotificationResponse
,
111 StopTransactionReason
,
112 type StopTransactionRequest
,
113 type StopTransactionResponse
,
114 SupervisionUrlDistribution
,
115 SupportedFeatureProfiles
,
119 WebSocketCloseEventStatusCode
,
129 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
130 buildConnectorsStatus
,
139 formatDurationMilliSeconds
,
140 formatDurationSeconds
,
142 getWebSocketCloseEventStatusString
,
158 export class ChargingStation
extends EventEmitter
{
159 public readonly index
: number;
160 public readonly templateFile
: string;
161 public stationInfo
!: ChargingStationInfo
;
162 public started
: boolean;
163 public starting
: boolean;
164 public idTagsCache
: IdTagsCache
;
165 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
166 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
167 public wsConnection
: WebSocket
| null;
168 public readonly connectors
: Map
<number, ConnectorStatus
>;
169 public readonly evses
: Map
<number, EvseStatus
>;
170 public readonly requests
: Map
<string, CachedRequest
>;
171 public performanceStatistics
!: PerformanceStatistics
| undefined;
172 public heartbeatSetInterval
?: NodeJS
.Timeout
;
173 public ocppRequestService
!: OCPPRequestService
;
174 public bootNotificationRequest
!: BootNotificationRequest
;
175 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
176 public powerDivider
!: number;
177 private stopping
: boolean;
178 private configurationFile
!: string;
179 private configurationFileHash
!: string;
180 private connectorsConfigurationHash
!: string;
181 private evsesConfigurationHash
!: string;
182 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
;
183 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
184 private readonly messageBuffer
: Set
<string>;
185 private configuredSupervisionUrl
!: URL
;
186 private autoReconnectRetryCount
: number;
187 private templateFileWatcher
!: FSWatcher
| undefined;
188 private templateFileHash
!: string;
189 private readonly sharedLRUCache
: SharedLRUCache
;
190 private webSocketPingSetInterval
?: NodeJS
.Timeout
;
191 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
192 private flushMessageBufferSetInterval
?: NodeJS
.Timeout
;
194 constructor(index
: number, templateFile
: string) {
196 this.started
= false;
197 this.starting
= false;
198 this.stopping
= false;
199 this.wsConnection
= null;
200 this.autoReconnectRetryCount
= 0;
202 this.templateFile
= templateFile
;
203 this.connectors
= new Map
<number, ConnectorStatus
>();
204 this.evses
= new Map
<number, EvseStatus
>();
205 this.requests
= new Map
<string, CachedRequest
>();
206 this.messageBuffer
= new Set
<string>();
207 this.sharedLRUCache
= SharedLRUCache
.getInstance();
208 this.idTagsCache
= IdTagsCache
.getInstance();
209 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
211 this.on(ChargingStationEvents
.started
, () => {
212 parentPort
?.postMessage(buildStartedMessage(this));
214 this.on(ChargingStationEvents
.stopped
, () => {
215 parentPort
?.postMessage(buildStoppedMessage(this));
217 this.on(ChargingStationEvents
.updated
, () => {
218 parentPort
?.postMessage(buildUpdatedMessage(this));
224 public get
hasEvses(): boolean {
225 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
228 private get
wsConnectionUrl(): URL
{
231 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
232 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
233 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value)
234 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value
235 : this.configuredSupervisionUrl.href
236 }/${this.stationInfo.chargingStationId}`,
240 public logPrefix
= (): string => {
241 if (isNotEmptyString(this?.stationInfo
?.chargingStationId
)) {
242 return logPrefix(` ${this?.stationInfo?.chargingStationId} |`);
244 let stationTemplate
: ChargingStationTemplate
| undefined;
246 stationTemplate
= JSON
.parse(
247 readFileSync(this.templateFile
, 'utf8'),
248 ) as ChargingStationTemplate
;
250 stationTemplate
= undefined;
252 return logPrefix(` ${getChargingStationId(this.index, stationTemplate)} |`);
255 public hasIdTags(): boolean {
256 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
259 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
260 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
261 switch (this.getCurrentOutType(stationInfo
)) {
263 return localStationInfo
.numberOfPhases
?? 3;
269 public isWebSocketConnectionOpened(): boolean {
270 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
273 public inUnknownState(): boolean {
274 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
277 public inPendingState(): boolean {
278 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
281 public inAcceptedState(): boolean {
282 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
285 public inRejectedState(): boolean {
286 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
289 public isRegistered(): boolean {
291 this.inUnknownState() === false &&
292 (this.inAcceptedState() === true || this.inPendingState() === true)
296 public isChargingStationAvailable(): boolean {
297 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
300 public hasConnector(connectorId
: number): boolean {
302 for (const evseStatus
of this.evses
.values()) {
303 if (evseStatus
.connectors
.has(connectorId
)) {
309 return this.connectors
.has(connectorId
);
312 public isConnectorAvailable(connectorId
: number): boolean {
315 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
319 public getNumberOfConnectors(): number {
321 let numberOfConnectors
= 0;
322 for (const [evseId
, evseStatus
] of this.evses
) {
324 numberOfConnectors
+= evseStatus
.connectors
.size
;
327 return numberOfConnectors
;
329 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
332 public getNumberOfEvses(): number {
333 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
336 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
338 for (const evseStatus
of this.evses
.values()) {
339 if (evseStatus
.connectors
.has(connectorId
)) {
340 return evseStatus
.connectors
.get(connectorId
);
345 return this.connectors
.get(connectorId
);
348 public getConnectorMaximumAvailablePower(connectorId
: number): number {
349 let connectorAmperageLimitationPowerLimit
: number | undefined;
351 !isNullOrUndefined(this.getAmperageLimitation()) &&
352 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
354 connectorAmperageLimitationPowerLimit
=
355 (this.stationInfo
?.currentOutType
=== CurrentType
.AC
356 ? ACElectricUtils
.powerTotal(
357 this.getNumberOfPhases(),
358 this.stationInfo
.voltageOut
!,
359 this.getAmperageLimitation()! *
360 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
362 : DCElectricUtils
.power(this.stationInfo
.voltageOut
!, this.getAmperageLimitation()!)) /
365 const connectorMaximumPower
= this.stationInfo
.maximumPower
! / this.powerDivider
;
366 const connectorChargingProfilesPowerLimit
=
367 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
369 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
370 isNaN(connectorAmperageLimitationPowerLimit
!)
372 : connectorAmperageLimitationPowerLimit
!,
373 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
377 public getTransactionIdTag(transactionId
: number): string | undefined {
379 for (const evseStatus
of this.evses
.values()) {
380 for (const connectorStatus
of evseStatus
.connectors
.values()) {
381 if (connectorStatus
.transactionId
=== transactionId
) {
382 return connectorStatus
.transactionIdTag
;
387 for (const connectorId
of this.connectors
.keys()) {
388 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
389 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
395 public getNumberOfRunningTransactions(): number {
396 let numberOfRunningTransactions
= 0;
398 for (const [evseId
, evseStatus
] of this.evses
) {
402 for (const connectorStatus
of evseStatus
.connectors
.values()) {
403 if (connectorStatus
.transactionStarted
=== true) {
404 ++numberOfRunningTransactions
;
409 for (const connectorId
of this.connectors
.keys()) {
410 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
411 ++numberOfRunningTransactions
;
415 return numberOfRunningTransactions
;
418 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
420 for (const evseStatus
of this.evses
.values()) {
421 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
422 if (connectorStatus
.transactionId
=== transactionId
) {
428 for (const connectorId
of this.connectors
.keys()) {
429 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
436 public getEnergyActiveImportRegisterByTransactionId(
437 transactionId
: number,
440 return this.getEnergyActiveImportRegister(
441 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
446 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
447 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
450 public getAuthorizeRemoteTxRequests(): boolean {
451 const authorizeRemoteTxRequests
= getConfigurationKey(
453 StandardParametersKey
.AuthorizeRemoteTxRequests
,
455 return authorizeRemoteTxRequests
!== undefined
456 ? convertToBoolean(authorizeRemoteTxRequests
.value
)
460 public getLocalAuthListEnabled(): boolean {
461 const localAuthListEnabled
= getConfigurationKey(
463 StandardParametersKey
.LocalAuthListEnabled
,
465 return localAuthListEnabled
!== undefined
466 ? convertToBoolean(localAuthListEnabled
.value
)
470 public getHeartbeatInterval(): number {
471 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
);
472 if (HeartbeatInterval
!== undefined) {
473 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
));
475 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
);
476 if (HeartBeatInterval
!== undefined) {
477 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
));
479 this.stationInfo
?.autoRegister
=== false &&
481 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
482 Constants.DEFAULT_HEARTBEAT_INTERVAL
485 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
488 public setSupervisionUrl(url
: string): void {
490 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
491 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
)
493 setConfigurationKeyValue(this, this.stationInfo
.supervisionUrlOcppKey
!, url
);
495 this.stationInfo
.supervisionUrls
= url
;
496 this.saveStationInfo();
497 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
501 public startHeartbeat(): void {
502 if (this.getHeartbeatInterval() > 0 && this.heartbeatSetInterval
=== undefined) {
503 this.heartbeatSetInterval
= setInterval(() => {
504 this.ocppRequestService
505 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
508 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
512 }, this.getHeartbeatInterval());
514 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
515 this.getHeartbeatInterval(),
518 } else if (this.heartbeatSetInterval
!== undefined) {
520 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
521 this.getHeartbeatInterval(),
526 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`,
531 public restartHeartbeat(): void {
533 this.stopHeartbeat();
535 this.startHeartbeat();
538 public restartWebSocketPing(): void {
539 // Stop WebSocket ping
540 this.stopWebSocketPing();
541 // Start WebSocket ping
542 this.startWebSocketPing();
545 public startMeterValues(connectorId
: number, interval
: number): void {
546 if (connectorId
=== 0) {
548 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
552 if (!this.getConnectorStatus(connectorId
)) {
554 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
559 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
561 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`,
565 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
566 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
569 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`,
574 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
575 const meterValue
= buildMeterValue(
578 this.getConnectorStatus(connectorId
)!.transactionId
!,
581 this.ocppRequestService
582 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
584 RequestCommand
.METER_VALUES
,
587 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
588 meterValue
: [meterValue
],
593 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
600 `${this.logPrefix()} Charging station ${
601 StandardParametersKey.MeterValueSampleInterval
602 } configuration set to ${interval}, not sending MeterValues`,
607 public stopMeterValues(connectorId
: number) {
608 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
!== undefined) {
609 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
613 public start(): void {
614 if (this.started
=== false) {
615 if (this.starting
=== false) {
616 this.starting
= true;
617 if (this.stationInfo
?.enableStatistics
=== true) {
618 this.performanceStatistics
?.start();
620 this.openWSConnection();
621 // Monitor charging station template file
622 this.templateFileWatcher
= watchJsonFile(
624 FileType
.ChargingStationTemplate
,
627 (event
, filename
): void => {
628 if (isNotEmptyString(filename
) && event
=== 'change') {
631 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
633 } file have changed, reload`,
635 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
638 this.idTagsCache
.deleteIdTags(getIdTagsFile(this.stationInfo
)!);
640 this.stopAutomaticTransactionGenerator();
641 delete this.automaticTransactionGeneratorConfiguration
;
642 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
643 this.startAutomaticTransactionGenerator();
645 if (this.stationInfo
?.enableStatistics
=== true) {
646 this.performanceStatistics
?.restart();
648 this.performanceStatistics
?.stop();
650 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
653 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
661 this.emit(ChargingStationEvents
.started
);
662 this.starting
= false;
664 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
667 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
671 public async stop(reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
672 if (this.started
=== true) {
673 if (this.stopping
=== false) {
674 this.stopping
= true;
675 await this.stopMessageSequence(reason
, stopTransactions
);
676 this.closeWSConnection();
677 if (this.stationInfo
?.enableStatistics
=== true) {
678 this.performanceStatistics
?.stop();
680 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
681 this.templateFileWatcher
?.close();
682 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
683 delete this.bootNotificationResponse
;
684 this.started
= false;
685 this.saveConfiguration();
686 this.emit(ChargingStationEvents
.stopped
);
687 this.stopping
= false;
689 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
692 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
696 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
697 await this.stop(reason
);
698 await sleep(this.stationInfo
.resetTime
!);
703 public saveOcppConfiguration(): void {
704 if (this.stationInfo
?.ocppPersistentConfiguration
=== true) {
705 this.saveConfiguration();
709 public bufferMessage(message
: string): void {
710 this.messageBuffer
.add(message
);
711 this.setIntervalFlushMessageBuffer();
714 public openWSConnection(
716 params
?: { closeOpened
?: boolean; terminateOpened
?: boolean },
719 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
720 ...this.stationInfo
?.wsOptions
,
723 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
724 if (!checkChargingStation(this, this.logPrefix())) {
728 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
729 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
731 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
733 if (params
?.closeOpened
) {
734 this.closeWSConnection();
736 if (params
?.terminateOpened
) {
737 this.terminateWSConnection();
740 if (this.isWebSocketConnectionOpened() === true) {
742 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`,
748 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
751 this.wsConnection
= new WebSocket(
752 this.wsConnectionUrl
,
753 `ocpp${this.stationInfo?.ocppVersion}`,
757 // Handle WebSocket message
758 this.wsConnection
.on(
760 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
762 // Handle WebSocket error
763 this.wsConnection
.on(
765 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
767 // Handle WebSocket close
768 this.wsConnection
.on(
770 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
772 // Handle WebSocket open
773 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
774 // Handle WebSocket ping
775 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
776 // Handle WebSocket pong
777 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
780 public closeWSConnection(): void {
781 if (this.isWebSocketConnectionOpened() === true) {
782 this.wsConnection
?.close();
783 this.wsConnection
= null;
787 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
788 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration
)) {
789 let automaticTransactionGeneratorConfiguration
:
790 | AutomaticTransactionGeneratorConfiguration
792 const stationTemplate
= this.getTemplateFromFile();
793 const stationConfiguration
= this.getConfigurationFromFile();
795 this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true &&
796 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
797 stationConfiguration
?.automaticTransactionGenerator
799 automaticTransactionGeneratorConfiguration
=
800 stationConfiguration
?.automaticTransactionGenerator
;
802 automaticTransactionGeneratorConfiguration
= stationTemplate
?.AutomaticTransactionGenerator
;
804 this.automaticTransactionGeneratorConfiguration
= {
805 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
806 ...automaticTransactionGeneratorConfiguration
,
809 return this.automaticTransactionGeneratorConfiguration
!;
812 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
813 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
816 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
817 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
818 if (isNotEmptyArray(connectorIds
)) {
819 for (const connectorId
of connectorIds
!) {
820 this.automaticTransactionGenerator
?.startConnector(connectorId
);
823 this.automaticTransactionGenerator
?.start();
825 this.saveAutomaticTransactionGeneratorConfiguration();
826 this.emit(ChargingStationEvents
.updated
);
829 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
830 if (isNotEmptyArray(connectorIds
)) {
831 for (const connectorId
of connectorIds
!) {
832 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
835 this.automaticTransactionGenerator
?.stop();
837 this.saveAutomaticTransactionGeneratorConfiguration();
838 this.emit(ChargingStationEvents
.updated
);
841 public async stopTransactionOnConnector(
843 reason
?: StopTransactionReason
,
844 ): Promise
<StopTransactionResponse
> {
845 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
847 this.stationInfo
?.beginEndMeterValues
=== true &&
848 this.stationInfo
?.ocppStrictCompliance
=== true &&
849 this.stationInfo
?.outOfOrderEndMeterValues
=== false
851 const transactionEndMeterValue
= buildTransactionEndMeterValue(
854 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
856 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
858 RequestCommand
.METER_VALUES
,
862 meterValue
: [transactionEndMeterValue
],
866 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
868 RequestCommand
.STOP_TRANSACTION
,
871 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
872 ...(isNullOrUndefined(reason
) && { reason
}),
877 public getReserveConnectorZeroSupported(): boolean {
878 return convertToBoolean(
879 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
,
883 public async addReservation(reservation
: Reservation
): Promise
<void> {
884 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
);
885 if (reservationFound
!== undefined) {
886 await this.removeReservation(reservationFound
, ReservationTerminationReason
.REPLACE_EXISTING
);
888 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
889 await sendAndSetConnectorStatus(
891 reservation
.connectorId
,
892 ConnectorStatusEnum
.Reserved
,
894 { send
: reservation
.connectorId
!== 0 },
898 public async removeReservation(
899 reservation
: Reservation
,
900 reason
: ReservationTerminationReason
,
902 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
904 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
905 case ReservationTerminationReason
.TRANSACTION_STARTED
:
906 delete connector
.reservation
;
908 case ReservationTerminationReason
.RESERVATION_CANCELED
:
909 case ReservationTerminationReason
.REPLACE_EXISTING
:
910 case ReservationTerminationReason
.EXPIRED
:
911 await sendAndSetConnectorStatus(
913 reservation
.connectorId
,
914 ConnectorStatusEnum
.Available
,
916 { send
: reservation
.connectorId
!== 0 },
918 delete connector
.reservation
;
921 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
922 throw new BaseError(`Unknown reservation termination reason '${reason}'`);
926 public getReservationBy(
927 filterKey
: ReservationKey
,
928 value
: number | string,
929 ): Reservation
| undefined {
931 for (const evseStatus
of this.evses
.values()) {
932 for (const connectorStatus
of evseStatus
.connectors
.values()) {
933 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
934 return connectorStatus
.reservation
;
939 for (const connectorStatus
of this.connectors
.values()) {
940 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
941 return connectorStatus
.reservation
;
947 public isConnectorReservable(
948 reservationId
: number,
950 connectorId
?: number,
952 const reservation
= this.getReservationBy('reservationId', reservationId
);
953 const reservationExists
= !isUndefined(reservation
) && !hasReservationExpired(reservation
!);
954 if (arguments.length
=== 1) {
955 return !reservationExists
;
956 } else if (arguments.length
> 1) {
957 const userReservation
= !isUndefined(idTag
)
958 ? this.getReservationBy('idTag', idTag
!)
960 const userReservationExists
=
961 !isUndefined(userReservation
) && !hasReservationExpired(userReservation
!);
962 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
963 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
965 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
971 private setIntervalFlushMessageBuffer(): void {
972 if (this.flushMessageBufferSetInterval
=== undefined) {
973 this.flushMessageBufferSetInterval
= setInterval(() => {
974 if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) {
975 this.flushMessageBuffer();
977 if (this.messageBuffer
.size
=== 0) {
978 this.clearIntervalFlushMessageBuffer();
980 }, Constants
.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL
);
984 private clearIntervalFlushMessageBuffer() {
985 if (this.flushMessageBufferSetInterval
!== undefined) {
986 clearInterval(this.flushMessageBufferSetInterval
);
987 delete this.flushMessageBufferSetInterval
;
991 private getNumberOfReservableConnectors(): number {
992 let numberOfReservableConnectors
= 0;
994 for (const evseStatus
of this.evses
.values()) {
995 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
);
998 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
);
1000 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
1003 private getNumberOfReservationsOnConnectorZero(): number {
1005 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1006 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) ||
1007 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
)
1014 private flushMessageBuffer(): void {
1015 if (this.messageBuffer
.size
> 0) {
1016 for (const message
of this.messageBuffer
.values()) {
1017 let beginId
: string | undefined;
1018 let commandName
: RequestCommand
| undefined;
1019 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1020 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1022 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1023 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1025 this.wsConnection
?.send(message
, (error
?: Error) => {
1026 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1027 if (isNullOrUndefined(error
)) {
1029 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1031 )} OCPP message sent '${JSON.stringify(message)}'`,
1033 this.messageBuffer
.delete(message
);
1036 `${this.logPrefix()} >> Buffered ${getMessageTypeString(
1038 )} OCPP message '${JSON.stringify(message)}' send failed:`,
1047 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1048 let template
: ChargingStationTemplate
| undefined;
1050 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1051 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1053 const measureId
= `${FileType.ChargingStationTemplate} read`;
1054 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1055 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1056 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1057 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1058 .update(JSON
.stringify(template
))
1060 this.sharedLRUCache
.setChargingStationTemplate(template
);
1061 this.templateFileHash
= template
.templateHash
;
1064 handleFileException(
1066 FileType
.ChargingStationTemplate
,
1067 error
as NodeJS
.ErrnoException
,
1074 private getStationInfoFromTemplate(): ChargingStationInfo
{
1075 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1076 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1077 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this);
1078 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
);
1079 if (stationTemplate
?.Connectors
) {
1080 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1082 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1083 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1084 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1085 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1086 createSerialNumber(stationTemplate
, stationInfo
);
1087 stationInfo
.voltageOut
= this.getVoltageOut(stationInfo
);
1088 if (isNotEmptyArray(stationTemplate
?.power
)) {
1089 stationTemplate
.power
= stationTemplate
.power
as number[];
1090 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1091 stationInfo
.maximumPower
=
1092 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1093 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1094 : stationTemplate
.power
[powerArrayRandomIndex
];
1096 stationTemplate
.power
= stationTemplate
?.power
as number;
1097 stationInfo
.maximumPower
=
1098 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1099 ? stationTemplate
.power
* 1000
1100 : stationTemplate
.power
;
1102 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1103 stationInfo
.firmwareVersionPattern
=
1104 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1106 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1107 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1110 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1112 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1115 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1122 stationTemplate
?.firmwareUpgrade
?? {},
1124 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1125 ? secondsToMilliseconds(stationTemplate
.resetTime
!)
1126 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1130 private getStationInfoFromFile(
1131 stationInfoPersistentConfiguration
= true,
1132 ): ChargingStationInfo
| undefined {
1133 let stationInfo
: ChargingStationInfo
| undefined;
1134 if (stationInfoPersistentConfiguration
=== true) {
1135 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1137 delete stationInfo
?.infoHash
;
1143 private getStationInfo(): ChargingStationInfo
{
1144 const defaultStationInfo
: Partial
<ChargingStationInfo
> = {
1145 enableStatistics
: false,
1146 remoteAuthorization
: true,
1147 currentOutType
: CurrentType
.AC
,
1148 mainVoltageMeterValues
: true,
1149 phaseLineToLineVoltageMeterValues
: false,
1150 customValueLimitationMeterValues
: true,
1151 ocppStrictCompliance
: true,
1152 outOfOrderEndMeterValues
: false,
1153 beginEndMeterValues
: false,
1154 meteringPerTransaction
: true,
1155 transactionDataMeterValues
: false,
1156 supervisionUrlOcppConfiguration
: false,
1157 supervisionUrlOcppKey
: VendorParametersKey
.ConnectionUrl
,
1158 ocppVersion
: OCPPVersion
.VERSION_16
,
1159 ocppPersistentConfiguration
: true,
1160 stationInfoPersistentConfiguration
: true,
1161 automaticTransactionGeneratorPersistentConfiguration
: true,
1162 autoReconnectMaxRetries
: -1,
1163 registrationMaxRetries
: -1,
1164 reconnectExponentialDelay
: false,
1165 stopTransactionsOnStopped
: true,
1167 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1168 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile(
1169 stationInfoFromTemplate
?.stationInfoPersistentConfiguration
,
1172 // 1. charging station info from template
1173 // 2. charging station info from configuration file
1174 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1175 return { ...defaultStationInfo
, ...stationInfoFromFile
! };
1177 stationInfoFromFile
&&
1178 propagateSerialNumber(
1179 this.getTemplateFromFile()!,
1180 stationInfoFromFile
,
1181 stationInfoFromTemplate
,
1183 return { ...defaultStationInfo
, ...stationInfoFromTemplate
};
1186 private saveStationInfo(): void {
1187 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true) {
1188 this.saveConfiguration();
1192 private handleUnsupportedVersion(version
: OCPPVersion
| undefined) {
1193 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1194 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1195 throw new BaseError(errorMsg
);
1198 private initialize(): void {
1199 const stationTemplate
= this.getTemplateFromFile()!;
1200 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1201 this.configurationFile
= join(
1202 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1203 `${getHashId(this.index, stationTemplate)}.json`,
1205 const stationConfiguration
= this.getConfigurationFromFile();
1207 stationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1208 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1209 (stationConfiguration
?.connectorsStatus
|| stationConfiguration
?.evsesStatus
)
1211 checkConfiguration(stationConfiguration
, this.logPrefix(), this.configurationFile
);
1212 this.initializeConnectorsOrEvsesFromFile(stationConfiguration
);
1214 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1216 this.stationInfo
= this.getStationInfo();
1218 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1219 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1220 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1222 const patternGroup
: number | undefined =
1223 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1224 this.stationInfo
.firmwareVersion
?.split('.').length
;
1225 const match
= this.stationInfo
1226 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1227 .slice(1, patternGroup
! + 1);
1228 const patchLevelIndex
= match
.length
- 1;
1229 match
[patchLevelIndex
] = (
1230 convertToInt(match
[patchLevelIndex
]) +
1231 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1233 this.stationInfo
.firmwareVersion
= match
?.join('.');
1235 this.saveStationInfo();
1236 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1237 if (this.stationInfo
?.enableStatistics
=== true) {
1238 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1239 this.stationInfo
.hashId
,
1240 this.stationInfo
.chargingStationId
!,
1241 this.configuredSupervisionUrl
,
1244 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1245 this.powerDivider
= this.getPowerDivider();
1246 // OCPP configuration
1247 this.ocppConfiguration
= this.getOcppConfiguration();
1248 this.initializeOcppConfiguration();
1249 this.initializeOcppServices();
1250 this.once(ChargingStationEvents
.accepted
, () => {
1251 this.startMessageSequence().catch((error
) => {
1252 logger
.error(`${this.logPrefix()} Error while starting the message sequence:`, error
);
1255 if (this.stationInfo
?.autoRegister
=== true) {
1256 this.bootNotificationResponse
= {
1257 currentTime
: new Date(),
1258 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1259 status: RegistrationStatusEnumType
.ACCEPTED
,
1264 private initializeOcppServices(): void {
1265 const ocppVersion
= this.stationInfo
?.ocppVersion
;
1266 switch (ocppVersion
) {
1267 case OCPPVersion
.VERSION_16
:
1268 this.ocppIncomingRequestService
=
1269 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1270 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1271 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1274 case OCPPVersion
.VERSION_20
:
1275 case OCPPVersion
.VERSION_201
:
1276 this.ocppIncomingRequestService
=
1277 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1278 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1279 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1283 this.handleUnsupportedVersion(ocppVersion
);
1288 private initializeOcppConfiguration(): void {
1289 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
))) {
1290 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0');
1292 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
))) {
1293 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false });
1296 this.stationInfo
?.supervisionUrlOcppConfiguration
=== true &&
1297 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1298 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1300 addConfigurationKey(
1302 this.stationInfo
.supervisionUrlOcppKey
!,
1303 this.configuredSupervisionUrl
.href
,
1307 this.stationInfo
?.supervisionUrlOcppConfiguration
=== false &&
1308 isNotEmptyString(this.stationInfo
?.supervisionUrlOcppKey
) &&
1309 !isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!))
1311 deleteConfigurationKey(this, this.stationInfo
.supervisionUrlOcppKey
!, { save
: false });
1314 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1315 isNullOrUndefined(getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!))
1317 addConfigurationKey(
1319 this.stationInfo
.amperageLimitationOcppKey
!,
1321 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1326 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
))
1328 addConfigurationKey(
1330 StandardParametersKey
.SupportedFeatureProfiles
,
1331 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1334 addConfigurationKey(
1336 StandardParametersKey
.NumberOfConnectors
,
1337 this.getNumberOfConnectors().toString(),
1339 { overwrite
: true },
1342 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
))
1344 addConfigurationKey(
1346 StandardParametersKey
.MeterValuesSampledData
,
1347 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1351 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
))
1353 const connectorsPhaseRotation
: string[] = [];
1354 if (this.hasEvses
) {
1355 for (const evseStatus
of this.evses
.values()) {
1356 for (const connectorId
of evseStatus
.connectors
.keys()) {
1357 connectorsPhaseRotation
.push(
1358 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1363 for (const connectorId
of this.connectors
.keys()) {
1364 connectorsPhaseRotation
.push(
1365 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1369 addConfigurationKey(
1371 StandardParametersKey
.ConnectorPhaseRotation
,
1372 connectorsPhaseRotation
.toString(),
1376 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
))
1378 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
1381 isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
)) &&
1382 getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)?.value
?.includes(
1383 SupportedFeatureProfiles
.LocalAuthListManagement
,
1386 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false');
1388 if (isNullOrUndefined(getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
))) {
1389 addConfigurationKey(
1391 StandardParametersKey
.ConnectionTimeOut
,
1392 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1395 this.saveOcppConfiguration();
1398 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1399 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1400 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1401 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1403 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1404 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1405 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1406 delete evseStatus
.connectorsStatus
;
1407 this.evses
.set(evseId
, {
1408 ...(evseStatus
as EvseStatus
),
1409 connectors
: new Map
<number, ConnectorStatus
>(
1410 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1417 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1418 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1419 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1420 throw new BaseError(errorMsg
);
1422 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1423 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1424 throw new BaseError(errorMsg
);
1428 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1429 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1430 this.initializeConnectorsFromTemplate(stationTemplate
);
1431 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1432 this.initializeEvsesFromTemplate(stationTemplate
);
1433 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1434 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1435 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1436 throw new BaseError(errorMsg
);
1438 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1439 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1440 throw new BaseError(errorMsg
);
1444 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1445 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1446 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1447 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1448 throw new BaseError(errorMsg
);
1450 if (!stationTemplate
?.Connectors
?.[0]) {
1452 `${this.logPrefix()} Charging station information from template ${
1454 } with no connector id 0 configuration`,
1457 if (stationTemplate
?.Connectors
) {
1458 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1459 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1460 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1462 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1465 const connectorsConfigChanged
=
1466 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1467 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1468 connectorsConfigChanged
&& this.connectors
.clear();
1469 this.connectorsConfigurationHash
= connectorsConfigHash
;
1470 if (templateMaxConnectors
> 0) {
1471 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1473 connectorId
=== 0 &&
1474 (!stationTemplate
?.Connectors
?.[connectorId
] ||
1475 this.getUseConnectorId0(stationTemplate
) === false)
1479 const templateConnectorId
=
1480 connectorId
> 0 && stationTemplate
?.randomConnectors
1481 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1483 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1484 checkStationInfoConnectorStatus(
1485 templateConnectorId
,
1490 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1492 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1493 this.saveConnectorsStatus();
1496 `${this.logPrefix()} Charging station information from template ${
1498 } with no connectors configuration defined, cannot create connectors`,
1504 `${this.logPrefix()} Charging station information from template ${
1506 } with no connectors configuration defined, using already defined connectors`,
1511 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1512 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1513 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1514 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1515 throw new BaseError(errorMsg
);
1517 if (!stationTemplate
?.Evses
?.[0]) {
1519 `${this.logPrefix()} Charging station information from template ${
1521 } with no evse id 0 configuration`,
1524 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1526 `${this.logPrefix()} Charging station information from template ${
1528 } with evse id 0 with no connector id 0 configuration`,
1531 if (Object.keys(stationTemplate
?.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1533 `${this.logPrefix()} Charging station information from template ${
1535 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`,
1538 if (stationTemplate
?.Evses
) {
1539 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1540 .update(JSON
.stringify(stationTemplate
?.Evses
))
1542 const evsesConfigChanged
=
1543 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1544 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1545 evsesConfigChanged
&& this.evses
.clear();
1546 this.evsesConfigurationHash
= evsesConfigHash
;
1547 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1548 if (templateMaxEvses
> 0) {
1549 for (const evseKey
in stationTemplate
.Evses
) {
1550 const evseId
= convertToInt(evseKey
);
1551 this.evses
.set(evseId
, {
1552 connectors
: buildConnectorsMap(
1553 stationTemplate
?.Evses
[evseKey
]?.Connectors
,
1557 availability
: AvailabilityType
.Operative
,
1559 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1561 this.saveEvsesStatus();
1564 `${this.logPrefix()} Charging station information from template ${
1566 } with no evses configuration defined, cannot create evses`,
1572 `${this.logPrefix()} Charging station information from template ${
1574 } with no evses configuration defined, using already defined evses`,
1579 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1580 let configuration
: ChargingStationConfiguration
| undefined;
1581 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1583 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1584 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1585 this.configurationFileHash
,
1588 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1589 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1590 configuration
= JSON
.parse(
1591 readFileSync(this.configurationFile
, 'utf8'),
1592 ) as ChargingStationConfiguration
;
1593 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1594 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1595 this.configurationFileHash
= configuration
.configurationHash
!;
1598 handleFileException(
1599 this.configurationFile
,
1600 FileType
.ChargingStationConfiguration
,
1601 error
as NodeJS
.ErrnoException
,
1606 return configuration
;
1609 private saveAutomaticTransactionGeneratorConfiguration(): void {
1610 if (this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
=== true) {
1611 this.saveConfiguration();
1615 private saveConnectorsStatus() {
1616 this.saveConfiguration();
1619 private saveEvsesStatus() {
1620 this.saveConfiguration();
1623 private saveConfiguration(): void {
1624 if (isNotEmptyString(this.configurationFile
)) {
1626 if (!existsSync(dirname(this.configurationFile
))) {
1627 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1629 let configurationData
: ChargingStationConfiguration
= this.getConfigurationFromFile()
1630 ? cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!)
1632 if (this.stationInfo
?.stationInfoPersistentConfiguration
=== true && this.stationInfo
) {
1633 configurationData
.stationInfo
= this.stationInfo
;
1635 delete configurationData
.stationInfo
;
1638 this.stationInfo
?.ocppPersistentConfiguration
=== true &&
1639 Array.isArray(this.ocppConfiguration
?.configurationKey
)
1641 configurationData
.configurationKey
= this.ocppConfiguration
?.configurationKey
;
1643 delete configurationData
.configurationKey
;
1645 configurationData
= merge
<ChargingStationConfiguration
>(
1647 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1650 !this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
||
1651 !this.getAutomaticTransactionGeneratorConfiguration()
1653 delete configurationData
.automaticTransactionGenerator
;
1655 if (this.connectors
.size
> 0) {
1656 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1658 delete configurationData
.connectorsStatus
;
1660 if (this.evses
.size
> 0) {
1661 configurationData
.evsesStatus
= buildEvsesStatus(this);
1663 delete configurationData
.evsesStatus
;
1665 delete configurationData
.configurationHash
;
1666 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1669 stationInfo
: configurationData
.stationInfo
,
1670 configurationKey
: configurationData
.configurationKey
,
1671 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1672 ...(this.connectors
.size
> 0 && {
1673 connectorsStatus
: configurationData
.connectorsStatus
,
1675 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
}),
1676 } as ChargingStationConfiguration
),
1679 if (this.configurationFileHash
!== configurationHash
) {
1680 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1681 configurationData
.configurationHash
= configurationHash
;
1682 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1683 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1685 this.configurationFile
,
1686 JSON
.stringify(configurationData
, undefined, 2),
1689 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1690 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1691 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1692 this.configurationFileHash
= configurationHash
;
1693 }).catch((error
) => {
1694 handleFileException(
1695 this.configurationFile
,
1696 FileType
.ChargingStationConfiguration
,
1697 error
as NodeJS
.ErrnoException
,
1703 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1704 this.configurationFile
1709 handleFileException(
1710 this.configurationFile
,
1711 FileType
.ChargingStationConfiguration
,
1712 error
as NodeJS
.ErrnoException
,
1718 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1723 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1724 return this.getTemplateFromFile()?.Configuration
;
1727 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1728 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1729 if (this.stationInfo
?.ocppPersistentConfiguration
=== true && Array.isArray(configurationKey
)) {
1730 return { configurationKey
};
1735 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1736 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1737 this.getOcppConfigurationFromFile();
1738 if (!ocppConfiguration
) {
1739 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1741 return ocppConfiguration
;
1744 private async onOpen(): Promise
<void> {
1745 if (this.isWebSocketConnectionOpened() === true) {
1747 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1749 let registrationRetryCount
= 0;
1750 if (this.isRegistered() === false) {
1751 // Send BootNotification
1753 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1754 BootNotificationRequest
,
1755 BootNotificationResponse
1756 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1757 skipBufferingOnError
: true,
1759 if (this.isRegistered() === false) {
1760 this.stationInfo
?.registrationMaxRetries
!== -1 && ++registrationRetryCount
;
1762 this?.bootNotificationResponse
?.interval
1763 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1764 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1768 this.isRegistered() === false &&
1769 (registrationRetryCount
<= this.stationInfo
.registrationMaxRetries
! ||
1770 this.stationInfo
?.registrationMaxRetries
=== -1)
1773 if (this.isRegistered() === true) {
1774 this.emit(ChargingStationEvents
.registered
);
1775 if (this.inAcceptedState() === true) {
1776 this.emit(ChargingStationEvents
.accepted
);
1780 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this
1781 .stationInfo?.registrationMaxRetries})`,
1784 this.autoReconnectRetryCount
= 0;
1785 this.emit(ChargingStationEvents
.updated
);
1788 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1793 private async onClose(code
: WebSocketCloseEventStatusCode
, reason
: Buffer
): Promise
<void> {
1796 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1797 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1799 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1801 )}' and reason '${reason.toString()}'`,
1803 this.autoReconnectRetryCount
= 0;
1808 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1810 )}' and reason '${reason.toString()}'`,
1812 this.started
=== true && (await this.reconnect());
1815 this.emit(ChargingStationEvents
.updated
);
1818 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1819 const cachedRequest
= this.requests
.get(messageId
);
1820 if (Array.isArray(cachedRequest
) === true) {
1821 return cachedRequest
;
1823 throw new OCPPError(
1824 ErrorType
.PROTOCOL_ERROR
,
1825 `Cached request for message id ${messageId} ${getMessageTypeString(
1827 )} is not an array`,
1833 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1834 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1835 if (this.stationInfo
?.enableStatistics
=== true) {
1836 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1839 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1843 // Process the message
1844 await this.ocppIncomingRequestService
.incomingRequestHandler(
1850 this.emit(ChargingStationEvents
.updated
);
1853 private handleResponseMessage(response
: Response
): void {
1854 const [messageType
, messageId
, commandPayload
] = response
;
1855 if (this.requests
.has(messageId
) === false) {
1857 throw new OCPPError(
1858 ErrorType
.INTERNAL_ERROR
,
1859 `Response for unknown message id ${messageId}`,
1865 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1870 `${this.logPrefix()} << Command '${
1871 requestCommandName ?? Constants.UNKNOWN_COMMAND
1872 }' received response payload: ${JSON.stringify(response)}`,
1874 responseCallback(commandPayload
, requestPayload
);
1877 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1878 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1879 if (this.requests
.has(messageId
) === false) {
1881 throw new OCPPError(
1882 ErrorType
.INTERNAL_ERROR
,
1883 `Error response for unknown message id ${messageId}`,
1885 { errorType
, errorMessage
, errorDetails
},
1888 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1890 `${this.logPrefix()} << Command '${
1891 requestCommandName ?? Constants.UNKNOWN_COMMAND
1892 }' received error response payload: ${JSON.stringify(errorResponse)}`,
1894 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1897 private async onMessage(data
: RawData
): Promise
<void> {
1898 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
1899 let messageType
: MessageType
| undefined;
1900 let errorMsg
: string;
1902 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1903 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1904 if (Array.isArray(request
) === true) {
1905 [messageType
] = request
;
1906 // Check the type of message
1907 switch (messageType
) {
1909 case MessageType
.CALL_MESSAGE
:
1910 await this.handleIncomingMessage(request
as IncomingRequest
);
1913 case MessageType
.CALL_RESULT_MESSAGE
:
1914 this.handleResponseMessage(request
as Response
);
1917 case MessageType
.CALL_ERROR_MESSAGE
:
1918 this.handleErrorMessage(request
as ErrorResponse
);
1922 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1923 errorMsg
= `Wrong message type ${messageType}`;
1924 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1925 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1928 throw new OCPPError(
1929 ErrorType
.PROTOCOL_ERROR
,
1930 'Incoming message is not an array',
1938 let commandName
: IncomingRequestCommand
| undefined;
1939 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
1940 let errorCallback
: ErrorCallback
;
1941 const [, messageId
] = request
!;
1942 switch (messageType
) {
1943 case MessageType
.CALL_MESSAGE
:
1944 [, , commandName
] = request
as IncomingRequest
;
1946 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1948 case MessageType
.CALL_RESULT_MESSAGE
:
1949 case MessageType
.CALL_ERROR_MESSAGE
:
1950 if (this.requests
.has(messageId
) === true) {
1951 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1952 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1953 errorCallback(error
as OCPPError
, false);
1955 // Remove the request from the cache in case of error at response handling
1956 this.requests
.delete(messageId
);
1960 if (error
instanceof OCPPError
=== false) {
1962 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1963 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1964 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1965 }' message '${data.toString()}' handling is not an OCPPError:`,
1970 `${this.logPrefix()} Incoming OCPP command '${
1971 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1972 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1973 }' message '${data.toString()}'${
1974 messageType !== MessageType.CALL_MESSAGE
1975 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1977 } processing error:`,
1983 private onPing(): void {
1984 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1987 private onPong(): void {
1988 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1991 private onError(error
: WSError
): void {
1992 this.closeWSConnection();
1993 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1996 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1997 if (this.stationInfo
?.meteringPerTransaction
=== true) {
2000 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
2001 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2006 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
2007 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2011 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
2012 return stationTemplate
?.useConnectorId0
?? true;
2015 private async stopRunningTransactions(reason
?: StopTransactionReason
): Promise
<void> {
2016 if (this.hasEvses
) {
2017 for (const [evseId
, evseStatus
] of this.evses
) {
2021 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2022 if (connectorStatus
.transactionStarted
=== true) {
2023 await this.stopTransactionOnConnector(connectorId
, reason
);
2028 for (const connectorId
of this.connectors
.keys()) {
2029 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2030 await this.stopTransactionOnConnector(connectorId
, reason
);
2037 private getConnectionTimeout(): number {
2038 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
) !== undefined) {
2039 return convertToInt(
2040 getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)!.value
! ??
2041 Constants
.DEFAULT_CONNECTION_TIMEOUT
,
2044 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2047 private getPowerDivider(): number {
2048 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2049 if (this.stationInfo
?.powerSharedByConnectors
=== true) {
2050 powerDivider
= this.getNumberOfRunningTransactions();
2052 return powerDivider
;
2055 private getMaximumAmperage(stationInfo
?: ChargingStationInfo
): number | undefined {
2056 const maximumPower
= (stationInfo
?? this.stationInfo
).maximumPower
!;
2057 switch (this.getCurrentOutType(stationInfo
)) {
2058 case CurrentType
.AC
:
2059 return ACElectricUtils
.amperagePerPhaseFromPower(
2060 this.getNumberOfPhases(stationInfo
),
2061 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2062 this.getVoltageOut(stationInfo
),
2064 case CurrentType
.DC
:
2065 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2069 private getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
2070 return (stationInfo
?? this.stationInfo
).currentOutType
?? CurrentType
.AC
;
2073 private getVoltageOut(stationInfo
?: ChargingStationInfo
): Voltage
{
2075 (stationInfo
?? this.stationInfo
).voltageOut
??
2076 getDefaultVoltageOut(this.getCurrentOutType(stationInfo
), this.logPrefix(), this.templateFile
)
2080 private getAmperageLimitation(): number | undefined {
2082 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2083 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!) !== undefined
2087 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)?.value
,
2088 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2093 private async startMessageSequence(): Promise
<void> {
2094 if (this.stationInfo
?.autoRegister
=== true) {
2095 await this.ocppRequestService
.requestHandler
<
2096 BootNotificationRequest
,
2097 BootNotificationResponse
2098 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2099 skipBufferingOnError
: true,
2102 // Start WebSocket ping
2103 this.startWebSocketPing();
2105 this.startHeartbeat();
2106 // Initialize connectors status
2107 if (this.hasEvses
) {
2108 for (const [evseId
, evseStatus
] of this.evses
) {
2110 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2111 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2112 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
, evseId
);
2117 for (const connectorId
of this.connectors
.keys()) {
2118 if (connectorId
> 0) {
2119 const connectorBootStatus
= getBootConnectorStatus(
2122 this.getConnectorStatus(connectorId
)!,
2124 await sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2128 if (this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
) {
2129 await this.ocppRequestService
.requestHandler
<
2130 FirmwareStatusNotificationRequest
,
2131 FirmwareStatusNotificationResponse
2132 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2133 status: FirmwareStatus
.Installed
,
2135 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2139 if (this.getAutomaticTransactionGeneratorConfiguration().enable
=== true) {
2140 this.startAutomaticTransactionGenerator();
2142 this.flushMessageBuffer();
2145 private async stopMessageSequence(
2146 reason
?: StopTransactionReason
,
2147 stopTransactions
= this.stationInfo
?.stopTransactionsOnStopped
,
2149 // Stop WebSocket ping
2150 this.stopWebSocketPing();
2152 this.stopHeartbeat();
2154 if (this.automaticTransactionGenerator
?.started
=== true) {
2155 this.stopAutomaticTransactionGenerator();
2157 // Stop ongoing transactions
2158 stopTransactions
&& (await this.stopRunningTransactions(reason
));
2159 if (this.hasEvses
) {
2160 for (const [evseId
, evseStatus
] of this.evses
) {
2162 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2163 await this.ocppRequestService
.requestHandler
<
2164 StatusNotificationRequest
,
2165 StatusNotificationResponse
2168 RequestCommand
.STATUS_NOTIFICATION
,
2169 buildStatusNotificationRequest(
2172 ConnectorStatusEnum
.Unavailable
,
2176 delete connectorStatus
?.status;
2181 for (const connectorId
of this.connectors
.keys()) {
2182 if (connectorId
> 0) {
2183 await this.ocppRequestService
.requestHandler
<
2184 StatusNotificationRequest
,
2185 StatusNotificationResponse
2188 RequestCommand
.STATUS_NOTIFICATION
,
2189 buildStatusNotificationRequest(this, connectorId
, ConnectorStatusEnum
.Unavailable
),
2191 delete this.getConnectorStatus(connectorId
)?.status;
2197 private startWebSocketPing(): void {
2198 const webSocketPingInterval
: number =
2199 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
) !== undefined
2201 getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
,
2204 if (webSocketPingInterval
> 0 && this.webSocketPingSetInterval
=== undefined) {
2205 this.webSocketPingSetInterval
= setInterval(() => {
2206 if (this.isWebSocketConnectionOpened() === true) {
2207 this.wsConnection
?.ping();
2209 }, secondsToMilliseconds(webSocketPingInterval
));
2211 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2212 webSocketPingInterval,
2215 } else if (this.webSocketPingSetInterval
!== undefined) {
2217 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2218 webSocketPingInterval,
2223 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2228 private stopWebSocketPing(): void {
2229 if (this.webSocketPingSetInterval
!== undefined) {
2230 clearInterval(this.webSocketPingSetInterval
);
2231 delete this.webSocketPingSetInterval
;
2235 private getConfiguredSupervisionUrl(): URL
{
2236 let configuredSupervisionUrl
: string;
2237 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2238 if (isNotEmptyArray(supervisionUrls
)) {
2239 let configuredSupervisionUrlIndex
: number;
2240 switch (Configuration
.getSupervisionUrlDistribution()) {
2241 case SupervisionUrlDistribution
.RANDOM
:
2242 configuredSupervisionUrlIndex
= Math.floor(
2243 secureRandom() * (supervisionUrls
as string[]).length
,
2246 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2247 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2249 Object.values(SupervisionUrlDistribution
).includes(
2250 Configuration
.getSupervisionUrlDistribution()!,
2253 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2254 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2255 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2258 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2261 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2263 configuredSupervisionUrl
= supervisionUrls
as string;
2265 if (isNotEmptyString(configuredSupervisionUrl
)) {
2266 return new URL(configuredSupervisionUrl
);
2268 const errorMsg
= 'No supervision url(s) configured';
2269 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2270 throw new BaseError(`${errorMsg}`);
2273 private stopHeartbeat(): void {
2274 if (this.heartbeatSetInterval
!== undefined) {
2275 clearInterval(this.heartbeatSetInterval
);
2276 delete this.heartbeatSetInterval
;
2280 private terminateWSConnection(): void {
2281 if (this.isWebSocketConnectionOpened() === true) {
2282 this.wsConnection
?.terminate();
2283 this.wsConnection
= null;
2287 private async reconnect(): Promise
<void> {
2288 // Stop WebSocket ping
2289 this.stopWebSocketPing();
2291 this.stopHeartbeat();
2292 // Stop the ATG if needed
2293 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2294 this.stopAutomaticTransactionGenerator();
2297 this.autoReconnectRetryCount
< this.stationInfo
.autoReconnectMaxRetries
! ||
2298 this.stationInfo
?.autoReconnectMaxRetries
=== -1
2300 ++this.autoReconnectRetryCount
;
2301 const reconnectDelay
=
2302 this.stationInfo
?.reconnectExponentialDelay
=== true
2303 ? exponentialDelay(this.autoReconnectRetryCount
)
2304 : secondsToMilliseconds(this.getConnectionTimeout());
2305 const reconnectDelayWithdraw
= 1000;
2306 const reconnectTimeout
=
2307 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2308 ? reconnectDelay
- reconnectDelayWithdraw
2311 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2314 )}ms, timeout ${reconnectTimeout}ms`,
2316 await sleep(reconnectDelay
);
2318 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2320 this.openWSConnection(
2322 handshakeTimeout
: reconnectTimeout
,
2324 { closeOpened
: true },
2326 } else if (this.stationInfo
?.autoReconnectMaxRetries
!== -1) {
2328 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2329 this.autoReconnectRetryCount
2330 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`,