1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
5 BootNotificationRequest
,
8 IncomingRequestCommand
,
10 } from
'../types/ocpp/Requests';
11 import { BootNotificationResponse
, RegistrationStatus
} from
'../types/ocpp/Responses';
12 import ChargingStationConfiguration
, {
14 } from
'../types/ChargingStationConfiguration';
15 import ChargingStationTemplate
, {
19 } from
'../types/ChargingStationTemplate';
21 ConnectorPhaseRotation
,
22 StandardParametersKey
,
23 SupportedFeatureProfiles
,
24 VendorDefaultParametersKey
,
25 } from
'../types/ocpp/Configuration';
26 import { MeterValueMeasurand
, MeterValuePhase
} from
'../types/ocpp/MeterValues';
27 import { WSError
, WebSocketCloseEventStatusCode
} from
'../types/WebSocket';
28 import WebSocket
, { ClientOptions
, Data
, OPEN
} from
'ws';
30 import AutomaticTransactionGenerator from
'./AutomaticTransactionGenerator';
31 import { ChargePointStatus
} from
'../types/ocpp/ChargePointStatus';
32 import { ChargingProfile
} from
'../types/ocpp/ChargingProfile';
33 import ChargingStationInfo from
'../types/ChargingStationInfo';
34 import { ChargingStationWorkerMessageEvents
} from
'../types/ChargingStationWorker';
35 import { ClientRequestArgs
} from
'http';
36 import Configuration from
'../utils/Configuration';
37 import { ConnectorStatus
} from
'../types/ConnectorStatus';
38 import Constants from
'../utils/Constants';
39 import { ErrorType
} from
'../types/ocpp/ErrorType';
40 import FileUtils from
'../utils/FileUtils';
41 import { JsonType
} from
'../types/JsonType';
42 import { MessageType
} from
'../types/ocpp/MessageType';
43 import OCPP16IncomingRequestService from
'./ocpp/1.6/OCPP16IncomingRequestService';
44 import OCPP16RequestService from
'./ocpp/1.6/OCPP16RequestService';
45 import OCPP16ResponseService from
'./ocpp/1.6/OCPP16ResponseService';
46 import OCPPError from
'../exception/OCPPError';
47 import OCPPIncomingRequestService from
'./ocpp/OCPPIncomingRequestService';
48 import OCPPRequestService from
'./ocpp/OCPPRequestService';
49 import { OCPPVersion
} from
'../types/ocpp/OCPPVersion';
50 import PerformanceStatistics from
'../performance/PerformanceStatistics';
51 import { SampledValueTemplate
} from
'../types/MeasurandPerPhaseSampledValueTemplates';
52 import { StopTransactionReason
} from
'../types/ocpp/Transaction';
53 import { SupervisionUrlDistribution
} from
'../types/ConfigurationData';
54 import { URL
} from
'url';
55 import Utils from
'../utils/Utils';
56 import crypto from
'crypto';
58 import logger from
'../utils/Logger';
59 import { parentPort
} from
'worker_threads';
60 import path from
'path';
62 export default class ChargingStation
{
63 public readonly id
: string;
64 public readonly stationTemplateFile
: string;
65 public authorizedTags
: string[];
66 public stationInfo
!: ChargingStationInfo
;
67 public readonly connectors
: Map
<number, ConnectorStatus
>;
68 public configuration
!: ChargingStationConfiguration
;
69 public wsConnection
!: WebSocket
;
70 public readonly requests
: Map
<string, CachedRequest
>;
71 public performanceStatistics
!: PerformanceStatistics
;
72 public heartbeatSetInterval
!: NodeJS
.Timeout
;
73 public ocppRequestService
!: OCPPRequestService
;
74 private readonly index
: number;
75 private bootNotificationRequest
!: BootNotificationRequest
;
76 private bootNotificationResponse
!: BootNotificationResponse
| null;
77 private connectorsConfigurationHash
!: string;
78 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
79 private readonly messageBuffer
: Set
<string>;
80 private wsConfiguredConnectionUrl
!: URL
;
81 private wsConnectionRestarted
: boolean;
82 private stopped
: boolean;
83 private autoReconnectRetryCount
: number;
84 private automaticTransactionGenerator
!: AutomaticTransactionGenerator
;
85 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
87 constructor(index
: number, stationTemplateFile
: string) {
88 this.id
= Utils
.generateUUID();
90 this.stationTemplateFile
= stationTemplateFile
;
92 this.wsConnectionRestarted
= false;
93 this.autoReconnectRetryCount
= 0;
94 this.connectors
= new Map
<number, ConnectorStatus
>();
95 this.requests
= new Map
<string, CachedRequest
>();
96 this.messageBuffer
= new Set
<string>();
98 this.authorizedTags
= this.getAuthorizedTags();
101 get
wsConnectionUrl(): URL
{
102 return this.getSupervisionUrlOcppConfiguration()
104 this.getConfigurationKey(
105 this.stationInfo
.supervisionUrlOcppKey
?? VendorDefaultParametersKey
.ConnectionUrl
108 this.stationInfo
.chargingStationId
110 : this.wsConfiguredConnectionUrl
;
113 public logPrefix(): string {
114 return Utils
.logPrefix(` ${this.stationInfo.chargingStationId} |`);
117 public getBootNotificationRequest(): BootNotificationRequest
{
118 return this.bootNotificationRequest
;
121 public getRandomIdTag(): string {
122 const index
= Math.floor(Utils
.secureRandom() * this.authorizedTags
.length
);
123 return this.authorizedTags
[index
];
126 public hasAuthorizedTags(): boolean {
127 return !Utils
.isEmptyArray(this.authorizedTags
);
130 public getEnableStatistics(): boolean | undefined {
131 return !Utils
.isUndefined(this.stationInfo
.enableStatistics
)
132 ? this.stationInfo
.enableStatistics
136 public getMayAuthorizeAtRemoteStart(): boolean | undefined {
137 return this.stationInfo
.mayAuthorizeAtRemoteStart
?? true;
140 public getNumberOfPhases(): number | undefined {
141 switch (this.getCurrentOutType()) {
143 return !Utils
.isUndefined(this.stationInfo
.numberOfPhases
)
144 ? this.stationInfo
.numberOfPhases
151 public isWebSocketConnectionOpened(): boolean {
152 return this?.wsConnection
?.readyState
=== OPEN
;
155 public getRegistrationStatus(): RegistrationStatus
{
156 return this?.bootNotificationResponse
?.status;
159 public isInUnknownState(): boolean {
160 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
163 public isInPendingState(): boolean {
164 return this?.bootNotificationResponse
?.status === RegistrationStatus
.PENDING
;
167 public isInAcceptedState(): boolean {
168 return this?.bootNotificationResponse
?.status === RegistrationStatus
.ACCEPTED
;
171 public isInRejectedState(): boolean {
172 return this?.bootNotificationResponse
?.status === RegistrationStatus
.REJECTED
;
175 public isRegistered(): boolean {
176 return !this.isInUnknownState() && (this.isInAcceptedState() || this.isInPendingState());
179 public isChargingStationAvailable(): boolean {
180 return this.getConnectorStatus(0).availability
=== AvailabilityType
.OPERATIVE
;
183 public isConnectorAvailable(id
: number): boolean {
184 return id
> 0 && this.getConnectorStatus(id
).availability
=== AvailabilityType
.OPERATIVE
;
187 public getNumberOfConnectors(): number {
188 return this.connectors
.get(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
191 public getConnectorStatus(id
: number): ConnectorStatus
{
192 return this.connectors
.get(id
);
195 public getCurrentOutType(): CurrentType
| undefined {
196 return this.stationInfo
.currentOutType
?? CurrentType
.AC
;
199 public getOcppStrictCompliance(): boolean {
200 return this.stationInfo
.ocppStrictCompliance
?? false;
203 public getVoltageOut(): number | undefined {
204 const errMsg
= `${this.logPrefix()} Unknown ${this.getCurrentOutType()} currentOutType in template file ${
205 this.stationTemplateFile
206 }, cannot define default voltage out`;
207 let defaultVoltageOut
: number;
208 switch (this.getCurrentOutType()) {
210 defaultVoltageOut
= Voltage
.VOLTAGE_230
;
213 defaultVoltageOut
= Voltage
.VOLTAGE_400
;
216 logger
.error(errMsg
);
217 throw new Error(errMsg
);
219 return !Utils
.isUndefined(this.stationInfo
.voltageOut
)
220 ? this.stationInfo
.voltageOut
224 public getTransactionIdTag(transactionId
: number): string | undefined {
225 for (const connectorId
of this.connectors
.keys()) {
226 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionId
=== transactionId
) {
227 return this.getConnectorStatus(connectorId
).transactionIdTag
;
232 public getOutOfOrderEndMeterValues(): boolean {
233 return this.stationInfo
.outOfOrderEndMeterValues
?? false;
236 public getBeginEndMeterValues(): boolean {
237 return this.stationInfo
.beginEndMeterValues
?? false;
240 public getMeteringPerTransaction(): boolean {
241 return this.stationInfo
.meteringPerTransaction
?? true;
244 public getTransactionDataMeterValues(): boolean {
245 return this.stationInfo
.transactionDataMeterValues
?? false;
248 public getMainVoltageMeterValues(): boolean {
249 return this.stationInfo
.mainVoltageMeterValues
?? true;
252 public getPhaseLineToLineVoltageMeterValues(): boolean {
253 return this.stationInfo
.phaseLineToLineVoltageMeterValues
?? false;
256 public getEnergyActiveImportRegisterByTransactionId(transactionId
: number): number | undefined {
257 if (this.getMeteringPerTransaction()) {
258 for (const connectorId
of this.connectors
.keys()) {
261 this.getConnectorStatus(connectorId
).transactionId
=== transactionId
263 return this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
;
267 for (const connectorId
of this.connectors
.keys()) {
268 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionId
=== transactionId
) {
269 return this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
;
274 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number): number | undefined {
275 if (this.getMeteringPerTransaction()) {
276 return this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
;
278 return this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
;
281 public getAuthorizeRemoteTxRequests(): boolean {
282 const authorizeRemoteTxRequests
= this.getConfigurationKey(
283 StandardParametersKey
.AuthorizeRemoteTxRequests
285 return authorizeRemoteTxRequests
286 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
290 public getLocalAuthListEnabled(): boolean {
291 const localAuthListEnabled
= this.getConfigurationKey(
292 StandardParametersKey
.LocalAuthListEnabled
294 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
297 public restartWebSocketPing(): void {
298 // Stop WebSocket ping
299 this.stopWebSocketPing();
300 // Start WebSocket ping
301 this.startWebSocketPing();
304 public getSampledValueTemplate(
306 measurand
: MeterValueMeasurand
= MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
,
307 phase
?: MeterValuePhase
308 ): SampledValueTemplate
| undefined {
309 const onPhaseStr
= phase
? `on phase ${phase} ` : '';
310 if (!Constants
.SUPPORTED_MEASURANDS
.includes(measurand
)) {
312 `${this.logPrefix()} Trying to get unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
317 measurand
!== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
318 !this.getConfigurationKey(StandardParametersKey
.MeterValuesSampledData
).value
.includes(
323 `${this.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${
324 StandardParametersKey.MeterValuesSampledData
329 const sampledValueTemplates
: SampledValueTemplate
[] =
330 this.getConnectorStatus(connectorId
).MeterValues
;
333 !Utils
.isEmptyArray(sampledValueTemplates
) && index
< sampledValueTemplates
.length
;
337 !Constants
.SUPPORTED_MEASURANDS
.includes(
338 sampledValueTemplates
[index
]?.measurand
??
339 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
343 `${this.logPrefix()} Unsupported MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
347 sampledValueTemplates
[index
]?.phase
=== phase
&&
348 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
349 this.getConfigurationKey(StandardParametersKey
.MeterValuesSampledData
).value
.includes(
353 return sampledValueTemplates
[index
];
356 !sampledValueTemplates
[index
].phase
&&
357 sampledValueTemplates
[index
]?.measurand
=== measurand
&&
358 this.getConfigurationKey(StandardParametersKey
.MeterValuesSampledData
).value
.includes(
362 return sampledValueTemplates
[index
];
364 measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
&&
365 (!sampledValueTemplates
[index
].measurand
||
366 sampledValueTemplates
[index
].measurand
=== measurand
)
368 return sampledValueTemplates
[index
];
371 if (measurand
=== MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
) {
372 const errorMsg
= `${this.logPrefix()} Missing MeterValues for default measurand '${measurand}' in template on connectorId ${connectorId}`;
373 logger
.error(errorMsg
);
374 throw new Error(errorMsg
);
377 `${this.logPrefix()} No MeterValues for measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId}`
381 public getAutomaticTransactionGeneratorRequireAuthorize(): boolean {
382 return this.stationInfo
.AutomaticTransactionGenerator
.requireAuthorize
?? true;
385 public startHeartbeat(): void {
387 this.getHeartbeatInterval() &&
388 this.getHeartbeatInterval() > 0 &&
389 !this.heartbeatSetInterval
391 // eslint-disable-next-line @typescript-eslint/no-misused-promises
392 this.heartbeatSetInterval
= setInterval(async (): Promise
<void> => {
393 await this.ocppRequestService
.sendHeartbeat();
394 }, this.getHeartbeatInterval());
397 ' Heartbeat started every ' +
398 Utils
.formatDurationMilliSeconds(this.getHeartbeatInterval())
400 } else if (this.heartbeatSetInterval
) {
403 ' Heartbeat already started every ' +
404 Utils
.formatDurationMilliSeconds(this.getHeartbeatInterval())
408 `${this.logPrefix()} Heartbeat interval set to ${
409 this.getHeartbeatInterval()
410 ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
411 : this.getHeartbeatInterval()
412 }, not starting the heartbeat`
417 public restartHeartbeat(): void {
419 this.stopHeartbeat();
421 this.startHeartbeat();
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
) {
439 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
443 this.getConnectorStatus(connectorId
)?.transactionStarted
&&
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 async (): Promise
<void> => {
455 await this.ocppRequestService
.sendMeterValues(
457 this.getConnectorStatus(connectorId
).transactionId
,
465 `${this.logPrefix()} Charging station ${
466 StandardParametersKey.MeterValueSampleInterval
467 } configuration set to ${
468 interval ? Utils.formatDurationMilliSeconds(interval) : interval
469 }, not sending MeterValues`
474 public start(): void {
475 if (this.getEnableStatistics()) {
476 this.performanceStatistics
.start();
478 this.openWSConnection();
479 // Monitor authorization file
480 this.startAuthorizationFileMonitoring();
481 // Monitor station template file
482 this.startStationTemplateFileMonitoring();
483 // Handle WebSocket message
484 this.wsConnection
.on('message', this.onMessage
.bind(this));
485 // Handle WebSocket error
486 this.wsConnection
.on('error', this.onError
.bind(this));
487 // Handle WebSocket close
488 this.wsConnection
.on('close', this.onClose
.bind(this));
489 // Handle WebSocket open
490 this.wsConnection
.on('open', this.onOpen
.bind(this));
491 // Handle WebSocket ping
492 this.wsConnection
.on('ping', this.onPing
.bind(this));
493 // Handle WebSocket pong
494 this.wsConnection
.on('pong', this.onPong
.bind(this));
495 parentPort
.postMessage({
496 id
: ChargingStationWorkerMessageEvents
.STARTED
,
497 data
: { id
: this.stationInfo
.chargingStationId
},
501 public async stop(reason
: StopTransactionReason
= StopTransactionReason
.NONE
): Promise
<void> {
502 // Stop message sequence
503 await this.stopMessageSequence(reason
);
504 for (const connectorId
of this.connectors
.keys()) {
505 if (connectorId
> 0) {
506 await this.ocppRequestService
.sendStatusNotification(
508 ChargePointStatus
.UNAVAILABLE
510 this.getConnectorStatus(connectorId
).status = ChargePointStatus
.UNAVAILABLE
;
513 if (this.isWebSocketConnectionOpened()) {
514 this.wsConnection
.close();
516 if (this.getEnableStatistics()) {
517 this.performanceStatistics
.stop();
519 this.bootNotificationResponse
= null;
520 parentPort
.postMessage({
521 id
: ChargingStationWorkerMessageEvents
.STOPPED
,
522 data
: { id
: this.stationInfo
.chargingStationId
},
527 public getConfigurationKey(
528 key
: string | StandardParametersKey
,
529 caseInsensitive
= false
530 ): ConfigurationKey
| undefined {
531 return this.configuration
.configurationKey
.find((configElement
) => {
532 if (caseInsensitive
) {
533 return configElement
.key
.toLowerCase() === key
.toLowerCase();
535 return configElement
.key
=== key
;
539 public addConfigurationKey(
540 key
: string | StandardParametersKey
,
542 options
: { readonly?: boolean; visible
?: boolean; reboot
?: boolean } = {
548 const keyFound
= this.getConfigurationKey(key
);
549 const readonly = options
.readonly;
550 const visible
= options
.visible
;
551 const reboot
= options
.reboot
;
553 this.configuration
.configurationKey
.push({
562 `${this.logPrefix()} Trying to add an already existing configuration key: %j`,
568 public setConfigurationKeyValue(key
: string | StandardParametersKey
, value
: string): void {
569 const keyFound
= this.getConfigurationKey(key
);
571 const keyIndex
= this.configuration
.configurationKey
.indexOf(keyFound
);
572 this.configuration
.configurationKey
[keyIndex
].value
= value
;
575 `${this.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
581 public setChargingProfile(connectorId
: number, cp
: ChargingProfile
): void {
582 let cpReplaced
= false;
583 if (!Utils
.isEmptyArray(this.getConnectorStatus(connectorId
).chargingProfiles
)) {
584 this.getConnectorStatus(connectorId
).chargingProfiles
?.forEach(
585 (chargingProfile
: ChargingProfile
, index
: number) => {
587 chargingProfile
.chargingProfileId
=== cp
.chargingProfileId
||
588 (chargingProfile
.stackLevel
=== cp
.stackLevel
&&
589 chargingProfile
.chargingProfilePurpose
=== cp
.chargingProfilePurpose
)
591 this.getConnectorStatus(connectorId
).chargingProfiles
[index
] = cp
;
597 !cpReplaced
&& this.getConnectorStatus(connectorId
).chargingProfiles
?.push(cp
);
600 public resetConnectorStatus(connectorId
: number): void {
601 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
602 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
603 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
604 this.getConnectorStatus(connectorId
).transactionStarted
= false;
605 delete this.getConnectorStatus(connectorId
).localAuthorizeIdTag
;
606 delete this.getConnectorStatus(connectorId
).authorizeIdTag
;
607 delete this.getConnectorStatus(connectorId
).transactionId
;
608 delete this.getConnectorStatus(connectorId
).transactionIdTag
;
609 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;
610 delete this.getConnectorStatus(connectorId
).transactionBeginMeterValue
;
611 this.stopMeterValues(connectorId
);
614 public bufferMessage(message
: string): void {
615 this.messageBuffer
.add(message
);
618 private flushMessageBuffer() {
619 if (this.messageBuffer
.size
> 0) {
620 this.messageBuffer
.forEach((message
) => {
621 // TODO: evaluate the need to track performance
622 this.wsConnection
.send(message
);
623 this.messageBuffer
.delete(message
);
628 private getSupervisionUrlOcppConfiguration(): boolean {
629 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
632 private getChargingStationId(stationTemplate
: ChargingStationTemplate
): string {
633 // In case of multiple instances: add instance index to charging station id
634 const instanceIndex
= process
.env
.CF_INSTANCE_INDEX
?? 0;
635 const idSuffix
= stationTemplate
.nameSuffix
?? '';
636 return stationTemplate
.fixedName
637 ? stationTemplate
.baseName
638 : stationTemplate
.baseName
+
640 instanceIndex
.toString() +
641 ('000000000' + this.index
.toString()).substr(
642 ('000000000' + this.index
.toString()).length
- 4
647 private buildStationInfo(): ChargingStationInfo
{
648 let stationTemplateFromFile
: ChargingStationTemplate
;
650 // Load template file
651 const fileDescriptor
= fs
.openSync(this.stationTemplateFile
, 'r');
652 stationTemplateFromFile
= JSON
.parse(
653 fs
.readFileSync(fileDescriptor
, 'utf8')
654 ) as ChargingStationTemplate
;
655 fs
.closeSync(fileDescriptor
);
657 FileUtils
.handleFileException(
660 this.stationTemplateFile
,
661 error
as NodeJS
.ErrnoException
664 const chargingStationId
= this.getChargingStationId(stationTemplateFromFile
);
665 // Deprecation template keys section
666 this.warnDeprecatedTemplateKey(
667 stationTemplateFromFile
,
670 "Use 'supervisionUrls' instead"
672 this.convertDeprecatedTemplateKey(stationTemplateFromFile
, 'supervisionUrl', 'supervisionUrls');
673 const stationInfo
: ChargingStationInfo
= stationTemplateFromFile
?? ({} as ChargingStationInfo
);
674 stationInfo
.wsOptions
= stationTemplateFromFile
?.wsOptions
?? {};
675 if (!Utils
.isEmptyArray(stationTemplateFromFile
.power
)) {
676 stationTemplateFromFile
.power
= stationTemplateFromFile
.power
as number[];
677 const powerArrayRandomIndex
= Math.floor(
678 Utils
.secureRandom() * stationTemplateFromFile
.power
.length
680 stationInfo
.maxPower
=
681 stationTemplateFromFile
.powerUnit
=== PowerUnits
.KILO_WATT
682 ? stationTemplateFromFile
.power
[powerArrayRandomIndex
] * 1000
683 : stationTemplateFromFile
.power
[powerArrayRandomIndex
];
685 stationTemplateFromFile
.power
= stationTemplateFromFile
.power
as number;
686 stationInfo
.maxPower
=
687 stationTemplateFromFile
.powerUnit
=== PowerUnits
.KILO_WATT
688 ? stationTemplateFromFile
.power
* 1000
689 : stationTemplateFromFile
.power
;
691 delete stationInfo
.power
;
692 delete stationInfo
.powerUnit
;
693 stationInfo
.chargingStationId
= chargingStationId
;
694 stationInfo
.resetTime
= stationTemplateFromFile
.resetTime
695 ? stationTemplateFromFile
.resetTime
* 1000
696 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
700 private getOcppVersion(): OCPPVersion
{
701 return this.stationInfo
.ocppVersion
? this.stationInfo
.ocppVersion
: OCPPVersion
.VERSION_16
;
704 private handleUnsupportedVersion(version
: OCPPVersion
) {
705 const errMsg
= `${this.logPrefix()} Unsupported protocol version '${version}' configured in template file ${
706 this.stationTemplateFile
708 logger
.error(errMsg
);
709 throw new Error(errMsg
);
712 private initialize(): void {
713 this.stationInfo
= this.buildStationInfo();
714 this.configuration
= this.getTemplateChargingStationConfiguration();
715 delete this.stationInfo
.Configuration
;
716 this.bootNotificationRequest
= {
717 chargePointModel
: this.stationInfo
.chargePointModel
,
718 chargePointVendor
: this.stationInfo
.chargePointVendor
,
719 ...(!Utils
.isUndefined(this.stationInfo
.chargeBoxSerialNumberPrefix
) && {
720 chargeBoxSerialNumber
: this.stationInfo
.chargeBoxSerialNumberPrefix
,
722 ...(!Utils
.isUndefined(this.stationInfo
.firmwareVersion
) && {
723 firmwareVersion
: this.stationInfo
.firmwareVersion
,
726 // Build connectors if needed
727 const maxConnectors
= this.getMaxNumberOfConnectors();
728 if (maxConnectors
<= 0) {
730 `${this.logPrefix()} Charging station template ${
731 this.stationTemplateFile
732 } with ${maxConnectors} connectors`
735 const templateMaxConnectors
= this.getTemplateMaxNumberOfConnectors();
736 if (templateMaxConnectors
<= 0) {
738 `${this.logPrefix()} Charging station template ${
739 this.stationTemplateFile
740 } with no connector configuration`
743 if (!this.stationInfo
.Connectors
[0]) {
745 `${this.logPrefix()} Charging station template ${
746 this.stationTemplateFile
747 } with no connector Id 0 configuration`
753 (this.stationInfo
.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) &&
754 !this.stationInfo
.randomConnectors
757 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
758 this.stationTemplateFile
759 }, forcing random connector configurations affectation`
761 this.stationInfo
.randomConnectors
= true;
763 const connectorsConfigHash
= crypto
764 .createHash('sha256')
765 .update(JSON
.stringify(this.stationInfo
.Connectors
) + maxConnectors
.toString())
767 const connectorsConfigChanged
=
768 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
769 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
770 connectorsConfigChanged
&& this.connectors
.clear();
771 this.connectorsConfigurationHash
= connectorsConfigHash
;
772 // Add connector Id 0
773 let lastConnector
= '0';
774 for (lastConnector
in this.stationInfo
.Connectors
) {
775 const lastConnectorId
= Utils
.convertToInt(lastConnector
);
777 lastConnectorId
=== 0 &&
778 this.getUseConnectorId0() &&
779 this.stationInfo
.Connectors
[lastConnector
]
783 Utils
.cloneObject
<ConnectorStatus
>(this.stationInfo
.Connectors
[lastConnector
])
785 this.getConnectorStatus(lastConnectorId
).availability
= AvailabilityType
.OPERATIVE
;
786 if (Utils
.isUndefined(this.getConnectorStatus(lastConnectorId
)?.chargingProfiles
)) {
787 this.getConnectorStatus(lastConnectorId
).chargingProfiles
= [];
791 // Generate all connectors
793 (this.stationInfo
.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) > 0
795 for (let index
= 1; index
<= maxConnectors
; index
++) {
796 const randConnectorId
= this.stationInfo
.randomConnectors
797 ? Utils
.getRandomInteger(Utils
.convertToInt(lastConnector
), 1)
801 Utils
.cloneObject
<ConnectorStatus
>(this.stationInfo
.Connectors
[randConnectorId
])
803 this.getConnectorStatus(index
).availability
= AvailabilityType
.OPERATIVE
;
804 if (Utils
.isUndefined(this.getConnectorStatus(index
)?.chargingProfiles
)) {
805 this.getConnectorStatus(index
).chargingProfiles
= [];
810 // Avoid duplication of connectors related information
811 delete this.stationInfo
.Connectors
;
812 // Initialize transaction attributes on connectors
813 for (const connectorId
of this.connectors
.keys()) {
814 if (connectorId
> 0 && !this.getConnectorStatus(connectorId
)?.transactionStarted
) {
815 this.initializeConnectorStatus(connectorId
);
818 this.wsConfiguredConnectionUrl
= new URL(
819 this.getConfiguredSupervisionUrl().href
+ '/' + this.stationInfo
.chargingStationId
821 switch (this.getOcppVersion()) {
822 case OCPPVersion
.VERSION_16
:
823 this.ocppIncomingRequestService
=
824 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>(this);
825 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
827 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>(this)
831 this.handleUnsupportedVersion(this.getOcppVersion());
835 this.initOcppParameters();
836 if (this.stationInfo
.autoRegister
) {
837 this.bootNotificationResponse
= {
838 currentTime
: new Date().toISOString(),
839 interval
: this.getHeartbeatInterval() / 1000,
840 status: RegistrationStatus
.ACCEPTED
,
843 this.stationInfo
.powerDivider
= this.getPowerDivider();
844 if (this.getEnableStatistics()) {
845 this.performanceStatistics
= PerformanceStatistics
.getInstance(
847 this.stationInfo
.chargingStationId
,
853 private initOcppParameters(): void {
855 this.getSupervisionUrlOcppConfiguration() &&
856 !this.getConfigurationKey(
857 this.stationInfo
.supervisionUrlOcppKey
?? VendorDefaultParametersKey
.ConnectionUrl
860 this.addConfigurationKey(
861 VendorDefaultParametersKey
.ConnectionUrl
,
862 this.getConfiguredSupervisionUrl().href
,
866 if (!this.getConfigurationKey(StandardParametersKey
.SupportedFeatureProfiles
)) {
867 this.addConfigurationKey(
868 StandardParametersKey
.SupportedFeatureProfiles
,
869 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.Local_Auth_List_Management},${SupportedFeatureProfiles.Smart_Charging}`
872 this.addConfigurationKey(
873 StandardParametersKey
.NumberOfConnectors
,
874 this.getNumberOfConnectors().toString(),
877 if (!this.getConfigurationKey(StandardParametersKey
.MeterValuesSampledData
)) {
878 this.addConfigurationKey(
879 StandardParametersKey
.MeterValuesSampledData
,
880 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
883 if (!this.getConfigurationKey(StandardParametersKey
.ConnectorPhaseRotation
)) {
884 const connectorPhaseRotation
= [];
885 for (const connectorId
of this.connectors
.keys()) {
887 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
888 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
889 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
890 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
892 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
893 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
894 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
895 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
898 this.addConfigurationKey(
899 StandardParametersKey
.ConnectorPhaseRotation
,
900 connectorPhaseRotation
.toString()
903 if (!this.getConfigurationKey(StandardParametersKey
.AuthorizeRemoteTxRequests
)) {
904 this.addConfigurationKey(StandardParametersKey
.AuthorizeRemoteTxRequests
, 'true');
907 !this.getConfigurationKey(StandardParametersKey
.LocalAuthListEnabled
) &&
908 this.getConfigurationKey(StandardParametersKey
.SupportedFeatureProfiles
).value
.includes(
909 SupportedFeatureProfiles
.Local_Auth_List_Management
912 this.addConfigurationKey(StandardParametersKey
.LocalAuthListEnabled
, 'false');
914 if (!this.getConfigurationKey(StandardParametersKey
.ConnectionTimeOut
)) {
915 this.addConfigurationKey(
916 StandardParametersKey
.ConnectionTimeOut
,
917 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
922 private async onOpen(): Promise
<void> {
924 `${this.logPrefix()} Connected to OCPP server through ${this.wsConnectionUrl.toString()}`
926 if (!this.isInAcceptedState()) {
927 // Send BootNotification
928 let registrationRetryCount
= 0;
930 this.bootNotificationResponse
= await this.ocppRequestService
.sendBootNotification(
931 this.bootNotificationRequest
.chargePointModel
,
932 this.bootNotificationRequest
.chargePointVendor
,
933 this.bootNotificationRequest
.chargeBoxSerialNumber
,
934 this.bootNotificationRequest
.firmwareVersion
936 if (!this.isInAcceptedState()) {
937 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
939 this.bootNotificationResponse
?.interval
940 ? this.bootNotificationResponse
.interval
* 1000
941 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
945 !this.isInAcceptedState() &&
946 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
947 this.getRegistrationMaxRetries() === -1)
950 if (this.isInAcceptedState()) {
951 await this.startMessageSequence();
952 this.stopped
&& (this.stopped
= false);
953 if (this.wsConnectionRestarted
&& this.isWebSocketConnectionOpened()) {
954 this.flushMessageBuffer();
958 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
961 this.autoReconnectRetryCount
= 0;
962 this.wsConnectionRestarted
= false;
965 private async onClose(code
: number, reason
: string): Promise
<void> {
968 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
969 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
971 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
973 )}' and reason '${reason}'`
975 this.autoReconnectRetryCount
= 0;
980 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
982 )}' and reason '${reason}'`
984 await this.reconnect(code
);
989 private async onMessage(data
: Data
): Promise
<void> {
990 let [messageType
, messageId
, commandName
, commandPayload
, errorDetails
]: IncomingRequest
= [
993 '' as IncomingRequestCommand
,
997 let responseCallback
: (
998 payload
: JsonType
| string,
999 requestPayload
: JsonType
| OCPPError
1001 let rejectCallback
: (error
: OCPPError
, requestStatistic
?: boolean) => void;
1002 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1003 let requestPayload
: JsonType
| OCPPError
;
1004 let cachedRequest
: CachedRequest
;
1007 const request
= JSON
.parse(data
.toString()) as IncomingRequest
;
1008 if (Utils
.isIterable(request
)) {
1009 // Parse the message
1010 [messageType
, messageId
, commandName
, commandPayload
, errorDetails
] = request
;
1012 throw new OCPPError(
1013 ErrorType
.PROTOCOL_ERROR
,
1014 'Incoming request is not iterable',
1018 // Check the Type of message
1019 switch (messageType
) {
1021 case MessageType
.CALL_MESSAGE
:
1022 if (this.getEnableStatistics()) {
1023 this.performanceStatistics
.addRequestStatistic(commandName
, messageType
);
1026 await this.ocppIncomingRequestService
.handleRequest(
1033 case MessageType
.CALL_RESULT_MESSAGE
:
1035 cachedRequest
= this.requests
.get(messageId
);
1036 if (Utils
.isIterable(cachedRequest
)) {
1037 [responseCallback
, , , requestPayload
] = cachedRequest
;
1039 throw new OCPPError(
1040 ErrorType
.PROTOCOL_ERROR
,
1041 `Cached request for message id ${messageId} response is not iterable`,
1045 if (!responseCallback
) {
1047 throw new OCPPError(
1048 ErrorType
.INTERNAL_ERROR
,
1049 `Response for unknown message id ${messageId}`,
1053 responseCallback(commandName
, requestPayload
);
1056 case MessageType
.CALL_ERROR_MESSAGE
:
1057 cachedRequest
= this.requests
.get(messageId
);
1058 if (Utils
.isIterable(cachedRequest
)) {
1059 [, rejectCallback
, requestCommandName
] = cachedRequest
;
1061 throw new OCPPError(
1062 ErrorType
.PROTOCOL_ERROR
,
1063 `Cached request for message id ${messageId} error response is not iterable`
1066 if (!rejectCallback
) {
1068 throw new OCPPError(
1069 ErrorType
.INTERNAL_ERROR
,
1070 `Error response for unknown message id ${messageId}`,
1075 new OCPPError(commandName
, commandPayload
.toString(), requestCommandName
, errorDetails
)
1080 errMsg
= `${this.logPrefix()} Wrong message type ${messageType}`;
1081 logger
.error(errMsg
);
1082 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1087 '%s Incoming OCPP message %j matching cached request %j processing error %j',
1090 this.requests
.get(messageId
),
1094 messageType
=== MessageType
.CALL_MESSAGE
&&
1095 (await this.ocppRequestService
.sendError(messageId
, error
as OCPPError
, commandName
));
1099 private onPing(): void {
1100 logger
.debug(this.logPrefix() + ' Received a WS ping (rfc6455) from the server');
1103 private onPong(): void {
1104 logger
.debug(this.logPrefix() + ' Received a WS pong (rfc6455) from the server');
1107 private async onError(error
: WSError
): Promise
<void> {
1108 logger
.error(this.logPrefix() + ' WebSocket error: %j', error
);
1109 // switch (error.code) {
1110 // case 'ECONNREFUSED':
1111 // await this.reconnect(error);
1116 private getTemplateChargingStationConfiguration(): ChargingStationConfiguration
{
1117 return this.stationInfo
.Configuration
?? ({} as ChargingStationConfiguration
);
1120 private getAuthorizationFile(): string | undefined {
1122 this.stationInfo
.authorizationFile
&&
1124 path
.resolve(__dirname
, '../'),
1126 path
.basename(this.stationInfo
.authorizationFile
)
1131 private getAuthorizedTags(): string[] {
1132 let authorizedTags
: string[] = [];
1133 const authorizationFile
= this.getAuthorizationFile();
1134 if (authorizationFile
) {
1136 // Load authorization file
1137 const fileDescriptor
= fs
.openSync(authorizationFile
, 'r');
1138 authorizedTags
= JSON
.parse(fs
.readFileSync(fileDescriptor
, 'utf8')) as string[];
1139 fs
.closeSync(fileDescriptor
);
1141 FileUtils
.handleFileException(
1145 error
as NodeJS
.ErrnoException
1151 ' No authorization file given in template file ' +
1152 this.stationTemplateFile
1155 return authorizedTags
;
1158 private getUseConnectorId0(): boolean | undefined {
1159 return !Utils
.isUndefined(this.stationInfo
.useConnectorId0
)
1160 ? this.stationInfo
.useConnectorId0
1164 private getNumberOfRunningTransactions(): number {
1166 for (const connectorId
of this.connectors
.keys()) {
1167 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
) {
1175 private getConnectionTimeout(): number | undefined {
1176 if (this.getConfigurationKey(StandardParametersKey
.ConnectionTimeOut
)) {
1178 parseInt(this.getConfigurationKey(StandardParametersKey
.ConnectionTimeOut
).value
) ??
1179 Constants
.DEFAULT_CONNECTION_TIMEOUT
1182 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1185 // -1 for unlimited, 0 for disabling
1186 private getAutoReconnectMaxRetries(): number | undefined {
1187 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1188 return this.stationInfo
.autoReconnectMaxRetries
;
1190 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1191 return Configuration
.getAutoReconnectMaxRetries();
1197 private getRegistrationMaxRetries(): number | undefined {
1198 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1199 return this.stationInfo
.registrationMaxRetries
;
1204 private getPowerDivider(): number {
1205 let powerDivider
= this.getNumberOfConnectors();
1206 if (this.stationInfo
.powerSharedByConnectors
) {
1207 powerDivider
= this.getNumberOfRunningTransactions();
1209 return powerDivider
;
1212 private getTemplateMaxNumberOfConnectors(): number {
1213 return Object.keys(this.stationInfo
.Connectors
).length
;
1216 private getMaxNumberOfConnectors(): number {
1217 let maxConnectors
: number;
1218 if (!Utils
.isEmptyArray(this.stationInfo
.numberOfConnectors
)) {
1219 const numberOfConnectors
= this.stationInfo
.numberOfConnectors
as number[];
1220 // Distribute evenly the number of connectors
1221 maxConnectors
= numberOfConnectors
[(this.index
- 1) % numberOfConnectors
.length
];
1222 } else if (!Utils
.isUndefined(this.stationInfo
.numberOfConnectors
)) {
1223 maxConnectors
= this.stationInfo
.numberOfConnectors
as number;
1225 maxConnectors
= this.stationInfo
.Connectors
[0]
1226 ? this.getTemplateMaxNumberOfConnectors() - 1
1227 : this.getTemplateMaxNumberOfConnectors();
1229 return maxConnectors
;
1232 private async startMessageSequence(): Promise
<void> {
1233 if (this.stationInfo
.autoRegister
) {
1234 await this.ocppRequestService
.sendBootNotification(
1235 this.bootNotificationRequest
.chargePointModel
,
1236 this.bootNotificationRequest
.chargePointVendor
,
1237 this.bootNotificationRequest
.chargeBoxSerialNumber
,
1238 this.bootNotificationRequest
.firmwareVersion
1241 // Start WebSocket ping
1242 this.startWebSocketPing();
1244 this.startHeartbeat();
1245 // Initialize connectors status
1246 for (const connectorId
of this.connectors
.keys()) {
1247 if (connectorId
=== 0) {
1251 !this.getConnectorStatus(connectorId
)?.status &&
1252 this.getConnectorStatus(connectorId
)?.bootStatus
1254 // Send status in template at startup
1255 await this.ocppRequestService
.sendStatusNotification(
1257 this.getConnectorStatus(connectorId
).bootStatus
1259 this.getConnectorStatus(connectorId
).status =
1260 this.getConnectorStatus(connectorId
).bootStatus
;
1263 this.getConnectorStatus(connectorId
)?.status &&
1264 this.getConnectorStatus(connectorId
)?.bootStatus
1266 // Send status in template after reset
1267 await this.ocppRequestService
.sendStatusNotification(
1269 this.getConnectorStatus(connectorId
).bootStatus
1271 this.getConnectorStatus(connectorId
).status =
1272 this.getConnectorStatus(connectorId
).bootStatus
;
1273 } else if (!this.stopped
&& this.getConnectorStatus(connectorId
)?.status) {
1274 // Send previous status at template reload
1275 await this.ocppRequestService
.sendStatusNotification(
1277 this.getConnectorStatus(connectorId
).status
1280 // Send default status
1281 await this.ocppRequestService
.sendStatusNotification(
1283 ChargePointStatus
.AVAILABLE
1285 this.getConnectorStatus(connectorId
).status = ChargePointStatus
.AVAILABLE
;
1289 this.startAutomaticTransactionGenerator();
1292 private startAutomaticTransactionGenerator() {
1293 if (this.stationInfo
.AutomaticTransactionGenerator
.enable
) {
1294 if (!this.automaticTransactionGenerator
) {
1295 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(this);
1297 if (!this.automaticTransactionGenerator
.started
) {
1298 this.automaticTransactionGenerator
.start();
1303 private async stopMessageSequence(
1304 reason
: StopTransactionReason
= StopTransactionReason
.NONE
1306 // Stop WebSocket ping
1307 this.stopWebSocketPing();
1309 this.stopHeartbeat();
1312 this.stationInfo
.AutomaticTransactionGenerator
.enable
&&
1313 this.automaticTransactionGenerator
?.started
1315 this.automaticTransactionGenerator
.stop();
1317 for (const connectorId
of this.connectors
.keys()) {
1318 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
) {
1319 const transactionId
= this.getConnectorStatus(connectorId
).transactionId
;
1320 await this.ocppRequestService
.sendStopTransaction(
1322 this.getEnergyActiveImportRegisterByTransactionId(transactionId
),
1323 this.getTransactionIdTag(transactionId
),
1331 private startWebSocketPing(): void {
1332 const webSocketPingInterval
: number = this.getConfigurationKey(
1333 StandardParametersKey
.WebSocketPingInterval
1335 ? Utils
.convertToInt(
1336 this.getConfigurationKey(StandardParametersKey
.WebSocketPingInterval
).value
1339 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
1340 this.webSocketPingSetInterval
= setInterval(() => {
1341 if (this.isWebSocketConnectionOpened()) {
1342 this.wsConnection
.ping((): void => {
1343 /* This is intentional */
1346 }, webSocketPingInterval
* 1000);
1349 ' WebSocket ping started every ' +
1350 Utils
.formatDurationSeconds(webSocketPingInterval
)
1352 } else if (this.webSocketPingSetInterval
) {
1355 ' WebSocket ping every ' +
1356 Utils
.formatDurationSeconds(webSocketPingInterval
) +
1361 `${this.logPrefix()} WebSocket ping interval set to ${
1362 webSocketPingInterval
1363 ? Utils.formatDurationSeconds(webSocketPingInterval)
1364 : webSocketPingInterval
1365 }, not starting the WebSocket ping`
1370 private stopWebSocketPing(): void {
1371 if (this.webSocketPingSetInterval
) {
1372 clearInterval(this.webSocketPingSetInterval
);
1376 private warnDeprecatedTemplateKey(
1377 template
: ChargingStationTemplate
,
1379 chargingStationId
: string,
1382 if (!Utils
.isUndefined(template
[key
])) {
1383 const logPrefixStr
= ` ${chargingStationId} |`;
1385 `${Utils.logPrefix(logPrefixStr)} Deprecated template key '${key}' usage in file '${
1386 this.stationTemplateFile
1387 }'${logMsgToAppend && '. ' + logMsgToAppend}`
1392 private convertDeprecatedTemplateKey(
1393 template
: ChargingStationTemplate
,
1394 deprecatedKey
: string,
1397 if (!Utils
.isUndefined(template
[deprecatedKey
])) {
1398 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1399 template
[key
] = template
[deprecatedKey
];
1400 delete template
[deprecatedKey
];
1404 private getConfiguredSupervisionUrl(): URL
{
1405 const supervisionUrls
= Utils
.cloneObject
<string | string[]>(
1406 this.stationInfo
.supervisionUrls
?? Configuration
.getSupervisionUrls()
1408 if (!Utils
.isEmptyArray(supervisionUrls
)) {
1410 switch (Configuration
.getSupervisionUrlDistribution()) {
1411 case SupervisionUrlDistribution
.ROUND_ROBIN
:
1412 urlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1414 case SupervisionUrlDistribution
.RANDOM
:
1416 urlIndex
= Math.floor(Utils
.secureRandom() * supervisionUrls
.length
);
1418 case SupervisionUrlDistribution
.SEQUENTIAL
:
1419 if (this.index
<= supervisionUrls
.length
) {
1420 urlIndex
= this.index
- 1;
1423 `${this.logPrefix()} No more configured supervision urls available, using the first one`
1429 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
1430 SupervisionUrlDistribution.ROUND_ROBIN
1433 urlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1436 return new URL(supervisionUrls
[urlIndex
]);
1438 return new URL(supervisionUrls
as string);
1441 private getHeartbeatInterval(): number | undefined {
1442 const HeartbeatInterval
= this.getConfigurationKey(StandardParametersKey
.HeartbeatInterval
);
1443 if (HeartbeatInterval
) {
1444 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
1446 const HeartBeatInterval
= this.getConfigurationKey(StandardParametersKey
.HeartBeatInterval
);
1447 if (HeartBeatInterval
) {
1448 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
1450 !this.stationInfo
.autoRegister
&&
1452 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
1453 Constants.DEFAULT_HEARTBEAT_INTERVAL
1456 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
1459 private stopHeartbeat(): void {
1460 if (this.heartbeatSetInterval
) {
1461 clearInterval(this.heartbeatSetInterval
);
1465 private openWSConnection(
1466 options
: ClientOptions
& ClientRequestArgs
= this.stationInfo
.wsOptions
,
1467 forceCloseOpened
= false
1469 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
1471 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
1472 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
1474 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
1476 if (this.isWebSocketConnectionOpened() && forceCloseOpened
) {
1477 this.wsConnection
.close();
1479 let protocol
: string;
1480 switch (this.getOcppVersion()) {
1481 case OCPPVersion
.VERSION_16
:
1482 protocol
= 'ocpp' + OCPPVersion
.VERSION_16
;
1485 this.handleUnsupportedVersion(this.getOcppVersion());
1488 this.wsConnection
= new WebSocket(this.wsConnectionUrl
, protocol
, options
);
1490 this.logPrefix() + ' Open OCPP connection to URL ' + this.wsConnectionUrl
.toString()
1494 private stopMeterValues(connectorId
: number) {
1495 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
1496 clearInterval(this.getConnectorStatus(connectorId
).transactionSetInterval
);
1500 private startAuthorizationFileMonitoring(): void {
1501 const authorizationFile
= this.getAuthorizationFile();
1502 if (authorizationFile
) {
1504 fs
.watch(authorizationFile
, (event
, filename
) => {
1505 if (filename
&& event
=== 'change') {
1509 ' Authorization file ' +
1511 ' have changed, reload'
1513 // Initialize authorizedTags
1514 this.authorizedTags
= this.getAuthorizedTags();
1516 logger
.error(this.logPrefix() + ' Authorization file monitoring error: %j', error
);
1521 FileUtils
.handleFileException(
1525 error
as NodeJS
.ErrnoException
1531 ' No authorization file given in template file ' +
1532 this.stationTemplateFile
+
1533 '. Not monitoring changes'
1538 private startStationTemplateFileMonitoring(): void {
1540 fs
.watch(this.stationTemplateFile
, (event
, filename
): void => {
1541 if (filename
&& event
=== 'change') {
1546 this.stationTemplateFile
+
1547 ' have changed, reload'
1553 !this.stationInfo
.AutomaticTransactionGenerator
.enable
&&
1554 this.automaticTransactionGenerator
1556 this.automaticTransactionGenerator
.stop();
1558 this.startAutomaticTransactionGenerator();
1559 if (this.getEnableStatistics()) {
1560 this.performanceStatistics
.restart();
1562 this.performanceStatistics
.stop();
1564 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
1567 this.logPrefix() + ' Charging station template file monitoring error: %j',
1574 FileUtils
.handleFileException(
1577 this.stationTemplateFile
,
1578 error
as NodeJS
.ErrnoException
1583 private getReconnectExponentialDelay(): boolean | undefined {
1584 return !Utils
.isUndefined(this.stationInfo
.reconnectExponentialDelay
)
1585 ? this.stationInfo
.reconnectExponentialDelay
1589 private async reconnect(code
: number): Promise
<void> {
1590 // Stop WebSocket ping
1591 this.stopWebSocketPing();
1593 this.stopHeartbeat();
1594 // Stop the ATG if needed
1596 this.stationInfo
.AutomaticTransactionGenerator
.enable
&&
1597 this.stationInfo
.AutomaticTransactionGenerator
.stopOnConnectionFailure
&&
1598 this.automaticTransactionGenerator
?.started
1600 this.automaticTransactionGenerator
.stop();
1603 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
1604 this.getAutoReconnectMaxRetries() === -1
1606 this.autoReconnectRetryCount
++;
1607 const reconnectDelay
= this.getReconnectExponentialDelay()
1608 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
1609 : this.getConnectionTimeout() * 1000;
1610 const reconnectTimeout
= reconnectDelay
- 100 > 0 && reconnectDelay
;
1612 `${this.logPrefix()} WebSocket: connection retry in ${Utils.roundTo(
1615 )}ms, timeout ${reconnectTimeout}ms`
1617 await Utils
.sleep(reconnectDelay
);
1620 ' WebSocket: reconnecting try #' +
1621 this.autoReconnectRetryCount
.toString()
1623 this.openWSConnection(
1624 { ...this.stationInfo
.wsOptions
, handshakeTimeout
: reconnectTimeout
},
1627 this.wsConnectionRestarted
= true;
1628 } else if (this.getAutoReconnectMaxRetries() !== -1) {
1630 `${this.logPrefix()} WebSocket reconnect failure: max retries reached (${
1631 this.autoReconnectRetryCount
1632 }) or retry disabled (${this.getAutoReconnectMaxRetries()})`
1637 private initializeConnectorStatus(connectorId
: number): void {
1638 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
1639 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
1640 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
1641 this.getConnectorStatus(connectorId
).transactionStarted
= false;
1642 this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
= 0;
1643 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;