1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { createHash
} from
'node:crypto';
4 import { EventEmitter
} from
'node:events';
5 import { type FSWatcher
, existsSync
, mkdirSync
, readFileSync
, writeFileSync
} from
'node:fs';
6 import { dirname
, join
} from
'node:path';
7 import { URL
} from
'node:url';
8 import { parentPort
} from
'node:worker_threads';
10 import { millisecondsToSeconds
, secondsToMilliseconds
} from
'date-fns';
11 import merge from
'just-merge';
12 import { type RawData
, WebSocket
} from
'ws';
14 import { AutomaticTransactionGenerator
} from
'./AutomaticTransactionGenerator';
15 import { ChargingStationWorkerBroadcastChannel
} from
'./broadcast-channel/ChargingStationWorkerBroadcastChannel';
18 deleteConfigurationKey
,
20 setConfigurationKeyValue
,
21 } from
'./ConfigurationKeyUtils';
25 checkConnectorsConfiguration
,
26 checkStationInfoConnectorStatus
,
28 createBootNotificationRequest
,
30 getAmperageLimitationUnitDivider
,
31 getBootConnectorStatus
,
32 getChargingStationConnectorChargingProfilesPowerLimit
,
38 getNumberOfReservableConnectors
,
39 getPhaseRotationValue
,
41 hasReservationExpired
,
42 initializeConnectorsMapStatus
,
43 propagateSerialNumber
,
44 removeExpiredReservations
,
45 stationTemplateToStationInfo
,
46 warnTemplateKeysDeprecation
,
48 import { IdTagsCache
} from
'./IdTagsCache';
50 OCPP16IncomingRequestService
,
52 OCPP16ResponseService
,
54 OCPP20IncomingRequestService
,
56 OCPP20ResponseService
,
57 type OCPPIncomingRequestService
,
58 type OCPPRequestService
,
61 import { SharedLRUCache
} from
'./SharedLRUCache';
62 import { BaseError
, OCPPError
} from
'../exception';
63 import { PerformanceStatistics
} from
'../performance';
65 type AutomaticTransactionGeneratorConfiguration
,
67 type BootNotificationRequest
,
68 type BootNotificationResponse
,
70 type ChargingStationConfiguration
,
71 ChargingStationEvents
,
72 type ChargingStationInfo
,
73 type ChargingStationOcppConfiguration
,
74 type ChargingStationTemplate
,
82 type EvseStatusConfiguration
,
85 type FirmwareStatusNotificationRequest
,
86 type FirmwareStatusNotificationResponse
,
88 type HeartbeatRequest
,
89 type HeartbeatResponse
,
91 type IncomingRequestCommand
,
96 type MeterValuesRequest
,
97 type MeterValuesResponse
,
101 RegistrationStatusEnumType
,
105 ReservationTerminationReason
,
107 StandardParametersKey
,
109 type StatusNotificationRequest
,
110 type StatusNotificationResponse
,
111 StopTransactionReason
,
112 type StopTransactionRequest
,
113 type StopTransactionResponse
,
114 SupervisionUrlDistribution
,
115 SupportedFeatureProfiles
,
118 WebSocketCloseEventStatusCode
,
128 buildChargingStationAutomaticTransactionGeneratorConfiguration
,
129 buildConnectorsStatus
,
138 formatDurationMilliSeconds
,
139 formatDurationSeconds
,
141 getWebSocketCloseEventStatusString
,
157 export class ChargingStation
extends EventEmitter
{
158 public readonly index
: number;
159 public readonly templateFile
: string;
160 public stationInfo
!: ChargingStationInfo
;
161 public started
: boolean;
162 public starting
: boolean;
163 public idTagsCache
: IdTagsCache
;
164 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
165 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
166 public wsConnection
: WebSocket
| null;
167 public readonly connectors
: Map
<number, ConnectorStatus
>;
168 public readonly evses
: Map
<number, EvseStatus
>;
169 public readonly requests
: Map
<string, CachedRequest
>;
170 public performanceStatistics
!: PerformanceStatistics
| undefined;
171 public heartbeatSetInterval
?: NodeJS
.Timeout
;
172 public ocppRequestService
!: OCPPRequestService
;
173 public bootNotificationRequest
!: BootNotificationRequest
;
174 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
175 public powerDivider
!: number;
176 private stopping
: boolean;
177 private configurationFile
!: string;
178 private configurationFileHash
!: string;
179 private connectorsConfigurationHash
!: string;
180 private evsesConfigurationHash
!: string;
181 private automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
;
182 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
183 private readonly messageBuffer
: Set
<string>;
184 private configuredSupervisionUrl
!: URL
;
185 private wsConnectionRestarted
: boolean;
186 private autoReconnectRetryCount
: number;
187 private templateFileWatcher
!: FSWatcher
| undefined;
188 private templateFileHash
!: string;
189 private readonly sharedLRUCache
: SharedLRUCache
;
190 private webSocketPingSetInterval
?: NodeJS
.Timeout
;
191 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
192 private reservationExpirationSetInterval
?: NodeJS
.Timeout
;
194 constructor(index
: number, templateFile
: string) {
196 this.started
= false;
197 this.starting
= false;
198 this.stopping
= false;
199 this.wsConnection
= null;
200 this.wsConnectionRestarted
= false;
201 this.autoReconnectRetryCount
= 0;
203 this.templateFile
= templateFile
;
204 this.connectors
= new Map
<number, ConnectorStatus
>();
205 this.evses
= new Map
<number, EvseStatus
>();
206 this.requests
= new Map
<string, CachedRequest
>();
207 this.messageBuffer
= new Set
<string>();
208 this.sharedLRUCache
= SharedLRUCache
.getInstance();
209 this.idTagsCache
= IdTagsCache
.getInstance();
210 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
212 this.on(ChargingStationEvents
.started
, () => {
213 parentPort
?.postMessage(buildStartedMessage(this));
215 this.on(ChargingStationEvents
.stopped
, () => {
216 parentPort
?.postMessage(buildStoppedMessage(this));
218 this.on(ChargingStationEvents
.updated
, () => {
219 parentPort
?.postMessage(buildUpdatedMessage(this));
225 public get
hasEvses(): boolean {
226 return this.connectors
.size
=== 0 && this.evses
.size
> 0;
229 private get
wsConnectionUrl(): URL
{
232 this.getSupervisionUrlOcppConfiguration() &&
233 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
234 isNotEmptyString(getConfigurationKey(this, this.getSupervisionUrlOcppKey())?.value)
235 ? getConfigurationKey(this, this.getSupervisionUrlOcppKey())!.value
236 : this.configuredSupervisionUrl.href
237 }/${this.stationInfo.chargingStationId}`,
241 public logPrefix
= (): string => {
244 (isNotEmptyString(this?.stationInfo?.chargingStationId)
245 ? this?.stationInfo?.chargingStationId
246 : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
247 'Error at building log prefix'
252 public hasIdTags(): boolean {
253 return isNotEmptyArray(this.idTagsCache
.getIdTags(getIdTagsFile(this.stationInfo
)!));
256 public getEnableStatistics(): boolean {
257 return this.stationInfo
.enableStatistics
?? false;
260 public getRemoteAuthorization(): boolean {
261 return this.stationInfo
.remoteAuthorization
?? true;
264 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number {
265 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
266 switch (this.getCurrentOutType(stationInfo
)) {
268 return !isUndefined(localStationInfo
.numberOfPhases
) ? localStationInfo
.numberOfPhases
! : 3;
274 public isWebSocketConnectionOpened(): boolean {
275 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
278 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
279 return this?.bootNotificationResponse
?.status;
282 public inUnknownState(): boolean {
283 return isNullOrUndefined(this?.bootNotificationResponse
?.status);
286 public inPendingState(): boolean {
287 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
290 public inAcceptedState(): boolean {
291 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
294 public inRejectedState(): boolean {
295 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
298 public isRegistered(): boolean {
300 this.inUnknownState() === false &&
301 (this.inAcceptedState() === true || this.inPendingState() === true)
305 public isChargingStationAvailable(): boolean {
306 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.Operative
;
309 public hasConnector(connectorId
: number): boolean {
311 for (const evseStatus
of this.evses
.values()) {
312 if (evseStatus
.connectors
.has(connectorId
)) {
318 return this.connectors
.has(connectorId
);
321 public isConnectorAvailable(connectorId
: number): boolean {
324 this.getConnectorStatus(connectorId
)?.availability
=== AvailabilityType
.Operative
328 public getNumberOfConnectors(): number {
330 let numberOfConnectors
= 0;
331 for (const [evseId
, evseStatus
] of this.evses
) {
333 numberOfConnectors
+= evseStatus
.connectors
.size
;
336 return numberOfConnectors
;
338 return this.connectors
.has(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
341 public getNumberOfEvses(): number {
342 return this.evses
.has(0) ? this.evses
.size
- 1 : this.evses
.size
;
345 public getConnectorStatus(connectorId
: number): ConnectorStatus
| undefined {
347 for (const evseStatus
of this.evses
.values()) {
348 if (evseStatus
.connectors
.has(connectorId
)) {
349 return evseStatus
.connectors
.get(connectorId
);
354 return this.connectors
.get(connectorId
);
357 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
358 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
361 public getOcppStrictCompliance(): boolean {
362 return this.stationInfo
?.ocppStrictCompliance
?? true;
365 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number {
366 const defaultVoltageOut
= getDefaultVoltageOut(
367 this.getCurrentOutType(stationInfo
),
371 return (stationInfo
?? this.stationInfo
).voltageOut
?? defaultVoltageOut
;
374 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
375 return (stationInfo
?? this.stationInfo
).maximumPower
!;
378 public getConnectorMaximumAvailablePower(connectorId
: number): number {
379 let connectorAmperageLimitationPowerLimit
: number | undefined;
381 !isNullOrUndefined(this.getAmperageLimitation()) &&
382 this.getAmperageLimitation()! < this.stationInfo
.maximumAmperage
!
384 connectorAmperageLimitationPowerLimit
=
385 (this.getCurrentOutType() === CurrentType
.AC
386 ? ACElectricUtils
.powerTotal(
387 this.getNumberOfPhases(),
388 this.getVoltageOut(),
389 this.getAmperageLimitation()! *
390 (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
392 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation()!)) /
395 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
396 const connectorChargingProfilesPowerLimit
=
397 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
399 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
400 isNaN(connectorAmperageLimitationPowerLimit
!)
402 : connectorAmperageLimitationPowerLimit
!,
403 isNaN(connectorChargingProfilesPowerLimit
!) ? Infinity : connectorChargingProfilesPowerLimit
!,
407 public getTransactionIdTag(transactionId
: number): string | undefined {
409 for (const evseStatus
of this.evses
.values()) {
410 for (const connectorStatus
of evseStatus
.connectors
.values()) {
411 if (connectorStatus
.transactionId
=== transactionId
) {
412 return connectorStatus
.transactionIdTag
;
417 for (const connectorId
of this.connectors
.keys()) {
418 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
419 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
425 public getNumberOfRunningTransactions(): number {
426 let numberOfRunningTransactions
= 0;
428 for (const [evseId
, evseStatus
] of this.evses
) {
432 for (const connectorStatus
of evseStatus
.connectors
.values()) {
433 if (connectorStatus
.transactionStarted
=== true) {
434 ++numberOfRunningTransactions
;
439 for (const connectorId
of this.connectors
.keys()) {
440 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
441 ++numberOfRunningTransactions
;
445 return numberOfRunningTransactions
;
448 public getOutOfOrderEndMeterValues(): boolean {
449 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
452 public getBeginEndMeterValues(): boolean {
453 return this.stationInfo
?.beginEndMeterValues
?? false;
456 public getMeteringPerTransaction(): boolean {
457 return this.stationInfo
?.meteringPerTransaction
?? true;
460 public getTransactionDataMeterValues(): boolean {
461 return this.stationInfo
?.transactionDataMeterValues
?? false;
464 public getMainVoltageMeterValues(): boolean {
465 return this.stationInfo
?.mainVoltageMeterValues
?? true;
468 public getPhaseLineToLineVoltageMeterValues(): boolean {
469 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
472 public getCustomValueLimitationMeterValues(): boolean {
473 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
476 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
478 for (const evseStatus
of this.evses
.values()) {
479 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
480 if (connectorStatus
.transactionId
=== transactionId
) {
486 for (const connectorId
of this.connectors
.keys()) {
487 if (this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
) {
494 public getEnergyActiveImportRegisterByTransactionId(
495 transactionId
: number,
498 return this.getEnergyActiveImportRegister(
499 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)!)!,
504 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
505 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
)!, rounded
);
508 public getAuthorizeRemoteTxRequests(): boolean {
509 const authorizeRemoteTxRequests
= getConfigurationKey(
511 StandardParametersKey
.AuthorizeRemoteTxRequests
,
513 return authorizeRemoteTxRequests
? convertToBoolean(authorizeRemoteTxRequests
.value
) : false;
516 public getLocalAuthListEnabled(): boolean {
517 const localAuthListEnabled
= getConfigurationKey(
519 StandardParametersKey
.LocalAuthListEnabled
,
521 return localAuthListEnabled
? convertToBoolean(localAuthListEnabled
.value
) : false;
524 public getHeartbeatInterval(): number {
525 const HeartbeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
);
526 if (HeartbeatInterval
) {
527 return secondsToMilliseconds(convertToInt(HeartbeatInterval
.value
));
529 const HeartBeatInterval
= getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
);
530 if (HeartBeatInterval
) {
531 return secondsToMilliseconds(convertToInt(HeartBeatInterval
.value
));
533 this.stationInfo
?.autoRegister
=== false &&
535 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
536 Constants.DEFAULT_HEARTBEAT_INTERVAL
539 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
542 public setSupervisionUrl(url
: string): void {
544 this.getSupervisionUrlOcppConfiguration() &&
545 isNotEmptyString(this.getSupervisionUrlOcppKey())
547 setConfigurationKeyValue(this, this.getSupervisionUrlOcppKey(), url
);
549 this.stationInfo
.supervisionUrls
= url
;
550 this.saveStationInfo();
551 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
555 public startHeartbeat(): void {
556 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval
) {
557 this.heartbeatSetInterval
= setInterval(() => {
558 this.ocppRequestService
559 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
562 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
566 }, this.getHeartbeatInterval());
568 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
569 this.getHeartbeatInterval(),
572 } else if (this.heartbeatSetInterval
) {
574 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
575 this.getHeartbeatInterval(),
580 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`,
585 public restartHeartbeat(): void {
587 this.stopHeartbeat();
589 this.startHeartbeat();
592 public restartWebSocketPing(): void {
593 // Stop WebSocket ping
594 this.stopWebSocketPing();
595 // Start WebSocket ping
596 this.startWebSocketPing();
599 public startMeterValues(connectorId
: number, interval
: number): void {
600 if (connectorId
=== 0) {
602 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
606 if (!this.getConnectorStatus(connectorId
)) {
608 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
613 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
615 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`,
619 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
620 isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
623 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`,
628 this.getConnectorStatus(connectorId
)!.transactionSetInterval
= setInterval(() => {
629 // FIXME: Implement OCPP version agnostic helpers
630 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
633 this.getConnectorStatus(connectorId
)!.transactionId
!,
636 this.ocppRequestService
637 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
639 RequestCommand
.METER_VALUES
,
642 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
643 meterValue
: [meterValue
],
648 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
655 `${this.logPrefix()} Charging station ${
656 StandardParametersKey.MeterValueSampleInterval
657 } configuration set to ${interval}, not sending MeterValues`,
662 public stopMeterValues(connectorId
: number) {
663 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
664 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
668 public start(): void {
669 if (this.started
=== false) {
670 if (this.starting
=== false) {
671 this.starting
= true;
672 if (this.getEnableStatistics() === true) {
673 this.performanceStatistics
?.start();
675 if (hasFeatureProfile(this, SupportedFeatureProfiles
.Reservation
)) {
676 this.startReservationExpirationSetInterval();
678 this.openWSConnection();
679 // Monitor charging station template file
680 // FIXME: Disabled until the spurious configuration file change detection is identified
681 // this.templateFileWatcher = watchJsonFile(
682 // this.templateFile,
683 // FileType.ChargingStationTemplate,
686 // (event, filename): void => {
687 // if (isNotEmptyString(filename) && event === 'change') {
690 // `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
692 // } file have changed, reload`,
694 // this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash);
696 // this.initialize();
697 // this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!);
698 // // Restart the ATG
699 // this.stopAutomaticTransactionGenerator()
701 // delete this.automaticTransactionGeneratorConfiguration;
702 // if (this.getAutomaticTransactionGeneratorConfiguration()?.enable === true) {
703 // this.startAutomaticTransactionGenerator();
708 // `${this.logPrefix()} failed to stop ATG at ${
709 // FileType.ChargingStationTemplate
714 // if (this.getEnableStatistics() === true) {
715 // this.performanceStatistics?.restart();
717 // this.performanceStatistics?.stop();
719 // // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
722 // `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
730 this.emit(ChargingStationEvents
.started
);
731 this.starting
= false;
733 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
736 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
740 public async stop(reason
?: StopTransactionReason
, stopTransactions
?: boolean): Promise
<void> {
741 if (this.started
=== true) {
742 if (this.stopping
=== false) {
743 this.stopping
= true;
744 await this.stopMessageSequence(reason
, stopTransactions
);
745 this.closeWSConnection();
746 if (this.getEnableStatistics() === true) {
747 this.performanceStatistics
?.stop();
749 if (hasFeatureProfile(this, SupportedFeatureProfiles
.Reservation
)) {
750 this.stopReservationExpirationSetInterval();
752 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
753 this.templateFileWatcher
?.close();
754 this.sharedLRUCache
.deleteChargingStationTemplate(this.templateFileHash
);
755 delete this.bootNotificationResponse
;
756 this.started
= false;
757 this.saveConfiguration();
758 this.emit(ChargingStationEvents
.stopped
);
759 this.stopping
= false;
761 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
764 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
768 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
769 await this.stop(reason
);
770 await sleep(this.stationInfo
.resetTime
!);
775 public saveOcppConfiguration(): void {
776 if (this.getOcppPersistentConfiguration()) {
777 this.saveConfiguration();
781 public bufferMessage(message
: string): void {
782 this.messageBuffer
.add(message
);
785 public openWSConnection(
787 params
?: { closeOpened
?: boolean; terminateOpened
?: boolean },
790 handshakeTimeout
: secondsToMilliseconds(this.getConnectionTimeout()),
791 ...this.stationInfo
?.wsOptions
,
794 params
= { ...{ closeOpened
: false, terminateOpened
: false }, ...params
};
795 if (!checkChargingStation(this, this.logPrefix())) {
799 !isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
800 !isNullOrUndefined(this.stationInfo
.supervisionPassword
)
802 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
804 if (params
?.closeOpened
) {
805 this.closeWSConnection();
807 if (params
?.terminateOpened
) {
808 this.terminateWSConnection();
811 if (this.isWebSocketConnectionOpened() === true) {
813 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`,
819 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
822 this.wsConnection
= new WebSocket(
823 this.wsConnectionUrl
,
824 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
828 // Handle WebSocket message
829 this.wsConnection
.on(
831 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void,
833 // Handle WebSocket error
834 this.wsConnection
.on(
836 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void,
838 // Handle WebSocket close
839 this.wsConnection
.on(
841 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void,
843 // Handle WebSocket open
844 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
845 // Handle WebSocket ping
846 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
847 // Handle WebSocket pong
848 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
851 public closeWSConnection(): void {
852 if (this.isWebSocketConnectionOpened() === true) {
853 this.wsConnection
?.close();
854 this.wsConnection
= null;
858 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration
{
859 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration
)) {
860 let automaticTransactionGeneratorConfiguration
:
861 | AutomaticTransactionGeneratorConfiguration
863 const automaticTransactionGeneratorConfigurationFromFile
=
864 this.getConfigurationFromFile()?.automaticTransactionGenerator
;
866 this.getAutomaticTransactionGeneratorPersistentConfiguration() &&
867 automaticTransactionGeneratorConfigurationFromFile
869 automaticTransactionGeneratorConfiguration
=
870 automaticTransactionGeneratorConfigurationFromFile
;
872 automaticTransactionGeneratorConfiguration
=
873 this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
875 this.automaticTransactionGeneratorConfiguration
= {
876 ...Constants
.DEFAULT_ATG_CONFIGURATION
,
877 ...automaticTransactionGeneratorConfiguration
,
880 return this.automaticTransactionGeneratorConfiguration
!;
883 public getAutomaticTransactionGeneratorStatuses(): Status
[] | undefined {
884 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses
;
887 public startAutomaticTransactionGenerator(connectorIds
?: number[]): void {
888 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
889 if (isNotEmptyArray(connectorIds
)) {
890 for (const connectorId
of connectorIds
!) {
891 this.automaticTransactionGenerator
?.startConnector(connectorId
);
894 this.automaticTransactionGenerator
?.start();
896 this.saveAutomaticTransactionGeneratorConfiguration();
897 this.emit(ChargingStationEvents
.updated
);
900 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
901 if (isNotEmptyArray(connectorIds
)) {
902 for (const connectorId
of connectorIds
!) {
903 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
906 this.automaticTransactionGenerator
?.stop();
908 this.saveAutomaticTransactionGeneratorConfiguration();
909 this.emit(ChargingStationEvents
.updated
);
912 public async stopTransactionOnConnector(
914 reason
?: StopTransactionReason
,
915 ): Promise
<StopTransactionResponse
> {
916 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
918 this.getBeginEndMeterValues() === true &&
919 this.getOcppStrictCompliance() === true &&
920 this.getOutOfOrderEndMeterValues() === false
922 // FIXME: Implement OCPP version agnostic helpers
923 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
926 this.getEnergyActiveImportRegisterByTransactionId(transactionId
!),
928 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
930 RequestCommand
.METER_VALUES
,
934 meterValue
: [transactionEndMeterValue
],
938 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
940 RequestCommand
.STOP_TRANSACTION
,
943 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
!, true),
944 ...(isNullOrUndefined(reason
) && { reason
}),
949 public getReserveConnectorZeroSupported(): boolean {
950 return convertToBoolean(
951 getConfigurationKey(this, StandardParametersKey
.ReserveConnectorZeroSupported
)!.value
,
955 public async addReservation(reservation
: Reservation
): Promise
<void> {
956 const reservationFound
= this.getReservationBy('reservationId', reservation
.reservationId
);
957 if (!isUndefined(reservationFound
)) {
958 await this.removeReservation(
960 ReservationTerminationReason
.REPLACE_EXISTING
,
963 this.getConnectorStatus(reservation
.connectorId
)!.reservation
= reservation
;
964 await OCPPServiceUtils
.sendAndSetConnectorStatus(
966 reservation
.connectorId
,
967 ConnectorStatusEnum
.Reserved
,
969 { send
: reservation
.connectorId
!== 0 },
973 public async removeReservation(
974 reservation
: Reservation
,
975 reason
: ReservationTerminationReason
,
977 const connector
= this.getConnectorStatus(reservation
.connectorId
)!;
979 case ReservationTerminationReason
.CONNECTOR_STATE_CHANGED
:
980 case ReservationTerminationReason
.TRANSACTION_STARTED
:
981 delete connector
.reservation
;
983 case ReservationTerminationReason
.RESERVATION_CANCELED
:
984 case ReservationTerminationReason
.REPLACE_EXISTING
:
985 case ReservationTerminationReason
.EXPIRED
:
986 await OCPPServiceUtils
.sendAndSetConnectorStatus(
988 reservation
.connectorId
,
989 ConnectorStatusEnum
.Available
,
991 { send
: reservation
.connectorId
!== 0 },
993 delete connector
.reservation
;
996 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
997 throw new BaseError(`Unknown reservation termination reason '${reason}'`);
1001 public getReservationBy(
1002 filterKey
: ReservationKey
,
1003 value
: number | string,
1004 ): Reservation
| undefined {
1005 if (this.hasEvses
) {
1006 for (const evseStatus
of this.evses
.values()) {
1007 for (const connectorStatus
of evseStatus
.connectors
.values()) {
1008 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
1009 return connectorStatus
.reservation
;
1014 for (const connectorStatus
of this.connectors
.values()) {
1015 if (connectorStatus
?.reservation
?.[filterKey
] === value
) {
1016 return connectorStatus
.reservation
;
1022 public isConnectorReservable(
1023 reservationId
: number,
1025 connectorId
?: number,
1027 const reservation
= this.getReservationBy('reservationId', reservationId
);
1028 const reservationExists
= !isUndefined(reservation
) && !hasReservationExpired(reservation
!);
1029 if (arguments.length
=== 1) {
1030 return !reservationExists
;
1031 } else if (arguments.length
> 1) {
1032 const userReservation
= !isUndefined(idTag
)
1033 ? this.getReservationBy('idTag', idTag
!)
1035 const userReservationExists
=
1036 !isUndefined(userReservation
) && !hasReservationExpired(userReservation
!);
1037 const notConnectorZero
= isUndefined(connectorId
) ? true : connectorId
! > 0;
1038 const freeConnectorsAvailable
= this.getNumberOfReservableConnectors() > 0;
1040 !reservationExists
&& !userReservationExists
&& notConnectorZero
&& freeConnectorsAvailable
1046 private startReservationExpirationSetInterval(customInterval
?: number): void {
1047 const interval
= customInterval
?? Constants
.DEFAULT_RESERVATION_EXPIRATION_INTERVAL
;
1050 `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds(
1054 this.reservationExpirationSetInterval
= setInterval((): void => {
1055 removeExpiredReservations(this).catch(Constants
.EMPTY_FUNCTION
);
1060 private stopReservationExpirationSetInterval(): void {
1061 if (!isNullOrUndefined(this.reservationExpirationSetInterval
)) {
1062 clearInterval(this.reservationExpirationSetInterval
);
1066 // private restartReservationExpiryDateSetInterval(): void {
1067 // this.stopReservationExpirationSetInterval();
1068 // this.startReservationExpirationSetInterval();
1071 private getNumberOfReservableConnectors(): number {
1072 let numberOfReservableConnectors
= 0;
1073 if (this.hasEvses
) {
1074 for (const evseStatus
of this.evses
.values()) {
1075 numberOfReservableConnectors
+= getNumberOfReservableConnectors(evseStatus
.connectors
);
1078 numberOfReservableConnectors
= getNumberOfReservableConnectors(this.connectors
);
1080 return numberOfReservableConnectors
- this.getNumberOfReservationsOnConnectorZero();
1083 private getNumberOfReservationsOnConnectorZero(): number {
1085 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1086 (this.hasEvses
&& this.evses
.get(0)?.connectors
.get(0)?.reservation
) ||
1087 (!this.hasEvses
&& this.connectors
.get(0)?.reservation
)
1094 private flushMessageBuffer(): void {
1095 if (this.messageBuffer
.size
> 0) {
1096 for (const message
of this.messageBuffer
.values()) {
1097 let beginId
: string | undefined;
1098 let commandName
: RequestCommand
| undefined;
1099 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
1100 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
1102 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
1103 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
1105 this.wsConnection
?.send(message
);
1106 isRequest
&& PerformanceStatistics
.endMeasure(commandName
!, beginId
!);
1108 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
1110 )} payload sent: ${message}`,
1112 this.messageBuffer
.delete(message
);
1117 private getSupervisionUrlOcppConfiguration(): boolean {
1118 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
1121 private getSupervisionUrlOcppKey(): string {
1122 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
1125 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
1126 let template
: ChargingStationTemplate
| undefined;
1128 if (this.sharedLRUCache
.hasChargingStationTemplate(this.templateFileHash
)) {
1129 template
= this.sharedLRUCache
.getChargingStationTemplate(this.templateFileHash
);
1131 const measureId
= `${FileType.ChargingStationTemplate} read`;
1132 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1133 template
= JSON
.parse(readFileSync(this.templateFile
, 'utf8')) as ChargingStationTemplate
;
1134 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1135 template
.templateHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1136 .update(JSON
.stringify(template
))
1138 this.sharedLRUCache
.setChargingStationTemplate(template
);
1139 this.templateFileHash
= template
.templateHash
;
1142 handleFileException(
1144 FileType
.ChargingStationTemplate
,
1145 error
as NodeJS
.ErrnoException
,
1152 private getStationInfoFromTemplate(): ChargingStationInfo
{
1153 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile()!;
1154 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1155 const warnTemplateKeysDeprecationOnce
= once(warnTemplateKeysDeprecation
, this);
1156 warnTemplateKeysDeprecationOnce(stationTemplate
, this.logPrefix(), this.templateFile
);
1157 if (stationTemplate
?.Connectors
) {
1158 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1160 const stationInfo
: ChargingStationInfo
= stationTemplateToStationInfo(stationTemplate
);
1161 stationInfo
.hashId
= getHashId(this.index
, stationTemplate
);
1162 stationInfo
.chargingStationId
= getChargingStationId(this.index
, stationTemplate
);
1163 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
1164 createSerialNumber(stationTemplate
, stationInfo
);
1165 if (isNotEmptyArray(stationTemplate
?.power
)) {
1166 stationTemplate
.power
= stationTemplate
.power
as number[];
1167 const powerArrayRandomIndex
= Math.floor(secureRandom() * stationTemplate
.power
.length
);
1168 stationInfo
.maximumPower
=
1169 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1170 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
1171 : stationTemplate
.power
[powerArrayRandomIndex
];
1173 stationTemplate
.power
= stationTemplate
?.power
as number;
1174 stationInfo
.maximumPower
=
1175 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
1176 ? stationTemplate
.power
* 1000
1177 : stationTemplate
.power
;
1179 stationInfo
.firmwareVersionPattern
=
1180 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
1182 isNotEmptyString(stationInfo
.firmwareVersion
) &&
1183 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
!) === false
1186 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1188 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
1191 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
1198 stationTemplate
?.firmwareUpgrade
?? {},
1200 stationInfo
.resetTime
= !isNullOrUndefined(stationTemplate
?.resetTime
)
1201 ? secondsToMilliseconds(stationTemplate
.resetTime
!)
1202 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
1203 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
1207 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
1208 let stationInfo
: ChargingStationInfo
| undefined;
1209 if (this.getStationInfoPersistentConfiguration()) {
1210 stationInfo
= this.getConfigurationFromFile()?.stationInfo
;
1212 delete stationInfo
?.infoHash
;
1218 private getStationInfo(): ChargingStationInfo
{
1219 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
1220 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
1222 // 1. charging station info from template
1223 // 2. charging station info from configuration file
1224 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
1225 return stationInfoFromFile
!;
1227 stationInfoFromFile
&&
1228 propagateSerialNumber(
1229 this.getTemplateFromFile()!,
1230 stationInfoFromFile
,
1231 stationInfoFromTemplate
,
1233 return stationInfoFromTemplate
;
1236 private saveStationInfo(): void {
1237 if (this.getStationInfoPersistentConfiguration()) {
1238 this.saveConfiguration();
1242 private getOcppPersistentConfiguration(): boolean {
1243 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
1246 private getStationInfoPersistentConfiguration(): boolean {
1247 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
1250 private getAutomaticTransactionGeneratorPersistentConfiguration(): boolean {
1251 return this.stationInfo
?.automaticTransactionGeneratorPersistentConfiguration
?? true;
1254 private handleUnsupportedVersion(version
: OCPPVersion
) {
1255 const errorMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
1256 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1257 throw new BaseError(errorMsg
);
1260 private initialize(): void {
1261 const stationTemplate
= this.getTemplateFromFile()!;
1262 checkTemplate(stationTemplate
, this.logPrefix(), this.templateFile
);
1263 this.configurationFile
= join(
1264 dirname(this.templateFile
.replace('station-templates', 'configurations')),
1265 `${getHashId(this.index, stationTemplate)}.json`,
1267 const chargingStationConfiguration
= this.getConfigurationFromFile();
1269 chargingStationConfiguration
?.stationInfo
?.templateHash
=== stationTemplate
?.templateHash
&&
1270 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1271 (chargingStationConfiguration
?.connectorsStatus
|| chargingStationConfiguration
?.evsesStatus
)
1273 this.initializeConnectorsOrEvsesFromFile(chargingStationConfiguration
);
1275 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate
);
1277 this.stationInfo
= this.getStationInfo();
1279 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1280 isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1281 isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1283 const patternGroup
: number | undefined =
1284 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1285 this.stationInfo
.firmwareVersion
?.split('.').length
;
1286 const match
= this.stationInfo
1287 .firmwareVersion
!.match(new RegExp(this.stationInfo
.firmwareVersionPattern
!))!
1288 .slice(1, patternGroup
! + 1);
1289 const patchLevelIndex
= match
.length
- 1;
1290 match
[patchLevelIndex
] = (
1291 convertToInt(match
[patchLevelIndex
]) +
1292 this.stationInfo
.firmwareUpgrade
!.versionUpgrade
!.step
!
1294 this.stationInfo
.firmwareVersion
= match
?.join('.');
1296 this.saveStationInfo();
1297 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
1298 if (this.getEnableStatistics() === true) {
1299 this.performanceStatistics
= PerformanceStatistics
.getInstance(
1300 this.stationInfo
.hashId
,
1301 this.stationInfo
.chargingStationId
!,
1302 this.configuredSupervisionUrl
,
1305 this.bootNotificationRequest
= createBootNotificationRequest(this.stationInfo
);
1306 this.powerDivider
= this.getPowerDivider();
1307 // OCPP configuration
1308 this.ocppConfiguration
= this.getOcppConfiguration();
1309 this.initializeOcppConfiguration();
1310 this.initializeOcppServices();
1311 if (this.stationInfo
?.autoRegister
=== true) {
1312 this.bootNotificationResponse
= {
1313 currentTime
: new Date(),
1314 interval
: millisecondsToSeconds(this.getHeartbeatInterval()),
1315 status: RegistrationStatusEnumType
.ACCEPTED
,
1320 private initializeOcppServices(): void {
1321 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1322 switch (ocppVersion
) {
1323 case OCPPVersion
.VERSION_16
:
1324 this.ocppIncomingRequestService
=
1325 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1326 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1327 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(),
1330 case OCPPVersion
.VERSION_20
:
1331 case OCPPVersion
.VERSION_201
:
1332 this.ocppIncomingRequestService
=
1333 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1334 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1335 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>(),
1339 this.handleUnsupportedVersion(ocppVersion
);
1344 private initializeOcppConfiguration(): void {
1345 if (!getConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
)) {
1346 addConfigurationKey(this, StandardParametersKey
.HeartbeatInterval
, '0');
1348 if (!getConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
)) {
1349 addConfigurationKey(this, StandardParametersKey
.HeartBeatInterval
, '0', { visible
: false });
1352 this.getSupervisionUrlOcppConfiguration() &&
1353 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1354 !getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1356 addConfigurationKey(
1358 this.getSupervisionUrlOcppKey(),
1359 this.configuredSupervisionUrl
.href
,
1363 !this.getSupervisionUrlOcppConfiguration() &&
1364 isNotEmptyString(this.getSupervisionUrlOcppKey()) &&
1365 getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1367 deleteConfigurationKey(this, this.getSupervisionUrlOcppKey(), { save
: false });
1370 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1371 !getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)
1373 addConfigurationKey(
1375 this.stationInfo
.amperageLimitationOcppKey
!,
1377 this.stationInfo
.maximumAmperage
! * getAmperageLimitationUnitDivider(this.stationInfo
)
1381 if (!getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)) {
1382 addConfigurationKey(
1384 StandardParametersKey
.SupportedFeatureProfiles
,
1385 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
1388 addConfigurationKey(
1390 StandardParametersKey
.NumberOfConnectors
,
1391 this.getNumberOfConnectors().toString(),
1393 { overwrite
: true },
1395 if (!getConfigurationKey(this, StandardParametersKey
.MeterValuesSampledData
)) {
1396 addConfigurationKey(
1398 StandardParametersKey
.MeterValuesSampledData
,
1399 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
1402 if (!getConfigurationKey(this, StandardParametersKey
.ConnectorPhaseRotation
)) {
1403 const connectorsPhaseRotation
: string[] = [];
1404 if (this.hasEvses
) {
1405 for (const evseStatus
of this.evses
.values()) {
1406 for (const connectorId
of evseStatus
.connectors
.keys()) {
1407 connectorsPhaseRotation
.push(
1408 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1413 for (const connectorId
of this.connectors
.keys()) {
1414 connectorsPhaseRotation
.push(
1415 getPhaseRotationValue(connectorId
, this.getNumberOfPhases())!,
1419 addConfigurationKey(
1421 StandardParametersKey
.ConnectorPhaseRotation
,
1422 connectorsPhaseRotation
.toString(),
1425 if (!getConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
)) {
1426 addConfigurationKey(this, StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
1429 !getConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
) &&
1430 getConfigurationKey(this, StandardParametersKey
.SupportedFeatureProfiles
)?.value
?.includes(
1431 SupportedFeatureProfiles
.LocalAuthListManagement
,
1434 addConfigurationKey(this, StandardParametersKey
.LocalAuthListEnabled
, 'false');
1436 if (!getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)) {
1437 addConfigurationKey(
1439 StandardParametersKey
.ConnectionTimeOut
,
1440 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString(),
1443 this.saveOcppConfiguration();
1446 private initializeConnectorsOrEvsesFromFile(configuration
: ChargingStationConfiguration
): void {
1447 if (configuration
?.connectorsStatus
&& !configuration
?.evsesStatus
) {
1448 for (const [connectorId
, connectorStatus
] of configuration
.connectorsStatus
.entries()) {
1449 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1451 } else if (configuration
?.evsesStatus
&& !configuration
?.connectorsStatus
) {
1452 for (const [evseId
, evseStatusConfiguration
] of configuration
.evsesStatus
.entries()) {
1453 const evseStatus
= cloneObject
<EvseStatusConfiguration
>(evseStatusConfiguration
);
1454 delete evseStatus
.connectorsStatus
;
1455 this.evses
.set(evseId
, {
1456 ...(evseStatus
as EvseStatus
),
1457 connectors
: new Map
<number, ConnectorStatus
>(
1458 evseStatusConfiguration
.connectorsStatus
!.map((connectorStatus
, connectorId
) => [
1465 } else if (configuration
?.evsesStatus
&& configuration
?.connectorsStatus
) {
1466 const errorMsg
= `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1467 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1468 throw new BaseError(errorMsg
);
1470 const errorMsg
= `No connectors or evses defined in configuration file ${this.configurationFile}`;
1471 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1472 throw new BaseError(errorMsg
);
1476 private initializeConnectorsOrEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
) {
1477 if (stationTemplate
?.Connectors
&& !stationTemplate
?.Evses
) {
1478 this.initializeConnectorsFromTemplate(stationTemplate
);
1479 } else if (stationTemplate
?.Evses
&& !stationTemplate
?.Connectors
) {
1480 this.initializeEvsesFromTemplate(stationTemplate
);
1481 } else if (stationTemplate
?.Evses
&& stationTemplate
?.Connectors
) {
1482 const errorMsg
= `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1483 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1484 throw new BaseError(errorMsg
);
1486 const errorMsg
= `No connectors or evses defined in template file ${this.templateFile}`;
1487 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1488 throw new BaseError(errorMsg
);
1492 private initializeConnectorsFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1493 if (!stationTemplate
?.Connectors
&& this.connectors
.size
=== 0) {
1494 const errorMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1495 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1496 throw new BaseError(errorMsg
);
1498 if (!stationTemplate
?.Connectors
?.[0]) {
1500 `${this.logPrefix()} Charging station information from template ${
1502 } with no connector id 0 configuration`,
1505 if (stationTemplate
?.Connectors
) {
1506 const { configuredMaxConnectors
, templateMaxConnectors
, templateMaxAvailableConnectors
} =
1507 checkConnectorsConfiguration(stationTemplate
, this.logPrefix(), this.templateFile
);
1508 const connectorsConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1510 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
1513 const connectorsConfigChanged
=
1514 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1515 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1516 connectorsConfigChanged
&& this.connectors
.clear();
1517 this.connectorsConfigurationHash
= connectorsConfigHash
;
1518 if (templateMaxConnectors
> 0) {
1519 for (let connectorId
= 0; connectorId
<= configuredMaxConnectors
; connectorId
++) {
1521 connectorId
=== 0 &&
1522 (!stationTemplate
?.Connectors
?.[connectorId
] ||
1523 this.getUseConnectorId0(stationTemplate
) === false)
1527 const templateConnectorId
=
1528 connectorId
> 0 && stationTemplate
?.randomConnectors
1529 ? getRandomInteger(templateMaxAvailableConnectors
, 1)
1531 const connectorStatus
= stationTemplate
?.Connectors
[templateConnectorId
];
1532 checkStationInfoConnectorStatus(
1533 templateConnectorId
,
1538 this.connectors
.set(connectorId
, cloneObject
<ConnectorStatus
>(connectorStatus
));
1540 initializeConnectorsMapStatus(this.connectors
, this.logPrefix());
1541 this.saveConnectorsStatus();
1544 `${this.logPrefix()} Charging station information from template ${
1546 } with no connectors configuration defined, cannot create connectors`,
1552 `${this.logPrefix()} Charging station information from template ${
1554 } with no connectors configuration defined, using already defined connectors`,
1559 private initializeEvsesFromTemplate(stationTemplate
: ChargingStationTemplate
): void {
1560 if (!stationTemplate
?.Evses
&& this.evses
.size
=== 0) {
1561 const errorMsg
= `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1562 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1563 throw new BaseError(errorMsg
);
1565 if (!stationTemplate
?.Evses
?.[0]) {
1567 `${this.logPrefix()} Charging station information from template ${
1569 } with no evse id 0 configuration`,
1572 if (!stationTemplate
?.Evses
?.[0]?.Connectors
?.[0]) {
1574 `${this.logPrefix()} Charging station information from template ${
1576 } with evse id 0 with no connector id 0 configuration`,
1579 if (Object.keys(stationTemplate
?.Evses
?.[0]?.Connectors
as object
).length
> 1) {
1581 `${this.logPrefix()} Charging station information from template ${
1583 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`,
1586 if (stationTemplate
?.Evses
) {
1587 const evsesConfigHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1588 .update(JSON
.stringify(stationTemplate
?.Evses
))
1590 const evsesConfigChanged
=
1591 this.evses
?.size
!== 0 && this.evsesConfigurationHash
!== evsesConfigHash
;
1592 if (this.evses
?.size
=== 0 || evsesConfigChanged
) {
1593 evsesConfigChanged
&& this.evses
.clear();
1594 this.evsesConfigurationHash
= evsesConfigHash
;
1595 const templateMaxEvses
= getMaxNumberOfEvses(stationTemplate
?.Evses
);
1596 if (templateMaxEvses
> 0) {
1597 for (const evseKey
in stationTemplate
.Evses
) {
1598 const evseId
= convertToInt(evseKey
);
1599 this.evses
.set(evseId
, {
1600 connectors
: buildConnectorsMap(
1601 stationTemplate
?.Evses
[evseKey
]?.Connectors
,
1605 availability
: AvailabilityType
.Operative
,
1607 initializeConnectorsMapStatus(this.evses
.get(evseId
)!.connectors
, this.logPrefix());
1609 this.saveEvsesStatus();
1612 `${this.logPrefix()} Charging station information from template ${
1614 } with no evses configuration defined, cannot create evses`,
1620 `${this.logPrefix()} Charging station information from template ${
1622 } with no evses configuration defined, using already defined evses`,
1627 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1628 let configuration
: ChargingStationConfiguration
| undefined;
1629 if (isNotEmptyString(this.configurationFile
) && existsSync(this.configurationFile
)) {
1631 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1632 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1633 this.configurationFileHash
,
1636 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1637 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1638 configuration
= JSON
.parse(
1639 readFileSync(this.configurationFile
, 'utf8'),
1640 ) as ChargingStationConfiguration
;
1641 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1642 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1643 this.configurationFileHash
= configuration
.configurationHash
!;
1646 handleFileException(
1647 this.configurationFile
,
1648 FileType
.ChargingStationConfiguration
,
1649 error
as NodeJS
.ErrnoException
,
1654 return configuration
;
1657 private saveAutomaticTransactionGeneratorConfiguration(): void {
1658 if (this.getAutomaticTransactionGeneratorPersistentConfiguration()) {
1659 this.saveConfiguration();
1663 private saveConnectorsStatus() {
1664 this.saveConfiguration();
1667 private saveEvsesStatus() {
1668 this.saveConfiguration();
1671 private saveConfiguration(): void {
1672 if (isNotEmptyString(this.configurationFile
)) {
1674 if (!existsSync(dirname(this.configurationFile
))) {
1675 mkdirSync(dirname(this.configurationFile
), { recursive
: true });
1677 let configurationData
: ChargingStationConfiguration
= this.getConfigurationFromFile()
1678 ? cloneObject
<ChargingStationConfiguration
>(this.getConfigurationFromFile()!)
1680 if (this.getStationInfoPersistentConfiguration() && this.stationInfo
) {
1681 configurationData
.stationInfo
= this.stationInfo
;
1683 delete configurationData
.stationInfo
;
1685 if (this.getOcppPersistentConfiguration() && this.ocppConfiguration
?.configurationKey
) {
1686 configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
;
1688 delete configurationData
.configurationKey
;
1690 configurationData
= merge
<ChargingStationConfiguration
>(
1692 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
1695 !this.getAutomaticTransactionGeneratorPersistentConfiguration() ||
1696 !this.getAutomaticTransactionGeneratorConfiguration()
1698 delete configurationData
.automaticTransactionGenerator
;
1700 if (this.connectors
.size
> 0) {
1701 configurationData
.connectorsStatus
= buildConnectorsStatus(this);
1703 delete configurationData
.connectorsStatus
;
1705 if (this.evses
.size
> 0) {
1706 configurationData
.evsesStatus
= buildEvsesStatus(this);
1708 delete configurationData
.evsesStatus
;
1710 delete configurationData
.configurationHash
;
1711 const configurationHash
= createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1714 stationInfo
: configurationData
.stationInfo
,
1715 configurationKey
: configurationData
.configurationKey
,
1716 automaticTransactionGenerator
: configurationData
.automaticTransactionGenerator
,
1717 ...(this.connectors
.size
> 0 && {
1718 connectorsStatus
: configurationData
.connectorsStatus
,
1720 ...(this.evses
.size
> 0 && { evsesStatus
: configurationData
.evsesStatus
}),
1721 } as ChargingStationConfiguration
),
1724 if (this.configurationFileHash
!== configurationHash
) {
1725 AsyncLock
.runExclusive(AsyncLockType
.configuration
, () => {
1726 configurationData
.configurationHash
= configurationHash
;
1727 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1728 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1730 this.configurationFile
,
1731 JSON
.stringify(configurationData
, undefined, 2),
1734 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1735 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1736 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1737 this.configurationFileHash
= configurationHash
;
1738 }).catch((error
) => {
1739 handleFileException(
1740 this.configurationFile
,
1741 FileType
.ChargingStationConfiguration
,
1742 error
as NodeJS
.ErrnoException
,
1748 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1749 this.configurationFile
1754 handleFileException(
1755 this.configurationFile
,
1756 FileType
.ChargingStationConfiguration
,
1757 error
as NodeJS
.ErrnoException
,
1763 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
1768 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1769 return this.getTemplateFromFile()?.Configuration
;
1772 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1773 const configurationKey
= this.getConfigurationFromFile()?.configurationKey
;
1774 if (this.getOcppPersistentConfiguration() === true && configurationKey
) {
1775 return { configurationKey
};
1780 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1781 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1782 this.getOcppConfigurationFromFile();
1783 if (!ocppConfiguration
) {
1784 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1786 return ocppConfiguration
;
1789 private async onOpen(): Promise
<void> {
1790 if (this.isWebSocketConnectionOpened() === true) {
1792 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
1794 if (this.isRegistered() === false) {
1795 // Send BootNotification
1796 let registrationRetryCount
= 0;
1798 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1799 BootNotificationRequest
,
1800 BootNotificationResponse
1801 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1802 skipBufferingOnError
: true,
1804 if (this.isRegistered() === false) {
1805 this.getRegistrationMaxRetries() !== -1 && ++registrationRetryCount
;
1807 this?.bootNotificationResponse
?.interval
1808 ? secondsToMilliseconds(this.bootNotificationResponse
.interval
)
1809 : Constants
.DEFAULT_BOOT_NOTIFICATION_INTERVAL
,
1813 this.isRegistered() === false &&
1814 (registrationRetryCount
<= this.getRegistrationMaxRetries()! ||
1815 this.getRegistrationMaxRetries() === -1)
1818 if (this.isRegistered() === true) {
1819 this.emit(ChargingStationEvents
.registered
);
1820 if (this.inAcceptedState() === true) {
1821 this.emit(ChargingStationEvents
.accepted
);
1822 await this.startMessageSequence();
1826 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`,
1829 this.wsConnectionRestarted
= false;
1830 this.autoReconnectRetryCount
= 0;
1831 this.emit(ChargingStationEvents
.updated
);
1834 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
1839 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1842 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1843 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1845 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
1847 )}' and reason '${reason.toString()}'`,
1849 this.autoReconnectRetryCount
= 0;
1854 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
1856 )}' and reason '${reason.toString()}'`,
1858 this.started
=== true && (await this.reconnect());
1861 this.emit(ChargingStationEvents
.updated
);
1864 private getCachedRequest(messageType
: MessageType
, messageId
: string): CachedRequest
| undefined {
1865 const cachedRequest
= this.requests
.get(messageId
);
1866 if (Array.isArray(cachedRequest
) === true) {
1867 return cachedRequest
;
1869 throw new OCPPError(
1870 ErrorType
.PROTOCOL_ERROR
,
1871 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
1873 )} is not an array`,
1875 cachedRequest
as JsonType
,
1879 private async handleIncomingMessage(request
: IncomingRequest
): Promise
<void> {
1880 const [messageType
, messageId
, commandName
, commandPayload
] = request
;
1881 if (this.getEnableStatistics() === true) {
1882 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1885 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1889 // Process the message
1890 await this.ocppIncomingRequestService
.incomingRequestHandler(
1898 private handleResponseMessage(response
: Response
): void {
1899 const [messageType
, messageId
, commandPayload
] = response
;
1900 if (this.requests
.has(messageId
) === false) {
1902 throw new OCPPError(
1903 ErrorType
.INTERNAL_ERROR
,
1904 `Response for unknown message id ${messageId}`,
1910 const [responseCallback
, , requestCommandName
, requestPayload
] = this.getCachedRequest(
1915 `${this.logPrefix()} << Command '${
1916 requestCommandName ?? Constants.UNKNOWN_COMMAND
1917 }' received response payload: ${JSON.stringify(response)}`,
1919 responseCallback(commandPayload
, requestPayload
);
1922 private handleErrorMessage(errorResponse
: ErrorResponse
): void {
1923 const [messageType
, messageId
, errorType
, errorMessage
, errorDetails
] = errorResponse
;
1924 if (this.requests
.has(messageId
) === false) {
1926 throw new OCPPError(
1927 ErrorType
.INTERNAL_ERROR
,
1928 `Error response for unknown message id ${messageId}`,
1930 { errorType
, errorMessage
, errorDetails
},
1933 const [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1935 `${this.logPrefix()} << Command '${
1936 requestCommandName ?? Constants.UNKNOWN_COMMAND
1937 }' received error response payload: ${JSON.stringify(errorResponse)}`,
1939 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1942 private async onMessage(data
: RawData
): Promise
<void> {
1943 let request
: IncomingRequest
| Response
| ErrorResponse
| undefined;
1944 let messageType
: number | undefined;
1945 let errorMsg
: string;
1947 // eslint-disable-next-line @typescript-eslint/no-base-to-string
1948 request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1949 if (Array.isArray(request
) === true) {
1950 [messageType
] = request
;
1951 // Check the type of message
1952 switch (messageType
) {
1954 case MessageType
.CALL_MESSAGE
:
1955 await this.handleIncomingMessage(request
as IncomingRequest
);
1958 case MessageType
.CALL_RESULT_MESSAGE
:
1959 this.handleResponseMessage(request
as Response
);
1962 case MessageType
.CALL_ERROR_MESSAGE
:
1963 this.handleErrorMessage(request
as ErrorResponse
);
1967 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1968 errorMsg
= `Wrong message type ${messageType}`;
1969 logger
.error(`${this.logPrefix()} ${errorMsg}`);
1970 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errorMsg
);
1972 this.emit(ChargingStationEvents
.updated
);
1974 throw new OCPPError(
1975 ErrorType
.PROTOCOL_ERROR
,
1976 'Incoming message is not an array',
1984 let commandName
: IncomingRequestCommand
| undefined;
1985 let requestCommandName
: RequestCommand
| IncomingRequestCommand
| undefined;
1986 let errorCallback
: ErrorCallback
;
1987 const [, messageId
] = request
!;
1988 switch (messageType
) {
1989 case MessageType
.CALL_MESSAGE
:
1990 [, , commandName
] = request
as IncomingRequest
;
1992 await this.ocppRequestService
.sendError(this, messageId
, error
as OCPPError
, commandName
);
1994 case MessageType
.CALL_RESULT_MESSAGE
:
1995 case MessageType
.CALL_ERROR_MESSAGE
:
1996 if (this.requests
.has(messageId
) === true) {
1997 [, errorCallback
, requestCommandName
] = this.getCachedRequest(messageType
, messageId
)!;
1998 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1999 errorCallback(error
as OCPPError
, false);
2001 // Remove the request from the cache in case of error at response handling
2002 this.requests
.delete(messageId
);
2006 if (error
instanceof OCPPError
=== false) {
2008 `${this.logPrefix()} Error thrown at incoming OCPP command '${
2009 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2010 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2011 }' message '${data.toString()}' handling is not an OCPPError:`,
2016 `${this.logPrefix()} Incoming OCPP command '${
2017 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
2018 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2019 }' message '${data.toString()}'${
2020 messageType !== MessageType.CALL_MESSAGE
2021 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
2023 } processing error:`,
2029 private onPing(): void {
2030 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
2033 private onPong(): void {
2034 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
2037 private onError(error
: WSError
): void {
2038 this.closeWSConnection();
2039 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
2042 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
2043 if (this.getMeteringPerTransaction() === true) {
2046 ? Math.round(connectorStatus
.transactionEnergyActiveImportRegisterValue
!)
2047 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
2052 ? Math.round(connectorStatus
.energyActiveImportRegisterValue
!)
2053 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
2057 private getUseConnectorId0(stationTemplate
?: ChargingStationTemplate
): boolean {
2058 return stationTemplate
?.useConnectorId0
?? true;
2061 private async stopRunningTransactions(reason
?: StopTransactionReason
): Promise
<void> {
2062 if (this.hasEvses
) {
2063 for (const [evseId
, evseStatus
] of this.evses
) {
2067 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2068 if (connectorStatus
.transactionStarted
=== true) {
2069 await this.stopTransactionOnConnector(connectorId
, reason
);
2074 for (const connectorId
of this.connectors
.keys()) {
2075 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
2076 await this.stopTransactionOnConnector(connectorId
, reason
);
2083 private getConnectionTimeout(): number {
2084 if (getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)) {
2086 parseInt(getConfigurationKey(this, StandardParametersKey
.ConnectionTimeOut
)!.value
!) ??
2087 Constants
.DEFAULT_CONNECTION_TIMEOUT
2090 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
2093 // -1 for unlimited, 0 for disabling
2094 private getAutoReconnectMaxRetries(): number | undefined {
2095 return this.stationInfo
.autoReconnectMaxRetries
?? -1;
2098 // -1 for unlimited, 0 for disabling
2099 private getRegistrationMaxRetries(): number | undefined {
2100 return this.stationInfo
.registrationMaxRetries
?? -1;
2103 private getPowerDivider(): number {
2104 let powerDivider
= this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors();
2105 if (this.stationInfo
?.powerSharedByConnectors
) {
2106 powerDivider
= this.getNumberOfRunningTransactions();
2108 return powerDivider
;
2111 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
2112 const maximumPower
= this.getMaximumPower(stationInfo
);
2113 switch (this.getCurrentOutType(stationInfo
)) {
2114 case CurrentType
.AC
:
2115 return ACElectricUtils
.amperagePerPhaseFromPower(
2116 this.getNumberOfPhases(stationInfo
),
2117 maximumPower
/ (this.hasEvses
? this.getNumberOfEvses() : this.getNumberOfConnectors()),
2118 this.getVoltageOut(stationInfo
),
2120 case CurrentType
.DC
:
2121 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
2125 private getAmperageLimitation(): number | undefined {
2127 isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
2128 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)
2132 getConfigurationKey(this, this.stationInfo
.amperageLimitationOcppKey
!)?.value
,
2133 ) / getAmperageLimitationUnitDivider(this.stationInfo
)
2138 private async startMessageSequence(): Promise
<void> {
2139 if (this.stationInfo
?.autoRegister
=== true) {
2140 await this.ocppRequestService
.requestHandler
<
2141 BootNotificationRequest
,
2142 BootNotificationResponse
2143 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
2144 skipBufferingOnError
: true,
2147 // Start WebSocket ping
2148 this.startWebSocketPing();
2150 this.startHeartbeat();
2151 // Initialize connectors status
2152 if (this.hasEvses
) {
2153 for (const [evseId
, evseStatus
] of this.evses
) {
2155 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2156 const connectorBootStatus
= getBootConnectorStatus(this, connectorId
, connectorStatus
);
2157 await OCPPServiceUtils
.sendAndSetConnectorStatus(
2160 connectorBootStatus
,
2167 for (const connectorId
of this.connectors
.keys()) {
2168 if (connectorId
> 0) {
2169 const connectorBootStatus
= getBootConnectorStatus(
2172 this.getConnectorStatus(connectorId
)!,
2174 await OCPPServiceUtils
.sendAndSetConnectorStatus(this, connectorId
, connectorBootStatus
);
2178 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
2179 await this.ocppRequestService
.requestHandler
<
2180 FirmwareStatusNotificationRequest
,
2181 FirmwareStatusNotificationResponse
2182 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
2183 status: FirmwareStatus
.Installed
,
2185 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
2189 if (this.getAutomaticTransactionGeneratorConfiguration()?.enable
=== true) {
2190 this.startAutomaticTransactionGenerator();
2192 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
2195 private async stopMessageSequence(
2196 reason
?: StopTransactionReason
,
2197 stopTransactions
= true,
2199 // Stop WebSocket ping
2200 this.stopWebSocketPing();
2202 this.stopHeartbeat();
2203 // Stop ongoing transactions
2204 stopTransactions
&& (await this.stopRunningTransactions(reason
));
2206 if (this.automaticTransactionGenerator
?.started
=== true) {
2207 this.stopAutomaticTransactionGenerator();
2209 if (this.hasEvses
) {
2210 for (const [evseId
, evseStatus
] of this.evses
) {
2212 for (const [connectorId
, connectorStatus
] of evseStatus
.connectors
) {
2213 await this.ocppRequestService
.requestHandler
<
2214 StatusNotificationRequest
,
2215 StatusNotificationResponse
2218 RequestCommand
.STATUS_NOTIFICATION
,
2219 OCPPServiceUtils
.buildStatusNotificationRequest(
2222 ConnectorStatusEnum
.Unavailable
,
2226 delete connectorStatus
?.status;
2231 for (const connectorId
of this.connectors
.keys()) {
2232 if (connectorId
> 0) {
2233 await this.ocppRequestService
.requestHandler
<
2234 StatusNotificationRequest
,
2235 StatusNotificationResponse
2238 RequestCommand
.STATUS_NOTIFICATION
,
2239 OCPPServiceUtils
.buildStatusNotificationRequest(
2242 ConnectorStatusEnum
.Unavailable
,
2245 delete this.getConnectorStatus(connectorId
)?.status;
2251 private startWebSocketPing(): void {
2252 const webSocketPingInterval
: number = getConfigurationKey(
2254 StandardParametersKey
.WebSocketPingInterval
,
2256 ? convertToInt(getConfigurationKey(this, StandardParametersKey
.WebSocketPingInterval
)?.value
)
2258 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
2259 this.webSocketPingSetInterval
= setInterval(() => {
2260 if (this.isWebSocketConnectionOpened() === true) {
2261 this.wsConnection
?.ping();
2263 }, secondsToMilliseconds(webSocketPingInterval
));
2265 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
2266 webSocketPingInterval,
2269 } else if (this.webSocketPingSetInterval
) {
2271 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
2272 webSocketPingInterval,
2277 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
2282 private stopWebSocketPing(): void {
2283 if (this.webSocketPingSetInterval
) {
2284 clearInterval(this.webSocketPingSetInterval
);
2285 delete this.webSocketPingSetInterval
;
2289 private getConfiguredSupervisionUrl(): URL
{
2290 let configuredSupervisionUrl
: string;
2291 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
2292 if (isNotEmptyArray(supervisionUrls
)) {
2293 let configuredSupervisionUrlIndex
: number;
2294 switch (Configuration
.getSupervisionUrlDistribution()) {
2295 case SupervisionUrlDistribution
.RANDOM
:
2296 configuredSupervisionUrlIndex
= Math.floor(
2297 secureRandom() * (supervisionUrls
as string[]).length
,
2300 case SupervisionUrlDistribution
.ROUND_ROBIN
:
2301 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
2303 Object.values(SupervisionUrlDistribution
).includes(
2304 Configuration
.getSupervisionUrlDistribution()!,
2307 // eslint-disable-next-line @typescript-eslint/no-base-to-string
2308 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2309 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
2312 configuredSupervisionUrlIndex
= (this.index
- 1) % (supervisionUrls
as string[]).length
;
2315 configuredSupervisionUrl
= (supervisionUrls
as string[])[configuredSupervisionUrlIndex
];
2317 configuredSupervisionUrl
= supervisionUrls
as string;
2319 if (isNotEmptyString(configuredSupervisionUrl
)) {
2320 return new URL(configuredSupervisionUrl
);
2322 const errorMsg
= 'No supervision url(s) configured';
2323 logger
.error(`${this.logPrefix()} ${errorMsg}`);
2324 throw new BaseError(`${errorMsg}`);
2327 private stopHeartbeat(): void {
2328 if (this.heartbeatSetInterval
) {
2329 clearInterval(this.heartbeatSetInterval
);
2330 delete this.heartbeatSetInterval
;
2334 private terminateWSConnection(): void {
2335 if (this.isWebSocketConnectionOpened() === true) {
2336 this.wsConnection
?.terminate();
2337 this.wsConnection
= null;
2341 private getReconnectExponentialDelay(): boolean {
2342 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2345 private async reconnect(): Promise
<void> {
2346 // Stop WebSocket ping
2347 this.stopWebSocketPing();
2349 this.stopHeartbeat();
2350 // Stop the ATG if needed
2351 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure
=== true) {
2352 this.stopAutomaticTransactionGenerator();
2355 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries()! ||
2356 this.getAutoReconnectMaxRetries() === -1
2358 ++this.autoReconnectRetryCount
;
2359 const reconnectDelay
= this.getReconnectExponentialDelay()
2360 ? exponentialDelay(this.autoReconnectRetryCount
)
2361 : secondsToMilliseconds(this.getConnectionTimeout());
2362 const reconnectDelayWithdraw
= 1000;
2363 const reconnectTimeout
=
2364 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2365 ? reconnectDelay
- reconnectDelayWithdraw
2368 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
2371 )}ms, timeout ${reconnectTimeout}ms`,
2373 await sleep(reconnectDelay
);
2375 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
2377 this.openWSConnection(
2379 handshakeTimeout
: reconnectTimeout
,
2381 { closeOpened
: true },
2383 this.wsConnectionRestarted
= true;
2384 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2386 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2387 this.autoReconnectRetryCount
2388 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`,