From: Jérôme Benoit Date: Fri, 24 Nov 2023 00:00:45 +0000 (+0100) Subject: feat: add message buffer flush interval X-Git-Tag: v1.2.26~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1cee0015ac704be480535c7228103525cc3c8cc2;p=e-mobility-charging-stations-simulator.git feat: add message buffer flush interval Signed-off-by: Jérôme Benoit --- diff --git a/build-requirements.js b/build-requirements.js index 839be558..632e7a34 100644 --- a/build-requirements.js +++ b/build-requirements.js @@ -6,7 +6,7 @@ import { version, exit } from 'node:process'; /** * Check if the current node version match the required engines version. */ -export function checkNodeVersion() { +export const checkNodeVersion = () => { const enginesNodeVersion = packageJson.engines.node; if (semVer.satisfies(version, enginesNodeVersion) === false) { console.error( @@ -17,6 +17,6 @@ export function checkNodeVersion() { // eslint-disable-next-line n/no-process-exit exit(1); } -} +}; checkNodeVersion(); diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 6fa98484..0cd8dba9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -37,11 +37,9 @@ import { getMaxNumberOfEvses, getNumberOfReservableConnectors, getPhaseRotationValue, - hasFeatureProfile, hasReservationExpired, initializeConnectorsMapStatus, propagateSerialNumber, - removeExpiredReservations, stationTemplateToStationInfo, warnTemplateKeysDeprecation, } from './Helpers'; @@ -189,7 +187,7 @@ export class ChargingStation extends EventEmitter { private readonly sharedLRUCache: SharedLRUCache; private webSocketPingSetInterval?: NodeJS.Timeout; private readonly chargingStationWorkerBroadcastChannel: ChargingStationWorkerBroadcastChannel; - private reservationExpirationSetInterval?: NodeJS.Timeout; + private flushMessageBufferSetInterval?: NodeJS.Timeout; constructor(index: number, templateFile: string) { super(); @@ -618,9 +616,6 @@ export class ChargingStation extends EventEmitter { if (this.stationInfo?.enableStatistics === true) { this.performanceStatistics?.start(); } - if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) { - this.startReservationExpirationSetInterval(); - } this.openWSConnection(); // Monitor charging station template file this.templateFileWatcher = watchJsonFile( @@ -681,9 +676,6 @@ export class ChargingStation extends EventEmitter { if (this.stationInfo?.enableStatistics === true) { this.performanceStatistics?.stop(); } - if (hasFeatureProfile(this, SupportedFeatureProfiles.Reservation)) { - this.stopReservationExpirationSetInterval(); - } this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash); this.templateFileWatcher?.close(); this.sharedLRUCache.deleteChargingStationTemplate(this.templateFileHash); @@ -715,6 +707,17 @@ export class ChargingStation extends EventEmitter { public bufferMessage(message: string): void { this.messageBuffer.add(message); + if (this.flushMessageBufferSetInterval === undefined) { + this.flushMessageBufferSetInterval = setInterval(() => { + if (this.isWebSocketConnectionOpened() === true && this.inAcceptedState() === true) { + this.flushMessageBuffer(); + } + if (this.flushMessageBufferSetInterval !== undefined && this.messageBuffer.size === 0) { + clearInterval(this.flushMessageBufferSetInterval); + delete this.flushMessageBufferSetInterval; + } + }, Constants.DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL); + } } public openWSConnection( @@ -975,31 +978,6 @@ export class ChargingStation extends EventEmitter { return false; } - private startReservationExpirationSetInterval(customInterval?: number): void { - const interval = customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_INTERVAL; - if (interval > 0) { - logger.info( - `${this.logPrefix()} Reservation expiration date checks started every ${formatDurationMilliSeconds( - interval, - )}`, - ); - this.reservationExpirationSetInterval = setInterval((): void => { - removeExpiredReservations(this).catch(Constants.EMPTY_FUNCTION); - }, interval); - } - } - - private stopReservationExpirationSetInterval(): void { - if (!isNullOrUndefined(this.reservationExpirationSetInterval)) { - clearInterval(this.reservationExpirationSetInterval); - } - } - - // private restartReservationExpiryDateSetInterval(): void { - // this.stopReservationExpirationSetInterval(); - // this.startReservationExpirationSetInterval(); - // } - private getNumberOfReservableConnectors(): number { let numberOfReservableConnectors = 0; if (this.hasEvses) { @@ -2214,7 +2192,7 @@ export class ChargingStation extends EventEmitter { getConfigurationKey(this, StandardParametersKey.WebSocketPingInterval)?.value, ) : 0; - if (webSocketPingInterval > 0 && !this.webSocketPingSetInterval) { + if (webSocketPingInterval > 0 && this.webSocketPingSetInterval === undefined) { this.webSocketPingSetInterval = setInterval(() => { if (this.isWebSocketConnectionOpened() === true) { this.wsConnection?.ping(); @@ -2225,7 +2203,7 @@ export class ChargingStation extends EventEmitter { webSocketPingInterval, )}`, ); - } else if (this.webSocketPingSetInterval) { + } else if (this.webSocketPingSetInterval !== undefined) { logger.info( `${this.logPrefix()} WebSocket ping already started every ${formatDurationSeconds( webSocketPingInterval, @@ -2239,7 +2217,7 @@ export class ChargingStation extends EventEmitter { } private stopWebSocketPing(): void { - if (this.webSocketPingSetInterval) { + if (this.webSocketPingSetInterval !== undefined) { clearInterval(this.webSocketPingSetInterval); delete this.webSocketPingSetInterval; } diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index 966fd83a..a17b56a8 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -56,7 +56,7 @@ export class Constants { /* This is intentional */ }); - static readonly DEFAULT_RESERVATION_EXPIRATION_INTERVAL = 60000; // Ms + static readonly DEFAULT_MESSAGE_BUFFER_FLUSH_INTERVAL = 60000; // Ms private constructor() { // This is intentional