1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import crypto from
'crypto';
5 import path from
'path';
6 import { URL
} from
'url';
7 import { parentPort
} from
'worker_threads';
9 import merge from
'just-merge';
10 import WebSocket
, { type RawData
} from
'ws';
12 import AuthorizedTagsCache from
'./AuthorizedTagsCache';
13 import AutomaticTransactionGenerator from
'./AutomaticTransactionGenerator';
14 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
15 import { ChargingStationUtils
} from
'./ChargingStationUtils';
16 import ChargingStationWorkerBroadcastChannel from
'./ChargingStationWorkerBroadcastChannel';
17 import { MessageChannelUtils
} from
'./MessageChannelUtils';
18 import OCPP16IncomingRequestService from
'./ocpp/1.6/OCPP16IncomingRequestService';
19 import OCPP16RequestService from
'./ocpp/1.6/OCPP16RequestService';
20 import OCPP16ResponseService from
'./ocpp/1.6/OCPP16ResponseService';
21 import { OCPP16ServiceUtils
} from
'./ocpp/1.6/OCPP16ServiceUtils';
22 import OCPP20IncomingRequestService from
'./ocpp/2.0/OCPP20IncomingRequestService';
23 import OCPP20RequestService from
'./ocpp/2.0/OCPP20RequestService';
24 import OCPP20ResponseService from
'./ocpp/2.0/OCPP20ResponseService';
25 import type OCPPIncomingRequestService from
'./ocpp/OCPPIncomingRequestService';
26 import type OCPPRequestService from
'./ocpp/OCPPRequestService';
27 import { OCPPServiceUtils
} from
'./ocpp/OCPPServiceUtils';
28 import SharedLRUCache from
'./SharedLRUCache';
29 import BaseError from
'../exception/BaseError';
30 import OCPPError from
'../exception/OCPPError';
31 import PerformanceStatistics from
'../performance/PerformanceStatistics';
32 import type { AutomaticTransactionGeneratorConfiguration
} from
'../types/AutomaticTransactionGenerator';
33 import type { ChargingStationConfiguration
} from
'../types/ChargingStationConfiguration';
34 import type { ChargingStationInfo
} from
'../types/ChargingStationInfo';
35 import type { ChargingStationOcppConfiguration
} from
'../types/ChargingStationOcppConfiguration';
37 type ChargingStationTemplate
,
42 } from
'../types/ChargingStationTemplate';
43 import { SupervisionUrlDistribution
} from
'../types/ConfigurationData';
44 import type { ConnectorStatus
} from
'../types/ConnectorStatus';
45 import { FileType
} from
'../types/FileType';
46 import type { JsonType
} from
'../types/JsonType';
47 import { ChargingProfile
, ChargingRateUnitType
} from
'../types/ocpp/ChargingProfile';
49 ConnectorPhaseRotation
,
50 StandardParametersKey
,
51 SupportedFeatureProfiles
,
52 VendorDefaultParametersKey
,
53 } from
'../types/ocpp/Configuration';
54 import { ConnectorStatusEnum
} from
'../types/ocpp/ConnectorStatusEnum';
55 import { ErrorType
} from
'../types/ocpp/ErrorType';
56 import { MessageType
} from
'../types/ocpp/MessageType';
57 import { MeterValue
, MeterValueMeasurand
} from
'../types/ocpp/MeterValues';
58 import { OCPPVersion
} from
'../types/ocpp/OCPPVersion';
61 type BootNotificationRequest
,
65 type FirmwareStatusNotificationRequest
,
66 type HeartbeatRequest
,
68 IncomingRequestCommand
,
69 type MeterValuesRequest
,
71 type ResponseCallback
,
72 type StatusNotificationRequest
,
73 } from
'../types/ocpp/Requests';
75 type BootNotificationResponse
,
77 type FirmwareStatusNotificationResponse
,
78 type HeartbeatResponse
,
79 type MeterValuesResponse
,
80 RegistrationStatusEnumType
,
82 type StatusNotificationResponse
,
83 } from
'../types/ocpp/Responses';
85 StopTransactionReason
,
86 type StopTransactionRequest
,
87 type StopTransactionResponse
,
88 } from
'../types/ocpp/Transaction';
89 import { WSError
, WebSocketCloseEventStatusCode
} from
'../types/WebSocket';
90 import Configuration from
'../utils/Configuration';
91 import Constants from
'../utils/Constants';
92 import { ACElectricUtils
, DCElectricUtils
} from
'../utils/ElectricUtils';
93 import FileUtils from
'../utils/FileUtils';
94 import logger from
'../utils/Logger';
95 import Utils from
'../utils/Utils';
97 export default class ChargingStation
{
98 public readonly index
: number;
99 public readonly templateFile
: string;
100 public stationInfo
!: ChargingStationInfo
;
101 public started
: boolean;
102 public starting
: boolean;
103 public authorizedTagsCache
: AuthorizedTagsCache
;
104 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
;
105 public ocppConfiguration
!: ChargingStationOcppConfiguration
;
106 public wsConnection
!: WebSocket
;
107 public readonly connectors
: Map
<number, ConnectorStatus
>;
108 public readonly requests
: Map
<string, CachedRequest
>;
109 public performanceStatistics
!: PerformanceStatistics
;
110 public heartbeatSetInterval
!: NodeJS
.Timeout
;
111 public ocppRequestService
!: OCPPRequestService
;
112 public bootNotificationRequest
!: BootNotificationRequest
;
113 public bootNotificationResponse
!: BootNotificationResponse
| null;
114 public powerDivider
!: number;
115 private stopping
: boolean;
116 private configurationFile
!: string;
117 private configurationFileHash
!: string;
118 private connectorsConfigurationHash
!: string;
119 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
120 private readonly messageBuffer
: Set
<string>;
121 private configuredSupervisionUrl
!: URL
;
122 private configuredSupervisionUrlIndex
!: number;
123 private wsConnectionRestarted
: boolean;
124 private autoReconnectRetryCount
: number;
125 private templateFileWatcher
!: fs
.FSWatcher
;
126 private readonly sharedLRUCache
: SharedLRUCache
;
127 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
128 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
130 constructor(index
: number, templateFile
: string) {
131 this.started
= false;
132 this.starting
= false;
133 this.stopping
= false;
134 this.wsConnectionRestarted
= false;
135 this.autoReconnectRetryCount
= 0;
137 this.templateFile
= templateFile
;
138 this.connectors
= new Map
<number, ConnectorStatus
>();
139 this.requests
= new Map
<string, CachedRequest
>();
140 this.messageBuffer
= new Set
<string>();
141 this.sharedLRUCache
= SharedLRUCache
.getInstance();
142 this.authorizedTagsCache
= AuthorizedTagsCache
.getInstance();
143 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
148 private get
wsConnectionUrl(): URL
{
150 (this.getSupervisionUrlOcppConfiguration()
151 ? ChargingStationConfigurationUtils
.getConfigurationKey(
153 this.getSupervisionUrlOcppKey()
155 : this.configuredSupervisionUrl
.href
) +
157 this.stationInfo
.chargingStationId
161 public logPrefix(): string {
162 return Utils
.logPrefix(
164 this?.stationInfo?.chargingStationId ??
165 ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())
170 public hasAuthorizedTags(): boolean {
171 return !Utils
.isEmptyArray(
172 this.authorizedTagsCache
.getAuthorizedTags(
173 ChargingStationUtils
.getAuthorizationFile(this.stationInfo
)
178 public getEnableStatistics(): boolean {
179 return this.stationInfo
.enableStatistics
?? false;
182 public getMustAuthorizeAtRemoteStart(): boolean {
183 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
186 public getPayloadSchemaValidation(): boolean {
187 return this.stationInfo
.payloadSchemaValidation
?? true;
190 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
191 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
192 switch (this.getCurrentOutType(stationInfo
)) {
194 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
195 ? localStationInfo
.numberOfPhases
202 public isWebSocketConnectionOpened(): boolean {
203 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
206 public getRegistrationStatus(): RegistrationStatusEnumType
{
207 return this?.bootNotificationResponse
?.status;
210 public isInUnknownState(): boolean {
211 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
214 public isInPendingState(): boolean {
215 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.PENDING
;
218 public isInAcceptedState(): boolean {
219 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.ACCEPTED
;
222 public isInRejectedState(): boolean {
223 return this?.bootNotificationResponse
?.status === RegistrationStatusEnumType
.REJECTED
;
226 public isRegistered(): boolean {
228 this.isInUnknownState() === false &&
229 (this.isInAcceptedState() === true || this.isInPendingState() === true)
233 public isChargingStationAvailable(): boolean {
234 return this.getConnectorStatus(0).availability
=== AvailabilityType
.OPERATIVE
;
237 public isConnectorAvailable(id
: number): boolean {
238 return id
> 0 && this.getConnectorStatus(id
).availability
=== AvailabilityType
.OPERATIVE
;
241 public getNumberOfConnectors(): number {
242 return this.connectors
.get(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
245 public getConnectorStatus(id
: number): ConnectorStatus
| undefined {
246 return this.connectors
.get(id
);
249 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
250 return (stationInfo
?? this.stationInfo
).currentOutType
?? CurrentType
.AC
;
253 public getOcppStrictCompliance(): boolean {
254 return this.stationInfo
?.ocppStrictCompliance
?? false;
257 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
258 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
259 this.getCurrentOutType(stationInfo
),
263 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
264 return !Utils
.isUndefined(localStationInfo
.voltageOut
)
265 ? localStationInfo
.voltageOut
269 public getConnectorMaximumAvailablePower(connectorId
: number): number {
270 let connectorAmperageLimitationPowerLimit
: number;
272 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
273 this.getAmperageLimitation() < this.stationInfo
.maximumAmperage
275 connectorAmperageLimitationPowerLimit
=
276 (this.getCurrentOutType() === CurrentType
.AC
277 ? ACElectricUtils
.powerTotal(
278 this.getNumberOfPhases(),
279 this.getVoltageOut(),
280 this.getAmperageLimitation() * this.getNumberOfConnectors()
282 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
285 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
286 const connectorChargingProfilePowerLimit
= this.getChargingProfilePowerLimit(connectorId
);
288 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
289 isNaN(connectorAmperageLimitationPowerLimit
)
291 : connectorAmperageLimitationPowerLimit
,
292 isNaN(connectorChargingProfilePowerLimit
) ? Infinity : connectorChargingProfilePowerLimit
296 public getTransactionIdTag(transactionId
: number): string | undefined {
297 for (const connectorId
of this.connectors
.keys()) {
298 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionId
=== transactionId
) {
299 return this.getConnectorStatus(connectorId
).transactionIdTag
;
304 public getOutOfOrderEndMeterValues(): boolean {
305 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
308 public getBeginEndMeterValues(): boolean {
309 return this.stationInfo
?.beginEndMeterValues
?? false;
312 public getMeteringPerTransaction(): boolean {
313 return this.stationInfo
?.meteringPerTransaction
?? true;
316 public getTransactionDataMeterValues(): boolean {
317 return this.stationInfo
?.transactionDataMeterValues
?? false;
320 public getMainVoltageMeterValues(): boolean {
321 return this.stationInfo
?.mainVoltageMeterValues
?? true;
324 public getPhaseLineToLineVoltageMeterValues(): boolean {
325 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
328 public getCustomValueLimitationMeterValues(): boolean {
329 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
332 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
333 for (const connectorId
of this.connectors
.keys()) {
336 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
343 public getEnergyActiveImportRegisterByTransactionId(
344 transactionId
: number,
347 return this.getEnergyActiveImportRegister(
348 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
353 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number): number {
354 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
));
357 public getAuthorizeRemoteTxRequests(): boolean {
358 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
360 StandardParametersKey
.AuthorizeRemoteTxRequests
362 return authorizeRemoteTxRequests
363 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
367 public getLocalAuthListEnabled(): boolean {
368 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
370 StandardParametersKey
.LocalAuthListEnabled
372 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
375 public startHeartbeat(): void {
377 this.getHeartbeatInterval() &&
378 this.getHeartbeatInterval() > 0 &&
379 !this.heartbeatSetInterval
381 // eslint-disable-next-line @typescript-eslint/no-misused-promises
382 this.heartbeatSetInterval
= setInterval(async (): Promise
<void> => {
383 await this.ocppRequestService
.requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(
385 RequestCommand
.HEARTBEAT
387 }, this.getHeartbeatInterval());
390 ' Heartbeat started every ' +
391 Utils
.formatDurationMilliSeconds(this.getHeartbeatInterval())
393 } else if (this.heartbeatSetInterval
) {
396 ' Heartbeat already started every ' +
397 Utils
.formatDurationMilliSeconds(this.getHeartbeatInterval())
401 `${this.logPrefix()} Heartbeat interval set to ${
402 this.getHeartbeatInterval()
403 ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
404 : this.getHeartbeatInterval()
405 }, not starting the heartbeat`
410 public restartHeartbeat(): void {
412 this.stopHeartbeat();
414 this.startHeartbeat();
417 public restartWebSocketPing(): void {
418 // Stop WebSocket ping
419 this.stopWebSocketPing();
420 // Start WebSocket ping
421 this.startWebSocketPing();
424 public startMeterValues(connectorId
: number, interval
: number): void {
425 if (connectorId
=== 0) {
427 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`
431 if (!this.getConnectorStatus(connectorId
)) {
433 `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`
437 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
439 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
443 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
444 !this.getConnectorStatus(connectorId
)?.transactionId
447 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`
452 // eslint-disable-next-line @typescript-eslint/no-misused-promises
453 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(
454 // eslint-disable-next-line @typescript-eslint/no-misused-promises
455 async (): Promise
<void> => {
456 // FIXME: Implement OCPP version agnostic helpers
457 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
460 this.getConnectorStatus(connectorId
).transactionId
,
463 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
465 RequestCommand
.METER_VALUES
,
468 transactionId
: this.getConnectorStatus(connectorId
).transactionId
,
469 meterValue
: [meterValue
],
477 `${this.logPrefix()} Charging station ${
478 StandardParametersKey.MeterValueSampleInterval
479 } configuration set to ${
480 interval ? Utils.formatDurationMilliSeconds(interval) : interval
481 }, not sending MeterValues`
486 public start(): void {
487 if (this.started
=== false) {
488 if (this.starting
=== false) {
489 this.starting
= true;
490 if (this.getEnableStatistics() === true) {
491 this.performanceStatistics
.start();
493 this.openWSConnection();
494 // Monitor charging station template file
495 this.templateFileWatcher
= FileUtils
.watchJsonFile(
497 FileType
.ChargingStationTemplate
,
500 (event
, filename
): void => {
501 if (filename
&& event
=== 'change') {
504 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
506 } file have changed, reload`
508 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
512 this.stopAutomaticTransactionGenerator();
514 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true
516 this.startAutomaticTransactionGenerator();
518 if (this.getEnableStatistics() === true) {
519 this.performanceStatistics
.restart();
521 this.performanceStatistics
.stop();
523 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
526 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
534 parentPort
.postMessage(MessageChannelUtils
.buildStartedMessage(this));
535 this.starting
= false;
537 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
540 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
544 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
545 if (this.started
=== true) {
546 if (this.stopping
=== false) {
547 this.stopping
= true;
548 await this.stopMessageSequence(reason
);
549 this.closeWSConnection();
550 if (this.getEnableStatistics() === true) {
551 this.performanceStatistics
.stop();
553 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
554 this.templateFileWatcher
.close();
555 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
556 this.bootNotificationResponse
= null;
557 this.started
= false;
558 parentPort
.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
559 this.stopping
= false;
561 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
564 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
568 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
569 await this.stop(reason
);
570 await Utils
.sleep(this.stationInfo
.resetTime
);
575 public saveOcppConfiguration(): void {
576 if (this.getOcppPersistentConfiguration()) {
577 this.saveConfiguration();
581 public resetConnectorStatus(connectorId
: number): void {
582 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
583 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
584 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
585 this.getConnectorStatus(connectorId
).transactionStarted
= false;
586 delete this.getConnectorStatus(connectorId
).localAuthorizeIdTag
;
587 delete this.getConnectorStatus(connectorId
).authorizeIdTag
;
588 delete this.getConnectorStatus(connectorId
).transactionId
;
589 delete this.getConnectorStatus(connectorId
).transactionIdTag
;
590 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;
591 delete this.getConnectorStatus(connectorId
).transactionBeginMeterValue
;
592 this.stopMeterValues(connectorId
);
593 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
596 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean {
597 return ChargingStationConfigurationUtils
.getConfigurationKey(
599 StandardParametersKey
.SupportedFeatureProfiles
600 )?.value
.includes(featureProfile
);
603 public bufferMessage(message
: string): void {
604 this.messageBuffer
.add(message
);
607 public openWSConnection(
608 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
609 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
611 terminateOpened
: false,
614 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
615 params
.closeOpened
= params
?.closeOpened
?? false;
616 params
.terminateOpened
= params
?.terminateOpened
?? false;
617 if (this.started
=== false && this.starting
=== false) {
619 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
624 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
625 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
627 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
629 if (params
?.closeOpened
) {
630 this.closeWSConnection();
632 if (params
?.terminateOpened
) {
633 this.terminateWSConnection();
635 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
636 let protocol
: string;
637 switch (ocppVersion
) {
638 case OCPPVersion
.VERSION_16
:
639 case OCPPVersion
.VERSION_20
:
640 case OCPPVersion
.VERSION_201
:
641 protocol
= 'ocpp' + ocppVersion
;
644 this.handleUnsupportedVersion(ocppVersion
);
648 if (this.isWebSocketConnectionOpened() === true) {
650 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
656 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
659 this.wsConnection
= new WebSocket(this.wsConnectionUrl
, protocol
, options
);
661 // Handle WebSocket message
662 this.wsConnection
.on(
664 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
666 // Handle WebSocket error
667 this.wsConnection
.on(
669 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
671 // Handle WebSocket close
672 this.wsConnection
.on(
674 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
676 // Handle WebSocket open
677 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
678 // Handle WebSocket ping
679 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
680 // Handle WebSocket pong
681 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
684 public closeWSConnection(): void {
685 if (this.isWebSocketConnectionOpened() === true) {
686 this.wsConnection
.close();
687 this.wsConnection
= null;
691 public startAutomaticTransactionGenerator(
692 connectorIds
?: number[],
693 automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
695 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(
696 automaticTransactionGeneratorConfiguration
??
697 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
700 if (!Utils
.isEmptyArray(connectorIds
)) {
701 for (const connectorId
of connectorIds
) {
702 this.automaticTransactionGenerator
.startConnector(connectorId
);
705 this.automaticTransactionGenerator
.start();
707 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
710 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
711 if (!Utils
.isEmptyArray(connectorIds
)) {
712 for (const connectorId
of connectorIds
) {
713 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
716 this.automaticTransactionGenerator
?.stop();
718 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
721 public async stopTransactionOnConnector(
723 reason
= StopTransactionReason
.NONE
724 ): Promise
<StopTransactionResponse
> {
725 const transactionId
= this.getConnectorStatus(connectorId
).transactionId
;
727 this.getBeginEndMeterValues() === true &&
728 this.getOcppStrictCompliance() === true &&
729 this.getOutOfOrderEndMeterValues() === false
731 // FIXME: Implement OCPP version agnostic helpers
732 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
735 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
737 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
739 RequestCommand
.METER_VALUES
,
743 meterValue
: [transactionEndMeterValue
],
747 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
749 RequestCommand
.STOP_TRANSACTION
,
752 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
758 private flushMessageBuffer(): void {
759 if (this.messageBuffer
.size
> 0) {
760 this.messageBuffer
.forEach((message
) => {
761 // TODO: evaluate the need to track performance
762 this.wsConnection
.send(message
);
763 this.messageBuffer
.delete(message
);
768 private getSupervisionUrlOcppConfiguration(): boolean {
769 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
772 private getSupervisionUrlOcppKey(): string {
773 return this.stationInfo
.supervisionUrlOcppKey
?? VendorDefaultParametersKey
.ConnectionUrl
;
776 private getTemplateFromFile(): ChargingStationTemplate
| null {
777 let template
: ChargingStationTemplate
= null;
779 if (this.sharedLRUCache
.hasChargingStationTemplate(this.stationInfo
?.templateHash
)) {
780 template
= this.sharedLRUCache
.getChargingStationTemplate(this.stationInfo
.templateHash
);
782 const measureId
= `${FileType.ChargingStationTemplate} read`;
783 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
784 template
= JSON
.parse(
785 fs
.readFileSync(this.templateFile
, 'utf8')
786 ) as ChargingStationTemplate
;
787 PerformanceStatistics
.endMeasure(measureId
, beginId
);
788 template
.templateHash
= crypto
789 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
790 .update(JSON
.stringify(template
))
792 this.sharedLRUCache
.setChargingStationTemplate(template
);
795 FileUtils
.handleFileException(
797 FileType
.ChargingStationTemplate
,
799 error
as NodeJS
.ErrnoException
805 private getStationInfoFromTemplate(): ChargingStationInfo
{
806 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile();
807 if (Utils
.isNullOrUndefined(stationTemplate
)) {
808 const errorMsg
= `Failed to read charging station template file ${this.templateFile}`;
809 logger
.error(`${this.logPrefix()} ${errorMsg}`);
810 throw new BaseError(errorMsg
);
812 if (Utils
.isEmptyObject(stationTemplate
)) {
813 const errorMsg
= `Empty charging station information from template file ${this.templateFile}`;
814 logger
.error(`${this.logPrefix()} ${errorMsg}`);
815 throw new BaseError(errorMsg
);
817 // Deprecation template keys section
818 ChargingStationUtils
.warnDeprecatedTemplateKey(
823 "Use 'supervisionUrls' instead"
825 ChargingStationUtils
.convertDeprecatedTemplateKey(
830 const stationInfo
: ChargingStationInfo
=
831 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
832 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
833 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
837 stationInfo
.ocppVersion
= stationTemplate
.ocppVersion
?? OCPPVersion
.VERSION_16
;
838 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
839 if (!Utils
.isEmptyArray(stationTemplate
.power
)) {
840 stationTemplate
.power
= stationTemplate
.power
as number[];
841 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
842 stationInfo
.maximumPower
=
843 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
844 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
845 : stationTemplate
.power
[powerArrayRandomIndex
];
847 stationTemplate
.power
= stationTemplate
.power
as number;
848 stationInfo
.maximumPower
=
849 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
850 ? stationTemplate
.power
* 1000
851 : stationTemplate
.power
;
853 stationInfo
.firmwareVersionPattern
=
854 stationTemplate
.firmwareVersionPattern
?? Constants
.SEMVER_PATTERN
;
856 stationInfo
.firmwareVersion
&&
857 new RegExp(stationInfo
.firmwareVersionPattern
).test(stationInfo
.firmwareVersion
) === false
860 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
862 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`
865 stationInfo
.firmwareUpgrade
= merge(
869 stationTemplate
.firmwareUpgrade
?? {}
871 stationInfo
.resetTime
= stationTemplate
.resetTime
872 ? stationTemplate
.resetTime
* 1000
873 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
874 const configuredMaxConnectors
=
875 ChargingStationUtils
.getConfiguredNumberOfConnectors(stationTemplate
);
876 ChargingStationUtils
.checkConfiguredMaxConnectors(
877 configuredMaxConnectors
,
881 const templateMaxConnectors
=
882 ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
);
883 ChargingStationUtils
.checkTemplateMaxConnectors(
884 templateMaxConnectors
,
889 configuredMaxConnectors
>
890 (stationTemplate
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) &&
891 !stationTemplate
?.randomConnectors
894 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
896 }, forcing random connector configurations affectation`
898 stationInfo
.randomConnectors
= true;
900 // Build connectors if needed (FIXME: should be factored out)
901 this.initializeConnectors(stationInfo
, configuredMaxConnectors
, templateMaxConnectors
);
902 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
903 ChargingStationUtils
.createStationInfoHash(stationInfo
);
907 private getStationInfoFromFile(): ChargingStationInfo
| null {
908 let stationInfo
: ChargingStationInfo
= null;
909 this.getStationInfoPersistentConfiguration() &&
910 (stationInfo
= this.getConfigurationFromFile()?.stationInfo
?? null);
911 stationInfo
&& ChargingStationUtils
.createStationInfoHash(stationInfo
);
915 private getStationInfo(): ChargingStationInfo
{
916 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
917 const stationInfoFromFile
: ChargingStationInfo
= this.getStationInfoFromFile();
918 // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
919 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
920 if (this.stationInfo
?.infoHash
=== stationInfoFromFile
?.infoHash
) {
921 return this.stationInfo
;
923 return stationInfoFromFile
;
925 stationInfoFromFile
&&
926 ChargingStationUtils
.propagateSerialNumber(
927 this.getTemplateFromFile(),
929 stationInfoFromTemplate
931 return stationInfoFromTemplate
;
934 private saveStationInfo(): void {
935 if (this.getStationInfoPersistentConfiguration()) {
936 this.saveConfiguration();
940 private getOcppPersistentConfiguration(): boolean {
941 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
944 private getStationInfoPersistentConfiguration(): boolean {
945 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
948 private handleUnsupportedVersion(version
: OCPPVersion
) {
949 const errMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
950 logger
.error(`${this.logPrefix()} ${errMsg}`);
951 throw new BaseError(errMsg
);
954 private initialize(): void {
955 this.configurationFile
= path
.join(
956 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
957 ChargingStationUtils
.getHashId(this.index
, this.getTemplateFromFile()) + '.json'
959 this.stationInfo
= this.getStationInfo();
960 this.saveStationInfo();
961 // Avoid duplication of connectors related information in RAM
962 this.stationInfo
?.Connectors
&& delete this.stationInfo
.Connectors
;
963 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
964 if (this.getEnableStatistics() === true) {
965 this.performanceStatistics
= PerformanceStatistics
.getInstance(
966 this.stationInfo
.hashId
,
967 this.stationInfo
.chargingStationId
,
968 this.configuredSupervisionUrl
971 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
974 this.powerDivider
= this.getPowerDivider();
975 // OCPP configuration
976 this.ocppConfiguration
= this.getOcppConfiguration();
977 this.initializeOcppConfiguration();
978 const ocppVersion
= this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
979 switch (ocppVersion
) {
980 case OCPPVersion
.VERSION_16
:
981 this.ocppIncomingRequestService
=
982 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
983 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
984 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
987 case OCPPVersion
.VERSION_20
:
988 case OCPPVersion
.VERSION_201
:
989 this.ocppIncomingRequestService
=
990 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
991 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
992 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
996 this.handleUnsupportedVersion(ocppVersion
);
999 if (this.stationInfo
?.autoRegister
=== true) {
1000 this.bootNotificationResponse
= {
1001 currentTime
: new Date(),
1002 interval
: this.getHeartbeatInterval() / 1000,
1003 status: RegistrationStatusEnumType
.ACCEPTED
,
1007 this.stationInfo
.firmwareStatus
=== FirmwareStatus
.Installing
&&
1008 this.stationInfo
.firmwareVersion
&&
1009 this.stationInfo
.firmwareVersionPattern
1011 const versionStep
= this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.step
?? 1;
1012 const patternGroup
: number =
1013 this.stationInfo
.firmwareUpgrade
?.versionUpgrade
?.patternGroup
??
1014 this.stationInfo
.firmwareVersion
.split('.').length
;
1015 const match
= this.stationInfo
.firmwareVersion
1016 .match(new RegExp(this.stationInfo
.firmwareVersionPattern
))
1017 .slice(1, patternGroup
+ 1);
1018 const patchLevelIndex
= match
.length
- 1;
1019 match
[patchLevelIndex
] = (
1020 Utils
.convertToInt(match
[patchLevelIndex
]) + versionStep
1022 this.stationInfo
.firmwareVersion
= match
.join('.');
1026 private initializeOcppConfiguration(): void {
1028 !ChargingStationConfigurationUtils
.getConfigurationKey(
1030 StandardParametersKey
.HeartbeatInterval
1033 ChargingStationConfigurationUtils
.addConfigurationKey(
1035 StandardParametersKey
.HeartbeatInterval
,
1040 !ChargingStationConfigurationUtils
.getConfigurationKey(
1042 StandardParametersKey
.HeartBeatInterval
1045 ChargingStationConfigurationUtils
.addConfigurationKey(
1047 StandardParametersKey
.HeartBeatInterval
,
1053 this.getSupervisionUrlOcppConfiguration() &&
1054 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1056 ChargingStationConfigurationUtils
.addConfigurationKey(
1058 this.getSupervisionUrlOcppKey(),
1059 this.configuredSupervisionUrl
.href
,
1063 !this.getSupervisionUrlOcppConfiguration() &&
1064 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1066 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1068 this.getSupervisionUrlOcppKey(),
1073 this.stationInfo
.amperageLimitationOcppKey
&&
1074 !ChargingStationConfigurationUtils
.getConfigurationKey(
1076 this.stationInfo
.amperageLimitationOcppKey
1079 ChargingStationConfigurationUtils
.addConfigurationKey(
1081 this.stationInfo
.amperageLimitationOcppKey
,
1083 this.stationInfo
.maximumAmperage
*
1084 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1089 !ChargingStationConfigurationUtils
.getConfigurationKey(
1091 StandardParametersKey
.SupportedFeatureProfiles
1094 ChargingStationConfigurationUtils
.addConfigurationKey(
1096 StandardParametersKey
.SupportedFeatureProfiles
,
1097 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1100 ChargingStationConfigurationUtils
.addConfigurationKey(
1102 StandardParametersKey
.NumberOfConnectors
,
1103 this.getNumberOfConnectors().toString(),
1108 !ChargingStationConfigurationUtils
.getConfigurationKey(
1110 StandardParametersKey
.MeterValuesSampledData
1113 ChargingStationConfigurationUtils
.addConfigurationKey(
1115 StandardParametersKey
.MeterValuesSampledData
,
1116 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1120 !ChargingStationConfigurationUtils
.getConfigurationKey(
1122 StandardParametersKey
.ConnectorPhaseRotation
1125 const connectorPhaseRotation
= [];
1126 for (const connectorId
of this.connectors
.keys()) {
1128 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
1129 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1130 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
1131 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1133 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
1134 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1135 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
1136 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1139 ChargingStationConfigurationUtils
.addConfigurationKey(
1141 StandardParametersKey
.ConnectorPhaseRotation
,
1142 connectorPhaseRotation
.toString()
1146 !ChargingStationConfigurationUtils
.getConfigurationKey(
1148 StandardParametersKey
.AuthorizeRemoteTxRequests
1151 ChargingStationConfigurationUtils
.addConfigurationKey(
1153 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1158 !ChargingStationConfigurationUtils
.getConfigurationKey(
1160 StandardParametersKey
.LocalAuthListEnabled
1162 ChargingStationConfigurationUtils
.getConfigurationKey(
1164 StandardParametersKey
.SupportedFeatureProfiles
1165 )?.value
.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1167 ChargingStationConfigurationUtils
.addConfigurationKey(
1169 StandardParametersKey
.LocalAuthListEnabled
,
1174 !ChargingStationConfigurationUtils
.getConfigurationKey(
1176 StandardParametersKey
.ConnectionTimeOut
1179 ChargingStationConfigurationUtils
.addConfigurationKey(
1181 StandardParametersKey
.ConnectionTimeOut
,
1182 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1185 this.saveOcppConfiguration();
1188 private initializeConnectors(
1189 stationInfo
: ChargingStationInfo
,
1190 configuredMaxConnectors
: number,
1191 templateMaxConnectors
: number
1193 if (!stationInfo
?.Connectors
&& this.connectors
.size
=== 0) {
1194 const logMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1195 logger
.error(`${this.logPrefix()} ${logMsg}`);
1196 throw new BaseError(logMsg
);
1198 if (!stationInfo
?.Connectors
[0]) {
1200 `${this.logPrefix()} Charging station information from template ${
1202 } with no connector Id 0 configuration`
1205 if (stationInfo
?.Connectors
) {
1206 const connectorsConfigHash
= crypto
1207 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1208 .update(JSON
.stringify(stationInfo
?.Connectors
) + configuredMaxConnectors
.toString())
1210 const connectorsConfigChanged
=
1211 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1212 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1213 connectorsConfigChanged
&& this.connectors
.clear();
1214 this.connectorsConfigurationHash
= connectorsConfigHash
;
1215 // Add connector Id 0
1216 let lastConnector
= '0';
1217 for (lastConnector
in stationInfo
?.Connectors
) {
1218 const connectorStatus
= stationInfo
?.Connectors
[lastConnector
];
1219 const lastConnectorId
= Utils
.convertToInt(lastConnector
);
1221 lastConnectorId
=== 0 &&
1222 this.getUseConnectorId0(stationInfo
) === true &&
1225 this.checkStationInfoConnectorStatus(lastConnectorId
, connectorStatus
);
1226 this.connectors
.set(
1228 Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
)
1230 this.getConnectorStatus(lastConnectorId
).availability
= AvailabilityType
.OPERATIVE
;
1231 if (Utils
.isUndefined(this.getConnectorStatus(lastConnectorId
)?.chargingProfiles
)) {
1232 this.getConnectorStatus(lastConnectorId
).chargingProfiles
= [];
1236 // Generate all connectors
1237 if ((stationInfo
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) > 0) {
1238 for (let index
= 1; index
<= configuredMaxConnectors
; index
++) {
1239 const randConnectorId
= stationInfo
?.randomConnectors
1240 ? Utils
.getRandomInteger(Utils
.convertToInt(lastConnector
), 1)
1242 const connectorStatus
= stationInfo
?.Connectors
[randConnectorId
.toString()];
1243 this.checkStationInfoConnectorStatus(randConnectorId
, connectorStatus
);
1244 this.connectors
.set(index
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1245 this.getConnectorStatus(index
).availability
= AvailabilityType
.OPERATIVE
;
1246 if (Utils
.isUndefined(this.getConnectorStatus(index
)?.chargingProfiles
)) {
1247 this.getConnectorStatus(index
).chargingProfiles
= [];
1254 `${this.logPrefix()} Charging station information from template ${
1256 } with no connectors configuration defined, using already defined connectors`
1259 // Initialize transaction attributes on connectors
1260 for (const connectorId
of this.connectors
.keys()) {
1261 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionStarted
=== true) {
1263 `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
1264 this.getConnectorStatus(connectorId).transactionId
1270 (this.getConnectorStatus(connectorId
).transactionStarted
=== undefined ||
1271 this.getConnectorStatus(connectorId
).transactionStarted
=== null)
1273 this.initializeConnectorStatus(connectorId
);
1278 private checkStationInfoConnectorStatus(
1279 connectorId
: number,
1280 connectorStatus
: ConnectorStatus
1282 if (!Utils
.isNullOrUndefined(connectorStatus
?.status)) {
1284 `${this.logPrefix()} Charging station information from template ${
1286 } with connector ${connectorId} status configuration defined, undefine it`
1288 connectorStatus
.status = undefined;
1292 private getConfigurationFromFile(): ChargingStationConfiguration
| null {
1293 let configuration
: ChargingStationConfiguration
= null;
1294 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1296 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1297 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1298 this.configurationFileHash
1301 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1302 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1303 configuration
= JSON
.parse(
1304 fs
.readFileSync(this.configurationFile
, 'utf8')
1305 ) as ChargingStationConfiguration
;
1306 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1307 this.configurationFileHash
= configuration
.configurationHash
;
1308 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1311 FileUtils
.handleFileException(
1313 FileType
.ChargingStationConfiguration
,
1314 this.configurationFile
,
1315 error
as NodeJS
.ErrnoException
1319 return configuration
;
1322 private saveConfiguration(): void {
1323 if (this.configurationFile
) {
1325 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1326 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1328 const configurationData
: ChargingStationConfiguration
=
1329 this.getConfigurationFromFile() ?? {};
1330 this.ocppConfiguration
?.configurationKey
&&
1331 (configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
);
1332 this.stationInfo
&& (configurationData
.stationInfo
= this.stationInfo
);
1333 delete configurationData
.configurationHash
;
1334 const configurationHash
= crypto
1335 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1336 .update(JSON
.stringify(configurationData
))
1338 if (this.configurationFileHash
!== configurationHash
) {
1339 configurationData
.configurationHash
= configurationHash
;
1340 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1341 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1342 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1343 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1344 fs
.closeSync(fileDescriptor
);
1345 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1346 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1347 this.configurationFileHash
= configurationHash
;
1348 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1351 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1352 this.configurationFile
1357 FileUtils
.handleFileException(
1359 FileType
.ChargingStationConfiguration
,
1360 this.configurationFile
,
1361 error
as NodeJS
.ErrnoException
1366 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1371 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| null {
1372 return this.getTemplateFromFile()?.Configuration
?? null;
1375 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| null {
1376 let configuration
: ChargingStationConfiguration
= null;
1377 if (this.getOcppPersistentConfiguration() === true) {
1378 const configurationFromFile
= this.getConfigurationFromFile();
1379 configuration
= configurationFromFile
?.configurationKey
&& configurationFromFile
;
1381 configuration
&& delete configuration
.stationInfo
;
1382 return configuration
;
1385 private getOcppConfiguration(): ChargingStationOcppConfiguration
| null {
1386 let ocppConfiguration
: ChargingStationOcppConfiguration
= this.getOcppConfigurationFromFile();
1387 if (!ocppConfiguration
) {
1388 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1390 return ocppConfiguration
;
1393 private async onOpen(): Promise
<void> {
1394 if (this.isWebSocketConnectionOpened() === true) {
1396 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1398 if (this.isRegistered() === false) {
1399 // Send BootNotification
1400 let registrationRetryCount
= 0;
1402 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1403 BootNotificationRequest
,
1404 BootNotificationResponse
1405 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1406 skipBufferingOnError
: true,
1408 if (this.isRegistered() === false) {
1409 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1411 this.bootNotificationResponse
?.interval
1412 ? this.bootNotificationResponse
.interval
* 1000
1413 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1417 this.isRegistered() === false &&
1418 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1419 this.getRegistrationMaxRetries() === -1)
1422 if (this.isRegistered() === true) {
1423 if (this.isInAcceptedState() === true) {
1424 await this.startMessageSequence();
1428 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1431 this.wsConnectionRestarted
= false;
1432 this.autoReconnectRetryCount
= 0;
1433 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1436 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1441 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1444 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1445 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1447 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1449 )}' and reason '${reason.toString()}'`
1451 this.autoReconnectRetryCount
= 0;
1456 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1458 )}' and reason '${reason.toString()}'`
1460 this.started
=== true && (await this.reconnect());
1463 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1466 private async onMessage(data
: RawData
): Promise
<void> {
1467 let messageType
: number;
1468 let messageId
: string;
1469 let commandName
: IncomingRequestCommand
;
1470 let commandPayload
: JsonType
;
1471 let errorType
: ErrorType
;
1472 let errorMessage
: string;
1473 let errorDetails
: JsonType
;
1474 let responseCallback
: ResponseCallback
;
1475 let errorCallback
: ErrorCallback
;
1476 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1477 let requestPayload
: JsonType
;
1478 let cachedRequest
: CachedRequest
;
1481 const request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1482 if (Array.isArray(request
) === true) {
1483 [messageType
, messageId
] = request
;
1484 // Check the type of message
1485 switch (messageType
) {
1487 case MessageType
.CALL_MESSAGE
:
1488 [, , commandName
, commandPayload
] = request
as IncomingRequest
;
1489 if (this.getEnableStatistics() === true) {
1490 this.performanceStatistics
.addRequestStatistic(commandName
, messageType
);
1493 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1497 // Process the message
1498 await this.ocppIncomingRequestService
.incomingRequestHandler(
1506 case MessageType
.CALL_RESULT_MESSAGE
:
1507 [, , commandPayload
] = request
as Response
;
1508 if (this.requests
.has(messageId
) === false) {
1510 throw new OCPPError(
1511 ErrorType
.INTERNAL_ERROR
,
1512 `Response for unknown message id ${messageId}`,
1518 cachedRequest
= this.requests
.get(messageId
);
1519 if (Array.isArray(cachedRequest
) === true) {
1520 [responseCallback
, errorCallback
, requestCommandName
, requestPayload
] = cachedRequest
;
1522 throw new OCPPError(
1523 ErrorType
.PROTOCOL_ERROR
,
1524 `Cached request for message id ${messageId} response is not an array`,
1526 cachedRequest
as unknown
as JsonType
1530 `${this.logPrefix()} << Command '${
1531 requestCommandName ?? Constants.UNKNOWN_COMMAND
1532 }' received response payload: ${JSON.stringify(request)}`
1534 responseCallback(commandPayload
, requestPayload
);
1537 case MessageType
.CALL_ERROR_MESSAGE
:
1538 [, , errorType
, errorMessage
, errorDetails
] = request
as ErrorResponse
;
1539 if (this.requests
.has(messageId
) === false) {
1541 throw new OCPPError(
1542 ErrorType
.INTERNAL_ERROR
,
1543 `Error response for unknown message id ${messageId}`,
1545 { errorType
, errorMessage
, errorDetails
}
1548 cachedRequest
= this.requests
.get(messageId
);
1549 if (Array.isArray(cachedRequest
) === true) {
1550 [, errorCallback
, requestCommandName
] = cachedRequest
;
1552 throw new OCPPError(
1553 ErrorType
.PROTOCOL_ERROR
,
1554 `Cached request for message id ${messageId} error response is not an array`,
1556 cachedRequest
as unknown
as JsonType
1560 `${this.logPrefix()} << Command '${
1561 requestCommandName ?? Constants.UNKNOWN_COMMAND
1562 }' received error payload: ${JSON.stringify(request)}`
1564 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1568 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1569 errMsg
= `Wrong message type ${messageType}`;
1570 logger
.error(`${this.logPrefix()} ${errMsg}`);
1571 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1573 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1575 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1582 `${this.logPrefix()} Incoming OCPP command '${
1583 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1584 }' message '${data.toString()}'${
1585 messageType !== MessageType.CALL_MESSAGE
1586 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1588 } processing error:`,
1591 if (error
instanceof OCPPError
=== false) {
1593 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1594 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1595 }' message '${data.toString()}' handling is not an OCPPError:`,
1599 switch (messageType
) {
1600 case MessageType
.CALL_MESSAGE
:
1602 await this.ocppRequestService
.sendError(
1606 commandName
?? requestCommandName
?? null
1609 case MessageType
.CALL_RESULT_MESSAGE
:
1610 case MessageType
.CALL_ERROR_MESSAGE
:
1611 if (errorCallback
) {
1612 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1613 errorCallback(error
as OCPPError
, false);
1615 // Remove the request from the cache in case of error at response handling
1616 this.requests
.delete(messageId
);
1623 private onPing(): void {
1624 logger
.debug(this.logPrefix() + ' Received a WS ping (rfc6455) from the server');
1627 private onPong(): void {
1628 logger
.debug(this.logPrefix() + ' Received a WS pong (rfc6455) from the server');
1631 private onError(error
: WSError
): void {
1632 this.closeWSConnection();
1633 logger
.error(this.logPrefix() + ' WebSocket error:', error
);
1636 private getEnergyActiveImportRegister(
1637 connectorStatus
: ConnectorStatus
,
1640 if (this.getMeteringPerTransaction() === true) {
1643 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1644 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1649 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1650 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1654 private getUseConnectorId0(stationInfo
?: ChargingStationInfo
): boolean {
1655 const localStationInfo
= stationInfo
?? this.stationInfo
;
1656 return !Utils
.isUndefined(localStationInfo
.useConnectorId0
)
1657 ? localStationInfo
.useConnectorId0
1661 private getNumberOfRunningTransactions(): number {
1663 for (const connectorId
of this.connectors
.keys()) {
1664 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1671 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1672 for (const connectorId
of this.connectors
.keys()) {
1673 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1674 await this.stopTransactionOnConnector(connectorId
, reason
);
1680 private getConnectionTimeout(): number {
1682 ChargingStationConfigurationUtils
.getConfigurationKey(
1684 StandardParametersKey
.ConnectionTimeOut
1689 ChargingStationConfigurationUtils
.getConfigurationKey(
1691 StandardParametersKey
.ConnectionTimeOut
1693 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1696 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1699 // -1 for unlimited, 0 for disabling
1700 private getAutoReconnectMaxRetries(): number {
1701 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1702 return this.stationInfo
.autoReconnectMaxRetries
;
1704 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1705 return Configuration
.getAutoReconnectMaxRetries();
1711 private getRegistrationMaxRetries(): number {
1712 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1713 return this.stationInfo
.registrationMaxRetries
;
1718 private getPowerDivider(): number {
1719 let powerDivider
= this.getNumberOfConnectors();
1720 if (this.stationInfo
?.powerSharedByConnectors
) {
1721 powerDivider
= this.getNumberOfRunningTransactions();
1723 return powerDivider
;
1726 private getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
1727 const localStationInfo
= stationInfo
?? this.stationInfo
;
1728 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
1731 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1732 const maximumPower
= this.getMaximumPower(stationInfo
);
1733 switch (this.getCurrentOutType(stationInfo
)) {
1734 case CurrentType
.AC
:
1735 return ACElectricUtils
.amperagePerPhaseFromPower(
1736 this.getNumberOfPhases(stationInfo
),
1737 maximumPower
/ this.getNumberOfConnectors(),
1738 this.getVoltageOut(stationInfo
)
1740 case CurrentType
.DC
:
1741 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1745 private getAmperageLimitation(): number | undefined {
1747 this.stationInfo
.amperageLimitationOcppKey
&&
1748 ChargingStationConfigurationUtils
.getConfigurationKey(
1750 this.stationInfo
.amperageLimitationOcppKey
1755 ChargingStationConfigurationUtils
.getConfigurationKey(
1757 this.stationInfo
.amperageLimitationOcppKey
1759 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1764 private getChargingProfilePowerLimit(connectorId
: number): number | undefined {
1765 let limit
: number, matchingChargingProfile
: ChargingProfile
;
1766 let chargingProfiles
: ChargingProfile
[] = [];
1767 // Get charging profiles for connector and sort by stack level
1768 chargingProfiles
= this.getConnectorStatus(connectorId
).chargingProfiles
.sort(
1769 (a
, b
) => b
.stackLevel
- a
.stackLevel
1771 // Get profiles on connector 0
1772 if (this.getConnectorStatus(0).chargingProfiles
) {
1773 chargingProfiles
.push(
1774 ...this.getConnectorStatus(0).chargingProfiles
.sort((a
, b
) => b
.stackLevel
- a
.stackLevel
)
1777 if (!Utils
.isEmptyArray(chargingProfiles
)) {
1778 const result
= ChargingStationUtils
.getLimitFromChargingProfiles(
1782 if (!Utils
.isNullOrUndefined(result
)) {
1783 limit
= result
.limit
;
1784 matchingChargingProfile
= result
.matchingChargingProfile
;
1785 switch (this.getCurrentOutType()) {
1786 case CurrentType
.AC
:
1788 matchingChargingProfile
.chargingSchedule
.chargingRateUnit
===
1789 ChargingRateUnitType
.WATT
1791 : ACElectricUtils
.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit
);
1793 case CurrentType
.DC
:
1795 matchingChargingProfile
.chargingSchedule
.chargingRateUnit
===
1796 ChargingRateUnitType
.WATT
1798 : DCElectricUtils
.power(this.getVoltageOut(), limit
);
1800 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
1801 if (limit
> connectorMaximumPower
) {
1803 `${this.logPrefix()} Charging profile id ${
1804 matchingChargingProfile.chargingProfileId
1805 } limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}, dump charging profiles' stack: %j`,
1806 this.getConnectorStatus(connectorId
).chargingProfiles
1808 limit
= connectorMaximumPower
;
1815 private async startMessageSequence(): Promise
<void> {
1816 if (this.stationInfo
?.autoRegister
=== true) {
1817 await this.ocppRequestService
.requestHandler
<
1818 BootNotificationRequest
,
1819 BootNotificationResponse
1820 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1821 skipBufferingOnError
: true,
1824 // Start WebSocket ping
1825 this.startWebSocketPing();
1827 this.startHeartbeat();
1828 // Initialize connectors status
1829 for (const connectorId
of this.connectors
.keys()) {
1830 let connectorStatus
: ConnectorStatusEnum
;
1831 if (connectorId
=== 0) {
1834 !this.getConnectorStatus(connectorId
)?.status &&
1835 (this.isChargingStationAvailable() === false ||
1836 this.isConnectorAvailable(connectorId
) === false)
1838 connectorStatus
= ConnectorStatusEnum
.UNAVAILABLE
;
1840 !this.getConnectorStatus(connectorId
)?.status &&
1841 this.getConnectorStatus(connectorId
)?.bootStatus
1843 // Set boot status in template at startup
1844 connectorStatus
= this.getConnectorStatus(connectorId
).bootStatus
;
1845 } else if (this.getConnectorStatus(connectorId
)?.status) {
1846 // Set previous status at startup
1847 connectorStatus
= this.getConnectorStatus(connectorId
).status;
1849 // Set default status
1850 connectorStatus
= ConnectorStatusEnum
.AVAILABLE
;
1852 await this.ocppRequestService
.requestHandler
<
1853 StatusNotificationRequest
,
1854 StatusNotificationResponse
1857 RequestCommand
.STATUS_NOTIFICATION
,
1858 OCPPServiceUtils
.buildStatusNotificationRequest(this, connectorId
, connectorStatus
)
1860 this.getConnectorStatus(connectorId
).status = connectorStatus
;
1862 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
1863 await this.ocppRequestService
.requestHandler
<
1864 FirmwareStatusNotificationRequest
,
1865 FirmwareStatusNotificationResponse
1866 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1867 status: FirmwareStatus
.Installed
,
1869 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
1873 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true) {
1874 this.startAutomaticTransactionGenerator();
1876 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
1879 private async stopMessageSequence(
1880 reason
: StopTransactionReason
= StopTransactionReason
.NONE
1882 // Stop WebSocket ping
1883 this.stopWebSocketPing();
1885 this.stopHeartbeat();
1886 // Stop ongoing transactions
1887 if (this.automaticTransactionGenerator
?.started
=== true) {
1888 this.stopAutomaticTransactionGenerator();
1890 await this.stopRunningTransactions(reason
);
1892 for (const connectorId
of this.connectors
.keys()) {
1893 if (connectorId
> 0) {
1894 await this.ocppRequestService
.requestHandler
<
1895 StatusNotificationRequest
,
1896 StatusNotificationResponse
1899 RequestCommand
.STATUS_NOTIFICATION
,
1900 OCPPServiceUtils
.buildStatusNotificationRequest(
1903 ConnectorStatusEnum
.UNAVAILABLE
1906 this.getConnectorStatus(connectorId
).status = null;
1911 private startWebSocketPing(): void {
1912 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
1914 StandardParametersKey
.WebSocketPingInterval
1916 ? Utils
.convertToInt(
1917 ChargingStationConfigurationUtils
.getConfigurationKey(
1919 StandardParametersKey
.WebSocketPingInterval
1923 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
1924 this.webSocketPingSetInterval
= setInterval(() => {
1925 if (this.isWebSocketConnectionOpened() === true) {
1926 this.wsConnection
.ping();
1928 }, webSocketPingInterval
* 1000);
1931 ' WebSocket ping started every ' +
1932 Utils
.formatDurationSeconds(webSocketPingInterval
)
1934 } else if (this.webSocketPingSetInterval
) {
1937 ' WebSocket ping already started every ' +
1938 Utils
.formatDurationSeconds(webSocketPingInterval
)
1942 `${this.logPrefix()} WebSocket ping interval set to ${
1943 webSocketPingInterval
1944 ? Utils.formatDurationSeconds(webSocketPingInterval)
1945 : webSocketPingInterval
1946 }, not starting the WebSocket ping`
1951 private stopWebSocketPing(): void {
1952 if (this.webSocketPingSetInterval
) {
1953 clearInterval(this.webSocketPingSetInterval
);
1957 private getConfiguredSupervisionUrl(): URL
{
1958 const supervisionUrls
= this.stationInfo
.supervisionUrls
?? Configuration
.getSupervisionUrls();
1959 if (!Utils
.isEmptyArray(supervisionUrls
)) {
1960 switch (Configuration
.getSupervisionUrlDistribution()) {
1961 case SupervisionUrlDistribution
.ROUND_ROBIN
:
1963 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1965 case SupervisionUrlDistribution
.RANDOM
:
1966 this.configuredSupervisionUrlIndex
= Math.floor(
1967 Utils
.secureRandom() * supervisionUrls
.length
1970 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
1971 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1975 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
1976 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
1979 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1982 return new URL(supervisionUrls
[this.configuredSupervisionUrlIndex
]);
1984 return new URL(supervisionUrls
as string);
1987 private getHeartbeatInterval(): number {
1988 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1990 StandardParametersKey
.HeartbeatInterval
1992 if (HeartbeatInterval
) {
1993 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
1995 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1997 StandardParametersKey
.HeartBeatInterval
1999 if (HeartBeatInterval
) {
2000 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
2002 this.stationInfo
?.autoRegister
=== false &&
2004 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
2005 Constants.DEFAULT_HEARTBEAT_INTERVAL
2008 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
2011 private stopHeartbeat(): void {
2012 if (this.heartbeatSetInterval
) {
2013 clearInterval(this.heartbeatSetInterval
);
2017 private terminateWSConnection(): void {
2018 if (this.isWebSocketConnectionOpened() === true) {
2019 this.wsConnection
.terminate();
2020 this.wsConnection
= null;
2024 private stopMeterValues(connectorId
: number) {
2025 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
2026 clearInterval(this.getConnectorStatus(connectorId
).transactionSetInterval
);
2030 private getReconnectExponentialDelay(): boolean {
2031 return !Utils
.isUndefined(this.stationInfo
.reconnectExponentialDelay
)
2032 ? this.stationInfo
.reconnectExponentialDelay
2036 private async reconnect(): Promise
<void> {
2037 // Stop WebSocket ping
2038 this.stopWebSocketPing();
2040 this.stopHeartbeat();
2041 // Stop the ATG if needed
2042 if (this.automaticTransactionGenerator
?.configuration
?.stopOnConnectionFailure
=== true) {
2043 this.stopAutomaticTransactionGenerator();
2046 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2047 this.getAutoReconnectMaxRetries() === -1
2049 this.autoReconnectRetryCount
++;
2050 const reconnectDelay
= this.getReconnectExponentialDelay()
2051 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2052 : this.getConnectionTimeout() * 1000;
2053 const reconnectDelayWithdraw
= 1000;
2054 const reconnectTimeout
=
2055 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2056 ? reconnectDelay
- reconnectDelayWithdraw
2059 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2062 )}ms, timeout ${reconnectTimeout}ms`
2064 await Utils
.sleep(reconnectDelay
);
2066 this.logPrefix() + ' WebSocket connection retry #' + this.autoReconnectRetryCount
.toString()
2068 this.openWSConnection(
2069 { ...(this.stationInfo
?.wsOptions
?? {}), handshakeTimeout
: reconnectTimeout
},
2070 { closeOpened
: true }
2072 this.wsConnectionRestarted
= true;
2073 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2075 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2076 this.autoReconnectRetryCount
2077 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2082 private getAutomaticTransactionGeneratorConfigurationFromTemplate(): AutomaticTransactionGeneratorConfiguration
| null {
2083 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
?? null;
2086 private initializeConnectorStatus(connectorId
: number): void {
2087 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
2088 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
2089 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
2090 this.getConnectorStatus(connectorId
).transactionStarted
= false;
2091 this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
= 0;
2092 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;