fix: make ATG wait for CS/connector availability
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
CommitLineData
edd13439 1// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
b4d34251 2
d972af76 3import { createHash } from 'node:crypto';
db54d2e0 4import { EventEmitter } from 'node:events';
0ebf7c2e 5import { type FSWatcher, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
d972af76 6import { dirname, join } from 'node:path';
130783a7 7import { URL } from 'node:url';
01f4001e 8import { parentPort } from 'node:worker_threads';
8114d10e 9
be4c6702 10import { millisecondsToSeconds, secondsToMilliseconds } from 'date-fns';
5d280aae 11import merge from 'just-merge';
d972af76 12import { type RawData, WebSocket } from 'ws';
8114d10e 13
4c3c0d59 14import { AutomaticTransactionGenerator } from './AutomaticTransactionGenerator';
7671fa0b 15import { ChargingStationWorkerBroadcastChannel } from './broadcast-channel/ChargingStationWorkerBroadcastChannel';
f2d5e3d9
JB
16import {
17 addConfigurationKey,
18 deleteConfigurationKey,
19 getConfigurationKey,
20 setConfigurationKeyValue,
357a5553 21} from './ConfigurationKeyUtils';
fba11dc6
JB
22import {
23 buildConnectorsMap,
e6a33233 24 checkChargingStation,
fba11dc6
JB
25 checkConnectorsConfiguration,
26 checkStationInfoConnectorStatus,
27 checkTemplate,
fba11dc6
JB
28 createBootNotificationRequest,
29 createSerialNumber,
30 getAmperageLimitationUnitDivider,
31 getBootConnectorStatus,
32 getChargingStationConnectorChargingProfilesPowerLimit,
33 getChargingStationId,
34 getDefaultVoltageOut,
35 getHashId,
36 getIdTagsFile,
37 getMaxNumberOfEvses,
90aceaf6 38 getNumberOfReservableConnectors,
fba11dc6 39 getPhaseRotationValue,
d8093be1 40 hasFeatureProfile,
90aceaf6 41 hasReservationExpired,
fba11dc6
JB
42 initializeConnectorsMapStatus,
43 propagateSerialNumber,
90aceaf6 44 removeExpiredReservations,
fba11dc6
JB
45 stationTemplateToStationInfo,
46 warnTemplateKeysDeprecation,
08b58f00
JB
47} from './Helpers';
48import { IdTagsCache } from './IdTagsCache';
49import {
50 OCPP16IncomingRequestService,
51 OCPP16RequestService,
52 OCPP16ResponseService,
53 OCPP16ServiceUtils,
54 OCPP20IncomingRequestService,
55 OCPP20RequestService,
56 OCPP20ResponseService,
57 type OCPPIncomingRequestService,
58 type OCPPRequestService,
59 OCPPServiceUtils,
60} from './ocpp';
61import { SharedLRUCache } from './SharedLRUCache';
268a74bb 62import { BaseError, OCPPError } from '../exception';
b84bca85 63import { PerformanceStatistics } from '../performance';
e7aeea18 64import {
268a74bb 65 type AutomaticTransactionGeneratorConfiguration,
e7aeea18 66 AvailabilityType,
e0b0ee21 67 type BootNotificationRequest,
268a74bb 68 type BootNotificationResponse,
e0b0ee21 69 type CachedRequest,
268a74bb 70 type ChargingStationConfiguration,
db54d2e0 71 ChargingStationEvents,
268a74bb
JB
72 type ChargingStationInfo,
73 type ChargingStationOcppConfiguration,
74 type ChargingStationTemplate,
c8aafe0d 75 type ConnectorStatus,
268a74bb
JB
76 ConnectorStatusEnum,
77 CurrentType,
e0b0ee21 78 type ErrorCallback,
268a74bb
JB
79 type ErrorResponse,
80 ErrorType,
2585c6e9 81 type EvseStatus,
52952bf8 82 type EvseStatusConfiguration,
268a74bb 83 FileType,
c9a4f9ea
JB
84 FirmwareStatus,
85 type FirmwareStatusNotificationRequest,
268a74bb
JB
86 type FirmwareStatusNotificationResponse,
87 type FirmwareUpgrade,
e0b0ee21 88 type HeartbeatRequest,
268a74bb 89 type HeartbeatResponse,
e0b0ee21 90 type IncomingRequest,
268a74bb
JB
91 type IncomingRequestCommand,
92 type JsonType,
93 MessageType,
94 type MeterValue,
95 MeterValueMeasurand,
e0b0ee21 96 type MeterValuesRequest,
268a74bb
JB
97 type MeterValuesResponse,
98 OCPPVersion,
8ca6874c 99 type OutgoingRequest,
268a74bb
JB
100 PowerUnits,
101 RegistrationStatusEnumType,
e7aeea18 102 RequestCommand,
66dd3447 103 type Reservation,
366f75f6 104 type ReservationKey,
66dd3447 105 ReservationTerminationReason,
268a74bb 106 type Response,
268a74bb 107 StandardParametersKey,
5ced7e80 108 type Status,
e0b0ee21 109 type StatusNotificationRequest,
e0b0ee21 110 type StatusNotificationResponse,
ef6fa3fb 111 StopTransactionReason,
e0b0ee21
JB
112 type StopTransactionRequest,
113 type StopTransactionResponse,
268a74bb
JB
114 SupervisionUrlDistribution,
115 SupportedFeatureProfiles,
6dad8e21 116 VendorParametersKey,
268a74bb
JB
117 type WSError,
118 WebSocketCloseEventStatusCode,
119 type WsOptions,
120} from '../types';
60a74391
JB
121import {
122 ACElectricUtils,
1227a6f1
JB
123 AsyncLock,
124 AsyncLockType,
60a74391
JB
125 Configuration,
126 Constants,
127 DCElectricUtils,
179ed367
JB
128 buildChargingStationAutomaticTransactionGeneratorConfiguration,
129 buildConnectorsStatus,
130 buildEvsesStatus,
c8faabc8
JB
131 buildStartedMessage,
132 buildStoppedMessage,
133 buildUpdatedMessage,
9bf0ef23
JB
134 cloneObject,
135 convertToBoolean,
136 convertToInt,
137 exponentialDelay,
138 formatDurationMilliSeconds,
139 formatDurationSeconds,
140 getRandomInteger,
141 getWebSocketCloseEventStatusString,
fa5995d6 142 handleFileException,
9bf0ef23
JB
143 isNotEmptyArray,
144 isNotEmptyString,
145 isNullOrUndefined,
146 isUndefined,
147 logPrefix,
60a74391 148 logger,
5adf6ca4 149 min,
5f742aac 150 once,
9bf0ef23
JB
151 roundTo,
152 secureRandom,
153 sleep,
1f8f6332 154 watchJsonFile,
60a74391 155} from '../utils';
3f40bc9c 156
db54d2e0 157export class ChargingStation extends EventEmitter {
c72f6634 158 public readonly index: number;
2484ac1e 159 public readonly templateFile: string;
452a82ca 160 public started: boolean;
cbf9b878 161 public starting: boolean;
f911a4af 162 public idTagsCache: IdTagsCache;
551e477c
JB
163 public automaticTransactionGenerator!: AutomaticTransactionGenerator | undefined;
164 public ocppConfiguration!: ChargingStationOcppConfiguration | undefined;
4ed03b6e 165 public wsConnection: WebSocket | null;
8df5ae48 166 public readonly connectors: Map<number, ConnectorStatus>;
2585c6e9 167 public readonly evses: Map<number, EvseStatus>;
9e23580d 168 public readonly requests: Map<string, CachedRequest>;
551e477c 169 public performanceStatistics!: PerformanceStatistics | undefined;
e1d9a0f4 170 public heartbeatSetInterval?: NodeJS.Timeout;
6e0964c8 171 public ocppRequestService!: OCPPRequestService;
0a03f36c 172 public bootNotificationRequest!: BootNotificationRequest;
1895299d 173 public bootNotificationResponse!: BootNotificationResponse | undefined;
fa7bccf4 174 public powerDivider!: number;
5398cecf 175 private internalStationInfo!: ChargingStationInfo;
950b1349 176 private stopping: boolean;
073bd098 177 private configurationFile!: string;
7c72977b 178 private configurationFileHash!: string;
6e0964c8 179 private connectorsConfigurationHash!: string;
2585c6e9 180 private evsesConfigurationHash!: string;
c7db8ecb 181 private automaticTransactionGeneratorConfiguration?: AutomaticTransactionGeneratorConfiguration;
a472cf2b 182 private ocppIncomingRequestService!: OCPPIncomingRequestService;
8e242273 183 private readonly messageBuffer: Set<string>;
fa7bccf4 184 private configuredSupervisionUrl!: URL;
265e4266 185 private wsConnectionRestarted: boolean;
ad2f27c3 186 private autoReconnectRetryCount: number;
d972af76 187 private templateFileWatcher!: FSWatcher | undefined;
cda5d0fb 188 private templateFileHash!: string;
57adbebc 189 private readonly sharedLRUCache: SharedLRUCache;
e1d9a0f4 190 private webSocketPingSetInterval?: NodeJS.Timeout;
89b7a234 191 private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel;
178956d8 192 private reservationExpirationSetInterval?: NodeJS.Timeout;
6af9012e 193
2484ac1e 194 constructor(index: number, templateFile: string) {
db54d2e0 195 super();
aa428a31 196 this.started = false;
950b1349
JB
197 this.starting = false;
198 this.stopping = false;
4ed03b6e 199 this.wsConnection = null;
aa428a31
JB
200 this.wsConnectionRestarted = false;
201 this.autoReconnectRetryCount = 0;
ad2f27c3 202 this.index = index;
2484ac1e 203 this.templateFile = templateFile;
9f2e3130 204 this.connectors = new Map<number, ConnectorStatus>();
2585c6e9 205 this.evses = new Map<number, EvseStatus>();
32b02249 206 this.requests = new Map<string, CachedRequest>();
8e242273 207 this.messageBuffer = new Set<string>();
b44b779a 208 this.sharedLRUCache = SharedLRUCache.getInstance();
f911a4af 209 this.idTagsCache = IdTagsCache.getInstance();
89b7a234 210 this.chargingStationWorkerBroadcastChannel = new ChargingStationWorkerBroadcastChannel(this);
32de5a57 211
db54d2e0
JB
212 this.on(ChargingStationEvents.started, () => {
213 parentPort?.postMessage(buildStartedMessage(this));
214 });
215 this.on(ChargingStationEvents.stopped, () => {
216 parentPort?.postMessage(buildStoppedMessage(this));
217 });
218 this.on(ChargingStationEvents.updated, () => {
219 parentPort?.postMessage(buildUpdatedMessage(this));
220 });
221
9f2e3130 222 this.initialize();
c0560973
JB
223 }
224
a14022a2
JB
225 public get hasEvses(): boolean {
226 return this.connectors.size === 0 && this.evses.size > 0;
227 }
228
5398cecf
JB
229 public get stationInfo(): ChargingStationInfo {
230 return {
5398cecf
JB
231 ...{
232 enableStatistics: false,
233 remoteAuthorization: true,
234 currentOutType: CurrentType.AC,
235 ocppStrictCompliance: true,
236 outOfOrderEndMeterValues: false,
237 beginEndMeterValues: false,
238 meteringPerTransaction: true,
239 transactionDataMeterValues: false,
240 mainVoltageMeterValues: true,
241 phaseLineToLineVoltageMeterValues: false,
242 customValueLimitationMeterValues: true,
243 supervisionUrlOcppConfiguration: false,
244 supervisionUrlOcppKey: VendorParametersKey.ConnectionUrl,
245 ocppVersion: OCPPVersion.VERSION_16,
246 ocppPersistentConfiguration: true,
247 stationInfoPersistentConfiguration: true,
248 automaticTransactionGeneratorPersistentConfiguration: true,
249 autoReconnectMaxRetries: -1,
250 registrationMaxRetries: -1,
251 reconnectExponentialDelay: false,
252 stopTransactionsOnStopped: true,
253 },
73de8f02 254 ...this.internalStationInfo,
5398cecf
JB
255 };
256 }
257
25f5a959 258 private get wsConnectionUrl(): URL {
fa7bccf4 259 return new URL(
44eb6026 260 `${
5398cecf
JB
261 this.stationInfo?.supervisionUrlOcppConfiguration &&
262 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
263 isNotEmptyString(getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)?.value)
264 ? getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)!.value
44eb6026 265 : this.configuredSupervisionUrl.href
5edd8ba0 266 }/${this.stationInfo.chargingStationId}`,
fa7bccf4 267 );
12fc74d6
JB
268 }
269
8b7072dc 270 public logPrefix = (): string => {
9bf0ef23 271 return logPrefix(
ccb1d6e9 272 ` ${
9bf0ef23 273 (isNotEmptyString(this?.stationInfo?.chargingStationId)
e302df1d 274 ? this?.stationInfo?.chargingStationId
e1d9a0f4 275 : getChargingStationId(this.index, this.getTemplateFromFile()!)) ??
e302df1d 276 'Error at building log prefix'
5edd8ba0 277 } |`,
ccb1d6e9 278 );
8b7072dc 279 };
c0560973 280
f911a4af 281 public hasIdTags(): boolean {
e1d9a0f4 282 return isNotEmptyArray(this.idTagsCache.getIdTags(getIdTagsFile(this.stationInfo)!));
c0560973
JB
283 }
284
e1d9a0f4 285 public getNumberOfPhases(stationInfo?: ChargingStationInfo): number {
fa7bccf4
JB
286 const localStationInfo: ChargingStationInfo = stationInfo ?? this.stationInfo;
287 switch (this.getCurrentOutType(stationInfo)) {
4c2b4904 288 case CurrentType.AC:
e1d9a0f4 289 return !isUndefined(localStationInfo.numberOfPhases) ? localStationInfo.numberOfPhases! : 3;
4c2b4904 290 case CurrentType.DC:
c0560973
JB
291 return 0;
292 }
293 }
294
d5bff457 295 public isWebSocketConnectionOpened(): boolean {
0d8140bd 296 return this?.wsConnection?.readyState === WebSocket.OPEN;
c0560973
JB
297 }
298
1895299d 299 public getRegistrationStatus(): RegistrationStatusEnumType | undefined {
672fed6e
JB
300 return this?.bootNotificationResponse?.status;
301 }
302
f7c2994d 303 public inUnknownState(): boolean {
9bf0ef23 304 return isNullOrUndefined(this?.bootNotificationResponse?.status);
73c4266d
JB
305 }
306
f7c2994d 307 public inPendingState(): boolean {
d270cc87 308 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.PENDING;
16cd35ad
JB
309 }
310
f7c2994d 311 public inAcceptedState(): boolean {
d270cc87 312 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.ACCEPTED;
c0560973
JB
313 }
314
f7c2994d 315 public inRejectedState(): boolean {
d270cc87 316 return this?.bootNotificationResponse?.status === RegistrationStatusEnumType.REJECTED;
16cd35ad
JB
317 }
318
319 public isRegistered(): boolean {
ed6cfcff 320 return (
f7c2994d
JB
321 this.inUnknownState() === false &&
322 (this.inAcceptedState() === true || this.inPendingState() === true)
ed6cfcff 323 );
16cd35ad
JB
324 }
325
c0560973 326 public isChargingStationAvailable(): boolean {
0d6f335f 327 return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative;
c0560973
JB
328 }
329
a14022a2
JB
330 public hasConnector(connectorId: number): boolean {
331 if (this.hasEvses) {
332 for (const evseStatus of this.evses.values()) {
333 if (evseStatus.connectors.has(connectorId)) {
334 return true;
335 }
336 }
337 return false;
338 }
339 return this.connectors.has(connectorId);
340 }
341
28e78158
JB
342 public isConnectorAvailable(connectorId: number): boolean {
343 return (
344 connectorId > 0 &&
345 this.getConnectorStatus(connectorId)?.availability === AvailabilityType.Operative
346 );
c0560973
JB
347 }
348
54544ef1 349 public getNumberOfConnectors(): number {
28e78158
JB
350 if (this.hasEvses) {
351 let numberOfConnectors = 0;
352 for (const [evseId, evseStatus] of this.evses) {
4334db72
JB
353 if (evseId > 0) {
354 numberOfConnectors += evseStatus.connectors.size;
28e78158 355 }
28e78158
JB
356 }
357 return numberOfConnectors;
358 }
007b5bde 359 return this.connectors.has(0) ? this.connectors.size - 1 : this.connectors.size;
54544ef1
JB
360 }
361
28e78158 362 public getNumberOfEvses(): number {
007b5bde 363 return this.evses.has(0) ? this.evses.size - 1 : this.evses.size;
28e78158
JB
364 }
365
366 public getConnectorStatus(connectorId: number): ConnectorStatus | undefined {
367 if (this.hasEvses) {
368 for (const evseStatus of this.evses.values()) {
369 if (evseStatus.connectors.has(connectorId)) {
370 return evseStatus.connectors.get(connectorId);
371 }
372 }
5e128725 373 return undefined;
28e78158
JB
374 }
375 return this.connectors.get(connectorId);
c0560973
JB
376 }
377
ad8537a7 378 public getConnectorMaximumAvailablePower(connectorId: number): number {
e1d9a0f4 379 let connectorAmperageLimitationPowerLimit: number | undefined;
b47d68d7 380 if (
9bf0ef23 381 !isNullOrUndefined(this.getAmperageLimitation()) &&
e1d9a0f4 382 this.getAmperageLimitation()! < this.stationInfo.maximumAmperage!
b47d68d7 383 ) {
4160ae28 384 connectorAmperageLimitationPowerLimit =
5398cecf 385 (this.stationInfo?.currentOutType === CurrentType.AC
cc6e8ab5
JB
386 ? ACElectricUtils.powerTotal(
387 this.getNumberOfPhases(),
5398cecf 388 this.stationInfo.voltageOut!,
e1d9a0f4 389 this.getAmperageLimitation()! *
5edd8ba0 390 (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()),
cc6e8ab5 391 )
5398cecf 392 : DCElectricUtils.power(this.stationInfo.voltageOut!, this.getAmperageLimitation()!)) /
fa7bccf4 393 this.powerDivider;
cc6e8ab5 394 }
5398cecf 395 const connectorMaximumPower = this.stationInfo.maximumPower! / this.powerDivider;
15068be9 396 const connectorChargingProfilesPowerLimit =
fba11dc6 397 getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId);
5adf6ca4 398 return min(
ad8537a7 399 isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower,
e1d9a0f4 400 isNaN(connectorAmperageLimitationPowerLimit!)
ad8537a7 401 ? Infinity
e1d9a0f4
JB
402 : connectorAmperageLimitationPowerLimit!,
403 isNaN(connectorChargingProfilesPowerLimit!) ? Infinity : connectorChargingProfilesPowerLimit!,
ad8537a7 404 );
cc6e8ab5
JB
405 }
406
6e0964c8 407 public getTransactionIdTag(transactionId: number): string | undefined {
28e78158
JB
408 if (this.hasEvses) {
409 for (const evseStatus of this.evses.values()) {
410 for (const connectorStatus of evseStatus.connectors.values()) {
411 if (connectorStatus.transactionId === transactionId) {
412 return connectorStatus.transactionIdTag;
413 }
414 }
415 }
416 } else {
417 for (const connectorId of this.connectors.keys()) {
3fa7f799 418 if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) {
28e78158
JB
419 return this.getConnectorStatus(connectorId)?.transactionIdTag;
420 }
c0560973
JB
421 }
422 }
423 }
424
ded57f02 425 public getNumberOfRunningTransactions(): number {
cfc9875a 426 let numberOfRunningTransactions = 0;
ded57f02 427 if (this.hasEvses) {
3fa7f799
JB
428 for (const [evseId, evseStatus] of this.evses) {
429 if (evseId === 0) {
430 continue;
431 }
ded57f02
JB
432 for (const connectorStatus of evseStatus.connectors.values()) {
433 if (connectorStatus.transactionStarted === true) {
cfc9875a 434 ++numberOfRunningTransactions;
ded57f02
JB
435 }
436 }
437 }
438 } else {
439 for (const connectorId of this.connectors.keys()) {
440 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
cfc9875a 441 ++numberOfRunningTransactions;
ded57f02
JB
442 }
443 }
444 }
cfc9875a 445 return numberOfRunningTransactions;
ded57f02
JB
446 }
447
f479a792 448 public getConnectorIdByTransactionId(transactionId: number): number | undefined {
28e78158
JB
449 if (this.hasEvses) {
450 for (const evseStatus of this.evses.values()) {
451 for (const [connectorId, connectorStatus] of evseStatus.connectors) {
452 if (connectorStatus.transactionId === transactionId) {
453 return connectorId;
454 }
455 }
456 }
457 } else {
458 for (const connectorId of this.connectors.keys()) {
3fa7f799 459 if (this.getConnectorStatus(connectorId)?.transactionId === transactionId) {
28e78158
JB
460 return connectorId;
461 }
c0560973
JB
462 }
463 }
464 }
465
07989fad
JB
466 public getEnergyActiveImportRegisterByTransactionId(
467 transactionId: number,
5edd8ba0 468 rounded = false,
07989fad
JB
469 ): number {
470 return this.getEnergyActiveImportRegister(
e1d9a0f4 471 this.getConnectorStatus(this.getConnectorIdByTransactionId(transactionId)!)!,
5edd8ba0 472 rounded,
cbad1217 473 );
cbad1217
JB
474 }
475
18bf8274 476 public getEnergyActiveImportRegisterByConnectorId(connectorId: number, rounded = false): number {
e1d9a0f4 477 return this.getEnergyActiveImportRegister(this.getConnectorStatus(connectorId)!, rounded);
6ed92bc1
JB
478 }
479
c0560973 480 public getAuthorizeRemoteTxRequests(): boolean {
f2d5e3d9 481 const authorizeRemoteTxRequests = getConfigurationKey(
17ac262c 482 this,
5edd8ba0 483 StandardParametersKey.AuthorizeRemoteTxRequests,
e7aeea18 484 );
9bf0ef23 485 return authorizeRemoteTxRequests ? convertToBoolean(authorizeRemoteTxRequests.value) : false;
c0560973
JB
486 }
487
488 public getLocalAuthListEnabled(): boolean {
f2d5e3d9 489 const localAuthListEnabled = getConfigurationKey(
17ac262c 490 this,
5edd8ba0 491 StandardParametersKey.LocalAuthListEnabled,
e7aeea18 492 );
9bf0ef23 493 return localAuthListEnabled ? convertToBoolean(localAuthListEnabled.value) : false;
c0560973
JB
494 }
495
8f953431 496 public getHeartbeatInterval(): number {
f2d5e3d9 497 const HeartbeatInterval = getConfigurationKey(this, StandardParametersKey.HeartbeatInterval);
8f953431 498 if (HeartbeatInterval) {
be4c6702 499 return secondsToMilliseconds(convertToInt(HeartbeatInterval.value));
8f953431 500 }
f2d5e3d9 501 const HeartBeatInterval = getConfigurationKey(this, StandardParametersKey.HeartBeatInterval);
8f953431 502 if (HeartBeatInterval) {
be4c6702 503 return secondsToMilliseconds(convertToInt(HeartBeatInterval.value));
8f953431
JB
504 }
505 this.stationInfo?.autoRegister === false &&
506 logger.warn(
507 `${this.logPrefix()} Heartbeat interval configuration key not set, using default value: ${
508 Constants.DEFAULT_HEARTBEAT_INTERVAL
5edd8ba0 509 }`,
8f953431
JB
510 );
511 return Constants.DEFAULT_HEARTBEAT_INTERVAL;
512 }
513
269de583
JB
514 public setSupervisionUrl(url: string): void {
515 if (
3e888c65 516 this.stationInfo?.supervisionUrlOcppConfiguration === true &&
5398cecf 517 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey)
269de583 518 ) {
5398cecf 519 setConfigurationKeyValue(this, this.stationInfo.supervisionUrlOcppKey!, url);
269de583
JB
520 } else {
521 this.stationInfo.supervisionUrls = url;
522 this.saveStationInfo();
523 this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl();
524 }
525 }
526
c0560973 527 public startHeartbeat(): void {
5b43123f 528 if (this.getHeartbeatInterval() > 0 && !this.heartbeatSetInterval) {
6a8329b4
JB
529 this.heartbeatSetInterval = setInterval(() => {
530 this.ocppRequestService
531 .requestHandler<HeartbeatRequest, HeartbeatResponse>(this, RequestCommand.HEARTBEAT)
72092cfc 532 .catch((error) => {
6a8329b4
JB
533 logger.error(
534 `${this.logPrefix()} Error while sending '${RequestCommand.HEARTBEAT}':`,
5edd8ba0 535 error,
6a8329b4
JB
536 );
537 });
c0560973 538 }, this.getHeartbeatInterval());
e7aeea18 539 logger.info(
9bf0ef23 540 `${this.logPrefix()} Heartbeat started every ${formatDurationMilliSeconds(
5edd8ba0
JB
541 this.getHeartbeatInterval(),
542 )}`,
e7aeea18 543 );
c0560973 544 } else if (this.heartbeatSetInterval) {
e7aeea18 545 logger.info(
9bf0ef23 546 `${this.logPrefix()} Heartbeat already started every ${formatDurationMilliSeconds(
5edd8ba0
JB
547 this.getHeartbeatInterval(),
548 )}`,
e7aeea18 549 );
c0560973 550 } else {
e7aeea18 551 logger.error(
944d4529 552 `${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval()}, not starting the heartbeat`,
e7aeea18 553 );
c0560973
JB
554 }
555 }
556
557 public restartHeartbeat(): void {
558 // Stop heartbeat
559 this.stopHeartbeat();
560 // Start heartbeat
561 this.startHeartbeat();
562 }
563
17ac262c
JB
564 public restartWebSocketPing(): void {
565 // Stop WebSocket ping
566 this.stopWebSocketPing();
567 // Start WebSocket ping
568 this.startWebSocketPing();
569 }
570
c0560973
JB
571 public startMeterValues(connectorId: number, interval: number): void {
572 if (connectorId === 0) {
e7aeea18 573 logger.error(
04c32a95 574 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId}`,
e7aeea18 575 );
c0560973
JB
576 return;
577 }
734d790d 578 if (!this.getConnectorStatus(connectorId)) {
e7aeea18 579 logger.error(
66dd3447 580 `${this.logPrefix()} Trying to start MeterValues on non existing connector id
04c32a95 581 ${connectorId}`,
e7aeea18 582 );
c0560973
JB
583 return;
584 }
5e3cb728 585 if (this.getConnectorStatus(connectorId)?.transactionStarted === false) {
e7aeea18 586 logger.error(
944d4529 587 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction started`,
e7aeea18 588 );
c0560973 589 return;
e7aeea18 590 } else if (
5e3cb728 591 this.getConnectorStatus(connectorId)?.transactionStarted === true &&
9bf0ef23 592 isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionId)
e7aeea18
JB
593 ) {
594 logger.error(
944d4529 595 `${this.logPrefix()} Trying to start MeterValues on connector id ${connectorId} with no transaction id`,
e7aeea18 596 );
c0560973
JB
597 return;
598 }
599 if (interval > 0) {
e1d9a0f4 600 this.getConnectorStatus(connectorId)!.transactionSetInterval = setInterval(() => {
6a8329b4
JB
601 // FIXME: Implement OCPP version agnostic helpers
602 const meterValue: MeterValue = OCPP16ServiceUtils.buildMeterValue(
603 this,
604 connectorId,
e1d9a0f4 605 this.getConnectorStatus(connectorId)!.transactionId!,
5edd8ba0 606 interval,
6a8329b4
JB
607 );
608 this.ocppRequestService
609 .requestHandler<MeterValuesRequest, MeterValuesResponse>(
08f130a0 610 this,
f22266fd
JB
611 RequestCommand.METER_VALUES,
612 {
613 connectorId,
72092cfc 614 transactionId: this.getConnectorStatus(connectorId)?.transactionId,
f22266fd 615 meterValue: [meterValue],
5edd8ba0 616 },
6a8329b4 617 )
72092cfc 618 .catch((error) => {
6a8329b4
JB
619 logger.error(
620 `${this.logPrefix()} Error while sending '${RequestCommand.METER_VALUES}':`,
5edd8ba0 621 error,
6a8329b4
JB
622 );
623 });
624 }, interval);
c0560973 625 } else {
e7aeea18
JB
626 logger.error(
627 `${this.logPrefix()} Charging station ${
628 StandardParametersKey.MeterValueSampleInterval
5edd8ba0 629 } configuration set to ${interval}, not sending MeterValues`,
e7aeea18 630 );
c0560973
JB
631 }
632 }
633
04b1261c
JB
634 public stopMeterValues(connectorId: number) {
635 if (this.getConnectorStatus(connectorId)?.transactionSetInterval) {
636 clearInterval(this.getConnectorStatus(connectorId)?.transactionSetInterval);
637 }
638 }
639
c0560973 640 public start(): void {
0d8852a5
JB
641 if (this.started === false) {
642 if (this.starting === false) {
643 this.starting = true;
5398cecf 644 if (this.stationInfo?.enableStatistics === true) {
551e477c 645 this.performanceStatistics?.start();
0d8852a5 646 }
d8093be1 647 if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) {
178956d8 648 this.startReservationExpirationSetInterval();
d193a949 649 }
0d8852a5
JB
650 this.openWSConnection();
651 // Monitor charging station template file
1f8f6332
JB
652 this.templateFileWatcher = watchJsonFile(
653 this.templateFile,
654 FileType.ChargingStationTemplate,
655 this.logPrefix(),
656 undefined,
657 (event, filename): void => {
658 if (isNotEmptyString(filename) && event === 'change') {
659 try {
660 logger.debug(
661 `${this.logPrefix()} ${FileType.ChargingStationTemplate} ${
662 this.templateFile
663 } file have changed, reload`,
664 );
665 this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash);
666 // Initialize
667 this.initialize();
668 this.idTagsCache.deleteIdTags(getIdTagsFile(this.stationInfo)!);
669 // Restart the ATG
670 this.stopAutomaticTransactionGenerator();
671 delete this.automaticTransactionGeneratorConfiguration;
61854f7c 672 if (this.getAutomaticTransactionGeneratorConfiguration().enable === true) {
1f8f6332
JB
673 this.startAutomaticTransactionGenerator();
674 }
5398cecf 675 if (this.stationInfo?.enableStatistics === true) {
1f8f6332
JB
676 this.performanceStatistics?.restart();
677 } else {
678 this.performanceStatistics?.stop();
679 }
680 // FIXME?: restart heartbeat and WebSocket ping when their interval values have changed
681 } catch (error) {
682 logger.error(
683 `${this.logPrefix()} ${FileType.ChargingStationTemplate} file monitoring error:`,
684 error,
685 );
686 }
687 }
688 },
689 );
56eb297e 690 this.started = true;
db54d2e0 691 this.emit(ChargingStationEvents.started);
0d8852a5
JB
692 this.starting = false;
693 } else {
694 logger.warn(`${this.logPrefix()} Charging station is already starting...`);
695 }
950b1349 696 } else {
0d8852a5 697 logger.warn(`${this.logPrefix()} Charging station is already started...`);
950b1349 698 }
c0560973
JB
699 }
700
9ff486f4 701 public async stop(reason?: StopTransactionReason, stopTransactions?: boolean): Promise<void> {
0d8852a5
JB
702 if (this.started === true) {
703 if (this.stopping === false) {
704 this.stopping = true;
9ff486f4 705 await this.stopMessageSequence(reason, stopTransactions);
0d8852a5 706 this.closeWSConnection();
5398cecf 707 if (this.stationInfo?.enableStatistics === true) {
551e477c 708 this.performanceStatistics?.stop();
0d8852a5 709 }
d8093be1 710 if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) {
5543b88d
JB
711 this.stopReservationExpirationSetInterval();
712 }
0d8852a5 713 this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
72092cfc 714 this.templateFileWatcher?.close();
cda5d0fb 715 this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash);
cdde2cfe 716 delete this.bootNotificationResponse;
0d8852a5 717 this.started = false;
ac7f79af 718 this.saveConfiguration();
db54d2e0 719 this.emit(ChargingStationEvents.stopped);
0d8852a5
JB
720 this.stopping = false;
721 } else {
722 logger.warn(`${this.logPrefix()} Charging station is already stopping...`);
c0560973 723 }
950b1349 724 } else {
0d8852a5 725 logger.warn(`${this.logPrefix()} Charging station is already stopped...`);
c0560973 726 }
c0560973
JB
727 }
728
60ddad53
JB
729 public async reset(reason?: StopTransactionReason): Promise<void> {
730 await this.stop(reason);
e1d9a0f4 731 await sleep(this.stationInfo.resetTime!);
fa7bccf4 732 this.initialize();
94ec7e96
JB
733 this.start();
734 }
735
17ac262c 736 public saveOcppConfiguration(): void {
5398cecf 737 if (this.stationInfo?.ocppPersistentConfiguration === true) {
b1bbdae5 738 this.saveConfiguration();
e6895390
JB
739 }
740 }
741
8e242273
JB
742 public bufferMessage(message: string): void {
743 this.messageBuffer.add(message);
3ba2381e
JB
744 }
745
db2336d9 746 public openWSConnection(
7f3decca
JB
747 options?: WsOptions,
748 params?: { closeOpened?: boolean; terminateOpened?: boolean },
db2336d9 749 ): void {
7f3decca
JB
750 options = {
751 handshakeTimeout: secondsToMilliseconds(this.getConnectionTimeout()),
e6a33233 752 ...this.stationInfo?.wsOptions,
7f3decca
JB
753 ...options,
754 };
b1bbdae5 755 params = { ...{ closeOpened: false, terminateOpened: false }, ...params };
e6a33233 756 if (!checkChargingStation(this, this.logPrefix())) {
d1c6c833
JB
757 return;
758 }
db2336d9 759 if (
9bf0ef23
JB
760 !isNullOrUndefined(this.stationInfo.supervisionUser) &&
761 !isNullOrUndefined(this.stationInfo.supervisionPassword)
db2336d9
JB
762 ) {
763 options.auth = `${this.stationInfo.supervisionUser}:${this.stationInfo.supervisionPassword}`;
764 }
765 if (params?.closeOpened) {
766 this.closeWSConnection();
767 }
768 if (params?.terminateOpened) {
769 this.terminateWSConnection();
770 }
db2336d9 771
56eb297e 772 if (this.isWebSocketConnectionOpened() === true) {
0a03f36c 773 logger.warn(
944d4529 774 `${this.logPrefix()} OCPP connection to URL ${this.wsConnectionUrl.toString()} is already opened`,
0a03f36c
JB
775 );
776 return;
777 }
778
db2336d9 779 logger.info(
5edd8ba0 780 `${this.logPrefix()} Open OCPP connection to URL ${this.wsConnectionUrl.toString()}`,
db2336d9
JB
781 );
782
feff11ec
JB
783 this.wsConnection = new WebSocket(
784 this.wsConnectionUrl,
9a77cc07 785 `ocpp${this.stationInfo?.ocppVersion}`,
5edd8ba0 786 options,
feff11ec 787 );
db2336d9
JB
788
789 // Handle WebSocket message
790 this.wsConnection.on(
791 'message',
5edd8ba0 792 this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void,
db2336d9
JB
793 );
794 // Handle WebSocket error
795 this.wsConnection.on(
796 'error',
5edd8ba0 797 this.onError.bind(this) as (this: WebSocket, error: Error) => void,
db2336d9
JB
798 );
799 // Handle WebSocket close
800 this.wsConnection.on(
801 'close',
5edd8ba0 802 this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void,
db2336d9
JB
803 );
804 // Handle WebSocket open
805 this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void);
806 // Handle WebSocket ping
807 this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void);
808 // Handle WebSocket pong
809 this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void);
810 }
811
812 public closeWSConnection(): void {
56eb297e 813 if (this.isWebSocketConnectionOpened() === true) {
72092cfc 814 this.wsConnection?.close();
db2336d9
JB
815 this.wsConnection = null;
816 }
817 }
818
e1d9a0f4 819 public getAutomaticTransactionGeneratorConfiguration(): AutomaticTransactionGeneratorConfiguration {
c7db8ecb
JB
820 if (isNullOrUndefined(this.automaticTransactionGeneratorConfiguration)) {
821 let automaticTransactionGeneratorConfiguration:
822 | AutomaticTransactionGeneratorConfiguration
823 | undefined;
61854f7c
JB
824 const stationTemplate = this.getTemplateFromFile();
825 const stationConfiguration = this.getConfigurationFromFile();
c7db8ecb 826 if (
5398cecf 827 this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration === true &&
61854f7c
JB
828 stationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash &&
829 stationConfiguration?.automaticTransactionGenerator
c7db8ecb
JB
830 ) {
831 automaticTransactionGeneratorConfiguration =
61854f7c 832 stationConfiguration?.automaticTransactionGenerator;
c7db8ecb 833 } else {
61854f7c 834 automaticTransactionGeneratorConfiguration = stationTemplate?.AutomaticTransactionGenerator;
c7db8ecb
JB
835 }
836 this.automaticTransactionGeneratorConfiguration = {
837 ...Constants.DEFAULT_ATG_CONFIGURATION,
838 ...automaticTransactionGeneratorConfiguration,
839 };
ac7f79af 840 }
c7db8ecb 841 return this.automaticTransactionGeneratorConfiguration!;
ac7f79af
JB
842 }
843
5ced7e80
JB
844 public getAutomaticTransactionGeneratorStatuses(): Status[] | undefined {
845 return this.getConfigurationFromFile()?.automaticTransactionGeneratorStatuses;
846 }
847
ac7f79af
JB
848 public startAutomaticTransactionGenerator(connectorIds?: number[]): void {
849 this.automaticTransactionGenerator = AutomaticTransactionGenerator.getInstance(this);
9bf0ef23 850 if (isNotEmptyArray(connectorIds)) {
e1d9a0f4 851 for (const connectorId of connectorIds!) {
551e477c 852 this.automaticTransactionGenerator?.startConnector(connectorId);
a5e9befc
JB
853 }
854 } else {
551e477c 855 this.automaticTransactionGenerator?.start();
4f69be04 856 }
cb60061f 857 this.saveAutomaticTransactionGeneratorConfiguration();
db54d2e0 858 this.emit(ChargingStationEvents.updated);
4f69be04
JB
859 }
860
9ff486f4 861 public stopAutomaticTransactionGenerator(connectorIds?: number[]): void {
9bf0ef23 862 if (isNotEmptyArray(connectorIds)) {
e1d9a0f4 863 for (const connectorId of connectorIds!) {
9ff486f4 864 this.automaticTransactionGenerator?.stopConnector(connectorId);
a5e9befc
JB
865 }
866 } else {
9ff486f4 867 this.automaticTransactionGenerator?.stop();
4f69be04 868 }
cb60061f 869 this.saveAutomaticTransactionGeneratorConfiguration();
db54d2e0 870 this.emit(ChargingStationEvents.updated);
4f69be04
JB
871 }
872
5e3cb728
JB
873 public async stopTransactionOnConnector(
874 connectorId: number,
9ff486f4 875 reason?: StopTransactionReason,
5e3cb728 876 ): Promise<StopTransactionResponse> {
72092cfc 877 const transactionId = this.getConnectorStatus(connectorId)?.transactionId;
5e3cb728 878 if (
5398cecf
JB
879 this.stationInfo?.beginEndMeterValues === true &&
880 this.stationInfo?.ocppStrictCompliance === true &&
881 this.stationInfo?.outOfOrderEndMeterValues === false
5e3cb728
JB
882 ) {
883 // FIXME: Implement OCPP version agnostic helpers
884 const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
885 this,
886 connectorId,
e1d9a0f4 887 this.getEnergyActiveImportRegisterByTransactionId(transactionId!),
5e3cb728
JB
888 );
889 await this.ocppRequestService.requestHandler<MeterValuesRequest, MeterValuesResponse>(
890 this,
891 RequestCommand.METER_VALUES,
892 {
893 connectorId,
894 transactionId,
895 meterValue: [transactionEndMeterValue],
5edd8ba0 896 },
5e3cb728
JB
897 );
898 }
899 return this.ocppRequestService.requestHandler<StopTransactionRequest, StopTransactionResponse>(
900 this,
901 RequestCommand.STOP_TRANSACTION,
902 {
903 transactionId,
e1d9a0f4 904 meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId!, true),
9ff486f4 905 ...(isNullOrUndefined(reason) && { reason }),
5edd8ba0 906 },
5e3cb728
JB
907 );
908 }
909
10e8c3e1 910 public getReserveConnectorZeroSupported(): boolean {
9bf0ef23 911 return convertToBoolean(
f2d5e3d9 912 getConfigurationKey(this, StandardParametersKey.ReserveConnectorZeroSupported)!.value,
24578c31
JB
913 );
914 }
915
d193a949 916 public async addReservation(reservation: Reservation): Promise<void> {
2ca0ea90 917 const reservationFound = this.getReservationBy('reservationId', reservation.reservationId);
530e5fbb
JB
918 if (!isUndefined(reservationFound)) {
919 await this.removeReservation(
920 reservationFound!,
921 ReservationTerminationReason.REPLACE_EXISTING,
922 );
d193a949 923 }
e1d9a0f4 924 this.getConnectorStatus(reservation.connectorId)!.reservation = reservation;
ec94a3cf 925 await OCPPServiceUtils.sendAndSetConnectorStatus(
d193a949 926 this,
ec94a3cf
JB
927 reservation.connectorId,
928 ConnectorStatusEnum.Reserved,
e1d9a0f4 929 undefined,
5edd8ba0 930 { send: reservation.connectorId !== 0 },
d193a949 931 );
24578c31
JB
932 }
933
d193a949
JB
934 public async removeReservation(
935 reservation: Reservation,
90aceaf6 936 reason: ReservationTerminationReason,
d193a949 937 ): Promise<void> {
e1d9a0f4 938 const connector = this.getConnectorStatus(reservation.connectorId)!;
d193a949 939 switch (reason) {
96d96b12 940 case ReservationTerminationReason.CONNECTOR_STATE_CHANGED:
ec94a3cf 941 case ReservationTerminationReason.TRANSACTION_STARTED:
ec9f36cc
JB
942 delete connector.reservation;
943 break;
e74bc549
JB
944 case ReservationTerminationReason.RESERVATION_CANCELED:
945 case ReservationTerminationReason.REPLACE_EXISTING:
946 case ReservationTerminationReason.EXPIRED:
ec94a3cf 947 await OCPPServiceUtils.sendAndSetConnectorStatus(
d193a949 948 this,
ec94a3cf
JB
949 reservation.connectorId,
950 ConnectorStatusEnum.Available,
e1d9a0f4 951 undefined,
5edd8ba0 952 { send: reservation.connectorId !== 0 },
d193a949 953 );
ec94a3cf 954 delete connector.reservation;
d193a949 955 break;
b029e74e 956 default:
90aceaf6 957 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
56563a3c 958 throw new BaseError(`Unknown reservation termination reason '${reason}'`);
d193a949 959 }
24578c31
JB
960 }
961
3fa7f799 962 public getReservationBy(
366f75f6 963 filterKey: ReservationKey,
5edd8ba0 964 value: number | string,
3fa7f799 965 ): Reservation | undefined {
66dd3447 966 if (this.hasEvses) {
3fa7f799
JB
967 for (const evseStatus of this.evses.values()) {
968 for (const connectorStatus of evseStatus.connectors.values()) {
2ca0ea90 969 if (connectorStatus?.reservation?.[filterKey] === value) {
3fa7f799 970 return connectorStatus.reservation;
66dd3447
JB
971 }
972 }
973 }
974 } else {
3fa7f799 975 for (const connectorStatus of this.connectors.values()) {
2ca0ea90 976 if (connectorStatus?.reservation?.[filterKey] === value) {
3fa7f799 977 return connectorStatus.reservation;
66dd3447
JB
978 }
979 }
980 }
d193a949
JB
981 }
982
e6948a57
JB
983 public isConnectorReservable(
984 reservationId: number,
985 idTag?: string,
986 connectorId?: number,
987 ): boolean {
90aceaf6
JB
988 const reservation = this.getReservationBy('reservationId', reservationId);
989 const reservationExists = !isUndefined(reservation) && !hasReservationExpired(reservation!);
e6948a57
JB
990 if (arguments.length === 1) {
991 return !reservationExists;
992 } else if (arguments.length > 1) {
90aceaf6
JB
993 const userReservation = !isUndefined(idTag)
994 ? this.getReservationBy('idTag', idTag!)
995 : undefined;
e6948a57 996 const userReservationExists =
90aceaf6 997 !isUndefined(userReservation) && !hasReservationExpired(userReservation!);
e6948a57
JB
998 const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0;
999 const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0;
1000 return (
1001 !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable
1002 );
1003 }
1004 return false;
1005 }
1006
1007 private startReservationExpirationSetInterval(customInterval?: number): void {
2035255d 1008 const interval = customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_INTERVAL;
42371a2e 1009 if (interval > 0) {
04c32a95
JB
1010 logger.info(
1011 `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds(
1012 interval,
1013 )}`,
1014 );
37aa4e56 1015 this.reservationExpirationSetInterval = setInterval((): void => {
90aceaf6 1016 removeExpiredReservations(this).catch(Constants.EMPTY_FUNCTION);
42371a2e
JB
1017 }, interval);
1018 }
d193a949
JB
1019 }
1020
e6948a57 1021 private stopReservationExpirationSetInterval(): void {
d116b9b5 1022 if (!isNullOrUndefined(this.reservationExpirationSetInterval)) {
e6948a57
JB
1023 clearInterval(this.reservationExpirationSetInterval);
1024 }
d193a949
JB
1025 }
1026
366f75f6
JB
1027 // private restartReservationExpiryDateSetInterval(): void {
1028 // this.stopReservationExpirationSetInterval();
1029 // this.startReservationExpirationSetInterval();
1030 // }
d193a949
JB
1031
1032 private getNumberOfReservableConnectors(): number {
90aceaf6 1033 let numberOfReservableConnectors = 0;
66dd3447 1034 if (this.hasEvses) {
3fa7f799 1035 for (const evseStatus of this.evses.values()) {
90aceaf6 1036 numberOfReservableConnectors += getNumberOfReservableConnectors(evseStatus.connectors);
66dd3447
JB
1037 }
1038 } else {
90aceaf6 1039 numberOfReservableConnectors = getNumberOfReservableConnectors(this.connectors);
66dd3447 1040 }
90aceaf6 1041 return numberOfReservableConnectors - this.getNumberOfReservationsOnConnectorZero();
66dd3447
JB
1042 }
1043
d193a949 1044 private getNumberOfReservationsOnConnectorZero(): number {
6913d568
JB
1045 if (
1046 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
1047 (this.hasEvses && this.evses.get(0)?.connectors.get(0)?.reservation) ||
1048 (!this.hasEvses && this.connectors.get(0)?.reservation)
1049 ) {
cfc9875a 1050 return 1;
66dd3447 1051 }
cfc9875a 1052 return 0;
24578c31
JB
1053 }
1054
f90c1757 1055 private flushMessageBuffer(): void {
8e242273 1056 if (this.messageBuffer.size > 0) {
7d3b0f64 1057 for (const message of this.messageBuffer.values()) {
e1d9a0f4
JB
1058 let beginId: string | undefined;
1059 let commandName: RequestCommand | undefined;
8ca6874c 1060 const [messageType] = JSON.parse(message) as OutgoingRequest | Response | ErrorResponse;
1431af78
JB
1061 const isRequest = messageType === MessageType.CALL_MESSAGE;
1062 if (isRequest) {
1063 [, , commandName] = JSON.parse(message) as OutgoingRequest;
1064 beginId = PerformanceStatistics.beginMeasure(commandName);
1065 }
72092cfc 1066 this.wsConnection?.send(message);
e1d9a0f4 1067 isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!);
8ca6874c
JB
1068 logger.debug(
1069 `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
5edd8ba0
JB
1070 messageType,
1071 )} payload sent: ${message}`,
8ca6874c 1072 );
8e242273 1073 this.messageBuffer.delete(message);
7d3b0f64 1074 }
77f00f84
JB
1075 }
1076 }
1077
72092cfc 1078 private getTemplateFromFile(): ChargingStationTemplate | undefined {
e1d9a0f4 1079 let template: ChargingStationTemplate | undefined;
5ad8570f 1080 try {
cda5d0fb
JB
1081 if (this.sharedLRUCache.hasChargingStationTemplate(this.templateFileHash)) {
1082 template = this.sharedLRUCache.getChargingStationTemplate(this.templateFileHash);
7c72977b
JB
1083 } else {
1084 const measureId = `${FileType.ChargingStationTemplate} read`;
1085 const beginId = PerformanceStatistics.beginMeasure(measureId);
d972af76 1086 template = JSON.parse(readFileSync(this.templateFile, 'utf8')) as ChargingStationTemplate;
7c72977b 1087 PerformanceStatistics.endMeasure(measureId, beginId);
d972af76 1088 template.templateHash = createHash(Constants.DEFAULT_HASH_ALGORITHM)
7c72977b
JB
1089 .update(JSON.stringify(template))
1090 .digest('hex');
57adbebc 1091 this.sharedLRUCache.setChargingStationTemplate(template);
cda5d0fb 1092 this.templateFileHash = template.templateHash;
7c72977b 1093 }
5ad8570f 1094 } catch (error) {
fa5995d6 1095 handleFileException(
2484ac1e 1096 this.templateFile,
7164966d
JB
1097 FileType.ChargingStationTemplate,
1098 error as NodeJS.ErrnoException,
5edd8ba0 1099 this.logPrefix(),
e7aeea18 1100 );
5ad8570f 1101 }
2484ac1e
JB
1102 return template;
1103 }
1104
7a3a2ebb 1105 private getStationInfoFromTemplate(): ChargingStationInfo {
e1d9a0f4 1106 const stationTemplate: ChargingStationTemplate = this.getTemplateFromFile()!;
fba11dc6 1107 checkTemplate(stationTemplate, this.logPrefix(), this.templateFile);
5f742aac
JB
1108 const warnTemplateKeysDeprecationOnce = once(warnTemplateKeysDeprecation, this);
1109 warnTemplateKeysDeprecationOnce(stationTemplate, this.logPrefix(), this.templateFile);
8a133cc8 1110 if (stationTemplate?.Connectors) {
fba11dc6 1111 checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile);
8a133cc8 1112 }
fba11dc6
JB
1113 const stationInfo: ChargingStationInfo = stationTemplateToStationInfo(stationTemplate);
1114 stationInfo.hashId = getHashId(this.index, stationTemplate);
1115 stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate);
72092cfc 1116 stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16;
fba11dc6 1117 createSerialNumber(stationTemplate, stationInfo);
9bf0ef23 1118 if (isNotEmptyArray(stationTemplate?.power)) {
551e477c 1119 stationTemplate.power = stationTemplate.power as number[];
9bf0ef23 1120 const powerArrayRandomIndex = Math.floor(secureRandom() * stationTemplate.power.length);
cc6e8ab5 1121 stationInfo.maximumPower =
72092cfc 1122 stationTemplate?.powerUnit === PowerUnits.KILO_WATT
fa7bccf4
JB
1123 ? stationTemplate.power[powerArrayRandomIndex] * 1000
1124 : stationTemplate.power[powerArrayRandomIndex];
5ad8570f 1125 } else {
551e477c 1126 stationTemplate.power = stationTemplate?.power as number;
cc6e8ab5 1127 stationInfo.maximumPower =
72092cfc 1128 stationTemplate?.powerUnit === PowerUnits.KILO_WATT
fa7bccf4
JB
1129 ? stationTemplate.power * 1000
1130 : stationTemplate.power;
1131 }
73de8f02 1132 stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo);
3637ca2c 1133 stationInfo.firmwareVersionPattern =
72092cfc 1134 stationTemplate?.firmwareVersionPattern ?? Constants.SEMVER_PATTERN;
3637ca2c 1135 if (
9bf0ef23 1136 isNotEmptyString(stationInfo.firmwareVersion) &&
e1d9a0f4 1137 new RegExp(stationInfo.firmwareVersionPattern).test(stationInfo.firmwareVersion!) === false
3637ca2c
JB
1138 ) {
1139 logger.warn(
1140 `${this.logPrefix()} Firmware version '${stationInfo.firmwareVersion}' in template file ${
1141 this.templateFile
5edd8ba0 1142 } does not match firmware version pattern '${stationInfo.firmwareVersionPattern}'`,
3637ca2c
JB
1143 );
1144 }
598c886d 1145 stationInfo.firmwareUpgrade = merge<FirmwareUpgrade>(
15748260 1146 {
598c886d
JB
1147 versionUpgrade: {
1148 step: 1,
1149 },
15748260
JB
1150 reset: true,
1151 },
5edd8ba0 1152 stationTemplate?.firmwareUpgrade ?? {},
15748260 1153 );
9bf0ef23 1154 stationInfo.resetTime = !isNullOrUndefined(stationTemplate?.resetTime)
be4c6702 1155 ? secondsToMilliseconds(stationTemplate.resetTime!)
e7aeea18 1156 : Constants.CHARGING_STATION_DEFAULT_RESET_TIME;
9ac86a7e 1157 return stationInfo;
5ad8570f
JB
1158 }
1159
551e477c
JB
1160 private getStationInfoFromFile(): ChargingStationInfo | undefined {
1161 let stationInfo: ChargingStationInfo | undefined;
5398cecf 1162 if (this.stationInfo?.stationInfoPersistentConfiguration === true) {
f832e5df
JB
1163 stationInfo = this.getConfigurationFromFile()?.stationInfo;
1164 if (stationInfo) {
1165 delete stationInfo?.infoHash;
1166 }
1167 }
f765beaa 1168 return stationInfo;
2484ac1e
JB
1169 }
1170
1171 private getStationInfo(): ChargingStationInfo {
1172 const stationInfoFromTemplate: ChargingStationInfo = this.getStationInfoFromTemplate();
551e477c 1173 const stationInfoFromFile: ChargingStationInfo | undefined = this.getStationInfoFromFile();
6b90dcca
JB
1174 // Priority:
1175 // 1. charging station info from template
1176 // 2. charging station info from configuration file
f765beaa 1177 if (stationInfoFromFile?.templateHash === stationInfoFromTemplate.templateHash) {
e1d9a0f4 1178 return stationInfoFromFile!;
f765beaa 1179 }
fec4d204 1180 stationInfoFromFile &&
fba11dc6 1181 propagateSerialNumber(
e1d9a0f4 1182 this.getTemplateFromFile()!,
fec4d204 1183 stationInfoFromFile,
5edd8ba0 1184 stationInfoFromTemplate,
fec4d204 1185 );
01efc60a 1186 return stationInfoFromTemplate;
2484ac1e
JB
1187 }
1188
1189 private saveStationInfo(): void {
5398cecf 1190 if (this.stationInfo?.stationInfoPersistentConfiguration === true) {
b1bbdae5 1191 this.saveConfiguration();
ccb1d6e9 1192 }
2484ac1e
JB
1193 }
1194
3e888c65 1195 private handleUnsupportedVersion(version: OCPPVersion | undefined) {
944d4529 1196 const errorMsg = `Unsupported protocol version '${version}' configured in template file ${this.templateFile}`;
ded57f02
JB
1197 logger.error(`${this.logPrefix()} ${errorMsg}`);
1198 throw new BaseError(errorMsg);
c0560973
JB
1199 }
1200
2484ac1e 1201 private initialize(): void {
e1d9a0f4 1202 const stationTemplate = this.getTemplateFromFile()!;
fba11dc6 1203 checkTemplate(stationTemplate, this.logPrefix(), this.templateFile);
d972af76
JB
1204 this.configurationFile = join(
1205 dirname(this.templateFile.replace('station-templates', 'configurations')),
5edd8ba0 1206 `${getHashId(this.index, stationTemplate)}.json`,
0642c3d2 1207 );
61854f7c 1208 const stationConfiguration = this.getConfigurationFromFile();
a4f7c75f 1209 if (
61854f7c 1210 stationConfiguration?.stationInfo?.templateHash === stationTemplate?.templateHash &&
e1d9a0f4 1211 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
61854f7c 1212 (stationConfiguration?.connectorsStatus || stationConfiguration?.evsesStatus)
a4f7c75f 1213 ) {
61854f7c 1214 this.initializeConnectorsOrEvsesFromFile(stationConfiguration);
a4f7c75f
JB
1215 } else {
1216 this.initializeConnectorsOrEvsesFromTemplate(stationTemplate);
1217 }
5398cecf 1218 this.internalStationInfo = this.getStationInfo();
3637ca2c
JB
1219 if (
1220 this.stationInfo.firmwareStatus === FirmwareStatus.Installing &&
9bf0ef23
JB
1221 isNotEmptyString(this.stationInfo.firmwareVersion) &&
1222 isNotEmptyString(this.stationInfo.firmwareVersionPattern)
3637ca2c 1223 ) {
d812bdcb 1224 const patternGroup: number | undefined =
15748260 1225 this.stationInfo.firmwareUpgrade?.versionUpgrade?.patternGroup ??
d812bdcb 1226 this.stationInfo.firmwareVersion?.split('.').length;
e1d9a0f4
JB
1227 const match = this.stationInfo
1228 .firmwareVersion!.match(new RegExp(this.stationInfo.firmwareVersionPattern!))!
1229 .slice(1, patternGroup! + 1);
3637ca2c 1230 const patchLevelIndex = match.length - 1;
5d280aae 1231 match[patchLevelIndex] = (
9bf0ef23 1232 convertToInt(match[patchLevelIndex]) +
e1d9a0f4 1233 this.stationInfo.firmwareUpgrade!.versionUpgrade!.step!
5d280aae 1234 ).toString();
72092cfc 1235 this.stationInfo.firmwareVersion = match?.join('.');
3637ca2c 1236 }
6bccfcbc 1237 this.saveStationInfo();
6bccfcbc 1238 this.configuredSupervisionUrl = this.getConfiguredSupervisionUrl();
5398cecf 1239 if (this.stationInfo?.enableStatistics === true) {
6bccfcbc
JB
1240 this.performanceStatistics = PerformanceStatistics.getInstance(
1241 this.stationInfo.hashId,
e1d9a0f4 1242 this.stationInfo.chargingStationId!,
5edd8ba0 1243 this.configuredSupervisionUrl,
6bccfcbc
JB
1244 );
1245 }
fba11dc6 1246 this.bootNotificationRequest = createBootNotificationRequest(this.stationInfo);
692f2f64
JB
1247 this.powerDivider = this.getPowerDivider();
1248 // OCPP configuration
1249 this.ocppConfiguration = this.getOcppConfiguration();
1250 this.initializeOcppConfiguration();
1251 this.initializeOcppServices();
3e888c65
JB
1252 this.once(ChargingStationEvents.accepted, () => {
1253 this.startMessageSequence().catch((error) => {
1254 logger.error(`${this.logPrefix()} Error while starting the message sequence:`, error);
1255 });
1256 });
692f2f64
JB
1257 if (this.stationInfo?.autoRegister === true) {
1258 this.bootNotificationResponse = {
1259 currentTime: new Date(),
be4c6702 1260 interval: millisecondsToSeconds(this.getHeartbeatInterval()),
692f2f64
JB
1261 status: RegistrationStatusEnumType.ACCEPTED,
1262 };
1263 }
147d0e0f
JB
1264 }
1265
feff11ec 1266 private initializeOcppServices(): void {
9a77cc07 1267 const ocppVersion = this.stationInfo?.ocppVersion;
feff11ec
JB
1268 switch (ocppVersion) {
1269 case OCPPVersion.VERSION_16:
1270 this.ocppIncomingRequestService =
1271 OCPP16IncomingRequestService.getInstance<OCPP16IncomingRequestService>();
1272 this.ocppRequestService = OCPP16RequestService.getInstance<OCPP16RequestService>(
5edd8ba0 1273 OCPP16ResponseService.getInstance<OCPP16ResponseService>(),
feff11ec
JB
1274 );
1275 break;
1276 case OCPPVersion.VERSION_20:
1277 case OCPPVersion.VERSION_201:
1278 this.ocppIncomingRequestService =
1279 OCPP20IncomingRequestService.getInstance<OCPP20IncomingRequestService>();
1280 this.ocppRequestService = OCPP20RequestService.getInstance<OCPP20RequestService>(
5edd8ba0 1281 OCPP20ResponseService.getInstance<OCPP20ResponseService>(),
feff11ec
JB
1282 );
1283 break;
1284 default:
1285 this.handleUnsupportedVersion(ocppVersion);
1286 break;
1287 }
1288 }
1289
2484ac1e 1290 private initializeOcppConfiguration(): void {
f2d5e3d9
JB
1291 if (!getConfigurationKey(this, StandardParametersKey.HeartbeatInterval)) {
1292 addConfigurationKey(this, StandardParametersKey.HeartbeatInterval, '0');
f0f65a62 1293 }
f2d5e3d9
JB
1294 if (!getConfigurationKey(this, StandardParametersKey.HeartBeatInterval)) {
1295 addConfigurationKey(this, StandardParametersKey.HeartBeatInterval, '0', { visible: false });
f0f65a62 1296 }
e7aeea18 1297 if (
5398cecf
JB
1298 this.stationInfo?.supervisionUrlOcppConfiguration &&
1299 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
1300 !getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)
e7aeea18 1301 ) {
f2d5e3d9 1302 addConfigurationKey(
17ac262c 1303 this,
5398cecf 1304 this.stationInfo.supervisionUrlOcppKey!,
fa7bccf4 1305 this.configuredSupervisionUrl.href,
5edd8ba0 1306 { reboot: true },
e7aeea18 1307 );
e6895390 1308 } else if (
5398cecf
JB
1309 !this.stationInfo?.supervisionUrlOcppConfiguration &&
1310 isNotEmptyString(this.stationInfo?.supervisionUrlOcppKey) &&
1311 getConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!)
e6895390 1312 ) {
5398cecf 1313 deleteConfigurationKey(this, this.stationInfo.supervisionUrlOcppKey!, { save: false });
12fc74d6 1314 }
cc6e8ab5 1315 if (
9bf0ef23 1316 isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) &&
f2d5e3d9 1317 !getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!)
cc6e8ab5 1318 ) {
f2d5e3d9 1319 addConfigurationKey(
17ac262c 1320 this,
e1d9a0f4 1321 this.stationInfo.amperageLimitationOcppKey!,
17ac262c 1322 (
e1d9a0f4 1323 this.stationInfo.maximumAmperage! * getAmperageLimitationUnitDivider(this.stationInfo)
5edd8ba0 1324 ).toString(),
cc6e8ab5
JB
1325 );
1326 }
f2d5e3d9
JB
1327 if (!getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)) {
1328 addConfigurationKey(
17ac262c 1329 this,
e7aeea18 1330 StandardParametersKey.SupportedFeatureProfiles,
5edd8ba0 1331 `${SupportedFeatureProfiles.Core},${SupportedFeatureProfiles.FirmwareManagement},${SupportedFeatureProfiles.LocalAuthListManagement},${SupportedFeatureProfiles.SmartCharging},${SupportedFeatureProfiles.RemoteTrigger}`,
e7aeea18
JB
1332 );
1333 }
f2d5e3d9 1334 addConfigurationKey(
17ac262c 1335 this,
e7aeea18
JB
1336 StandardParametersKey.NumberOfConnectors,
1337 this.getNumberOfConnectors().toString(),
a95873d8 1338 { readonly: true },
5edd8ba0 1339 { overwrite: true },
e7aeea18 1340 );
f2d5e3d9
JB
1341 if (!getConfigurationKey(this, StandardParametersKey.MeterValuesSampledData)) {
1342 addConfigurationKey(
17ac262c 1343 this,
e7aeea18 1344 StandardParametersKey.MeterValuesSampledData,
5edd8ba0 1345 MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER,
e7aeea18 1346 );
7abfea5f 1347 }
f2d5e3d9 1348 if (!getConfigurationKey(this, StandardParametersKey.ConnectorPhaseRotation)) {
dd08d43d 1349 const connectorsPhaseRotation: string[] = [];
28e78158
JB
1350 if (this.hasEvses) {
1351 for (const evseStatus of this.evses.values()) {
1352 for (const connectorId of evseStatus.connectors.keys()) {
dd08d43d 1353 connectorsPhaseRotation.push(
e1d9a0f4 1354 getPhaseRotationValue(connectorId, this.getNumberOfPhases())!,
dd08d43d 1355 );
28e78158
JB
1356 }
1357 }
1358 } else {
1359 for (const connectorId of this.connectors.keys()) {
dd08d43d 1360 connectorsPhaseRotation.push(
e1d9a0f4 1361 getPhaseRotationValue(connectorId, this.getNumberOfPhases())!,
dd08d43d 1362 );
7e1dc878
JB
1363 }
1364 }
f2d5e3d9 1365 addConfigurationKey(
17ac262c 1366 this,
e7aeea18 1367 StandardParametersKey.ConnectorPhaseRotation,
5edd8ba0 1368 connectorsPhaseRotation.toString(),
e7aeea18 1369 );
7e1dc878 1370 }
f2d5e3d9
JB
1371 if (!getConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests)) {
1372 addConfigurationKey(this, StandardParametersKey.AuthorizeRemoteTxRequests, 'true');
36f6a92e 1373 }
17ac262c 1374 if (
f2d5e3d9
JB
1375 !getConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled) &&
1376 getConfigurationKey(this, StandardParametersKey.SupportedFeatureProfiles)?.value?.includes(
1377 SupportedFeatureProfiles.LocalAuthListManagement,
17ac262c
JB
1378 )
1379 ) {
f2d5e3d9
JB
1380 addConfigurationKey(this, StandardParametersKey.LocalAuthListEnabled, 'false');
1381 }
1382 if (!getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)) {
1383 addConfigurationKey(
17ac262c 1384 this,
e7aeea18 1385 StandardParametersKey.ConnectionTimeOut,
5edd8ba0 1386 Constants.DEFAULT_CONNECTION_TIMEOUT.toString(),
e7aeea18 1387 );
8bce55bf 1388 }
2484ac1e 1389 this.saveOcppConfiguration();
073bd098
JB
1390 }
1391
a4f7c75f
JB
1392 private initializeConnectorsOrEvsesFromFile(configuration: ChargingStationConfiguration): void {
1393 if (configuration?.connectorsStatus && !configuration?.evsesStatus) {
8df5ae48 1394 for (const [connectorId, connectorStatus] of configuration.connectorsStatus.entries()) {
9bf0ef23 1395 this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus));
8df5ae48 1396 }
a4f7c75f
JB
1397 } else if (configuration?.evsesStatus && !configuration?.connectorsStatus) {
1398 for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) {
9bf0ef23 1399 const evseStatus = cloneObject<EvseStatusConfiguration>(evseStatusConfiguration);
a4f7c75f
JB
1400 delete evseStatus.connectorsStatus;
1401 this.evses.set(evseId, {
8df5ae48 1402 ...(evseStatus as EvseStatus),
a4f7c75f 1403 connectors: new Map<number, ConnectorStatus>(
e1d9a0f4 1404 evseStatusConfiguration.connectorsStatus!.map((connectorStatus, connectorId) => [
a4f7c75f
JB
1405 connectorId,
1406 connectorStatus,
5edd8ba0 1407 ]),
a4f7c75f
JB
1408 ),
1409 });
1410 }
1411 } else if (configuration?.evsesStatus && configuration?.connectorsStatus) {
1412 const errorMsg = `Connectors and evses defined at the same time in configuration file ${this.configurationFile}`;
1413 logger.error(`${this.logPrefix()} ${errorMsg}`);
1414 throw new BaseError(errorMsg);
1415 } else {
1416 const errorMsg = `No connectors or evses defined in configuration file ${this.configurationFile}`;
1417 logger.error(`${this.logPrefix()} ${errorMsg}`);
1418 throw new BaseError(errorMsg);
1419 }
1420 }
1421
34eeb1fb 1422 private initializeConnectorsOrEvsesFromTemplate(stationTemplate: ChargingStationTemplate) {
cda5d0fb 1423 if (stationTemplate?.Connectors && !stationTemplate?.Evses) {
34eeb1fb 1424 this.initializeConnectorsFromTemplate(stationTemplate);
cda5d0fb 1425 } else if (stationTemplate?.Evses && !stationTemplate?.Connectors) {
34eeb1fb 1426 this.initializeEvsesFromTemplate(stationTemplate);
cda5d0fb 1427 } else if (stationTemplate?.Evses && stationTemplate?.Connectors) {
ae25f265
JB
1428 const errorMsg = `Connectors and evses defined at the same time in template file ${this.templateFile}`;
1429 logger.error(`${this.logPrefix()} ${errorMsg}`);
1430 throw new BaseError(errorMsg);
1431 } else {
1432 const errorMsg = `No connectors or evses defined in template file ${this.templateFile}`;
1433 logger.error(`${this.logPrefix()} ${errorMsg}`);
1434 throw new BaseError(errorMsg);
1435 }
1436 }
1437
34eeb1fb 1438 private initializeConnectorsFromTemplate(stationTemplate: ChargingStationTemplate): void {
cda5d0fb 1439 if (!stationTemplate?.Connectors && this.connectors.size === 0) {
ded57f02
JB
1440 const errorMsg = `No already defined connectors and charging station information from template ${this.templateFile} with no connectors configuration defined`;
1441 logger.error(`${this.logPrefix()} ${errorMsg}`);
1442 throw new BaseError(errorMsg);
3d25cc86 1443 }
e1d9a0f4 1444 if (!stationTemplate?.Connectors?.[0]) {
3d25cc86
JB
1445 logger.warn(
1446 `${this.logPrefix()} Charging station information from template ${
1447 this.templateFile
5edd8ba0 1448 } with no connector id 0 configuration`,
3d25cc86
JB
1449 );
1450 }
cda5d0fb
JB
1451 if (stationTemplate?.Connectors) {
1452 const { configuredMaxConnectors, templateMaxConnectors, templateMaxAvailableConnectors } =
fba11dc6 1453 checkConnectorsConfiguration(stationTemplate, this.logPrefix(), this.templateFile);
d972af76 1454 const connectorsConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM)
cda5d0fb 1455 .update(
5edd8ba0 1456 `${JSON.stringify(stationTemplate?.Connectors)}${configuredMaxConnectors.toString()}`,
cda5d0fb 1457 )
3d25cc86
JB
1458 .digest('hex');
1459 const connectorsConfigChanged =
1460 this.connectors?.size !== 0 && this.connectorsConfigurationHash !== connectorsConfigHash;
1461 if (this.connectors?.size === 0 || connectorsConfigChanged) {
1462 connectorsConfigChanged && this.connectors.clear();
1463 this.connectorsConfigurationHash = connectorsConfigHash;
269196a8
JB
1464 if (templateMaxConnectors > 0) {
1465 for (let connectorId = 0; connectorId <= configuredMaxConnectors; connectorId++) {
1466 if (
1467 connectorId === 0 &&
1c9de2b9 1468 (!stationTemplate?.Connectors?.[connectorId] ||
cda5d0fb 1469 this.getUseConnectorId0(stationTemplate) === false)
269196a8
JB
1470 ) {
1471 continue;
1472 }
1473 const templateConnectorId =
cda5d0fb 1474 connectorId > 0 && stationTemplate?.randomConnectors
9bf0ef23 1475 ? getRandomInteger(templateMaxAvailableConnectors, 1)
269196a8 1476 : connectorId;
cda5d0fb 1477 const connectorStatus = stationTemplate?.Connectors[templateConnectorId];
fba11dc6 1478 checkStationInfoConnectorStatus(
ae25f265 1479 templateConnectorId,
04b1261c
JB
1480 connectorStatus,
1481 this.logPrefix(),
5edd8ba0 1482 this.templateFile,
04b1261c 1483 );
9bf0ef23 1484 this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus));
3d25cc86 1485 }
fba11dc6 1486 initializeConnectorsMapStatus(this.connectors, this.logPrefix());
52952bf8 1487 this.saveConnectorsStatus();
ae25f265
JB
1488 } else {
1489 logger.warn(
1490 `${this.logPrefix()} Charging station information from template ${
1491 this.templateFile
5edd8ba0 1492 } with no connectors configuration defined, cannot create connectors`,
ae25f265 1493 );
3d25cc86
JB
1494 }
1495 }
1496 } else {
1497 logger.warn(
1498 `${this.logPrefix()} Charging station information from template ${
1499 this.templateFile
5edd8ba0 1500 } with no connectors configuration defined, using already defined connectors`,
3d25cc86
JB
1501 );
1502 }
3d25cc86
JB
1503 }
1504
34eeb1fb 1505 private initializeEvsesFromTemplate(stationTemplate: ChargingStationTemplate): void {
cda5d0fb 1506 if (!stationTemplate?.Evses && this.evses.size === 0) {
ded57f02
JB
1507 const errorMsg = `No already defined evses and charging station information from template ${this.templateFile} with no evses configuration defined`;
1508 logger.error(`${this.logPrefix()} ${errorMsg}`);
1509 throw new BaseError(errorMsg);
2585c6e9 1510 }
e1d9a0f4 1511 if (!stationTemplate?.Evses?.[0]) {
2585c6e9
JB
1512 logger.warn(
1513 `${this.logPrefix()} Charging station information from template ${
1514 this.templateFile
5edd8ba0 1515 } with no evse id 0 configuration`,
2585c6e9
JB
1516 );
1517 }
e1d9a0f4 1518 if (!stationTemplate?.Evses?.[0]?.Connectors?.[0]) {
59a0f26d
JB
1519 logger.warn(
1520 `${this.logPrefix()} Charging station information from template ${
1521 this.templateFile
5edd8ba0 1522 } with evse id 0 with no connector id 0 configuration`,
59a0f26d
JB
1523 );
1524 }
491dad29
JB
1525 if (Object.keys(stationTemplate?.Evses?.[0]?.Connectors as object).length > 1) {
1526 logger.warn(
1527 `${this.logPrefix()} Charging station information from template ${
1528 this.templateFile
1529 } with evse id 0 with more than one connector configuration, only connector id 0 configuration will be used`,
1530 );
1531 }
cda5d0fb 1532 if (stationTemplate?.Evses) {
d972af76 1533 const evsesConfigHash = createHash(Constants.DEFAULT_HASH_ALGORITHM)
ba01a213 1534 .update(JSON.stringify(stationTemplate?.Evses))
2585c6e9
JB
1535 .digest('hex');
1536 const evsesConfigChanged =
1537 this.evses?.size !== 0 && this.evsesConfigurationHash !== evsesConfigHash;
1538 if (this.evses?.size === 0 || evsesConfigChanged) {
1539 evsesConfigChanged && this.evses.clear();
1540 this.evsesConfigurationHash = evsesConfigHash;
fba11dc6 1541 const templateMaxEvses = getMaxNumberOfEvses(stationTemplate?.Evses);
ae25f265 1542 if (templateMaxEvses > 0) {
eb979012
JB
1543 for (const evseKey in stationTemplate.Evses) {
1544 const evseId = convertToInt(evseKey);
52952bf8 1545 this.evses.set(evseId, {
fba11dc6 1546 connectors: buildConnectorsMap(
eb979012 1547 stationTemplate?.Evses[evseKey]?.Connectors,
ae25f265 1548 this.logPrefix(),
5edd8ba0 1549 this.templateFile,
ae25f265
JB
1550 ),
1551 availability: AvailabilityType.Operative,
1552 });
e1d9a0f4 1553 initializeConnectorsMapStatus(this.evses.get(evseId)!.connectors, this.logPrefix());
ae25f265 1554 }
52952bf8 1555 this.saveEvsesStatus();
ae25f265
JB
1556 } else {
1557 logger.warn(
1558 `${this.logPrefix()} Charging station information from template ${
04b1261c 1559 this.templateFile
5edd8ba0 1560 } with no evses configuration defined, cannot create evses`,
04b1261c 1561 );
2585c6e9
JB
1562 }
1563 }
513db108
JB
1564 } else {
1565 logger.warn(
1566 `${this.logPrefix()} Charging station information from template ${
1567 this.templateFile
5edd8ba0 1568 } with no evses configuration defined, using already defined evses`,
513db108 1569 );
2585c6e9
JB
1570 }
1571 }
1572
551e477c
JB
1573 private getConfigurationFromFile(): ChargingStationConfiguration | undefined {
1574 let configuration: ChargingStationConfiguration | undefined;
9bf0ef23 1575 if (isNotEmptyString(this.configurationFile) && existsSync(this.configurationFile)) {
073bd098 1576 try {
57adbebc
JB
1577 if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) {
1578 configuration = this.sharedLRUCache.getChargingStationConfiguration(
5edd8ba0 1579 this.configurationFileHash,
57adbebc 1580 );
7c72977b
JB
1581 } else {
1582 const measureId = `${FileType.ChargingStationConfiguration} read`;
1583 const beginId = PerformanceStatistics.beginMeasure(measureId);
1584 configuration = JSON.parse(
5edd8ba0 1585 readFileSync(this.configurationFile, 'utf8'),
7c72977b
JB
1586 ) as ChargingStationConfiguration;
1587 PerformanceStatistics.endMeasure(measureId, beginId);
57adbebc 1588 this.sharedLRUCache.setChargingStationConfiguration(configuration);
e1d9a0f4 1589 this.configurationFileHash = configuration.configurationHash!;
7c72977b 1590 }
073bd098 1591 } catch (error) {
fa5995d6 1592 handleFileException(
073bd098 1593 this.configurationFile,
7164966d
JB
1594 FileType.ChargingStationConfiguration,
1595 error as NodeJS.ErrnoException,
5edd8ba0 1596 this.logPrefix(),
073bd098
JB
1597 );
1598 }
1599 }
1600 return configuration;
1601 }
1602
cb60061f 1603 private saveAutomaticTransactionGeneratorConfiguration(): void {
5398cecf 1604 if (this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration === true) {
5ced7e80
JB
1605 this.saveConfiguration();
1606 }
ac7f79af
JB
1607 }
1608
52952bf8 1609 private saveConnectorsStatus() {
7446de3b 1610 this.saveConfiguration();
52952bf8
JB
1611 }
1612
1613 private saveEvsesStatus() {
7446de3b 1614 this.saveConfiguration();
52952bf8
JB
1615 }
1616
179ed367 1617 private saveConfiguration(): void {
9bf0ef23 1618 if (isNotEmptyString(this.configurationFile)) {
2484ac1e 1619 try {
d972af76
JB
1620 if (!existsSync(dirname(this.configurationFile))) {
1621 mkdirSync(dirname(this.configurationFile), { recursive: true });
073bd098 1622 }
ae8ceef3
JB
1623 let configurationData: ChargingStationConfiguration = this.getConfigurationFromFile()
1624 ? cloneObject<ChargingStationConfiguration>(this.getConfigurationFromFile()!)
1625 : {};
5398cecf 1626 if (this.stationInfo?.stationInfoPersistentConfiguration === true && this.stationInfo) {
52952bf8 1627 configurationData.stationInfo = this.stationInfo;
5ced7e80
JB
1628 } else {
1629 delete configurationData.stationInfo;
52952bf8 1630 }
5398cecf
JB
1631 if (
1632 this.stationInfo?.ocppPersistentConfiguration === true &&
1633 this.ocppConfiguration?.configurationKey
1634 ) {
52952bf8 1635 configurationData.configurationKey = this.ocppConfiguration.configurationKey;
5ced7e80
JB
1636 } else {
1637 delete configurationData.configurationKey;
52952bf8 1638 }
179ed367
JB
1639 configurationData = merge<ChargingStationConfiguration>(
1640 configurationData,
5edd8ba0 1641 buildChargingStationAutomaticTransactionGeneratorConfiguration(this),
179ed367 1642 );
5ced7e80 1643 if (
5398cecf 1644 !this.stationInfo?.automaticTransactionGeneratorPersistentConfiguration ||
5ced7e80
JB
1645 !this.getAutomaticTransactionGeneratorConfiguration()
1646 ) {
1647 delete configurationData.automaticTransactionGenerator;
1648 }
b1bbdae5 1649 if (this.connectors.size > 0) {
179ed367 1650 configurationData.connectorsStatus = buildConnectorsStatus(this);
5ced7e80
JB
1651 } else {
1652 delete configurationData.connectorsStatus;
52952bf8 1653 }
b1bbdae5 1654 if (this.evses.size > 0) {
179ed367 1655 configurationData.evsesStatus = buildEvsesStatus(this);
5ced7e80
JB
1656 } else {
1657 delete configurationData.evsesStatus;
52952bf8 1658 }
7c72977b 1659 delete configurationData.configurationHash;
d972af76 1660 const configurationHash = createHash(Constants.DEFAULT_HASH_ALGORITHM)
5ced7e80
JB
1661 .update(
1662 JSON.stringify({
1663 stationInfo: configurationData.stationInfo,
1664 configurationKey: configurationData.configurationKey,
1665 automaticTransactionGenerator: configurationData.automaticTransactionGenerator,
8ab96efb
JB
1666 ...(this.connectors.size > 0 && {
1667 connectorsStatus: configurationData.connectorsStatus,
1668 }),
1669 ...(this.evses.size > 0 && { evsesStatus: configurationData.evsesStatus }),
5edd8ba0 1670 } as ChargingStationConfiguration),
5ced7e80 1671 )
7c72977b
JB
1672 .digest('hex');
1673 if (this.configurationFileHash !== configurationHash) {
0ebf7c2e
JB
1674 AsyncLock.runExclusive(AsyncLockType.configuration, () => {
1675 configurationData.configurationHash = configurationHash;
1676 const measureId = `${FileType.ChargingStationConfiguration} write`;
1677 const beginId = PerformanceStatistics.beginMeasure(measureId);
1678 writeFileSync(
1679 this.configurationFile,
4ed03b6e 1680 JSON.stringify(configurationData, undefined, 2),
0ebf7c2e
JB
1681 'utf8',
1682 );
1683 PerformanceStatistics.endMeasure(measureId, beginId);
1684 this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
1685 this.sharedLRUCache.setChargingStationConfiguration(configurationData);
1686 this.configurationFileHash = configurationHash;
1687 }).catch((error) => {
1688 handleFileException(
1689 this.configurationFile,
1690 FileType.ChargingStationConfiguration,
1691 error as NodeJS.ErrnoException,
1692 this.logPrefix(),
1693 );
1694 });
7c72977b
JB
1695 } else {
1696 logger.debug(
1697 `${this.logPrefix()} Not saving unchanged charging station configuration file ${
1698 this.configurationFile
5edd8ba0 1699 }`,
7c72977b 1700 );
2484ac1e 1701 }
2484ac1e 1702 } catch (error) {
fa5995d6 1703 handleFileException(
2484ac1e 1704 this.configurationFile,
7164966d
JB
1705 FileType.ChargingStationConfiguration,
1706 error as NodeJS.ErrnoException,
5edd8ba0 1707 this.logPrefix(),
073bd098
JB
1708 );
1709 }
2484ac1e
JB
1710 } else {
1711 logger.error(
5edd8ba0 1712 `${this.logPrefix()} Trying to save charging station configuration to undefined configuration file`,
2484ac1e 1713 );
073bd098
JB
1714 }
1715 }
1716
551e477c
JB
1717 private getOcppConfigurationFromTemplate(): ChargingStationOcppConfiguration | undefined {
1718 return this.getTemplateFromFile()?.Configuration;
2484ac1e
JB
1719 }
1720
551e477c 1721 private getOcppConfigurationFromFile(): ChargingStationOcppConfiguration | undefined {
60655b26 1722 const configurationKey = this.getConfigurationFromFile()?.configurationKey;
5398cecf 1723 if (this.stationInfo?.ocppPersistentConfiguration === true && configurationKey) {
60655b26 1724 return { configurationKey };
648512ce 1725 }
60655b26 1726 return undefined;
7dde0b73
JB
1727 }
1728
551e477c
JB
1729 private getOcppConfiguration(): ChargingStationOcppConfiguration | undefined {
1730 let ocppConfiguration: ChargingStationOcppConfiguration | undefined =
72092cfc 1731 this.getOcppConfigurationFromFile();
2484ac1e
JB
1732 if (!ocppConfiguration) {
1733 ocppConfiguration = this.getOcppConfigurationFromTemplate();
1734 }
1735 return ocppConfiguration;
1736 }
1737
c0560973 1738 private async onOpen(): Promise<void> {
56eb297e 1739 if (this.isWebSocketConnectionOpened() === true) {
5144f4d1 1740 logger.info(
5edd8ba0 1741 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`,
5144f4d1 1742 );
f23be6aa 1743 let registrationRetryCount = 0;
ed6cfcff 1744 if (this.isRegistered() === false) {
5144f4d1 1745 // Send BootNotification
5144f4d1 1746 do {
f7f98c68 1747 this.bootNotificationResponse = await this.ocppRequestService.requestHandler<
5144f4d1
JB
1748 BootNotificationRequest,
1749 BootNotificationResponse
8bfbc743
JB
1750 >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
1751 skipBufferingOnError: true,
1752 });
ed6cfcff 1753 if (this.isRegistered() === false) {
5398cecf 1754 this.stationInfo?.registrationMaxRetries !== -1 && ++registrationRetryCount;
9bf0ef23 1755 await sleep(
1895299d 1756 this?.bootNotificationResponse?.interval
be4c6702 1757 ? secondsToMilliseconds(this.bootNotificationResponse.interval)
5edd8ba0 1758 : Constants.DEFAULT_BOOT_NOTIFICATION_INTERVAL,
5144f4d1
JB
1759 );
1760 }
1761 } while (
ed6cfcff 1762 this.isRegistered() === false &&
5398cecf 1763 (registrationRetryCount <= this.stationInfo.registrationMaxRetries! ||
9a77cc07 1764 this.stationInfo?.registrationMaxRetries === -1)
5144f4d1
JB
1765 );
1766 }
ed6cfcff 1767 if (this.isRegistered() === true) {
db54d2e0 1768 this.emit(ChargingStationEvents.registered);
f7c2994d 1769 if (this.inAcceptedState() === true) {
db54d2e0 1770 this.emit(ChargingStationEvents.accepted);
c0560973 1771 }
5144f4d1
JB
1772 } else {
1773 logger.error(
f23be6aa 1774 `${this.logPrefix()} Registration failure: maximum retries reached (${registrationRetryCount}) or retry disabled (${this
5398cecf 1775 .stationInfo?.registrationMaxRetries})`,
5144f4d1 1776 );
caad9d6b 1777 }
5144f4d1 1778 this.wsConnectionRestarted = false;
aa428a31 1779 this.autoReconnectRetryCount = 0;
db54d2e0 1780 this.emit(ChargingStationEvents.updated);
2e6f5966 1781 } else {
5144f4d1 1782 logger.warn(
5edd8ba0 1783 `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`,
e7aeea18 1784 );
2e6f5966 1785 }
2e6f5966
JB
1786 }
1787
ef7d8c21 1788 private async onClose(code: number, reason: Buffer): Promise<void> {
d09085e9 1789 switch (code) {
6c65a295
JB
1790 // Normal close
1791 case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
c0560973 1792 case WebSocketCloseEventStatusCode.CLOSE_NO_STATUS:
e7aeea18 1793 logger.info(
9bf0ef23 1794 `${this.logPrefix()} WebSocket normally closed with status '${getWebSocketCloseEventStatusString(
5edd8ba0
JB
1795 code,
1796 )}' and reason '${reason.toString()}'`,
e7aeea18 1797 );
c0560973
JB
1798 this.autoReconnectRetryCount = 0;
1799 break;
6c65a295
JB
1800 // Abnormal close
1801 default:
e7aeea18 1802 logger.error(
9bf0ef23 1803 `${this.logPrefix()} WebSocket abnormally closed with status '${getWebSocketCloseEventStatusString(
5edd8ba0
JB
1804 code,
1805 )}' and reason '${reason.toString()}'`,
e7aeea18 1806 );
56eb297e 1807 this.started === true && (await this.reconnect());
c0560973
JB
1808 break;
1809 }
db54d2e0 1810 this.emit(ChargingStationEvents.updated);
2e6f5966
JB
1811 }
1812
56d09fd7
JB
1813 private getCachedRequest(messageType: MessageType, messageId: string): CachedRequest | undefined {
1814 const cachedRequest = this.requests.get(messageId);
1815 if (Array.isArray(cachedRequest) === true) {
1816 return cachedRequest;
1817 }
1818 throw new OCPPError(
1819 ErrorType.PROTOCOL_ERROR,
1820 `Cached request for message id ${messageId} ${OCPPServiceUtils.getMessageTypeString(
5edd8ba0 1821 messageType,
56d09fd7
JB
1822 )} is not an array`,
1823 undefined,
5edd8ba0 1824 cachedRequest as JsonType,
56d09fd7
JB
1825 );
1826 }
1827
1828 private async handleIncomingMessage(request: IncomingRequest): Promise<void> {
1829 const [messageType, messageId, commandName, commandPayload] = request;
9a77cc07 1830 if (this.stationInfo?.enableStatistics === true) {
56d09fd7
JB
1831 this.performanceStatistics?.addRequestStatistic(commandName, messageType);
1832 }
1833 logger.debug(
1834 `${this.logPrefix()} << Command '${commandName}' received request payload: ${JSON.stringify(
5edd8ba0
JB
1835 request,
1836 )}`,
56d09fd7
JB
1837 );
1838 // Process the message
1839 await this.ocppIncomingRequestService.incomingRequestHandler(
1840 this,
1841 messageId,
1842 commandName,
5edd8ba0 1843 commandPayload,
56d09fd7
JB
1844 );
1845 }
1846
1847 private handleResponseMessage(response: Response): void {
1848 const [messageType, messageId, commandPayload] = response;
1849 if (this.requests.has(messageId) === false) {
1850 // Error
1851 throw new OCPPError(
1852 ErrorType.INTERNAL_ERROR,
1853 `Response for unknown message id ${messageId}`,
1854 undefined,
5edd8ba0 1855 commandPayload,
56d09fd7
JB
1856 );
1857 }
1858 // Respond
1859 const [responseCallback, , requestCommandName, requestPayload] = this.getCachedRequest(
1860 messageType,
5edd8ba0 1861 messageId,
e1d9a0f4 1862 )!;
56d09fd7
JB
1863 logger.debug(
1864 `${this.logPrefix()} << Command '${
1865 requestCommandName ?? Constants.UNKNOWN_COMMAND
5edd8ba0 1866 }' received response payload: ${JSON.stringify(response)}`,
56d09fd7
JB
1867 );
1868 responseCallback(commandPayload, requestPayload);
1869 }
1870
1871 private handleErrorMessage(errorResponse: ErrorResponse): void {
1872 const [messageType, messageId, errorType, errorMessage, errorDetails] = errorResponse;
1873 if (this.requests.has(messageId) === false) {
1874 // Error
1875 throw new OCPPError(
1876 ErrorType.INTERNAL_ERROR,
1877 `Error response for unknown message id ${messageId}`,
1878 undefined,
5edd8ba0 1879 { errorType, errorMessage, errorDetails },
56d09fd7
JB
1880 );
1881 }
e1d9a0f4 1882 const [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)!;
56d09fd7
JB
1883 logger.debug(
1884 `${this.logPrefix()} << Command '${
1885 requestCommandName ?? Constants.UNKNOWN_COMMAND
5edd8ba0 1886 }' received error response payload: ${JSON.stringify(errorResponse)}`,
56d09fd7
JB
1887 );
1888 errorCallback(new OCPPError(errorType, errorMessage, requestCommandName, errorDetails));
1889 }
1890
ef7d8c21 1891 private async onMessage(data: RawData): Promise<void> {
e1d9a0f4
JB
1892 let request: IncomingRequest | Response | ErrorResponse | undefined;
1893 let messageType: number | undefined;
ded57f02 1894 let errorMsg: string;
c0560973 1895 try {
e1d9a0f4 1896 // eslint-disable-next-line @typescript-eslint/no-base-to-string
56d09fd7 1897 request = JSON.parse(data.toString()) as IncomingRequest | Response | ErrorResponse;
53e5fd67 1898 if (Array.isArray(request) === true) {
56d09fd7 1899 [messageType] = request;
b3ec7bc1
JB
1900 // Check the type of message
1901 switch (messageType) {
1902 // Incoming Message
1903 case MessageType.CALL_MESSAGE:
56d09fd7 1904 await this.handleIncomingMessage(request as IncomingRequest);
b3ec7bc1 1905 break;
56d09fd7 1906 // Response Message
b3ec7bc1 1907 case MessageType.CALL_RESULT_MESSAGE:
56d09fd7 1908 this.handleResponseMessage(request as Response);
a2d1c0f1
JB
1909 break;
1910 // Error Message
1911 case MessageType.CALL_ERROR_MESSAGE:
56d09fd7 1912 this.handleErrorMessage(request as ErrorResponse);
b3ec7bc1 1913 break;
56d09fd7 1914 // Unknown Message
b3ec7bc1
JB
1915 default:
1916 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
ded57f02
JB
1917 errorMsg = `Wrong message type ${messageType}`;
1918 logger.error(`${this.logPrefix()} ${errorMsg}`);
1919 throw new OCPPError(ErrorType.PROTOCOL_ERROR, errorMsg);
b3ec7bc1 1920 }
db54d2e0 1921 this.emit(ChargingStationEvents.updated);
47e22477 1922 } else {
e1d9a0f4
JB
1923 throw new OCPPError(
1924 ErrorType.PROTOCOL_ERROR,
1925 'Incoming message is not an array',
1926 undefined,
1927 {
1928 request,
1929 },
1930 );
47e22477 1931 }
c0560973 1932 } catch (error) {
e1d9a0f4
JB
1933 let commandName: IncomingRequestCommand | undefined;
1934 let requestCommandName: RequestCommand | IncomingRequestCommand | undefined;
56d09fd7 1935 let errorCallback: ErrorCallback;
e1d9a0f4 1936 const [, messageId] = request!;
13701f69
JB
1937 switch (messageType) {
1938 case MessageType.CALL_MESSAGE:
56d09fd7 1939 [, , commandName] = request as IncomingRequest;
13701f69 1940 // Send error
56d09fd7 1941 await this.ocppRequestService.sendError(this, messageId, error as OCPPError, commandName);
13701f69
JB
1942 break;
1943 case MessageType.CALL_RESULT_MESSAGE:
1944 case MessageType.CALL_ERROR_MESSAGE:
56d09fd7 1945 if (this.requests.has(messageId) === true) {
e1d9a0f4 1946 [, errorCallback, requestCommandName] = this.getCachedRequest(messageType, messageId)!;
13701f69
JB
1947 // Reject the deferred promise in case of error at response handling (rejecting an already fulfilled promise is a no-op)
1948 errorCallback(error as OCPPError, false);
1949 } else {
1950 // Remove the request from the cache in case of error at response handling
1951 this.requests.delete(messageId);
1952 }
de4cb8b6 1953 break;
ba7965c4 1954 }
56d09fd7
JB
1955 if (error instanceof OCPPError === false) {
1956 logger.warn(
1957 `${this.logPrefix()} Error thrown at incoming OCPP command '${
1958 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
e1d9a0f4 1959 // eslint-disable-next-line @typescript-eslint/no-base-to-string
56d09fd7 1960 }' message '${data.toString()}' handling is not an OCPPError:`,
5edd8ba0 1961 error,
56d09fd7
JB
1962 );
1963 }
1964 logger.error(
1965 `${this.logPrefix()} Incoming OCPP command '${
1966 commandName ?? requestCommandName ?? Constants.UNKNOWN_COMMAND
e1d9a0f4 1967 // eslint-disable-next-line @typescript-eslint/no-base-to-string
56d09fd7
JB
1968 }' message '${data.toString()}'${
1969 messageType !== MessageType.CALL_MESSAGE
1970 ? ` matching cached request '${JSON.stringify(this.requests.get(messageId))}'`
1971 : ''
1972 } processing error:`,
5edd8ba0 1973 error,
56d09fd7 1974 );
c0560973 1975 }
2328be1e
JB
1976 }
1977
c0560973 1978 private onPing(): void {
44eb6026 1979 logger.debug(`${this.logPrefix()} Received a WS ping (rfc6455) from the server`);
c0560973
JB
1980 }
1981
1982 private onPong(): void {
44eb6026 1983 logger.debug(`${this.logPrefix()} Received a WS pong (rfc6455) from the server`);
c0560973
JB
1984 }
1985
9534e74e 1986 private onError(error: WSError): void {
bcc9c3c0 1987 this.closeWSConnection();
44eb6026 1988 logger.error(`${this.logPrefix()} WebSocket error:`, error);
c0560973
JB
1989 }
1990
18bf8274 1991 private getEnergyActiveImportRegister(connectorStatus: ConnectorStatus, rounded = false): number {
5398cecf 1992 if (this.stationInfo?.meteringPerTransaction === true) {
07989fad 1993 return (
18bf8274 1994 (rounded === true
e1d9a0f4 1995 ? Math.round(connectorStatus.transactionEnergyActiveImportRegisterValue!)
07989fad
JB
1996 : connectorStatus?.transactionEnergyActiveImportRegisterValue) ?? 0
1997 );
1998 }
1999 return (
18bf8274 2000 (rounded === true
e1d9a0f4 2001 ? Math.round(connectorStatus.energyActiveImportRegisterValue!)
07989fad
JB
2002 : connectorStatus?.energyActiveImportRegisterValue) ?? 0
2003 );
2004 }
2005
cda5d0fb
JB
2006 private getUseConnectorId0(stationTemplate?: ChargingStationTemplate): boolean {
2007 return stationTemplate?.useConnectorId0 ?? true;
8bce55bf
JB
2008 }
2009
9ff486f4 2010 private async stopRunningTransactions(reason?: StopTransactionReason): Promise<void> {
28e78158 2011 if (this.hasEvses) {
3fa7f799
JB
2012 for (const [evseId, evseStatus] of this.evses) {
2013 if (evseId === 0) {
2014 continue;
2015 }
28e78158
JB
2016 for (const [connectorId, connectorStatus] of evseStatus.connectors) {
2017 if (connectorStatus.transactionStarted === true) {
2018 await this.stopTransactionOnConnector(connectorId, reason);
2019 }
2020 }
2021 }
2022 } else {
2023 for (const connectorId of this.connectors.keys()) {
2024 if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted === true) {
2025 await this.stopTransactionOnConnector(connectorId, reason);
2026 }
60ddad53
JB
2027 }
2028 }
2029 }
2030
1f761b9a 2031 // 0 for disabling
c72f6634 2032 private getConnectionTimeout(): number {
f2d5e3d9 2033 if (getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)) {
e7aeea18 2034 return (
f2d5e3d9
JB
2035 parseInt(getConfigurationKey(this, StandardParametersKey.ConnectionTimeOut)!.value!) ??
2036 Constants.DEFAULT_CONNECTION_TIMEOUT
e7aeea18 2037 );
291cb255 2038 }
291cb255 2039 return Constants.DEFAULT_CONNECTION_TIMEOUT;
3574dfd3
JB
2040 }
2041
c0560973 2042 private getPowerDivider(): number {
b1bbdae5 2043 let powerDivider = this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors();
be1e907c 2044 if (this.stationInfo?.powerSharedByConnectors === true) {
c0560973 2045 powerDivider = this.getNumberOfRunningTransactions();
6ecb15e4
JB
2046 }
2047 return powerDivider;
2048 }
2049
fa7bccf4
JB
2050 private getMaximumAmperage(stationInfo: ChargingStationInfo): number | undefined {
2051 const maximumPower = this.getMaximumPower(stationInfo);
2052 switch (this.getCurrentOutType(stationInfo)) {
cc6e8ab5
JB
2053 case CurrentType.AC:
2054 return ACElectricUtils.amperagePerPhaseFromPower(
fa7bccf4 2055 this.getNumberOfPhases(stationInfo),
b1bbdae5 2056 maximumPower / (this.hasEvses ? this.getNumberOfEvses() : this.getNumberOfConnectors()),
5edd8ba0 2057 this.getVoltageOut(stationInfo),
cc6e8ab5
JB
2058 );
2059 case CurrentType.DC:
fa7bccf4 2060 return DCElectricUtils.amperage(maximumPower, this.getVoltageOut(stationInfo));
cc6e8ab5
JB
2061 }
2062 }
2063
5398cecf
JB
2064 private getMaximumPower(stationInfo?: ChargingStationInfo): number {
2065 return (stationInfo ?? this.stationInfo).maximumPower!;
2066 }
2067
2068 private getCurrentOutType(stationInfo?: ChargingStationInfo): CurrentType {
73de8f02 2069 return (stationInfo ?? this.stationInfo).currentOutType ?? CurrentType.AC;
5398cecf
JB
2070 }
2071
2072 private getVoltageOut(stationInfo?: ChargingStationInfo): number {
2073 const defaultVoltageOut = getDefaultVoltageOut(
2074 this.getCurrentOutType(stationInfo),
2075 this.logPrefix(),
2076 this.templateFile,
2077 );
2078 return (stationInfo ?? this.stationInfo).voltageOut ?? defaultVoltageOut;
2079 }
2080
cc6e8ab5
JB
2081 private getAmperageLimitation(): number | undefined {
2082 if (
9bf0ef23 2083 isNotEmptyString(this.stationInfo?.amperageLimitationOcppKey) &&
f2d5e3d9 2084 getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!)
cc6e8ab5
JB
2085 ) {
2086 return (
9bf0ef23 2087 convertToInt(
f2d5e3d9 2088 getConfigurationKey(this, this.stationInfo.amperageLimitationOcppKey!)?.value,
fba11dc6 2089 ) / getAmperageLimitationUnitDivider(this.stationInfo)
cc6e8ab5
JB
2090 );
2091 }
2092 }
2093
c0560973 2094 private async startMessageSequence(): Promise<void> {
b7f9e41d 2095 if (this.stationInfo?.autoRegister === true) {
f7f98c68 2096 await this.ocppRequestService.requestHandler<
ef6fa3fb
JB
2097 BootNotificationRequest,
2098 BootNotificationResponse
8bfbc743
JB
2099 >(this, RequestCommand.BOOT_NOTIFICATION, this.bootNotificationRequest, {
2100 skipBufferingOnError: true,
2101 });
6114e6f1 2102 }
136c90ba 2103 // Start WebSocket ping
c0560973 2104 this.startWebSocketPing();
5ad8570f 2105 // Start heartbeat
c0560973 2106 this.startHeartbeat();
0a60c33c 2107 // Initialize connectors status
c3b83130
JB
2108 if (this.hasEvses) {
2109 for (const [evseId, evseStatus] of this.evses) {
4334db72
JB
2110 if (evseId > 0) {
2111 for (const [connectorId, connectorStatus] of evseStatus.connectors) {
fba11dc6 2112 const connectorBootStatus = getBootConnectorStatus(this, connectorId, connectorStatus);
4334db72
JB
2113 await OCPPServiceUtils.sendAndSetConnectorStatus(
2114 this,
2115 connectorId,
12f26d4a 2116 connectorBootStatus,
5edd8ba0 2117 evseId,
4334db72
JB
2118 );
2119 }
c3b83130 2120 }
4334db72
JB
2121 }
2122 } else {
2123 for (const connectorId of this.connectors.keys()) {
2124 if (connectorId > 0) {
fba11dc6 2125 const connectorBootStatus = getBootConnectorStatus(
c3b83130
JB
2126 this,
2127 connectorId,
e1d9a0f4 2128 this.getConnectorStatus(connectorId)!,
c3b83130
JB
2129 );
2130 await OCPPServiceUtils.sendAndSetConnectorStatus(this, connectorId, connectorBootStatus);
2131 }
2132 }
5ad8570f 2133 }
c9a4f9ea
JB
2134 if (this.stationInfo?.firmwareStatus === FirmwareStatus.Installing) {
2135 await this.ocppRequestService.requestHandler<
2136 FirmwareStatusNotificationRequest,
2137 FirmwareStatusNotificationResponse
2138 >(this, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, {
2139 status: FirmwareStatus.Installed,
2140 });
2141 this.stationInfo.firmwareStatus = FirmwareStatus.Installed;
c9a4f9ea 2142 }
3637ca2c 2143
0a60c33c 2144 // Start the ATG
61854f7c 2145 if (this.getAutomaticTransactionGeneratorConfiguration().enable === true) {
4f69be04 2146 this.startAutomaticTransactionGenerator();
fa7bccf4 2147 }
aa428a31 2148 this.wsConnectionRestarted === true && this.flushMessageBuffer();
fa7bccf4
JB
2149 }
2150
e7aeea18 2151 private async stopMessageSequence(
9ff486f4 2152 reason?: StopTransactionReason,
5398cecf 2153 stopTransactions = this.stationInfo?.stopTransactionsOnStopped,
e7aeea18 2154 ): Promise<void> {
136c90ba 2155 // Stop WebSocket ping
c0560973 2156 this.stopWebSocketPing();
79411696 2157 // Stop heartbeat
c0560973 2158 this.stopHeartbeat();
9ff486f4 2159 // Stop the ATG
b20eb107 2160 if (this.automaticTransactionGenerator?.started === true) {
9ff486f4 2161 this.stopAutomaticTransactionGenerator();
79411696 2162 }
3e888c65
JB
2163 // Stop ongoing transactions
2164 stopTransactions && (await this.stopRunningTransactions(reason));
039211f9
JB
2165 if (this.hasEvses) {
2166 for (const [evseId, evseStatus] of this.evses) {
2167 if (evseId > 0) {
2168 for (const [connectorId, connectorStatus] of evseStatus.connectors) {
2169 await this.ocppRequestService.requestHandler<
2170 StatusNotificationRequest,
2171 StatusNotificationResponse
2172 >(
2173 this,
2174 RequestCommand.STATUS_NOTIFICATION,
2175 OCPPServiceUtils.buildStatusNotificationRequest(
2176 this,
2177 connectorId,
12f26d4a 2178 ConnectorStatusEnum.Unavailable,
5edd8ba0
JB
2179 evseId,
2180 ),
039211f9
JB
2181 );
2182 delete connectorStatus?.status;
2183 }
2184 }
2185 }
2186 } else {
2187 for (const connectorId of this.connectors.keys()) {
2188 if (connectorId > 0) {
2189 await this.ocppRequestService.requestHandler<
2190 StatusNotificationRequest,
2191 StatusNotificationResponse
2192 >(
6e939d9e 2193 this,
039211f9
JB
2194 RequestCommand.STATUS_NOTIFICATION,
2195 OCPPServiceUtils.buildStatusNotificationRequest(
2196 this,
2197 connectorId,
5edd8ba0
JB
2198 ConnectorStatusEnum.Unavailable,
2199 ),
039211f9
JB
2200 );
2201 delete this.getConnectorStatus(connectorId)?.status;
2202 }
45c0ae82
JB
2203 }
2204 }
79411696
JB
2205 }
2206
c0560973 2207 private startWebSocketPing(): void {
f2d5e3d9 2208 const webSocketPingInterval: number = getConfigurationKey(
17ac262c 2209 this,
5edd8ba0 2210 StandardParametersKey.WebSocketPingInterval,
e7aeea18 2211 )
f2d5e3d9 2212 ? convertToInt(getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value)
9cd3dfb0 2213 : 0;
ad2f27c3
JB
2214 if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) {
2215 this.webSocketPingSetInterval = setInterval(() => {
56eb297e 2216 if (this.isWebSocketConnectionOpened() === true) {
72092cfc 2217 this.wsConnection?.ping();
136c90ba 2218 }
be4c6702 2219 }, secondsToMilliseconds(webSocketPingInterval));
e7aeea18 2220 logger.info(
9bf0ef23 2221 `${this.logPrefix()} WebSocket ping started every ${formatDurationSeconds(
5edd8ba0
JB
2222 webSocketPingInterval,
2223 )}`,
e7aeea18 2224 );
ad2f27c3 2225 } else if (this.webSocketPingSetInterval) {
e7aeea18 2226 logger.info(
9bf0ef23 2227 `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds(
5edd8ba0
JB
2228 webSocketPingInterval,
2229 )}`,
e7aeea18 2230 );
136c90ba 2231 } else {
e7aeea18 2232 logger.error(
5edd8ba0 2233 `${this.logPrefix()} WebSocket ping interval set to ${webSocketPingInterval}, not starting the WebSocket ping`,
e7aeea18 2234 );
136c90ba
JB
2235 }
2236 }
2237
c0560973 2238 private stopWebSocketPing(): void {
ad2f27c3
JB
2239 if (this.webSocketPingSetInterval) {
2240 clearInterval(this.webSocketPingSetInterval);
dfe81c8f 2241 delete this.webSocketPingSetInterval;
136c90ba
JB
2242 }
2243 }
2244
1f5df42a 2245 private getConfiguredSupervisionUrl(): URL {
d5c3df49 2246 let configuredSupervisionUrl: string;
72092cfc 2247 const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls();
9bf0ef23 2248 if (isNotEmptyArray(supervisionUrls)) {
269de583 2249 let configuredSupervisionUrlIndex: number;
2dcfe98e 2250 switch (Configuration.getSupervisionUrlDistribution()) {
2dcfe98e 2251 case SupervisionUrlDistribution.RANDOM:
e1d9a0f4
JB
2252 configuredSupervisionUrlIndex = Math.floor(
2253 secureRandom() * (supervisionUrls as string[]).length,
2254 );
2dcfe98e 2255 break;
a52a6446 2256 case SupervisionUrlDistribution.ROUND_ROBIN:
c72f6634 2257 case SupervisionUrlDistribution.CHARGING_STATION_AFFINITY:
2dcfe98e 2258 default:
a52a6446 2259 Object.values(SupervisionUrlDistribution).includes(
e1d9a0f4 2260 Configuration.getSupervisionUrlDistribution()!,
a52a6446
JB
2261 ) === false &&
2262 logger.error(
e1d9a0f4 2263 // eslint-disable-next-line @typescript-eslint/no-base-to-string
a52a6446
JB
2264 `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${
2265 SupervisionUrlDistribution.CHARGING_STATION_AFFINITY
5edd8ba0 2266 }`,
a52a6446 2267 );
e1d9a0f4 2268 configuredSupervisionUrlIndex = (this.index - 1) % (supervisionUrls as string[]).length;
2dcfe98e 2269 break;
c0560973 2270 }
e1d9a0f4 2271 configuredSupervisionUrl = (supervisionUrls as string[])[configuredSupervisionUrlIndex];
d5c3df49
JB
2272 } else {
2273 configuredSupervisionUrl = supervisionUrls as string;
2274 }
9bf0ef23 2275 if (isNotEmptyString(configuredSupervisionUrl)) {
d5c3df49 2276 return new URL(configuredSupervisionUrl);
c0560973 2277 }
49c508b0 2278 const errorMsg = 'No supervision url(s) configured';
7f77d16f
JB
2279 logger.error(`${this.logPrefix()} ${errorMsg}`);
2280 throw new BaseError(`${errorMsg}`);
136c90ba
JB
2281 }
2282
c0560973 2283 private stopHeartbeat(): void {
ad2f27c3
JB
2284 if (this.heartbeatSetInterval) {
2285 clearInterval(this.heartbeatSetInterval);
dfe81c8f 2286 delete this.heartbeatSetInterval;
7dde0b73 2287 }
5ad8570f
JB
2288 }
2289
55516218 2290 private terminateWSConnection(): void {
56eb297e 2291 if (this.isWebSocketConnectionOpened() === true) {
72092cfc 2292 this.wsConnection?.terminate();
55516218
JB
2293 this.wsConnection = null;
2294 }
2295 }
2296
aa428a31 2297 private async reconnect(): Promise<void> {
7874b0b1
JB
2298 // Stop WebSocket ping
2299 this.stopWebSocketPing();
136c90ba 2300 // Stop heartbeat
c0560973 2301 this.stopHeartbeat();
5ad8570f 2302 // Stop the ATG if needed
ac7f79af 2303 if (this.getAutomaticTransactionGeneratorConfiguration().stopOnConnectionFailure === true) {
9ff486f4 2304 this.stopAutomaticTransactionGenerator();
ad2f27c3 2305 }
e7aeea18 2306 if (
5398cecf
JB
2307 this.autoReconnectRetryCount < this.stationInfo.autoReconnectMaxRetries! ||
2308 this.stationInfo?.autoReconnectMaxRetries === -1
e7aeea18 2309 ) {
1fe0632a 2310 ++this.autoReconnectRetryCount;
5398cecf
JB
2311 const reconnectDelay =
2312 this.stationInfo?.reconnectExponentialDelay === true
2313 ? exponentialDelay(this.autoReconnectRetryCount)
2314 : secondsToMilliseconds(this.getConnectionTimeout());
1e080116
JB
2315 const reconnectDelayWithdraw = 1000;
2316 const reconnectTimeout =
2317 reconnectDelay && reconnectDelay - reconnectDelayWithdraw > 0
2318 ? reconnectDelay - reconnectDelayWithdraw
2319 : 0;
e7aeea18 2320 logger.error(
9bf0ef23 2321 `${this.logPrefix()} WebSocket connection retry in ${roundTo(
e7aeea18 2322 reconnectDelay,
5edd8ba0
JB
2323 2,
2324 )}ms, timeout ${reconnectTimeout}ms`,
e7aeea18 2325 );
9bf0ef23 2326 await sleep(reconnectDelay);
e7aeea18 2327 logger.error(
5edd8ba0 2328 `${this.logPrefix()} WebSocket connection retry #${this.autoReconnectRetryCount.toString()}`,
e7aeea18
JB
2329 );
2330 this.openWSConnection(
59b6ed8d 2331 {
59b6ed8d
JB
2332 handshakeTimeout: reconnectTimeout,
2333 },
5edd8ba0 2334 { closeOpened: true },
e7aeea18 2335 );
265e4266 2336 this.wsConnectionRestarted = true;
5398cecf 2337 } else if (this.stationInfo?.autoReconnectMaxRetries !== -1) {
e7aeea18 2338 logger.error(
d56ea27c 2339 `${this.logPrefix()} WebSocket connection retries failure: maximum retries reached (${
e7aeea18 2340 this.autoReconnectRetryCount
5398cecf 2341 }) or retries disabled (${this.stationInfo?.autoReconnectMaxRetries})`,
e7aeea18 2342 );
5ad8570f
JB
2343 }
2344 }
7dde0b73 2345}