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 WebSocket
, { type RawData
} from
'ws';
11 import BaseError from
'../exception/BaseError';
12 import OCPPError from
'../exception/OCPPError';
13 import PerformanceStatistics from
'../performance/PerformanceStatistics';
14 import type { AutomaticTransactionGeneratorConfiguration
} from
'../types/AutomaticTransactionGenerator';
15 import type { ChargingStationConfiguration
} from
'../types/ChargingStationConfiguration';
16 import type { ChargingStationInfo
} from
'../types/ChargingStationInfo';
17 import type { ChargingStationOcppConfiguration
} from
'../types/ChargingStationOcppConfiguration';
19 type ChargingStationTemplate
,
23 } from
'../types/ChargingStationTemplate';
24 import { SupervisionUrlDistribution
} from
'../types/ConfigurationData';
25 import type { ConnectorStatus
} from
'../types/ConnectorStatus';
26 import { FileType
} from
'../types/FileType';
27 import type { JsonType
} from
'../types/JsonType';
28 import { ChargePointErrorCode
} from
'../types/ocpp/ChargePointErrorCode';
29 import { ChargePointStatus
} from
'../types/ocpp/ChargePointStatus';
30 import { ChargingProfile
, ChargingRateUnitType
} from
'../types/ocpp/ChargingProfile';
32 ConnectorPhaseRotation
,
33 StandardParametersKey
,
34 SupportedFeatureProfiles
,
35 VendorDefaultParametersKey
,
36 } from
'../types/ocpp/Configuration';
37 import { ErrorType
} from
'../types/ocpp/ErrorType';
38 import { MessageType
} from
'../types/ocpp/MessageType';
39 import { MeterValue
, MeterValueMeasurand
} from
'../types/ocpp/MeterValues';
40 import { OCPPVersion
} from
'../types/ocpp/OCPPVersion';
43 type BootNotificationRequest
,
47 type FirmwareStatusNotificationRequest
,
48 type HeartbeatRequest
,
50 IncomingRequestCommand
,
51 type MeterValuesRequest
,
53 type ResponseCallback
,
54 type StatusNotificationRequest
,
55 } from
'../types/ocpp/Requests';
57 type BootNotificationResponse
,
59 type FirmwareStatusNotificationResponse
,
60 type HeartbeatResponse
,
61 type MeterValuesResponse
,
62 RegistrationStatusEnumType
,
64 type StatusNotificationResponse
,
65 } from
'../types/ocpp/Responses';
67 StopTransactionReason
,
68 type StopTransactionRequest
,
69 type StopTransactionResponse
,
70 } from
'../types/ocpp/Transaction';
71 import { WSError
, WebSocketCloseEventStatusCode
} from
'../types/WebSocket';
72 import Configuration from
'../utils/Configuration';
73 import Constants from
'../utils/Constants';
74 import { ACElectricUtils
, DCElectricUtils
} from
'../utils/ElectricUtils';
75 import FileUtils from
'../utils/FileUtils';
76 import logger from
'../utils/Logger';
77 import Utils from
'../utils/Utils';
78 import AuthorizedTagsCache from
'./AuthorizedTagsCache';
79 import AutomaticTransactionGenerator from
'./AutomaticTransactionGenerator';
80 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
81 import { ChargingStationUtils
} from
'./ChargingStationUtils';
82 import ChargingStationWorkerBroadcastChannel from
'./ChargingStationWorkerBroadcastChannel';
83 import { MessageChannelUtils
} from
'./MessageChannelUtils';
84 import OCPP16IncomingRequestService from
'./ocpp/1.6/OCPP16IncomingRequestService';
85 import OCPP16RequestService from
'./ocpp/1.6/OCPP16RequestService';
86 import OCPP16ResponseService from
'./ocpp/1.6/OCPP16ResponseService';
87 import { OCPP16ServiceUtils
} from
'./ocpp/1.6/OCPP16ServiceUtils';
88 import OCPP20IncomingRequestService from
'./ocpp/2.0/OCPP20IncomingRequestService';
89 import OCPP20RequestService from
'./ocpp/2.0/OCPP20RequestService';
90 import OCPP20ResponseService from
'./ocpp/2.0/OCPP20ResponseService';
91 import type OCPPIncomingRequestService from
'./ocpp/OCPPIncomingRequestService';
92 import type OCPPRequestService from
'./ocpp/OCPPRequestService';
93 import SharedLRUCache from
'./SharedLRUCache';
95 export default class ChargingStation
{
96 public readonly index
: number;
97 public readonly templateFile
: string;
98 public stationInfo
!: ChargingStationInfo
;
99 public started
: boolean;
100 public starting
: boolean;
101 public authorizedTagsCache
: AuthorizedTagsCache
;
102 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
;
103 public ocppConfiguration
!: ChargingStationOcppConfiguration
;
104 public wsConnection
!: WebSocket
;
105 public readonly connectors
: Map
<number, ConnectorStatus
>;
106 public readonly requests
: Map
<string, CachedRequest
>;
107 public performanceStatistics
!: PerformanceStatistics
;
108 public heartbeatSetInterval
!: NodeJS
.Timeout
;
109 public ocppRequestService
!: OCPPRequestService
;
110 public bootNotificationRequest
!: BootNotificationRequest
;
111 public bootNotificationResponse
!: BootNotificationResponse
| null;
112 public powerDivider
!: number;
113 private stopping
: boolean;
114 private configurationFile
!: string;
115 private configurationFileHash
!: string;
116 private connectorsConfigurationHash
!: string;
117 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
118 private readonly messageBuffer
: Set
<string>;
119 private configuredSupervisionUrl
!: URL
;
120 private configuredSupervisionUrlIndex
!: number;
121 private wsConnectionRestarted
: boolean;
122 private autoReconnectRetryCount
: number;
123 private templateFileWatcher
!: fs
.FSWatcher
;
124 private readonly sharedLRUCache
: SharedLRUCache
;
125 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
126 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
128 constructor(index
: number, templateFile
: string) {
129 this.started
= false;
130 this.starting
= false;
131 this.stopping
= false;
132 this.wsConnectionRestarted
= false;
133 this.autoReconnectRetryCount
= 0;
135 this.templateFile
= templateFile
;
136 this.connectors
= new Map
<number, ConnectorStatus
>();
137 this.requests
= new Map
<string, CachedRequest
>();
138 this.messageBuffer
= new Set
<string>();
139 this.sharedLRUCache
= SharedLRUCache
.getInstance();
140 this.authorizedTagsCache
= AuthorizedTagsCache
.getInstance();
141 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
146 private get
wsConnectionUrl(): URL
{
148 (this.getSupervisionUrlOcppConfiguration()
149 ? ChargingStationConfigurationUtils
.getConfigurationKey(
151 this.getSupervisionUrlOcppKey()
153 : this.configuredSupervisionUrl
.href
) +
155 this.stationInfo
.chargingStationId
159 public logPrefix(): string {
160 return Utils
.logPrefix(
162 this?.stationInfo?.chargingStationId ??
163 ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())
168 public hasAuthorizedTags(): boolean {
169 return !Utils
.isEmptyArray(
170 this.authorizedTagsCache
.getAuthorizedTags(
171 ChargingStationUtils
.getAuthorizationFile(this.stationInfo
)
176 public getEnableStatistics(): boolean | undefined {
177 return !Utils
.isUndefined(this.stationInfo
.enableStatistics
)
178 ? this.stationInfo
.enableStatistics
182 public getMustAuthorizeAtRemoteStart(): boolean | undefined {
183 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
186 public getPayloadSchemaValidation(): boolean | undefined {
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()) {
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()) {
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()) {
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.getOcppVersion();
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 firmwareVersionRegExp
= stationTemplate
.firmwareVersionPattern
831 ? new RegExp(stationTemplate
.firmwareVersionPattern
)
832 : Constants
.SEMVER_REGEXP
;
834 stationTemplate
.firmwareVersion
&&
835 firmwareVersionRegExp
.test(stationTemplate
.firmwareVersion
) === false
838 `${this.logPrefix()} Firmware version '${
839 stationTemplate.firmwareVersion
840 }' in template file ${
842 } does not match regular expression '${firmwareVersionRegExp.toString()}'`
845 const stationInfo
: ChargingStationInfo
=
846 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
847 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
848 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
852 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
853 if (!Utils
.isEmptyArray(stationTemplate
.power
)) {
854 stationTemplate
.power
= stationTemplate
.power
as number[];
855 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
856 stationInfo
.maximumPower
=
857 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
858 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
859 : stationTemplate
.power
[powerArrayRandomIndex
];
861 stationTemplate
.power
= stationTemplate
.power
as number;
862 stationInfo
.maximumPower
=
863 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
864 ? stationTemplate
.power
* 1000
865 : stationTemplate
.power
;
867 stationInfo
.resetTime
= stationTemplate
.resetTime
868 ? stationTemplate
.resetTime
* 1000
869 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
870 const configuredMaxConnectors
=
871 ChargingStationUtils
.getConfiguredNumberOfConnectors(stationTemplate
);
872 ChargingStationUtils
.checkConfiguredMaxConnectors(
873 configuredMaxConnectors
,
877 const templateMaxConnectors
=
878 ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
);
879 ChargingStationUtils
.checkTemplateMaxConnectors(
880 templateMaxConnectors
,
885 configuredMaxConnectors
>
886 (stationTemplate
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) &&
887 !stationTemplate
?.randomConnectors
890 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
892 }, forcing random connector configurations affectation`
894 stationInfo
.randomConnectors
= true;
896 // Build connectors if needed (FIXME: should be factored out)
897 this.initializeConnectors(stationInfo
, configuredMaxConnectors
, templateMaxConnectors
);
898 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
899 ChargingStationUtils
.createStationInfoHash(stationInfo
);
903 private getStationInfoFromFile(): ChargingStationInfo
| null {
904 let stationInfo
: ChargingStationInfo
= null;
905 this.getStationInfoPersistentConfiguration() &&
906 (stationInfo
= this.getConfigurationFromFile()?.stationInfo
?? null);
907 stationInfo
&& ChargingStationUtils
.createStationInfoHash(stationInfo
);
911 private getStationInfo(): ChargingStationInfo
{
912 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
913 const stationInfoFromFile
: ChargingStationInfo
= this.getStationInfoFromFile();
914 // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
915 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
916 if (this.stationInfo
?.infoHash
=== stationInfoFromFile
?.infoHash
) {
917 return this.stationInfo
;
919 return stationInfoFromFile
;
921 stationInfoFromFile
&&
922 ChargingStationUtils
.propagateSerialNumber(
923 this.getTemplateFromFile(),
925 stationInfoFromTemplate
927 return stationInfoFromTemplate
;
930 private saveStationInfo(): void {
931 if (this.getStationInfoPersistentConfiguration()) {
932 this.saveConfiguration();
936 private getOcppVersion(): OCPPVersion
{
937 return this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
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()) {
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 switch (this.getOcppVersion()) {
979 case OCPPVersion
.VERSION_16
:
980 this.ocppIncomingRequestService
=
981 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
982 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
983 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
986 case OCPPVersion
.VERSION_20
:
987 case OCPPVersion
.VERSION_201
:
988 this.ocppIncomingRequestService
=
989 OCPP20IncomingRequestService
.getInstance
<OCPP20IncomingRequestService
>();
990 this.ocppRequestService
= OCPP20RequestService
.getInstance
<OCPP20RequestService
>(
991 OCPP20ResponseService
.getInstance
<OCPP20ResponseService
>()
995 this.handleUnsupportedVersion(this.getOcppVersion());
998 if (this.stationInfo
?.autoRegister
=== true) {
999 this.bootNotificationResponse
= {
1000 currentTime
: new Date(),
1001 interval
: this.getHeartbeatInterval() / 1000,
1002 status: RegistrationStatusEnumType
.ACCEPTED
,
1007 private initializeOcppConfiguration(): void {
1009 !ChargingStationConfigurationUtils
.getConfigurationKey(
1011 StandardParametersKey
.HeartbeatInterval
1014 ChargingStationConfigurationUtils
.addConfigurationKey(
1016 StandardParametersKey
.HeartbeatInterval
,
1021 !ChargingStationConfigurationUtils
.getConfigurationKey(
1023 StandardParametersKey
.HeartBeatInterval
1026 ChargingStationConfigurationUtils
.addConfigurationKey(
1028 StandardParametersKey
.HeartBeatInterval
,
1034 this.getSupervisionUrlOcppConfiguration() &&
1035 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1037 ChargingStationConfigurationUtils
.addConfigurationKey(
1039 this.getSupervisionUrlOcppKey(),
1040 this.configuredSupervisionUrl
.href
,
1044 !this.getSupervisionUrlOcppConfiguration() &&
1045 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1047 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1049 this.getSupervisionUrlOcppKey(),
1054 this.stationInfo
.amperageLimitationOcppKey
&&
1055 !ChargingStationConfigurationUtils
.getConfigurationKey(
1057 this.stationInfo
.amperageLimitationOcppKey
1060 ChargingStationConfigurationUtils
.addConfigurationKey(
1062 this.stationInfo
.amperageLimitationOcppKey
,
1064 this.stationInfo
.maximumAmperage
*
1065 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1070 !ChargingStationConfigurationUtils
.getConfigurationKey(
1072 StandardParametersKey
.SupportedFeatureProfiles
1075 ChargingStationConfigurationUtils
.addConfigurationKey(
1077 StandardParametersKey
.SupportedFeatureProfiles
,
1078 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1081 ChargingStationConfigurationUtils
.addConfigurationKey(
1083 StandardParametersKey
.NumberOfConnectors
,
1084 this.getNumberOfConnectors().toString(),
1089 !ChargingStationConfigurationUtils
.getConfigurationKey(
1091 StandardParametersKey
.MeterValuesSampledData
1094 ChargingStationConfigurationUtils
.addConfigurationKey(
1096 StandardParametersKey
.MeterValuesSampledData
,
1097 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1101 !ChargingStationConfigurationUtils
.getConfigurationKey(
1103 StandardParametersKey
.ConnectorPhaseRotation
1106 const connectorPhaseRotation
= [];
1107 for (const connectorId
of this.connectors
.keys()) {
1109 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
1110 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1111 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
1112 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1114 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
1115 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1116 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
1117 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1120 ChargingStationConfigurationUtils
.addConfigurationKey(
1122 StandardParametersKey
.ConnectorPhaseRotation
,
1123 connectorPhaseRotation
.toString()
1127 !ChargingStationConfigurationUtils
.getConfigurationKey(
1129 StandardParametersKey
.AuthorizeRemoteTxRequests
1132 ChargingStationConfigurationUtils
.addConfigurationKey(
1134 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1139 !ChargingStationConfigurationUtils
.getConfigurationKey(
1141 StandardParametersKey
.LocalAuthListEnabled
1143 ChargingStationConfigurationUtils
.getConfigurationKey(
1145 StandardParametersKey
.SupportedFeatureProfiles
1146 )?.value
.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1148 ChargingStationConfigurationUtils
.addConfigurationKey(
1150 StandardParametersKey
.LocalAuthListEnabled
,
1155 !ChargingStationConfigurationUtils
.getConfigurationKey(
1157 StandardParametersKey
.ConnectionTimeOut
1160 ChargingStationConfigurationUtils
.addConfigurationKey(
1162 StandardParametersKey
.ConnectionTimeOut
,
1163 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1166 this.saveOcppConfiguration();
1169 private initializeConnectors(
1170 stationInfo
: ChargingStationInfo
,
1171 configuredMaxConnectors
: number,
1172 templateMaxConnectors
: number
1174 if (!stationInfo
?.Connectors
&& this.connectors
.size
=== 0) {
1175 const logMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1176 logger
.error(`${this.logPrefix()} ${logMsg}`);
1177 throw new BaseError(logMsg
);
1179 if (!stationInfo
?.Connectors
[0]) {
1181 `${this.logPrefix()} Charging station information from template ${
1183 } with no connector Id 0 configuration`
1186 if (stationInfo
?.Connectors
) {
1187 const connectorsConfigHash
= crypto
1188 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1189 .update(JSON
.stringify(stationInfo
?.Connectors
) + configuredMaxConnectors
.toString())
1191 const connectorsConfigChanged
=
1192 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1193 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1194 connectorsConfigChanged
&& this.connectors
.clear();
1195 this.connectorsConfigurationHash
= connectorsConfigHash
;
1196 // Add connector Id 0
1197 let lastConnector
= '0';
1198 for (lastConnector
in stationInfo
?.Connectors
) {
1199 const connectorStatus
= stationInfo
?.Connectors
[lastConnector
];
1200 const lastConnectorId
= Utils
.convertToInt(lastConnector
);
1202 lastConnectorId
=== 0 &&
1203 this.getUseConnectorId0(stationInfo
) === true &&
1206 this.checkStationInfoConnectorStatus(lastConnectorId
, connectorStatus
);
1207 this.connectors
.set(
1209 Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
)
1211 this.getConnectorStatus(lastConnectorId
).availability
= AvailabilityType
.OPERATIVE
;
1212 if (Utils
.isUndefined(this.getConnectorStatus(lastConnectorId
)?.chargingProfiles
)) {
1213 this.getConnectorStatus(lastConnectorId
).chargingProfiles
= [];
1217 // Generate all connectors
1218 if ((stationInfo
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) > 0) {
1219 for (let index
= 1; index
<= configuredMaxConnectors
; index
++) {
1220 const randConnectorId
= stationInfo
?.randomConnectors
1221 ? Utils
.getRandomInteger(Utils
.convertToInt(lastConnector
), 1)
1223 const connectorStatus
= stationInfo
?.Connectors
[randConnectorId
.toString()];
1224 this.checkStationInfoConnectorStatus(randConnectorId
, connectorStatus
);
1225 this.connectors
.set(index
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1226 this.getConnectorStatus(index
).availability
= AvailabilityType
.OPERATIVE
;
1227 if (Utils
.isUndefined(this.getConnectorStatus(index
)?.chargingProfiles
)) {
1228 this.getConnectorStatus(index
).chargingProfiles
= [];
1235 `${this.logPrefix()} Charging station information from template ${
1237 } with no connectors configuration defined, using already defined connectors`
1240 // Initialize transaction attributes on connectors
1241 for (const connectorId
of this.connectors
.keys()) {
1242 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionStarted
=== true) {
1244 `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
1245 this.getConnectorStatus(connectorId).transactionId
1251 (this.getConnectorStatus(connectorId
).transactionStarted
=== undefined ||
1252 this.getConnectorStatus(connectorId
).transactionStarted
=== null)
1254 this.initializeConnectorStatus(connectorId
);
1259 private checkStationInfoConnectorStatus(
1260 connectorId
: number,
1261 connectorStatus
: ConnectorStatus
1263 if (!Utils
.isNullOrUndefined(connectorStatus
?.status)) {
1265 `${this.logPrefix()} Charging station information from template ${
1267 } with connector ${connectorId} status configuration defined, undefine it`
1269 connectorStatus
.status = undefined;
1273 private getConfigurationFromFile(): ChargingStationConfiguration
| null {
1274 let configuration
: ChargingStationConfiguration
= null;
1275 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1277 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1278 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1279 this.configurationFileHash
1282 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1283 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1284 configuration
= JSON
.parse(
1285 fs
.readFileSync(this.configurationFile
, 'utf8')
1286 ) as ChargingStationConfiguration
;
1287 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1288 this.configurationFileHash
= configuration
.configurationHash
;
1289 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1292 FileUtils
.handleFileException(
1294 FileType
.ChargingStationConfiguration
,
1295 this.configurationFile
,
1296 error
as NodeJS
.ErrnoException
1300 return configuration
;
1303 private saveConfiguration(): void {
1304 if (this.configurationFile
) {
1306 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1307 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1309 const configurationData
: ChargingStationConfiguration
=
1310 this.getConfigurationFromFile() ?? {};
1311 this.ocppConfiguration
?.configurationKey
&&
1312 (configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
);
1313 this.stationInfo
&& (configurationData
.stationInfo
= this.stationInfo
);
1314 delete configurationData
.configurationHash
;
1315 const configurationHash
= crypto
1316 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1317 .update(JSON
.stringify(configurationData
))
1319 if (this.configurationFileHash
!== configurationHash
) {
1320 configurationData
.configurationHash
= configurationHash
;
1321 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1322 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1323 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1324 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1325 fs
.closeSync(fileDescriptor
);
1326 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1327 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1328 this.configurationFileHash
= configurationHash
;
1329 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1332 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1333 this.configurationFile
1338 FileUtils
.handleFileException(
1340 FileType
.ChargingStationConfiguration
,
1341 this.configurationFile
,
1342 error
as NodeJS
.ErrnoException
1347 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1352 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| null {
1353 return this.getTemplateFromFile()?.Configuration
?? null;
1356 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| null {
1357 let configuration
: ChargingStationConfiguration
= null;
1358 if (this.getOcppPersistentConfiguration() === true) {
1359 const configurationFromFile
= this.getConfigurationFromFile();
1360 configuration
= configurationFromFile
?.configurationKey
&& configurationFromFile
;
1362 configuration
&& delete configuration
.stationInfo
;
1363 return configuration
;
1366 private getOcppConfiguration(): ChargingStationOcppConfiguration
| null {
1367 let ocppConfiguration
: ChargingStationOcppConfiguration
= this.getOcppConfigurationFromFile();
1368 if (!ocppConfiguration
) {
1369 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1371 return ocppConfiguration
;
1374 private async onOpen(): Promise
<void> {
1375 if (this.isWebSocketConnectionOpened() === true) {
1377 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1379 if (this.isRegistered() === false) {
1380 // Send BootNotification
1381 let registrationRetryCount
= 0;
1383 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1384 BootNotificationRequest
,
1385 BootNotificationResponse
1386 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1387 skipBufferingOnError
: true,
1389 if (this.isRegistered() === false) {
1390 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1392 this.bootNotificationResponse
?.interval
1393 ? this.bootNotificationResponse
.interval
* 1000
1394 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1398 this.isRegistered() === false &&
1399 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1400 this.getRegistrationMaxRetries() === -1)
1403 if (this.isRegistered() === true) {
1404 if (this.isInAcceptedState() === true) {
1405 await this.startMessageSequence();
1409 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1412 this.wsConnectionRestarted
= false;
1413 this.autoReconnectRetryCount
= 0;
1414 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1417 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1422 private async onClose(code
: number, reason
: Buffer
): Promise
<void> {
1425 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1426 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1428 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1430 )}' and reason '${reason.toString()}'`
1432 this.autoReconnectRetryCount
= 0;
1437 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1439 )}' and reason '${reason.toString()}'`
1441 this.started
=== true && (await this.reconnect());
1444 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1447 private async onMessage(data
: RawData
): Promise
<void> {
1448 let messageType
: number;
1449 let messageId
: string;
1450 let commandName
: IncomingRequestCommand
;
1451 let commandPayload
: JsonType
;
1452 let errorType
: ErrorType
;
1453 let errorMessage
: string;
1454 let errorDetails
: JsonType
;
1455 let responseCallback
: ResponseCallback
;
1456 let errorCallback
: ErrorCallback
;
1457 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1458 let requestPayload
: JsonType
;
1459 let cachedRequest
: CachedRequest
;
1462 const request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1463 if (Array.isArray(request
) === true) {
1464 [messageType
, messageId
] = request
;
1465 // Check the type of message
1466 switch (messageType
) {
1468 case MessageType
.CALL_MESSAGE
:
1469 [, , commandName
, commandPayload
] = request
as IncomingRequest
;
1470 if (this.getEnableStatistics() === true) {
1471 this.performanceStatistics
.addRequestStatistic(commandName
, messageType
);
1474 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1478 // Process the message
1479 await this.ocppIncomingRequestService
.incomingRequestHandler(
1487 case MessageType
.CALL_RESULT_MESSAGE
:
1488 [, , commandPayload
] = request
as Response
;
1489 if (this.requests
.has(messageId
) === false) {
1491 throw new OCPPError(
1492 ErrorType
.INTERNAL_ERROR
,
1493 `Response for unknown message id ${messageId}`,
1499 cachedRequest
= this.requests
.get(messageId
);
1500 if (Array.isArray(cachedRequest
) === true) {
1501 [responseCallback
, errorCallback
, requestCommandName
, requestPayload
] = cachedRequest
;
1503 throw new OCPPError(
1504 ErrorType
.PROTOCOL_ERROR
,
1505 `Cached request for message id ${messageId} response is not an array`,
1507 cachedRequest
as unknown
as JsonType
1511 `${this.logPrefix()} << Command '${
1512 requestCommandName ?? Constants.UNKNOWN_COMMAND
1513 }' received response payload: ${JSON.stringify(request)}`
1515 responseCallback(commandPayload
, requestPayload
);
1518 case MessageType
.CALL_ERROR_MESSAGE
:
1519 [, , errorType
, errorMessage
, errorDetails
] = request
as ErrorResponse
;
1520 if (this.requests
.has(messageId
) === false) {
1522 throw new OCPPError(
1523 ErrorType
.INTERNAL_ERROR
,
1524 `Error response for unknown message id ${messageId}`,
1526 { errorType
, errorMessage
, errorDetails
}
1529 cachedRequest
= this.requests
.get(messageId
);
1530 if (Array.isArray(cachedRequest
) === true) {
1531 [, errorCallback
, requestCommandName
] = cachedRequest
;
1533 throw new OCPPError(
1534 ErrorType
.PROTOCOL_ERROR
,
1535 `Cached request for message id ${messageId} error response is not an array`,
1537 cachedRequest
as unknown
as JsonType
1541 `${this.logPrefix()} << Command '${
1542 requestCommandName ?? Constants.UNKNOWN_COMMAND
1543 }' received error payload: ${JSON.stringify(request)}`
1545 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1549 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1550 errMsg
= `Wrong message type ${messageType}`;
1551 logger
.error(`${this.logPrefix()} ${errMsg}`);
1552 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1554 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1556 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1563 `${this.logPrefix()} Incoming OCPP command '${
1564 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1565 }' message '${data.toString()}'${
1566 messageType !== MessageType.CALL_MESSAGE
1567 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1569 } processing error:`,
1572 if (error
instanceof OCPPError
=== false) {
1574 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1575 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1576 }' message '${data.toString()}' handling is not an OCPPError:`,
1580 switch (messageType
) {
1581 case MessageType
.CALL_MESSAGE
:
1583 await this.ocppRequestService
.sendError(
1587 commandName
?? requestCommandName
?? null
1590 case MessageType
.CALL_RESULT_MESSAGE
:
1591 case MessageType
.CALL_ERROR_MESSAGE
:
1592 if (errorCallback
) {
1593 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1594 errorCallback(error
as OCPPError
, false);
1596 // Remove the request from the cache in case of error at response handling
1597 this.requests
.delete(messageId
);
1604 private onPing(): void {
1605 logger
.debug(this.logPrefix() + ' Received a WS ping (rfc6455) from the server');
1608 private onPong(): void {
1609 logger
.debug(this.logPrefix() + ' Received a WS pong (rfc6455) from the server');
1612 private onError(error
: WSError
): void {
1613 this.closeWSConnection();
1614 logger
.error(this.logPrefix() + ' WebSocket error:', error
);
1617 private getEnergyActiveImportRegister(
1618 connectorStatus
: ConnectorStatus
,
1621 if (this.getMeteringPerTransaction() === true) {
1624 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1625 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1630 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1631 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1635 private getUseConnectorId0(stationInfo
?: ChargingStationInfo
): boolean {
1636 const localStationInfo
= stationInfo
?? this.stationInfo
;
1637 return !Utils
.isUndefined(localStationInfo
.useConnectorId0
)
1638 ? localStationInfo
.useConnectorId0
1642 private getNumberOfRunningTransactions(): number {
1644 for (const connectorId
of this.connectors
.keys()) {
1645 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1652 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1653 for (const connectorId
of this.connectors
.keys()) {
1654 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1655 await this.stopTransactionOnConnector(connectorId
, reason
);
1661 private getConnectionTimeout(): number {
1663 ChargingStationConfigurationUtils
.getConfigurationKey(
1665 StandardParametersKey
.ConnectionTimeOut
1670 ChargingStationConfigurationUtils
.getConfigurationKey(
1672 StandardParametersKey
.ConnectionTimeOut
1674 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1677 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1680 // -1 for unlimited, 0 for disabling
1681 private getAutoReconnectMaxRetries(): number {
1682 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1683 return this.stationInfo
.autoReconnectMaxRetries
;
1685 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1686 return Configuration
.getAutoReconnectMaxRetries();
1692 private getRegistrationMaxRetries(): number {
1693 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1694 return this.stationInfo
.registrationMaxRetries
;
1699 private getPowerDivider(): number {
1700 let powerDivider
= this.getNumberOfConnectors();
1701 if (this.stationInfo
?.powerSharedByConnectors
) {
1702 powerDivider
= this.getNumberOfRunningTransactions();
1704 return powerDivider
;
1707 private getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
1708 const localStationInfo
= stationInfo
?? this.stationInfo
;
1709 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
1712 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1713 const maximumPower
= this.getMaximumPower(stationInfo
);
1714 switch (this.getCurrentOutType(stationInfo
)) {
1715 case CurrentType
.AC
:
1716 return ACElectricUtils
.amperagePerPhaseFromPower(
1717 this.getNumberOfPhases(stationInfo
),
1718 maximumPower
/ this.getNumberOfConnectors(),
1719 this.getVoltageOut(stationInfo
)
1721 case CurrentType
.DC
:
1722 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1726 private getAmperageLimitation(): number | undefined {
1728 this.stationInfo
.amperageLimitationOcppKey
&&
1729 ChargingStationConfigurationUtils
.getConfigurationKey(
1731 this.stationInfo
.amperageLimitationOcppKey
1736 ChargingStationConfigurationUtils
.getConfigurationKey(
1738 this.stationInfo
.amperageLimitationOcppKey
1740 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1745 private getChargingProfilePowerLimit(connectorId
: number): number | undefined {
1746 let limit
: number, matchingChargingProfile
: ChargingProfile
;
1747 let chargingProfiles
: ChargingProfile
[] = [];
1748 // Get charging profiles for connector and sort by stack level
1749 chargingProfiles
= this.getConnectorStatus(connectorId
).chargingProfiles
.sort(
1750 (a
, b
) => b
.stackLevel
- a
.stackLevel
1752 // Get profiles on connector 0
1753 if (this.getConnectorStatus(0).chargingProfiles
) {
1754 chargingProfiles
.push(
1755 ...this.getConnectorStatus(0).chargingProfiles
.sort((a
, b
) => b
.stackLevel
- a
.stackLevel
)
1758 if (!Utils
.isEmptyArray(chargingProfiles
)) {
1759 const result
= ChargingStationUtils
.getLimitFromChargingProfiles(
1763 if (!Utils
.isNullOrUndefined(result
)) {
1764 limit
= result
.limit
;
1765 matchingChargingProfile
= result
.matchingChargingProfile
;
1766 switch (this.getCurrentOutType()) {
1767 case CurrentType
.AC
:
1769 matchingChargingProfile
.chargingSchedule
.chargingRateUnit
===
1770 ChargingRateUnitType
.WATT
1772 : ACElectricUtils
.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit
);
1774 case CurrentType
.DC
:
1776 matchingChargingProfile
.chargingSchedule
.chargingRateUnit
===
1777 ChargingRateUnitType
.WATT
1779 : DCElectricUtils
.power(this.getVoltageOut(), limit
);
1781 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
1782 if (limit
> connectorMaximumPower
) {
1784 `${this.logPrefix()} Charging profile id ${
1785 matchingChargingProfile.chargingProfileId
1786 } limit ${limit} is greater than connector id ${connectorId} maximum ${connectorMaximumPower}, dump charging profiles' stack: %j`,
1787 this.getConnectorStatus(connectorId
).chargingProfiles
1789 limit
= connectorMaximumPower
;
1796 private async startMessageSequence(): Promise
<void> {
1797 if (this.stationInfo
?.autoRegister
=== true) {
1798 await this.ocppRequestService
.requestHandler
<
1799 BootNotificationRequest
,
1800 BootNotificationResponse
1801 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1802 skipBufferingOnError
: true,
1805 // Start WebSocket ping
1806 this.startWebSocketPing();
1808 this.startHeartbeat();
1809 // Initialize connectors status
1810 for (const connectorId
of this.connectors
.keys()) {
1811 let chargePointStatus
: ChargePointStatus
;
1812 if (connectorId
=== 0) {
1815 !this.getConnectorStatus(connectorId
)?.status &&
1816 (this.isChargingStationAvailable() === false ||
1817 this.isConnectorAvailable(connectorId
) === false)
1819 chargePointStatus
= ChargePointStatus
.UNAVAILABLE
;
1821 !this.getConnectorStatus(connectorId
)?.status &&
1822 this.getConnectorStatus(connectorId
)?.bootStatus
1824 // Set boot status in template at startup
1825 chargePointStatus
= this.getConnectorStatus(connectorId
).bootStatus
;
1826 } else if (this.getConnectorStatus(connectorId
)?.status) {
1827 // Set previous status at startup
1828 chargePointStatus
= this.getConnectorStatus(connectorId
).status;
1830 // Set default status
1831 chargePointStatus
= ChargePointStatus
.AVAILABLE
;
1833 await this.ocppRequestService
.requestHandler
<
1834 StatusNotificationRequest
,
1835 StatusNotificationResponse
1836 >(this, RequestCommand
.STATUS_NOTIFICATION
, {
1838 status: chargePointStatus
,
1839 errorCode
: ChargePointErrorCode
.NO_ERROR
,
1841 this.getConnectorStatus(connectorId
).status = chargePointStatus
;
1843 if (this.stationInfo
?.firmwareStatus
=== FirmwareStatus
.Installing
) {
1844 await this.ocppRequestService
.requestHandler
<
1845 FirmwareStatusNotificationRequest
,
1846 FirmwareStatusNotificationResponse
1847 >(this, RequestCommand
.FIRMWARE_STATUS_NOTIFICATION
, {
1848 status: FirmwareStatus
.Installed
,
1850 this.stationInfo
.firmwareStatus
= FirmwareStatus
.Installed
;
1851 // TODO: bump firmware version
1854 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true) {
1855 this.startAutomaticTransactionGenerator();
1857 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
1860 private async stopMessageSequence(
1861 reason
: StopTransactionReason
= StopTransactionReason
.NONE
1863 // Stop WebSocket ping
1864 this.stopWebSocketPing();
1866 this.stopHeartbeat();
1867 // Stop ongoing transactions
1868 if (this.automaticTransactionGenerator
?.started
=== true) {
1869 this.stopAutomaticTransactionGenerator();
1871 await this.stopRunningTransactions(reason
);
1873 for (const connectorId
of this.connectors
.keys()) {
1874 if (connectorId
> 0) {
1875 await this.ocppRequestService
.requestHandler
<
1876 StatusNotificationRequest
,
1877 StatusNotificationResponse
1878 >(this, RequestCommand
.STATUS_NOTIFICATION
, {
1880 status: ChargePointStatus
.UNAVAILABLE
,
1881 errorCode
: ChargePointErrorCode
.NO_ERROR
,
1883 this.getConnectorStatus(connectorId
).status = null;
1888 private startWebSocketPing(): void {
1889 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
1891 StandardParametersKey
.WebSocketPingInterval
1893 ? Utils
.convertToInt(
1894 ChargingStationConfigurationUtils
.getConfigurationKey(
1896 StandardParametersKey
.WebSocketPingInterval
1900 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
1901 this.webSocketPingSetInterval
= setInterval(() => {
1902 if (this.isWebSocketConnectionOpened() === true) {
1903 this.wsConnection
.ping();
1905 }, webSocketPingInterval
* 1000);
1908 ' WebSocket ping started every ' +
1909 Utils
.formatDurationSeconds(webSocketPingInterval
)
1911 } else if (this.webSocketPingSetInterval
) {
1914 ' WebSocket ping already started every ' +
1915 Utils
.formatDurationSeconds(webSocketPingInterval
)
1919 `${this.logPrefix()} WebSocket ping interval set to ${
1920 webSocketPingInterval
1921 ? Utils.formatDurationSeconds(webSocketPingInterval)
1922 : webSocketPingInterval
1923 }, not starting the WebSocket ping`
1928 private stopWebSocketPing(): void {
1929 if (this.webSocketPingSetInterval
) {
1930 clearInterval(this.webSocketPingSetInterval
);
1934 private getConfiguredSupervisionUrl(): URL
{
1935 const supervisionUrls
= this.stationInfo
.supervisionUrls
?? Configuration
.getSupervisionUrls();
1936 if (!Utils
.isEmptyArray(supervisionUrls
)) {
1937 switch (Configuration
.getSupervisionUrlDistribution()) {
1938 case SupervisionUrlDistribution
.ROUND_ROBIN
:
1940 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1942 case SupervisionUrlDistribution
.RANDOM
:
1943 this.configuredSupervisionUrlIndex
= Math.floor(
1944 Utils
.secureRandom() * supervisionUrls
.length
1947 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
1948 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1952 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
1953 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
1956 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1959 return new URL(supervisionUrls
[this.configuredSupervisionUrlIndex
]);
1961 return new URL(supervisionUrls
as string);
1964 private getHeartbeatInterval(): number {
1965 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1967 StandardParametersKey
.HeartbeatInterval
1969 if (HeartbeatInterval
) {
1970 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
1972 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1974 StandardParametersKey
.HeartBeatInterval
1976 if (HeartBeatInterval
) {
1977 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
1979 this.stationInfo
?.autoRegister
=== false &&
1981 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
1982 Constants.DEFAULT_HEARTBEAT_INTERVAL
1985 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
1988 private stopHeartbeat(): void {
1989 if (this.heartbeatSetInterval
) {
1990 clearInterval(this.heartbeatSetInterval
);
1994 private terminateWSConnection(): void {
1995 if (this.isWebSocketConnectionOpened() === true) {
1996 this.wsConnection
.terminate();
1997 this.wsConnection
= null;
2001 private stopMeterValues(connectorId
: number) {
2002 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
2003 clearInterval(this.getConnectorStatus(connectorId
).transactionSetInterval
);
2007 private getReconnectExponentialDelay(): boolean {
2008 return !Utils
.isUndefined(this.stationInfo
.reconnectExponentialDelay
)
2009 ? this.stationInfo
.reconnectExponentialDelay
2013 private async reconnect(): Promise
<void> {
2014 // Stop WebSocket ping
2015 this.stopWebSocketPing();
2017 this.stopHeartbeat();
2018 // Stop the ATG if needed
2019 if (this.automaticTransactionGenerator
?.configuration
?.stopOnConnectionFailure
=== true) {
2020 this.stopAutomaticTransactionGenerator();
2023 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
2024 this.getAutoReconnectMaxRetries() === -1
2026 this.autoReconnectRetryCount
++;
2027 const reconnectDelay
= this.getReconnectExponentialDelay()
2028 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
2029 : this.getConnectionTimeout() * 1000;
2030 const reconnectDelayWithdraw
= 1000;
2031 const reconnectTimeout
=
2032 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
2033 ? reconnectDelay
- reconnectDelayWithdraw
2036 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2039 )}ms, timeout ${reconnectTimeout}ms`
2041 await Utils
.sleep(reconnectDelay
);
2043 this.logPrefix() + ' WebSocket connection retry #' + this.autoReconnectRetryCount
.toString()
2045 this.openWSConnection(
2046 { ...(this.stationInfo
?.wsOptions
?? {}), handshakeTimeout
: reconnectTimeout
},
2047 { closeOpened
: true }
2049 this.wsConnectionRestarted
= true;
2050 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2052 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2053 this.autoReconnectRetryCount
2054 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2059 private getAutomaticTransactionGeneratorConfigurationFromTemplate(): AutomaticTransactionGeneratorConfiguration
| null {
2060 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
?? null;
2063 private initializeConnectorStatus(connectorId
: number): void {
2064 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
2065 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
2066 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
2067 this.getConnectorStatus(connectorId
).transactionStarted
= false;
2068 this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
= 0;
2069 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;