1 // Partial Copyright Jerome Benoit. 2021. 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
, { Data
, 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 BootNotificationRequest
,
48 IncomingRequestCommand
,
52 StatusNotificationRequest
,
53 } from
'../types/ocpp/Requests';
55 BootNotificationResponse
,
61 StatusNotificationResponse
,
62 } from
'../types/ocpp/Responses';
64 StopTransactionReason
,
65 StopTransactionRequest
,
66 StopTransactionResponse
,
67 } from
'../types/ocpp/Transaction';
68 import { WSError
, WebSocketCloseEventStatusCode
} from
'../types/WebSocket';
69 import Configuration from
'../utils/Configuration';
70 import Constants from
'../utils/Constants';
71 import { ACElectricUtils
, DCElectricUtils
} from
'../utils/ElectricUtils';
72 import FileUtils from
'../utils/FileUtils';
73 import logger from
'../utils/Logger';
74 import Utils from
'../utils/Utils';
75 import AuthorizedTagsCache from
'./AuthorizedTagsCache';
76 import AutomaticTransactionGenerator from
'./AutomaticTransactionGenerator';
77 import { ChargingStationConfigurationUtils
} from
'./ChargingStationConfigurationUtils';
78 import { ChargingStationUtils
} from
'./ChargingStationUtils';
79 import ChargingStationWorkerBroadcastChannel from
'./ChargingStationWorkerBroadcastChannel';
80 import { MessageChannelUtils
} from
'./MessageChannelUtils';
81 import OCPP16IncomingRequestService from
'./ocpp/1.6/OCPP16IncomingRequestService';
82 import OCPP16RequestService from
'./ocpp/1.6/OCPP16RequestService';
83 import OCPP16ResponseService from
'./ocpp/1.6/OCPP16ResponseService';
84 import { OCPP16ServiceUtils
} from
'./ocpp/1.6/OCPP16ServiceUtils';
85 import type OCPPIncomingRequestService from
'./ocpp/OCPPIncomingRequestService';
86 import type OCPPRequestService from
'./ocpp/OCPPRequestService';
87 import SharedLRUCache from
'./SharedLRUCache';
89 export default class ChargingStation
{
90 public readonly index
: number;
91 public readonly templateFile
: string;
92 public stationInfo
!: ChargingStationInfo
;
93 public started
: boolean;
94 public authorizedTagsCache
: AuthorizedTagsCache
;
95 public automaticTransactionGenerator
!: AutomaticTransactionGenerator
;
96 public ocppConfiguration
!: ChargingStationOcppConfiguration
;
97 public wsConnection
!: WebSocket
;
98 public readonly connectors
: Map
<number, ConnectorStatus
>;
99 public readonly requests
: Map
<string, CachedRequest
>;
100 public performanceStatistics
!: PerformanceStatistics
;
101 public heartbeatSetInterval
!: NodeJS
.Timeout
;
102 public ocppRequestService
!: OCPPRequestService
;
103 public bootNotificationRequest
!: BootNotificationRequest
;
104 public bootNotificationResponse
!: BootNotificationResponse
| null;
105 public powerDivider
!: number;
106 private starting
: boolean;
107 private stopping
: boolean;
108 private configurationFile
!: string;
109 private configurationFileHash
!: string;
110 private connectorsConfigurationHash
!: string;
111 private ocppIncomingRequestService
!: OCPPIncomingRequestService
;
112 private readonly messageBuffer
: Set
<string>;
113 private configuredSupervisionUrl
!: URL
;
114 private configuredSupervisionUrlIndex
!: number;
115 private wsConnectionRestarted
: boolean;
116 private autoReconnectRetryCount
: number;
117 private templateFileWatcher
!: fs
.FSWatcher
;
118 private readonly sharedLRUCache
: SharedLRUCache
;
119 private webSocketPingSetInterval
!: NodeJS
.Timeout
;
120 private readonly chargingStationWorkerBroadcastChannel
: ChargingStationWorkerBroadcastChannel
;
122 constructor(index
: number, templateFile
: string) {
123 this.started
= false;
124 this.starting
= false;
125 this.stopping
= false;
126 this.wsConnectionRestarted
= false;
127 this.autoReconnectRetryCount
= 0;
129 this.templateFile
= templateFile
;
130 this.connectors
= new Map
<number, ConnectorStatus
>();
131 this.requests
= new Map
<string, CachedRequest
>();
132 this.messageBuffer
= new Set
<string>();
133 this.sharedLRUCache
= SharedLRUCache
.getInstance();
134 this.authorizedTagsCache
= AuthorizedTagsCache
.getInstance();
135 this.chargingStationWorkerBroadcastChannel
= new ChargingStationWorkerBroadcastChannel(this);
140 private get
wsConnectionUrl(): URL
{
142 (this.getSupervisionUrlOcppConfiguration()
143 ? ChargingStationConfigurationUtils
.getConfigurationKey(
145 this.getSupervisionUrlOcppKey()
147 : this.configuredSupervisionUrl
.href
) +
149 this.stationInfo
.chargingStationId
153 public logPrefix(): string {
154 return Utils
.logPrefix(
156 this?.stationInfo?.chargingStationId ??
157 ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())
162 public hasAuthorizedTags(): boolean {
163 return !Utils
.isEmptyArray(
164 this.authorizedTagsCache
.getAuthorizedTags(
165 ChargingStationUtils
.getAuthorizationFile(this.stationInfo
)
170 public getEnableStatistics(): boolean | undefined {
171 return !Utils
.isUndefined(this.stationInfo
.enableStatistics
)
172 ? this.stationInfo
.enableStatistics
176 public getMustAuthorizeAtRemoteStart(): boolean | undefined {
177 return this.stationInfo
.mustAuthorizeAtRemoteStart
?? true;
180 public getPayloadSchemaValidation(): boolean | undefined {
181 return this.stationInfo
.payloadSchemaValidation
?? true;
184 public getNumberOfPhases(stationInfo
?: ChargingStationInfo
): number | undefined {
185 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
186 switch (this.getCurrentOutType(stationInfo
)) {
188 return !Utils
.isUndefined(localStationInfo
.numberOfPhases
)
189 ? localStationInfo
.numberOfPhases
196 public isWebSocketConnectionOpened(): boolean {
197 return this?.wsConnection
?.readyState
=== WebSocket
.OPEN
;
200 public getRegistrationStatus(): RegistrationStatus
{
201 return this?.bootNotificationResponse
?.status;
204 public isInUnknownState(): boolean {
205 return Utils
.isNullOrUndefined(this?.bootNotificationResponse
?.status);
208 public isInPendingState(): boolean {
209 return this?.bootNotificationResponse
?.status === RegistrationStatus
.PENDING
;
212 public isInAcceptedState(): boolean {
213 return this?.bootNotificationResponse
?.status === RegistrationStatus
.ACCEPTED
;
216 public isInRejectedState(): boolean {
217 return this?.bootNotificationResponse
?.status === RegistrationStatus
.REJECTED
;
220 public isRegistered(): boolean {
222 this.isInUnknownState() === false &&
223 (this.isInAcceptedState() === true || this.isInPendingState() === true)
227 public isChargingStationAvailable(): boolean {
228 return this.getConnectorStatus(0).availability
=== AvailabilityType
.OPERATIVE
;
231 public isConnectorAvailable(id
: number): boolean {
232 return id
> 0 && this.getConnectorStatus(id
).availability
=== AvailabilityType
.OPERATIVE
;
235 public getNumberOfConnectors(): number {
236 return this.connectors
.get(0) ? this.connectors
.size
- 1 : this.connectors
.size
;
239 public getConnectorStatus(id
: number): ConnectorStatus
| undefined {
240 return this.connectors
.get(id
);
243 public getCurrentOutType(stationInfo
?: ChargingStationInfo
): CurrentType
{
244 return (stationInfo
?? this.stationInfo
).currentOutType
?? CurrentType
.AC
;
247 public getOcppStrictCompliance(): boolean {
248 return this.stationInfo
?.ocppStrictCompliance
?? false;
251 public getVoltageOut(stationInfo
?: ChargingStationInfo
): number | undefined {
252 const defaultVoltageOut
= ChargingStationUtils
.getDefaultVoltageOut(
253 this.getCurrentOutType(stationInfo
),
257 const localStationInfo
: ChargingStationInfo
= stationInfo
?? this.stationInfo
;
258 return !Utils
.isUndefined(localStationInfo
.voltageOut
)
259 ? localStationInfo
.voltageOut
263 public getConnectorMaximumAvailablePower(connectorId
: number): number {
264 let connectorAmperageLimitationPowerLimit
: number;
266 !Utils
.isNullOrUndefined(this.getAmperageLimitation()) &&
267 this.getAmperageLimitation() < this.stationInfo
.maximumAmperage
269 connectorAmperageLimitationPowerLimit
=
270 (this.getCurrentOutType() === CurrentType
.AC
271 ? ACElectricUtils
.powerTotal(
272 this.getNumberOfPhases(),
273 this.getVoltageOut(),
274 this.getAmperageLimitation() * this.getNumberOfConnectors()
276 : DCElectricUtils
.power(this.getVoltageOut(), this.getAmperageLimitation())) /
279 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
280 const connectorChargingProfilePowerLimit
= this.getChargingProfilePowerLimit(connectorId
);
282 isNaN(connectorMaximumPower
) ? Infinity : connectorMaximumPower
,
283 isNaN(connectorAmperageLimitationPowerLimit
)
285 : connectorAmperageLimitationPowerLimit
,
286 isNaN(connectorChargingProfilePowerLimit
) ? Infinity : connectorChargingProfilePowerLimit
290 public getTransactionIdTag(transactionId
: number): string | undefined {
291 for (const connectorId
of this.connectors
.keys()) {
292 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionId
=== transactionId
) {
293 return this.getConnectorStatus(connectorId
).transactionIdTag
;
298 public getOutOfOrderEndMeterValues(): boolean {
299 return this.stationInfo
?.outOfOrderEndMeterValues
?? false;
302 public getBeginEndMeterValues(): boolean {
303 return this.stationInfo
?.beginEndMeterValues
?? false;
306 public getMeteringPerTransaction(): boolean {
307 return this.stationInfo
?.meteringPerTransaction
?? true;
310 public getTransactionDataMeterValues(): boolean {
311 return this.stationInfo
?.transactionDataMeterValues
?? false;
314 public getMainVoltageMeterValues(): boolean {
315 return this.stationInfo
?.mainVoltageMeterValues
?? true;
318 public getPhaseLineToLineVoltageMeterValues(): boolean {
319 return this.stationInfo
?.phaseLineToLineVoltageMeterValues
?? false;
322 public getCustomValueLimitationMeterValues(): boolean {
323 return this.stationInfo
?.customValueLimitationMeterValues
?? true;
326 public getConnectorIdByTransactionId(transactionId
: number): number | undefined {
327 for (const connectorId
of this.connectors
.keys()) {
330 this.getConnectorStatus(connectorId
)?.transactionId
=== transactionId
337 public getEnergyActiveImportRegisterByTransactionId(
338 transactionId
: number,
341 return this.getEnergyActiveImportRegister(
342 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId
)),
347 public getEnergyActiveImportRegisterByConnectorId(connectorId
: number): number {
348 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId
));
351 public getAuthorizeRemoteTxRequests(): boolean {
352 const authorizeRemoteTxRequests
= ChargingStationConfigurationUtils
.getConfigurationKey(
354 StandardParametersKey
.AuthorizeRemoteTxRequests
356 return authorizeRemoteTxRequests
357 ? Utils
.convertToBoolean(authorizeRemoteTxRequests
.value
)
361 public getLocalAuthListEnabled(): boolean {
362 const localAuthListEnabled
= ChargingStationConfigurationUtils
.getConfigurationKey(
364 StandardParametersKey
.LocalAuthListEnabled
366 return localAuthListEnabled
? Utils
.convertToBoolean(localAuthListEnabled
.value
) : false;
369 public startHeartbeat(): void {
371 this.getHeartbeatInterval() &&
372 this.getHeartbeatInterval() > 0 &&
373 !this.heartbeatSetInterval
375 // eslint-disable-next-line @typescript-eslint/no-misused-promises
376 this.heartbeatSetInterval
= setInterval(async (): Promise
<void> => {
377 await this.ocppRequestService
.requestHandler
<HeartbeatRequest
, HeartbeatResponse
>(
379 RequestCommand
.HEARTBEAT
381 }, this.getHeartbeatInterval());
384 ' Heartbeat started every ' +
385 Utils
.formatDurationMilliSeconds(this.getHeartbeatInterval())
387 } else if (this.heartbeatSetInterval
) {
390 ' Heartbeat already started every ' +
391 Utils
.formatDurationMilliSeconds(this.getHeartbeatInterval())
395 `${this.logPrefix()} Heartbeat interval set to ${
396 this.getHeartbeatInterval()
397 ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
398 : this.getHeartbeatInterval()
399 }, not starting the heartbeat`
404 public restartHeartbeat(): void {
406 this.stopHeartbeat();
408 this.startHeartbeat();
411 public restartWebSocketPing(): void {
412 // Stop WebSocket ping
413 this.stopWebSocketPing();
414 // Start WebSocket ping
415 this.startWebSocketPing();
418 public startMeterValues(connectorId
: number, interval
: number): void {
419 if (connectorId
=== 0) {
421 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`
425 if (!this.getConnectorStatus(connectorId
)) {
427 `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`
431 if (this.getConnectorStatus(connectorId
)?.transactionStarted
=== false) {
433 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
437 this.getConnectorStatus(connectorId
)?.transactionStarted
=== true &&
438 !this.getConnectorStatus(connectorId
)?.transactionId
441 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`
446 // eslint-disable-next-line @typescript-eslint/no-misused-promises
447 this.getConnectorStatus(connectorId
).transactionSetInterval
= setInterval(
448 // eslint-disable-next-line @typescript-eslint/no-misused-promises
449 async (): Promise
<void> => {
450 // FIXME: Implement OCPP version agnostic helpers
451 const meterValue
: MeterValue
= OCPP16ServiceUtils
.buildMeterValue(
454 this.getConnectorStatus(connectorId
).transactionId
,
457 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
459 RequestCommand
.METER_VALUES
,
462 transactionId
: this.getConnectorStatus(connectorId
).transactionId
,
463 meterValue
: [meterValue
],
471 `${this.logPrefix()} Charging station ${
472 StandardParametersKey.MeterValueSampleInterval
473 } configuration set to ${
474 interval ? Utils.formatDurationMilliSeconds(interval) : interval
475 }, not sending MeterValues`
480 public start(): void {
481 if (this.started
=== false) {
482 if (this.starting
=== false) {
483 this.starting
= true;
484 if (this.getEnableStatistics()) {
485 this.performanceStatistics
.start();
487 this.openWSConnection();
488 // Monitor charging station template file
489 this.templateFileWatcher
= FileUtils
.watchJsonFile(
491 FileType
.ChargingStationTemplate
,
494 (event
, filename
): void => {
495 if (filename
&& event
=== 'change') {
498 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
500 } file have changed, reload`
502 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
506 this.stopAutomaticTransactionGenerator();
508 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true
510 this.startAutomaticTransactionGenerator();
512 if (this.getEnableStatistics()) {
513 this.performanceStatistics
.restart();
515 this.performanceStatistics
.stop();
517 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
520 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
528 parentPort
.postMessage(MessageChannelUtils
.buildStartedMessage(this));
529 this.starting
= false;
531 logger
.warn(`${this.logPrefix()} Charging station is already starting...`);
534 logger
.warn(`${this.logPrefix()} Charging station is already started...`);
538 public async stop(reason
?: StopTransactionReason
): Promise
<void> {
539 if (this.started
=== true) {
540 if (this.stopping
=== false) {
541 this.stopping
= true;
542 await this.stopMessageSequence(reason
);
543 this.closeWSConnection();
544 if (this.getEnableStatistics()) {
545 this.performanceStatistics
.stop();
547 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
548 this.templateFileWatcher
.close();
549 this.sharedLRUCache
.deleteChargingStationTemplate(this.stationInfo
?.templateHash
);
550 this.bootNotificationResponse
= null;
551 this.started
= false;
552 parentPort
.postMessage(MessageChannelUtils
.buildStoppedMessage(this));
553 this.stopping
= false;
555 logger
.warn(`${this.logPrefix()} Charging station is already stopping...`);
558 logger
.warn(`${this.logPrefix()} Charging station is already stopped...`);
562 public async reset(reason
?: StopTransactionReason
): Promise
<void> {
563 await this.stop(reason
);
564 await Utils
.sleep(this.stationInfo
.resetTime
);
569 public saveOcppConfiguration(): void {
570 if (this.getOcppPersistentConfiguration()) {
571 this.saveConfiguration();
575 public resetConnectorStatus(connectorId
: number): void {
576 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
577 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
578 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
579 this.getConnectorStatus(connectorId
).transactionStarted
= false;
580 delete this.getConnectorStatus(connectorId
).localAuthorizeIdTag
;
581 delete this.getConnectorStatus(connectorId
).authorizeIdTag
;
582 delete this.getConnectorStatus(connectorId
).transactionId
;
583 delete this.getConnectorStatus(connectorId
).transactionIdTag
;
584 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;
585 delete this.getConnectorStatus(connectorId
).transactionBeginMeterValue
;
586 this.stopMeterValues(connectorId
);
587 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
590 public hasFeatureProfile(featureProfile
: SupportedFeatureProfiles
): boolean {
591 return ChargingStationConfigurationUtils
.getConfigurationKey(
593 StandardParametersKey
.SupportedFeatureProfiles
594 )?.value
.includes(featureProfile
);
597 public bufferMessage(message
: string): void {
598 this.messageBuffer
.add(message
);
601 public openWSConnection(
602 options
: WsOptions
= this.stationInfo
?.wsOptions
?? {},
603 params
: { closeOpened
?: boolean; terminateOpened
?: boolean } = {
605 terminateOpened
: false,
608 options
.handshakeTimeout
= options
?.handshakeTimeout
?? this.getConnectionTimeout() * 1000;
609 params
.closeOpened
= params
?.closeOpened
?? false;
610 params
.terminateOpened
= params
?.terminateOpened
?? false;
612 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionUser
) &&
613 !Utils
.isNullOrUndefined(this.stationInfo
.supervisionPassword
)
615 options
.auth
= `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
617 if (params
?.closeOpened
) {
618 this.closeWSConnection();
620 if (params
?.terminateOpened
) {
621 this.terminateWSConnection();
623 let protocol
: string;
624 switch (this.getOcppVersion()) {
625 case OCPPVersion
.VERSION_16
:
626 protocol
= 'ocpp' + OCPPVersion
.VERSION_16
;
629 this.handleUnsupportedVersion(this.getOcppVersion());
633 if (this.isWebSocketConnectionOpened() === true) {
635 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
641 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
644 this.wsConnection
= new WebSocket(this.wsConnectionUrl
, protocol
, options
);
646 // Handle WebSocket message
647 this.wsConnection
.on(
649 this.onMessage
.bind(this) as (this: WebSocket
, data
: RawData
, isBinary
: boolean) => void
651 // Handle WebSocket error
652 this.wsConnection
.on(
654 this.onError
.bind(this) as (this: WebSocket
, error
: Error) => void
656 // Handle WebSocket close
657 this.wsConnection
.on(
659 this.onClose
.bind(this) as (this: WebSocket
, code
: number, reason
: Buffer
) => void
661 // Handle WebSocket open
662 this.wsConnection
.on('open', this.onOpen
.bind(this) as (this: WebSocket
) => void);
663 // Handle WebSocket ping
664 this.wsConnection
.on('ping', this.onPing
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
665 // Handle WebSocket pong
666 this.wsConnection
.on('pong', this.onPong
.bind(this) as (this: WebSocket
, data
: Buffer
) => void);
669 public closeWSConnection(): void {
670 if (this.isWebSocketConnectionOpened() === true) {
671 this.wsConnection
.close();
672 this.wsConnection
= null;
676 public startAutomaticTransactionGenerator(
677 connectorIds
?: number[],
678 automaticTransactionGeneratorConfiguration
?: AutomaticTransactionGeneratorConfiguration
680 this.automaticTransactionGenerator
= AutomaticTransactionGenerator
.getInstance(
681 automaticTransactionGeneratorConfiguration
??
682 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
685 if (!Utils
.isEmptyArray(connectorIds
)) {
686 for (const connectorId
of connectorIds
) {
687 this.automaticTransactionGenerator
.startConnector(connectorId
);
690 this.automaticTransactionGenerator
.start();
692 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
695 public stopAutomaticTransactionGenerator(connectorIds
?: number[]): void {
696 if (!Utils
.isEmptyArray(connectorIds
)) {
697 for (const connectorId
of connectorIds
) {
698 this.automaticTransactionGenerator
?.stopConnector(connectorId
);
701 this.automaticTransactionGenerator
?.stop();
703 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
706 public async stopTransactionOnConnector(
708 reason
= StopTransactionReason
.NONE
709 ): Promise
<StopTransactionResponse
> {
710 const transactionId
= this.getConnectorStatus(connectorId
).transactionId
;
712 this.getBeginEndMeterValues() === true &&
713 this.getOcppStrictCompliance() === true &&
714 this.getOutOfOrderEndMeterValues() === false
716 // FIXME: Implement OCPP version agnostic helpers
717 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(
720 this.getEnergyActiveImportRegisterByTransactionId(transactionId
)
722 await this.ocppRequestService
.requestHandler
<MeterValuesRequest
, MeterValuesResponse
>(
724 RequestCommand
.METER_VALUES
,
728 meterValue
: [transactionEndMeterValue
],
732 return this.ocppRequestService
.requestHandler
<StopTransactionRequest
, StopTransactionResponse
>(
734 RequestCommand
.STOP_TRANSACTION
,
737 meterStop
: this.getEnergyActiveImportRegisterByTransactionId(transactionId
, true),
743 private flushMessageBuffer(): void {
744 if (this.messageBuffer
.size
> 0) {
745 this.messageBuffer
.forEach((message
) => {
746 // TODO: evaluate the need to track performance
747 this.wsConnection
.send(message
);
748 this.messageBuffer
.delete(message
);
753 private getSupervisionUrlOcppConfiguration(): boolean {
754 return this.stationInfo
.supervisionUrlOcppConfiguration
?? false;
757 private getSupervisionUrlOcppKey(): string {
758 return this.stationInfo
.supervisionUrlOcppKey
?? VendorDefaultParametersKey
.ConnectionUrl
;
761 private getTemplateFromFile(): ChargingStationTemplate
| null {
762 let template
: ChargingStationTemplate
= null;
764 if (this.sharedLRUCache
.hasChargingStationTemplate(this.stationInfo
?.templateHash
)) {
765 template
= this.sharedLRUCache
.getChargingStationTemplate(this.stationInfo
.templateHash
);
767 const measureId
= `${FileType.ChargingStationTemplate} read`;
768 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
769 template
= JSON
.parse(
770 fs
.readFileSync(this.templateFile
, 'utf8')
771 ) as ChargingStationTemplate
;
772 PerformanceStatistics
.endMeasure(measureId
, beginId
);
773 template
.templateHash
= crypto
774 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
775 .update(JSON
.stringify(template
))
777 this.sharedLRUCache
.setChargingStationTemplate(template
);
780 FileUtils
.handleFileException(
782 FileType
.ChargingStationTemplate
,
784 error
as NodeJS
.ErrnoException
790 private getStationInfoFromTemplate(): ChargingStationInfo
{
791 const stationTemplate
: ChargingStationTemplate
= this.getTemplateFromFile();
792 if (Utils
.isNullOrUndefined(stationTemplate
)) {
793 const errorMsg
= 'Failed to read charging station template file';
794 logger
.error(`${this.logPrefix()} ${errorMsg}`);
795 throw new BaseError(errorMsg
);
797 if (Utils
.isEmptyObject(stationTemplate
)) {
798 const errorMsg
= `Empty charging station information from template file ${this.templateFile}`;
799 logger
.error(`${this.logPrefix()} ${errorMsg}`);
800 throw new BaseError(errorMsg
);
802 // Deprecation template keys section
803 ChargingStationUtils
.warnDeprecatedTemplateKey(
808 "Use 'supervisionUrls' instead"
810 ChargingStationUtils
.convertDeprecatedTemplateKey(
815 const stationInfo
: ChargingStationInfo
=
816 ChargingStationUtils
.stationTemplateToStationInfo(stationTemplate
);
817 stationInfo
.hashId
= ChargingStationUtils
.getHashId(this.index
, stationTemplate
);
818 stationInfo
.chargingStationId
= ChargingStationUtils
.getChargingStationId(
822 ChargingStationUtils
.createSerialNumber(stationTemplate
, stationInfo
);
823 if (!Utils
.isEmptyArray(stationTemplate
.power
)) {
824 stationTemplate
.power
= stationTemplate
.power
as number[];
825 const powerArrayRandomIndex
= Math.floor(Utils
.secureRandom() * stationTemplate
.power
.length
);
826 stationInfo
.maximumPower
=
827 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
828 ? stationTemplate
.power
[powerArrayRandomIndex
] * 1000
829 : stationTemplate
.power
[powerArrayRandomIndex
];
831 stationTemplate
.power
= stationTemplate
.power
as number;
832 stationInfo
.maximumPower
=
833 stationTemplate
.powerUnit
=== PowerUnits
.KILO_WATT
834 ? stationTemplate
.power
* 1000
835 : stationTemplate
.power
;
837 stationInfo
.resetTime
= stationTemplate
.resetTime
838 ? stationTemplate
.resetTime
* 1000
839 : Constants
.CHARGING_STATION_DEFAULT_RESET_TIME
;
840 const configuredMaxConnectors
=
841 ChargingStationUtils
.getConfiguredNumberOfConnectors(stationTemplate
);
842 ChargingStationUtils
.checkConfiguredMaxConnectors(
843 configuredMaxConnectors
,
847 const templateMaxConnectors
=
848 ChargingStationUtils
.getTemplateMaxNumberOfConnectors(stationTemplate
);
849 ChargingStationUtils
.checkTemplateMaxConnectors(
850 templateMaxConnectors
,
855 configuredMaxConnectors
>
856 (stationTemplate
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) &&
857 !stationTemplate
?.randomConnectors
860 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
862 }, forcing random connector configurations affectation`
864 stationInfo
.randomConnectors
= true;
866 // Build connectors if needed (FIXME: should be factored out)
867 this.initializeConnectors(stationInfo
, configuredMaxConnectors
, templateMaxConnectors
);
868 stationInfo
.maximumAmperage
= this.getMaximumAmperage(stationInfo
);
869 ChargingStationUtils
.createStationInfoHash(stationInfo
);
873 private getStationInfoFromFile(): ChargingStationInfo
| null {
874 let stationInfo
: ChargingStationInfo
= null;
875 this.getStationInfoPersistentConfiguration() &&
876 (stationInfo
= this.getConfigurationFromFile()?.stationInfo
?? null);
877 stationInfo
&& ChargingStationUtils
.createStationInfoHash(stationInfo
);
881 private getStationInfo(): ChargingStationInfo
{
882 const stationInfoFromTemplate
: ChargingStationInfo
= this.getStationInfoFromTemplate();
883 const stationInfoFromFile
: ChargingStationInfo
= this.getStationInfoFromFile();
884 // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
885 if (stationInfoFromFile
?.templateHash
=== stationInfoFromTemplate
.templateHash
) {
886 if (this.stationInfo
?.infoHash
=== stationInfoFromFile
?.infoHash
) {
887 return this.stationInfo
;
889 return stationInfoFromFile
;
891 stationInfoFromFile
&&
892 ChargingStationUtils
.propagateSerialNumber(
893 this.getTemplateFromFile(),
895 stationInfoFromTemplate
897 return stationInfoFromTemplate
;
900 private saveStationInfo(): void {
901 if (this.getStationInfoPersistentConfiguration()) {
902 this.saveConfiguration();
906 private getOcppVersion(): OCPPVersion
{
907 return this.stationInfo
.ocppVersion
?? OCPPVersion
.VERSION_16
;
910 private getOcppPersistentConfiguration(): boolean {
911 return this.stationInfo
?.ocppPersistentConfiguration
?? true;
914 private getStationInfoPersistentConfiguration(): boolean {
915 return this.stationInfo
?.stationInfoPersistentConfiguration
?? true;
918 private handleUnsupportedVersion(version
: OCPPVersion
) {
919 const errMsg
= `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
920 logger
.error(`${this.logPrefix()} ${errMsg}`);
921 throw new BaseError(errMsg
);
924 private initialize(): void {
925 this.configurationFile
= path
.join(
926 path
.dirname(this.templateFile
.replace('station-templates', 'configurations')),
927 ChargingStationUtils
.getHashId(this.index
, this.getTemplateFromFile()) + '.json'
929 this.stationInfo
= this.getStationInfo();
930 this.saveStationInfo();
931 // Avoid duplication of connectors related information in RAM
932 this.stationInfo
?.Connectors
&& delete this.stationInfo
.Connectors
;
933 this.configuredSupervisionUrl
= this.getConfiguredSupervisionUrl();
934 if (this.getEnableStatistics()) {
935 this.performanceStatistics
= PerformanceStatistics
.getInstance(
936 this.stationInfo
.hashId
,
937 this.stationInfo
.chargingStationId
,
938 this.configuredSupervisionUrl
941 this.bootNotificationRequest
= ChargingStationUtils
.createBootNotificationRequest(
944 this.powerDivider
= this.getPowerDivider();
945 // OCPP configuration
946 this.ocppConfiguration
= this.getOcppConfiguration();
947 this.initializeOcppConfiguration();
948 switch (this.getOcppVersion()) {
949 case OCPPVersion
.VERSION_16
:
950 this.ocppIncomingRequestService
=
951 OCPP16IncomingRequestService
.getInstance
<OCPP16IncomingRequestService
>();
952 this.ocppRequestService
= OCPP16RequestService
.getInstance
<OCPP16RequestService
>(
953 OCPP16ResponseService
.getInstance
<OCPP16ResponseService
>()
957 this.handleUnsupportedVersion(this.getOcppVersion());
960 if (this.stationInfo
?.autoRegister
=== true) {
961 this.bootNotificationResponse
= {
962 currentTime
: new Date().toISOString(),
963 interval
: this.getHeartbeatInterval() / 1000,
964 status: RegistrationStatus
.ACCEPTED
,
969 private initializeOcppConfiguration(): void {
971 !ChargingStationConfigurationUtils
.getConfigurationKey(
973 StandardParametersKey
.HeartbeatInterval
976 ChargingStationConfigurationUtils
.addConfigurationKey(
978 StandardParametersKey
.HeartbeatInterval
,
983 !ChargingStationConfigurationUtils
.getConfigurationKey(
985 StandardParametersKey
.HeartBeatInterval
988 ChargingStationConfigurationUtils
.addConfigurationKey(
990 StandardParametersKey
.HeartBeatInterval
,
996 this.getSupervisionUrlOcppConfiguration() &&
997 !ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
999 ChargingStationConfigurationUtils
.addConfigurationKey(
1001 this.getSupervisionUrlOcppKey(),
1002 this.configuredSupervisionUrl
.href
,
1006 !this.getSupervisionUrlOcppConfiguration() &&
1007 ChargingStationConfigurationUtils
.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1009 ChargingStationConfigurationUtils
.deleteConfigurationKey(
1011 this.getSupervisionUrlOcppKey(),
1016 this.stationInfo
.amperageLimitationOcppKey
&&
1017 !ChargingStationConfigurationUtils
.getConfigurationKey(
1019 this.stationInfo
.amperageLimitationOcppKey
1022 ChargingStationConfigurationUtils
.addConfigurationKey(
1024 this.stationInfo
.amperageLimitationOcppKey
,
1026 this.stationInfo
.maximumAmperage
*
1027 ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1032 !ChargingStationConfigurationUtils
.getConfigurationKey(
1034 StandardParametersKey
.SupportedFeatureProfiles
1037 ChargingStationConfigurationUtils
.addConfigurationKey(
1039 StandardParametersKey
.SupportedFeatureProfiles
,
1040 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1043 ChargingStationConfigurationUtils
.addConfigurationKey(
1045 StandardParametersKey
.NumberOfConnectors
,
1046 this.getNumberOfConnectors().toString(),
1051 !ChargingStationConfigurationUtils
.getConfigurationKey(
1053 StandardParametersKey
.MeterValuesSampledData
1056 ChargingStationConfigurationUtils
.addConfigurationKey(
1058 StandardParametersKey
.MeterValuesSampledData
,
1059 MeterValueMeasurand
.ENERGY_ACTIVE_IMPORT_REGISTER
1063 !ChargingStationConfigurationUtils
.getConfigurationKey(
1065 StandardParametersKey
.ConnectorPhaseRotation
1068 const connectorPhaseRotation
= [];
1069 for (const connectorId
of this.connectors
.keys()) {
1071 if (connectorId
=== 0 && this.getNumberOfPhases() === 0) {
1072 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1073 } else if (connectorId
> 0 && this.getNumberOfPhases() === 0) {
1074 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1076 } else if (connectorId
> 0 && this.getNumberOfPhases() === 1) {
1077 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1078 } else if (connectorId
> 0 && this.getNumberOfPhases() === 3) {
1079 connectorPhaseRotation
.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1082 ChargingStationConfigurationUtils
.addConfigurationKey(
1084 StandardParametersKey
.ConnectorPhaseRotation
,
1085 connectorPhaseRotation
.toString()
1089 !ChargingStationConfigurationUtils
.getConfigurationKey(
1091 StandardParametersKey
.AuthorizeRemoteTxRequests
1094 ChargingStationConfigurationUtils
.addConfigurationKey(
1096 StandardParametersKey
.AuthorizeRemoteTxRequests
,
1101 !ChargingStationConfigurationUtils
.getConfigurationKey(
1103 StandardParametersKey
.LocalAuthListEnabled
1105 ChargingStationConfigurationUtils
.getConfigurationKey(
1107 StandardParametersKey
.SupportedFeatureProfiles
1108 )?.value
.includes(SupportedFeatureProfiles
.LocalAuthListManagement
)
1110 ChargingStationConfigurationUtils
.addConfigurationKey(
1112 StandardParametersKey
.LocalAuthListEnabled
,
1117 !ChargingStationConfigurationUtils
.getConfigurationKey(
1119 StandardParametersKey
.ConnectionTimeOut
1122 ChargingStationConfigurationUtils
.addConfigurationKey(
1124 StandardParametersKey
.ConnectionTimeOut
,
1125 Constants
.DEFAULT_CONNECTION_TIMEOUT
.toString()
1128 this.saveOcppConfiguration();
1131 private initializeConnectors(
1132 stationInfo
: ChargingStationInfo
,
1133 configuredMaxConnectors
: number,
1134 templateMaxConnectors
: number
1136 if (!stationInfo
?.Connectors
&& this.connectors
.size
=== 0) {
1137 const logMsg
= `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1138 logger
.error(`${this.logPrefix()} ${logMsg}`);
1139 throw new BaseError(logMsg
);
1141 if (!stationInfo
?.Connectors
[0]) {
1143 `${this.logPrefix()} Charging station information from template ${
1145 } with no connector Id 0 configuration`
1148 if (stationInfo
?.Connectors
) {
1149 const connectorsConfigHash
= crypto
1150 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1151 .update(JSON
.stringify(stationInfo
?.Connectors
) + configuredMaxConnectors
.toString())
1153 const connectorsConfigChanged
=
1154 this.connectors
?.size
!== 0 && this.connectorsConfigurationHash
!== connectorsConfigHash
;
1155 if (this.connectors
?.size
=== 0 || connectorsConfigChanged
) {
1156 connectorsConfigChanged
&& this.connectors
.clear();
1157 this.connectorsConfigurationHash
= connectorsConfigHash
;
1158 // Add connector Id 0
1159 let lastConnector
= '0';
1160 for (lastConnector
in stationInfo
?.Connectors
) {
1161 const connectorStatus
= stationInfo
?.Connectors
[lastConnector
];
1162 const lastConnectorId
= Utils
.convertToInt(lastConnector
);
1164 lastConnectorId
=== 0 &&
1165 this.getUseConnectorId0(stationInfo
) === true &&
1168 this.checkStationInfoConnectorStatus(lastConnectorId
, connectorStatus
);
1169 this.connectors
.set(
1171 Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
)
1173 this.getConnectorStatus(lastConnectorId
).availability
= AvailabilityType
.OPERATIVE
;
1174 if (Utils
.isUndefined(this.getConnectorStatus(lastConnectorId
)?.chargingProfiles
)) {
1175 this.getConnectorStatus(lastConnectorId
).chargingProfiles
= [];
1179 // Generate all connectors
1180 if ((stationInfo
?.Connectors
[0] ? templateMaxConnectors
- 1 : templateMaxConnectors
) > 0) {
1181 for (let index
= 1; index
<= configuredMaxConnectors
; index
++) {
1182 const randConnectorId
= stationInfo
?.randomConnectors
1183 ? Utils
.getRandomInteger(Utils
.convertToInt(lastConnector
), 1)
1185 const connectorStatus
= stationInfo
?.Connectors
[randConnectorId
.toString()];
1186 this.checkStationInfoConnectorStatus(randConnectorId
, connectorStatus
);
1187 this.connectors
.set(index
, Utils
.cloneObject
<ConnectorStatus
>(connectorStatus
));
1188 this.getConnectorStatus(index
).availability
= AvailabilityType
.OPERATIVE
;
1189 if (Utils
.isUndefined(this.getConnectorStatus(index
)?.chargingProfiles
)) {
1190 this.getConnectorStatus(index
).chargingProfiles
= [];
1197 `${this.logPrefix()} Charging station information from template ${
1199 } with no connectors configuration defined, using already defined connectors`
1202 // Initialize transaction attributes on connectors
1203 for (const connectorId
of this.connectors
.keys()) {
1204 if (connectorId
> 0 && this.getConnectorStatus(connectorId
).transactionStarted
=== true) {
1206 `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
1207 this.getConnectorStatus(connectorId).transactionId
1213 (this.getConnectorStatus(connectorId
).transactionStarted
=== undefined ||
1214 this.getConnectorStatus(connectorId
).transactionStarted
=== null)
1216 this.initializeConnectorStatus(connectorId
);
1221 private checkStationInfoConnectorStatus(
1222 connectorId
: number,
1223 connectorStatus
: ConnectorStatus
1225 if (!Utils
.isNullOrUndefined(connectorStatus
?.status)) {
1227 `${this.logPrefix()} Charging station information from template ${
1229 } with connector ${connectorId} status configuration defined, undefine it`
1231 connectorStatus
.status = undefined;
1235 private getConfigurationFromFile(): ChargingStationConfiguration
| null {
1236 let configuration
: ChargingStationConfiguration
= null;
1237 if (this.configurationFile
&& fs
.existsSync(this.configurationFile
)) {
1239 if (this.sharedLRUCache
.hasChargingStationConfiguration(this.configurationFileHash
)) {
1240 configuration
= this.sharedLRUCache
.getChargingStationConfiguration(
1241 this.configurationFileHash
1244 const measureId
= `${FileType.ChargingStationConfiguration} read`;
1245 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1246 configuration
= JSON
.parse(
1247 fs
.readFileSync(this.configurationFile
, 'utf8')
1248 ) as ChargingStationConfiguration
;
1249 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1250 this.configurationFileHash
= configuration
.configurationHash
;
1251 this.sharedLRUCache
.setChargingStationConfiguration(configuration
);
1254 FileUtils
.handleFileException(
1256 FileType
.ChargingStationConfiguration
,
1257 this.configurationFile
,
1258 error
as NodeJS
.ErrnoException
1262 return configuration
;
1265 private saveConfiguration(): void {
1266 if (this.configurationFile
) {
1268 if (!fs
.existsSync(path
.dirname(this.configurationFile
))) {
1269 fs
.mkdirSync(path
.dirname(this.configurationFile
), { recursive
: true });
1271 const configurationData
: ChargingStationConfiguration
=
1272 this.getConfigurationFromFile() ?? {};
1273 this.ocppConfiguration
?.configurationKey
&&
1274 (configurationData
.configurationKey
= this.ocppConfiguration
.configurationKey
);
1275 this.stationInfo
&& (configurationData
.stationInfo
= this.stationInfo
);
1276 delete configurationData
.configurationHash
;
1277 const configurationHash
= crypto
1278 .createHash(Constants
.DEFAULT_HASH_ALGORITHM
)
1279 .update(JSON
.stringify(configurationData
))
1281 if (this.configurationFileHash
!== configurationHash
) {
1282 configurationData
.configurationHash
= configurationHash
;
1283 const measureId
= `${FileType.ChargingStationConfiguration} write`;
1284 const beginId
= PerformanceStatistics
.beginMeasure(measureId
);
1285 const fileDescriptor
= fs
.openSync(this.configurationFile
, 'w');
1286 fs
.writeFileSync(fileDescriptor
, JSON
.stringify(configurationData
, null, 2), 'utf8');
1287 fs
.closeSync(fileDescriptor
);
1288 PerformanceStatistics
.endMeasure(measureId
, beginId
);
1289 this.sharedLRUCache
.deleteChargingStationConfiguration(this.configurationFileHash
);
1290 this.configurationFileHash
= configurationHash
;
1291 this.sharedLRUCache
.setChargingStationConfiguration(configurationData
);
1294 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1295 this.configurationFile
1300 FileUtils
.handleFileException(
1302 FileType
.ChargingStationConfiguration
,
1303 this.configurationFile
,
1304 error
as NodeJS
.ErrnoException
1309 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1314 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration
| null {
1315 return this.getTemplateFromFile()?.Configuration
?? null;
1318 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration
| null {
1319 let configuration
: ChargingStationConfiguration
= null;
1320 if (this.getOcppPersistentConfiguration() === true) {
1321 const configurationFromFile
= this.getConfigurationFromFile();
1322 configuration
= configurationFromFile
?.configurationKey
&& configurationFromFile
;
1324 configuration
&& delete configuration
.stationInfo
;
1325 return configuration
;
1328 private getOcppConfiguration(): ChargingStationOcppConfiguration
| null {
1329 let ocppConfiguration
: ChargingStationOcppConfiguration
= this.getOcppConfigurationFromFile();
1330 if (!ocppConfiguration
) {
1331 ocppConfiguration
= this.getOcppConfigurationFromTemplate();
1333 return ocppConfiguration
;
1336 private async onOpen(): Promise
<void> {
1337 if (this.isWebSocketConnectionOpened() === true) {
1339 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1341 if (this.isRegistered() === false) {
1342 // Send BootNotification
1343 let registrationRetryCount
= 0;
1345 this.bootNotificationResponse
= await this.ocppRequestService
.requestHandler
<
1346 BootNotificationRequest
,
1347 BootNotificationResponse
1348 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1349 skipBufferingOnError
: true,
1351 if (this.isRegistered() === false) {
1352 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount
++;
1354 this.bootNotificationResponse
?.interval
1355 ? this.bootNotificationResponse
.interval
* 1000
1356 : Constants
.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1360 this.isRegistered() === false &&
1361 (registrationRetryCount
<= this.getRegistrationMaxRetries() ||
1362 this.getRegistrationMaxRetries() === -1)
1365 if (this.isRegistered() === true) {
1366 if (this.isInAcceptedState() === true) {
1367 await this.startMessageSequence();
1371 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1374 this.wsConnectionRestarted
= false;
1375 this.autoReconnectRetryCount
= 0;
1376 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1379 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1384 private async onClose(code
: number, reason
: string): Promise
<void> {
1387 case WebSocketCloseEventStatusCode
.CLOSE_NORMAL
:
1388 case WebSocketCloseEventStatusCode
.CLOSE_NO_STATUS
:
1390 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1392 )}' and reason '${reason}'`
1394 this.autoReconnectRetryCount
= 0;
1399 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1401 )}' and reason '${reason}'`
1403 this.started
=== true && (await this.reconnect());
1406 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1409 private async onMessage(data
: Data
): Promise
<void> {
1410 let messageType
: number;
1411 let messageId
: string;
1412 let commandName
: IncomingRequestCommand
;
1413 let commandPayload
: JsonType
;
1414 let errorType
: ErrorType
;
1415 let errorMessage
: string;
1416 let errorDetails
: JsonType
;
1417 let responseCallback
: ResponseCallback
;
1418 let errorCallback
: ErrorCallback
;
1419 let requestCommandName
: RequestCommand
| IncomingRequestCommand
;
1420 let requestPayload
: JsonType
;
1421 let cachedRequest
: CachedRequest
;
1424 const request
= JSON
.parse(data
.toString()) as IncomingRequest
| Response
| ErrorResponse
;
1425 if (Array.isArray(request
) === true) {
1426 [messageType
, messageId
] = request
;
1427 // Check the type of message
1428 switch (messageType
) {
1430 case MessageType
.CALL_MESSAGE
:
1431 [, , commandName
, commandPayload
] = request
as IncomingRequest
;
1432 if (this.getEnableStatistics() === true) {
1433 this.performanceStatistics
.addRequestStatistic(commandName
, messageType
);
1436 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1440 // Process the message
1441 await this.ocppIncomingRequestService
.incomingRequestHandler(
1449 case MessageType
.CALL_RESULT_MESSAGE
:
1450 [, , commandPayload
] = request
as Response
;
1451 if (this.requests
.has(messageId
) === false) {
1453 throw new OCPPError(
1454 ErrorType
.INTERNAL_ERROR
,
1455 `Response for unknown message id ${messageId}`,
1461 cachedRequest
= this.requests
.get(messageId
);
1462 if (Array.isArray(cachedRequest
) === true) {
1463 [responseCallback
, errorCallback
, requestCommandName
, requestPayload
] = cachedRequest
;
1465 throw new OCPPError(
1466 ErrorType
.PROTOCOL_ERROR
,
1467 `Cached request for message id ${messageId} response is not an array`,
1469 cachedRequest
as unknown
as JsonType
1473 `${this.logPrefix()} << Command '${
1474 requestCommandName ?? Constants.UNKNOWN_COMMAND
1475 }' received response payload: ${JSON.stringify(request)}`
1477 responseCallback(commandPayload
, requestPayload
);
1480 case MessageType
.CALL_ERROR_MESSAGE
:
1481 [, , errorType
, errorMessage
, errorDetails
] = request
as ErrorResponse
;
1482 if (this.requests
.has(messageId
) === false) {
1484 throw new OCPPError(
1485 ErrorType
.INTERNAL_ERROR
,
1486 `Error response for unknown message id ${messageId}`,
1488 { errorType
, errorMessage
, errorDetails
}
1491 cachedRequest
= this.requests
.get(messageId
);
1492 if (Array.isArray(cachedRequest
) === true) {
1493 [, errorCallback
, requestCommandName
] = cachedRequest
;
1495 throw new OCPPError(
1496 ErrorType
.PROTOCOL_ERROR
,
1497 `Cached request for message id ${messageId} error response is not an array`,
1499 cachedRequest
as unknown
as JsonType
1503 `${this.logPrefix()} << Command '${
1504 requestCommandName ?? Constants.UNKNOWN_COMMAND
1505 }' received error payload: ${JSON.stringify(request)}`
1507 errorCallback(new OCPPError(errorType
, errorMessage
, requestCommandName
, errorDetails
));
1511 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1512 errMsg
= `Wrong message type ${messageType}`;
1513 logger
.error(`${this.logPrefix()} ${errMsg}`);
1514 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, errMsg
);
1516 parentPort
.postMessage(MessageChannelUtils
.buildUpdatedMessage(this));
1518 throw new OCPPError(ErrorType
.PROTOCOL_ERROR
, 'Incoming message is not an array', null, {
1525 `${this.logPrefix()} Incoming OCPP command '${
1526 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1527 }' message '${data.toString()}'${
1528 messageType !== MessageType.CALL_MESSAGE
1529 ? ` matching cached request
'${JSON.stringify(this.requests.get(messageId))}'`
1531 } processing error:`,
1534 if (error
instanceof OCPPError
=== false) {
1536 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1537 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1538 }' message '${data.toString()}' handling is not an OCPPError:`,
1542 switch (messageType
) {
1543 case MessageType
.CALL_MESSAGE
:
1545 await this.ocppRequestService
.sendError(
1549 commandName
?? requestCommandName
?? null
1552 case MessageType
.CALL_RESULT_MESSAGE
:
1553 case MessageType
.CALL_ERROR_MESSAGE
:
1554 if (errorCallback
) {
1555 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1556 errorCallback(error
as OCPPError
, false);
1558 // Remove the request from the cache in case of error at response handling
1559 this.requests
.delete(messageId
);
1566 private onPing(): void {
1567 logger
.debug(this.logPrefix() + ' Received a WS ping (rfc6455) from the server');
1570 private onPong(): void {
1571 logger
.debug(this.logPrefix() + ' Received a WS pong (rfc6455) from the server');
1574 private onError(error
: WSError
): void {
1575 this.closeWSConnection();
1576 logger
.error(this.logPrefix() + ' WebSocket error:', error
);
1579 private getEnergyActiveImportRegister(
1580 connectorStatus
: ConnectorStatus
,
1583 if (this.getMeteringPerTransaction() === true) {
1586 ? Math.round(connectorStatus
?.transactionEnergyActiveImportRegisterValue
)
1587 : connectorStatus
?.transactionEnergyActiveImportRegisterValue
) ?? 0
1592 ? Math.round(connectorStatus
?.energyActiveImportRegisterValue
)
1593 : connectorStatus
?.energyActiveImportRegisterValue
) ?? 0
1597 private getUseConnectorId0(stationInfo
?: ChargingStationInfo
): boolean {
1598 const localStationInfo
= stationInfo
?? this.stationInfo
;
1599 return !Utils
.isUndefined(localStationInfo
.useConnectorId0
)
1600 ? localStationInfo
.useConnectorId0
1604 private getNumberOfRunningTransactions(): number {
1606 for (const connectorId
of this.connectors
.keys()) {
1607 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1614 private async stopRunningTransactions(reason
= StopTransactionReason
.NONE
): Promise
<void> {
1615 for (const connectorId
of this.connectors
.keys()) {
1616 if (connectorId
> 0 && this.getConnectorStatus(connectorId
)?.transactionStarted
=== true) {
1617 await this.stopTransactionOnConnector(connectorId
, reason
);
1623 private getConnectionTimeout(): number {
1625 ChargingStationConfigurationUtils
.getConfigurationKey(
1627 StandardParametersKey
.ConnectionTimeOut
1632 ChargingStationConfigurationUtils
.getConfigurationKey(
1634 StandardParametersKey
.ConnectionTimeOut
1636 ) ?? Constants
.DEFAULT_CONNECTION_TIMEOUT
1639 return Constants
.DEFAULT_CONNECTION_TIMEOUT
;
1642 // -1 for unlimited, 0 for disabling
1643 private getAutoReconnectMaxRetries(): number {
1644 if (!Utils
.isUndefined(this.stationInfo
.autoReconnectMaxRetries
)) {
1645 return this.stationInfo
.autoReconnectMaxRetries
;
1647 if (!Utils
.isUndefined(Configuration
.getAutoReconnectMaxRetries())) {
1648 return Configuration
.getAutoReconnectMaxRetries();
1654 private getRegistrationMaxRetries(): number {
1655 if (!Utils
.isUndefined(this.stationInfo
.registrationMaxRetries
)) {
1656 return this.stationInfo
.registrationMaxRetries
;
1661 private getPowerDivider(): number {
1662 let powerDivider
= this.getNumberOfConnectors();
1663 if (this.stationInfo
?.powerSharedByConnectors
) {
1664 powerDivider
= this.getNumberOfRunningTransactions();
1666 return powerDivider
;
1669 private getMaximumPower(stationInfo
?: ChargingStationInfo
): number {
1670 const localStationInfo
= stationInfo
?? this.stationInfo
;
1671 return (localStationInfo
['maxPower'] as number) ?? localStationInfo
.maximumPower
;
1674 private getMaximumAmperage(stationInfo
: ChargingStationInfo
): number | undefined {
1675 const maximumPower
= this.getMaximumPower(stationInfo
);
1676 switch (this.getCurrentOutType(stationInfo
)) {
1677 case CurrentType
.AC
:
1678 return ACElectricUtils
.amperagePerPhaseFromPower(
1679 this.getNumberOfPhases(stationInfo
),
1680 maximumPower
/ this.getNumberOfConnectors(),
1681 this.getVoltageOut(stationInfo
)
1683 case CurrentType
.DC
:
1684 return DCElectricUtils
.amperage(maximumPower
, this.getVoltageOut(stationInfo
));
1688 private getAmperageLimitation(): number | undefined {
1690 this.stationInfo
.amperageLimitationOcppKey
&&
1691 ChargingStationConfigurationUtils
.getConfigurationKey(
1693 this.stationInfo
.amperageLimitationOcppKey
1698 ChargingStationConfigurationUtils
.getConfigurationKey(
1700 this.stationInfo
.amperageLimitationOcppKey
1702 ) / ChargingStationUtils
.getAmperageLimitationUnitDivider(this.stationInfo
)
1707 private getChargingProfilePowerLimit(connectorId
: number): number | undefined {
1708 let limit
: number, matchingChargingProfile
: ChargingProfile
;
1709 let chargingProfiles
: ChargingProfile
[] = [];
1710 // Get charging profiles for connector and sort by stack level
1711 chargingProfiles
= this.getConnectorStatus(connectorId
).chargingProfiles
.sort(
1712 (a
, b
) => b
.stackLevel
- a
.stackLevel
1714 // Get profiles on connector 0
1715 if (this.getConnectorStatus(0).chargingProfiles
) {
1716 chargingProfiles
.push(
1717 ...this.getConnectorStatus(0).chargingProfiles
.sort((a
, b
) => b
.stackLevel
- a
.stackLevel
)
1720 if (!Utils
.isEmptyArray(chargingProfiles
)) {
1721 const result
= ChargingStationUtils
.getLimitFromChargingProfiles(
1725 if (!Utils
.isNullOrUndefined(result
)) {
1726 limit
= result
.limit
;
1727 matchingChargingProfile
= result
.matchingChargingProfile
;
1728 switch (this.getCurrentOutType()) {
1729 case CurrentType
.AC
:
1731 matchingChargingProfile
.chargingSchedule
.chargingRateUnit
===
1732 ChargingRateUnitType
.WATT
1734 : ACElectricUtils
.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit
);
1736 case CurrentType
.DC
:
1738 matchingChargingProfile
.chargingSchedule
.chargingRateUnit
===
1739 ChargingRateUnitType
.WATT
1741 : DCElectricUtils
.power(this.getVoltageOut(), limit
);
1743 const connectorMaximumPower
= this.getMaximumPower() / this.powerDivider
;
1744 if (limit
> connectorMaximumPower
) {
1746 `${this.logPrefix()} Charging profile id ${
1747 matchingChargingProfile.chargingProfileId
1748 } limit is greater than connector id ${connectorId} maximum, dump charging profiles' stack: %j`,
1749 this.getConnectorStatus(connectorId
).chargingProfiles
1751 limit
= connectorMaximumPower
;
1758 private async startMessageSequence(): Promise
<void> {
1759 if (this.stationInfo
?.autoRegister
=== true) {
1760 await this.ocppRequestService
.requestHandler
<
1761 BootNotificationRequest
,
1762 BootNotificationResponse
1763 >(this, RequestCommand
.BOOT_NOTIFICATION
, this.bootNotificationRequest
, {
1764 skipBufferingOnError
: true,
1767 // Start WebSocket ping
1768 this.startWebSocketPing();
1770 this.startHeartbeat();
1771 // Initialize connectors status
1772 for (const connectorId
of this.connectors
.keys()) {
1773 let chargePointStatus
: ChargePointStatus
;
1774 if (connectorId
=== 0) {
1777 !this.getConnectorStatus(connectorId
)?.status &&
1778 (this.isChargingStationAvailable() === false ||
1779 this.isConnectorAvailable(connectorId
) === false)
1781 chargePointStatus
= ChargePointStatus
.UNAVAILABLE
;
1783 !this.getConnectorStatus(connectorId
)?.status &&
1784 this.getConnectorStatus(connectorId
)?.bootStatus
1786 // Set boot status in template at startup
1787 chargePointStatus
= this.getConnectorStatus(connectorId
).bootStatus
;
1788 } else if (this.getConnectorStatus(connectorId
)?.status) {
1789 // Set previous status at startup
1790 chargePointStatus
= this.getConnectorStatus(connectorId
).status;
1792 // Set default status
1793 chargePointStatus
= ChargePointStatus
.AVAILABLE
;
1795 await this.ocppRequestService
.requestHandler
<
1796 StatusNotificationRequest
,
1797 StatusNotificationResponse
1798 >(this, RequestCommand
.STATUS_NOTIFICATION
, {
1800 status: chargePointStatus
,
1801 errorCode
: ChargePointErrorCode
.NO_ERROR
,
1803 this.getConnectorStatus(connectorId
).status = chargePointStatus
;
1806 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable
=== true) {
1807 this.startAutomaticTransactionGenerator();
1809 this.wsConnectionRestarted
=== true && this.flushMessageBuffer();
1812 private async stopMessageSequence(
1813 reason
: StopTransactionReason
= StopTransactionReason
.NONE
1815 // Stop WebSocket ping
1816 this.stopWebSocketPing();
1818 this.stopHeartbeat();
1819 // Stop ongoing transactions
1820 if (this.automaticTransactionGenerator
?.started
=== true) {
1821 this.stopAutomaticTransactionGenerator();
1823 await this.stopRunningTransactions(reason
);
1825 for (const connectorId
of this.connectors
.keys()) {
1826 if (connectorId
> 0) {
1827 await this.ocppRequestService
.requestHandler
<
1828 StatusNotificationRequest
,
1829 StatusNotificationResponse
1830 >(this, RequestCommand
.STATUS_NOTIFICATION
, {
1832 status: ChargePointStatus
.UNAVAILABLE
,
1833 errorCode
: ChargePointErrorCode
.NO_ERROR
,
1835 this.getConnectorStatus(connectorId
).status = null;
1840 private startWebSocketPing(): void {
1841 const webSocketPingInterval
: number = ChargingStationConfigurationUtils
.getConfigurationKey(
1843 StandardParametersKey
.WebSocketPingInterval
1845 ? Utils
.convertToInt(
1846 ChargingStationConfigurationUtils
.getConfigurationKey(
1848 StandardParametersKey
.WebSocketPingInterval
1852 if (webSocketPingInterval
> 0 && !this.webSocketPingSetInterval
) {
1853 this.webSocketPingSetInterval
= setInterval(() => {
1854 if (this.isWebSocketConnectionOpened() === true) {
1855 this.wsConnection
.ping();
1857 }, webSocketPingInterval
* 1000);
1860 ' WebSocket ping started every ' +
1861 Utils
.formatDurationSeconds(webSocketPingInterval
)
1863 } else if (this.webSocketPingSetInterval
) {
1866 ' WebSocket ping already started every ' +
1867 Utils
.formatDurationSeconds(webSocketPingInterval
)
1871 `${this.logPrefix()} WebSocket ping interval set to ${
1872 webSocketPingInterval
1873 ? Utils.formatDurationSeconds(webSocketPingInterval)
1874 : webSocketPingInterval
1875 }, not starting the WebSocket ping`
1880 private stopWebSocketPing(): void {
1881 if (this.webSocketPingSetInterval
) {
1882 clearInterval(this.webSocketPingSetInterval
);
1886 private getConfiguredSupervisionUrl(): URL
{
1887 const supervisionUrls
= Utils
.cloneObject
<string | string[]>(
1888 this.stationInfo
.supervisionUrls
?? Configuration
.getSupervisionUrls()
1890 if (!Utils
.isEmptyArray(supervisionUrls
)) {
1891 switch (Configuration
.getSupervisionUrlDistribution()) {
1892 case SupervisionUrlDistribution
.ROUND_ROBIN
:
1894 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1896 case SupervisionUrlDistribution
.RANDOM
:
1897 this.configuredSupervisionUrlIndex
= Math.floor(
1898 Utils
.secureRandom() * supervisionUrls
.length
1901 case SupervisionUrlDistribution
.CHARGING_STATION_AFFINITY
:
1902 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1906 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
1907 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
1910 this.configuredSupervisionUrlIndex
= (this.index
- 1) % supervisionUrls
.length
;
1913 return new URL(supervisionUrls
[this.configuredSupervisionUrlIndex
]);
1915 return new URL(supervisionUrls
as string);
1918 private getHeartbeatInterval(): number {
1919 const HeartbeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1921 StandardParametersKey
.HeartbeatInterval
1923 if (HeartbeatInterval
) {
1924 return Utils
.convertToInt(HeartbeatInterval
.value
) * 1000;
1926 const HeartBeatInterval
= ChargingStationConfigurationUtils
.getConfigurationKey(
1928 StandardParametersKey
.HeartBeatInterval
1930 if (HeartBeatInterval
) {
1931 return Utils
.convertToInt(HeartBeatInterval
.value
) * 1000;
1933 this.stationInfo
?.autoRegister
=== false &&
1935 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
1936 Constants.DEFAULT_HEARTBEAT_INTERVAL
1939 return Constants
.DEFAULT_HEARTBEAT_INTERVAL
;
1942 private stopHeartbeat(): void {
1943 if (this.heartbeatSetInterval
) {
1944 clearInterval(this.heartbeatSetInterval
);
1948 private terminateWSConnection(): void {
1949 if (this.isWebSocketConnectionOpened() === true) {
1950 this.wsConnection
.terminate();
1951 this.wsConnection
= null;
1955 private stopMeterValues(connectorId
: number) {
1956 if (this.getConnectorStatus(connectorId
)?.transactionSetInterval
) {
1957 clearInterval(this.getConnectorStatus(connectorId
).transactionSetInterval
);
1961 private getReconnectExponentialDelay(): boolean {
1962 return !Utils
.isUndefined(this.stationInfo
.reconnectExponentialDelay
)
1963 ? this.stationInfo
.reconnectExponentialDelay
1967 private async reconnect(): Promise
<void> {
1968 // Stop WebSocket ping
1969 this.stopWebSocketPing();
1971 this.stopHeartbeat();
1972 // Stop the ATG if needed
1973 if (this.automaticTransactionGenerator
?.configuration
?.stopOnConnectionFailure
=== true) {
1974 this.stopAutomaticTransactionGenerator();
1977 this.autoReconnectRetryCount
< this.getAutoReconnectMaxRetries() ||
1978 this.getAutoReconnectMaxRetries() === -1
1980 this.autoReconnectRetryCount
++;
1981 const reconnectDelay
= this.getReconnectExponentialDelay()
1982 ? Utils
.exponentialDelay(this.autoReconnectRetryCount
)
1983 : this.getConnectionTimeout() * 1000;
1984 const reconnectDelayWithdraw
= 1000;
1985 const reconnectTimeout
=
1986 reconnectDelay
&& reconnectDelay
- reconnectDelayWithdraw
> 0
1987 ? reconnectDelay
- reconnectDelayWithdraw
1990 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
1993 )}ms, timeout ${reconnectTimeout}ms`
1995 await Utils
.sleep(reconnectDelay
);
1997 this.logPrefix() + ' WebSocket connection retry #' + this.autoReconnectRetryCount
.toString()
1999 this.openWSConnection(
2000 { ...(this.stationInfo
?.wsOptions
?? {}), handshakeTimeout
: reconnectTimeout
},
2001 { closeOpened
: true }
2003 this.wsConnectionRestarted
= true;
2004 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2006 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2007 this.autoReconnectRetryCount
2008 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2013 private getAutomaticTransactionGeneratorConfigurationFromTemplate(): AutomaticTransactionGeneratorConfiguration
| null {
2014 return this.getTemplateFromFile()?.AutomaticTransactionGenerator
?? null;
2017 private initializeConnectorStatus(connectorId
: number): void {
2018 this.getConnectorStatus(connectorId
).idTagLocalAuthorized
= false;
2019 this.getConnectorStatus(connectorId
).idTagAuthorized
= false;
2020 this.getConnectorStatus(connectorId
).transactionRemoteStarted
= false;
2021 this.getConnectorStatus(connectorId
).transactionStarted
= false;
2022 this.getConnectorStatus(connectorId
).energyActiveImportRegisterValue
= 0;
2023 this.getConnectorStatus(connectorId
).transactionEnergyActiveImportRegisterValue
= 0;