Add BootNotification and ClearCache OCPP 2.0.1 commands support
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3 import crypto from 'crypto';
4 import fs from 'fs';
5 import path from 'path';
6 import { URL } from 'url';
7 import { parentPort } from 'worker_threads';
8
9 import WebSocket, { type RawData } from 'ws';
10
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';
18 import {
19 type ChargingStationTemplate,
20 CurrentType,
21 PowerUnits,
22 type WsOptions,
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';
31 import {
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';
41 import {
42 AvailabilityType,
43 type BootNotificationRequest,
44 type CachedRequest,
45 type ErrorCallback,
46 type HeartbeatRequest,
47 type IncomingRequest,
48 IncomingRequestCommand,
49 type MeterValuesRequest,
50 RequestCommand,
51 type ResponseCallback,
52 type StatusNotificationRequest,
53 } from '../types/ocpp/Requests';
54 import {
55 type BootNotificationResponse,
56 type ErrorResponse,
57 type HeartbeatResponse,
58 type MeterValuesResponse,
59 RegistrationStatusEnumType,
60 type Response,
61 type StatusNotificationResponse,
62 } from '../types/ocpp/Responses';
63 import {
64 StopTransactionReason,
65 type StopTransactionRequest,
66 type 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 OCPP20IncomingRequestService from './ocpp/2.0/OCPP20IncomingRequestService';
86 import OCPP20RequestService from './ocpp/2.0/OCPP20RequestService';
87 import OCPP20ResponseService from './ocpp/2.0/OCPP20ResponseService';
88 import type OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService';
89 import type OCPPRequestService from './ocpp/OCPPRequestService';
90 import SharedLRUCache from './SharedLRUCache';
91
92 export default class ChargingStation {
93 public readonly index: number;
94 public readonly templateFile: string;
95 public stationInfo!: ChargingStationInfo;
96 public started: boolean;
97 public starting: boolean;
98 public authorizedTagsCache: AuthorizedTagsCache;
99 public automaticTransactionGenerator!: AutomaticTransactionGenerator;
100 public ocppConfiguration!: ChargingStationOcppConfiguration;
101 public wsConnection!: WebSocket;
102 public readonly connectors: Map<number, ConnectorStatus>;
103 public readonly requests: Map<string, CachedRequest>;
104 public performanceStatistics!: PerformanceStatistics;
105 public heartbeatSetInterval!: NodeJS.Timeout;
106 public ocppRequestService!: OCPPRequestService;
107 public bootNotificationRequest!: BootNotificationRequest;
108 public bootNotificationResponse!: BootNotificationResponse | null;
109 public powerDivider!: number;
110 private stopping: boolean;
111 private configurationFile!: string;
112 private configurationFileHash!: string;
113 private connectorsConfigurationHash!: string;
114 private ocppIncomingRequestService!: OCPPIncomingRequestService;
115 private readonly messageBuffer: Set<string>;
116 private configuredSupervisionUrl!: URL;
117 private configuredSupervisionUrlIndex!: number;
118 private wsConnectionRestarted: boolean;
119 private autoReconnectRetryCount: number;
120 private templateFileWatcher!: fs.FSWatcher;
121 private readonly sharedLRUCache: SharedLRUCache;
122 private webSocketPingSetInterval!: NodeJS.Timeout;
123 private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel;
124
125 constructor(index: number, templateFile: string) {
126 this.started = false;
127 this.starting = false;
128 this.stopping = false;
129 this.wsConnectionRestarted = false;
130 this.autoReconnectRetryCount = 0;
131 this.index = index;
132 this.templateFile = templateFile;
133 this.connectors = new Map<number, ConnectorStatus>();
134 this.requests = new Map<string, CachedRequest>();
135 this.messageBuffer = new Set<string>();
136 this.sharedLRUCache = SharedLRUCache.getInstance();
137 this.authorizedTagsCache = AuthorizedTagsCache.getInstance();
138 this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this);
139
140 this.initialize();
141 }
142
143 private get wsConnectionUrl(): URL {
144 return new URL(
145 (this.getSupervisionUrlOcppConfiguration()
146 ? ChargingStationConfigurationUtils.getConfigurationKey(
147 this,
148 this.getSupervisionUrlOcppKey()
149 ).value
150 : this.configuredSupervisionUrl.href) +
151 '/' +
152 this.stationInfo.chargingStationId
153 );
154 }
155
156 public logPrefix(): string {
157 return Utils.logPrefix(
158 ` ${
159 this?.stationInfo?.chargingStationId ??
160 ChargingStationUtils.getChargingStationId(this.index, this.getTemplateFromFile())
161 } |`
162 );
163 }
164
165 public hasAuthorizedTags(): boolean {
166 return !Utils.isEmptyArray(
167 this.authorizedTagsCache.getAuthorizedTags(
168 ChargingStationUtils.getAuthorizationFile(this.stationInfo)
169 )
170 );
171 }
172
173 public getEnableStatistics(): boolean | undefined {
174 return !Utils.isUndefined(this.stationInfo.enableStatistics)
175 ? this.stationInfo.enableStatistics
176 : true;
177 }
178
179 public getMustAuthorizeAtRemoteStart(): boolean | undefined {
180 return this.stationInfo.mustAuthorizeAtRemoteStart ?? true;
181 }
182
183 public getPayloadSchemaValidation(): boolean | undefined {
184 return this.stationInfo.payloadSchemaValidation ?? true;
185 }
186
187 public getNumberOfPhases(stationInfo?: ChargingStationInfo): number | undefined {
188 const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo;
189 switch (this.getCurrentOutType(stationInfo)) {
190 case CurrentType.AC:
191 return !Utils.isUndefined(localStationInfo.numberOfPhases)
192 ? localStationInfo.numberOfPhases
193 : 3;
194 case CurrentType.DC:
195 return 0;
196 }
197 }
198
199 public isWebSocketConnectionOpened(): boolean {
200 return this?.wsConnection?.readyState === WebSocket.OPEN;
201 }
202
203 public getRegistrationStatus(): RegistrationStatusEnumType {
204 return this?.bootNotificationResponse?.status;
205 }
206
207 public isInUnknownState(): boolean {
208 return Utils.isNullOrUndefined(this?.bootNotificationResponse?.status);
209 }
210
211 public isInPendingState(): boolean {
212 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING;
213 }
214
215 public isInAcceptedState(): boolean {
216 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED;
217 }
218
219 public isInRejectedState(): boolean {
220 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED;
221 }
222
223 public isRegistered(): boolean {
224 return (
225 this.isInUnknownState() === false &&
226 (this.isInAcceptedState() === true || this.isInPendingState() === true)
227 );
228 }
229
230 public isChargingStationAvailable(): boolean {
231 return this.getConnectorStatus(0).availability === AvailabilityType.OPERATIVE;
232 }
233
234 public isConnectorAvailable(id: number): boolean {
235 return id > 0 && this.getConnectorStatus(id).availability === AvailabilityType.OPERATIVE;
236 }
237
238 public getNumberOfConnectors(): number {
239 return this.connectors.get(0) ? this.connectors.size - 1 : this.connectors.size;
240 }
241
242 public getConnectorStatus(id: number): ConnectorStatus | undefined {
243 return this.connectors.get(id);
244 }
245
246 public getCurrentOutType(stationInfo?: ChargingStationInfo): CurrentType {
247 return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC;
248 }
249
250 public getOcppStrictCompliance(): boolean {
251 return this.stationInfo?.ocppStrictCompliance ?? false;
252 }
253
254 public getVoltageOut(stationInfo?: ChargingStationInfo): number | undefined {
255 const defaultVoltageOut = ChargingStationUtils.getDefaultVoltageOut(
256 this.getCurrentOutType(stationInfo),
257 this.templateFile,
258 this.logPrefix()
259 );
260 const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo;
261 return !Utils.isUndefined(localStationInfo.voltageOut)
262 ? localStationInfo.voltageOut
263 : defaultVoltageOut;
264 }
265
266 public getConnectorMaximumAvailablePower(connectorId: number): number {
267 let connectorAmperageLimitationPowerLimit: number;
268 if (
269 !Utils.isNullOrUndefined(this.getAmperageLimitation()) &&
270 this.getAmperageLimitation() < this.stationInfo.maximumAmperage
271 ) {
272 connectorAmperageLimitationPowerLimit =
273 (this.getCurrentOutType() === CurrentType.AC
274 ? ACElectricUtils.powerTotal(
275 this.getNumberOfPhases(),
276 this.getVoltageOut(),
277 this.getAmperageLimitation() * this.getNumberOfConnectors()
278 )
279 : DCElectricUtils.power(this.getVoltageOut(), this.getAmperageLimitation())) /
280 this.powerDivider;
281 }
282 const connectorMaximumPower = this.getMaximumPower() / this.powerDivider;
283 const connectorChargingProfilePowerLimit = this.getChargingProfilePowerLimit(connectorId);
284 return Math.min(
285 isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower,
286 isNaN(connectorAmperageLimitationPowerLimit)
287 ? Infinity
288 : connectorAmperageLimitationPowerLimit,
289 isNaN(connectorChargingProfilePowerLimit) ? Infinity : connectorChargingProfilePowerLimit
290 );
291 }
292
293 public getTransactionIdTag(transactionId: number): string | undefined {
294 for (const connectorId of this.connectors.keys()) {
295 if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionId === transactionId) {
296 return this.getConnectorStatus(connectorId).transactionIdTag;
297 }
298 }
299 }
300
301 public getOutOfOrderEndMeterValues(): boolean {
302 return this.stationInfo?.outOfOrderEndMeterValues ?? false;
303 }
304
305 public getBeginEndMeterValues(): boolean {
306 return this.stationInfo?.beginEndMeterValues ?? false;
307 }
308
309 public getMeteringPerTransaction(): boolean {
310 return this.stationInfo?.meteringPerTransaction ?? true;
311 }
312
313 public getTransactionDataMeterValues(): boolean {
314 return this.stationInfo?.transactionDataMeterValues ?? false;
315 }
316
317 public getMainVoltageMeterValues(): boolean {
318 return this.stationInfo?.mainVoltageMeterValues ?? true;
319 }
320
321 public getPhaseLineToLineVoltageMeterValues(): boolean {
322 return this.stationInfo?.phaseLineToLineVoltageMeterValues ?? false;
323 }
324
325 public getCustomValueLimitationMeterValues(): boolean {
326 return this.stationInfo?.customValueLimitationMeterValues ?? true;
327 }
328
329 public getConnectorIdByTransactionId(transactionId: number): number | undefined {
330 for (const connectorId of this.connectors.keys()) {
331 if (
332 connectorId > 0 &&
333 this.getConnectorStatus(connectorId)?.transactionId === transactionId
334 ) {
335 return connectorId;
336 }
337 }
338 }
339
340 public getEnergyActiveImportRegisterByTransactionId(
341 transactionId: number,
342 meterStop = false
343 ): number {
344 return this.getEnergyActiveImportRegister(
345 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)),
346 meterStop
347 );
348 }
349
350 public getEnergyActiveImportRegisterByConnectorId(connectorId: number): number {
351 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId));
352 }
353
354 public getAuthorizeRemoteTxRequests(): boolean {
355 const authorizeRemoteTxRequests = ChargingStationConfigurationUtils.getConfigurationKey(
356 this,
357 StandardParametersKey.AuthorizeRemoteTxRequests
358 );
359 return authorizeRemoteTxRequests
360 ? Utils.convertToBoolean(authorizeRemoteTxRequests.value)
361 : false;
362 }
363
364 public getLocalAuthListEnabled(): boolean {
365 const localAuthListEnabled = ChargingStationConfigurationUtils.getConfigurationKey(
366 this,
367 StandardParametersKey.LocalAuthListEnabled
368 );
369 return localAuthListEnabled ? Utils.convertToBoolean(localAuthListEnabled.value) : false;
370 }
371
372 public startHeartbeat(): void {
373 if (
374 this.getHeartbeatInterval() &&
375 this.getHeartbeatInterval() > 0 &&
376 !this.heartbeatSetInterval
377 ) {
378 // eslint-disable-next-line @typescript-eslint/no-misused-promises
379 this.heartbeatSetInterval = setInterval(async (): Promise<void> => {
380 await this.ocppRequestService.requestHandler<HeartbeatRequest, HeartbeatResponse>(
381 this,
382 RequestCommand.HEARTBEAT
383 );
384 }, this.getHeartbeatInterval());
385 logger.info(
386 this.logPrefix() +
387 ' Heartbeat started every ' +
388 Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
389 );
390 } else if (this.heartbeatSetInterval) {
391 logger.info(
392 this.logPrefix() +
393 ' Heartbeat already started every ' +
394 Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
395 );
396 } else {
397 logger.error(
398 `${this.logPrefix()} Heartbeat interval set to ${
399 this.getHeartbeatInterval()
400 ? Utils.formatDurationMilliSeconds(this.getHeartbeatInterval())
401 : this.getHeartbeatInterval()
402 }, not starting the heartbeat`
403 );
404 }
405 }
406
407 public restartHeartbeat(): void {
408 // Stop heartbeat
409 this.stopHeartbeat();
410 // Start heartbeat
411 this.startHeartbeat();
412 }
413
414 public restartWebSocketPing(): void {
415 // Stop WebSocket ping
416 this.stopWebSocketPing();
417 // Start WebSocket ping
418 this.startWebSocketPing();
419 }
420
421 public startMeterValues(connectorId: number, interval: number): void {
422 if (connectorId === 0) {
423 logger.error(
424 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId.toString()}`
425 );
426 return;
427 }
428 if (!this.getConnectorStatus(connectorId)) {
429 logger.error(
430 `${this.logPrefix()} Trying to start MeterValues on non existing connector Id ${connectorId.toString()}`
431 );
432 return;
433 }
434 if (this.getConnectorStatus(connectorId)?.transactionStarted === false) {
435 logger.error(
436 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction started`
437 );
438 return;
439 } else if (
440 this.getConnectorStatus(connectorId)?.transactionStarted === true &&
441 !this.getConnectorStatus(connectorId)?.transactionId
442 ) {
443 logger.error(
444 `${this.logPrefix()} Trying to start MeterValues on connector Id ${connectorId} with no transaction id`
445 );
446 return;
447 }
448 if (interval > 0) {
449 // eslint-disable-next-line @typescript-eslint/no-misused-promises
450 this.getConnectorStatus(connectorId).transactionSetInterval = setInterval(
451 // eslint-disable-next-line @typescript-eslint/no-misused-promises
452 async (): Promise<void> => {
453 // FIXME: Implement OCPP version agnostic helpers
454 const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue(
455 this,
456 connectorId,
457 this.getConnectorStatus(connectorId).transactionId,
458 interval
459 );
460 await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
461 this,
462 RequestCommand.METER_VALUES,
463 {
464 connectorId,
465 transactionId: this.getConnectorStatus(connectorId).transactionId,
466 meterValue: [meterValue],
467 }
468 );
469 },
470 interval
471 );
472 } else {
473 logger.error(
474 `${this.logPrefix()} Charging station ${
475 StandardParametersKey.MeterValueSampleInterval
476 } configuration set to ${
477 interval ? Utils.formatDurationMilliSeconds(interval) : interval
478 }, not sending MeterValues`
479 );
480 }
481 }
482
483 public start(): void {
484 if (this.started === false) {
485 if (this.starting === false) {
486 this.starting = true;
487 if (this.getEnableStatistics()) {
488 this.performanceStatistics.start();
489 }
490 this.openWSConnection();
491 // Monitor charging station template file
492 this.templateFileWatcher = FileUtils.watchJsonFile(
493 this.logPrefix(),
494 FileType.ChargingStationTemplate,
495 this.templateFile,
496 null,
497 (event, filename): void => {
498 if (filename && event === 'change') {
499 try {
500 logger.debug(
501 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
502 this.templateFile
503 } file have changed, reload`
504 );
505 this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
506 // Initialize
507 this.initialize();
508 // Restart the ATG
509 this.stopAutomaticTransactionGenerator();
510 if (
511 this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable === true
512 ) {
513 this.startAutomaticTransactionGenerator();
514 }
515 if (this.getEnableStatistics()) {
516 this.performanceStatistics.restart();
517 } else {
518 this.performanceStatistics.stop();
519 }
520 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
521 } catch (error) {
522 logger.error(
523 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
524 error
525 );
526 }
527 }
528 }
529 );
530 this.started = true;
531 parentPort.postMessage(MessageChannelUtils.buildStartedMessage(this));
532 this.starting = false;
533 } else {
534 logger.warn(`${this.logPrefix()} Charging station is already starting...`);
535 }
536 } else {
537 logger.warn(`${this.logPrefix()} Charging station is already started...`);
538 }
539 }
540
541 public async stop(reason?: StopTransactionReason): Promise<void> {
542 if (this.started === true) {
543 if (this.stopping === false) {
544 this.stopping = true;
545 await this.stopMessageSequence(reason);
546 this.closeWSConnection();
547 if (this.getEnableStatistics()) {
548 this.performanceStatistics.stop();
549 }
550 this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
551 this.templateFileWatcher.close();
552 this.sharedLRUCache.deleteChargingStationTemplate(this.stationInfo?.templateHash);
553 this.bootNotificationResponse = null;
554 this.started = false;
555 parentPort.postMessage(MessageChannelUtils.buildStoppedMessage(this));
556 this.stopping = false;
557 } else {
558 logger.warn(`${this.logPrefix()} Charging station is already stopping...`);
559 }
560 } else {
561 logger.warn(`${this.logPrefix()} Charging station is already stopped...`);
562 }
563 }
564
565 public async reset(reason?: StopTransactionReason): Promise<void> {
566 await this.stop(reason);
567 await Utils.sleep(this.stationInfo.resetTime);
568 this.initialize();
569 this.start();
570 }
571
572 public saveOcppConfiguration(): void {
573 if (this.getOcppPersistentConfiguration()) {
574 this.saveConfiguration();
575 }
576 }
577
578 public resetConnectorStatus(connectorId: number): void {
579 this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
580 this.getConnectorStatus(connectorId).idTagAuthorized = false;
581 this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
582 this.getConnectorStatus(connectorId).transactionStarted = false;
583 delete this.getConnectorStatus(connectorId).localAuthorizeIdTag;
584 delete this.getConnectorStatus(connectorId).authorizeIdTag;
585 delete this.getConnectorStatus(connectorId).transactionId;
586 delete this.getConnectorStatus(connectorId).transactionIdTag;
587 this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
588 delete this.getConnectorStatus(connectorId).transactionBeginMeterValue;
589 this.stopMeterValues(connectorId);
590 parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
591 }
592
593 public hasFeatureProfile(featureProfile: SupportedFeatureProfiles): boolean {
594 return ChargingStationConfigurationUtils.getConfigurationKey(
595 this,
596 StandardParametersKey.SupportedFeatureProfiles
597 )?.value.includes(featureProfile);
598 }
599
600 public bufferMessage(message: string): void {
601 this.messageBuffer.add(message);
602 }
603
604 public openWSConnection(
605 options: WsOptions = this.stationInfo?.wsOptions ?? {},
606 params: { closeOpened?: boolean; terminateOpened?: boolean } = {
607 closeOpened: false,
608 terminateOpened: false,
609 }
610 ): void {
611 options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000;
612 params.closeOpened = params?.closeOpened ?? false;
613 params.terminateOpened = params?.terminateOpened ?? false;
614 if (this.started === false && this.starting === false) {
615 logger.warn(
616 `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station`
617 );
618 return;
619 }
620 if (
621 !Utils.isNullOrUndefined(this.stationInfo.supervisionUser) &&
622 !Utils.isNullOrUndefined(this.stationInfo.supervisionPassword)
623 ) {
624 options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
625 }
626 if (params?.closeOpened) {
627 this.closeWSConnection();
628 }
629 if (params?.terminateOpened) {
630 this.terminateWSConnection();
631 }
632 const ocppVersion = this.getOcppVersion();
633 let protocol: string;
634 switch (ocppVersion) {
635 case OCPPVersion.VERSION_16:
636 case OCPPVersion.VERSION_20:
637 case OCPPVersion.VERSION_201:
638 protocol = 'ocpp' + ocppVersion;
639 break;
640 default:
641 this.handleUnsupportedVersion(ocppVersion);
642 break;
643 }
644
645 if (this.isWebSocketConnectionOpened() === true) {
646 logger.warn(
647 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`
648 );
649 return;
650 }
651
652 logger.info(
653 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`
654 );
655
656 this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options);
657
658 // Handle WebSocket message
659 this.wsConnection.on(
660 'message',
661 this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void
662 );
663 // Handle WebSocket error
664 this.wsConnection.on(
665 'error',
666 this.onError.bind(this) as (this: WebSocket, error: Error) => void
667 );
668 // Handle WebSocket close
669 this.wsConnection.on(
670 'close',
671 this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void
672 );
673 // Handle WebSocket open
674 this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void);
675 // Handle WebSocket ping
676 this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void);
677 // Handle WebSocket pong
678 this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void);
679 }
680
681 public closeWSConnection(): void {
682 if (this.isWebSocketConnectionOpened() === true) {
683 this.wsConnection.close();
684 this.wsConnection = null;
685 }
686 }
687
688 public startAutomaticTransactionGenerator(
689 connectorIds?: number[],
690 automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration
691 ): void {
692 this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(
693 automaticTransactionGeneratorConfiguration ??
694 this.getAutomaticTransactionGeneratorConfigurationFromTemplate(),
695 this
696 );
697 if (!Utils.isEmptyArray(connectorIds)) {
698 for (const connectorId of connectorIds) {
699 this.automaticTransactionGenerator.startConnector(connectorId);
700 }
701 } else {
702 this.automaticTransactionGenerator.start();
703 }
704 parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
705 }
706
707 public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
708 if (!Utils.isEmptyArray(connectorIds)) {
709 for (const connectorId of connectorIds) {
710 this.automaticTransactionGenerator?.stopConnector(connectorId);
711 }
712 } else {
713 this.automaticTransactionGenerator?.stop();
714 }
715 parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
716 }
717
718 public async stopTransactionOnConnector(
719 connectorId: number,
720 reason = StopTransactionReason.NONE
721 ): Promise<StopTransactionResponse> {
722 const transactionId = this.getConnectorStatus(connectorId).transactionId;
723 if (
724 this.getBeginEndMeterValues() === true &&
725 this.getOcppStrictCompliance() === true &&
726 this.getOutOfOrderEndMeterValues() === false
727 ) {
728 // FIXME: Implement OCPP version agnostic helpers
729 const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
730 this,
731 connectorId,
732 this.getEnergyActiveImportRegisterByTransactionId(transactionId)
733 );
734 await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
735 this,
736 RequestCommand.METER_VALUES,
737 {
738 connectorId,
739 transactionId,
740 meterValue: [transactionEndMeterValue],
741 }
742 );
743 }
744 return this.ocppRequestService.requestHandler<StopTransactionRequest, StopTransactionResponse>(
745 this,
746 RequestCommand.STOP_TRANSACTION,
747 {
748 transactionId,
749 meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId, true),
750 reason,
751 }
752 );
753 }
754
755 private flushMessageBuffer(): void {
756 if (this.messageBuffer.size > 0) {
757 this.messageBuffer.forEach((message) => {
758 // TODO: evaluate the need to track performance
759 this.wsConnection.send(message);
760 this.messageBuffer.delete(message);
761 });
762 }
763 }
764
765 private getSupervisionUrlOcppConfiguration(): boolean {
766 return this.stationInfo.supervisionUrlOcppConfiguration ?? false;
767 }
768
769 private getSupervisionUrlOcppKey(): string {
770 return this.stationInfo.supervisionUrlOcppKey ?? VendorDefaultParametersKey.ConnectionUrl;
771 }
772
773 private getTemplateFromFile(): ChargingStationTemplate | null {
774 let template: ChargingStationTemplate = null;
775 try {
776 if (this.sharedLRUCache.hasChargingStationTemplate(this.stationInfo?.templateHash)) {
777 template = this.sharedLRUCache.getChargingStationTemplate(this.stationInfo.templateHash);
778 } else {
779 const measureId = `${FileType.ChargingStationTemplate} read`;
780 const beginId = PerformanceStatistics.beginMeasure(measureId);
781 template = JSON.parse(
782 fs.readFileSync(this.templateFile, 'utf8')
783 ) as ChargingStationTemplate;
784 PerformanceStatistics.endMeasure(measureId, beginId);
785 template.templateHash = crypto
786 .createHash(Constants.DEFAULT_HASH_ALGORITHM)
787 .update(JSON.stringify(template))
788 .digest('hex');
789 this.sharedLRUCache.setChargingStationTemplate(template);
790 }
791 } catch (error) {
792 FileUtils.handleFileException(
793 this.logPrefix(),
794 FileType.ChargingStationTemplate,
795 this.templateFile,
796 error as NodeJS.ErrnoException
797 );
798 }
799 return template;
800 }
801
802 private getStationInfoFromTemplate(): ChargingStationInfo {
803 const stationTemplate: ChargingStationTemplate = this.getTemplateFromFile();
804 if (Utils.isNullOrUndefined(stationTemplate)) {
805 const errorMsg = `Failed to read charging station template file ${this.templateFile}`;
806 logger.error(`${this.logPrefix()} ${errorMsg}`);
807 throw new BaseError(errorMsg);
808 }
809 if (Utils.isEmptyObject(stationTemplate)) {
810 const errorMsg = `Empty charging station information from template file ${this.templateFile}`;
811 logger.error(`${this.logPrefix()} ${errorMsg}`);
812 throw new BaseError(errorMsg);
813 }
814 // Deprecation template keys section
815 ChargingStationUtils.warnDeprecatedTemplateKey(
816 stationTemplate,
817 'supervisionUrl',
818 this.templateFile,
819 this.logPrefix(),
820 "Use 'supervisionUrls' instead"
821 );
822 ChargingStationUtils.convertDeprecatedTemplateKey(
823 stationTemplate,
824 'supervisionUrl',
825 'supervisionUrls'
826 );
827 const firmwareVersionRegExp = stationTemplate.firmwareVersionPattern
828 ? new RegExp(stationTemplate.firmwareVersionPattern)
829 : Constants.SEMVER_REGEXP;
830 if (
831 stationTemplate.firmwareVersion &&
832 firmwareVersionRegExp.test(stationTemplate.firmwareVersion) === false
833 ) {
834 logger.warn(
835 `${this.logPrefix()} Firmware version '${
836 stationTemplate.firmwareVersion
837 }' in template file ${
838 this.templateFile
839 } does not match regular expression '${firmwareVersionRegExp.toString()}'`
840 );
841 }
842 const stationInfo: ChargingStationInfo =
843 ChargingStationUtils.stationTemplateToStationInfo(stationTemplate);
844 stationInfo.hashId = ChargingStationUtils.getHashId(this.index, stationTemplate);
845 stationInfo.chargingStationId = ChargingStationUtils.getChargingStationId(
846 this.index,
847 stationTemplate
848 );
849 ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo);
850 if (!Utils.isEmptyArray(stationTemplate.power)) {
851 stationTemplate.power = stationTemplate.power as number[];
852 const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplate.power.length);
853 stationInfo.maximumPower =
854 stationTemplate.powerUnit === PowerUnits.KILO_WATT
855 ? stationTemplate.power[powerArrayRandomIndex] * 1000
856 : stationTemplate.power[powerArrayRandomIndex];
857 } else {
858 stationTemplate.power = stationTemplate.power as number;
859 stationInfo.maximumPower =
860 stationTemplate.powerUnit === PowerUnits.KILO_WATT
861 ? stationTemplate.power * 1000
862 : stationTemplate.power;
863 }
864 stationInfo.resetTime = stationTemplate.resetTime
865 ? stationTemplate.resetTime * 1000
866 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
867 const configuredMaxConnectors =
868 ChargingStationUtils.getConfiguredNumberOfConnectors(stationTemplate);
869 ChargingStationUtils.checkConfiguredMaxConnectors(
870 configuredMaxConnectors,
871 this.templateFile,
872 this.logPrefix()
873 );
874 const templateMaxConnectors =
875 ChargingStationUtils.getTemplateMaxNumberOfConnectors(stationTemplate);
876 ChargingStationUtils.checkTemplateMaxConnectors(
877 templateMaxConnectors,
878 this.templateFile,
879 this.logPrefix()
880 );
881 if (
882 configuredMaxConnectors >
883 (stationTemplate?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) &&
884 !stationTemplate?.randomConnectors
885 ) {
886 logger.warn(
887 `${this.logPrefix()} Number of connectors exceeds the number of connector configurations in template ${
888 this.templateFile
889 }, forcing random connector configurations affectation`
890 );
891 stationInfo.randomConnectors = true;
892 }
893 // Build connectors if needed (FIXME: should be factored out)
894 this.initializeConnectors(stationInfo, configuredMaxConnectors, templateMaxConnectors);
895 stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
896 ChargingStationUtils.createStationInfoHash(stationInfo);
897 return stationInfo;
898 }
899
900 private getStationInfoFromFile(): ChargingStationInfo | null {
901 let stationInfo: ChargingStationInfo = null;
902 this.getStationInfoPersistentConfiguration() &&
903 (stationInfo = this.getConfigurationFromFile()?.stationInfo ?? null);
904 stationInfo && ChargingStationUtils.createStationInfoHash(stationInfo);
905 return stationInfo;
906 }
907
908 private getStationInfo(): ChargingStationInfo {
909 const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate();
910 const stationInfoFromFile: ChargingStationInfo = this.getStationInfoFromFile();
911 // Priority: charging station info from template > charging station info from configuration file > charging station info attribute
912 if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) {
913 if (this.stationInfo?.infoHash === stationInfoFromFile?.infoHash) {
914 return this.stationInfo;
915 }
916 return stationInfoFromFile;
917 }
918 stationInfoFromFile &&
919 ChargingStationUtils.propagateSerialNumber(
920 this.getTemplateFromFile(),
921 stationInfoFromFile,
922 stationInfoFromTemplate
923 );
924 return stationInfoFromTemplate;
925 }
926
927 private saveStationInfo(): void {
928 if (this.getStationInfoPersistentConfiguration()) {
929 this.saveConfiguration();
930 }
931 }
932
933 private getOcppVersion(): OCPPVersion {
934 return this.stationInfo.ocppVersion ?? OCPPVersion.VERSION_16;
935 }
936
937 private getOcppPersistentConfiguration(): boolean {
938 return this.stationInfo?.ocppPersistentConfiguration ?? true;
939 }
940
941 private getStationInfoPersistentConfiguration(): boolean {
942 return this.stationInfo?.stationInfoPersistentConfiguration ?? true;
943 }
944
945 private handleUnsupportedVersion(version: OCPPVersion) {
946 const errMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
947 logger.error(`${this.logPrefix()} ${errMsg}`);
948 throw new BaseError(errMsg);
949 }
950
951 private initialize(): void {
952 this.configurationFile = path.join(
953 path.dirname(this.templateFile.replace('station-templates', 'configurations')),
954 ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile()) + '.json'
955 );
956 this.stationInfo = this.getStationInfo();
957 this.saveStationInfo();
958 // Avoid duplication of connectors related information in RAM
959 this.stationInfo?.Connectors && delete this.stationInfo.Connectors;
960 this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl();
961 if (this.getEnableStatistics()) {
962 this.performanceStatistics = PerformanceStatistics.getInstance(
963 this.stationInfo.hashId,
964 this.stationInfo.chargingStationId,
965 this.configuredSupervisionUrl
966 );
967 }
968 this.bootNotificationRequest = ChargingStationUtils.createBootNotificationRequest(
969 this.stationInfo
970 );
971 this.powerDivider = this.getPowerDivider();
972 // OCPP configuration
973 this.ocppConfiguration = this.getOcppConfiguration();
974 this.initializeOcppConfiguration();
975 switch (this.getOcppVersion()) {
976 case OCPPVersion.VERSION_16:
977 this.ocppIncomingRequestService =
978 OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>();
979 this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(
980 OCPP16ResponseService.getInstance<OCPP16ResponseService>()
981 );
982 break;
983 case OCPPVersion.VERSION_20:
984 case OCPPVersion.VERSION_201:
985 this.ocppIncomingRequestService =
986 OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>();
987 this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>(
988 OCPP20ResponseService.getInstance<OCPP20ResponseService>()
989 );
990 break;
991 default:
992 this.handleUnsupportedVersion(this.getOcppVersion());
993 break;
994 }
995 if (this.stationInfo?.autoRegister === true) {
996 this.bootNotificationResponse = {
997 currentTime: new Date(),
998 interval: this.getHeartbeatInterval() / 1000,
999 status: RegistrationStatusEnumType.ACCEPTED,
1000 };
1001 }
1002 }
1003
1004 private initializeOcppConfiguration(): void {
1005 if (
1006 !ChargingStationConfigurationUtils.getConfigurationKey(
1007 this,
1008 StandardParametersKey.HeartbeatInterval
1009 )
1010 ) {
1011 ChargingStationConfigurationUtils.addConfigurationKey(
1012 this,
1013 StandardParametersKey.HeartbeatInterval,
1014 '0'
1015 );
1016 }
1017 if (
1018 !ChargingStationConfigurationUtils.getConfigurationKey(
1019 this,
1020 StandardParametersKey.HeartBeatInterval
1021 )
1022 ) {
1023 ChargingStationConfigurationUtils.addConfigurationKey(
1024 this,
1025 StandardParametersKey.HeartBeatInterval,
1026 '0',
1027 { visible: false }
1028 );
1029 }
1030 if (
1031 this.getSupervisionUrlOcppConfiguration() &&
1032 !ChargingStationConfigurationUtils.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1033 ) {
1034 ChargingStationConfigurationUtils.addConfigurationKey(
1035 this,
1036 this.getSupervisionUrlOcppKey(),
1037 this.configuredSupervisionUrl.href,
1038 { reboot: true }
1039 );
1040 } else if (
1041 !this.getSupervisionUrlOcppConfiguration() &&
1042 ChargingStationConfigurationUtils.getConfigurationKey(this, this.getSupervisionUrlOcppKey())
1043 ) {
1044 ChargingStationConfigurationUtils.deleteConfigurationKey(
1045 this,
1046 this.getSupervisionUrlOcppKey(),
1047 { save: false }
1048 );
1049 }
1050 if (
1051 this.stationInfo.amperageLimitationOcppKey &&
1052 !ChargingStationConfigurationUtils.getConfigurationKey(
1053 this,
1054 this.stationInfo.amperageLimitationOcppKey
1055 )
1056 ) {
1057 ChargingStationConfigurationUtils.addConfigurationKey(
1058 this,
1059 this.stationInfo.amperageLimitationOcppKey,
1060 (
1061 this.stationInfo.maximumAmperage *
1062 ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
1063 ).toString()
1064 );
1065 }
1066 if (
1067 !ChargingStationConfigurationUtils.getConfigurationKey(
1068 this,
1069 StandardParametersKey.SupportedFeatureProfiles
1070 )
1071 ) {
1072 ChargingStationConfigurationUtils.addConfigurationKey(
1073 this,
1074 StandardParametersKey.SupportedFeatureProfiles,
1075 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`
1076 );
1077 }
1078 ChargingStationConfigurationUtils.addConfigurationKey(
1079 this,
1080 StandardParametersKey.NumberOfConnectors,
1081 this.getNumberOfConnectors().toString(),
1082 { readonly: true },
1083 { overwrite: true }
1084 );
1085 if (
1086 !ChargingStationConfigurationUtils.getConfigurationKey(
1087 this,
1088 StandardParametersKey.MeterValuesSampledData
1089 )
1090 ) {
1091 ChargingStationConfigurationUtils.addConfigurationKey(
1092 this,
1093 StandardParametersKey.MeterValuesSampledData,
1094 MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER
1095 );
1096 }
1097 if (
1098 !ChargingStationConfigurationUtils.getConfigurationKey(
1099 this,
1100 StandardParametersKey.ConnectorPhaseRotation
1101 )
1102 ) {
1103 const connectorPhaseRotation = [];
1104 for (const connectorId of this.connectors.keys()) {
1105 // AC/DC
1106 if (connectorId === 0 && this.getNumberOfPhases() === 0) {
1107 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1108 } else if (connectorId > 0 && this.getNumberOfPhases() === 0) {
1109 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1110 // AC
1111 } else if (connectorId > 0 && this.getNumberOfPhases() === 1) {
1112 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.NotApplicable}`);
1113 } else if (connectorId > 0 && this.getNumberOfPhases() === 3) {
1114 connectorPhaseRotation.push(`${connectorId}.${ConnectorPhaseRotation.RST}`);
1115 }
1116 }
1117 ChargingStationConfigurationUtils.addConfigurationKey(
1118 this,
1119 StandardParametersKey.ConnectorPhaseRotation,
1120 connectorPhaseRotation.toString()
1121 );
1122 }
1123 if (
1124 !ChargingStationConfigurationUtils.getConfigurationKey(
1125 this,
1126 StandardParametersKey.AuthorizeRemoteTxRequests
1127 )
1128 ) {
1129 ChargingStationConfigurationUtils.addConfigurationKey(
1130 this,
1131 StandardParametersKey.AuthorizeRemoteTxRequests,
1132 'true'
1133 );
1134 }
1135 if (
1136 !ChargingStationConfigurationUtils.getConfigurationKey(
1137 this,
1138 StandardParametersKey.LocalAuthListEnabled
1139 ) &&
1140 ChargingStationConfigurationUtils.getConfigurationKey(
1141 this,
1142 StandardParametersKey.SupportedFeatureProfiles
1143 )?.value.includes(SupportedFeatureProfiles.LocalAuthListManagement)
1144 ) {
1145 ChargingStationConfigurationUtils.addConfigurationKey(
1146 this,
1147 StandardParametersKey.LocalAuthListEnabled,
1148 'false'
1149 );
1150 }
1151 if (
1152 !ChargingStationConfigurationUtils.getConfigurationKey(
1153 this,
1154 StandardParametersKey.ConnectionTimeOut
1155 )
1156 ) {
1157 ChargingStationConfigurationUtils.addConfigurationKey(
1158 this,
1159 StandardParametersKey.ConnectionTimeOut,
1160 Constants.DEFAULT_CONNECTION_TIMEOUT.toString()
1161 );
1162 }
1163 this.saveOcppConfiguration();
1164 }
1165
1166 private initializeConnectors(
1167 stationInfo: ChargingStationInfo,
1168 configuredMaxConnectors: number,
1169 templateMaxConnectors: number
1170 ): void {
1171 if (!stationInfo?.Connectors && this.connectors.size === 0) {
1172 const logMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1173 logger.error(`${this.logPrefix()} ${logMsg}`);
1174 throw new BaseError(logMsg);
1175 }
1176 if (!stationInfo?.Connectors[0]) {
1177 logger.warn(
1178 `${this.logPrefix()} Charging station information from template ${
1179 this.templateFile
1180 } with no connector Id 0 configuration`
1181 );
1182 }
1183 if (stationInfo?.Connectors) {
1184 const connectorsConfigHash = crypto
1185 .createHash(Constants.DEFAULT_HASH_ALGORITHM)
1186 .update(JSON.stringify(stationInfo?.Connectors) + configuredMaxConnectors.toString())
1187 .digest('hex');
1188 const connectorsConfigChanged =
1189 this.connectors?.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash;
1190 if (this.connectors?.size === 0 || connectorsConfigChanged) {
1191 connectorsConfigChanged && this.connectors.clear();
1192 this.connectorsConfigurationHash = connectorsConfigHash;
1193 // Add connector Id 0
1194 let lastConnector = '0';
1195 for (lastConnector in stationInfo?.Connectors) {
1196 const connectorStatus = stationInfo?.Connectors[lastConnector];
1197 const lastConnectorId = Utils.convertToInt(lastConnector);
1198 if (
1199 lastConnectorId === 0 &&
1200 this.getUseConnectorId0(stationInfo) === true &&
1201 connectorStatus
1202 ) {
1203 this.checkStationInfoConnectorStatus(lastConnectorId, connectorStatus);
1204 this.connectors.set(
1205 lastConnectorId,
1206 Utils.cloneObject<ConnectorStatus>(connectorStatus)
1207 );
1208 this.getConnectorStatus(lastConnectorId).availability = AvailabilityType.OPERATIVE;
1209 if (Utils.isUndefined(this.getConnectorStatus(lastConnectorId)?.chargingProfiles)) {
1210 this.getConnectorStatus(lastConnectorId).chargingProfiles = [];
1211 }
1212 }
1213 }
1214 // Generate all connectors
1215 if ((stationInfo?.Connectors[0] ? templateMaxConnectors - 1 : templateMaxConnectors) > 0) {
1216 for (let index = 1; index <= configuredMaxConnectors; index++) {
1217 const randConnectorId = stationInfo?.randomConnectors
1218 ? Utils.getRandomInteger(Utils.convertToInt(lastConnector), 1)
1219 : index;
1220 const connectorStatus = stationInfo?.Connectors[randConnectorId.toString()];
1221 this.checkStationInfoConnectorStatus(randConnectorId, connectorStatus);
1222 this.connectors.set(index, Utils.cloneObject<ConnectorStatus>(connectorStatus));
1223 this.getConnectorStatus(index).availability = AvailabilityType.OPERATIVE;
1224 if (Utils.isUndefined(this.getConnectorStatus(index)?.chargingProfiles)) {
1225 this.getConnectorStatus(index).chargingProfiles = [];
1226 }
1227 }
1228 }
1229 }
1230 } else {
1231 logger.warn(
1232 `${this.logPrefix()} Charging station information from template ${
1233 this.templateFile
1234 } with no connectors configuration defined, using already defined connectors`
1235 );
1236 }
1237 // Initialize transaction attributes on connectors
1238 for (const connectorId of this.connectors.keys()) {
1239 if (connectorId > 0 && this.getConnectorStatus(connectorId).transactionStarted === true) {
1240 logger.warn(
1241 `${this.logPrefix()} Connector ${connectorId} at initialization has a transaction started: ${
1242 this.getConnectorStatus(connectorId).transactionId
1243 }`
1244 );
1245 }
1246 if (
1247 connectorId > 0 &&
1248 (this.getConnectorStatus(connectorId).transactionStarted === undefined ||
1249 this.getConnectorStatus(connectorId).transactionStarted === null)
1250 ) {
1251 this.initializeConnectorStatus(connectorId);
1252 }
1253 }
1254 }
1255
1256 private checkStationInfoConnectorStatus(
1257 connectorId: number,
1258 connectorStatus: ConnectorStatus
1259 ): void {
1260 if (!Utils.isNullOrUndefined(connectorStatus?.status)) {
1261 logger.warn(
1262 `${this.logPrefix()} Charging station information from template ${
1263 this.templateFile
1264 } with connector ${connectorId} status configuration defined, undefine it`
1265 );
1266 connectorStatus.status = undefined;
1267 }
1268 }
1269
1270 private getConfigurationFromFile(): ChargingStationConfiguration | null {
1271 let configuration: ChargingStationConfiguration = null;
1272 if (this.configurationFile && fs.existsSync(this.configurationFile)) {
1273 try {
1274 if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) {
1275 configuration = this.sharedLRUCache.getChargingStationConfiguration(
1276 this.configurationFileHash
1277 );
1278 } else {
1279 const measureId = `${FileType.ChargingStationConfiguration} read`;
1280 const beginId = PerformanceStatistics.beginMeasure(measureId);
1281 configuration = JSON.parse(
1282 fs.readFileSync(this.configurationFile, 'utf8')
1283 ) as ChargingStationConfiguration;
1284 PerformanceStatistics.endMeasure(measureId, beginId);
1285 this.configurationFileHash = configuration.configurationHash;
1286 this.sharedLRUCache.setChargingStationConfiguration(configuration);
1287 }
1288 } catch (error) {
1289 FileUtils.handleFileException(
1290 this.logPrefix(),
1291 FileType.ChargingStationConfiguration,
1292 this.configurationFile,
1293 error as NodeJS.ErrnoException
1294 );
1295 }
1296 }
1297 return configuration;
1298 }
1299
1300 private saveConfiguration(): void {
1301 if (this.configurationFile) {
1302 try {
1303 if (!fs.existsSync(path.dirname(this.configurationFile))) {
1304 fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true });
1305 }
1306 const configurationData: ChargingStationConfiguration =
1307 this.getConfigurationFromFile() ?? {};
1308 this.ocppConfiguration?.configurationKey &&
1309 (configurationData.configurationKey = this.ocppConfiguration.configurationKey);
1310 this.stationInfo && (configurationData.stationInfo = this.stationInfo);
1311 delete configurationData.configurationHash;
1312 const configurationHash = crypto
1313 .createHash(Constants.DEFAULT_HASH_ALGORITHM)
1314 .update(JSON.stringify(configurationData))
1315 .digest('hex');
1316 if (this.configurationFileHash !== configurationHash) {
1317 configurationData.configurationHash = configurationHash;
1318 const measureId = `${FileType.ChargingStationConfiguration} write`;
1319 const beginId = PerformanceStatistics.beginMeasure(measureId);
1320 const fileDescriptor = fs.openSync(this.configurationFile, 'w');
1321 fs.writeFileSync(fileDescriptor, JSON.stringify(configurationData, null, 2), 'utf8');
1322 fs.closeSync(fileDescriptor);
1323 PerformanceStatistics.endMeasure(measureId, beginId);
1324 this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
1325 this.configurationFileHash = configurationHash;
1326 this.sharedLRUCache.setChargingStationConfiguration(configurationData);
1327 } else {
1328 logger.debug(
1329 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1330 this.configurationFile
1331 }`
1332 );
1333 }
1334 } catch (error) {
1335 FileUtils.handleFileException(
1336 this.logPrefix(),
1337 FileType.ChargingStationConfiguration,
1338 this.configurationFile,
1339 error as NodeJS.ErrnoException
1340 );
1341 }
1342 } else {
1343 logger.error(
1344 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`
1345 );
1346 }
1347 }
1348
1349 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration | null {
1350 return this.getTemplateFromFile()?.Configuration ?? null;
1351 }
1352
1353 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | null {
1354 let configuration: ChargingStationConfiguration = null;
1355 if (this.getOcppPersistentConfiguration() === true) {
1356 const configurationFromFile = this.getConfigurationFromFile();
1357 configuration = configurationFromFile?.configurationKey && configurationFromFile;
1358 }
1359 configuration && delete configuration.stationInfo;
1360 return configuration;
1361 }
1362
1363 private getOcppConfiguration(): ChargingStationOcppConfiguration | null {
1364 let ocppConfiguration: ChargingStationOcppConfiguration = this.getOcppConfigurationFromFile();
1365 if (!ocppConfiguration) {
1366 ocppConfiguration = this.getOcppConfigurationFromTemplate();
1367 }
1368 return ocppConfiguration;
1369 }
1370
1371 private async onOpen(): Promise<void> {
1372 if (this.isWebSocketConnectionOpened() === true) {
1373 logger.info(
1374 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
1375 );
1376 if (this.isRegistered() === false) {
1377 // Send BootNotification
1378 let registrationRetryCount = 0;
1379 do {
1380 this.bootNotificationResponse = await this.ocppRequestService.requestHandler<
1381 BootNotificationRequest,
1382 BootNotificationResponse
1383 >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
1384 skipBufferingOnError: true,
1385 });
1386 if (this.isRegistered() === false) {
1387 this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
1388 await Utils.sleep(
1389 this.bootNotificationResponse?.interval
1390 ? this.bootNotificationResponse.interval * 1000
1391 : Constants.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
1392 );
1393 }
1394 } while (
1395 this.isRegistered() === false &&
1396 (registrationRetryCount <= this.getRegistrationMaxRetries() ||
1397 this.getRegistrationMaxRetries() === -1)
1398 );
1399 }
1400 if (this.isRegistered() === true) {
1401 if (this.isInAcceptedState() === true) {
1402 await this.startMessageSequence();
1403 }
1404 } else {
1405 logger.error(
1406 `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
1407 );
1408 }
1409 this.wsConnectionRestarted = false;
1410 this.autoReconnectRetryCount = 0;
1411 parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
1412 } else {
1413 logger.warn(
1414 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
1415 );
1416 }
1417 }
1418
1419 private async onClose(code: number, reason: Buffer): Promise<void> {
1420 switch (code) {
1421 // Normal close
1422 case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
1423 case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS:
1424 logger.info(
1425 `${this.logPrefix()} WebSocket normally closed with status '${Utils.getWebSocketCloseEventStatusString(
1426 code
1427 )}' and reason '${reason.toString()}'`
1428 );
1429 this.autoReconnectRetryCount = 0;
1430 break;
1431 // Abnormal close
1432 default:
1433 logger.error(
1434 `${this.logPrefix()} WebSocket abnormally closed with status '${Utils.getWebSocketCloseEventStatusString(
1435 code
1436 )}' and reason '${reason.toString()}'`
1437 );
1438 this.started === true && (await this.reconnect());
1439 break;
1440 }
1441 parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
1442 }
1443
1444 private async onMessage(data: RawData): Promise<void> {
1445 let messageType: number;
1446 let messageId: string;
1447 let commandName: IncomingRequestCommand;
1448 let commandPayload: JsonType;
1449 let errorType: ErrorType;
1450 let errorMessage: string;
1451 let errorDetails: JsonType;
1452 let responseCallback: ResponseCallback;
1453 let errorCallback: ErrorCallback;
1454 let requestCommandName: RequestCommand | IncomingRequestCommand;
1455 let requestPayload: JsonType;
1456 let cachedRequest: CachedRequest;
1457 let errMsg: string;
1458 try {
1459 const request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse;
1460 if (Array.isArray(request) === true) {
1461 [messageType, messageId] = request;
1462 // Check the type of message
1463 switch (messageType) {
1464 // Incoming Message
1465 case MessageType.CALL_MESSAGE:
1466 [, , commandName, commandPayload] = request as IncomingRequest;
1467 if (this.getEnableStatistics() === true) {
1468 this.performanceStatistics.addRequestStatistic(commandName, messageType);
1469 }
1470 logger.debug(
1471 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
1472 request
1473 )}`
1474 );
1475 // Process the message
1476 await this.ocppIncomingRequestService.incomingRequestHandler(
1477 this,
1478 messageId,
1479 commandName,
1480 commandPayload
1481 );
1482 break;
1483 // Outcome Message
1484 case MessageType.CALL_RESULT_MESSAGE:
1485 [, , commandPayload] = request as Response;
1486 if (this.requests.has(messageId) === false) {
1487 // Error
1488 throw new OCPPError(
1489 ErrorType.INTERNAL_ERROR,
1490 `Response for unknown message id ${messageId}`,
1491 null,
1492 commandPayload
1493 );
1494 }
1495 // Respond
1496 cachedRequest = this.requests.get(messageId);
1497 if (Array.isArray(cachedRequest) === true) {
1498 [responseCallback, errorCallback, requestCommandName, requestPayload] = cachedRequest;
1499 } else {
1500 throw new OCPPError(
1501 ErrorType.PROTOCOL_ERROR,
1502 `Cached request for message id ${messageId} response is not an array`,
1503 null,
1504 cachedRequest as unknown as JsonType
1505 );
1506 }
1507 logger.debug(
1508 `${this.logPrefix()} << Command '${
1509 requestCommandName ?? Constants.UNKNOWN_COMMAND
1510 }' received response payload: ${JSON.stringify(request)}`
1511 );
1512 responseCallback(commandPayload, requestPayload);
1513 break;
1514 // Error Message
1515 case MessageType.CALL_ERROR_MESSAGE:
1516 [, , errorType, errorMessage, errorDetails] = request as ErrorResponse;
1517 if (this.requests.has(messageId) === false) {
1518 // Error
1519 throw new OCPPError(
1520 ErrorType.INTERNAL_ERROR,
1521 `Error response for unknown message id ${messageId}`,
1522 null,
1523 { errorType, errorMessage, errorDetails }
1524 );
1525 }
1526 cachedRequest = this.requests.get(messageId);
1527 if (Array.isArray(cachedRequest) === true) {
1528 [, errorCallback, requestCommandName] = cachedRequest;
1529 } else {
1530 throw new OCPPError(
1531 ErrorType.PROTOCOL_ERROR,
1532 `Cached request for message id ${messageId} error response is not an array`,
1533 null,
1534 cachedRequest as unknown as JsonType
1535 );
1536 }
1537 logger.debug(
1538 `${this.logPrefix()} << Command '${
1539 requestCommandName ?? Constants.UNKNOWN_COMMAND
1540 }' received error payload: ${JSON.stringify(request)}`
1541 );
1542 errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
1543 break;
1544 // Error
1545 default:
1546 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1547 errMsg = `Wrong message type ${messageType}`;
1548 logger.error(`${this.logPrefix()} ${errMsg}`);
1549 throw new OCPPError(ErrorType.PROTOCOL_ERROR, errMsg);
1550 }
1551 parentPort.postMessage(MessageChannelUtils.buildUpdatedMessage(this));
1552 } else {
1553 throw new OCPPError(ErrorType.PROTOCOL_ERROR, 'Incoming message is not an array', null, {
1554 request,
1555 });
1556 }
1557 } catch (error) {
1558 // Log
1559 logger.error(
1560 `${this.logPrefix()} Incoming OCPP command '${
1561 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1562 }' message '${data.toString()}'${
1563 messageType !== MessageType.CALL_MESSAGE
1564 ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'`
1565 : ''
1566 } processing error:`,
1567 error
1568 );
1569 if (error instanceof OCPPError === false) {
1570 logger.warn(
1571 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1572 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
1573 }' message '${data.toString()}' handling is not an OCPPError:`,
1574 error
1575 );
1576 }
1577 switch (messageType) {
1578 case MessageType.CALL_MESSAGE:
1579 // Send error
1580 await this.ocppRequestService.sendError(
1581 this,
1582 messageId,
1583 error as OCPPError,
1584 commandName ?? requestCommandName ?? null
1585 );
1586 break;
1587 case MessageType.CALL_RESULT_MESSAGE:
1588 case MessageType.CALL_ERROR_MESSAGE:
1589 if (errorCallback) {
1590 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1591 errorCallback(error as OCPPError, false);
1592 } else {
1593 // Remove the request from the cache in case of error at response handling
1594 this.requests.delete(messageId);
1595 }
1596 break;
1597 }
1598 }
1599 }
1600
1601 private onPing(): void {
1602 logger.debug(this.logPrefix() + ' Received a WS ping (rfc6455) from the server');
1603 }
1604
1605 private onPong(): void {
1606 logger.debug(this.logPrefix() + ' Received a WS pong (rfc6455) from the server');
1607 }
1608
1609 private onError(error: WSError): void {
1610 this.closeWSConnection();
1611 logger.error(this.logPrefix() + ' WebSocket error:', error);
1612 }
1613
1614 private getEnergyActiveImportRegister(
1615 connectorStatus: ConnectorStatus,
1616 meterStop = false
1617 ): number {
1618 if (this.getMeteringPerTransaction() === true) {
1619 return (
1620 (meterStop === true
1621 ? Math.round(connectorStatus?.transactionEnergyActiveImportRegisterValue)
1622 : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0
1623 );
1624 }
1625 return (
1626 (meterStop === true
1627 ? Math.round(connectorStatus?.energyActiveImportRegisterValue)
1628 : connectorStatus?.energyActiveImportRegisterValue) ?? 0
1629 );
1630 }
1631
1632 private getUseConnectorId0(stationInfo?: ChargingStationInfo): boolean {
1633 const localStationInfo = stationInfo ?? this.stationInfo;
1634 return !Utils.isUndefined(localStationInfo.useConnectorId0)
1635 ? localStationInfo.useConnectorId0
1636 : true;
1637 }
1638
1639 private getNumberOfRunningTransactions(): number {
1640 let trxCount = 0;
1641 for (const connectorId of this.connectors.keys()) {
1642 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
1643 trxCount++;
1644 }
1645 }
1646 return trxCount;
1647 }
1648
1649 private async stopRunningTransactions(reason = StopTransactionReason.NONE): Promise<void> {
1650 for (const connectorId of this.connectors.keys()) {
1651 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
1652 await this.stopTransactionOnConnector(connectorId, reason);
1653 }
1654 }
1655 }
1656
1657 // 0 for disabling
1658 private getConnectionTimeout(): number {
1659 if (
1660 ChargingStationConfigurationUtils.getConfigurationKey(
1661 this,
1662 StandardParametersKey.ConnectionTimeOut
1663 )
1664 ) {
1665 return (
1666 parseInt(
1667 ChargingStationConfigurationUtils.getConfigurationKey(
1668 this,
1669 StandardParametersKey.ConnectionTimeOut
1670 ).value
1671 ) ?? Constants.DEFAULT_CONNECTION_TIMEOUT
1672 );
1673 }
1674 return Constants.DEFAULT_CONNECTION_TIMEOUT;
1675 }
1676
1677 // -1 for unlimited, 0 for disabling
1678 private getAutoReconnectMaxRetries(): number {
1679 if (!Utils.isUndefined(this.stationInfo.autoReconnectMaxRetries)) {
1680 return this.stationInfo.autoReconnectMaxRetries;
1681 }
1682 if (!Utils.isUndefined(Configuration.getAutoReconnectMaxRetries())) {
1683 return Configuration.getAutoReconnectMaxRetries();
1684 }
1685 return -1;
1686 }
1687
1688 // 0 for disabling
1689 private getRegistrationMaxRetries(): number {
1690 if (!Utils.isUndefined(this.stationInfo.registrationMaxRetries)) {
1691 return this.stationInfo.registrationMaxRetries;
1692 }
1693 return -1;
1694 }
1695
1696 private getPowerDivider(): number {
1697 let powerDivider = this.getNumberOfConnectors();
1698 if (this.stationInfo?.powerSharedByConnectors) {
1699 powerDivider = this.getNumberOfRunningTransactions();
1700 }
1701 return powerDivider;
1702 }
1703
1704 private getMaximumPower(stationInfo?: ChargingStationInfo): number {
1705 const localStationInfo = stationInfo ?? this.stationInfo;
1706 return (localStationInfo['maxPower'] as number) ?? localStationInfo.maximumPower;
1707 }
1708
1709 private getMaximumAmperage(stationInfo: ChargingStationInfo): number | undefined {
1710 const maximumPower = this.getMaximumPower(stationInfo);
1711 switch (this.getCurrentOutType(stationInfo)) {
1712 case CurrentType.AC:
1713 return ACElectricUtils.amperagePerPhaseFromPower(
1714 this.getNumberOfPhases(stationInfo),
1715 maximumPower / this.getNumberOfConnectors(),
1716 this.getVoltageOut(stationInfo)
1717 );
1718 case CurrentType.DC:
1719 return DCElectricUtils.amperage(maximumPower, this.getVoltageOut(stationInfo));
1720 }
1721 }
1722
1723 private getAmperageLimitation(): number | undefined {
1724 if (
1725 this.stationInfo.amperageLimitationOcppKey &&
1726 ChargingStationConfigurationUtils.getConfigurationKey(
1727 this,
1728 this.stationInfo.amperageLimitationOcppKey
1729 )
1730 ) {
1731 return (
1732 Utils.convertToInt(
1733 ChargingStationConfigurationUtils.getConfigurationKey(
1734 this,
1735 this.stationInfo.amperageLimitationOcppKey
1736 ).value
1737 ) / ChargingStationUtils.getAmperageLimitationUnitDivider(this.stationInfo)
1738 );
1739 }
1740 }
1741
1742 private getChargingProfilePowerLimit(connectorId: number): number | undefined {
1743 let limit: number, matchingChargingProfile: ChargingProfile;
1744 let chargingProfiles: ChargingProfile[] = [];
1745 // Get charging profiles for connector and sort by stack level
1746 chargingProfiles = this.getConnectorStatus(connectorId).chargingProfiles.sort(
1747 (a, b) => b.stackLevel - a.stackLevel
1748 );
1749 // Get profiles on connector 0
1750 if (this.getConnectorStatus(0).chargingProfiles) {
1751 chargingProfiles.push(
1752 ...this.getConnectorStatus(0).chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel)
1753 );
1754 }
1755 if (!Utils.isEmptyArray(chargingProfiles)) {
1756 const result = ChargingStationUtils.getLimitFromChargingProfiles(
1757 chargingProfiles,
1758 this.logPrefix()
1759 );
1760 if (!Utils.isNullOrUndefined(result)) {
1761 limit = result.limit;
1762 matchingChargingProfile = result.matchingChargingProfile;
1763 switch (this.getCurrentOutType()) {
1764 case CurrentType.AC:
1765 limit =
1766 matchingChargingProfile.chargingSchedule.chargingRateUnit ===
1767 ChargingRateUnitType.WATT
1768 ? limit
1769 : ACElectricUtils.powerTotal(this.getNumberOfPhases(), this.getVoltageOut(), limit);
1770 break;
1771 case CurrentType.DC:
1772 limit =
1773 matchingChargingProfile.chargingSchedule.chargingRateUnit ===
1774 ChargingRateUnitType.WATT
1775 ? limit
1776 : DCElectricUtils.power(this.getVoltageOut(), limit);
1777 }
1778 const connectorMaximumPower = this.getMaximumPower() / this.powerDivider;
1779 if (limit > connectorMaximumPower) {
1780 logger.error(
1781 `${this.logPrefix()} Charging profile id ${
1782 matchingChargingProfile.chargingProfileId
1783 } limit is greater than connector id ${connectorId} maximum, dump charging profiles' stack: %j`,
1784 this.getConnectorStatus(connectorId).chargingProfiles
1785 );
1786 limit = connectorMaximumPower;
1787 }
1788 }
1789 }
1790 return limit;
1791 }
1792
1793 private async startMessageSequence(): Promise<void> {
1794 if (this.stationInfo?.autoRegister === true) {
1795 await this.ocppRequestService.requestHandler<
1796 BootNotificationRequest,
1797 BootNotificationResponse
1798 >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
1799 skipBufferingOnError: true,
1800 });
1801 }
1802 // Start WebSocket ping
1803 this.startWebSocketPing();
1804 // Start heartbeat
1805 this.startHeartbeat();
1806 // Initialize connectors status
1807 for (const connectorId of this.connectors.keys()) {
1808 let chargePointStatus: ChargePointStatus;
1809 if (connectorId === 0) {
1810 continue;
1811 } else if (
1812 !this.getConnectorStatus(connectorId)?.status &&
1813 (this.isChargingStationAvailable() === false ||
1814 this.isConnectorAvailable(connectorId) === false)
1815 ) {
1816 chargePointStatus = ChargePointStatus.UNAVAILABLE;
1817 } else if (
1818 !this.getConnectorStatus(connectorId)?.status &&
1819 this.getConnectorStatus(connectorId)?.bootStatus
1820 ) {
1821 // Set boot status in template at startup
1822 chargePointStatus = this.getConnectorStatus(connectorId).bootStatus;
1823 } else if (this.getConnectorStatus(connectorId)?.status) {
1824 // Set previous status at startup
1825 chargePointStatus = this.getConnectorStatus(connectorId).status;
1826 } else {
1827 // Set default status
1828 chargePointStatus = ChargePointStatus.AVAILABLE;
1829 }
1830 await this.ocppRequestService.requestHandler<
1831 StatusNotificationRequest,
1832 StatusNotificationResponse
1833 >(this, RequestCommand.STATUS_NOTIFICATION, {
1834 connectorId,
1835 status: chargePointStatus,
1836 errorCode: ChargePointErrorCode.NO_ERROR,
1837 });
1838 this.getConnectorStatus(connectorId).status = chargePointStatus;
1839 }
1840 // Start the ATG
1841 if (this.getAutomaticTransactionGeneratorConfigurationFromTemplate()?.enable === true) {
1842 this.startAutomaticTransactionGenerator();
1843 }
1844 this.wsConnectionRestarted === true && this.flushMessageBuffer();
1845 }
1846
1847 private async stopMessageSequence(
1848 reason: StopTransactionReason = StopTransactionReason.NONE
1849 ): Promise<void> {
1850 // Stop WebSocket ping
1851 this.stopWebSocketPing();
1852 // Stop heartbeat
1853 this.stopHeartbeat();
1854 // Stop ongoing transactions
1855 if (this.automaticTransactionGenerator?.started === true) {
1856 this.stopAutomaticTransactionGenerator();
1857 } else {
1858 await this.stopRunningTransactions(reason);
1859 }
1860 for (const connectorId of this.connectors.keys()) {
1861 if (connectorId > 0) {
1862 await this.ocppRequestService.requestHandler<
1863 StatusNotificationRequest,
1864 StatusNotificationResponse
1865 >(this, RequestCommand.STATUS_NOTIFICATION, {
1866 connectorId,
1867 status: ChargePointStatus.UNAVAILABLE,
1868 errorCode: ChargePointErrorCode.NO_ERROR,
1869 });
1870 this.getConnectorStatus(connectorId).status = null;
1871 }
1872 }
1873 }
1874
1875 private startWebSocketPing(): void {
1876 const webSocketPingInterval: number = ChargingStationConfigurationUtils.getConfigurationKey(
1877 this,
1878 StandardParametersKey.WebSocketPingInterval
1879 )
1880 ? Utils.convertToInt(
1881 ChargingStationConfigurationUtils.getConfigurationKey(
1882 this,
1883 StandardParametersKey.WebSocketPingInterval
1884 ).value
1885 )
1886 : 0;
1887 if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
1888 this.webSocketPingSetInterval = setInterval(() => {
1889 if (this.isWebSocketConnectionOpened() === true) {
1890 this.wsConnection.ping();
1891 }
1892 }, webSocketPingInterval * 1000);
1893 logger.info(
1894 this.logPrefix() +
1895 ' WebSocket ping started every ' +
1896 Utils.formatDurationSeconds(webSocketPingInterval)
1897 );
1898 } else if (this.webSocketPingSetInterval) {
1899 logger.info(
1900 this.logPrefix() +
1901 ' WebSocket ping already started every ' +
1902 Utils.formatDurationSeconds(webSocketPingInterval)
1903 );
1904 } else {
1905 logger.error(
1906 `${this.logPrefix()} WebSocket ping interval set to ${
1907 webSocketPingInterval
1908 ? Utils.formatDurationSeconds(webSocketPingInterval)
1909 : webSocketPingInterval
1910 }, not starting the WebSocket ping`
1911 );
1912 }
1913 }
1914
1915 private stopWebSocketPing(): void {
1916 if (this.webSocketPingSetInterval) {
1917 clearInterval(this.webSocketPingSetInterval);
1918 }
1919 }
1920
1921 private getConfiguredSupervisionUrl(): URL {
1922 const supervisionUrls = this.stationInfo.supervisionUrls ?? Configuration.getSupervisionUrls();
1923 if (!Utils.isEmptyArray(supervisionUrls)) {
1924 switch (Configuration.getSupervisionUrlDistribution()) {
1925 case SupervisionUrlDistribution.ROUND_ROBIN:
1926 // FIXME
1927 this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
1928 break;
1929 case SupervisionUrlDistribution.RANDOM:
1930 this.configuredSupervisionUrlIndex = Math.floor(
1931 Utils.secureRandom() * supervisionUrls.length
1932 );
1933 break;
1934 case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY:
1935 this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
1936 break;
1937 default:
1938 logger.error(
1939 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
1940 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
1941 }`
1942 );
1943 this.configuredSupervisionUrlIndex = (this.index - 1) % supervisionUrls.length;
1944 break;
1945 }
1946 return new URL(supervisionUrls[this.configuredSupervisionUrlIndex]);
1947 }
1948 return new URL(supervisionUrls as string);
1949 }
1950
1951 private getHeartbeatInterval(): number {
1952 const HeartbeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
1953 this,
1954 StandardParametersKey.HeartbeatInterval
1955 );
1956 if (HeartbeatInterval) {
1957 return Utils.convertToInt(HeartbeatInterval.value) * 1000;
1958 }
1959 const HeartBeatInterval = ChargingStationConfigurationUtils.getConfigurationKey(
1960 this,
1961 StandardParametersKey.HeartBeatInterval
1962 );
1963 if (HeartBeatInterval) {
1964 return Utils.convertToInt(HeartBeatInterval.value) * 1000;
1965 }
1966 this.stationInfo?.autoRegister === false &&
1967 logger.warn(
1968 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
1969 Constants.DEFAULT_HEARTBEAT_INTERVAL
1970 }`
1971 );
1972 return Constants.DEFAULT_HEARTBEAT_INTERVAL;
1973 }
1974
1975 private stopHeartbeat(): void {
1976 if (this.heartbeatSetInterval) {
1977 clearInterval(this.heartbeatSetInterval);
1978 }
1979 }
1980
1981 private terminateWSConnection(): void {
1982 if (this.isWebSocketConnectionOpened() === true) {
1983 this.wsConnection.terminate();
1984 this.wsConnection = null;
1985 }
1986 }
1987
1988 private stopMeterValues(connectorId: number) {
1989 if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
1990 clearInterval(this.getConnectorStatus(connectorId).transactionSetInterval);
1991 }
1992 }
1993
1994 private getReconnectExponentialDelay(): boolean {
1995 return !Utils.isUndefined(this.stationInfo.reconnectExponentialDelay)
1996 ? this.stationInfo.reconnectExponentialDelay
1997 : false;
1998 }
1999
2000 private async reconnect(): Promise<void> {
2001 // Stop WebSocket ping
2002 this.stopWebSocketPing();
2003 // Stop heartbeat
2004 this.stopHeartbeat();
2005 // Stop the ATG if needed
2006 if (this.automaticTransactionGenerator?.configuration?.stopOnConnectionFailure === true) {
2007 this.stopAutomaticTransactionGenerator();
2008 }
2009 if (
2010 this.autoReconnectRetryCount < this.getAutoReconnectMaxRetries() ||
2011 this.getAutoReconnectMaxRetries() === -1
2012 ) {
2013 this.autoReconnectRetryCount++;
2014 const reconnectDelay = this.getReconnectExponentialDelay()
2015 ? Utils.exponentialDelay(this.autoReconnectRetryCount)
2016 : this.getConnectionTimeout() * 1000;
2017 const reconnectDelayWithdraw = 1000;
2018 const reconnectTimeout =
2019 reconnectDelay && reconnectDelay - reconnectDelayWithdraw > 0
2020 ? reconnectDelay - reconnectDelayWithdraw
2021 : 0;
2022 logger.error(
2023 `${this.logPrefix()} WebSocket connection retry in ${Utils.roundTo(
2024 reconnectDelay,
2025 2
2026 )}ms, timeout ${reconnectTimeout}ms`
2027 );
2028 await Utils.sleep(reconnectDelay);
2029 logger.error(
2030 this.logPrefix() + ' WebSocket connection retry #' + this.autoReconnectRetryCount.toString()
2031 );
2032 this.openWSConnection(
2033 { ...(this.stationInfo?.wsOptions ?? {}), handshakeTimeout: reconnectTimeout },
2034 { closeOpened: true }
2035 );
2036 this.wsConnectionRestarted = true;
2037 } else if (this.getAutoReconnectMaxRetries() !== -1) {
2038 logger.error(
2039 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
2040 this.autoReconnectRetryCount
2041 }) or retries disabled (${this.getAutoReconnectMaxRetries()})`
2042 );
2043 }
2044 }
2045
2046 private getAutomaticTransactionGeneratorConfigurationFromTemplate(): AutomaticTransactionGeneratorConfiguration | null {
2047 return this.getTemplateFromFile()?.AutomaticTransactionGenerator ?? null;
2048 }
2049
2050 private initializeConnectorStatus(connectorId: number): void {
2051 this.getConnectorStatus(connectorId).idTagLocalAuthorized = false;
2052 this.getConnectorStatus(connectorId).idTagAuthorized = false;
2053 this.getConnectorStatus(connectorId).transactionRemoteStarted = false;
2054 this.getConnectorStatus(connectorId).transactionStarted = false;
2055 this.getConnectorStatus(connectorId).energyActiveImportRegisterValue = 0;
2056 this.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0;
2057 }
2058 }