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';
14 AutomaticTransactionGenerator
,
15 ChargingStationConfigurationUtils
,
17 ChargingStationWorkerBroadcastChannel
,
22 // OCPP16IncomingRequestService,
24 // OCPP16ResponseService,
26 OCPP20IncomingRequestService
,
28 // OCPP20ResponseService,
29 type OCPPIncomingRequestService
,
30 type OCPPRequestService
,
33 import { OCPP16IncomingRequestService
} from
'./ocpp/1.6/OCPP16IncomingRequestService';
34 import { OCPP16ResponseService
} from
'./ocpp/1.6/OCPP16ResponseService';
35 import { OCPP20ResponseService
} from
'./ocpp/2.0/OCPP20ResponseService';
36 import { OCPPServiceUtils
} from
'./ocpp/OCPPServiceUtils';
37 import { BaseError
, OCPPError
} from
'../exception';
38 import { PerformanceStatistics
} from
'../performance';
40 type AutomaticTransactionGeneratorConfiguration
,
42 type BootNotificationRequest
,
43 type BootNotificationResponse
,
45 type ChargingStationConfiguration
,
46 type ChargingStationInfo
,
47 type ChargingStationOcppConfiguration
,
48 type ChargingStationTemplate
,
49 ConnectorPhaseRotation
,
58 type FirmwareStatusNotificationRequest
,
59 type FirmwareStatusNotificationResponse
,
61 type HeartbeatRequest
,
62 type HeartbeatResponse
,
64 type IncomingRequestCommand
,
69 type MeterValuesRequest
,
70 type MeterValuesResponse
,
74 RegistrationStatusEnumType
,
77 type ResponseCallback
,
78 StandardParametersKey
,
79 type StatusNotificationRequest
,
80 type StatusNotificationResponse
,
81 StopTransactionReason
,
82 type StopTransactionRequest
,
83 type StopTransactionResponse
,
84 SupervisionUrlDistribution
,
85 SupportedFeatureProfiles
,
88 WebSocketCloseEventStatusCode
,
101 export class ChargingStation
{
102 public readonly index
: number;
103 public readonly templateFile
: string;
104 public stationInfo
!: ChargingStationInfo
;
105 public started
: boolean;
106 public starting
: boolean;
107 public authorizedTagsCache
: AuthorizedTagsCache
;
108 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
| undefined;
109 public ocppConfiguration
!: ChargingStationOcppConfiguration
| undefined;
110 public wsConnection
!: WebSocket
| null;
111 public readonly connectors
: Map
<number, ConnectorStatus
>;
112 public readonly requests
: Map
<string, CachedRequest
>;
113 public performanceStatistics
!: PerformanceStatistics
| undefined;
114 public heartbeatSetInterval
!: NodeJS
.Timeout
;
115 public ocppRequestService
!: OCPPRequestService
;
116 public bootNotificationRequest
!: BootNotificationRequest
;
117 public bootNotificationResponse
!: BootNotificationResponse
| undefined;
118 public powerDivider
!: number;
119 private stopping
: boolean;
120 private configurationFile
!: string;
121 private configurationFileHash
!: string;
122 private connectorsConfigurationHash
!: string;
123 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
124 private readonly messageBuffer
: Set
<string>;
125 private configuredSupervisionUrl
!: URL
;
126 private configuredSupervisionUrlIndex
!: number;
127 private wsConnectionRestarted
: boolean;
128 private autoReconnectRetryCount
: number;
129 private templateFileWatcher
!: fs
.FSWatcher
| undefined;
130 private readonly sharedLRUCache
: SharedLRUCache
;
131 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
132 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
134 constructor(index
: number, templateFile
: string) {
135 this.started
= false;
136 this.starting
= false;
137 this.stopping
= false;
138 this.wsConnectionRestarted
= false;
139 this.autoReconnectRetryCount
= 0;
141 this.templateFile
= templateFile
;
142 this.connectors
= new Map
<number, ConnectorStatus
>();
143 this.requests
= new Map
<string, CachedRequest
>();
144 this.messageBuffer
= new Set
<string>();
145 this.sharedLRUCache
= SharedLRUCache
.getInstance();
146 this.authorizedTagsCache
= AuthorizedTagsCache
.getInstance();
147 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
152 private get
wsConnectionUrl(): URL
{
155 this.getSupervisionUrlOcppConfiguration()
156 ? ChargingStationConfigurationUtils.getConfigurationKey(
158 this.getSupervisionUrlOcppKey()
160 : this.configuredSupervisionUrl.href
161 }/${this.stationInfo.chargingStationId}`
165 public logPrefix
= (): string => {
166 return Utils
.logPrefix(
168 (Utils.isNotEmptyString(this?.stationInfo?.chargingStationId) &&
169 this?.stationInfo?.chargingStationId) ??
170 ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile()) ??
176 public hasAuthorizedTags(): boolean {
177 return Utils
.isNotEmptyArray(
178 this.authorizedTagsCache
.getAuthorizedTags(
179 ChargingStationUtils
.getAuthorizationFile(this.stationInfo
)
184 public getEnableStatistics(): boolean {
185 return this.stationInfo
.enableStatistics
?? false;
188 public getMustAuthorizeAtRemoteStart(): boolean {
189 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
192 public getPayloadSchemaValidation(): boolean {
193 return this.stationInfo
.payloadSchemaValidation
?? true;
196 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
197 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
198 switch (this.getCurrentOutType(stationInfo
)) {
200 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
201 ? localStationInfo
.numberOfPhases
208 public isWebSocketConnectionOpened(): boolean {
209 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
212 public getRegistrationStatus(): RegistrationStatusEnumType
| undefined {
213 return this?.bootNotificationResponse
?.status;
216 public isInUnknownState(): boolean {
217 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
220 public isInPendingState(): boolean {
221 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
224 public isInAcceptedState(): boolean {
225 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
228 public isInRejectedState(): boolean {
229 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
232 public isRegistered(): boolean {
234 this.isInUnknownState() === false &&
235 (this.isInAcceptedState() === true || this.isInPendingState() === true)
239 public isChargingStationAvailable(): boolean {
240 return this.getConnectorStatus(0)?.availability
=== AvailabilityType
.OPERATIVE
;
243 public isConnectorAvailable(id
: number): boolean {
244 return id
> 0 && this.getConnectorStatus(id
)?.availability
=== AvailabilityType
.OPERATIVE
;
247 public getNumberOfConnectors(): number {
248 return this.connectors
.get(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
251 public getConnectorStatus(id
: number): ConnectorStatus
| undefined {
252 return this.connectors
.get(id
);
255 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
256 return (stationInfo
?? this.stationInfo
)?.currentOutType
?? CurrentType
.AC
;
259 public getOcppStrictCompliance(): boolean {
260 return this.stationInfo
?.ocppStrictCompliance
?? false;
263 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
264 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
265 this.getCurrentOutType(stationInfo
),
269 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
270 return !Utils
.isUndefined(localStationInfo
.voltageOut
)
271 ? localStationInfo
.voltageOut
275 public getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
276 const localStationInfo
= stationInfo
?? this.stationInfo
;
277 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
280 public getConnectorMaximumAvailablePower(connectorId
: number): number {
281 let connectorAmperageLimitationPowerLimit
: number;
283 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
284 this.getAmperageLimitation() < this.stationInfo
?.maximumAmperage
286 connectorAmperageLimitationPowerLimit
=
287 (this.getCurrentOutType() === CurrentType
.AC
288 ? ACElectricUtils
.powerTotal(
289 this.getNumberOfPhases(),
290 this.getVoltageOut(),
291 this.getAmperageLimitation() * this.getNumberOfConnectors()
293 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
296 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
297 const connectorChargingProfilesPowerLimit
=
298 ChargingStationUtils
.getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId
);
300 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
301 isNaN(connectorAmperageLimitationPowerLimit
)
303 : connectorAmperageLimitationPowerLimit
,
304 isNaN(connectorChargingProfilesPowerLimit
) ? Infinity : connectorChargingProfilesPowerLimit
308 public getTransactionIdTag(transactionId
: number): string | undefined {
309 for (const connectorId
of this.connectors
.keys()) {
312 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
314 return this.getConnectorStatus(connectorId
)?.transactionIdTag
;
319 public getOutOfOrderEndMeterValues(): boolean {
320 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
323 public getBeginEndMeterValues(): boolean {
324 return this.stationInfo
?.beginEndMeterValues
?? false;
327 public getMeteringPerTransaction(): boolean {
328 return this.stationInfo
?.meteringPerTransaction
?? true;
331 public getTransactionDataMeterValues(): boolean {
332 return this.stationInfo
?.transactionDataMeterValues
?? false;
335 public getMainVoltageMeterValues(): boolean {
336 return this.stationInfo
?.mainVoltageMeterValues
?? true;
339 public getPhaseLineToLineVoltageMeterValues(): boolean {
340 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
343 public getCustomValueLimitationMeterValues(): boolean {
344 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
347 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
348 for (const connectorId
of this.connectors
.keys()) {
351 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
358 public getEnergyActiveImportRegisterByTransactionId(
359 transactionId
: number,
362 return this.getEnergyActiveImportRegister(
363 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
368 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number, rounded
= false): number {
369 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
), rounded
);
372 public getAuthorizeRemoteTxRequests(): boolean {
373 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
375 StandardParametersKey
.AuthorizeRemoteTxRequests
377 return authorizeRemoteTxRequests
378 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
382 public getLocalAuthListEnabled(): boolean {
383 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
385 StandardParametersKey
.LocalAuthListEnabled
387 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
390 public startHeartbeat(): void {
392 this.getHeartbeatInterval() &&
393 this.getHeartbeatInterval() > 0 &&
394 !this.heartbeatSetInterval
396 this.heartbeatSetInterval
= setInterval(() => {
397 this.ocppRequestService
398 .requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(this, RequestCommand
.HEARTBEAT
)
401 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
405 }, this.getHeartbeatInterval());
407 `${this.logPrefix()} Heartbeat started every ${Utils.formatDurationMilliSeconds(
408 this.getHeartbeatInterval()
411 } else if (this.heartbeatSetInterval
) {
413 `${this.logPrefix()} Heartbeat already started every ${Utils.formatDurationMilliSeconds(
414 this.getHeartbeatInterval()
419 `${this.logPrefix()} Heartbeat interval set to ${
420 this.getHeartbeatInterval()
421 ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
422 : this.getHeartbeatInterval()
423 }, not starting the heartbeat`
428 public restartHeartbeat(): void {
430 this.stopHeartbeat();
432 this.startHeartbeat();
435 public restartWebSocketPing(): void {
436 // Stop WebSocket ping
437 this.stopWebSocketPing();
438 // Start WebSocket ping
439 this.startWebSocketPing();
442 public startMeterValues(connectorId
: number, interval
: number): void {
443 if (connectorId
=== 0) {
445 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`
449 if (!this.getConnectorStatus(connectorId
)) {
451 `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`
455 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
457 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
461 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
462 Utils
.isNullOrUndefined(this.getConnectorStatus(connectorId
)?.transactionId
)
465 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`
470 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(() => {
471 // FIXME: Implement OCPP version agnostic helpers
472 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
475 this.getConnectorStatus(connectorId
).transactionId
,
478 this.ocppRequestService
479 .requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
481 RequestCommand
.METER_VALUES
,
484 transactionId
: this.getConnectorStatus(connectorId
)?.transactionId
,
485 meterValue
: [meterValue
],
490 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
497 `${this.logPrefix()} Charging station ${
498 StandardParametersKey.MeterValueSampleInterval
499 } configuration set to ${
500 interval ? Utils.formatDurationMilliSeconds(interval) : interval
501 }, not sending MeterValues`
506 public start(): void {
507 if (this.started
=== false) {
508 if (this.starting
=== false) {
509 this.starting
= true;
510 if (this.getEnableStatistics() === true) {
511 this.performanceStatistics
?.start();
513 this.openWSConnection();
514 // Monitor charging station template file
515 this.templateFileWatcher
= FileUtils
.watchJsonFile(
517 FileType
.ChargingStationTemplate
,
520 (event
, filename
): void => {
521 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
524 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
526 } file have changed, reload`
528 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
532 this.stopAutomaticTransactionGenerator();
534 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true
536 this.startAutomaticTransactionGenerator();
538 if (this.getEnableStatistics() === true) {
539 this.performanceStatistics
?.restart();
541 this.performanceStatistics
?.stop();
543 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
546 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
554 parentPort
?.postMessage(MessageChannelUtils
.buildStartedMessage(this));
555 this.starting
= false;
557 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
560 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
564 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
565 if (this.started
=== true) {
566 if (this.stopping
=== false) {
567 this.stopping
= true;
568 await this.stopMessageSequence(reason
);
569 this.closeWSConnection();
570 if (this.getEnableStatistics() === true) {
571 this.performanceStatistics
?.stop();
573 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
574 this.templateFileWatcher
?.close();
575 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
576 this.bootNotificationResponse
= undefined;
577 this.started
= false;
578 parentPort
?.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
579 this.stopping
= false;
581 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
584 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
588 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
589 await this.stop(reason
);
590 await Utils
.sleep(this.stationInfo
.resetTime
);
595 public saveOcppConfiguration(): void {
596 if (this.getOcppPersistentConfiguration()) {
597 this.saveConfiguration();
601 public resetConnectorStatus(connectorId
: number): void {
602 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
603 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
604 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
605 this.getConnectorStatus(connectorId
).transactionStarted
= false;
606 delete this.getConnectorStatus(connectorId
)?.localAuthorizeIdTag
;
607 delete this.getConnectorStatus(connectorId
)?.authorizeIdTag
;
608 delete this.getConnectorStatus(connectorId
)?.transactionId
;
609 delete this.getConnectorStatus(connectorId
)?.transactionIdTag
;
610 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;
611 delete this.getConnectorStatus(connectorId
)?.transactionBeginMeterValue
;
612 this.stopMeterValues(connectorId
);
613 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
616 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean | undefined {
617 return ChargingStationConfigurationUtils
.getConfigurationKey(
619 StandardParametersKey
.SupportedFeatureProfiles
620 )?.value
?.includes(featureProfile
);
623 public bufferMessage(message
: string): void {
624 this.messageBuffer
.add(message
);
627 public openWSConnection(
628 options
: WsOptions
= this.stationInfo
?.wsOptions
?? Constants
.EMPTY_OBJECT
,
629 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
631 terminateOpened
: false,
634 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
635 params
.closeOpened
= params
?.closeOpened
?? false;
636 params
.terminateOpened
= params
?.terminateOpened
?? false;
637 if (this.started
=== false && this.starting
=== false) {
639 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
644 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
645 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
647 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
649 if (params
?.closeOpened
) {
650 this.closeWSConnection();
652 if (params
?.terminateOpened
) {
653 this.terminateWSConnection();
656 if (this.isWebSocketConnectionOpened() === true) {
658 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
664 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
667 this.wsConnection
= new WebSocket(
668 this.wsConnectionUrl
,
669 `ocpp${this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16}`,
673 // Handle WebSocket message
674 this.wsConnection
.on(
676 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
678 // Handle WebSocket error
679 this.wsConnection
.on(
681 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
683 // Handle WebSocket close
684 this.wsConnection
.on(
686 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
688 // Handle WebSocket open
689 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
690 // Handle WebSocket ping
691 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
692 // Handle WebSocket pong
693 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
696 public closeWSConnection(): void {
697 if (this.isWebSocketConnectionOpened() === true) {
698 this.wsConnection
?.close();
699 this.wsConnection
= null;
703 public startAutomaticTransactionGenerator(
704 connectorIds
?: number[],
705 automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
707 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(
708 automaticTransactionGeneratorConfiguration
??
709 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
712 if (Utils
.isNotEmptyArray(connectorIds
)) {
713 for (const connectorId
of connectorIds
) {
714 this.automaticTransactionGenerator
?.startConnector(connectorId
);
717 this.automaticTransactionGenerator
?.start();
719 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
722 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
723 if (Utils
.isNotEmptyArray(connectorIds
)) {
724 for (const connectorId
of connectorIds
) {
725 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
728 this.automaticTransactionGenerator
?.stop();
730 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
733 public async stopTransactionOnConnector(
735 reason
= StopTransactionReason
.NONE
736 ): Promise
<StopTransactionResponse
> {
737 const transactionId
= this.getConnectorStatus(connectorId
)?.transactionId
;
739 this.getBeginEndMeterValues() === true &&
740 this.getOcppStrictCompliance() === true &&
741 this.getOutOfOrderEndMeterValues() === false
743 // FIXME: Implement OCPP version agnostic helpers
744 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
747 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
749 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
751 RequestCommand
.METER_VALUES
,
755 meterValue
: [transactionEndMeterValue
],
759 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
761 RequestCommand
.STOP_TRANSACTION
,
764 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
770 private flushMessageBuffer(): void {
771 if (this.messageBuffer
.size
> 0) {
772 this.messageBuffer
.forEach((message
) => {
774 let commandName
: RequestCommand
;
775 const [messageType
] = JSON
.parse(message
) as OutgoingRequest
| Response
| ErrorResponse
;
776 const isRequest
= messageType
=== MessageType
.CALL_MESSAGE
;
778 [, , commandName
] = JSON
.parse(message
) as OutgoingRequest
;
779 beginId
= PerformanceStatistics
.beginMeasure(commandName
);
781 this.wsConnection
?.send(message
);
782 isRequest
&& PerformanceStatistics
.endMeasure(commandName
, beginId
);
784 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
786 )} payload sent: ${message}`
788 this.messageBuffer
.delete(message
);
793 private getSupervisionUrlOcppConfiguration(): boolean {
794 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
797 private getSupervisionUrlOcppKey(): string {
798 return this.stationInfo
.supervisionUrlOcppKey
?? VendorParametersKey
.ConnectionUrl
;
801 private getTemplateFromFile(): ChargingStationTemplate
| undefined {
802 let template
: ChargingStationTemplate
;
804 if (this.sharedLRUCache
.hasChargingStationTemplate(this.stationInfo
?.templateHash
)) {
805 template
= this.sharedLRUCache
.getChargingStationTemplate(this.stationInfo
.templateHash
);
807 const measureId
= `${FileType.ChargingStationTemplate} read`;
808 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
809 template
= JSON
.parse(
810 fs
.readFileSync(this.templateFile
, 'utf8')
811 ) as ChargingStationTemplate
;
812 PerformanceStatistics
.endMeasure(measureId
, beginId
);
813 template
.templateHash
= crypto
814 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
815 .update(JSON
.stringify(template
))
817 this.sharedLRUCache
.setChargingStationTemplate(template
);
820 FileUtils
.handleFileException(
822 FileType
.ChargingStationTemplate
,
823 error
as NodeJS
.ErrnoException
,
830 private getStationInfoFromTemplate(): ChargingStationInfo
{
831 const stationTemplate
: ChargingStationTemplate
| undefined = this.getTemplateFromFile();
832 if (Utils
.isNullOrUndefined(stationTemplate
)) {
833 const errorMsg
= `Failed to read charging station template file ${this.templateFile}`;
834 logger
.error(`${this.logPrefix()} ${errorMsg}`);
835 throw new BaseError(errorMsg
);
837 if (Utils
.isEmptyObject(stationTemplate
)) {
838 const errorMsg
= `Empty charging station information from template file ${this.templateFile}`;
839 logger
.error(`${this.logPrefix()} ${errorMsg}`);
840 throw new BaseError(errorMsg
);
842 // Deprecation template keys section
843 ChargingStationUtils
.warnDeprecatedTemplateKey(
848 "Use 'supervisionUrls' instead"
850 ChargingStationUtils
.convertDeprecatedTemplateKey(
855 const stationInfo
: ChargingStationInfo
=
856 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
857 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
858 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
862 stationInfo
.ocppVersion
= stationTemplate
?.ocppVersion
?? OCPPVersion
.VERSION_16
;
863 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
864 if (Utils
.isNotEmptyArray(stationTemplate
?.power
)) {
865 stationTemplate
.power
= stationTemplate
.power
as number[];
866 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
867 stationInfo
.maximumPower
=
868 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
869 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
870 : stationTemplate
.power
[powerArrayRandomIndex
];
872 stationTemplate
.power
= stationTemplate
?.power
as number;
873 stationInfo
.maximumPower
=
874 stationTemplate
?.powerUnit
=== PowerUnits
.KILO_WATT
875 ? stationTemplate
.power
* 1000
876 : stationTemplate
.power
;
878 stationInfo
.firmwareVersionPattern
=
879 stationTemplate
?.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
881 Utils
.isNotEmptyString(stationInfo
.firmwareVersion
) &&
882 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
885 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
887 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
890 stationInfo
.firmwareUpgrade
= merge
<FirmwareUpgrade
>(
897 stationTemplate
?.firmwareUpgrade
?? Constants
.EMPTY_OBJECT
899 stationInfo
.resetTime
= !Utils
.isNullOrUndefined(stationTemplate
?.resetTime
)
900 ? stationTemplate
.resetTime
* 1000
901 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
902 const configuredMaxConnectors
=
903 ChargingStationUtils
.getConfiguredNumberOfConnectors(stationTemplate
);
904 ChargingStationUtils
.checkConfiguredMaxConnectors(
905 configuredMaxConnectors
,
909 const templateMaxConnectors
=
910 ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
);
911 ChargingStationUtils
.checkTemplateMaxConnectors(
912 templateMaxConnectors
,
917 configuredMaxConnectors
>
918 (stationTemplate
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) &&
919 !stationTemplate
?.randomConnectors
922 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
924 }, forcing random connector configurations affectation`
926 stationInfo
.randomConnectors
= true;
928 // Build connectors if needed (FIXME: should be factored out)
929 this.initializeConnectors(stationInfo
, configuredMaxConnectors
, templateMaxConnectors
);
930 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
931 ChargingStationUtils
.createStationInfoHash(stationInfo
);
935 private getStationInfoFromFile(): ChargingStationInfo
| undefined {
936 let stationInfo
: ChargingStationInfo
| undefined;
937 this.getStationInfoPersistentConfiguration() &&
938 (stationInfo
= this.getConfigurationFromFile()?.stationInfo
);
939 stationInfo
&& ChargingStationUtils
.createStationInfoHash(stationInfo
);
943 private getStationInfo(): ChargingStationInfo
{
944 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
945 const stationInfoFromFile
: ChargingStationInfo
| undefined = this.getStationInfoFromFile();
946 // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
947 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
948 if (this.stationInfo
?.infoHash
=== stationInfoFromFile
?.infoHash
) {
949 return this.stationInfo
;
951 return stationInfoFromFile
;
953 stationInfoFromFile
&&
954 ChargingStationUtils
.propagateSerialNumber(
955 this.getTemplateFromFile(),
957 stationInfoFromTemplate
959 return stationInfoFromTemplate
;
962 private saveStationInfo(): void {
963 if (this.getStationInfoPersistentConfiguration()) {
964 this.saveConfiguration();
968 private getOcppPersistentConfiguration(): boolean {
969 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
972 private getStationInfoPersistentConfiguration(): boolean {
973 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
976 private handleUnsupportedVersion(version
: OCPPVersion
) {
977 const errMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
978 logger
.error(`${this.logPrefix()} ${errMsg}`);
979 throw new BaseError(errMsg
);
982 private initialize(): void {
983 this.configurationFile
= path
.join(
984 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
985 `${ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile())}.json`
987 this.stationInfo
= this.getStationInfo();
988 this.saveStationInfo();
989 // Avoid duplication of connectors related information in RAM
990 this.stationInfo
?.Connectors
&& delete this.stationInfo
.Connectors
;
991 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
992 if (this.getEnableStatistics() === true) {
993 this.performanceStatistics
= PerformanceStatistics
.getInstance(
994 this.stationInfo
.hashId
,
995 this.stationInfo
.chargingStationId
,
996 this.configuredSupervisionUrl
999 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
1002 this.powerDivider
= this.getPowerDivider();
1003 // OCPP configuration
1004 this.ocppConfiguration
= this.getOcppConfiguration();
1005 this.initializeOcppConfiguration();
1006 this.initializeOcppServices();
1007 if (this.stationInfo
?.autoRegister
=== true) {
1008 this.bootNotificationResponse
= {
1009 currentTime
: new Date(),
1010 interval
: this.getHeartbeatInterval() / 1000,
1011 status: RegistrationStatusEnumType
.ACCEPTED
,
1015 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1016 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersion
) &&
1017 Utils
.isNotEmptyString(this.stationInfo
.firmwareVersionPattern
)
1019 const patternGroup
: number | undefined =
1020 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1021 this.stationInfo
.firmwareVersion
?.split('.').length
;
1022 const match
= this.stationInfo
?.firmwareVersion
1023 ?.match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1024 ?.slice(1, patternGroup
+ 1);
1025 const patchLevelIndex
= match
.length
- 1;
1026 match
[patchLevelIndex
] = (
1027 Utils
.convertToInt(match
[patchLevelIndex
]) +
1028 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
1030 this.stationInfo
.firmwareVersion
= match
?.join('.');
1034 private initializeOcppServices(): void {
1035 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
1036 switch (ocppVersion
) {
1037 case OCPPVersion
.VERSION_16
:
1038 this.ocppIncomingRequestService
=
1039 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
1040 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
1041 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
1044 case OCPPVersion
.VERSION_20
:
1045 case OCPPVersion
.VERSION_201
:
1046 this.ocppIncomingRequestService
=
1047 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
1048 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
1049 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
1053 this.handleUnsupportedVersion(ocppVersion
);
1058 private initializeOcppConfiguration(): void {
1060 !ChargingStationConfigurationUtils
.getConfigurationKey(
1062 StandardParametersKey
.HeartbeatInterval
1065 ChargingStationConfigurationUtils
.addConfigurationKey(
1067 StandardParametersKey
.HeartbeatInterval
,
1072 !ChargingStationConfigurationUtils
.getConfigurationKey(
1074 StandardParametersKey
.HeartBeatInterval
1077 ChargingStationConfigurationUtils
.addConfigurationKey(
1079 StandardParametersKey
.HeartBeatInterval
,
1085 this.getSupervisionUrlOcppConfiguration() &&
1086 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1088 ChargingStationConfigurationUtils
.addConfigurationKey(
1090 this.getSupervisionUrlOcppKey(),
1091 this.configuredSupervisionUrl
.href
,
1095 !this.getSupervisionUrlOcppConfiguration() &&
1096 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1098 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1100 this.getSupervisionUrlOcppKey(),
1105 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1106 !ChargingStationConfigurationUtils
.getConfigurationKey(
1108 this.stationInfo
.amperageLimitationOcppKey
1111 ChargingStationConfigurationUtils
.addConfigurationKey(
1113 this.stationInfo
.amperageLimitationOcppKey
,
1115 this.stationInfo
.maximumAmperage
*
1116 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1121 !ChargingStationConfigurationUtils
.getConfigurationKey(
1123 StandardParametersKey
.SupportedFeatureProfiles
1126 ChargingStationConfigurationUtils
.addConfigurationKey(
1128 StandardParametersKey
.SupportedFeatureProfiles
,
1129 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1132 ChargingStationConfigurationUtils
.addConfigurationKey(
1134 StandardParametersKey
.NumberOfConnectors
,
1135 this.getNumberOfConnectors().toString(),
1140 !ChargingStationConfigurationUtils
.getConfigurationKey(
1142 StandardParametersKey
.MeterValuesSampledData
1145 ChargingStationConfigurationUtils
.addConfigurationKey(
1147 StandardParametersKey
.MeterValuesSampledData
,
1148 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1152 !ChargingStationConfigurationUtils
.getConfigurationKey(
1154 StandardParametersKey
.ConnectorPhaseRotation
1157 const connectorPhaseRotation
= [];
1158 for (const connectorId
of this.connectors
.keys()) {
1160 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
1161 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1162 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
1163 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1165 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
1166 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1167 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
1168 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1171 ChargingStationConfigurationUtils
.addConfigurationKey(
1173 StandardParametersKey
.ConnectorPhaseRotation
,
1174 connectorPhaseRotation
.toString()
1178 !ChargingStationConfigurationUtils
.getConfigurationKey(
1180 StandardParametersKey
.AuthorizeRemoteTxRequests
1183 ChargingStationConfigurationUtils
.addConfigurationKey(
1185 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1190 !ChargingStationConfigurationUtils
.getConfigurationKey(
1192 StandardParametersKey
.LocalAuthListEnabled
1194 ChargingStationConfigurationUtils
.getConfigurationKey(
1196 StandardParametersKey
.SupportedFeatureProfiles
1197 )?.value
?.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1199 ChargingStationConfigurationUtils
.addConfigurationKey(
1201 StandardParametersKey
.LocalAuthListEnabled
,
1206 !ChargingStationConfigurationUtils
.getConfigurationKey(
1208 StandardParametersKey
.ConnectionTimeOut
1211 ChargingStationConfigurationUtils
.addConfigurationKey(
1213 StandardParametersKey
.ConnectionTimeOut
,
1214 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1217 this.saveOcppConfiguration();
1220 private initializeConnectors(
1221 stationInfo
: ChargingStationInfo
,
1222 configuredMaxConnectors
: number,
1223 templateMaxConnectors
: number
1225 if (!stationInfo
?.Connectors
&& this.connectors
.size
=== 0) {
1226 const logMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1227 logger
.error(`${this.logPrefix()} ${logMsg}`);
1228 throw new BaseError(logMsg
);
1230 if (!stationInfo
?.Connectors
[0]) {
1232 `${this.logPrefix()} Charging station information from template ${
1234 } with no connector Id 0 configuration`
1237 if (stationInfo
?.Connectors
) {
1238 const connectorsConfigHash
= crypto
1239 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1240 .update(`${JSON.stringify(stationInfo?.Connectors)}${configuredMaxConnectors.toString()}`)
1242 const connectorsConfigChanged
=
1243 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1244 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1245 connectorsConfigChanged
&& this.connectors
.clear();
1246 this.connectorsConfigurationHash
= connectorsConfigHash
;
1247 // Add connector Id 0
1248 let lastConnector
= '0';
1249 for (lastConnector
in stationInfo
?.Connectors
) {
1250 const connectorStatus
= stationInfo
?.Connectors
[lastConnector
];
1251 const lastConnectorId
= Utils
.convertToInt(lastConnector
);
1253 lastConnectorId
=== 0 &&
1254 this.getUseConnectorId0(stationInfo
) === true &&
1257 this.checkStationInfoConnectorStatus(lastConnectorId
, connectorStatus
);
1258 this.connectors
.set(
1260 Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
)
1262 this.getConnectorStatus(lastConnectorId
).availability
= AvailabilityType
.OPERATIVE
;
1263 if (Utils
.isUndefined(this.getConnectorStatus(lastConnectorId
)?.chargingProfiles
)) {
1264 this.getConnectorStatus(lastConnectorId
).chargingProfiles
= [];
1268 // Generate all connectors
1269 if ((stationInfo
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) > 0) {
1270 for (let index
= 1; index
<= configuredMaxConnectors
; index
++) {
1271 const randConnectorId
= stationInfo
?.randomConnectors
1272 ? Utils
.getRandomInteger(Utils
.convertToInt(lastConnector
), 1)
1274 const connectorStatus
= stationInfo
?.Connectors
[randConnectorId
.toString()];
1275 this.checkStationInfoConnectorStatus(randConnectorId
, connectorStatus
);
1276 this.connectors
.set(index
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1277 this.getConnectorStatus(index
).availability
= AvailabilityType
.OPERATIVE
;
1278 if (Utils
.isUndefined(this.getConnectorStatus(index
)?.chargingProfiles
)) {
1279 this.getConnectorStatus(index
).chargingProfiles
= [];
1286 `${this.logPrefix()} Charging station information from template ${
1288 } with no connectors configuration defined, using already defined connectors`
1291 // Initialize transaction attributes on connectors
1292 for (const connectorId
of this.connectors
.keys()) {
1293 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1295 `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
1296 this.getConnectorStatus(connectorId)?.transactionId
1302 (this.getConnectorStatus(connectorId
)?.transactionStarted
=== undefined ||
1303 this.getConnectorStatus(connectorId
)?.transactionStarted
=== null)
1305 this.initializeConnectorStatus(connectorId
);
1310 private checkStationInfoConnectorStatus(
1311 connectorId
: number,
1312 connectorStatus
: ConnectorStatus
1314 if (!Utils
.isNullOrUndefined(connectorStatus
?.status)) {
1316 `${this.logPrefix()} Charging station information from template ${
1318 } with connector ${connectorId} status configuration defined, undefine it`
1320 connectorStatus
.status = undefined;
1324 private getConfigurationFromFile(): ChargingStationConfiguration
| undefined {
1325 let configuration
: ChargingStationConfiguration
| undefined;
1326 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1328 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1329 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1330 this.configurationFileHash
1333 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1334 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1335 configuration
= JSON
.parse(
1336 fs
.readFileSync(this.configurationFile
, 'utf8')
1337 ) as ChargingStationConfiguration
;
1338 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1339 this.configurationFileHash
= configuration
.configurationHash
;
1340 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1343 FileUtils
.handleFileException(
1344 this.configurationFile
,
1345 FileType
.ChargingStationConfiguration
,
1346 error
as NodeJS
.ErrnoException
,
1351 return configuration
;
1354 private saveConfiguration(): void {
1355 if (this.configurationFile
) {
1357 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1358 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1360 const configurationData
: ChargingStationConfiguration
=
1361 Utils
.cloneObject(this.getConfigurationFromFile()) ?? Constants
.EMPTY_OBJECT
;
1362 this.ocppConfiguration
?.configurationKey
&&
1363 (configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
);
1364 this.stationInfo
&& (configurationData
.stationInfo
= this.stationInfo
);
1365 delete configurationData
.configurationHash
;
1366 const configurationHash
= crypto
1367 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1368 .update(JSON
.stringify(configurationData
))
1370 if (this.configurationFileHash
!== configurationHash
) {
1371 configurationData
.configurationHash
= configurationHash
;
1372 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1373 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1374 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1375 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1376 fs
.closeSync(fileDescriptor
);
1377 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1378 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1379 this.configurationFileHash
= configurationHash
;
1380 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1383 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1384 this.configurationFile
1389 FileUtils
.handleFileException(
1390 this.configurationFile
,
1391 FileType
.ChargingStationConfiguration
,
1392 error
as NodeJS
.ErrnoException
,
1398 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1403 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| undefined {
1404 return this.getTemplateFromFile()?.Configuration
;
1407 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| undefined {
1408 let configuration
: ChargingStationConfiguration
| undefined;
1409 if (this.getOcppPersistentConfiguration() === true) {
1410 const configurationFromFile
= this.getConfigurationFromFile();
1411 configuration
= configurationFromFile
?.configurationKey
&& configurationFromFile
;
1413 if (!Utils
.isNullOrUndefined(configuration
)) {
1414 delete configuration
.stationInfo
;
1415 delete configuration
.configurationHash
;
1417 return configuration
;
1420 private getOcppConfiguration(): ChargingStationOcppConfiguration
| undefined {
1421 let ocppConfiguration
: ChargingStationOcppConfiguration
| undefined =
1422 this.getOcppConfigurationFromFile();
1423 if (!ocppConfiguration
) {
1424 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1426 return ocppConfiguration
;
1429 private async onOpen(): Promise
<void> {
1430 if (this.isWebSocketConnectionOpened() === true) {
1432 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1434 if (this.isRegistered() === false) {
1435 // Send BootNotification
1436 let registrationRetryCount
= 0;
1438 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1439 BootNotificationRequest
,
1440 BootNotificationResponse
1441 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1442 skipBufferingOnError
: true,
1444 if (this.isRegistered() === false) {
1445 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1447 this?.bootNotificationResponse
?.interval
1448 ? this.bootNotificationResponse
.interval
* 1000
1449 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1453 this.isRegistered() === false &&
1454 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1455 this.getRegistrationMaxRetries() === -1)
1458 if (this.isRegistered() === true) {
1459 if (this.isInAcceptedState() === true) {
1460 await this.startMessageSequence();
1464 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1467 this.wsConnectionRestarted
= false;
1468 this.autoReconnectRetryCount
= 0;
1469 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1472 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1477 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1480 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1481 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1483 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1485 )}' and reason '${reason.toString()}'`
1487 this.autoReconnectRetryCount
= 0;
1492 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1494 )}' and reason '${reason.toString()}'`
1496 this.started
=== true && (await this.reconnect());
1499 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1502 private async onMessage(data
: RawData
): Promise
<void> {
1503 let messageType
: number;
1504 let messageId
: string;
1505 let commandName
: IncomingRequestCommand
;
1506 let commandPayload
: JsonType
;
1507 let errorType
: ErrorType
;
1508 let errorMessage
: string;
1509 let errorDetails
: JsonType
;
1510 let responseCallback
: ResponseCallback
;
1511 let errorCallback
: ErrorCallback
;
1512 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1513 let requestPayload
: JsonType
;
1514 let cachedRequest
: CachedRequest
;
1517 const request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1518 if (Array.isArray(request
) === true) {
1519 [messageType
, messageId
] = request
;
1520 // Check the type of message
1521 switch (messageType
) {
1523 case MessageType
.CALL_MESSAGE
:
1524 [, , commandName
, commandPayload
] = request
as IncomingRequest
;
1525 if (this.getEnableStatistics() === true) {
1526 this.performanceStatistics
?.addRequestStatistic(commandName
, messageType
);
1529 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1533 // Process the message
1534 await this.ocppIncomingRequestService
.incomingRequestHandler(
1542 case MessageType
.CALL_RESULT_MESSAGE
:
1543 [, , commandPayload
] = request
as Response
;
1544 if (this.requests
.has(messageId
) === false) {
1546 throw new OCPPError(
1547 ErrorType
.INTERNAL_ERROR
,
1548 `Response for unknown message id ${messageId}`,
1554 cachedRequest
= this.requests
.get(messageId
);
1555 if (Array.isArray(cachedRequest
) === true) {
1556 [responseCallback
, errorCallback
, requestCommandName
, requestPayload
] = cachedRequest
;
1558 throw new OCPPError(
1559 ErrorType
.PROTOCOL_ERROR
,
1560 `Cached request for message id ${messageId} response is not an array`,
1562 cachedRequest
as unknown
as JsonType
1566 `${this.logPrefix()} << Command '${
1567 requestCommandName ?? Constants.UNKNOWN_COMMAND
1568 }' received response payload: ${JSON.stringify(request)}`
1570 responseCallback(commandPayload
, requestPayload
);
1573 case MessageType
.CALL_ERROR_MESSAGE
:
1574 [, , errorType
, errorMessage
, errorDetails
] = request
as ErrorResponse
;
1575 if (this.requests
.has(messageId
) === false) {
1577 throw new OCPPError(
1578 ErrorType
.INTERNAL_ERROR
,
1579 `Error response for unknown message id ${messageId}`,
1581 { errorType
, errorMessage
, errorDetails
}
1584 cachedRequest
= this.requests
.get(messageId
);
1585 if (Array.isArray(cachedRequest
) === true) {
1586 [, errorCallback
, requestCommandName
] = cachedRequest
;
1588 throw new OCPPError(
1589 ErrorType
.PROTOCOL_ERROR
,
1590 `Cached request for message id ${messageId} error response is not an array`,
1592 cachedRequest
as unknown
as JsonType
1596 `${this.logPrefix()} << Command '${
1597 requestCommandName ?? Constants.UNKNOWN_COMMAND
1598 }' received error response payload: ${JSON.stringify(request)}`
1600 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1604 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1605 errMsg
= `Wrong message type ${messageType}`;
1606 logger
.error(`${this.logPrefix()} ${errMsg}`);
1607 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1609 parentPort
?.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1611 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1618 `${this.logPrefix()} Incoming OCPP command '${
1619 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1620 }' message '${data.toString()}'${
1621 messageType !== MessageType.CALL_MESSAGE
1622 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1624 } processing error:`,
1627 if (error
instanceof OCPPError
=== false) {
1629 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1630 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1631 }' message '${data.toString()}' handling is not an OCPPError:`,
1635 switch (messageType
) {
1636 case MessageType
.CALL_MESSAGE
:
1638 await this.ocppRequestService
.sendError(
1642 commandName
?? requestCommandName
?? null
1645 case MessageType
.CALL_RESULT_MESSAGE
:
1646 case MessageType
.CALL_ERROR_MESSAGE
:
1647 if (errorCallback
) {
1648 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1649 errorCallback(error
as OCPPError
, false);
1651 // Remove the request from the cache in case of error at response handling
1652 this.requests
.delete(messageId
);
1659 private onPing(): void {
1660 logger
.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
1663 private onPong(): void {
1664 logger
.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
1667 private onError(error
: WSError
): void {
1668 this.closeWSConnection();
1669 logger
.error(`${this.logPrefix()} WebSocket error:`, error
);
1672 private getEnergyActiveImportRegister(connectorStatus
: ConnectorStatus
, rounded
= false): number {
1673 if (this.getMeteringPerTransaction() === true) {
1676 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1677 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1682 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1683 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1687 private getUseConnectorId0(stationInfo
?: ChargingStationInfo
): boolean {
1688 const localStationInfo
= stationInfo
?? this.stationInfo
;
1689 return localStationInfo
?.useConnectorId0
?? true;
1692 private getNumberOfRunningTransactions(): number {
1694 for (const connectorId
of this.connectors
.keys()) {
1695 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1702 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1703 for (const connectorId
of this.connectors
.keys()) {
1704 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1705 await this.stopTransactionOnConnector(connectorId
, reason
);
1711 private getConnectionTimeout(): number {
1713 ChargingStationConfigurationUtils
.getConfigurationKey(
1715 StandardParametersKey
.ConnectionTimeOut
1720 ChargingStationConfigurationUtils
.getConfigurationKey(
1722 StandardParametersKey
.ConnectionTimeOut
1724 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1727 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1730 // -1 for unlimited, 0 for disabling
1731 private getAutoReconnectMaxRetries(): number | undefined {
1732 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1733 return this.stationInfo
.autoReconnectMaxRetries
;
1735 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1736 return Configuration
.getAutoReconnectMaxRetries();
1742 private getRegistrationMaxRetries(): number | undefined {
1743 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1744 return this.stationInfo
.registrationMaxRetries
;
1749 private getPowerDivider(): number {
1750 let powerDivider
= this.getNumberOfConnectors();
1751 if (this.stationInfo
?.powerSharedByConnectors
) {
1752 powerDivider
= this.getNumberOfRunningTransactions();
1754 return powerDivider
;
1757 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1758 const maximumPower
= this.getMaximumPower(stationInfo
);
1759 switch (this.getCurrentOutType(stationInfo
)) {
1760 case CurrentType
.AC
:
1761 return ACElectricUtils
.amperagePerPhaseFromPower(
1762 this.getNumberOfPhases(stationInfo
),
1763 maximumPower
/ this.getNumberOfConnectors(),
1764 this.getVoltageOut(stationInfo
)
1766 case CurrentType
.DC
:
1767 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1771 private getAmperageLimitation(): number | undefined {
1773 Utils
.isNotEmptyString(this.stationInfo
?.amperageLimitationOcppKey
) &&
1774 ChargingStationConfigurationUtils
.getConfigurationKey(
1776 this.stationInfo
.amperageLimitationOcppKey
1781 ChargingStationConfigurationUtils
.getConfigurationKey(
1783 this.stationInfo
.amperageLimitationOcppKey
1785 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1790 private async startMessageSequence(): Promise
<void> {
1791 if (this.stationInfo
?.autoRegister
=== true) {
1792 await this.ocppRequestService
.requestHandler
<
1793 BootNotificationRequest
,
1794 BootNotificationResponse
1795 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1796 skipBufferingOnError
: true,
1799 // Start WebSocket ping
1800 this.startWebSocketPing();
1802 this.startHeartbeat();
1803 // Initialize connectors status
1804 for (const connectorId
of this.connectors
.keys()) {
1805 let connectorStatus
: ConnectorStatusEnum
| undefined;
1806 if (connectorId
=== 0) {
1809 !this.getConnectorStatus(connectorId
)?.status &&
1810 (this.isChargingStationAvailable() === false ||
1811 this.isConnectorAvailable(connectorId
) === false)
1813 connectorStatus
= ConnectorStatusEnum
.Unavailable
;
1815 !this.getConnectorStatus(connectorId
)?.status &&
1816 this.getConnectorStatus(connectorId
)?.bootStatus
1818 // Set boot status in template at startup
1819 connectorStatus
= this.getConnectorStatus(connectorId
)?.bootStatus
;
1820 } else if (this.getConnectorStatus(connectorId
)?.status) {
1821 // Set previous status at startup
1822 connectorStatus
= this.getConnectorStatus(connectorId
)?.status;
1824 // Set default status
1825 connectorStatus
= ConnectorStatusEnum
.Available
;
1827 await this.ocppRequestService
.requestHandler
<
1828 StatusNotificationRequest
,
1829 StatusNotificationResponse
1832 RequestCommand
.STATUS_NOTIFICATION
,
1833 OCPPServiceUtils
.buildStatusNotificationRequest(this, connectorId
, connectorStatus
)
1835 this.getConnectorStatus(connectorId
).status = connectorStatus
;
1837 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
1838 await this.ocppRequestService
.requestHandler
<
1839 FirmwareStatusNotificationRequest
,
1840 FirmwareStatusNotificationResponse
1841 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1842 status: FirmwareStatus
.Installed
,
1844 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
1848 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true) {
1849 this.startAutomaticTransactionGenerator();
1851 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
1854 private async stopMessageSequence(
1855 reason
: StopTransactionReason
= StopTransactionReason
.NONE
1857 // Stop WebSocket ping
1858 this.stopWebSocketPing();
1860 this.stopHeartbeat();
1861 // Stop ongoing transactions
1862 if (this.automaticTransactionGenerator
?.started
=== true) {
1863 this.stopAutomaticTransactionGenerator();
1865 await this.stopRunningTransactions(reason
);
1867 for (const connectorId
of this.connectors
.keys()) {
1868 if (connectorId
> 0) {
1869 await this.ocppRequestService
.requestHandler
<
1870 StatusNotificationRequest
,
1871 StatusNotificationResponse
1874 RequestCommand
.STATUS_NOTIFICATION
,
1875 OCPPServiceUtils
.buildStatusNotificationRequest(
1878 ConnectorStatusEnum
.Unavailable
1881 this.getConnectorStatus(connectorId
).status = undefined;
1886 private startWebSocketPing(): void {
1887 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
1889 StandardParametersKey
.WebSocketPingInterval
1891 ? Utils
.convertToInt(
1892 ChargingStationConfigurationUtils
.getConfigurationKey(
1894 StandardParametersKey
.WebSocketPingInterval
1898 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
1899 this.webSocketPingSetInterval
= setInterval(() => {
1900 if (this.isWebSocketConnectionOpened() === true) {
1901 this.wsConnection
?.ping();
1903 }, webSocketPingInterval
* 1000);
1905 `${this.logPrefix()} WebSocket ping started every ${Utils.formatDurationSeconds(
1906 webSocketPingInterval
1909 } else if (this.webSocketPingSetInterval
) {
1911 `${this.logPrefix()} WebSocket ping already started every ${Utils.formatDurationSeconds(
1912 webSocketPingInterval
1917 `${this.logPrefix()} WebSocket ping interval set to ${
1918 webSocketPingInterval
1919 ? Utils.formatDurationSeconds(webSocketPingInterval)
1920 : webSocketPingInterval
1921 }, not starting the WebSocket ping`
1926 private stopWebSocketPing(): void {
1927 if (this.webSocketPingSetInterval
) {
1928 clearInterval(this.webSocketPingSetInterval
);
1932 private getConfiguredSupervisionUrl(): URL
{
1933 const supervisionUrls
= this.stationInfo
?.supervisionUrls
?? Configuration
.getSupervisionUrls();
1934 if (Utils
.isNotEmptyArray(supervisionUrls
)) {
1935 switch (Configuration
.getSupervisionUrlDistribution()) {
1936 case SupervisionUrlDistribution
.RANDOM
:
1937 this.configuredSupervisionUrlIndex
= Math.floor(
1938 Utils
.secureRandom() * supervisionUrls
.length
1941 case SupervisionUrlDistribution
.ROUND_ROBIN
:
1942 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
1944 Object.values(SupervisionUrlDistribution
).includes(
1945 Configuration
.getSupervisionUrlDistribution()
1948 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
1949 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
1952 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1955 return new URL(supervisionUrls
[this.configuredSupervisionUrlIndex
]);
1957 return new URL(supervisionUrls
as string);
1960 private getHeartbeatInterval(): number {
1961 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1963 StandardParametersKey
.HeartbeatInterval
1965 if (HeartbeatInterval
) {
1966 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
1968 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1970 StandardParametersKey
.HeartBeatInterval
1972 if (HeartBeatInterval
) {
1973 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
1975 this.stationInfo
?.autoRegister
=== false &&
1977 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
1978 Constants.DEFAULT_HEARTBEAT_INTERVAL
1981 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
1984 private stopHeartbeat(): void {
1985 if (this.heartbeatSetInterval
) {
1986 clearInterval(this.heartbeatSetInterval
);
1990 private terminateWSConnection(): void {
1991 if (this.isWebSocketConnectionOpened() === true) {
1992 this.wsConnection
?.terminate();
1993 this.wsConnection
= null;
1997 private stopMeterValues(connectorId
: number) {
1998 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
1999 clearInterval(this.getConnectorStatus(connectorId
)?.transactionSetInterval
);
2003 private getReconnectExponentialDelay(): boolean {
2004 return this.stationInfo
?.reconnectExponentialDelay
?? false;
2007 private async reconnect(): Promise
<void> {
2008 // Stop WebSocket ping
2009 this.stopWebSocketPing();
2011 this.stopHeartbeat();
2012 // Stop the ATG if needed
2013 if (this.automaticTransactionGenerator
?.configuration
?.stopOnConnectionFailure
=== true) {
2014 this.stopAutomaticTransactionGenerator();
2017 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2018 this.getAutoReconnectMaxRetries() === -1
2020 this.autoReconnectRetryCount
++;
2021 const reconnectDelay
= this.getReconnectExponentialDelay()
2022 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2023 : this.getConnectionTimeout() * 1000;
2024 const reconnectDelayWithdraw
= 1000;
2025 const reconnectTimeout
=
2026 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2027 ? reconnectDelay
- reconnectDelayWithdraw
2030 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2033 )}ms, timeout ${reconnectTimeout}ms`
2035 await Utils
.sleep(reconnectDelay
);
2037 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`
2039 this.openWSConnection(
2041 ...(this.stationInfo
?.wsOptions
?? Constants
.EMPTY_OBJECT
),
2042 handshakeTimeout
: reconnectTimeout
,
2044 { closeOpened
: true }
2046 this.wsConnectionRestarted
= true;
2047 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2049 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2050 this.autoReconnectRetryCount
2051 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2056 private getAutomaticTransactionGeneratorConfigurationFromTemplate():
2057 | AutomaticTransactionGeneratorConfiguration
2059 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
;
2062 private initializeConnectorStatus(connectorId
: number): void {
2063 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
2064 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
2065 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
2066 this.getConnectorStatus(connectorId
).transactionStarted
= false;
2067 this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
= 0;
2068 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;