1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import crypto from
'node:crypto';
4 import fs from
'node:fs';
5 import path from
'node:path';
6 import { URL
} from
'node:url';
7 import { parentPort
} from
'node:worker_threads';
9 import merge from
'just-merge';
10 import WebSocket
, { type RawData
} from
'ws';
12 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator';
13 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel';
14 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
15 import { ChargingStationUtils
} from
'./ChargingStationUtils';
16 import { IdTagsCache
} from
'./IdTagsCache';
18 OCPP16IncomingRequestService
,
20 OCPP16ResponseService
,
22 OCPP20IncomingRequestService
,
24 OCPP20ResponseService
,
25 type OCPPIncomingRequestService
,
26 type OCPPRequestService
,
29 import { OCPPConstants
} from
'./ocpp/OCPPConstants';
30 import { SharedLRUCache
} from
'./SharedLRUCache';
31 import { BaseError
, OCPPError
} from
'../exception';
32 import { PerformanceStatistics
} from
'../performance';
34 type AutomaticTransactionGeneratorConfiguration
,
36 type BootNotificationRequest
,
37 type BootNotificationResponse
,
39 type ChargingStationConfiguration
,
40 type ChargingStationInfo
,
41 type ChargingStationOcppConfiguration
,
42 type ChargingStationTemplate
,
50 type EvseStatusConfiguration
,
53 type FirmwareStatusNotificationRequest
,
54 type FirmwareStatusNotificationResponse
,
56 type HeartbeatRequest
,
57 type HeartbeatResponse
,
59 type IncomingRequestCommand
,
64 type MeterValuesRequest
,
65 type MeterValuesResponse
,
66 OCPP16SupportedFeatureProfiles
,
67 OCPP20ConnectorStatusEnumType
,
71 RegistrationStatusEnumType
,
74 StandardParametersKey
,
75 type StatusNotificationRequest
,
76 type StatusNotificationResponse
,
77 StopTransactionReason
,
78 type StopTransactionRequest
,
79 type StopTransactionResponse
,
80 SupervisionUrlDistribution
,
81 SupportedFeatureProfiles
,
84 WebSocketCloseEventStatusCode
,
87 import type { Reservation
} from
'../types/ocpp/Reservation';
99 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
100 buildConnectorsStatus
,
105 export class ChargingStation
{
106 public readonly index
: number;
107 public readonly templateFile
: string;
108 public stationInfo
!: ChargingStationInfo
;
109 public started
: boolean;
110 public starting
: boolean;
111 public idTagsCache
: IdTagsCache
;
112 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
113 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
114 public wsConnection
!: WebSocket
| null;
115 public readonly connectors
: Map
<number, ConnectorStatus
>;
116 public readonly evses
: Map
<number, EvseStatus
>;
117 public readonly requests
: Map
<string, CachedRequest
>;
118 public performanceStatistics
!: PerformanceStatistics
| undefined;
119 public heartbeatSetInterval
!: NodeJS
.Timeout
;
120 public ocppRequestService
!: OCPPRequestService
;
121 public bootNotificationRequest
!: BootNotificationRequest
;
122 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
123 public powerDivider
!: number;
124 private stopping
: boolean;
125 private configurationFile
!: string;
126 private configurationFileHash
!: string;
127 private connectorsConfigurationHash
!: string;
128 private evsesConfigurationHash
!: string;
129 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
130 private readonly messageBuffer
: Set
<string>;
131 private configuredSupervisionUrl
!: URL
;
132 private wsConnectionRestarted
: boolean;
133 private autoReconnectRetryCount
: number;
134 private templateFileWatcher
!: fs
.FSWatcher
| undefined;
135 private templateFileHash
!: string;
136 private readonly sharedLRUCache
: SharedLRUCache
;
137 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
138 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
139 private reservations
?: Reservation
[];
141 constructor(index
: number, templateFile
: string) {
142 this.started
= false;
143 this.starting
= false;
144 this.stopping
= false;
145 this.wsConnectionRestarted
= false;
146 this.autoReconnectRetryCount
= 0;
148 this.templateFile
= templateFile
;
149 this.connectors
= new Map
<number, ConnectorStatus
>();
150 this.evses
= new Map
<number, EvseStatus
>();
151 this.requests
= new Map
<string, CachedRequest
>();
152 this.messageBuffer
= new Set
<string>();
153 this.sharedLRUCache
= SharedLRUCache
.getInstance();
154 this.idTagsCache
= IdTagsCache
.getInstance();
155 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
160 public get
hasEvses(): boolean {
161 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
164 private get
wsConnectionUrl(): URL
{
167 this.getSupervisionUrlOcppConfiguration() &&
168 Utils.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
169 Utils.isNotEmptyString(
170 ChargingStationConfigurationUtils.getConfigurationKey(
172 this.getSupervisionUrlOcppKey()
175 ? ChargingStationConfigurationUtils.getConfigurationKey(
177 this.getSupervisionUrlOcppKey()
179 : this.configuredSupervisionUrl.href
180 }/${this.stationInfo.chargingStationId}`
184 public logPrefix
= (): string => {
185 return Utils
.logPrefix(
187 (Utils.isNotEmptyString(this?.stationInfo?.chargingStationId)
188 ? this?.stationInfo?.chargingStationId
189 : ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())) ??
190 'Error at building log prefix'
195 public hasIdTags(): boolean {
196 return Utils
.isNotEmptyArray(
197 this.idTagsCache
.getIdTags(ChargingStationUtils
.getIdTagsFile(this.stationInfo
))
201 public getEnableStatistics(): boolean {
202 return this.stationInfo
.enableStatistics
?? false;
205 public getMustAuthorizeAtRemoteStart(): boolean {
206 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
209 public getPayloadSchemaValidation(): boolean {
210 return this.stationInfo
.payloadSchemaValidation
?? true;
213 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
214 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
215 switch (this.getCurrentOutType(stationInfo
)) {
217 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
218 ? localStationInfo
.numberOfPhases
225 public isWebSocketConnectionOpened(): boolean {
226 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
229 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
230 return this?.bootNotificationResponse
?.status;
233 public inUnknownState(): boolean {
234 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
237 public inPendingState(): boolean {
238 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
241 public inAcceptedState(): boolean {
242 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
245 public inRejectedState(): boolean {
246 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
249 public isRegistered(): boolean {
251 this.inUnknownState() === false &&
252 (this.inAcceptedState() === true || this.inPendingState() === true)
256 public isChargingStationAvailable(): boolean {
257 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
260 public hasConnector(connectorId
: number): boolean {
262 for (const evseStatus
of this.evses
.values()) {
263 if (evseStatus
.connectors
.has(connectorId
)) {
269 return this.connectors
.has(connectorId
);
272 public isConnectorAvailable(connectorId
: number): boolean {
275 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
279 public getNumberOfConnectors(): number {
281 if (this.evses
.size
=== 0) {
282 throw new BaseError('Evses not initialized, cannot get number of connectors');
284 let numberOfConnectors
= 0;
285 for (const [evseId
, evseStatus
] of this.evses
) {
287 numberOfConnectors
+= evseStatus
.connectors
.size
;
290 return numberOfConnectors
;
292 if (this.connectors
.size
=== 0) {
293 throw new BaseError('Connectors not initialized, cannot get number of connectors');
295 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
298 public getNumberOfEvses(): number {
299 if (this.evses
.size
=== 0) {
300 throw new BaseError('Evses not initialized, cannot get number of evses');
302 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
305 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
307 for (const evseStatus
of this.evses
.values()) {
308 if (evseStatus
.connectors
.has(connectorId
)) {
309 return evseStatus
.connectors
.get(connectorId
);
314 return this.connectors
.get(connectorId
);
317 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
318 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
321 public getOcppStrictCompliance(): boolean {
322 return this.stationInfo
?.ocppStrictCompliance
?? false;
325 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
326 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
327 this.getCurrentOutType(stationInfo
),
331 return (stationInfo
?? this.stationInfo
).voltageOut
?? defaultVoltageOut
;
334 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
335 const localStationInfo
= stationInfo
?? this.stationInfo
;
336 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
339 public getConnectorMaximumAvailablePower(connectorId
: number): number {
340 let connectorAmperageLimitationPowerLimit
: number;
342 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
343 this.getAmperageLimitation() < this.stationInfo
?.maximumAmperage
345 connectorAmperageLimitationPowerLimit
=
346 (this.getCurrentOutType() === CurrentType
.AC
347 ? ACElectricUtils
.powerTotal(
348 this.getNumberOfPhases(),
349 this.getVoltageOut(),
350 this.getAmperageLimitation() *
351 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors())
353 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
356 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
357 const connectorChargingProfilesPowerLimit
=
358 ChargingStationUtils
.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
360 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
361 isNaN(connectorAmperageLimitationPowerLimit
)
363 : connectorAmperageLimitationPowerLimit
,
364 isNaN(connectorChargingProfilesPowerLimit
) ? Infinity : connectorChargingProfilesPowerLimit
368 public getTransactionIdTag(transactionId
: number): string | undefined {
370 for (const evseStatus
of this.evses
.values()) {
371 for (const connectorStatus
of evseStatus
.connectors
.values()) {
372 if (connectorStatus
.transactionId
=== transactionId
) {
373 return connectorStatus
.transactionIdTag
;
378 for (const connectorId
of this.connectors
.keys()) {
381 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
383 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
389 public getNumberOfRunningTransactions(): number {
392 for (const evseStatus
of this.evses
.values()) {
393 for (const connectorStatus
of evseStatus
.connectors
.values()) {
394 if (connectorStatus
.transactionStarted
=== true) {
400 for (const connectorId
of this.connectors
.keys()) {
401 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
409 public getOutOfOrderEndMeterValues(): boolean {
410 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
413 public getBeginEndMeterValues(): boolean {
414 return this.stationInfo
?.beginEndMeterValues
?? false;
417 public getMeteringPerTransaction(): boolean {
418 return this.stationInfo
?.meteringPerTransaction
?? true;
421 public getTransactionDataMeterValues(): boolean {
422 return this.stationInfo
?.transactionDataMeterValues
?? false;
425 public getMainVoltageMeterValues(): boolean {
426 return this.stationInfo
?.mainVoltageMeterValues
?? true;
429 public getPhaseLineToLineVoltageMeterValues(): boolean {
430 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
433 public getCustomValueLimitationMeterValues(): boolean {
434 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
437 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
439 for (const evseStatus
of this.evses
.values()) {
440 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
441 if (connectorStatus
.transactionId
=== transactionId
) {
447 for (const connectorId
of this.connectors
.keys()) {
450 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
458 public getEnergyActiveImportRegisterByTransactionId(
459 transactionId
: number,
462 return this.getEnergyActiveImportRegister(
463 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
468 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
469 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
);
472 public getAuthorizeRemoteTxRequests(): boolean {
473 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
475 StandardParametersKey
.AuthorizeRemoteTxRequests
477 return authorizeRemoteTxRequests
478 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
482 public getLocalAuthListEnabled(): boolean {
483 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
485 StandardParametersKey
.LocalAuthListEnabled
487 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
490 public getHeartbeatInterval(): number {
491 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
493 StandardParametersKey
.HeartbeatInterval
495 if (HeartbeatInterval
) {
496 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
498 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
500 StandardParametersKey
.HeartBeatInterval
502 if (HeartBeatInterval
) {
503 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
505 this.stationInfo
?.autoRegister
=== false &&
507 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
508 Constants.DEFAULT_HEARTBEAT_INTERVAL
511 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
514 public setSupervisionUrl(url
: string): void {
516 this.getSupervisionUrlOcppConfiguration() &&
517 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey())
519 ChargingStationConfigurationUtils
.setConfigurationKeyValue(
521 this.getSupervisionUrlOcppKey(),
525 this.stationInfo
.supervisionUrls
= url
;
526 this.saveStationInfo();
527 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
531 public startHeartbeat(): void {
532 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
533 this.heartbeatSetInterval
= setInterval(() => {
534 this.ocppRequestService
535 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
538 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
542 }, this.getHeartbeatInterval());
544 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
545 this.getHeartbeatInterval()
548 } else if (this.heartbeatSetInterval
) {
550 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
551 this.getHeartbeatInterval()
556 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`
561 public restartHeartbeat(): void {
563 this.stopHeartbeat();
565 this.startHeartbeat();
568 public restartWebSocketPing(): void {
569 // Stop WebSocket ping
570 this.stopWebSocketPing();
571 // Start WebSocket ping
572 this.startWebSocketPing();
575 public startMeterValues(connectorId
: number, interval
: number): void {
576 if (connectorId
=== 0) {
578 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId.toString()}`
582 if (!this.getConnectorStatus(connectorId
)) {
584 `${this.logPrefix()} Trying to start MeterValues on non existing connector id ${connectorId.toString()}`
588 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
590 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`
594 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
595 Utils
.isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
598 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`
603 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(() => {
604 // FIXME: Implement OCPP version agnostic helpers
605 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
608 this.getConnectorStatus(connectorId
).transactionId
,
611 this.ocppRequestService
612 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
614 RequestCommand
.METER_VALUES
,
617 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
618 meterValue
: [meterValue
],
623 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
630 `${this.logPrefix()} Charging station ${
631 StandardParametersKey.MeterValueSampleInterval
632 } configuration set to ${interval}, not sending MeterValues`
637 public stopMeterValues(connectorId
: number) {
638 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
639 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
643 public start(): void {
644 if (this.started
=== false) {
645 if (this.starting
=== false) {
646 this.starting
= true;
647 if (this.getEnableStatistics() === true) {
648 this.performanceStatistics
?.start();
650 this.openWSConnection();
651 // Monitor charging station template file
652 this.templateFileWatcher
= FileUtils
.watchJsonFile(
654 FileType
.ChargingStationTemplate
,
657 (event
, filename
): void => {
658 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
661 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
663 } file have changed, reload`
665 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
669 this.stopAutomaticTransactionGenerator();
670 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
671 this.startAutomaticTransactionGenerator();
673 if (this.getEnableStatistics() === true) {
674 this.performanceStatistics
?.restart();
676 this.performanceStatistics
?.stop();
678 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
681 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
689 parentPort
?.postMessage(MessageChannelUtils
.buildStartedMessage(this));
690 this.starting
= false;
692 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
695 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
699 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
700 if (this.started
=== true) {
701 if (this.stopping
=== false) {
702 this.stopping
= true;
703 await this.stopMessageSequence(reason
);
704 this.closeWSConnection();
705 if (this.getEnableStatistics() === true) {
706 this.performanceStatistics
?.stop();
708 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
709 this.templateFileWatcher
?.close();
710 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
711 delete this.bootNotificationResponse
;
712 this.started
= false;
713 this.saveConfiguration();
714 parentPort
?.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
715 this.stopping
= false;
717 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
720 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
724 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
725 await this.stop(reason
);
726 await Utils
.sleep(this.stationInfo
.resetTime
);
731 public saveOcppConfiguration(): void {
732 if (this.getOcppPersistentConfiguration()) {
733 this.saveConfiguration();
737 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
738 return ChargingStationConfigurationUtils
.getConfigurationKey(
740 StandardParametersKey
.SupportedFeatureProfiles
741 )?.value
?.includes(featureProfile
);
744 public bufferMessage(message
: string): void {
745 this.messageBuffer
.add(message
);
748 public openWSConnection(
749 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
750 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
752 terminateOpened
: false,
755 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
756 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
757 if (this.started
=== false && this.starting
=== false) {
759 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
764 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
765 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
767 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
769 if (params
?.closeOpened
) {
770 this.closeWSConnection();
772 if (params
?.terminateOpened
) {
773 this.terminateWSConnection();
776 if (this.isWebSocketConnectionOpened() === true) {
778 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
784 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
787 this.wsConnection
= new WebSocket(
788 this.wsConnectionUrl
,
789 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
793 // Handle WebSocket message
794 this.wsConnection
.on(
796 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
798 // Handle WebSocket error
799 this.wsConnection
.on(
801 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
803 // Handle WebSocket close
804 this.wsConnection
.on(
806 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
808 // Handle WebSocket open
809 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
810 // Handle WebSocket ping
811 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
812 // Handle WebSocket pong
813 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
816 public closeWSConnection(): void {
817 if (this.isWebSocketConnectionOpened() === true) {
818 this.wsConnection
?.close();
819 this.wsConnection
= null;
823 public getAutomaticTransactionGeneratorConfiguration():
824 | AutomaticTransactionGeneratorConfiguration
826 const automaticTransactionGeneratorConfigurationFromFile
=
827 this.getConfigurationFromFile()?.automaticTransactionGenerator
;
828 if (automaticTransactionGeneratorConfigurationFromFile
) {
829 return automaticTransactionGeneratorConfigurationFromFile
;
831 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
834 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
835 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
836 if (Utils
.isNotEmptyArray(connectorIds
)) {
837 for (const connectorId
of connectorIds
) {
838 this.automaticTransactionGenerator
?.startConnector(connectorId
);
841 this.automaticTransactionGenerator
?.start();
843 this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
844 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
847 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
848 if (Utils
.isNotEmptyArray(connectorIds
)) {
849 for (const connectorId
of connectorIds
) {
850 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
853 this.automaticTransactionGenerator
?.stop();
855 this.saveChargingStationAutomaticTransactionGeneratorConfiguration();
856 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
859 public async stopTransactionOnConnector(
861 reason
= StopTransactionReason
.NONE
862 ): Promise
<StopTransactionResponse
> {
863 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
865 this.getBeginEndMeterValues() === true &&
866 this.getOcppStrictCompliance() === true &&
867 this.getOutOfOrderEndMeterValues() === false
869 // FIXME: Implement OCPP version agnostic helpers
870 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
873 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
875 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
877 RequestCommand
.METER_VALUES
,
881 meterValue
: [transactionEndMeterValue
],
885 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
887 RequestCommand
.STOP_TRANSACTION
,
890 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
896 public supportsReservations(): boolean {
897 logger
.info(`${this.logPrefix()} Check for reservation support in charging station`);
898 return ChargingStationConfigurationUtils
.getConfigurationKey(
900 StandardParametersKey
.SupportedFeatureProfiles
901 ).value
.includes(OCPP16SupportedFeatureProfiles
.Reservation
);
904 public supportsReservationsOnConnectorId0(): boolean {
906 `Check for reservation support on connector 0 in charging station (CS): ${this.logPrefix()}`
909 this.supportsReservations() &&
910 ChargingStationConfigurationUtils
.getConfigurationKey(
912 OCPPConstants
.OCPP_RESERVE_CONNECTOR_ZERO_SUPPORTED
917 public addReservation(newReservation
: Reservation
): void {
918 if (Utils
.isNullOrUndefined(this.reservations
)) {
919 this.reservations
= [];
921 const [exists
, foundReservation
] = this.doesReservationExist(newReservation
.reservationId
);
923 this.replaceExistingReservation(foundReservation
, newReservation
);
925 this.reservations
.push(newReservation
);
929 public removeReservation(existingReservationId
: number): void {
930 const index
= this.reservations
.findIndex((res
) => res
.reservationId
=== existingReservationId
);
931 this.reservations
.splice(index
, 1);
934 public getReservation(reservationId
: number, reservationIndex
?: number): Reservation
{
935 if (!Utils
.isNullOrUndefined(reservationIndex
)) {
936 return this.reservations
[reservationIndex
];
938 return this.reservations
.find((r
) => r
.reservationId
=== reservationId
);
941 public doesReservationExist(
942 reservationId
: number,
943 reservation
?: Reservation
944 ): [boolean, Reservation
] {
945 const foundReservation
= this.reservations
.find(
946 (r
) => r
.reservationId
=== reservationId
|| r
.reservationId
=== reservation
.reservationId
948 return Utils
.isUndefined(foundReservation
) ? [false, null] : [true, foundReservation
];
951 public getReservationByConnectorId(connectorId
: number): Reservation
{
952 return this.reservations
.find((r
) => r
.connectorId
=== connectorId
);
955 public getAvailableConnector(): Map
<number, ConnectorStatus
> {
956 for (const connectorId
in this.connectors
) {
957 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
958 const connector
= this.connectors
[Utils
.convertToInt(connectorId
)];
960 this.isConnectorAvailable(Utils
.convertToInt(connectorId
)) &&
961 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
962 connector
.status === OCPP20ConnectorStatusEnumType
.Available
964 // eslint-disable-next-line @typescript-eslint/no-unsafe-return
970 private flushMessageBuffer(): void {
971 if (this.messageBuffer
.size
> 0) {
972 for (const message
of this.messageBuffer
.values()) {
974 let commandName
: RequestCommand
;
975 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
976 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
978 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
979 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
981 this.wsConnection
?.send(message
);
982 isRequest
&& PerformanceStatistics
.endMeasure(commandName
, beginId
);
984 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
986 )} payload sent: ${message}`
988 this.messageBuffer
.delete(message
);
993 private getSupervisionUrlOcppConfiguration(): boolean {
994 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
997 private replaceExistingReservation(
998 existingReservation
: Reservation
,
999 newReservation
: Reservation
1001 const existingReservationIndex
= this.reservations
.findIndex(
1002 (r
) => r
.reservationId
=== existingReservation
.reservationId
1004 this.reservations
.splice(existingReservationIndex
, 1, newReservation
);
1007 private getSupervisionUrlOcppKey(): string {
1008 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
1011 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1012 let template
: ChargingStationTemplate
;
1014 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1015 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1017 const measureId
= `${FileType.ChargingStationTemplate} read`;
1018 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1019 template
= JSON
.parse(
1020 fs
.readFileSync(this.templateFile
, 'utf8')
1021 ) as ChargingStationTemplate
;
1022 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1023 template
.templateHash
= crypto
1024 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1025 .update(JSON
.stringify(template
))
1027 this.sharedLRUCache
.setChargingStationTemplate(template
);
1028 this.templateFileHash
= template
.templateHash
;
1031 ErrorUtils
.handleFileException(
1033 FileType
.ChargingStationTemplate
,
1034 error
as NodeJS
.ErrnoException
,
1041 private getStationInfoFromTemplate(): ChargingStationInfo
{
1042 const stationTemplate
: ChargingStationTemplate
| undefined = this.getTemplateFromFile();
1043 ChargingStationUtils
.checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1044 ChargingStationUtils
.warnTemplateKeysDeprecation(
1049 if (stationTemplate
?.Connectors
) {
1050 ChargingStationUtils
.checkConnectorsConfiguration(
1056 const stationInfo
: ChargingStationInfo
=
1057 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
1058 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
1059 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
1063 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1064 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
1065 if (Utils
.isNotEmptyArray(stationTemplate
?.power
)) {
1066 stationTemplate
.power
= stationTemplate
.power
as number[];
1067 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
1068 stationInfo
.maximumPower
=
1069 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1070 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1071 : stationTemplate
.power
[powerArrayRandomIndex
];
1073 stationTemplate
.power
= stationTemplate
?.power
as number;
1074 stationInfo
.maximumPower
=
1075 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1076 ? stationTemplate
.power
* 1000
1077 : stationTemplate
.power
;
1079 stationInfo
.firmwareVersionPattern
=
1080 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1082 Utils
.isNotEmptyString(stationInfo
.firmwareVersion
) &&
1083 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
1086 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1088 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
1091 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1098 stationTemplate
?.firmwareUpgrade
?? {}
1100 stationInfo
.resetTime
= !Utils
.isNullOrUndefined(stationTemplate
?.resetTime
)
1101 ? stationTemplate
.resetTime
* 1000
1102 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1103 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1107 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
1108 let stationInfo
: ChargingStationInfo
| undefined;
1109 if (this.getStationInfoPersistentConfiguration()) {
1110 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1112 delete stationInfo
?.infoHash
;
1118 private getStationInfo(): ChargingStationInfo
{
1119 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1120 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
1122 // 1. charging station info from template
1123 // 2. charging station info from configuration file
1124 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1125 return stationInfoFromFile
;
1127 stationInfoFromFile
&&
1128 ChargingStationUtils
.propagateSerialNumber(
1129 this.getTemplateFromFile(),
1130 stationInfoFromFile
,
1131 stationInfoFromTemplate
1133 return stationInfoFromTemplate
;
1136 private saveStationInfo(): void {
1137 if (this.getStationInfoPersistentConfiguration()) {
1138 this.saveConfiguration();
1142 private getOcppPersistentConfiguration(): boolean {
1143 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1146 private getStationInfoPersistentConfiguration(): boolean {
1147 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1150 private handleUnsupportedVersion(version
: OCPPVersion
) {
1151 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1152 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1153 throw new BaseError(errorMsg
);
1156 private initialize(): void {
1157 const stationTemplate
= this.getTemplateFromFile();
1158 ChargingStationUtils
.checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1159 this.configurationFile
= path
.join(
1160 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
1161 `${ChargingStationUtils.getHashId(this.index, stationTemplate)}.json`
1163 const chargingStationConfiguration
= this.getConfigurationFromFile();
1164 const featureFlag
= false;
1167 (chargingStationConfiguration
?.connectorsStatus
|| chargingStationConfiguration
?.evsesStatus
)
1169 this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration
);
1171 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1173 this.stationInfo
= this.getStationInfo();
1175 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1176 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1177 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1179 const patternGroup
: number | undefined =
1180 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1181 this.stationInfo
.firmwareVersion
?.split('.').length
;
1182 const match
= this.stationInfo
?.firmwareVersion
1183 ?.match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1184 ?.slice(1, patternGroup
+ 1);
1185 const patchLevelIndex
= match
.length
- 1;
1186 match
[patchLevelIndex
] = (
1187 Utils
.convertToInt(match
[patchLevelIndex
]) +
1188 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
1190 this.stationInfo
.firmwareVersion
= match
?.join('.');
1192 this.saveStationInfo();
1193 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1194 if (this.getEnableStatistics() === true) {
1195 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1196 this.stationInfo
.hashId
,
1197 this.stationInfo
.chargingStationId
,
1198 this.configuredSupervisionUrl
1201 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
1204 this.powerDivider
= this.getPowerDivider();
1205 // OCPP configuration
1206 this.ocppConfiguration
= this.getOcppConfiguration();
1207 this.initializeOcppConfiguration();
1208 this.initializeOcppServices();
1209 if (this.stationInfo
?.autoRegister
=== true) {
1210 this.bootNotificationResponse
= {
1211 currentTime
: new Date(),
1212 interval
: this.getHeartbeatInterval() / 1000,
1213 status: RegistrationStatusEnumType
.ACCEPTED
,
1218 private initializeOcppServices(): void {
1219 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1220 switch (ocppVersion
) {
1221 case OCPPVersion
.VERSION_16
:
1222 this.ocppIncomingRequestService
=
1223 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1224 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1225 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1228 case OCPPVersion
.VERSION_20
:
1229 case OCPPVersion
.VERSION_201
:
1230 this.ocppIncomingRequestService
=
1231 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1232 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1233 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1237 this.handleUnsupportedVersion(ocppVersion
);
1242 private initializeOcppConfiguration(): void {
1244 !ChargingStationConfigurationUtils
.getConfigurationKey(
1246 StandardParametersKey
.HeartbeatInterval
1249 ChargingStationConfigurationUtils
.addConfigurationKey(
1251 StandardParametersKey
.HeartbeatInterval
,
1256 !ChargingStationConfigurationUtils
.getConfigurationKey(
1258 StandardParametersKey
.HeartBeatInterval
1261 ChargingStationConfigurationUtils
.addConfigurationKey(
1263 StandardParametersKey
.HeartBeatInterval
,
1269 this.getSupervisionUrlOcppConfiguration() &&
1270 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1271 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1273 ChargingStationConfigurationUtils
.addConfigurationKey(
1275 this.getSupervisionUrlOcppKey(),
1276 this.configuredSupervisionUrl
.href
,
1280 !this.getSupervisionUrlOcppConfiguration() &&
1281 Utils
.isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1282 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1284 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1286 this.getSupervisionUrlOcppKey(),
1291 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1292 !ChargingStationConfigurationUtils
.getConfigurationKey(
1294 this.stationInfo
.amperageLimitationOcppKey
1297 ChargingStationConfigurationUtils
.addConfigurationKey(
1299 this.stationInfo
.amperageLimitationOcppKey
,
1301 this.stationInfo
.maximumAmperage
*
1302 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1307 !ChargingStationConfigurationUtils
.getConfigurationKey(
1309 StandardParametersKey
.SupportedFeatureProfiles
1312 ChargingStationConfigurationUtils
.addConfigurationKey(
1314 StandardParametersKey
.SupportedFeatureProfiles
,
1315 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1318 ChargingStationConfigurationUtils
.addConfigurationKey(
1320 StandardParametersKey
.NumberOfConnectors
,
1321 this.getNumberOfConnectors().toString(),
1326 !ChargingStationConfigurationUtils
.getConfigurationKey(
1328 StandardParametersKey
.MeterValuesSampledData
1331 ChargingStationConfigurationUtils
.addConfigurationKey(
1333 StandardParametersKey
.MeterValuesSampledData
,
1334 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1338 !ChargingStationConfigurationUtils
.getConfigurationKey(
1340 StandardParametersKey
.ConnectorPhaseRotation
1343 const connectorsPhaseRotation
: string[] = [];
1344 if (this.hasEvses
) {
1345 for (const evseStatus
of this.evses
.values()) {
1346 for (const connectorId
of evseStatus
.connectors
.keys()) {
1347 connectorsPhaseRotation
.push(
1348 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1353 for (const connectorId
of this.connectors
.keys()) {
1354 connectorsPhaseRotation
.push(
1355 ChargingStationUtils
.getPhaseRotationValue(connectorId
, this.getNumberOfPhases())
1359 ChargingStationConfigurationUtils
.addConfigurationKey(
1361 StandardParametersKey
.ConnectorPhaseRotation
,
1362 connectorsPhaseRotation
.toString()
1366 !ChargingStationConfigurationUtils
.getConfigurationKey(
1368 StandardParametersKey
.AuthorizeRemoteTxRequests
1371 ChargingStationConfigurationUtils
.addConfigurationKey(
1373 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1378 !ChargingStationConfigurationUtils
.getConfigurationKey(
1380 StandardParametersKey
.LocalAuthListEnabled
1382 ChargingStationConfigurationUtils
.getConfigurationKey(
1384 StandardParametersKey
.SupportedFeatureProfiles
1385 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1387 ChargingStationConfigurationUtils
.addConfigurationKey(
1389 StandardParametersKey
.LocalAuthListEnabled
,
1394 !ChargingStationConfigurationUtils
.getConfigurationKey(
1396 StandardParametersKey
.ConnectionTimeOut
1399 ChargingStationConfigurationUtils
.addConfigurationKey(
1401 StandardParametersKey
.ConnectionTimeOut
,
1402 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1405 this.saveOcppConfiguration();
1408 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1409 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1410 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1411 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1413 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1414 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1415 const evseStatus
= Utils
.cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1416 delete evseStatus
.connectorsStatus
;
1417 this.evses
.set(evseId
, {
1418 ...(evseStatus
as EvseStatus
),
1419 connectors
: new Map
<number, ConnectorStatus
>(
1420 evseStatusConfiguration
.connectorsStatus
.map((connectorStatus
, connectorId
) => [
1427 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1428 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1429 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1430 throw new BaseError(errorMsg
);
1432 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1433 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1434 throw new BaseError(errorMsg
);
1438 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1439 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1440 this.initializeConnectorsFromTemplate(stationTemplate
);
1441 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1442 this.initializeEvsesFromTemplate(stationTemplate
);
1443 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1444 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1445 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1446 throw new BaseError(errorMsg
);
1448 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1449 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1450 throw new BaseError(errorMsg
);
1454 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1455 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1456 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1457 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1458 throw new BaseError(errorMsg
);
1460 if (!stationTemplate
?.Connectors
[0]) {
1462 `${this.logPrefix()} Charging station information from template ${
1464 } with no connector id 0 configuration`
1467 if (stationTemplate
?.Connectors
) {
1468 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1469 ChargingStationUtils
.checkConnectorsConfiguration(
1474 const connectorsConfigHash
= crypto
1475 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1477 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`
1480 const connectorsConfigChanged
=
1481 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1482 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1483 connectorsConfigChanged
&& this.connectors
.clear();
1484 this.connectorsConfigurationHash
= connectorsConfigHash
;
1485 if (templateMaxConnectors
> 0) {
1486 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1488 connectorId
=== 0 &&
1489 (!stationTemplate
?.Connectors
[connectorId
] ||
1490 this.getUseConnectorId0(stationTemplate
) === false)
1494 const templateConnectorId
=
1495 connectorId
> 0 && stationTemplate
?.randomConnectors
1496 ? Utils
.getRandomInteger(templateMaxAvailableConnectors
, 1)
1498 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1499 ChargingStationUtils
.checkStationInfoConnectorStatus(
1500 templateConnectorId
,
1505 this.connectors
.set(connectorId
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1507 ChargingStationUtils
.initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1508 this.saveConnectorsStatus();
1511 `${this.logPrefix()} Charging station information from template ${
1513 } with no connectors configuration defined, cannot create connectors`
1519 `${this.logPrefix()} Charging station information from template ${
1521 } with no connectors configuration defined, using already defined connectors`
1526 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1527 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1528 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1529 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1530 throw new BaseError(errorMsg
);
1532 if (!stationTemplate
?.Evses
[0]) {
1534 `${this.logPrefix()} Charging station information from template ${
1536 } with no evse id 0 configuration`
1539 if (!stationTemplate
?.Evses
[0]?.Connectors
[0]) {
1541 `${this.logPrefix()} Charging station information from template ${
1543 } with evse id 0 with no connector id 0 configuration`
1546 if (stationTemplate
?.Evses
) {
1547 const evsesConfigHash
= crypto
1548 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1549 .update(`${JSON.stringify(stationTemplate?.Evses)}`)
1551 const evsesConfigChanged
=
1552 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1553 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1554 evsesConfigChanged
&& this.evses
.clear();
1555 this.evsesConfigurationHash
= evsesConfigHash
;
1556 const templateMaxEvses
= ChargingStationUtils
.getMaxNumberOfEvses(stationTemplate
?.Evses
);
1557 if (templateMaxEvses
> 0) {
1558 for (const evse
in stationTemplate
.Evses
) {
1559 const evseId
= Utils
.convertToInt(evse
);
1560 this.evses
.set(evseId
, {
1561 connectors
: ChargingStationUtils
.buildConnectorsMap(
1562 stationTemplate
?.Evses
[evse
]?.Connectors
,
1566 availability
: AvailabilityType
.Operative
,
1568 ChargingStationUtils
.initializeConnectorsMapStatus(
1569 this.evses
.get(evseId
)?.connectors
,
1573 this.saveEvsesStatus();
1576 `${this.logPrefix()} Charging station information from template ${
1578 } with no evses configuration defined, cannot create evses`
1584 `${this.logPrefix()} Charging station information from template ${
1586 } with no evses configuration defined, using already defined evses`
1591 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1592 let configuration
: ChargingStationConfiguration
| undefined;
1593 if (Utils
.isNotEmptyString(this.configurationFile
) && fs
.existsSync(this.configurationFile
)) {
1595 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1596 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1597 this.configurationFileHash
1600 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1601 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1602 configuration
= JSON
.parse(
1603 fs
.readFileSync(this.configurationFile
, 'utf8')
1604 ) as ChargingStationConfiguration
;
1605 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1606 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1607 this.configurationFileHash
= configuration
.configurationHash
;
1610 ErrorUtils
.handleFileException(
1611 this.configurationFile
,
1612 FileType
.ChargingStationConfiguration
,
1613 error
as NodeJS
.ErrnoException
,
1618 return configuration
;
1621 private saveChargingStationAutomaticTransactionGeneratorConfiguration(): void {
1622 this.saveConfiguration();
1625 private saveConnectorsStatus() {
1626 this.saveConfiguration();
1629 private saveEvsesStatus() {
1630 this.saveConfiguration();
1633 private saveConfiguration(): void {
1634 if (Utils
.isNotEmptyString(this.configurationFile
)) {
1636 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1637 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1639 let configurationData
: ChargingStationConfiguration
=
1640 Utils
.cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()) ?? {};
1641 if (this.getStationInfoPersistentConfiguration() && this.stationInfo
) {
1642 configurationData
.stationInfo
= this.stationInfo
;
1644 if (this.getOcppPersistentConfiguration() && this.ocppConfiguration
?.configurationKey
) {
1645 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1647 configurationData
= merge
<ChargingStationConfiguration
>(
1649 buildChargingStationAutomaticTransactionGeneratorConfiguration(this)
1651 if (this.connectors
.size
> 0) {
1652 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1654 if (this.evses
.size
> 0) {
1655 configurationData
.evsesStatus
= buildEvsesStatus(this);
1657 delete configurationData
.configurationHash
;
1658 const configurationHash
= crypto
1659 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1660 .update(JSON
.stringify(configurationData
))
1662 if (this.configurationFileHash
!== configurationHash
) {
1663 AsyncLock
.acquire(AsyncLockType
.configuration
)
1665 configurationData
.configurationHash
= configurationHash
;
1666 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1667 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1668 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1669 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1670 fs
.closeSync(fileDescriptor
);
1671 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1672 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1673 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1674 this.configurationFileHash
= configurationHash
;
1677 ErrorUtils
.handleFileException(
1678 this.configurationFile
,
1679 FileType
.ChargingStationConfiguration
,
1680 error
as NodeJS
.ErrnoException
,
1685 AsyncLock
.release(AsyncLockType
.configuration
).catch(Constants
.EMPTY_FUNCTION
);
1689 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1690 this.configurationFile
1695 ErrorUtils
.handleFileException(
1696 this.configurationFile
,
1697 FileType
.ChargingStationConfiguration
,
1698 error
as NodeJS
.ErrnoException
,
1704 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1709 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1710 return this.getTemplateFromFile()?.Configuration
;
1713 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1714 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1715 if (this.getOcppPersistentConfiguration() === true && configurationKey
) {
1716 return { configurationKey
};
1721 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1722 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1723 this.getOcppConfigurationFromFile();
1724 if (!ocppConfiguration
) {
1725 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1727 return ocppConfiguration
;
1730 private async onOpen(): Promise
<void> {
1731 if (this.isWebSocketConnectionOpened() === true) {
1733 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1735 if (this.isRegistered() === false) {
1736 // Send BootNotification
1737 let registrationRetryCount
= 0;
1739 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1740 BootNotificationRequest
,
1741 BootNotificationResponse
1742 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1743 skipBufferingOnError
: true,
1745 if (this.isRegistered() === false) {
1746 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1748 this?.bootNotificationResponse
?.interval
1749 ? this.bootNotificationResponse
.interval
* 1000
1750 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
1754 this.isRegistered() === false &&
1755 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1756 this.getRegistrationMaxRetries() === -1)
1759 if (this.isRegistered() === true) {
1760 if (this.inAcceptedState() === true) {
1761 await this.startMessageSequence();
1765 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1768 this.wsConnectionRestarted
= false;
1769 this.autoReconnectRetryCount
= 0;
1770 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1773 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1778 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1781 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1782 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1784 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1786 )}' and reason '${reason.toString()}'`
1788 this.autoReconnectRetryCount
= 0;
1793 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1795 )}' and reason '${reason.toString()}'`
1797 this.started
=== true && (await this.reconnect());
1800 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1803 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1804 const cachedRequest
= this.requests
.get(messageId
);
1805 if (Array.isArray(cachedRequest
) === true) {
1806 return cachedRequest
;
1808 throw new OCPPError(
1809 ErrorType
.PROTOCOL_ERROR
,
1810 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1812 )} is not an array`,
1814 cachedRequest
as JsonType
1818 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1819 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1820 if (this.getEnableStatistics() === true) {
1821 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1824 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1828 // Process the message
1829 await this.ocppIncomingRequestService
.incomingRequestHandler(
1837 private handleResponseMessage(response
: Response
): void {
1838 const [messageType
, messageId
, commandPayload
] = response
;
1839 if (this.requests
.has(messageId
) === false) {
1841 throw new OCPPError(
1842 ErrorType
.INTERNAL_ERROR
,
1843 `Response for unknown message id ${messageId}`,
1849 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1854 `${this.logPrefix()} << Command '${
1855 requestCommandName ?? Constants.UNKNOWN_COMMAND
1856 }' received response payload: ${JSON.stringify(response)}`
1858 responseCallback(commandPayload
, requestPayload
);
1861 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1862 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1863 if (this.requests
.has(messageId
) === false) {
1865 throw new OCPPError(
1866 ErrorType
.INTERNAL_ERROR
,
1867 `Error response for unknown message id ${messageId}`,
1869 { errorType
, errorMessage
, errorDetails
}
1872 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1874 `${this.logPrefix()} << Command '${
1875 requestCommandName ?? Constants.UNKNOWN_COMMAND
1876 }' received error response payload: ${JSON.stringify(errorResponse)}`
1878 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1881 private async onMessage(data
: RawData
): Promise
<void> {
1882 let request
: IncomingRequest
| Response
| ErrorResponse
;
1883 let messageType
: number;
1884 let errorMsg
: string;
1886 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1887 if (Array.isArray(request
) === true) {
1888 [messageType
] = request
;
1889 // Check the type of message
1890 switch (messageType
) {
1892 case MessageType
.CALL_MESSAGE
:
1893 await this.handleIncomingMessage(request
as IncomingRequest
);
1896 case MessageType
.CALL_RESULT_MESSAGE
:
1897 this.handleResponseMessage(request
as Response
);
1900 case MessageType
.CALL_ERROR_MESSAGE
:
1901 this.handleErrorMessage(request
as ErrorResponse
);
1905 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1906 errorMsg
= `Wrong message type ${messageType}`;
1907 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1908 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1910 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1912 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1917 let commandName
: IncomingRequestCommand
;
1918 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1919 let errorCallback
: ErrorCallback
;
1920 const [, messageId
] = request
;
1921 switch (messageType
) {
1922 case MessageType
.CALL_MESSAGE
:
1923 [, , commandName
] = request
as IncomingRequest
;
1925 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1927 case MessageType
.CALL_RESULT_MESSAGE
:
1928 case MessageType
.CALL_ERROR_MESSAGE
:
1929 if (this.requests
.has(messageId
) === true) {
1930 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
);
1931 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1932 errorCallback(error
as OCPPError
, false);
1934 // Remove the request from the cache in case of error at response handling
1935 this.requests
.delete(messageId
);
1939 if (error
instanceof OCPPError
=== false) {
1941 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1942 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1943 }' message '${data.toString()}' handling is not an OCPPError:`,
1948 `${this.logPrefix()} Incoming OCPP command '${
1949 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1950 }' message '${data.toString()}'${
1951 messageType !== MessageType.CALL_MESSAGE
1952 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1954 } processing error:`,
1960 private onPing(): void {
1961 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1964 private onPong(): void {
1965 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1968 private onError(error
: WSError
): void {
1969 this.closeWSConnection();
1970 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1973 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1974 if (this.getMeteringPerTransaction() === true) {
1977 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1978 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1983 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1984 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1988 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
1989 return stationTemplate
?.useConnectorId0
?? true;
1992 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1993 if (this.hasEvses
) {
1994 for (const evseStatus
of this.evses
.values()) {
1995 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
1996 if (connectorStatus
.transactionStarted
=== true) {
1997 await this.stopTransactionOnConnector(connectorId
, reason
);
2002 for (const connectorId
of this.connectors
.keys()) {
2003 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2004 await this.stopTransactionOnConnector(connectorId
, reason
);
2011 private getConnectionTimeout(): number {
2013 ChargingStationConfigurationUtils
.getConfigurationKey(
2015 StandardParametersKey
.ConnectionTimeOut
2020 ChargingStationConfigurationUtils
.getConfigurationKey(
2022 StandardParametersKey
.ConnectionTimeOut
2024 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
2027 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2030 // -1 for unlimited, 0 for disabling
2031 private getAutoReconnectMaxRetries(): number | undefined {
2033 this.stationInfo
.autoReconnectMaxRetries
?? Configuration
.getAutoReconnectMaxRetries() ?? -1
2038 private getRegistrationMaxRetries(): number | undefined {
2039 return this.stationInfo
.registrationMaxRetries
?? -1;
2042 private getPowerDivider(): number {
2043 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2044 if (this.stationInfo
?.powerSharedByConnectors
) {
2045 powerDivider
= this.getNumberOfRunningTransactions();
2047 return powerDivider
;
2050 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
2051 const maximumPower
= this.getMaximumPower(stationInfo
);
2052 switch (this.getCurrentOutType(stationInfo
)) {
2053 case CurrentType
.AC
:
2054 return ACElectricUtils
.amperagePerPhaseFromPower(
2055 this.getNumberOfPhases(stationInfo
),
2056 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2057 this.getVoltageOut(stationInfo
)
2059 case CurrentType
.DC
:
2060 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2064 private getAmperageLimitation(): number | undefined {
2066 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2067 ChargingStationConfigurationUtils
.getConfigurationKey(
2069 this.stationInfo
.amperageLimitationOcppKey
2074 ChargingStationConfigurationUtils
.getConfigurationKey(
2076 this.stationInfo
.amperageLimitationOcppKey
2078 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
2083 private async startMessageSequence(): Promise
<void> {
2084 if (this.stationInfo
?.autoRegister
=== true) {
2085 await this.ocppRequestService
.requestHandler
<
2086 BootNotificationRequest
,
2087 BootNotificationResponse
2088 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2089 skipBufferingOnError
: true,
2092 // Start WebSocket ping
2093 this.startWebSocketPing();
2095 this.startHeartbeat();
2096 // Initialize connectors status
2097 if (this.hasEvses
) {
2098 for (const [evseId
, evseStatus
] of this.evses
) {
2100 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2101 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2106 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2109 connectorBootStatus
,
2116 for (const connectorId
of this.connectors
.keys()) {
2117 if (connectorId
> 0) {
2118 const connectorBootStatus
= ChargingStationUtils
.getBootConnectorStatus(
2121 this.getConnectorStatus(connectorId
)
2123 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2127 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2128 await this.ocppRequestService
.requestHandler
<
2129 FirmwareStatusNotificationRequest
,
2130 FirmwareStatusNotificationResponse
2131 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2132 status: FirmwareStatus
.Installed
,
2134 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2138 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2139 this.startAutomaticTransactionGenerator();
2141 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2144 private async stopMessageSequence(
2145 reason
: StopTransactionReason
= StopTransactionReason
.NONE
2147 // Stop WebSocket ping
2148 this.stopWebSocketPing();
2150 this.stopHeartbeat();
2151 // Stop ongoing transactions
2152 if (this.automaticTransactionGenerator
?.started
=== true) {
2153 this.stopAutomaticTransactionGenerator();
2155 await this.stopRunningTransactions(reason
);
2157 if (this.hasEvses
) {
2158 for (const [evseId
, evseStatus
] of this.evses
) {
2160 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2161 await this.ocppRequestService
.requestHandler
<
2162 StatusNotificationRequest
,
2163 StatusNotificationResponse
2166 RequestCommand
.STATUS_NOTIFICATION
,
2167 OCPPServiceUtils
.buildStatusNotificationRequest(
2170 ConnectorStatusEnum
.Unavailable
,
2174 delete connectorStatus
?.status;
2179 for (const connectorId
of this.connectors
.keys()) {
2180 if (connectorId
> 0) {
2181 await this.ocppRequestService
.requestHandler
<
2182 StatusNotificationRequest
,
2183 StatusNotificationResponse
2186 RequestCommand
.STATUS_NOTIFICATION
,
2187 OCPPServiceUtils
.buildStatusNotificationRequest(
2190 ConnectorStatusEnum
.Unavailable
2193 delete this.getConnectorStatus(connectorId
)?.status;
2199 private startWebSocketPing(): void {
2200 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
2202 StandardParametersKey
.WebSocketPingInterval
2204 ? Utils
.convertToInt(
2205 ChargingStationConfigurationUtils
.getConfigurationKey(
2207 StandardParametersKey
.WebSocketPingInterval
2211 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2212 this.webSocketPingSetInterval
= setInterval(() => {
2213 if (this.isWebSocketConnectionOpened() === true) {
2214 this.wsConnection
?.ping();
2216 }, webSocketPingInterval
* 1000);
2218 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
2219 webSocketPingInterval
2222 } else if (this.webSocketPingSetInterval
) {
2224 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
2225 webSocketPingInterval
2230 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`
2235 private stopWebSocketPing(): void {
2236 if (this.webSocketPingSetInterval
) {
2237 clearInterval(this.webSocketPingSetInterval
);
2238 delete this.webSocketPingSetInterval
;
2242 private getConfiguredSupervisionUrl(): URL
{
2243 let configuredSupervisionUrl
: string;
2244 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2245 if (Utils
.isNotEmptyArray(supervisionUrls
)) {
2246 let configuredSupervisionUrlIndex
: number;
2247 switch (Configuration
.getSupervisionUrlDistribution()) {
2248 case SupervisionUrlDistribution
.RANDOM
:
2249 configuredSupervisionUrlIndex
= Math.floor(Utils
.secureRandom() * supervisionUrls
.length
);
2251 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2252 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2254 Object.values(SupervisionUrlDistribution
).includes(
2255 Configuration
.getSupervisionUrlDistribution()
2258 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2259 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2262 configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
2265 configuredSupervisionUrl
= supervisionUrls
[configuredSupervisionUrlIndex
];
2267 configuredSupervisionUrl
= supervisionUrls
as string;
2269 if (Utils
.isNotEmptyString(configuredSupervisionUrl
)) {
2270 return new URL(configuredSupervisionUrl
);
2272 const errorMsg
= 'No supervision url(s) configured';
2273 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2274 throw new BaseError(`${errorMsg}`);
2277 private stopHeartbeat(): void {
2278 if (this.heartbeatSetInterval
) {
2279 clearInterval(this.heartbeatSetInterval
);
2280 delete this.heartbeatSetInterval
;
2284 private terminateWSConnection(): void {
2285 if (this.isWebSocketConnectionOpened() === true) {
2286 this.wsConnection
?.terminate();
2287 this.wsConnection
= null;
2291 private getReconnectExponentialDelay(): boolean {
2292 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2295 private async reconnect(): Promise
<void> {
2296 // Stop WebSocket ping
2297 this.stopWebSocketPing();
2299 this.stopHeartbeat();
2300 // Stop the ATG if needed
2301 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2302 this.stopAutomaticTransactionGenerator();
2305 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2306 this.getAutoReconnectMaxRetries() === -1
2308 this.autoReconnectRetryCount
++;
2309 const reconnectDelay
= this.getReconnectExponentialDelay()
2310 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2311 : this.getConnectionTimeout() * 1000;
2312 const reconnectDelayWithdraw
= 1000;
2313 const reconnectTimeout
=
2314 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2315 ? reconnectDelay
- reconnectDelayWithdraw
2318 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2321 )}ms, timeout ${reconnectTimeout}ms`
2323 await Utils
.sleep(reconnectDelay
);
2325 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2327 this.openWSConnection(
2329 ...(this.stationInfo
?.wsOptions
?? {}),
2330 handshakeTimeout
: reconnectTimeout
,
2332 { closeOpened
: true }
2334 this.wsConnectionRestarted
= true;
2335 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2337 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2338 this.autoReconnectRetryCount
2339 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`